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