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