aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/pktio/tap.c
blob: c61e05575252c851adfec40fe317ed3742dd577e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* Copyright (c) 2015, Ilya Maximets <i.maximets@samsung.com>
 * Copyright (c) 2021-2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * TAP pktio type
 *
 * This file provides a pktio interface that allows for creating and
 * send/receive packets through TAP interface. It is intended for use
 * as a simple conventional communication method between applications
 * that use kernel network stack (ping, ssh, iperf, etc.) and ODP
 * applications for the purpose of functional testing.
 *
 * To use this interface the name passed to odp_pktio_open() must begin
 * with "tap:" and be in the format:
 *
 * tap:iface
 *
 *   iface   the name of TAP device to be created.
 *
 * TUN/TAP kernel module should be loaded to use this pktio.
 * There should be no device named 'iface' in the system.
 * The total length of the 'iface' is limited by IF_NAMESIZE.
 */

#include <odp_posix_extensions.h>

#include <odp/api/debug.h>
#include <odp/api/hints.h>
#include <odp/api/packet_io.h>
#include <odp/api/random.h>
#include <odp/api/ticketlock.h>

#include <odp/api/plat/packet_inlines.h>

#include <odp_parse_internal.h>
#include <odp_debug_internal.h>
#include <odp_socket_common.h>
#include <odp_packet_internal.h>
#include <odp_packet_io_internal.h>
#include <odp_classification_internal.h>
#include <odp_errno_define.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_tun.h>

typedef struct {
	int fd;				/**< file descriptor for tap interface*/
	int skfd;			/**< socket descriptor */
	uint32_t mtu;			/**< cached mtu */
	uint32_t mtu_max;		/**< maximum supported MTU value */
	unsigned char if_mac[ETH_ALEN];	/**< MAC address of pktio side (not a
					     MAC address of kernel interface)*/
	odp_pool_t pool;		/**< pool to alloc packets from */
} pkt_tap_t;

ODP_STATIC_ASSERT(PKTIO_PRIVATE_SIZE >= sizeof(pkt_tap_t),
		  "PKTIO_PRIVATE_SIZE too small");

static inline pkt_tap_t *pkt_priv(pktio_entry_t *pktio_entry)
{
	return (pkt_tap_t *)(uintptr_t)(pktio_entry->s.pkt_priv);
}

static int gen_random_mac(unsigned char *mac)
{
	mac[0] = 0x7a; /* not multicast and local assignment bit is set */
	if (odp_random_data(mac + 1, 5, ODP_RANDOM_BASIC) < 5) {
		ODP_ERR("odp_random_data failed.\n");
		return -1;
	}
	return 0;
}

static int mac_addr_set_fd(int fd, const char *name,
			   const unsigned char mac_dst[])
{
	struct ifreq ethreq;
	int ret;

	memset(&ethreq, 0, sizeof(ethreq));
	snprintf(ethreq.ifr_name, IF_NAMESIZE, "%s", name);

	ethreq.ifr_hwaddr.sa_family = AF_UNIX;
	memcpy(ethreq.ifr_hwaddr.sa_data, mac_dst, ETH_ALEN);

	ret = ioctl(fd, SIOCSIFHWADDR, &ethreq);
	if (ret != 0) {
		_odp_errno = errno;
		ODP_ERR("ioctl(SIOCSIFHWADDR): %s: \"%s\".\n", strerror(errno),
			ethreq.ifr_name);
		return -1;
	}

	return 0;
}

