52380b4b549e0ff6d98ba17897c2f03fee6f58c6
[dpdk.git] / drivers / net / tap / rte_eth_tap.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_atomic.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_byteorder.h>
37 #include <rte_common.h>
38 #include <rte_mbuf.h>
39 #include <rte_ethdev.h>
40 #include <rte_ethdev_vdev.h>
41 #include <rte_malloc.h>
42 #include <rte_vdev.h>
43 #include <rte_kvargs.h>
44 #include <rte_net.h>
45 #include <rte_debug.h>
46 #include <rte_ip.h>
47
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/utsname.h>
53 #include <sys/mman.h>
54 #include <errno.h>
55 #include <signal.h>
56 #include <stdint.h>
57 #include <sys/uio.h>
58 #include <unistd.h>
59 #include <arpa/inet.h>
60 #include <net/if.h>
61 #include <linux/if_tun.h>
62 #include <linux/if_ether.h>
63 #include <linux/version.h>
64 #include <fcntl.h>
65
66 #include <rte_eth_tap.h>
67 #include <tap_flow.h>
68 #include <tap_netlink.h>
69 #include <tap_tcmsgs.h>
70
71 /* Linux based path to the TUN device */
72 #define TUN_TAP_DEV_PATH        "/dev/net/tun"
73 #define DEFAULT_TAP_NAME        "dtap"
74
75 #define ETH_TAP_IFACE_ARG       "iface"
76 #define ETH_TAP_SPEED_ARG       "speed"
77 #define ETH_TAP_REMOTE_ARG      "remote"
78 #define ETH_TAP_MAC_ARG         "mac"
79 #define ETH_TAP_MAC_FIXED       "fixed"
80
81 #define FLOWER_KERNEL_VERSION KERNEL_VERSION(4, 2, 0)
82 #define FLOWER_VLAN_KERNEL_VERSION KERNEL_VERSION(4, 9, 0)
83
84 static struct rte_vdev_driver pmd_tap_drv;
85
86 static const char *valid_arguments[] = {
87         ETH_TAP_IFACE_ARG,
88         ETH_TAP_SPEED_ARG,
89         ETH_TAP_REMOTE_ARG,
90         ETH_TAP_MAC_ARG,
91         NULL
92 };
93
94 static int tap_unit;
95
96 static volatile uint32_t tap_trigger;   /* Rx trigger */
97
98 static struct rte_eth_link pmd_link = {
99         .link_speed = ETH_SPEED_NUM_10G,
100         .link_duplex = ETH_LINK_FULL_DUPLEX,
101         .link_status = ETH_LINK_DOWN,
102         .link_autoneg = ETH_LINK_SPEED_AUTONEG
103 };
104
105 static void
106 tap_trigger_cb(int sig __rte_unused)
107 {
108         /* Valid trigger values are nonzero */
109         tap_trigger = (tap_trigger + 1) | 0x80000000;
110 }
111
112 /* Specifies on what netdevices the ioctl should be applied */
113 enum ioctl_mode {
114         LOCAL_AND_REMOTE,
115         LOCAL_ONLY,
116         REMOTE_ONLY,
117 };
118
119 static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
120
121 /* Tun/Tap allocation routine
122  *
123  * name is the number of the interface to use, unless NULL to take the host
124  * supplied name.
125  */
126 static int
127 tun_alloc(struct pmd_internals *pmd)
128 {
129         struct ifreq ifr;
130 #ifdef IFF_MULTI_QUEUE
131         unsigned int features;
132 #endif
133         int fd;
134
135         memset(&ifr, 0, sizeof(struct ifreq));
136
137         /*
138          * Do not set IFF_NO_PI as packet information header will be needed
139          * to check if a received packet has been truncated.
140          */
141         ifr.ifr_flags = IFF_TAP;
142         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", pmd->name);
143
144         RTE_LOG(DEBUG, PMD, "ifr_name '%s'\n", ifr.ifr_name);
145
146         fd = open(TUN_TAP_DEV_PATH, O_RDWR);
147         if (fd < 0) {
148                 RTE_LOG(ERR, PMD, "Unable to create TAP interface\n");
149                 goto error;
150         }
151
152 #ifdef IFF_MULTI_QUEUE
153         /* Grab the TUN features to verify we can work multi-queue */
154         if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
155                 RTE_LOG(ERR, PMD, "TAP unable to get TUN/TAP features\n");
156                 goto error;
157         }
158         RTE_LOG(DEBUG, PMD, "  TAP Features %08x\n", features);
159
160         if (features & IFF_MULTI_QUEUE) {
161                 RTE_LOG(DEBUG, PMD, "  Multi-queue support for %d queues\n",
162                         RTE_PMD_TAP_MAX_QUEUES);
163                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
164         } else
165 #endif
166         {
167                 ifr.ifr_flags |= IFF_ONE_QUEUE;
168                 RTE_LOG(DEBUG, PMD, "  Single queue only support\n");
169         }
170
171         /* Set the TUN/TAP configuration and set the name if needed */
172         if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
173                 RTE_LOG(WARNING, PMD,
174                         "Unable to set TUNSETIFF for %s\n",
175                         ifr.ifr_name);
176                 perror("TUNSETIFF");
177                 goto error;
178         }
179
180         /* Always set the file descriptor to non-blocking */
181         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
182                 RTE_LOG(WARNING, PMD,
183                         "Unable to set %s to nonblocking\n",
184                         ifr.ifr_name);
185                 perror("F_SETFL, NONBLOCK");
186                 goto error;
187         }
188
189         /* Set up trigger to optimize empty Rx bursts */
190         errno = 0;
191         do {
192                 struct sigaction sa;
193                 int flags = fcntl(fd, F_GETFL);
194
195                 if (flags == -1 || sigaction(SIGIO, NULL, &sa) == -1)
196                         break;
197                 if (sa.sa_handler != tap_trigger_cb) {
198                         /*
199                          * Make sure SIGIO is not already taken. This is done
200                          * as late as possible to leave the application a
201                          * chance to set up its own signal handler first.
202                          */
203                         if (sa.sa_handler != SIG_IGN &&
204                             sa.sa_handler != SIG_DFL) {
205                                 errno = EBUSY;
206                                 break;
207                         }
208                         sa = (struct sigaction){
209                                 .sa_flags = SA_RESTART,
210                                 .sa_handler = tap_trigger_cb,
211                         };
212                         if (sigaction(SIGIO, &sa, NULL) == -1)
213                                 break;
214                 }
215                 /* Enable SIGIO on file descriptor */
216                 fcntl(fd, F_SETFL, flags | O_ASYNC);
217                 fcntl(fd, F_SETOWN, getpid());
218         } while (0);
219         if (errno) {
220                 /* Disable trigger globally in case of error */
221                 tap_trigger = 0;
222                 RTE_LOG(WARNING, PMD, "Rx trigger disabled: %s\n",
223                         strerror(errno));
224         }
225
226         return fd;
227
228 error:
229         if (fd > 0)
230                 close(fd);
231         return -1;
232 }
233
234 static void
235 tap_verify_csum(struct rte_mbuf *mbuf)
236 {
237         uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
238         uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
239         uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
240         unsigned int l2_len = sizeof(struct ether_hdr);
241         unsigned int l3_len;
242         uint16_t cksum = 0;
243         void *l3_hdr;
244         void *l4_hdr;
245
246         if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
247                 l2_len += 4;
248         else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
249                 l2_len += 8;
250         /* Don't verify checksum for packets with discontinuous L2 header */
251         if (unlikely(l2_len + sizeof(struct ipv4_hdr) >
252                      rte_pktmbuf_data_len(mbuf)))
253                 return;
254         l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
255         if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
256                 struct ipv4_hdr *iph = l3_hdr;
257
258                 /* ihl contains the number of 4-byte words in the header */
259                 l3_len = 4 * (iph->version_ihl & 0xf);
260                 if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
261                         return;
262
263                 cksum = ~rte_raw_cksum(iph, l3_len);
264                 mbuf->ol_flags |= cksum ?
265                         PKT_RX_IP_CKSUM_BAD :
266                         PKT_RX_IP_CKSUM_GOOD;
267         } else if (l3 == RTE_PTYPE_L3_IPV6) {
268                 l3_len = sizeof(struct ipv6_hdr);
269         } else {
270                 /* IPv6 extensions are not supported */
271                 return;
272         }
273         if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
274                 l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
275                 /* Don't verify checksum for multi-segment packets. */
276                 if (mbuf->nb_segs > 1)
277                         return;
278                 if (l3 == RTE_PTYPE_L3_IPV4)
279                         cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
280                 else if (l3 == RTE_PTYPE_L3_IPV6)
281                         cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
282                 mbuf->ol_flags |= cksum ?
283                         PKT_RX_L4_CKSUM_BAD :
284                         PKT_RX_L4_CKSUM_GOOD;
285         }
286 }
287
288 /* Callback to handle the rx burst of packets to the correct interface and
289  * file descriptor(s) in a multi-queue setup.
290  */
291 static uint16_t
292 pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
293 {
294         struct rx_queue *rxq = queue;
295         uint16_t num_rx;
296         unsigned long num_rx_bytes = 0;
297         uint32_t trigger = tap_trigger;
298
299         if (trigger == rxq->trigger_seen)
300                 return 0;
301         if (trigger)
302                 rxq->trigger_seen = trigger;
303         rte_compiler_barrier();
304         for (num_rx = 0; num_rx < nb_pkts; ) {
305                 struct rte_mbuf *mbuf = rxq->pool;
306                 struct rte_mbuf *seg = NULL;
307                 struct rte_mbuf *new_tail = NULL;
308                 uint16_t data_off = rte_pktmbuf_headroom(mbuf);
309                 int len;
310
311                 len = readv(rxq->fd, *rxq->iovecs,
312                             1 + (rxq->rxmode->enable_scatter ?
313                                  rxq->nb_rx_desc : 1));
314                 if (len < (int)sizeof(struct tun_pi))
315                         break;
316
317                 /* Packet couldn't fit in the provided mbuf */
318                 if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
319                         rxq->stats.ierrors++;
320                         continue;
321                 }
322
323                 len -= sizeof(struct tun_pi);
324
325                 mbuf->pkt_len = len;
326                 mbuf->port = rxq->in_port;
327                 while (1) {
328                         struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
329
330                         if (unlikely(!buf)) {
331                                 rxq->stats.rx_nombuf++;
332                                 /* No new buf has been allocated: do nothing */
333                                 if (!new_tail || !seg)
334                                         goto end;
335
336                                 seg->next = NULL;
337                                 rte_pktmbuf_free(mbuf);
338
339                                 goto end;
340                         }
341                         seg = seg ? seg->next : mbuf;
342                         if (rxq->pool == mbuf)
343                                 rxq->pool = buf;
344                         if (new_tail)
345                                 new_tail->next = buf;
346                         new_tail = buf;
347                         new_tail->next = seg->next;
348
349                         /* iovecs[0] is reserved for packet info (pi) */
350                         (*rxq->iovecs)[mbuf->nb_segs].iov_len =
351                                 buf->buf_len - data_off;
352                         (*rxq->iovecs)[mbuf->nb_segs].iov_base =
353                                 (char *)buf->buf_addr + data_off;
354
355                         seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
356                         seg->data_off = data_off;
357
358                         len -= seg->data_len;
359                         if (len <= 0)
360                                 break;
361                         mbuf->nb_segs++;
362                         /* First segment has headroom, not the others */
363                         data_off = 0;
364                 }
365                 seg->next = NULL;
366                 mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
367                                                       RTE_PTYPE_ALL_MASK);
368                 if (rxq->rxmode->hw_ip_checksum)
369                         tap_verify_csum(mbuf);
370
371                 /* account for the receive frame */
372                 bufs[num_rx++] = mbuf;
373                 num_rx_bytes += mbuf->pkt_len;
374         }
375 end:
376         rxq->stats.ipackets += num_rx;
377         rxq->stats.ibytes += num_rx_bytes;
378
379         return num_rx;
380 }
381
382 static void
383 tap_tx_offload(char *packet, uint64_t ol_flags, unsigned int l2_len,
384                unsigned int l3_len)
385 {
386         void *l3_hdr = packet + l2_len;
387
388         if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4)) {
389                 struct ipv4_hdr *iph = l3_hdr;
390                 uint16_t cksum;
391
392                 iph->hdr_checksum = 0;
393                 cksum = rte_raw_cksum(iph, l3_len);
394                 iph->hdr_checksum = (cksum == 0xffff) ? cksum : ~cksum;
395         }
396         if (ol_flags & PKT_TX_L4_MASK) {
397                 uint16_t l4_len;
398                 uint32_t cksum;
399                 uint16_t *l4_cksum;
400                 void *l4_hdr;
401
402                 l4_hdr = packet + l2_len + l3_len;
403                 if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM)
404                         l4_cksum = &((struct udp_hdr *)l4_hdr)->dgram_cksum;
405                 else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM)
406                         l4_cksum = &((struct tcp_hdr *)l4_hdr)->cksum;
407                 else
408                         return;
409                 *l4_cksum = 0;
410                 if (ol_flags & PKT_TX_IPV4) {
411                         struct ipv4_hdr *iph = l3_hdr;
412
413                         l4_len = rte_be_to_cpu_16(iph->total_length) - l3_len;
414                         cksum = rte_ipv4_phdr_cksum(l3_hdr, 0);
415                 } else {
416                         struct ipv6_hdr *ip6h = l3_hdr;
417
418                         /* payload_len does not include ext headers */
419                         l4_len = rte_be_to_cpu_16(ip6h->payload_len) -
420                                 l3_len + sizeof(struct ipv6_hdr);
421                         cksum = rte_ipv6_phdr_cksum(l3_hdr, 0);
422                 }
423                 cksum += rte_raw_cksum(l4_hdr, l4_len);
424                 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
425                 cksum = (~cksum) & 0xffff;
426                 if (cksum == 0)
427                         cksum = 0xffff;
428                 *l4_cksum = cksum;
429         }
430 }
431
432 /* Callback to handle sending packets from the tap interface
433  */
434 static uint16_t
435 pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
436 {
437         struct tx_queue *txq = queue;
438         uint16_t num_tx = 0;
439         unsigned long num_tx_bytes = 0;
440         uint32_t max_size;
441         int i;
442
443         if (unlikely(nb_pkts == 0))
444                 return 0;
445
446         max_size = *txq->mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN + 4);
447         for (i = 0; i < nb_pkts; i++) {
448                 struct rte_mbuf *mbuf = bufs[num_tx];
449                 struct iovec iovecs[mbuf->nb_segs + 1];
450                 struct tun_pi pi = { .flags = 0 };
451                 struct rte_mbuf *seg = mbuf;
452                 char m_copy[mbuf->data_len];
453                 int n;
454                 int j;
455
456                 /* stats.errs will be incremented */
457                 if (rte_pktmbuf_pkt_len(mbuf) > max_size)
458                         break;
459
460                 iovecs[0].iov_base = &pi;
461                 iovecs[0].iov_len = sizeof(pi);
462                 for (j = 1; j <= mbuf->nb_segs; j++) {
463                         iovecs[j].iov_len = rte_pktmbuf_data_len(seg);
464                         iovecs[j].iov_base =
465                                 rte_pktmbuf_mtod(seg, void *);
466                         seg = seg->next;
467                 }
468                 if (mbuf->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_IPV4) ||
469                     (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM ||
470                     (mbuf->ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM) {
471                         /* Support only packets with all data in the same seg */
472                         if (mbuf->nb_segs > 1)
473                                 break;
474                         /* To change checksums, work on a copy of data. */
475                         rte_memcpy(m_copy, rte_pktmbuf_mtod(mbuf, void *),
476                                    rte_pktmbuf_data_len(mbuf));
477                         tap_tx_offload(m_copy, mbuf->ol_flags,
478                                        mbuf->l2_len, mbuf->l3_len);
479                         iovecs[1].iov_base = m_copy;
480                 }
481                 /* copy the tx frame data */
482                 n = writev(txq->fd, iovecs, mbuf->nb_segs + 1);
483                 if (n <= 0)
484                         break;
485
486                 num_tx++;
487                 num_tx_bytes += mbuf->pkt_len;
488                 rte_pktmbuf_free(mbuf);
489         }
490
491         txq->stats.opackets += num_tx;
492         txq->stats.errs += nb_pkts - num_tx;
493         txq->stats.obytes += num_tx_bytes;
494
495         return num_tx;
496 }
497
498 static const char *
499 tap_ioctl_req2str(unsigned long request)
500 {
501         switch (request) {
502         case SIOCSIFFLAGS:
503                 return "SIOCSIFFLAGS";
504         case SIOCGIFFLAGS:
505                 return "SIOCGIFFLAGS";
506         case SIOCGIFHWADDR:
507                 return "SIOCGIFHWADDR";
508         case SIOCSIFHWADDR:
509                 return "SIOCSIFHWADDR";
510         case SIOCSIFMTU:
511                 return "SIOCSIFMTU";
512         }
513         return "UNKNOWN";
514 }
515
516 static int
517 tap_ioctl(struct pmd_internals *pmd, unsigned long request,
518           struct ifreq *ifr, int set, enum ioctl_mode mode)
519 {
520         short req_flags = ifr->ifr_flags;
521         int remote = pmd->remote_if_index &&
522                 (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
523
524         if (!pmd->remote_if_index && mode == REMOTE_ONLY)
525                 return 0;
526         /*
527          * If there is a remote netdevice, apply ioctl on it, then apply it on
528          * the tap netdevice.
529          */
530 apply:
531         if (remote)
532                 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
533         else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
534                 snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
535         switch (request) {
536         case SIOCSIFFLAGS:
537                 /* fetch current flags to leave other flags untouched */
538                 if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
539                         goto error;
540                 if (set)
541                         ifr->ifr_flags |= req_flags;
542                 else
543                         ifr->ifr_flags &= ~req_flags;
544                 break;
545         case SIOCGIFFLAGS:
546         case SIOCGIFHWADDR:
547         case SIOCSIFHWADDR:
548         case SIOCSIFMTU:
549                 break;
550         default:
551                 RTE_ASSERT(!"unsupported request type: must not happen");
552         }
553         if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
554                 goto error;
555         if (remote-- && mode == LOCAL_AND_REMOTE)
556                 goto apply;
557         return 0;
558
559 error:
560         RTE_LOG(DEBUG, PMD, "%s: %s(%s) failed: %s(%d)\n", ifr->ifr_name,
561                 __func__, tap_ioctl_req2str(request), strerror(errno), errno);
562         return -errno;
563 }
564
565 static int
566 tap_link_set_down(struct rte_eth_dev *dev)
567 {
568         struct pmd_internals *pmd = dev->data->dev_private;
569         struct ifreq ifr = { .ifr_flags = IFF_UP };
570
571         dev->data->dev_link.link_status = ETH_LINK_DOWN;
572         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
573 }
574
575 static int
576 tap_link_set_up(struct rte_eth_dev *dev)
577 {
578         struct pmd_internals *pmd = dev->data->dev_private;
579         struct ifreq ifr = { .ifr_flags = IFF_UP };
580
581         dev->data->dev_link.link_status = ETH_LINK_UP;
582         return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
583 }
584
585 static int
586 tap_dev_start(struct rte_eth_dev *dev)
587 {
588         int err;
589
590         err = tap_intr_handle_set(dev, 1);
591         if (err)
592                 return err;
593         return tap_link_set_up(dev);
594 }
595
596 /* This function gets called when the current port gets stopped.
597  */
598 static void
599 tap_dev_stop(struct rte_eth_dev *dev)
600 {
601         tap_intr_handle_set(dev, 0);
602         tap_link_set_down(dev);
603 }
604
605 static int
606 tap_dev_configure(struct rte_eth_dev *dev)
607 {
608         if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
609                 RTE_LOG(ERR, PMD,
610                         "%s: number of rx queues %d exceeds max num of queues %d\n",
611                         dev->device->name,
612                         dev->data->nb_rx_queues,
613                         RTE_PMD_TAP_MAX_QUEUES);
614                 return -1;
615         }
616         if (dev->data->nb_tx_queues > RTE_PMD_TAP_MAX_QUEUES) {
617                 RTE_LOG(ERR, PMD,
618                         "%s: number of tx queues %d exceeds max num of queues %d\n",
619                         dev->device->name,
620                         dev->data->nb_tx_queues,
621                         RTE_PMD_TAP_MAX_QUEUES);
622                 return -1;
623         }
624
625         RTE_LOG(INFO, PMD, "%s: %p: TX configured queues number: %u\n",
626              dev->device->name, (void *)dev, dev->data->nb_tx_queues);
627
628         RTE_LOG(INFO, PMD, "%s: %p: RX configured queues number: %u\n",
629              dev->device->name, (void *)dev, dev->data->nb_rx_queues);
630
631         return 0;
632 }
633
634 static uint32_t
635 tap_dev_speed_capa(void)
636 {
637         uint32_t speed = pmd_link.link_speed;
638         uint32_t capa = 0;
639
640         if (speed >= ETH_SPEED_NUM_10M)
641                 capa |= ETH_LINK_SPEED_10M;
642         if (speed >= ETH_SPEED_NUM_100M)
643                 capa |= ETH_LINK_SPEED_100M;
644         if (speed >= ETH_SPEED_NUM_1G)
645                 capa |= ETH_LINK_SPEED_1G;
646         if (speed >= ETH_SPEED_NUM_5G)
647                 capa |= ETH_LINK_SPEED_2_5G;
648         if (speed >= ETH_SPEED_NUM_5G)
649                 capa |= ETH_LINK_SPEED_5G;
650         if (speed >= ETH_SPEED_NUM_10G)
651                 capa |= ETH_LINK_SPEED_10G;
652         if (speed >= ETH_SPEED_NUM_20G)
653                 capa |= ETH_LINK_SPEED_20G;
654         if (speed >= ETH_SPEED_NUM_25G)
655                 capa |= ETH_LINK_SPEED_25G;
656         if (speed >= ETH_SPEED_NUM_40G)
657                 capa |= ETH_LINK_SPEED_40G;
658         if (speed >= ETH_SPEED_NUM_50G)
659                 capa |= ETH_LINK_SPEED_50G;
660         if (speed >= ETH_SPEED_NUM_56G)
661                 capa |= ETH_LINK_SPEED_56G;
662         if (speed >= ETH_SPEED_NUM_100G)
663                 capa |= ETH_LINK_SPEED_100G;
664
665         return capa;
666 }
667
668 static void
669 tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
670 {
671         struct pmd_internals *internals = dev->data->dev_private;
672
673         dev_info->if_index = internals->if_index;
674         dev_info->max_mac_addrs = 1;
675         dev_info->max_rx_pktlen = (uint32_t)ETHER_MAX_VLAN_FRAME_LEN;
676         dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
677         dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
678         dev_info->min_rx_bufsize = 0;
679         dev_info->pci_dev = NULL;
680         dev_info->speed_capa = tap_dev_speed_capa();
681         dev_info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
682                                      DEV_RX_OFFLOAD_UDP_CKSUM |
683                                      DEV_RX_OFFLOAD_TCP_CKSUM);
684         dev_info->tx_offload_capa =
685                 (DEV_TX_OFFLOAD_IPV4_CKSUM |
686                  DEV_TX_OFFLOAD_UDP_CKSUM |
687                  DEV_TX_OFFLOAD_TCP_CKSUM);
688 }
689
690 static void
691 tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
692 {
693         unsigned int i, imax;
694         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
695         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
696         unsigned long rx_nombuf = 0, ierrors = 0;
697         const struct pmd_internals *pmd = dev->data->dev_private;
698
699         /* rx queue statistics */
700         imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
701                 dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
702         for (i = 0; i < imax; i++) {
703                 tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
704                 tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
705                 rx_total += tap_stats->q_ipackets[i];
706                 rx_bytes_total += tap_stats->q_ibytes[i];
707                 rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
708                 ierrors += pmd->rxq[i].stats.ierrors;
709         }
710
711         /* tx queue statistics */
712         imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
713                 dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
714
715         for (i = 0; i < imax; i++) {
716                 tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
717                 tap_stats->q_errors[i] = pmd->txq[i].stats.errs;
718                 tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
719                 tx_total += tap_stats->q_opackets[i];
720                 tx_err_total += tap_stats->q_errors[i];
721                 tx_bytes_total += tap_stats->q_obytes[i];
722         }
723
724         tap_stats->ipackets = rx_total;
725         tap_stats->ibytes = rx_bytes_total;
726         tap_stats->ierrors = ierrors;
727         tap_stats->rx_nombuf = rx_nombuf;
728         tap_stats->opackets = tx_total;
729         tap_stats->oerrors = tx_err_total;
730         tap_stats->obytes = tx_bytes_total;
731 }
732
733 static void
734 tap_stats_reset(struct rte_eth_dev *dev)
735 {
736         int i;
737         struct pmd_internals *pmd = dev->data->dev_private;
738
739         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
740                 pmd->rxq[i].stats.ipackets = 0;
741                 pmd->rxq[i].stats.ibytes = 0;
742                 pmd->rxq[i].stats.ierrors = 0;
743                 pmd->rxq[i].stats.rx_nombuf = 0;
744
745                 pmd->txq[i].stats.opackets = 0;
746                 pmd->txq[i].stats.errs = 0;
747                 pmd->txq[i].stats.obytes = 0;
748         }
749 }
750
751 static void
752 tap_dev_close(struct rte_eth_dev *dev)
753 {
754         int i;
755         struct pmd_internals *internals = dev->data->dev_private;
756
757         tap_link_set_down(dev);
758         tap_flow_flush(dev, NULL);
759         tap_flow_implicit_flush(internals, NULL);
760
761         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
762                 if (internals->rxq[i].fd != -1) {
763                         close(internals->rxq[i].fd);
764                         internals->rxq[i].fd = -1;
765                 }
766                 if (internals->txq[i].fd != -1) {
767                         close(internals->txq[i].fd);
768                         internals->txq[i].fd = -1;
769                 }
770         }
771
772         if (internals->remote_if_index) {
773                 /* Restore initial remote state */
774                 ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
775                                 &internals->remote_initial_flags);
776         }
777 }
778
779 static void
780 tap_rx_queue_release(void *queue)
781 {
782         struct rx_queue *rxq = queue;
783
784         if (rxq && (rxq->fd > 0)) {
785                 close(rxq->fd);
786                 rxq->fd = -1;
787                 rte_pktmbuf_free(rxq->pool);
788                 rte_free(rxq->iovecs);
789                 rxq->pool = NULL;
790                 rxq->iovecs = NULL;
791         }
792 }
793
794 static void
795 tap_tx_queue_release(void *queue)
796 {
797         struct tx_queue *txq = queue;
798
799         if (txq && (txq->fd > 0)) {
800                 close(txq->fd);
801                 txq->fd = -1;
802         }
803 }
804
805 static int
806 tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
807 {
808         struct rte_eth_link *dev_link = &dev->data->dev_link;
809         struct pmd_internals *pmd = dev->data->dev_private;
810         struct ifreq ifr = { .ifr_flags = 0 };
811
812         if (pmd->remote_if_index) {
813                 tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
814                 if (!(ifr.ifr_flags & IFF_UP) ||
815                     !(ifr.ifr_flags & IFF_RUNNING)) {
816                         dev_link->link_status = ETH_LINK_DOWN;
817                         return 0;
818                 }
819         }
820         tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
821         dev_link->link_status =
822                 ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
823                  ETH_LINK_UP :
824                  ETH_LINK_DOWN);
825         return 0;
826 }
827
828 static void
829 tap_promisc_enable(struct rte_eth_dev *dev)
830 {
831         struct pmd_internals *pmd = dev->data->dev_private;
832         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
833
834         dev->data->promiscuous = 1;
835         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
836         if (pmd->remote_if_index && !pmd->flow_isolate)
837                 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
838 }
839
840 static void
841 tap_promisc_disable(struct rte_eth_dev *dev)
842 {
843         struct pmd_internals *pmd = dev->data->dev_private;
844         struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
845
846         dev->data->promiscuous = 0;
847         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
848         if (pmd->remote_if_index && !pmd->flow_isolate)
849                 tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
850 }
851
852 static void
853 tap_allmulti_enable(struct rte_eth_dev *dev)
854 {
855         struct pmd_internals *pmd = dev->data->dev_private;
856         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
857
858         dev->data->all_multicast = 1;
859         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
860         if (pmd->remote_if_index && !pmd->flow_isolate)
861                 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
862 }
863
864 static void
865 tap_allmulti_disable(struct rte_eth_dev *dev)
866 {
867         struct pmd_internals *pmd = dev->data->dev_private;
868         struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
869
870         dev->data->all_multicast = 0;
871         tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
872         if (pmd->remote_if_index && !pmd->flow_isolate)
873                 tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
874 }
875
876 static void
877 tap_mac_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
878 {
879         struct pmd_internals *pmd = dev->data->dev_private;
880         enum ioctl_mode mode = LOCAL_ONLY;
881         struct ifreq ifr;
882
883         if (is_zero_ether_addr(mac_addr)) {
884                 RTE_LOG(ERR, PMD, "%s: can't set an empty MAC address\n",
885                         dev->device->name);
886                 return;
887         }
888         /* Check the actual current MAC address on the tap netdevice */
889         if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
890                 return;
891         if (is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
892                                mac_addr))
893                 return;
894         /* Check the current MAC address on the remote */
895         if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0)
896                 return;
897         if (!is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
898                                mac_addr))
899                 mode = LOCAL_AND_REMOTE;
900         ifr.ifr_hwaddr.sa_family = AF_LOCAL;
901         rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
902         if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode) < 0)
903                 return;
904         rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
905         if (pmd->remote_if_index && !pmd->flow_isolate) {
906                 /* Replace MAC redirection rule after a MAC change */
907                 if (tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC) < 0) {
908                         RTE_LOG(ERR, PMD,
909                                 "%s: Couldn't delete MAC redirection rule\n",
910                                 dev->device->name);
911                         return;
912                 }
913                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
914                         RTE_LOG(ERR, PMD,
915                                 "%s: Couldn't add MAC redirection rule\n",
916                                 dev->device->name);
917         }
918 }
919
920 static int
921 tap_setup_queue(struct rte_eth_dev *dev,
922                 struct pmd_internals *internals,
923                 uint16_t qid,
924                 int is_rx)
925 {
926         int *fd;
927         int *other_fd;
928         const char *dir;
929         struct pmd_internals *pmd = dev->data->dev_private;
930         struct rx_queue *rx = &internals->rxq[qid];
931         struct tx_queue *tx = &internals->txq[qid];
932
933         if (is_rx) {
934                 fd = &rx->fd;
935                 other_fd = &tx->fd;
936                 dir = "rx";
937         } else {
938                 fd = &tx->fd;
939                 other_fd = &rx->fd;
940                 dir = "tx";
941         }
942         if (*fd != -1) {
943                 /* fd for this queue already exists */
944                 RTE_LOG(DEBUG, PMD, "%s: fd %d for %s queue qid %d exists\n",
945                         pmd->name, *fd, dir, qid);
946         } else if (*other_fd != -1) {
947                 /* Only other_fd exists. dup it */
948                 *fd = dup(*other_fd);
949                 if (*fd < 0) {
950                         *fd = -1;
951                         RTE_LOG(ERR, PMD, "%s: dup() failed.\n",
952                                 pmd->name);
953                         return -1;
954                 }
955                 RTE_LOG(DEBUG, PMD, "%s: dup fd %d for %s queue qid %d (%d)\n",
956                         pmd->name, *other_fd, dir, qid, *fd);
957         } else {
958                 /* Both RX and TX fds do not exist (equal -1). Create fd */
959                 *fd = tun_alloc(pmd);
960                 if (*fd < 0) {
961                         *fd = -1; /* restore original value */
962                         RTE_LOG(ERR, PMD, "%s: tun_alloc() failed.\n",
963                                 pmd->name);
964                         return -1;
965                 }
966                 RTE_LOG(DEBUG, PMD, "%s: add %s queue for qid %d fd %d\n",
967                         pmd->name, dir, qid, *fd);
968         }
969
970         tx->mtu = &dev->data->mtu;
971         rx->rxmode = &dev->data->dev_conf.rxmode;
972
973         return *fd;
974 }
975
976 static int
977 tap_rx_queue_setup(struct rte_eth_dev *dev,
978                    uint16_t rx_queue_id,
979                    uint16_t nb_rx_desc,
980                    unsigned int socket_id,
981                    const struct rte_eth_rxconf *rx_conf __rte_unused,
982                    struct rte_mempool *mp)
983 {
984         struct pmd_internals *internals = dev->data->dev_private;
985         struct rx_queue *rxq = &internals->rxq[rx_queue_id];
986         struct rte_mbuf **tmp = &rxq->pool;
987         long iov_max = sysconf(_SC_IOV_MAX);
988         uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
989         struct iovec (*iovecs)[nb_desc + 1];
990         int data_off = RTE_PKTMBUF_HEADROOM;
991         int ret = 0;
992         int fd;
993         int i;
994
995         if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
996                 RTE_LOG(WARNING, PMD,
997                         "nb_rx_queues %d too small or mempool NULL\n",
998                         dev->data->nb_rx_queues);
999                 return -1;
1000         }
1001
1002         rxq->mp = mp;
1003         rxq->trigger_seen = 1; /* force initial burst */
1004         rxq->in_port = dev->data->port_id;
1005         rxq->nb_rx_desc = nb_desc;
1006         iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1007                                     socket_id);
1008         if (!iovecs) {
1009                 RTE_LOG(WARNING, PMD,
1010                         "%s: Couldn't allocate %d RX descriptors\n",
1011                         dev->device->name, nb_desc);
1012                 return -ENOMEM;
1013         }
1014         rxq->iovecs = iovecs;
1015
1016         dev->data->rx_queues[rx_queue_id] = rxq;
1017         fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1018         if (fd == -1) {
1019                 ret = fd;
1020                 goto error;
1021         }
1022
1023         (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1024         (*rxq->iovecs)[0].iov_base = &rxq->pi;
1025
1026         for (i = 1; i <= nb_desc; i++) {
1027                 *tmp = rte_pktmbuf_alloc(rxq->mp);
1028                 if (!*tmp) {
1029                         RTE_LOG(WARNING, PMD,
1030                                 "%s: couldn't allocate memory for queue %d\n",
1031                                 dev->device->name, rx_queue_id);
1032                         ret = -ENOMEM;
1033                         goto error;
1034                 }
1035                 (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1036                 (*rxq->iovecs)[i].iov_base =
1037                         (char *)(*tmp)->buf_addr + data_off;
1038                 data_off = 0;
1039                 tmp = &(*tmp)->next;
1040         }
1041
1042         RTE_LOG(DEBUG, PMD, "  RX TAP device name %s, qid %d on fd %d\n",
1043                 internals->name, rx_queue_id, internals->rxq[rx_queue_id].fd);
1044
1045         return 0;
1046
1047 error:
1048         rte_pktmbuf_free(rxq->pool);
1049         rxq->pool = NULL;
1050         rte_free(rxq->iovecs);
1051         rxq->iovecs = NULL;
1052         return ret;
1053 }
1054
1055 static int
1056 tap_tx_queue_setup(struct rte_eth_dev *dev,
1057                    uint16_t tx_queue_id,
1058                    uint16_t nb_tx_desc __rte_unused,
1059                    unsigned int socket_id __rte_unused,
1060                    const struct rte_eth_txconf *tx_conf __rte_unused)
1061 {
1062         struct pmd_internals *internals = dev->data->dev_private;
1063         int ret;
1064
1065         if (tx_queue_id >= dev->data->nb_tx_queues)
1066                 return -1;
1067
1068         dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1069         ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1070         if (ret == -1)
1071                 return -1;
1072
1073         RTE_LOG(DEBUG, PMD, "  TX TAP device name %s, qid %d on fd %d\n",
1074                 internals->name, tx_queue_id, internals->txq[tx_queue_id].fd);
1075
1076         return 0;
1077 }
1078
1079 static int
1080 tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1081 {
1082         struct pmd_internals *pmd = dev->data->dev_private;
1083         struct ifreq ifr = { .ifr_mtu = mtu };
1084         int err = 0;
1085
1086         err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1087         if (!err)
1088                 dev->data->mtu = mtu;
1089
1090         return err;
1091 }
1092
1093 static int
1094 tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1095                      struct ether_addr *mc_addr_set __rte_unused,
1096                      uint32_t nb_mc_addr __rte_unused)
1097 {
1098         /*
1099          * Nothing to do actually: the tap has no filtering whatsoever, every
1100          * packet is received.
1101          */
1102         return 0;
1103 }
1104
1105 static int
1106 tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1107 {
1108         struct rte_eth_dev *dev = arg;
1109         struct pmd_internals *pmd = dev->data->dev_private;
1110         struct ifinfomsg *info = NLMSG_DATA(nh);
1111
1112         if (nh->nlmsg_type != RTM_NEWLINK ||
1113             (info->ifi_index != pmd->if_index &&
1114              info->ifi_index != pmd->remote_if_index))
1115                 return 0;
1116         return tap_link_update(dev, 0);
1117 }
1118
1119 static void
1120 tap_dev_intr_handler(void *cb_arg)
1121 {
1122         struct rte_eth_dev *dev = cb_arg;
1123         struct pmd_internals *pmd = dev->data->dev_private;
1124
1125         nl_recv(pmd->intr_handle.fd, tap_nl_msg_handler, dev);
1126 }
1127
1128 static int
1129 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1130 {
1131         struct pmd_internals *pmd = dev->data->dev_private;
1132
1133         /* In any case, disable interrupt if the conf is no longer there. */
1134         if (!dev->data->dev_conf.intr_conf.lsc) {
1135                 if (pmd->intr_handle.fd != -1) {
1136                         nl_final(pmd->intr_handle.fd);
1137                         rte_intr_callback_unregister(&pmd->intr_handle,
1138                                 tap_dev_intr_handler, dev);
1139                 }
1140                 return 0;
1141         }
1142         if (set) {
1143                 pmd->intr_handle.fd = nl_init(RTMGRP_LINK);
1144                 if (unlikely(pmd->intr_handle.fd == -1))
1145                         return -EBADF;
1146                 return rte_intr_callback_register(
1147                         &pmd->intr_handle, tap_dev_intr_handler, dev);
1148         }
1149         nl_final(pmd->intr_handle.fd);
1150         return rte_intr_callback_unregister(&pmd->intr_handle,
1151                                             tap_dev_intr_handler, dev);
1152 }
1153
1154 static const uint32_t*
1155 tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1156 {
1157         static const uint32_t ptypes[] = {
1158                 RTE_PTYPE_INNER_L2_ETHER,
1159                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1160                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1161                 RTE_PTYPE_INNER_L3_IPV4,
1162                 RTE_PTYPE_INNER_L3_IPV4_EXT,
1163                 RTE_PTYPE_INNER_L3_IPV6,
1164                 RTE_PTYPE_INNER_L3_IPV6_EXT,
1165                 RTE_PTYPE_INNER_L4_FRAG,
1166                 RTE_PTYPE_INNER_L4_UDP,
1167                 RTE_PTYPE_INNER_L4_TCP,
1168                 RTE_PTYPE_INNER_L4_SCTP,
1169                 RTE_PTYPE_L2_ETHER,
1170                 RTE_PTYPE_L2_ETHER_VLAN,
1171                 RTE_PTYPE_L2_ETHER_QINQ,
1172                 RTE_PTYPE_L3_IPV4,
1173                 RTE_PTYPE_L3_IPV4_EXT,
1174                 RTE_PTYPE_L3_IPV6_EXT,
1175                 RTE_PTYPE_L3_IPV6,
1176                 RTE_PTYPE_L4_FRAG,
1177                 RTE_PTYPE_L4_UDP,
1178                 RTE_PTYPE_L4_TCP,
1179                 RTE_PTYPE_L4_SCTP,
1180         };
1181
1182         return ptypes;
1183 }
1184
1185 static int
1186 tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1187                   struct rte_eth_fc_conf *fc_conf)
1188 {
1189         fc_conf->mode = RTE_FC_NONE;
1190         return 0;
1191 }
1192
1193 static int
1194 tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1195                   struct rte_eth_fc_conf *fc_conf)
1196 {
1197         if (fc_conf->mode != RTE_FC_NONE)
1198                 return -ENOTSUP;
1199         return 0;
1200 }
1201
1202 static const struct eth_dev_ops ops = {
1203         .dev_start              = tap_dev_start,
1204         .dev_stop               = tap_dev_stop,
1205         .dev_close              = tap_dev_close,
1206         .dev_configure          = tap_dev_configure,
1207         .dev_infos_get          = tap_dev_info,
1208         .rx_queue_setup         = tap_rx_queue_setup,
1209         .tx_queue_setup         = tap_tx_queue_setup,
1210         .rx_queue_release       = tap_rx_queue_release,
1211         .tx_queue_release       = tap_tx_queue_release,
1212         .flow_ctrl_get          = tap_flow_ctrl_get,
1213         .flow_ctrl_set          = tap_flow_ctrl_set,
1214         .link_update            = tap_link_update,
1215         .dev_set_link_up        = tap_link_set_up,
1216         .dev_set_link_down      = tap_link_set_down,
1217         .promiscuous_enable     = tap_promisc_enable,
1218         .promiscuous_disable    = tap_promisc_disable,
1219         .allmulticast_enable    = tap_allmulti_enable,
1220         .allmulticast_disable   = tap_allmulti_disable,
1221         .mac_addr_set           = tap_mac_set,
1222         .mtu_set                = tap_mtu_set,
1223         .set_mc_addr_list       = tap_set_mc_addr_list,
1224         .stats_get              = tap_stats_get,
1225         .stats_reset            = tap_stats_reset,
1226         .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1227         .filter_ctrl            = tap_dev_filter_ctrl,
1228 };
1229
1230 static int
1231 eth_dev_tap_create(struct rte_vdev_device *vdev, char *tap_name,
1232                    char *remote_iface, int fixed_mac_type)
1233 {
1234         int numa_node = rte_socket_id();
1235         struct rte_eth_dev *dev;
1236         struct pmd_internals *pmd;
1237         struct rte_eth_dev_data *data;
1238         struct ifreq ifr;
1239         int i;
1240
1241         RTE_LOG(DEBUG, PMD, "  TAP device on numa %u\n", rte_socket_id());
1242
1243         data = rte_zmalloc_socket(tap_name, sizeof(*data), 0, numa_node);
1244         if (!data) {
1245                 RTE_LOG(ERR, PMD, "TAP Failed to allocate data\n");
1246                 goto error_exit;
1247         }
1248
1249         dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1250         if (!dev) {
1251                 RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
1252                 goto error_exit;
1253         }
1254
1255         pmd = dev->data->dev_private;
1256         pmd->dev = dev;
1257         snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
1258
1259         pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1260         if (pmd->ioctl_sock == -1) {
1261                 RTE_LOG(ERR, PMD,
1262                         "TAP Unable to get a socket for management: %s\n",
1263                         strerror(errno));
1264                 goto error_exit;
1265         }
1266
1267         /* Setup some default values */
1268         rte_memcpy(data, dev->data, sizeof(*data));
1269         data->dev_private = pmd;
1270         data->dev_flags = RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1271         data->numa_node = numa_node;
1272
1273         data->dev_link = pmd_link;
1274         data->mac_addrs = &pmd->eth_addr;
1275         /* Set the number of RX and TX queues */
1276         data->nb_rx_queues = 0;
1277         data->nb_tx_queues = 0;
1278
1279         dev->data = data;
1280         dev->dev_ops = &ops;
1281         dev->rx_pkt_burst = pmd_rx_burst;
1282         dev->tx_pkt_burst = pmd_tx_burst;
1283
1284         pmd->intr_handle.type = RTE_INTR_HANDLE_EXT;
1285         pmd->intr_handle.fd = -1;
1286
1287         /* Presetup the fds to -1 as being not valid */
1288         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1289                 pmd->rxq[i].fd = -1;
1290                 pmd->txq[i].fd = -1;
1291         }
1292
1293         if (fixed_mac_type) {
1294                 /* fixed mac = 00:64:74:61:70:<iface_idx> */
1295                 static int iface_idx;
1296                 char mac[ETHER_ADDR_LEN] = "\0dtap";
1297
1298                 mac[ETHER_ADDR_LEN - 1] = iface_idx++;
1299                 rte_memcpy(&pmd->eth_addr, mac, ETHER_ADDR_LEN);
1300         } else {
1301                 eth_random_addr((uint8_t *)&pmd->eth_addr);
1302         }
1303
1304         /* Immediately create the netdevice (this will create the 1st queue). */
1305         /* rx queue */
1306         if (tap_setup_queue(dev, pmd, 0, 1) == -1)
1307                 goto error_exit;
1308         /* tx queue */
1309         if (tap_setup_queue(dev, pmd, 0, 0) == -1)
1310                 goto error_exit;
1311
1312         ifr.ifr_mtu = dev->data->mtu;
1313         if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
1314                 goto error_exit;
1315
1316         memset(&ifr, 0, sizeof(struct ifreq));
1317         ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1318         rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr, ETHER_ADDR_LEN);
1319         if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
1320                 goto error_exit;
1321
1322         /*
1323          * Set up everything related to rte_flow:
1324          * - netlink socket
1325          * - tap / remote if_index
1326          * - mandatory QDISCs
1327          * - rte_flow actual/implicit lists
1328          * - implicit rules
1329          */
1330         pmd->nlsk_fd = nl_init(0);
1331         if (pmd->nlsk_fd == -1) {
1332                 RTE_LOG(WARNING, PMD, "%s: failed to create netlink socket.\n",
1333                         pmd->name);
1334                 goto disable_rte_flow;
1335         }
1336         pmd->if_index = if_nametoindex(pmd->name);
1337         if (!pmd->if_index) {
1338                 RTE_LOG(ERR, PMD, "%s: failed to get if_index.\n", pmd->name);
1339                 goto disable_rte_flow;
1340         }
1341         if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
1342                 RTE_LOG(ERR, PMD, "%s: failed to create multiq qdisc.\n",
1343                         pmd->name);
1344                 goto disable_rte_flow;
1345         }
1346         if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
1347                 RTE_LOG(ERR, PMD, "%s: failed to create ingress qdisc.\n",
1348                         pmd->name);
1349                 goto disable_rte_flow;
1350         }
1351         LIST_INIT(&pmd->flows);
1352
1353         if (strlen(remote_iface)) {
1354                 pmd->remote_if_index = if_nametoindex(remote_iface);
1355                 if (!pmd->remote_if_index) {
1356                         RTE_LOG(ERR, PMD, "%s: failed to get %s if_index.\n",
1357                                 pmd->name, remote_iface);
1358                         goto error_remote;
1359                 }
1360                 snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
1361                          "%s", remote_iface);
1362
1363                 /* Save state of remote device */
1364                 tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
1365
1366                 /* Replicate remote MAC address */
1367                 if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
1368                         RTE_LOG(ERR, PMD, "%s: failed to get %s MAC address.\n",
1369                                 pmd->name, pmd->remote_iface);
1370                         goto error_remote;
1371                 }
1372                 rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
1373                            ETHER_ADDR_LEN);
1374                 /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
1375                 if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
1376                         RTE_LOG(ERR, PMD, "%s: failed to get %s MAC address.\n",
1377                                 pmd->name, remote_iface);
1378                         goto error_remote;
1379                 }
1380
1381                 /*
1382                  * Flush usually returns negative value because it tries to
1383                  * delete every QDISC (and on a running device, one QDISC at
1384                  * least is needed). Ignore negative return value.
1385                  */
1386                 qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
1387                 if (qdisc_create_ingress(pmd->nlsk_fd,
1388                                          pmd->remote_if_index) < 0) {
1389                         RTE_LOG(ERR, PMD, "%s: failed to create ingress qdisc.\n",
1390                                 pmd->remote_iface);
1391                         goto error_remote;
1392                 }
1393                 LIST_INIT(&pmd->implicit_flows);
1394                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
1395                     tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
1396                     tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
1397                     tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
1398                         RTE_LOG(ERR, PMD,
1399                                 "%s: failed to create implicit rules.\n",
1400                                 pmd->name);
1401                         goto error_remote;
1402                 }
1403         }
1404
1405         return 0;
1406
1407 disable_rte_flow:
1408         RTE_LOG(ERR, PMD, " Disabling rte flow support: %s(%d)\n",
1409                 strerror(errno), errno);
1410         if (strlen(remote_iface)) {
1411                 RTE_LOG(ERR, PMD, "Remote feature requires flow support.\n");
1412                 goto error_exit;
1413         }
1414         return 0;
1415
1416 error_remote:
1417         RTE_LOG(ERR, PMD, " Can't set up remote feature: %s(%d)\n",
1418                 strerror(errno), errno);
1419         tap_flow_implicit_flush(pmd, NULL);
1420
1421 error_exit:
1422         RTE_LOG(ERR, PMD, "TAP Unable to initialize %s\n",
1423                 rte_vdev_device_name(vdev));
1424
1425         rte_free(data);
1426         return -EINVAL;
1427 }
1428
1429 static int
1430 set_interface_name(const char *key __rte_unused,
1431                    const char *value,
1432                    void *extra_args)
1433 {
1434         char *name = (char *)extra_args;
1435
1436         if (value)
1437                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
1438         else
1439                 snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
1440                          DEFAULT_TAP_NAME, (tap_unit - 1));
1441
1442         return 0;
1443 }
1444
1445 static int
1446 set_interface_speed(const char *key __rte_unused,
1447                     const char *value,
1448                     void *extra_args)
1449 {
1450         *(int *)extra_args = (value) ? atoi(value) : ETH_SPEED_NUM_10G;
1451
1452         return 0;
1453 }
1454
1455 static int
1456 set_remote_iface(const char *key __rte_unused,
1457                  const char *value,
1458                  void *extra_args)
1459 {
1460         char *name = (char *)extra_args;
1461
1462         if (value)
1463                 snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s", value);
1464
1465         return 0;
1466 }
1467
1468 static int
1469 set_mac_type(const char *key __rte_unused,
1470              const char *value,
1471              void *extra_args)
1472 {
1473         if (value &&
1474             !strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED)))
1475                 *(int *)extra_args = 1;
1476         return 0;
1477 }
1478
1479 /* Open a TAP interface device.
1480  */
1481 static int
1482 rte_pmd_tap_probe(struct rte_vdev_device *dev)
1483 {
1484         const char *name, *params;
1485         int ret;
1486         struct rte_kvargs *kvlist = NULL;
1487         int speed;
1488         char tap_name[RTE_ETH_NAME_MAX_LEN];
1489         char remote_iface[RTE_ETH_NAME_MAX_LEN];
1490         int fixed_mac_type = 0;
1491
1492         name = rte_vdev_device_name(dev);
1493         params = rte_vdev_device_args(dev);
1494
1495         speed = ETH_SPEED_NUM_10G;
1496         snprintf(tap_name, sizeof(tap_name), "%s%d",
1497                  DEFAULT_TAP_NAME, tap_unit++);
1498         memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
1499
1500         if (params && (params[0] != '\0')) {
1501                 RTE_LOG(DEBUG, PMD, "parameters (%s)\n", params);
1502
1503                 kvlist = rte_kvargs_parse(params, valid_arguments);
1504                 if (kvlist) {
1505                         if (rte_kvargs_count(kvlist, ETH_TAP_SPEED_ARG) == 1) {
1506                                 ret = rte_kvargs_process(kvlist,
1507                                                          ETH_TAP_SPEED_ARG,
1508                                                          &set_interface_speed,
1509                                                          &speed);
1510                                 if (ret == -1)
1511                                         goto leave;
1512                         }
1513
1514                         if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
1515                                 ret = rte_kvargs_process(kvlist,
1516                                                          ETH_TAP_IFACE_ARG,
1517                                                          &set_interface_name,
1518                                                          tap_name);
1519                                 if (ret == -1)
1520                                         goto leave;
1521                         }
1522
1523                         if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
1524                                 ret = rte_kvargs_process(kvlist,
1525                                                          ETH_TAP_REMOTE_ARG,
1526                                                          &set_remote_iface,
1527                                                          remote_iface);
1528                                 if (ret == -1)
1529                                         goto leave;
1530                         }
1531
1532                         if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
1533                                 ret = rte_kvargs_process(kvlist,
1534                                                          ETH_TAP_MAC_ARG,
1535                                                          &set_mac_type,
1536                                                          &fixed_mac_type);
1537                                 if (ret == -1)
1538                                         goto leave;
1539                         }
1540                 }
1541         }
1542         pmd_link.link_speed = speed;
1543
1544         RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
1545                 name, tap_name);
1546
1547         ret = eth_dev_tap_create(dev, tap_name, remote_iface, fixed_mac_type);
1548
1549 leave:
1550         if (ret == -1) {
1551                 RTE_LOG(ERR, PMD, "Failed to create pmd for %s as %s\n",
1552                         name, tap_name);
1553                 tap_unit--;             /* Restore the unit number */
1554         }
1555         rte_kvargs_free(kvlist);
1556
1557         return ret;
1558 }
1559
1560 /* detach a TAP device.
1561  */
1562 static int
1563 rte_pmd_tap_remove(struct rte_vdev_device *dev)
1564 {
1565         struct rte_eth_dev *eth_dev = NULL;
1566         struct pmd_internals *internals;
1567         int i;
1568
1569         RTE_LOG(DEBUG, PMD, "Closing TUN/TAP Ethernet device on numa %u\n",
1570                 rte_socket_id());
1571
1572         /* find the ethdev entry */
1573         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1574         if (!eth_dev)
1575                 return 0;
1576
1577         internals = eth_dev->data->dev_private;
1578         if (internals->nlsk_fd) {
1579                 tap_flow_flush(eth_dev, NULL);
1580                 tap_flow_implicit_flush(internals, NULL);
1581                 nl_final(internals->nlsk_fd);
1582         }
1583         for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1584                 if (internals->rxq[i].fd != -1) {
1585                         close(internals->rxq[i].fd);
1586                         internals->rxq[i].fd = -1;
1587                 }
1588                 if (internals->txq[i].fd != -1) {
1589                         close(internals->txq[i].fd);
1590                         internals->txq[i].fd = -1;
1591                 }
1592         }
1593
1594         close(internals->ioctl_sock);
1595         rte_free(eth_dev->data->dev_private);
1596         rte_free(eth_dev->data);
1597
1598         rte_eth_dev_release_port(eth_dev);
1599
1600         return 0;
1601 }
1602
1603 static struct rte_vdev_driver pmd_tap_drv = {
1604         .probe = rte_pmd_tap_probe,
1605         .remove = rte_pmd_tap_remove,
1606 };
1607 RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
1608 RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
1609 RTE_PMD_REGISTER_PARAM_STRING(net_tap,
1610                               ETH_TAP_IFACE_ARG "=<string> "
1611                               ETH_TAP_SPEED_ARG "=<int> "
1612                               ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_FIXED " "
1613                               ETH_TAP_REMOTE_ARG "=<string>");