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