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