static int tap_pktio_open(odp_pktio_t id ODP_UNUSED,
			  pktio_entry_t *pktio_entry,
			  const char *devname, odp_pool_t pool)
{
	int fd, skfd, flags;
	uint32_t mtu;
	struct ifreq ifr;
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	if (strncmp(devname, "tap:", 4) != 0)
		return -1;

	/* Init pktio entry */
	memset(tap, 0, sizeof(*tap));
	tap->fd = -1;
	tap->skfd = -1;

	if (pool == ODP_POOL_INVALID)
		return -1;

	fd = open("/dev/net/tun", O_RDWR);
	if (fd < 0) {
		_odp_errno = errno;
		ODP_ERR("failed to open /dev/net/tun: %s\n", strerror(errno));
		return -1;
	}

	memset(&ifr, 0, sizeof(ifr));
	/* Flags: IFF_TUN   - TUN device (no Ethernet headers)
	 *        IFF_TAP   - TAP device
	 *
	 *        IFF_NO_PI - Do not provide packet information
	 */
	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
	snprintf(ifr.ifr_name, IF_NAMESIZE, "%s", devname + 4);

	if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
		_odp_errno = errno;
		ODP_ERR("%s: creating tap device failed: %s\n",
			ifr.ifr_name, strerror(errno));
		goto tap_err;
	}

	/* Set nonblocking mode on interface. */
	flags = fcntl(fd, F_GETFL, 0);
	if (flags < 0) {
		_odp_errno = errno;
		ODP_ERR("fcntl(F_GETFL) failed: %s\n", strerror(errno));
		goto tap_err;
	}

	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
		_odp_errno = errno;
		ODP_ERR("fcntl(F_SETFL) failed: %s\n", strerror(errno));
		goto tap_err;
	}

	if (gen_random_mac(tap->if_mac) < 0)
		goto tap_err;

	/* Create AF_INET socket for network interface related operations. */
	skfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (skfd < 0) {
		_odp_errno = errno;
		ODP_ERR("socket creation failed: %s\n", strerror(errno));
		goto tap_err;
	}

	mtu = _odp_mtu_get_fd(skfd, devname + 4);
	if (mtu == 0) {
		_odp_errno = errno;
		ODP_ERR("_odp_mtu_get_fd failed: %s\n", strerror(errno));
		goto sock_err;
	}
	tap->mtu_max = _ODP_SOCKET_MTU_MAX;
	if (mtu > tap->mtu_max)
		tap->mtu_max =  mtu;

	tap->fd = fd;
	tap->skfd = skfd;
	tap->mtu = mtu;
	tap->pool = pool;
	return 0;
sock_err:
	close(skfd);
tap_err:
	close(fd);
	ODP_ERR("Tap device alloc failed.\n");
	return -1;
}

static int tap_pktio_start(pktio_entry_t *pktio_entry)
{
	struct ifreq ifr;
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	odp_memset(&ifr, 0, sizeof(ifr));
	snprintf(ifr.ifr_name, IF_NAMESIZE, "%s",
		 (char *)pktio_entry->s.name + 4);

		/* Up interface by default. */
	if (ioctl(tap->skfd, SIOCGIFFLAGS, &ifr) < 0) {
		_odp_errno = errno;
		ODP_ERR("ioctl(SIOCGIFFLAGS) failed: %s\n", strerror(errno));
		goto sock_err;
	}

	ifr.ifr_flags |= IFF_UP;
	ifr.ifr_flags |= IFF_RUNNING;

	if (ioctl(tap->skfd, SIOCSIFFLAGS, &ifr) < 0) {
		_odp_errno = errno;
		ODP_ERR("failed to come up: %s\n", strerror(errno));
		goto sock_err;
	}

	return 0;
sock_err:
	ODP_ERR("Tap device open failed.\n");
	return -1;
}

static int tap_pktio_stop(pktio_entry_t *pktio_entry)
{
	struct ifreq ifr;
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	odp_memset(&ifr, 0, sizeof(ifr));
	snprintf(ifr.ifr_name, IF_NAMESIZE, "%s",
		 (char *)pktio_entry->s.name + 4);

		/* Up interface by default. */
	if (ioctl(tap->skfd, SIOCGIFFLAGS, &ifr) < 0) {
		_odp_errno = errno;
		ODP_ERR("ioctl(SIOCGIFFLAGS) failed: %s\n", strerror(errno));
		goto sock_err;
	}

	ifr.ifr_flags &= ~IFF_UP;
	ifr.ifr_flags &= ~IFF_RUNNING;

	if (ioctl(tap->skfd, SIOCSIFFLAGS, &ifr) < 0) {
		_odp_errno = errno;
		ODP_ERR("failed to come up: %s\n", strerror(errno));
		goto sock_err;
	}

	return 0;
sock_err:
	ODP_ERR("Tap device open failed.\n");
	return -1;
}

static int tap_pktio_close(pktio_entry_t *pktio_entry)
{
	int ret = 0;
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	if (tap->fd != -1 && close(tap->fd) != 0) {
		_odp_errno = errno;
		ODP_ERR("close(tap->fd): %s\n", strerror(errno));
		ret = -1;
	}

	if (tap->skfd != -1 && close(tap->skfd) != 0) {
		_odp_errno = errno;
		ODP_ERR("close(tap->skfd): %s\n", strerror(errno));
		ret = -1;
	}

	return ret;
}

