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