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