static odp_packet_t pack_odp_pkt(pktio_entry_t *pktio_entry, const void *data,
				 unsigned int len, odp_time_t *ts)
{
	odp_packet_t pkt;
	odp_packet_hdr_t *pkt_hdr;
	int num;
	uint16_t frame_offset = pktio_entry->s.pktin_frame_offset;
	const odp_proto_layer_t layer = pktio_entry->s.parse_layer;
	const odp_pktin_config_opt_t opt = pktio_entry->s.config.pktin;

	num = _odp_packet_alloc_multi(pkt_priv(pktio_entry)->pool,
				      len + frame_offset, &pkt, 1);
	if (num != 1)
		return ODP_PACKET_INVALID;

	pkt_hdr = packet_hdr(pkt);

	if (frame_offset)
		pull_head(pkt_hdr, frame_offset);

	if (odp_packet_copy_from_mem(pkt, 0, len, data) < 0) {
		ODP_ERR("failed to copy packet data\n");
		odp_packet_free(pkt);
		return ODP_PACKET_INVALID;
	}

	if (layer) {
		if (_odp_packet_parse_common(pkt_hdr, data, len, len, layer,
					     opt) < 0) {
			odp_packet_free(pkt);
			return ODP_PACKET_INVALID;
		}

		if (pktio_cls_enabled(pktio_entry)) {
			odp_pool_t new_pool;

			if (_odp_cls_classify_packet(pktio_entry, data,
						     &new_pool, pkt_hdr)) {
				odp_packet_free(pkt);
				return ODP_PACKET_INVALID;
			}

			if (odp_unlikely(_odp_pktio_packet_to_pool(
				    &pkt, &pkt_hdr, new_pool))) {
				odp_packet_free(pkt);
				odp_atomic_inc_u64(
					&pktio_entry->s.stats_extra.in_discards);
				return ODP_PACKET_INVALID;
			}
		}
	}

	packet_set_ts(pkt_hdr, ts);
	pkt_hdr->input = pktio_entry->s.handle;

	return pkt;
}

static int tap_pktio_recv(pktio_entry_t *pktio_entry, int index ODP_UNUSED,
			  odp_packet_t pkts[], int num)
{
	pkt_tap_t *tap = pkt_priv(pktio_entry);
	ssize_t retval;
	int i;
	uint32_t mtu = tap->mtu;
	uint8_t buf[mtu];
	odp_time_t ts_val;
	odp_time_t *ts = NULL;
	int num_rx = 0;

	odp_ticketlock_lock(&pktio_entry->s.rxl);

	if (pktio_entry->s.config.pktin.bit.ts_all ||
	    pktio_entry->s.config.pktin.bit.ts_ptp)
		ts = &ts_val;

	for (i = 0; i < num; i++) {
		do {
			retval = read(tap->fd, buf, mtu);
		} while (retval < 0 && errno == EINTR);

		if (ts != NULL)
			ts_val = odp_time_global();

		if (retval < 0) {
			_odp_errno = errno;
			break;
		}

		pkts[num_rx] = pack_odp_pkt(pktio_entry, buf, retval, ts);
		if (pkts[num_rx] == ODP_PACKET_INVALID)
			break;
		num_rx++;
	}

	odp_ticketlock_unlock(&pktio_entry->s.rxl);

	return num_rx;
}

static int tap_pktio_send_lockless(pktio_entry_t *pktio_entry,
				   const odp_packet_t pkts[], int num)
{
	pkt_tap_t *tap = pkt_priv(pktio_entry);
	ssize_t retval;
	int i, n;
	uint32_t pkt_len;
	uint32_t mtu = tap->mtu;
	uint8_t tx_ts_enabled = _odp_pktio_tx_ts_enabled(pktio_entry);
	uint8_t buf[mtu];

	for (i = 0; i < num; i++) {
		pkt_len = odp_packet_len(pkts[i]);

		if (odp_unlikely(pkt_len > mtu)) {
			if (i == 0) {
				_odp_errno = EMSGSIZE;
				return -1;
			}
			break;
		}

		if (odp_packet_copy_to_mem(pkts[i], 0, pkt_len, buf) < 0) {
			ODP_ERR("failed to copy packet data\n");
			break;
		}

		do {
			retval = write(tap->fd, buf, pkt_len);
		} while (retval < 0 && errno == EINTR);

		if (retval < 0) {
			if (i == 0 && SOCK_ERR_REPORT(errno)) {
				_odp_errno = errno;
				ODP_ERR("write(): %s\n", strerror(errno));
				return -1;
			}
			break;
		} else if ((uint32_t)retval != pkt_len) {
			ODP_ERR("sent partial ethernet packet\n");
			if (i == 0) {
				_odp_errno = EMSGSIZE;
				return -1;
			}
			break;
		}

		if (tx_ts_enabled) {
			if (odp_unlikely(packet_hdr(pkts[i])->p.flags.ts_set))
				_odp_pktio_tx_ts_set(pktio_entry);
		}
	}

	for (n = 0; n < i; n++)
		odp_packet_free(pkts[n]);

	return i;
}

