xref: /f-stack/dpdk/drivers/net/tap/rte_eth_tap.c (revision 16d80a6d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 
5 #include <rte_atomic.h>
6 #include <rte_branch_prediction.h>
7 #include <rte_byteorder.h>
8 #include <rte_common.h>
9 #include <rte_mbuf.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_ethdev_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_bus_vdev.h>
14 #include <rte_kvargs.h>
15 #include <rte_net.h>
16 #include <rte_debug.h>
17 #include <rte_ip.h>
18 #include <rte_string_fns.h>
19 #include <rte_ethdev.h>
20 #include <rte_errno.h>
21 
22 #include <assert.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <sys/uio.h>
34 #include <unistd.h>
35 #include <arpa/inet.h>
36 #include <net/if.h>
37 #include <linux/if_tun.h>
38 #include <linux/if_ether.h>
39 #include <fcntl.h>
40 
41 #include <tap_rss.h>
42 #include <rte_eth_tap.h>
43 #include <tap_flow.h>
44 #include <tap_netlink.h>
45 #include <tap_tcmsgs.h>
46 
47 /* Linux based path to the TUN device */
48 #define TUN_TAP_DEV_PATH        "/dev/net/tun"
49 #define DEFAULT_TAP_NAME        "dtap"
50 #define DEFAULT_TUN_NAME        "dtun"
51 
52 #define ETH_TAP_IFACE_ARG       "iface"
53 #define ETH_TAP_REMOTE_ARG      "remote"
54 #define ETH_TAP_MAC_ARG         "mac"
55 #define ETH_TAP_MAC_FIXED       "fixed"
56 
57 #define ETH_TAP_USR_MAC_FMT     "xx:xx:xx:xx:xx:xx"
58 #define ETH_TAP_CMP_MAC_FMT     "0123456789ABCDEFabcdef"
59 #define ETH_TAP_MAC_ARG_FMT     ETH_TAP_MAC_FIXED "|" ETH_TAP_USR_MAC_FMT
60 
61 #define TAP_GSO_MBUFS_PER_CORE	128
62 #define TAP_GSO_MBUF_SEG_SIZE	128
63 #define TAP_GSO_MBUF_CACHE_SIZE	4
64 #define TAP_GSO_MBUFS_NUM \
65 	(TAP_GSO_MBUFS_PER_CORE * TAP_GSO_MBUF_CACHE_SIZE)
66 
67 /* IPC key for queue fds sync */
68 #define TAP_MP_KEY "tap_mp_sync_queues"
69 
70 #define TAP_IOV_DEFAULT_MAX 1024
71 
72 static int tap_devices_count;
73 static struct rte_vdev_driver pmd_tap_drv;
74 static struct rte_vdev_driver pmd_tun_drv;
75 
76 static const char *valid_arguments[] = {
77 	ETH_TAP_IFACE_ARG,
78 	ETH_TAP_REMOTE_ARG,
79 	ETH_TAP_MAC_ARG,
80 	NULL
81 };
82 
83 static char tuntap_name[8];
84 
85 static volatile uint32_t tap_trigger;	/* Rx trigger */
86 
87 static struct rte_eth_link pmd_link = {
88 	.link_speed = ETH_SPEED_NUM_10G,
89 	.link_duplex = ETH_LINK_FULL_DUPLEX,
90 	.link_status = ETH_LINK_DOWN,
91 	.link_autoneg = ETH_LINK_FIXED,
92 };
93 
94 static void
95 tap_trigger_cb(int sig __rte_unused)
96 {
97 	/* Valid trigger values are nonzero */
98 	tap_trigger = (tap_trigger + 1) | 0x80000000;
99 }
100 
101 /* Specifies on what netdevices the ioctl should be applied */
102 enum ioctl_mode {
103 	LOCAL_AND_REMOTE,
104 	LOCAL_ONLY,
105 	REMOTE_ONLY,
106 };
107 
108 /* Message header to synchronize queues via IPC */
109 struct ipc_queues {
110 	char port_name[RTE_DEV_NAME_MAX_LEN];
111 	int rxq_count;
112 	int txq_count;
113 	/*
114 	 * The file descriptors are in the dedicated part
115 	 * of the Unix message to be translated by the kernel.
116 	 */
117 };
118 
119 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
120 
121 /**
122  * Tun/Tap allocation routine
123  *
124  * @param[in] pmd
125  *   Pointer to private structure.
126  *
127  * @param[in] is_keepalive
128  *   Keepalive flag
129  *
130  * @return
131  *   -1 on failure, fd on success
132  */
133 static int
134 tun_alloc(struct pmd_internals *pmd, int is_keepalive)
135 {
136 	struct ifreq ifr;
137 #ifdef IFF_MULTI_QUEUE
138 	unsigned int features;
139 #endif
140 	int fd;
141 
142 	memset(&ifr, 0, sizeof(struct ifreq));
143 
144 	/*
145 	 * Do not set IFF_NO_PI as packet information header will be needed
146 	 * to check if a received packet has been truncated.
147 	 */
148 	ifr.ifr_flags = (pmd->type == ETH_TUNTAP_TYPE_TAP) ?
149 		IFF_TAP : IFF_TUN | IFF_POINTOPOINT;
150 	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
151 
152 	fd = open(TUN_TAP_DEV_PATH, O_RDWR);
153 	if (fd < 0) {
154 		TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
155 		goto error;
156 	}
157 
158 #ifdef IFF_MULTI_QUEUE
159 	/* Grab the TUN features to verify we can work multi-queue */
160 	if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
161 		TAP_LOG(ERR, "%s unable to get TUN/TAP features",
162 			tuntap_name);
163 		goto error;
164 	}
165 	TAP_LOG(DEBUG, "%s Features %08x", tuntap_name, features);
166 
167 	if (features & IFF_MULTI_QUEUE) {
168 		TAP_LOG(DEBUG, "  Multi-queue support for %d queues",
169 			RTE_PMD_TAP_MAX_QUEUES);
170 		ifr.ifr_flags |= IFF_MULTI_QUEUE;
171 	} else
172 #endif
173 	{
174 		ifr.ifr_flags |= IFF_ONE_QUEUE;
175 		TAP_LOG(DEBUG, "  Single queue only support");
176 	}
177 
178 	/* Set the TUN/TAP configuration and set the name if needed */
179 	if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
180 		TAP_LOG(WARNING, "Unable to set TUNSETIFF for %s: %s",
181 			ifr.ifr_name, strerror(errno));
182 		goto error;
183 	}
184 
185 	/*
186 	 * Name passed to kernel might be wildcard like dtun%d
187 	 * and need to find the resulting device.
188 	 */
189 	TAP_LOG(DEBUG, "Device name is '%s'", ifr.ifr_name);
190 	strlcpy(pmd->name, ifr.ifr_name, RTE_ETH_NAME_MAX_LEN);
191 
192 	if (is_keepalive) {
193 		/*
194 		 * Detach the TUN/TAP keep-alive queue
195 		 * to avoid traffic through it
196 		 */
197 		ifr.ifr_flags = IFF_DETACH_QUEUE;
198 		if (ioctl(fd, TUNSETQUEUE, (void *)&ifr) < 0) {
199 			TAP_LOG(WARNING,
200 				"Unable to detach keep-alive queue for %s: %s",
201 				ifr.ifr_name, strerror(errno));
202 			goto error;
203 		}
204 	}
205 
206 	/* Always set the file descriptor to non-blocking */
207 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
208 		TAP_LOG(WARNING,
209 			"Unable to set %s to nonblocking: %s",
210 			ifr.ifr_name, strerror(errno));
211 		goto error;
212 	}
213 
214 	/* Set up trigger to optimize empty Rx bursts */
215 	errno = 0;
216 	do {
217 		struct sigaction sa;
218 		int flags = fcntl(fd, F_GETFL);
219 
220 		if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
221 			break;
222 		if (sa.sa_handler != tap_trigger_cb) {
223 			/*
224 			 * Make sure SIGIO is not already taken. This is done
225 			 * as late as possible to leave the application a
226 			 * chance to set up its own signal handler first.
227 			 */
228 			if (sa.sa_handler != SIG_IGN &&
229 			    sa.sa_handler != SIG_DFL) {
230 				errno = EBUSY;
231 				break;
232 			}
233 			sa = (struct sigaction){
234 				.sa_flags = SA_RESTART,
235 				.sa_handler = tap_trigger_cb,
236 			};
237 			if (sigaction(SIGIO, &sa, NULL) == -1)
238 				break;
239 		}
240 		/* Enable SIGIO on file descriptor */
241 		fcntl(fd, F_SETFL, flags | O_ASYNC);
242 		fcntl(fd, F_SETOWN, getpid());
243 	} while (0);
244 
245 	if (errno) {
246 		/* Disable trigger globally in case of error */
247 		tap_trigger = 0;
248 		TAP_LOG(WARNING, "Rx trigger disabled: %s",
249 			strerror(errno));
250 	}
251 
252 	return fd;
253 
254 error:
255 	if (fd >= 0)
256 		close(fd);
257 	return -1;
258 }
259 
260 static void
261 tap_verify_csum(struct rte_mbuf *mbuf)
262 {
263 	uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
264 	uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
265 	uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
266 	unsigned int l2_len = sizeof(struct ether_hdr);
267 	unsigned int l3_len;
268 	uint16_t cksum = 0;
269 	void *l3_hdr;
270 	void *l4_hdr;
271 
272 	if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
273 		l2_len += 4;
274 	else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
275 		l2_len += 8;
276 	/* Don't verify checksum for packets with discontinuous L2 header */
277 	if (unlikely(l2_len + sizeof(struct ipv4_hdr) >
278 		     rte_pktmbuf_data_len(mbuf)))
279 		return;
280 	l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
281 	if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
282 		struct ipv4_hdr *iph = l3_hdr;
283 
284 		/* ihl contains the number of 4-byte words in the header */
285 		l3_len = 4 * (iph->version_ihl & 0xf);
286 		if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
287 			return;
288 		/* check that the total length reported by header is not
289 		 * greater than the total received size
290 		 */
291 		if (l2_len + rte_be_to_cpu_16(iph->total_length) >
292 				rte_pktmbuf_data_len(mbuf))
293 			return;
294 
295 		cksum = ~rte_raw_cksum(iph, l3_len);
296 		mbuf->ol_flags |= cksum ?
297 			PKT_RX_IP_CKSUM_BAD :
298 			PKT_RX_IP_CKSUM_GOOD;
299 	} else if (l3 == RTE_PTYPE_L3_IPV6) {
300 		struct ipv6_hdr *iph = l3_hdr;
301 
302 		l3_len = sizeof(struct ipv6_hdr);
303 		/* check that the total length reported by header is not
304 		 * greater than the total received size
305 		 */
306 		if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) >
307 				rte_pktmbuf_data_len(mbuf))
308 			return;
309 	} else {
310 		/* IPv6 extensions are not supported */
311 		return;
312 	}
313 	if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
314 		l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
315 		/* Don't verify checksum for multi-segment packets. */
316 		if (mbuf->nb_segs > 1)
317 			return;
318 		if (l3 == RTE_PTYPE_L3_IPV4)
319 			cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
320 		else if (l3 == RTE_PTYPE_L3_IPV6)
321 			cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
322 		mbuf->ol_flags |= cksum ?
323 			PKT_RX_L4_CKSUM_BAD :
324 			PKT_RX_L4_CKSUM_GOOD;
325 	}
326 }
327 
328 static uint64_t
329 tap_rx_offload_get_port_capa(void)
330 {
331 	/*
332 	 * No specific port Rx offload capabilities.
333 	 */
334 	return 0;
335 }
336 
337 static uint64_t
338 tap_rx_offload_get_queue_capa(void)
339 {
340 	return DEV_RX_OFFLOAD_SCATTER |
341 	       DEV_RX_OFFLOAD_IPV4_CKSUM |
342 	       DEV_RX_OFFLOAD_UDP_CKSUM |
343 	       DEV_RX_OFFLOAD_TCP_CKSUM;
344 }
345 
346 /* Callback to handle the rx burst of packets to the correct interface and
347  * file descriptor(s) in a multi-queue setup.
348  */
349 static uint16_t
350 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
351 {
352 	struct rx_queue *rxq = queue;
353 	struct pmd_process_private *process_private;
354 	uint16_t num_rx;
355 	unsigned long num_rx_bytes = 0;
356 	uint32_t trigger = tap_trigger;
357 
358 	if (trigger == rxq->trigger_seen)
359 		return 0;
360 	if (trigger)
361 		rxq->trigger_seen = trigger;
362 	process_private = rte_eth_devices[rxq->in_port].process_private;
363 	rte_compiler_barrier();
364 	for (num_rx = 0; num_rx < nb_pkts; ) {
365 		struct rte_mbuf *mbuf = rxq->pool;
366 		struct rte_mbuf *seg = NULL;
367 		struct rte_mbuf *new_tail = NULL;
368 		uint16_t data_off = rte_pktmbuf_headroom(mbuf);
369 		int len;
370 
371 		len = readv(process_private->rxq_fds[rxq->queue_id],
372 			*rxq->iovecs,
373 			1 + (rxq->rxmode->offloads & DEV_RX_OFFLOAD_SCATTER ?
374 			     rxq->nb_rx_desc : 1));
375 		if (len < (int)sizeof(struct tun_pi))
376 			break;
377 
378 		/* Packet couldn't fit in the provided mbuf */
379 		if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
380 			rxq->stats.ierrors++;
381 			continue;
382 		}
383 
384 		len -= sizeof(struct tun_pi);
385 
386 		mbuf->pkt_len = len;
387 		mbuf->port = rxq->in_port;
388 		while (1) {
389 			struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
390 
391 			if (unlikely(!buf)) {
392 				rxq->stats.rx_nombuf++;
393 				/* No new buf has been allocated: do nothing */
394 				if (!new_tail || !seg)
395 					goto end;
396 
397 				seg->next = NULL;
398 				rte_pktmbuf_free(mbuf);
399 
400 				goto end;
401 			}
402 			seg = seg ? seg->next : mbuf;
403 			if (rxq->pool == mbuf)
404 				rxq->pool = buf;
405 			if (new_tail)
406 				new_tail->next = buf;
407 			new_tail = buf;
408 			new_tail->next = seg->next;
409 
410 			/* iovecs[0] is reserved for packet info (pi) */
411 			(*rxq->iovecs)[mbuf->nb_segs].iov_len =
412 				buf->buf_len - data_off;
413 			(*rxq->iovecs)[mbuf->nb_segs].iov_base =
414 				(char *)buf->buf_addr + data_off;
415 
416 			seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
417 			seg->data_off = data_off;
418 
419 			len -= seg->data_len;
420 			if (len <= 0)
421 				break;
422 			mbuf->nb_segs++;
423 			/* First segment has headroom, not the others */
424 			data_off = 0;
425 		}
426 		seg->next = NULL;
427 		mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
428 						      RTE_PTYPE_ALL_MASK);
429 		if (rxq->rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM)
430 			tap_verify_csum(mbuf);
431 
432 		/* account for the receive frame */
433 		bufs[num_rx++] = mbuf;
434 		num_rx_bytes += mbuf->pkt_len;
435 	}
436 end:
437 	rxq->stats.ipackets += num_rx;
438 	rxq->stats.ibytes += num_rx_bytes;
439 
440 	return num_rx;
441 }
442 
443 static uint64_t
444 tap_tx_offload_get_port_capa(void)
445 {
446 	/*
447 	 * No specific port Tx offload capabilities.
448 	 */
449 	return 0;
450 }
451 
452 static uint64_t
453 tap_tx_offload_get_queue_capa(void)
454 {
455 	return DEV_TX_OFFLOAD_MULTI_SEGS |
456 	       DEV_TX_OFFLOAD_IPV4_CKSUM |
457 	       DEV_TX_OFFLOAD_UDP_CKSUM |
458 	       DEV_TX_OFFLOAD_TCP_CKSUM |
459 	       DEV_TX_OFFLOAD_TCP_TSO;
460 }
461 
462 /* Finalize l4 checksum calculation */
463 static void
464 tap_tx_l4_cksum(uint16_t *l4_cksum, uint16_t l4_phdr_cksum,
465 		uint32_t l4_raw_cksum)
466 {
467 	if (l4_cksum) {
468 		uint32_t cksum;
469 
470 		cksum = __rte_raw_cksum_reduce(l4_raw_cksum);
471 		cksum += l4_phdr_cksum;
472 
473 		cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
474 		cksum = (~cksum) & 0xffff;
475 		if (cksum == 0)
476 			cksum = 0xffff;
477 		*l4_cksum = cksum;
478 	}
479 }
480 
481 /* Accumaulate L4 raw checksums */
482 static void
483 tap_tx_l4_add_rcksum(char *l4_data, unsigned int l4_len, uint16_t *l4_cksum,
484 			uint32_t *l4_raw_cksum)
485 {
486 	if (l4_cksum == NULL)
487 		return;
488 
489 	*l4_raw_cksum = __rte_raw_cksum(l4_data, l4_len, *l4_raw_cksum);
490 }
491 
492 /* L3 and L4 pseudo headers checksum offloads */
493 static void
494 tap_tx_l3_cksum(char *packet, uint64_t ol_flags, unsigned int l2_len,
495 		unsigned int l3_len, unsigned int l4_len, uint16_t **l4_cksum,
496 		uint16_t *l4_phdr_cksum, uint32_t *l4_raw_cksum)
497 {
498 	void *l3_hdr = packet + l2_len;
499 
500 	if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4)) {
501 		struct ipv4_hdr *iph = l3_hdr;
502 		uint16_t cksum;
503 
504 		iph->hdr_checksum = 0;
505 		cksum = rte_raw_cksum(iph, l3_len);
506 		iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum;
507 	}
508 	if (ol_flags & PKT_TX_L4_MASK) {
509 		void *l4_hdr;
510 
511 		l4_hdr = packet + l2_len + l3_len;
512 		if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM)
513 			*l4_cksum = &((struct udp_hdr *)l4_hdr)->dgram_cksum;
514 		else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM)
515 			*l4_cksum = &((struct tcp_hdr *)l4_hdr)->cksum;
516 		else
517 			return;
518 		**l4_cksum = 0;
519 		if (ol_flags & PKT_TX_IPV4)
520 			*l4_phdr_cksum = rte_ipv4_phdr_cksum(l3_hdr, 0);
521 		else
522 			*l4_phdr_cksum = rte_ipv6_phdr_cksum(l3_hdr, 0);
523 		*l4_raw_cksum = __rte_raw_cksum(l4_hdr, l4_len, 0);
524 	}
525 }
526 
527 static inline void
528 tap_write_mbufs(struct tx_queue *txq, uint16_t num_mbufs,
529 			struct rte_mbuf **pmbufs,
530 			uint16_t *num_packets, unsigned long *num_tx_bytes)
531 {
532 	int i;
533 	uint16_t l234_hlen;
534 	struct pmd_process_private *process_private;
535 
536 	process_private = rte_eth_devices[txq->out_port].process_private;
537 
538 	for (i = 0; i < num_mbufs; i++) {
539 		struct rte_mbuf *mbuf = pmbufs[i];
540 		struct iovec iovecs[mbuf->nb_segs + 2];
541 		struct tun_pi pi = { .flags = 0, .proto = 0x00 };
542 		struct rte_mbuf *seg = mbuf;
543 		char m_copy[mbuf->data_len];
544 		int proto;
545 		int n;
546 		int j;
547 		int k; /* current index in iovecs for copying segments */
548 		uint16_t seg_len; /* length of first segment */
549 		uint16_t nb_segs;
550 		uint16_t *l4_cksum; /* l4 checksum (pseudo header + payload) */
551 		uint32_t l4_raw_cksum = 0; /* TCP/UDP payload raw checksum */
552 		uint16_t l4_phdr_cksum = 0; /* TCP/UDP pseudo header checksum */
553 		uint16_t is_cksum = 0; /* in case cksum should be offloaded */
554 
555 		l4_cksum = NULL;
556 		if (txq->type == ETH_TUNTAP_TYPE_TUN) {
557 			/*
558 			 * TUN and TAP are created with IFF_NO_PI disabled.
559 			 * For TUN PMD this mandatory as fields are used by
560 			 * Kernel tun.c to determine whether its IP or non IP
561 			 * packets.
562 			 *
563 			 * The logic fetches the first byte of data from mbuf
564 			 * then compares whether its v4 or v6. If first byte
565 			 * is 4 or 6, then protocol field is updated.
566 			 */
567 			char *buff_data = rte_pktmbuf_mtod(seg, void *);
568 			proto = (*buff_data & 0xf0);
569 			pi.proto = (proto == 0x40) ?
570 				rte_cpu_to_be_16(ETHER_TYPE_IPv4) :
571 				((proto == 0x60) ?
572 					rte_cpu_to_be_16(ETHER_TYPE_IPv6) :
573 					0x00);
574 		}
575 
576 		k = 0;
577 		iovecs[k].iov_base = &pi;
578 		iovecs[k].iov_len = sizeof(pi);
579 		k++;
580 
581 		nb_segs = mbuf->nb_segs;
582 		if (txq->csum &&
583 		    ((mbuf->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4) ||
584 		     (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM ||
585 		     (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM))) {
586 			is_cksum = 1;
587 
588 			/* Support only packets with at least layer 4
589 			 * header included in the first segment
590 			 */
591 			seg_len = rte_pktmbuf_data_len(mbuf);
592 			l234_hlen = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len;
593 			if (seg_len < l234_hlen)
594 				break;
595 
596 			/* To change checksums, work on a * copy of l2, l3
597 			 * headers + l4 pseudo header
598 			 */
599 			rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *),
600 					l234_hlen);
601 			tap_tx_l3_cksum(m_copy, mbuf->ol_flags,
602 				       mbuf->l2_len, mbuf->l3_len, mbuf->l4_len,
603 				       &l4_cksum, &l4_phdr_cksum,
604 				       &l4_raw_cksum);
605 			iovecs[k].iov_base = m_copy;
606 			iovecs[k].iov_len = l234_hlen;
607 			k++;
608 
609 			/* Update next iovecs[] beyond l2, l3, l4 headers */
610 			if (seg_len > l234_hlen) {
611 				iovecs[k].iov_len = seg_len - l234_hlen;
612 				iovecs[k].iov_base =
613 					rte_pktmbuf_mtod(seg, char *) +
614 						l234_hlen;
615 				tap_tx_l4_add_rcksum(iovecs[k].iov_base,
616 					iovecs[k].iov_len, l4_cksum,
617 					&l4_raw_cksum);
618 				k++;
619 				nb_segs++;
620 			}
621 			seg = seg->next;
622 		}
623 
624 		for (j = k; j <= nb_segs; j++) {
625 			iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
626 			iovecs[j].iov_base = rte_pktmbuf_mtod(seg, void *);
627 			if (is_cksum)
628 				tap_tx_l4_add_rcksum(iovecs[j].iov_base,
629 					iovecs[j].iov_len, l4_cksum,
630 					&l4_raw_cksum);
631 			seg = seg->next;
632 		}
633 
634 		if (is_cksum)
635 			tap_tx_l4_cksum(l4_cksum, l4_phdr_cksum, l4_raw_cksum);
636 
637 		/* copy the tx frame data */
638 		n = writev(process_private->txq_fds[txq->queue_id], iovecs, j);
639 		if (n <= 0)
640 			break;
641 		(*num_packets)++;
642 		(*num_tx_bytes) += rte_pktmbuf_pkt_len(mbuf);
643 	}
644 }
645 
646 /* Callback to handle sending packets from the tap interface
647  */
648 static uint16_t
649 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
650 {
651 	struct tx_queue *txq = queue;
652 	uint16_t num_tx = 0;
653 	uint16_t num_packets = 0;
654 	unsigned long num_tx_bytes = 0;
655 	uint32_t max_size;
656 	int i;
657 
658 	if (unlikely(nb_pkts == 0))
659 		return 0;
660 
661 	struct rte_mbuf *gso_mbufs[MAX_GSO_MBUFS];
662 	max_size = *txq->mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN + 4);
663 	for (i = 0; i < nb_pkts; i++) {
664 		struct rte_mbuf *mbuf_in = bufs[num_tx];
665 		struct rte_mbuf **mbuf;
666 		uint16_t num_mbufs = 0;
667 		uint16_t tso_segsz = 0;
668 		int ret;
669 		uint16_t hdrs_len;
670 		int j;
671 		uint64_t tso;
672 
673 		tso = mbuf_in->ol_flags & PKT_TX_TCP_SEG;
674 		if (tso) {
675 			struct rte_gso_ctx *gso_ctx = &txq->gso_ctx;
676 
677 			assert(gso_ctx != NULL);
678 
679 			/* TCP segmentation implies TCP checksum offload */
680 			mbuf_in->ol_flags |= PKT_TX_TCP_CKSUM;
681 
682 			/* gso size is calculated without ETHER_CRC_LEN */
683 			hdrs_len = mbuf_in->l2_len + mbuf_in->l3_len +
684 					mbuf_in->l4_len;
685 			tso_segsz = mbuf_in->tso_segsz + hdrs_len;
686 			if (unlikely(tso_segsz == hdrs_len) ||
687 				tso_segsz > *txq->mtu) {
688 				txq->stats.errs++;
689 				break;
690 			}
691 			gso_ctx->gso_size = tso_segsz;
692 			ret = rte_gso_segment(mbuf_in, /* packet to segment */
693 				gso_ctx, /* gso control block */
694 				(struct rte_mbuf **)&gso_mbufs, /* out mbufs */
695 				RTE_DIM(gso_mbufs)); /* max tso mbufs */
696 
697 			/* ret contains the number of new created mbufs */
698 			if (ret < 0)
699 				break;
700 
701 			mbuf = gso_mbufs;
702 			num_mbufs = ret;
703 		} else {
704 			/* stats.errs will be incremented */
705 			if (rte_pktmbuf_pkt_len(mbuf_in) > max_size)
706 				break;
707 
708 			/* ret 0 indicates no new mbufs were created */
709 			ret = 0;
710 			mbuf = &mbuf_in;
711 			num_mbufs = 1;
712 		}
713 
714 		tap_write_mbufs(txq, num_mbufs, mbuf,
715 				&num_packets, &num_tx_bytes);
716 		num_tx++;
717 		/* free original mbuf */
718 		rte_pktmbuf_free(mbuf_in);
719 		/* free tso mbufs */
720 		for (j = 0; j < ret; j++)
721 			rte_pktmbuf_free(mbuf[j]);
722 	}
723 
724 	txq->stats.opackets += num_packets;
725 	txq->stats.errs += nb_pkts - num_tx;
726 	txq->stats.obytes += num_tx_bytes;
727 
728 	return num_packets;
729 }
730 
731 static const char *
732 tap_ioctl_req2str(unsigned long request)
733 {
734 	switch (request) {
735 	case SIOCSIFFLAGS:
736 		return "SIOCSIFFLAGS";
737 	case SIOCGIFFLAGS:
738 		return "SIOCGIFFLAGS";
739 	case SIOCGIFHWADDR:
740 		return "SIOCGIFHWADDR";
741 	case SIOCSIFHWADDR:
742 		return "SIOCSIFHWADDR";
743 	case SIOCSIFMTU:
744 		return "SIOCSIFMTU";
745 	}
746 	return "UNKNOWN";
747 }
748 
749 static int
750 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
751 	  struct ifreq *ifr, int set, enum ioctl_mode mode)
752 {
753 	short req_flags = ifr->ifr_flags;
754 	int remote = pmd->remote_if_index &&
755 		(mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
756 
757 	if (!pmd->remote_if_index && mode == REMOTE_ONLY)
758 		return 0;
759 	/*
760 	 * If there is a remote netdevice, apply ioctl on it, then apply it on
761 	 * the tap netdevice.
762 	 */
763 apply:
764 	if (remote)
765 		snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
766 	else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
767 		snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
768 	switch (request) {
769 	case SIOCSIFFLAGS:
770 		/* fetch current flags to leave other flags untouched */
771 		if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
772 			goto error;
773 		if (set)
774 			ifr->ifr_flags |= req_flags;
775 		else
776 			ifr->ifr_flags &= ~req_flags;
777 		break;
778 	case SIOCGIFFLAGS:
779 	case SIOCGIFHWADDR:
780 	case SIOCSIFHWADDR:
781 	case SIOCSIFMTU:
782 		break;
783 	default:
784 		RTE_LOG(WARNING, PMD, "%s: ioctl() called with wrong arg\n",
785 			pmd->name);
786 		return -EINVAL;
787 	}
788 	if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
789 		goto error;
790 	if (remote-- && mode == LOCAL_AND_REMOTE)
791 		goto apply;
792 	return 0;
793 
794 error:
795 	TAP_LOG(DEBUG, "%s(%s) failed: %s(%d)", ifr->ifr_name,
796 		tap_ioctl_req2str(request), strerror(errno), errno);
797 	return -errno;
798 }
799 
800 static int
801 tap_link_set_down(struct rte_eth_dev *dev)
802 {
803 	struct pmd_internals *pmd = dev->data->dev_private;
804 	struct ifreq ifr = { .ifr_flags = IFF_UP };
805 
806 	dev->data->dev_link.link_status = ETH_LINK_DOWN;
807 	return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
808 }
809 
810 static int
811 tap_link_set_up(struct rte_eth_dev *dev)
812 {
813 	struct pmd_internals *pmd = dev->data->dev_private;
814 	struct ifreq ifr = { .ifr_flags = IFF_UP };
815 
816 	dev->data->dev_link.link_status = ETH_LINK_UP;
817 	return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
818 }
819 
820 static int
821 tap_dev_start(struct rte_eth_dev *dev)
822 {
823 	int err, i;
824 
825 	err = tap_intr_handle_set(dev, 1);
826 	if (err)
827 		return err;
828 
829 	err = tap_link_set_up(dev);
830 	if (err)
831 		return err;
832 
833 	for (i = 0; i < dev->data->nb_tx_queues; i++)
834 		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
835 	for (i = 0; i < dev->data->nb_rx_queues; i++)
836 		dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
837 
838 	return err;
839 }
840 
841 /* This function gets called when the current port gets stopped.
842  */
843 static void
844 tap_dev_stop(struct rte_eth_dev *dev)
845 {
846 	int i;
847 
848 	for (i = 0; i < dev->data->nb_tx_queues; i++)
849 		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
850 	for (i = 0; i < dev->data->nb_rx_queues; i++)
851 		dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
852 
853 	tap_intr_handle_set(dev, 0);
854 	tap_link_set_down(dev);
855 }
856 
857 static int
858 tap_dev_configure(struct rte_eth_dev *dev)
859 {
860 	if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
861 		TAP_LOG(ERR,
862 			"%s: number of rx queues %d exceeds max num of queues %d",
863 			dev->device->name,
864 			dev->data->nb_rx_queues,
865 			RTE_PMD_TAP_MAX_QUEUES);
866 		return -1;
867 	}
868 	if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) {
869 		TAP_LOG(ERR,
870 			"%s: number of tx queues %d exceeds max num of queues %d",
871 			dev->device->name,
872 			dev->data->nb_tx_queues,
873 			RTE_PMD_TAP_MAX_QUEUES);
874 		return -1;
875 	}
876 
877 	TAP_LOG(INFO, "%s: %p: TX configured queues number: %u",
878 		dev->device->name, (void *)dev, dev->data->nb_tx_queues);
879 
880 	TAP_LOG(INFO, "%s: %p: RX configured queues number: %u",
881 		dev->device->name, (void *)dev, dev->data->nb_rx_queues);
882 
883 	return 0;
884 }
885 
886 static uint32_t
887 tap_dev_speed_capa(void)
888 {
889 	uint32_t speed = pmd_link.link_speed;
890 	uint32_t capa = 0;
891 
892 	if (speed >= ETH_SPEED_NUM_10M)
893 		capa |= ETH_LINK_SPEED_10M;
894 	if (speed >= ETH_SPEED_NUM_100M)
895 		capa |= ETH_LINK_SPEED_100M;
896 	if (speed >= ETH_SPEED_NUM_1G)
897 		capa |= ETH_LINK_SPEED_1G;
898 	if (speed >= ETH_SPEED_NUM_5G)
899 		capa |= ETH_LINK_SPEED_2_5G;
900 	if (speed >= ETH_SPEED_NUM_5G)
901 		capa |= ETH_LINK_SPEED_5G;
902 	if (speed >= ETH_SPEED_NUM_10G)
903 		capa |= ETH_LINK_SPEED_10G;
904 	if (speed >= ETH_SPEED_NUM_20G)
905 		capa |= ETH_LINK_SPEED_20G;
906 	if (speed >= ETH_SPEED_NUM_25G)
907 		capa |= ETH_LINK_SPEED_25G;
908 	if (speed >= ETH_SPEED_NUM_40G)
909 		capa |= ETH_LINK_SPEED_40G;
910 	if (speed >= ETH_SPEED_NUM_50G)
911 		capa |= ETH_LINK_SPEED_50G;
912 	if (speed >= ETH_SPEED_NUM_56G)
913 		capa |= ETH_LINK_SPEED_56G;
914 	if (speed >= ETH_SPEED_NUM_100G)
915 		capa |= ETH_LINK_SPEED_100G;
916 
917 	return capa;
918 }
919 
920 static void
921 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
922 {
923 	struct pmd_internals *internals = dev->data->dev_private;
924 
925 	dev_info->if_index = internals->if_index;
926 	dev_info->max_mac_addrs = 1;
927 	dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
928 	dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
929 	dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
930 	dev_info->min_rx_bufsize = 0;
931 	dev_info->speed_capa = tap_dev_speed_capa();
932 	dev_info->rx_queue_offload_capa = tap_rx_offload_get_queue_capa();
933 	dev_info->rx_offload_capa = tap_rx_offload_get_port_capa() |
934 				    dev_info->rx_queue_offload_capa;
935 	dev_info->tx_queue_offload_capa = tap_tx_offload_get_queue_capa();
936 	dev_info->tx_offload_capa = tap_tx_offload_get_port_capa() |
937 				    dev_info->tx_queue_offload_capa;
938 	dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE;
939 	/*
940 	 * limitation: TAP supports all of IP, UDP and TCP hash
941 	 * functions together and not in partial combinations
942 	 */
943 	dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK;
944 }
945 
946 static int
947 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
948 {
949 	unsigned int i, imax;
950 	unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
951 	unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
952 	unsigned long rx_nombuf = 0, ierrors = 0;
953 	const struct pmd_internals *pmd = dev->data->dev_private;
954 
955 	/* rx queue statistics */
956 	imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
957 		dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
958 	for (i = 0; i < imax; i++) {
959 		tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
960 		tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
961 		rx_total += tap_stats->q_ipackets[i];
962 		rx_bytes_total += tap_stats->q_ibytes[i];
963 		rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
964 		ierrors += pmd->rxq[i].stats.ierrors;
965 	}
966 
967 	/* tx queue statistics */
968 	imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
969 		dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
970 
971 	for (i = 0; i < imax; i++) {
972 		tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
973 		tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
974 		tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
975 		tx_total += tap_stats->q_opackets[i];
976 		tx_err_total += tap_stats->q_errors[i];
977 		tx_bytes_total += tap_stats->q_obytes[i];
978 	}
979 
980 	tap_stats->ipackets = rx_total;
981 	tap_stats->ibytes = rx_bytes_total;
982 	tap_stats->ierrors = ierrors;
983 	tap_stats->rx_nombuf = rx_nombuf;
984 	tap_stats->opackets = tx_total;
985 	tap_stats->oerrors = tx_err_total;
986 	tap_stats->obytes = tx_bytes_total;
987 	return 0;
988 }
989 
990 static void
991 tap_stats_reset(struct rte_eth_dev *dev)
992 {
993 	int i;
994 	struct pmd_internals *pmd = dev->data->dev_private;
995 
996 	for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
997 		pmd->rxq[i].stats.ipackets = 0;
998 		pmd->rxq[i].stats.ibytes = 0;
999 		pmd->rxq[i].stats.ierrors = 0;
1000 		pmd->rxq[i].stats.rx_nombuf = 0;
1001 
1002 		pmd->txq[i].stats.opackets = 0;
1003 		pmd->txq[i].stats.errs = 0;
1004 		pmd->txq[i].stats.obytes = 0;
1005 	}
1006 }
1007 
1008 static void
1009 tap_dev_close(struct rte_eth_dev *dev)
1010 {
1011 	int i;
1012 	struct pmd_internals *internals = dev->data->dev_private;
1013 	struct pmd_process_private *process_private = dev->process_private;
1014 
1015 	tap_link_set_down(dev);
1016 	tap_flow_flush(dev, NULL);
1017 	tap_flow_implicit_flush(internals, NULL);
1018 
1019 	for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1020 		if (process_private->rxq_fds[i] != -1) {
1021 			close(process_private->rxq_fds[i]);
1022 			process_private->rxq_fds[i] = -1;
1023 		}
1024 		if (process_private->txq_fds[i] != -1) {
1025 			close(process_private->txq_fds[i]);
1026 			process_private->txq_fds[i] = -1;
1027 		}
1028 	}
1029 
1030 	if (internals->remote_if_index) {
1031 		/* Restore initial remote state */
1032 		ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
1033 				&internals->remote_initial_flags);
1034 	}
1035 
1036 	if (internals->ka_fd != -1) {
1037 		close(internals->ka_fd);
1038 		internals->ka_fd = -1;
1039 	}
1040 	/*
1041 	 * Since TUN device has no more opened file descriptors
1042 	 * it will be removed from kernel
1043 	 */
1044 }
1045 
1046 static void
1047 tap_rx_queue_release(void *queue)
1048 {
1049 	struct rx_queue *rxq = queue;
1050 	struct pmd_process_private *process_private;
1051 
1052 	if (!rxq)
1053 		return;
1054 	process_private = rte_eth_devices[rxq->in_port].process_private;
1055 	if (process_private->rxq_fds[rxq->queue_id] > 0) {
1056 		close(process_private->rxq_fds[rxq->queue_id]);
1057 		process_private->rxq_fds[rxq->queue_id] = -1;
1058 		rte_pktmbuf_free(rxq->pool);
1059 		rte_free(rxq->iovecs);
1060 		rxq->pool = NULL;
1061 		rxq->iovecs = NULL;
1062 	}
1063 }
1064 
1065 static void
1066 tap_tx_queue_release(void *queue)
1067 {
1068 	struct tx_queue *txq = queue;
1069 	struct pmd_process_private *process_private;
1070 
1071 	if (!txq)
1072 		return;
1073 	process_private = rte_eth_devices[txq->out_port].process_private;
1074 
1075 	if (process_private->txq_fds[txq->queue_id] > 0) {
1076 		close(process_private->txq_fds[txq->queue_id]);
1077 		process_private->txq_fds[txq->queue_id] = -1;
1078 	}
1079 }
1080 
1081 static int
1082 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
1083 {
1084 	struct rte_eth_link *dev_link = &dev->data->dev_link;
1085 	struct pmd_internals *pmd = dev->data->dev_private;
1086 	struct ifreq ifr = { .ifr_flags = 0 };
1087 
1088 	if (pmd->remote_if_index) {
1089 		tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
1090 		if (!(ifr.ifr_flags & IFF_UP) ||
1091 		    !(ifr.ifr_flags & IFF_RUNNING)) {
1092 			dev_link->link_status = ETH_LINK_DOWN;
1093 			return 0;
1094 		}
1095 	}
1096 	tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
1097 	dev_link->link_status =
1098 		((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
1099 		 ETH_LINK_UP :
1100 		 ETH_LINK_DOWN);
1101 	return 0;
1102 }
1103 
1104 static void
1105 tap_promisc_enable(struct rte_eth_dev *dev)
1106 {
1107 	struct pmd_internals *pmd = dev->data->dev_private;
1108 	struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1109 
1110 	dev->data->promiscuous = 1;
1111 	tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1112 	if (pmd->remote_if_index && !pmd->flow_isolate)
1113 		tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
1114 }
1115 
1116 static void
1117 tap_promisc_disable(struct rte_eth_dev *dev)
1118 {
1119 	struct pmd_internals *pmd = dev->data->dev_private;
1120 	struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1121 
1122 	dev->data->promiscuous = 0;
1123 	tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1124 	if (pmd->remote_if_index && !pmd->flow_isolate)
1125 		tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
1126 }
1127 
1128 static void
1129 tap_allmulti_enable(struct rte_eth_dev *dev)
1130 {
1131 	struct pmd_internals *pmd = dev->data->dev_private;
1132 	struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1133 
1134 	dev->data->all_multicast = 1;
1135 	tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1136 	if (pmd->remote_if_index && !pmd->flow_isolate)
1137 		tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
1138 }
1139 
1140 static void
1141 tap_allmulti_disable(struct rte_eth_dev *dev)
1142 {
1143 	struct pmd_internals *pmd = dev->data->dev_private;
1144 	struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1145 
1146 	dev->data->all_multicast = 0;
1147 	tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1148 	if (pmd->remote_if_index && !pmd->flow_isolate)
1149 		tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
1150 }
1151 
1152 static int
1153 tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1154 {
1155 	struct pmd_internals *pmd = dev->data->dev_private;
1156 	enum ioctl_mode mode = LOCAL_ONLY;
1157 	struct ifreq ifr;
1158 	int ret;
1159 
1160 	if (pmd->type == ETH_TUNTAP_TYPE_TUN) {
1161 		TAP_LOG(ERR, "%s: can't MAC address for TUN",
1162 			dev->device->name);
1163 		return -ENOTSUP;
1164 	}
1165 
1166 	if (is_zero_ether_addr(mac_addr)) {
1167 		TAP_LOG(ERR, "%s: can't set an empty MAC address",
1168 			dev->device->name);
1169 		return -EINVAL;
1170 	}
1171 	/* Check the actual current MAC address on the tap netdevice */
1172 	ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY);
1173 	if (ret < 0)
1174 		return ret;
1175 	if (is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
1176 			       mac_addr))
1177 		return 0;
1178 	/* Check the current MAC address on the remote */
1179 	ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY);
1180 	if (ret < 0)
1181 		return ret;
1182 	if (!is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
1183 			       mac_addr))
1184 		mode = LOCAL_AND_REMOTE;
1185 	ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1186 	rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
1187 	ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode);
1188 	if (ret < 0)
1189 		return ret;
1190 	rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
1191 	if (pmd->remote_if_index && !pmd->flow_isolate) {
1192 		/* Replace MAC redirection rule after a MAC change */
1193 		ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC);
1194 		if (ret < 0) {
1195 			TAP_LOG(ERR,
1196 				"%s: Couldn't delete MAC redirection rule",
1197 				dev->device->name);
1198 			return ret;
1199 		}
1200 		ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC);
1201 		if (ret < 0) {
1202 			TAP_LOG(ERR,
1203 				"%s: Couldn't add MAC redirection rule",
1204 				dev->device->name);
1205 			return ret;
1206 		}
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static int
1213 tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev)
1214 {
1215 	uint32_t gso_types;
1216 	char pool_name[64];
1217 
1218 	/*
1219 	 * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE bytes
1220 	 * size per mbuf use this pool for both direct and indirect mbufs
1221 	 */
1222 
1223 	struct rte_mempool *mp;      /* Mempool for GSO packets */
1224 
1225 	/* initialize GSO context */
1226 	gso_types = DEV_TX_OFFLOAD_TCP_TSO;
1227 	snprintf(pool_name, sizeof(pool_name), "mp_%s", dev->device->name);
1228 	mp = rte_mempool_lookup((const char *)pool_name);
1229 	if (!mp) {
1230 		mp = rte_pktmbuf_pool_create(pool_name, TAP_GSO_MBUFS_NUM,
1231 			TAP_GSO_MBUF_CACHE_SIZE, 0,
1232 			RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE,
1233 			SOCKET_ID_ANY);
1234 		if (!mp) {
1235 			struct pmd_internals *pmd = dev->data->dev_private;
1236 			RTE_LOG(DEBUG, PMD, "%s: failed to create mbuf pool for device %s\n",
1237 				pmd->name, dev->device->name);
1238 			return -1;
1239 		}
1240 	}
1241 
1242 	gso_ctx->direct_pool = mp;
1243 	gso_ctx->indirect_pool = mp;
1244 	gso_ctx->gso_types = gso_types;
1245 	gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */
1246 	gso_ctx->flag = 0;
1247 
1248 	return 0;
1249 }
1250 
1251 static int
1252 tap_setup_queue(struct rte_eth_dev *dev,
1253 		struct pmd_internals *internals,
1254 		uint16_t qid,
1255 		int is_rx)
1256 {
1257 	int ret;
1258 	int *fd;
1259 	int *other_fd;
1260 	const char *dir;
1261 	struct pmd_internals *pmd = dev->data->dev_private;
1262 	struct pmd_process_private *process_private = dev->process_private;
1263 	struct rx_queue *rx = &internals->rxq[qid];
1264 	struct tx_queue *tx = &internals->txq[qid];
1265 	struct rte_gso_ctx *gso_ctx;
1266 
1267 	if (is_rx) {
1268 		fd = &process_private->rxq_fds[qid];
1269 		other_fd = &process_private->txq_fds[qid];
1270 		dir = "rx";
1271 		gso_ctx = NULL;
1272 	} else {
1273 		fd = &process_private->txq_fds[qid];
1274 		other_fd = &process_private->rxq_fds[qid];
1275 		dir = "tx";
1276 		gso_ctx = &tx->gso_ctx;
1277 	}
1278 	if (*fd != -1) {
1279 		/* fd for this queue already exists */
1280 		TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists",
1281 			pmd->name, *fd, dir, qid);
1282 		gso_ctx = NULL;
1283 	} else if (*other_fd != -1) {
1284 		/* Only other_fd exists. dup it */
1285 		*fd = dup(*other_fd);
1286 		if (*fd < 0) {
1287 			*fd = -1;
1288 			TAP_LOG(ERR, "%s: dup() failed.", pmd->name);
1289 			return -1;
1290 		}
1291 		TAP_LOG(DEBUG, "%s: dup fd %d for %s queue qid %d (%d)",
1292 			pmd->name, *other_fd, dir, qid, *fd);
1293 	} else {
1294 		/* Both RX and TX fds do not exist (equal -1). Create fd */
1295 		*fd = tun_alloc(pmd, 0);
1296 		if (*fd < 0) {
1297 			*fd = -1; /* restore original value */
1298 			TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name);
1299 			return -1;
1300 		}
1301 		TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d",
1302 			pmd->name, dir, qid, *fd);
1303 	}
1304 
1305 	tx->mtu = &dev->data->mtu;
1306 	rx->rxmode = &dev->data->dev_conf.rxmode;
1307 	if (gso_ctx) {
1308 		ret = tap_gso_ctx_setup(gso_ctx, dev);
1309 		if (ret)
1310 			return -1;
1311 	}
1312 
1313 	tx->type = pmd->type;
1314 
1315 	return *fd;
1316 }
1317 
1318 static int
1319 tap_rx_queue_setup(struct rte_eth_dev *dev,
1320 		   uint16_t rx_queue_id,
1321 		   uint16_t nb_rx_desc,
1322 		   unsigned int socket_id,
1323 		   const struct rte_eth_rxconf *rx_conf __rte_unused,
1324 		   struct rte_mempool *mp)
1325 {
1326 	struct pmd_internals *internals = dev->data->dev_private;
1327 	struct pmd_process_private *process_private = dev->process_private;
1328 	struct rx_queue *rxq = &internals->rxq[rx_queue_id];
1329 	struct rte_mbuf **tmp = &rxq->pool;
1330 	long iov_max = sysconf(_SC_IOV_MAX);
1331 
1332 	if (iov_max <= 0) {
1333 		TAP_LOG(WARNING,
1334 			"_SC_IOV_MAX is not defined. Using %d as default",
1335 			TAP_IOV_DEFAULT_MAX);
1336 		iov_max = TAP_IOV_DEFAULT_MAX;
1337 	}
1338 	uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
1339 	struct iovec (*iovecs)[nb_desc + 1];
1340 	int data_off = RTE_PKTMBUF_HEADROOM;
1341 	int ret = 0;
1342 	int fd;
1343 	int i;
1344 
1345 	if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
1346 		TAP_LOG(WARNING,
1347 			"nb_rx_queues %d too small or mempool NULL",
1348 			dev->data->nb_rx_queues);
1349 		return -1;
1350 	}
1351 
1352 	rxq->mp = mp;
1353 	rxq->trigger_seen = 1; /* force initial burst */
1354 	rxq->in_port = dev->data->port_id;
1355 	rxq->queue_id = rx_queue_id;
1356 	rxq->nb_rx_desc = nb_desc;
1357 	iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1358 				    socket_id);
1359 	if (!iovecs) {
1360 		TAP_LOG(WARNING,
1361 			"%s: Couldn't allocate %d RX descriptors",
1362 			dev->device->name, nb_desc);
1363 		return -ENOMEM;
1364 	}
1365 	rxq->iovecs = iovecs;
1366 
1367 	dev->data->rx_queues[rx_queue_id] = rxq;
1368 	fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1369 	if (fd == -1) {
1370 		ret = fd;
1371 		goto error;
1372 	}
1373 
1374 	(*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1375 	(*rxq->iovecs)[0].iov_base = &rxq->pi;
1376 
1377 	for (i = 1; i <= nb_desc; i++) {
1378 		*tmp = rte_pktmbuf_alloc(rxq->mp);
1379 		if (!*tmp) {
1380 			TAP_LOG(WARNING,
1381 				"%s: couldn't allocate memory for queue %d",
1382 				dev->device->name, rx_queue_id);
1383 			ret = -ENOMEM;
1384 			goto error;
1385 		}
1386 		(*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1387 		(*rxq->iovecs)[i].iov_base =
1388 			(char *)(*tmp)->buf_addr + data_off;
1389 		data_off = 0;
1390 		tmp = &(*tmp)->next;
1391 	}
1392 
1393 	TAP_LOG(DEBUG, "  RX TUNTAP device name %s, qid %d on fd %d",
1394 		internals->name, rx_queue_id,
1395 		process_private->rxq_fds[rx_queue_id]);
1396 
1397 	return 0;
1398 
1399 error:
1400 	rte_pktmbuf_free(rxq->pool);
1401 	rxq->pool = NULL;
1402 	rte_free(rxq->iovecs);
1403 	rxq->iovecs = NULL;
1404 	return ret;
1405 }
1406 
1407 static int
1408 tap_tx_queue_setup(struct rte_eth_dev *dev,
1409 		   uint16_t tx_queue_id,
1410 		   uint16_t nb_tx_desc __rte_unused,
1411 		   unsigned int socket_id __rte_unused,
1412 		   const struct rte_eth_txconf *tx_conf)
1413 {
1414 	struct pmd_internals *internals = dev->data->dev_private;
1415 	struct pmd_process_private *process_private = dev->process_private;
1416 	struct tx_queue *txq;
1417 	int ret;
1418 	uint64_t offloads;
1419 
1420 	if (tx_queue_id >= dev->data->nb_tx_queues)
1421 		return -1;
1422 	dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1423 	txq = dev->data->tx_queues[tx_queue_id];
1424 	txq->out_port = dev->data->port_id;
1425 	txq->queue_id = tx_queue_id;
1426 
1427 	offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1428 	txq->csum = !!(offloads &
1429 			(DEV_TX_OFFLOAD_IPV4_CKSUM |
1430 			 DEV_TX_OFFLOAD_UDP_CKSUM |
1431 			 DEV_TX_OFFLOAD_TCP_CKSUM));
1432 
1433 	ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1434 	if (ret == -1)
1435 		return -1;
1436 	TAP_LOG(DEBUG,
1437 		"  TX TUNTAP device name %s, qid %d on fd %d csum %s",
1438 		internals->name, tx_queue_id,
1439 		process_private->txq_fds[tx_queue_id],
1440 		txq->csum ? "on" : "off");
1441 
1442 	return 0;
1443 }
1444 
1445 static int
1446 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1447 {
1448 	struct pmd_internals *pmd = dev->data->dev_private;
1449 	struct ifreq ifr = { .ifr_mtu = mtu };
1450 	int err = 0;
1451 
1452 	err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1453 	if (!err)
1454 		dev->data->mtu = mtu;
1455 
1456 	return err;
1457 }
1458 
1459 static int
1460 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1461 		     struct ether_addr *mc_addr_set __rte_unused,
1462 		     uint32_t nb_mc_addr __rte_unused)
1463 {
1464 	/*
1465 	 * Nothing to do actually: the tap has no filtering whatsoever, every
1466 	 * packet is received.
1467 	 */
1468 	return 0;
1469 }
1470 
1471 static int
1472 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1473 {
1474 	struct rte_eth_dev *dev = arg;
1475 	struct pmd_internals *pmd = dev->data->dev_private;
1476 	struct ifinfomsg *info = NLMSG_DATA(nh);
1477 
1478 	if (nh->nlmsg_type != RTM_NEWLINK ||
1479 	    (info->ifi_index != pmd->if_index &&
1480 	     info->ifi_index != pmd->remote_if_index))
1481 		return 0;
1482 	return tap_link_update(dev, 0);
1483 }
1484 
1485 static void
1486 tap_dev_intr_handler(void *cb_arg)
1487 {
1488 	struct rte_eth_dev *dev = cb_arg;
1489 	struct pmd_internals *pmd = dev->data->dev_private;
1490 
1491 	tap_nl_recv(pmd->intr_handle.fd, tap_nl_msg_handler, dev);
1492 }
1493 
1494 static int
1495 tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
1496 {
1497 	struct pmd_internals *pmd = dev->data->dev_private;
1498 
1499 	/* In any case, disable interrupt if the conf is no longer there. */
1500 	if (!dev->data->dev_conf.intr_conf.lsc) {
1501 		if (pmd->intr_handle.fd != -1) {
1502 			tap_nl_final(pmd->intr_handle.fd);
1503 			rte_intr_callback_unregister(&pmd->intr_handle,
1504 				tap_dev_intr_handler, dev);
1505 		}
1506 		return 0;
1507 	}
1508 	if (set) {
1509 		pmd->intr_handle.fd = tap_nl_init(RTMGRP_LINK);
1510 		if (unlikely(pmd->intr_handle.fd == -1))
1511 			return -EBADF;
1512 		return rte_intr_callback_register(
1513 			&pmd->intr_handle, tap_dev_intr_handler, dev);
1514 	}
1515 	tap_nl_final(pmd->intr_handle.fd);
1516 	return rte_intr_callback_unregister(&pmd->intr_handle,
1517 					    tap_dev_intr_handler, dev);
1518 }
1519 
1520 static int
1521 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1522 {
1523 	int err;
1524 
1525 	err = tap_lsc_intr_handle_set(dev, set);
1526 	if (err)
1527 		return err;
1528 	err = tap_rx_intr_vec_set(dev, set);
1529 	if (err && set)
1530 		tap_lsc_intr_handle_set(dev, 0);
1531 	return err;
1532 }
1533 
1534 static const uint32_t*
1535 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1536 {
1537 	static const uint32_t ptypes[] = {
1538 		RTE_PTYPE_INNER_L2_ETHER,
1539 		RTE_PTYPE_INNER_L2_ETHER_VLAN,
1540 		RTE_PTYPE_INNER_L2_ETHER_QINQ,
1541 		RTE_PTYPE_INNER_L3_IPV4,
1542 		RTE_PTYPE_INNER_L3_IPV4_EXT,
1543 		RTE_PTYPE_INNER_L3_IPV6,
1544 		RTE_PTYPE_INNER_L3_IPV6_EXT,
1545 		RTE_PTYPE_INNER_L4_FRAG,
1546 		RTE_PTYPE_INNER_L4_UDP,
1547 		RTE_PTYPE_INNER_L4_TCP,
1548 		RTE_PTYPE_INNER_L4_SCTP,
1549 		RTE_PTYPE_L2_ETHER,
1550 		RTE_PTYPE_L2_ETHER_VLAN,
1551 		RTE_PTYPE_L2_ETHER_QINQ,
1552 		RTE_PTYPE_L3_IPV4,
1553 		RTE_PTYPE_L3_IPV4_EXT,
1554 		RTE_PTYPE_L3_IPV6_EXT,
1555 		RTE_PTYPE_L3_IPV6,
1556 		RTE_PTYPE_L4_FRAG,
1557 		RTE_PTYPE_L4_UDP,
1558 		RTE_PTYPE_L4_TCP,
1559 		RTE_PTYPE_L4_SCTP,
1560 	};
1561 
1562 	return ptypes;
1563 }
1564 
1565 static int
1566 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1567 		  struct rte_eth_fc_conf *fc_conf)
1568 {
1569 	fc_conf->mode = RTE_FC_NONE;
1570 	return 0;
1571 }
1572 
1573 static int
1574 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1575 		  struct rte_eth_fc_conf *fc_conf)
1576 {
1577 	if (fc_conf->mode != RTE_FC_NONE)
1578 		return -ENOTSUP;
1579 	return 0;
1580 }
1581 
1582 /**
1583  * DPDK callback to update the RSS hash configuration.
1584  *
1585  * @param dev
1586  *   Pointer to Ethernet device structure.
1587  * @param[in] rss_conf
1588  *   RSS configuration data.
1589  *
1590  * @return
1591  *   0 on success, a negative errno value otherwise and rte_errno is set.
1592  */
1593 static int
1594 tap_rss_hash_update(struct rte_eth_dev *dev,
1595 		struct rte_eth_rss_conf *rss_conf)
1596 {
1597 	if (rss_conf->rss_hf & TAP_RSS_HF_MASK) {
1598 		rte_errno = EINVAL;
1599 		return -rte_errno;
1600 	}
1601 	if (rss_conf->rss_key && rss_conf->rss_key_len) {
1602 		/*
1603 		 * Currently TAP RSS key is hard coded
1604 		 * and cannot be updated
1605 		 */
1606 		TAP_LOG(ERR,
1607 			"port %u RSS key cannot be updated",
1608 			dev->data->port_id);
1609 		rte_errno = EINVAL;
1610 		return -rte_errno;
1611 	}
1612 	return 0;
1613 }
1614 
1615 static int
1616 tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1617 {
1618 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1619 
1620 	return 0;
1621 }
1622 
1623 static int
1624 tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1625 {
1626 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1627 
1628 	return 0;
1629 }
1630 
1631 static int
1632 tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1633 {
1634 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1635 
1636 	return 0;
1637 }
1638 
1639 static int
1640 tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1641 {
1642 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1643 
1644 	return 0;
1645 }
1646 static const struct eth_dev_ops ops = {
1647 	.dev_start              = tap_dev_start,
1648 	.dev_stop               = tap_dev_stop,
1649 	.dev_close              = tap_dev_close,
1650 	.dev_configure          = tap_dev_configure,
1651 	.dev_infos_get          = tap_dev_info,
1652 	.rx_queue_setup         = tap_rx_queue_setup,
1653 	.tx_queue_setup         = tap_tx_queue_setup,
1654 	.rx_queue_start         = tap_rx_queue_start,
1655 	.tx_queue_start         = tap_tx_queue_start,
1656 	.rx_queue_stop          = tap_rx_queue_stop,
1657 	.tx_queue_stop          = tap_tx_queue_stop,
1658 	.rx_queue_release       = tap_rx_queue_release,
1659 	.tx_queue_release       = tap_tx_queue_release,
1660 	.flow_ctrl_get          = tap_flow_ctrl_get,
1661 	.flow_ctrl_set          = tap_flow_ctrl_set,
1662 	.link_update            = tap_link_update,
1663 	.dev_set_link_up        = tap_link_set_up,
1664 	.dev_set_link_down      = tap_link_set_down,
1665 	.promiscuous_enable     = tap_promisc_enable,
1666 	.promiscuous_disable    = tap_promisc_disable,
1667 	.allmulticast_enable    = tap_allmulti_enable,
1668 	.allmulticast_disable   = tap_allmulti_disable,
1669 	.mac_addr_set           = tap_mac_set,
1670 	.mtu_set                = tap_mtu_set,
1671 	.set_mc_addr_list       = tap_set_mc_addr_list,
1672 	.stats_get              = tap_stats_get,
1673 	.stats_reset            = tap_stats_reset,
1674 	.dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1675 	.rss_hash_update        = tap_rss_hash_update,
1676 	.filter_ctrl            = tap_dev_filter_ctrl,
1677 };
1678 
1679 static int
1680 eth_dev_tap_create(struct rte_vdev_device *vdev, char *tap_name,
1681 		   char *remote_iface, struct ether_addr *mac_addr,
1682 		   enum rte_tuntap_type type)
1683 {
1684 	int numa_node = rte_socket_id();
1685 	struct rte_eth_dev *dev;
1686 	struct pmd_internals *pmd;
1687 	struct pmd_process_private *process_private;
1688 	struct rte_eth_dev_data *data;
1689 	struct ifreq ifr;
1690 	int i;
1691 
1692 	TAP_LOG(DEBUG, "%s device on numa %u",
1693 			tuntap_name, rte_socket_id());
1694 
1695 	dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1696 	if (!dev) {
1697 		TAP_LOG(ERR, "%s Unable to allocate device struct",
1698 				tuntap_name);
1699 		goto error_exit_nodev;
1700 	}
1701 
1702 	process_private = (struct pmd_process_private *)
1703 		rte_zmalloc_socket(tap_name, sizeof(struct pmd_process_private),
1704 			RTE_CACHE_LINE_SIZE, dev->device->numa_node);
1705 
1706 	if (process_private == NULL) {
1707 		TAP_LOG(ERR, "Failed to alloc memory for process private");
1708 		return -1;
1709 	}
1710 	pmd = dev->data->dev_private;
1711 	dev->process_private = process_private;
1712 	pmd->dev = dev;
1713 	snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
1714 	pmd->type = type;
1715 
1716 	pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1717 	if (pmd->ioctl_sock == -1) {
1718 		TAP_LOG(ERR,
1719 			"%s Unable to get a socket for management: %s",
1720 			tuntap_name, strerror(errno));
1721 		goto error_exit;
1722 	}
1723 
1724 	/* Setup some default values */
1725 	data = dev->data;
1726 	data->dev_private = pmd;
1727 	data->dev_flags = RTE_ETH_DEV_INTR_LSC;
1728 	data->numa_node = numa_node;
1729 
1730 	data->dev_link = pmd_link;
1731 	data->mac_addrs = &pmd->eth_addr;
1732 	/* Set the number of RX and TX queues */
1733 	data->nb_rx_queues = 0;
1734 	data->nb_tx_queues = 0;
1735 
1736 	dev->dev_ops = &ops;
1737 	dev->rx_pkt_burst = pmd_rx_burst;
1738 	dev->tx_pkt_burst = pmd_tx_burst;
1739 
1740 	pmd->intr_handle.type = RTE_INTR_HANDLE_EXT;
1741 	pmd->intr_handle.fd = -1;
1742 	dev->intr_handle = &pmd->intr_handle;
1743 
1744 	/* Presetup the fds to -1 as being not valid */
1745 	pmd->ka_fd = -1;
1746 	for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1747 		process_private->rxq_fds[i] = -1;
1748 		process_private->txq_fds[i] = -1;
1749 	}
1750 
1751 	if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1752 		if (is_zero_ether_addr(mac_addr))
1753 			eth_random_addr((uint8_t *)&pmd->eth_addr);
1754 		else
1755 			rte_memcpy(&pmd->eth_addr, mac_addr, sizeof(*mac_addr));
1756 	}
1757 
1758 	/*
1759 	 * Allocate a TUN device keep-alive file descriptor that will only be
1760 	 * closed when the TUN device itself is closed or removed.
1761 	 * This keep-alive file descriptor will guarantee that the TUN device
1762 	 * exists even when all of its queues are closed
1763 	 */
1764 	pmd->ka_fd = tun_alloc(pmd, 1);
1765 	if (pmd->ka_fd == -1) {
1766 		TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
1767 		goto error_exit;
1768 	}
1769 	TAP_LOG(DEBUG, "allocated %s", pmd->name);
1770 
1771 	ifr.ifr_mtu = dev->data->mtu;
1772 	if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
1773 		goto error_exit;
1774 
1775 	if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1776 		memset(&ifr, 0, sizeof(struct ifreq));
1777 		ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1778 		rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr,
1779 				ETHER_ADDR_LEN);
1780 		if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
1781 			goto error_exit;
1782 	}
1783 
1784 	/*
1785 	 * Set up everything related to rte_flow:
1786 	 * - netlink socket
1787 	 * - tap / remote if_index
1788 	 * - mandatory QDISCs
1789 	 * - rte_flow actual/implicit lists
1790 	 * - implicit rules
1791 	 */
1792 	pmd->nlsk_fd = tap_nl_init(0);
1793 	if (pmd->nlsk_fd == -1) {
1794 		TAP_LOG(WARNING, "%s: failed to create netlink socket.",
1795 			pmd->name);
1796 		goto disable_rte_flow;
1797 	}
1798 	pmd->if_index = if_nametoindex(pmd->name);
1799 	if (!pmd->if_index) {
1800 		TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name);
1801 		goto disable_rte_flow;
1802 	}
1803 	if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
1804 		TAP_LOG(ERR, "%s: failed to create multiq qdisc.",
1805 			pmd->name);
1806 		goto disable_rte_flow;
1807 	}
1808 	if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
1809 		TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
1810 			pmd->name);
1811 		goto disable_rte_flow;
1812 	}
1813 	LIST_INIT(&pmd->flows);
1814 
1815 	if (strlen(remote_iface)) {
1816 		pmd->remote_if_index = if_nametoindex(remote_iface);
1817 		if (!pmd->remote_if_index) {
1818 			TAP_LOG(ERR, "%s: failed to get %s if_index.",
1819 				pmd->name, remote_iface);
1820 			goto error_remote;
1821 		}
1822 		snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
1823 			 "%s", remote_iface);
1824 
1825 		/* Save state of remote device */
1826 		tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
1827 
1828 		/* Replicate remote MAC address */
1829 		if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
1830 			TAP_LOG(ERR, "%s: failed to get %s MAC address.",
1831 				pmd->name, pmd->remote_iface);
1832 			goto error_remote;
1833 		}
1834 		rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
1835 			   ETHER_ADDR_LEN);
1836 		/* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
1837 		if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
1838 			TAP_LOG(ERR, "%s: failed to get %s MAC address.",
1839 				pmd->name, remote_iface);
1840 			goto error_remote;
1841 		}
1842 
1843 		/*
1844 		 * Flush usually returns negative value because it tries to
1845 		 * delete every QDISC (and on a running device, one QDISC at
1846 		 * least is needed). Ignore negative return value.
1847 		 */
1848 		qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
1849 		if (qdisc_create_ingress(pmd->nlsk_fd,
1850 					 pmd->remote_if_index) < 0) {
1851 			TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
1852 				pmd->remote_iface);
1853 			goto error_remote;
1854 		}
1855 		LIST_INIT(&pmd->implicit_flows);
1856 		if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
1857 		    tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
1858 		    tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
1859 		    tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
1860 			TAP_LOG(ERR,
1861 				"%s: failed to create implicit rules.",
1862 				pmd->name);
1863 			goto error_remote;
1864 		}
1865 	}
1866 
1867 	rte_eth_dev_probing_finish(dev);
1868 	return 0;
1869 
1870 disable_rte_flow:
1871 	TAP_LOG(ERR, " Disabling rte flow support: %s(%d)",
1872 		strerror(errno), errno);
1873 	if (strlen(remote_iface)) {
1874 		TAP_LOG(ERR, "Remote feature requires flow support.");
1875 		goto error_exit;
1876 	}
1877 	rte_eth_dev_probing_finish(dev);
1878 	return 0;
1879 
1880 error_remote:
1881 	TAP_LOG(ERR, " Can't set up remote feature: %s(%d)",
1882 		strerror(errno), errno);
1883 	tap_flow_implicit_flush(pmd, NULL);
1884 
1885 error_exit:
1886 	if (pmd->ioctl_sock > 0)
1887 		close(pmd->ioctl_sock);
1888 	/* mac_addrs must not be freed alone because part of dev_private */
1889 	dev->data->mac_addrs = NULL;
1890 	rte_eth_dev_release_port(dev);
1891 
1892 error_exit_nodev:
1893 	TAP_LOG(ERR, "%s Unable to initialize %s",
1894 		tuntap_name, rte_vdev_device_name(vdev));
1895 
1896 	return -EINVAL;
1897 }
1898 
1899 static int
1900 set_interface_name(const char *key __rte_unused,
1901 		   const char *value,
1902 		   void *extra_args)
1903 {
1904 	char *name = (char *)extra_args;
1905 
1906 	if (value)
1907 		strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
1908 	else
1909 		/* use tap%d which causes kernel to choose next available */
1910 		strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
1911 
1912 	return 0;
1913 }
1914 
1915 static int
1916 set_remote_iface(const char *key __rte_unused,
1917 		 const char *value,
1918 		 void *extra_args)
1919 {
1920 	char *name = (char *)extra_args;
1921 
1922 	if (value)
1923 		strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
1924 
1925 	return 0;
1926 }
1927 
1928 static int parse_user_mac(struct ether_addr *user_mac,
1929 		const char *value)
1930 {
1931 	unsigned int index = 0;
1932 	char mac_temp[strlen(ETH_TAP_USR_MAC_FMT) + 1], *mac_byte = NULL;
1933 
1934 	if (user_mac == NULL || value == NULL)
1935 		return 0;
1936 
1937 	strlcpy(mac_temp, value, sizeof(mac_temp));
1938 	mac_byte = strtok(mac_temp, ":");
1939 
1940 	while ((mac_byte != NULL) &&
1941 			(strlen(mac_byte) <= 2) &&
1942 			(strlen(mac_byte) == strspn(mac_byte,
1943 					ETH_TAP_CMP_MAC_FMT))) {
1944 		user_mac->addr_bytes[index++] = strtoul(mac_byte, NULL, 16);
1945 		mac_byte = strtok(NULL, ":");
1946 	}
1947 
1948 	return index;
1949 }
1950 
1951 static int
1952 set_mac_type(const char *key __rte_unused,
1953 	     const char *value,
1954 	     void *extra_args)
1955 {
1956 	struct ether_addr *user_mac = extra_args;
1957 
1958 	if (!value)
1959 		return 0;
1960 
1961 	if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) {
1962 		static int iface_idx;
1963 
1964 		/* fixed mac = 00:64:74:61:70:<iface_idx> */
1965 		memcpy((char *)user_mac->addr_bytes, "\0dtap", ETHER_ADDR_LEN);
1966 		user_mac->addr_bytes[ETHER_ADDR_LEN - 1] = iface_idx++ + '0';
1967 		goto success;
1968 	}
1969 
1970 	if (parse_user_mac(user_mac, value) != 6)
1971 		goto error;
1972 success:
1973 	TAP_LOG(DEBUG, "TAP user MAC param (%s)", value);
1974 	return 0;
1975 
1976 error:
1977 	TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)",
1978 		value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT);
1979 	return -1;
1980 }
1981 
1982 /*
1983  * Open a TUN interface device. TUN PMD
1984  * 1) sets tap_type as false
1985  * 2) intakes iface as argument.
1986  * 3) as interface is virtual set speed to 10G
1987  */
1988 static int
1989 rte_pmd_tun_probe(struct rte_vdev_device *dev)
1990 {
1991 	const char *name, *params;
1992 	int ret;
1993 	struct rte_kvargs *kvlist = NULL;
1994 	char tun_name[RTE_ETH_NAME_MAX_LEN];
1995 	char remote_iface[RTE_ETH_NAME_MAX_LEN];
1996 	struct rte_eth_dev *eth_dev;
1997 
1998 	strcpy(tuntap_name, "TUN");
1999 
2000 	name = rte_vdev_device_name(dev);
2001 	params = rte_vdev_device_args(dev);
2002 	memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2003 
2004 	if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
2005 	    strlen(params) == 0) {
2006 		eth_dev = rte_eth_dev_attach_secondary(name);
2007 		if (!eth_dev) {
2008 			TAP_LOG(ERR, "Failed to probe %s", name);
2009 			return -1;
2010 		}
2011 		eth_dev->dev_ops = &ops;
2012 		eth_dev->device = &dev->device;
2013 		rte_eth_dev_probing_finish(eth_dev);
2014 		return 0;
2015 	}
2016 
2017 	/* use tun%d which causes kernel to choose next available */
2018 	strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2019 
2020 	if (params && (params[0] != '\0')) {
2021 		TAP_LOG(DEBUG, "parameters (%s)", params);
2022 
2023 		kvlist = rte_kvargs_parse(params, valid_arguments);
2024 		if (kvlist) {
2025 			if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2026 				ret = rte_kvargs_process(kvlist,
2027 					ETH_TAP_IFACE_ARG,
2028 					&set_interface_name,
2029 					tun_name);
2030 
2031 				if (ret == -1)
2032 					goto leave;
2033 			}
2034 		}
2035 	}
2036 	pmd_link.link_speed = ETH_SPEED_NUM_10G;
2037 
2038 	TAP_LOG(NOTICE, "Initializing pmd_tun for %s", name);
2039 
2040 	ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0,
2041 				 ETH_TUNTAP_TYPE_TUN);
2042 
2043 leave:
2044 	if (ret == -1) {
2045 		TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2046 			name, tun_name);
2047 	}
2048 	rte_kvargs_free(kvlist);
2049 
2050 	return ret;
2051 }
2052 
2053 /* Request queue file descriptors from secondary to primary. */
2054 static int
2055 tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev)
2056 {
2057 	int ret;
2058 	struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
2059 	struct rte_mp_msg request, *reply;
2060 	struct rte_mp_reply replies;
2061 	struct ipc_queues *request_param = (struct ipc_queues *)request.param;
2062 	struct ipc_queues *reply_param;
2063 	struct pmd_process_private *process_private = dev->process_private;
2064 	int queue, fd_iterator;
2065 
2066 	/* Prepare the request */
2067 	memset(&request, 0, sizeof(request));
2068 	strlcpy(request.name, TAP_MP_KEY, sizeof(request.name));
2069 	strlcpy(request_param->port_name, port_name,
2070 		sizeof(request_param->port_name));
2071 	request.len_param = sizeof(*request_param);
2072 	/* Send request and receive reply */
2073 	ret = rte_mp_request_sync(&request, &replies, &timeout);
2074 	if (ret < 0 || replies.nb_received != 1) {
2075 		TAP_LOG(ERR, "Failed to request queues from primary: %d",
2076 			rte_errno);
2077 		return -1;
2078 	}
2079 	reply = &replies.msgs[0];
2080 	reply_param = (struct ipc_queues *)reply->param;
2081 	TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name);
2082 
2083 	/* Attach the queues from received file descriptors */
2084 	if (reply_param->rxq_count + reply_param->txq_count != reply->num_fds) {
2085 		TAP_LOG(ERR, "Unexpected number of fds received");
2086 		return -1;
2087 	}
2088 
2089 	dev->data->nb_rx_queues = reply_param->rxq_count;
2090 	dev->data->nb_tx_queues = reply_param->txq_count;
2091 	fd_iterator = 0;
2092 	for (queue = 0; queue < reply_param->rxq_count; queue++)
2093 		process_private->rxq_fds[queue] = reply->fds[fd_iterator++];
2094 	for (queue = 0; queue < reply_param->txq_count; queue++)
2095 		process_private->txq_fds[queue] = reply->fds[fd_iterator++];
2096 	free(reply);
2097 	return 0;
2098 }
2099 
2100 /* Send the queue file descriptors from the primary process to secondary. */
2101 static int
2102 tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer)
2103 {
2104 	struct rte_eth_dev *dev;
2105 	struct pmd_process_private *process_private;
2106 	struct rte_mp_msg reply;
2107 	const struct ipc_queues *request_param =
2108 		(const struct ipc_queues *)request->param;
2109 	struct ipc_queues *reply_param =
2110 		(struct ipc_queues *)reply.param;
2111 	uint16_t port_id;
2112 	int queue;
2113 	int ret;
2114 
2115 	/* Get requested port */
2116 	TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name);
2117 	ret = rte_eth_dev_get_port_by_name(request_param->port_name, &port_id);
2118 	if (ret) {
2119 		TAP_LOG(ERR, "Failed to get port id for %s",
2120 			request_param->port_name);
2121 		return -1;
2122 	}
2123 	dev = &rte_eth_devices[port_id];
2124 	process_private = dev->process_private;
2125 
2126 	/* Fill file descriptors for all queues */
2127 	reply.num_fds = 0;
2128 	reply_param->rxq_count = 0;
2129 	if (dev->data->nb_rx_queues + dev->data->nb_tx_queues >
2130 			RTE_MP_MAX_FD_NUM){
2131 		TAP_LOG(ERR, "Number of rx/tx queues exceeds max number of fds");
2132 		return -1;
2133 	}
2134 
2135 	for (queue = 0; queue < dev->data->nb_rx_queues; queue++) {
2136 		reply.fds[reply.num_fds++] = process_private->rxq_fds[queue];
2137 		reply_param->rxq_count++;
2138 	}
2139 	RTE_ASSERT(reply_param->rxq_count == dev->data->nb_rx_queues);
2140 
2141 	reply_param->txq_count = 0;
2142 	for (queue = 0; queue < dev->data->nb_tx_queues; queue++) {
2143 		reply.fds[reply.num_fds++] = process_private->txq_fds[queue];
2144 		reply_param->txq_count++;
2145 	}
2146 	RTE_ASSERT(reply_param->txq_count == dev->data->nb_tx_queues);
2147 
2148 	/* Send reply */
2149 	strlcpy(reply.name, request->name, sizeof(reply.name));
2150 	strlcpy(reply_param->port_name, request_param->port_name,
2151 		sizeof(reply_param->port_name));
2152 	reply.len_param = sizeof(*reply_param);
2153 	if (rte_mp_reply(&reply, peer) < 0) {
2154 		TAP_LOG(ERR, "Failed to reply an IPC request to sync queues");
2155 		return -1;
2156 	}
2157 	return 0;
2158 }
2159 
2160 /* Open a TAP interface device.
2161  */
2162 static int
2163 rte_pmd_tap_probe(struct rte_vdev_device *dev)
2164 {
2165 	const char *name, *params;
2166 	int ret;
2167 	struct rte_kvargs *kvlist = NULL;
2168 	int speed;
2169 	char tap_name[RTE_ETH_NAME_MAX_LEN];
2170 	char remote_iface[RTE_ETH_NAME_MAX_LEN];
2171 	struct ether_addr user_mac = { .addr_bytes = {0} };
2172 	struct rte_eth_dev *eth_dev;
2173 	int tap_devices_count_increased = 0;
2174 
2175 	strcpy(tuntap_name, "TAP");
2176 
2177 	name = rte_vdev_device_name(dev);
2178 	params = rte_vdev_device_args(dev);
2179 
2180 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2181 		eth_dev = rte_eth_dev_attach_secondary(name);
2182 		if (!eth_dev) {
2183 			TAP_LOG(ERR, "Failed to probe %s", name);
2184 			return -1;
2185 		}
2186 		eth_dev->dev_ops = &ops;
2187 		eth_dev->device = &dev->device;
2188 		eth_dev->rx_pkt_burst = pmd_rx_burst;
2189 		eth_dev->tx_pkt_burst = pmd_tx_burst;
2190 		if (!rte_eal_primary_proc_alive(NULL)) {
2191 			TAP_LOG(ERR, "Primary process is missing");
2192 			return -1;
2193 		}
2194 		eth_dev->process_private = (struct pmd_process_private *)
2195 			rte_zmalloc_socket(name,
2196 				sizeof(struct pmd_process_private),
2197 				RTE_CACHE_LINE_SIZE,
2198 				eth_dev->device->numa_node);
2199 		if (eth_dev->process_private == NULL) {
2200 			TAP_LOG(ERR,
2201 				"Failed to alloc memory for process private");
2202 			return -1;
2203 		}
2204 
2205 		ret = tap_mp_attach_queues(name, eth_dev);
2206 		if (ret != 0)
2207 			return -1;
2208 		rte_eth_dev_probing_finish(eth_dev);
2209 		return 0;
2210 	}
2211 
2212 	speed = ETH_SPEED_NUM_10G;
2213 
2214 	/* use tap%d which causes kernel to choose next available */
2215 	strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2216 	memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2217 
2218 	if (params && (params[0] != '\0')) {
2219 		TAP_LOG(DEBUG, "parameters (%s)", params);
2220 
2221 		kvlist = rte_kvargs_parse(params, valid_arguments);
2222 		if (kvlist) {
2223 			if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2224 				ret = rte_kvargs_process(kvlist,
2225 							 ETH_TAP_IFACE_ARG,
2226 							 &set_interface_name,
2227 							 tap_name);
2228 				if (ret == -1)
2229 					goto leave;
2230 			}
2231 
2232 			if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
2233 				ret = rte_kvargs_process(kvlist,
2234 							 ETH_TAP_REMOTE_ARG,
2235 							 &set_remote_iface,
2236 							 remote_iface);
2237 				if (ret == -1)
2238 					goto leave;
2239 			}
2240 
2241 			if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
2242 				ret = rte_kvargs_process(kvlist,
2243 							 ETH_TAP_MAC_ARG,
2244 							 &set_mac_type,
2245 							 &user_mac);
2246 				if (ret == -1)
2247 					goto leave;
2248 			}
2249 		}
2250 	}
2251 	pmd_link.link_speed = speed;
2252 
2253 	TAP_LOG(NOTICE, "Initializing pmd_tap for %s as %s",
2254 		name, tap_name);
2255 
2256 	/* Register IPC feed callback */
2257 	if (!tap_devices_count) {
2258 		ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues);
2259 		if (ret < 0) {
2260 			TAP_LOG(ERR, "%s: Failed to register IPC callback: %s",
2261 				tuntap_name, strerror(rte_errno));
2262 			goto leave;
2263 		}
2264 	}
2265 	tap_devices_count++;
2266 	tap_devices_count_increased = 1;
2267 	ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac,
2268 		ETH_TUNTAP_TYPE_TAP);
2269 
2270 leave:
2271 	if (ret == -1) {
2272 		TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2273 			name, tap_name);
2274 		if (tap_devices_count_increased == 1) {
2275 			if (tap_devices_count == 1)
2276 				rte_mp_action_unregister(TAP_MP_KEY);
2277 			tap_devices_count--;
2278 		}
2279 	}
2280 	rte_kvargs_free(kvlist);
2281 
2282 	return ret;
2283 }
2284 
2285 /* detach a TUNTAP device.
2286  */
2287 static int
2288 rte_pmd_tap_remove(struct rte_vdev_device *dev)
2289 {
2290 	struct rte_eth_dev *eth_dev = NULL;
2291 	struct pmd_internals *internals;
2292 	struct pmd_process_private *process_private;
2293 	int i;
2294 
2295 	/* find the ethdev entry */
2296 	eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2297 	if (!eth_dev)
2298 		return -ENODEV;
2299 
2300 	/* mac_addrs must not be freed alone because part of dev_private */
2301 	eth_dev->data->mac_addrs = NULL;
2302 
2303 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2304 		return rte_eth_dev_release_port(eth_dev);
2305 
2306 	internals = eth_dev->data->dev_private;
2307 	process_private = eth_dev->process_private;
2308 
2309 	TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u",
2310 		(internals->type == ETH_TUNTAP_TYPE_TAP) ? "TAP" : "TUN",
2311 		rte_socket_id());
2312 
2313 	if (internals->nlsk_fd) {
2314 		tap_flow_flush(eth_dev, NULL);
2315 		tap_flow_implicit_flush(internals, NULL);
2316 		tap_nl_final(internals->nlsk_fd);
2317 	}
2318 	for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
2319 		if (process_private->rxq_fds[i] != -1) {
2320 			close(process_private->rxq_fds[i]);
2321 			process_private->rxq_fds[i] = -1;
2322 		}
2323 		if (process_private->txq_fds[i] != -1) {
2324 			close(process_private->txq_fds[i]);
2325 			process_private->txq_fds[i] = -1;
2326 		}
2327 	}
2328 
2329 	close(internals->ioctl_sock);
2330 	rte_free(eth_dev->process_private);
2331 	if (tap_devices_count == 1)
2332 		rte_mp_action_unregister(TAP_MP_KEY);
2333 	tap_devices_count--;
2334 	rte_eth_dev_release_port(eth_dev);
2335 
2336 	if (internals->ka_fd != -1) {
2337 		close(internals->ka_fd);
2338 		internals->ka_fd = -1;
2339 	}
2340 	return 0;
2341 }
2342 
2343 static struct rte_vdev_driver pmd_tun_drv = {
2344 	.probe = rte_pmd_tun_probe,
2345 	.remove = rte_pmd_tap_remove,
2346 };
2347 
2348 static struct rte_vdev_driver pmd_tap_drv = {
2349 	.probe = rte_pmd_tap_probe,
2350 	.remove = rte_pmd_tap_remove,
2351 };
2352 
2353 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
2354 RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv);
2355 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
2356 RTE_PMD_REGISTER_PARAM_STRING(net_tun,
2357 			      ETH_TAP_IFACE_ARG "=<string> ");
2358 RTE_PMD_REGISTER_PARAM_STRING(net_tap,
2359 			      ETH_TAP_IFACE_ARG "=<string> "
2360 			      ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " "
2361 			      ETH_TAP_REMOTE_ARG "=<string>");
2362 int tap_logtype;
2363 
2364 RTE_INIT(tap_init_log)
2365 {
2366 	tap_logtype = rte_log_register("pmd.net.tap");
2367 	if (tap_logtype >= 0)
2368 		rte_log_set_level(tap_logtype, RTE_LOG_NOTICE);
2369 }
2370