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