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