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