static int tap_pktio_send(pktio_entry_t *pktio_entry, int index ODP_UNUSED,
			  const odp_packet_t pkts[], int num)
{
	int ret;

	odp_ticketlock_lock(&pktio_entry->s.txl);

	ret = tap_pktio_send_lockless(pktio_entry, pkts, num);

	odp_ticketlock_unlock(&pktio_entry->s.txl);

	return ret;
}

static uint32_t tap_mtu_get(pktio_entry_t *pktio_entry)
{
	uint32_t ret;

	ret =  _odp_mtu_get_fd(pkt_priv(pktio_entry)->skfd,
			       pktio_entry->s.name + 4);
	if (ret > 0)
		pkt_priv(pktio_entry)->mtu = ret;

	return ret;
}

static int tap_mtu_set(pktio_entry_t *pktio_entry, uint32_t maxlen_input,
		       uint32_t maxlen_output ODP_UNUSED)
{
	pkt_tap_t *tap = pkt_priv(pktio_entry);
	int ret;

	ret = _odp_mtu_set_fd(tap->skfd, pktio_entry->s.name + 4, maxlen_input);
	if (ret)
		return ret;

	tap->mtu = maxlen_input;

	return 0;
}

static int tap_promisc_mode_set(pktio_entry_t *pktio_entry,
				odp_bool_t enable)
{
	return _odp_promisc_mode_set_fd(pkt_priv(pktio_entry)->skfd,
					pktio_entry->s.name + 4, enable);
}

static int tap_promisc_mode_get(pktio_entry_t *pktio_entry)
{
	return _odp_promisc_mode_get_fd(pkt_priv(pktio_entry)->skfd,
					pktio_entry->s.name + 4);
}

static int tap_mac_addr_get(pktio_entry_t *pktio_entry, void *mac_addr)
{
	memcpy(mac_addr, pkt_priv(pktio_entry)->if_mac, ETH_ALEN);
	return ETH_ALEN;
}

static int tap_mac_addr_set(pktio_entry_t *pktio_entry, const void *mac_addr)
{
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	memcpy(tap->if_mac, mac_addr, ETH_ALEN);

	return mac_addr_set_fd(tap->fd, (char *)pktio_entry->s.name + 4,
			  tap->if_mac);
}

static int tap_link_status(pktio_entry_t *pktio_entry)
{
	return _odp_link_status_fd(pkt_priv(pktio_entry)->skfd,
				   pktio_entry->s.name + 4);
}

static int tap_link_info(pktio_entry_t *pktio_entry, odp_pktio_link_info_t *info)
{
	return _odp_link_info_fd(pkt_priv(pktio_entry)->skfd, pktio_entry->s.name + 4, info);
}

static int tap_capability(pktio_entry_t *pktio_entry ODP_UNUSED,
			  odp_pktio_capability_t *capa)
{
	pkt_tap_t *tap = pkt_priv(pktio_entry);

	memset(capa, 0, sizeof(odp_pktio_capability_t));

	capa->max_input_queues  = 1;
	capa->max_output_queues = 1;
	capa->set_op.op.promisc_mode = 1;
	capa->set_op.op.mac_addr = 1;
	capa->set_op.op.maxlen = 1;

	capa->maxlen.equal = true;
	capa->maxlen.min_input = _ODP_SOCKET_MTU_MIN;
	capa->maxlen.max_input = tap->mtu_max;
	capa->maxlen.min_output = _ODP_SOCKET_MTU_MIN;
	capa->maxlen.max_output = tap->mtu_max;

	odp_pktio_config_init(&capa->config);
	capa->config.pktin.bit.ts_all = 1;
	capa->config.pktin.bit.ts_ptp = 1;

	capa->config.pktout.bit.ts_ena = 1;

	return 0;
}

const pktio_if_ops_t _odp_tap_pktio_ops = {
	.name = "tap",
	.print = NULL,
	.init_global = NULL,
	.init_local = NULL,
	.term = NULL,
	.open = tap_pktio_open,
	.close = tap_pktio_close,
	.start = tap_pktio_start,
	.stop = tap_pktio_stop,
	.recv = tap_pktio_recv,
	.send = tap_pktio_send,
	.maxlen_get = tap_mtu_get,
	.maxlen_set = tap_mtu_set,
	.promisc_mode_set = tap_promisc_mode_set,
	.promisc_mode_get = tap_promisc_mode_get,
	.mac_get = tap_mac_addr_get,
	.mac_set = tap_mac_addr_set,
	.link_status = tap_link_status,
	.link_info = tap_link_info,
	.capability = tap_capability,
	.pktio_ts_res = NULL,
	.pktio_ts_from_ns = NULL,
	.pktio_time = NULL,
	.config = NULL
};