2 *------------------------------------------------------------------
3 * tuntap.c - kernel stack (reverse) punt/inject path
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
21 * @brief TunTap Kernel stack (reverse) punt/inject path.
23 * This driver runs in one of two distinct modes:
24 * - "punt/inject" mode, where we send pkts not otherwise processed
25 * by the forwarding to the Linux kernel stack, and
27 * - "normal interface" mode, where we treat the Linux kernel stack
30 * By default, we select punt/inject mode.
33 #include <fcntl.h> /* for open */
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/uio.h> /* for iovec */
39 #include <netinet/in.h>
41 #include <linux/if_arp.h>
42 #include <linux/if_tun.h>
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
47 #include <vnet/ip/ip.h>
48 #include <vnet/fib/fib_table.h>
50 #include <vnet/ethernet/ethernet.h>
51 #include <vnet/devices/devices.h>
52 #include <vnet/feature/feature.h>
54 static vnet_device_class_t tuntap_dev_class;
55 static vnet_hw_interface_class_t tuntap_interface_class;
57 static void tuntap_punt_frame (vlib_main_t * vm,
58 vlib_node_runtime_t * node,
59 vlib_frame_t * frame);
60 static void tuntap_nopunt_frame (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
62 vlib_frame_t * frame);
72 * @brief TUNTAP per thread struct
76 /** Vector of VLIB rx buffers to use. We allocate them in blocks
77 of VLIB_FRAME_SIZE (256). */
80 /** Vector of iovecs for readv/writev calls. */
82 } tuntap_per_thread_t;
85 * @brief TUNTAP node main state
89 /** per thread variables */
90 tuntap_per_thread_t *threads;
92 /** File descriptors for /dev/net/tun and provisioning socket. */
93 int dev_net_tun_fd, dev_tap_fd;
95 /** Create a "tap" [ethernet] encaps device */
98 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
100 int have_normal_interface;
102 /** tap device destination MAC address. Required, or Linux drops pkts */
105 /** Interface MTU in bytes and # of default sized buffers. */
106 u32 mtu_bytes, mtu_buffers;
108 /** Linux interface name for tun device. */
111 /** Pool of subinterface addresses */
112 subif_address_t *subifs;
114 /** Hash for subif addresses */
117 /** Unix file index */
120 /** For the "normal" interface, if configured */
121 u32 hw_if_index, sw_if_index;
125 static tuntap_main_t tuntap_main = {
128 /** Suitable defaults for an Ethernet-like tun/tap device */
129 .mtu_bytes = 4096 + 256,
136 * Output node, writes the buffers comprising the incoming frame
137 * to the tun/tap device, aka hands them to the Linux kernel stack.
139 * @param *vm - vlib_main_t
140 * @param *node - vlib_node_runtime_t
141 * @param *frame - vlib_frame_t
147 tuntap_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
149 u32 *buffers = vlib_frame_vector_args (frame);
150 uword n_packets = frame->n_vectors;
151 tuntap_main_t *tm = &tuntap_main;
152 vnet_main_t *vnm = vnet_get_main ();
153 vnet_interface_main_t *im = &vnm->interface_main;
156 u16 thread_index = vm->thread_index;
158 for (i = 0; i < n_packets; i++)
164 b = vlib_get_buffer (vm, buffers[i]);
166 if (tm->is_ether && (!tm->have_normal_interface))
168 vlib_buffer_reset (b);
169 clib_memcpy_fast (vlib_buffer_get_current (b), tm->ether_dst_mac,
173 /* Re-set iovecs if present. */
174 if (tm->threads[thread_index].iovecs)
175 _vec_len (tm->threads[thread_index].iovecs) = 0;
177 /** VLIB buffer chain -> Unix iovec(s). */
178 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
179 iov->iov_base = b->data + b->current_data;
180 iov->iov_len = l = b->current_length;
182 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
186 b = vlib_get_buffer (vm, b->next_buffer);
188 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
190 iov->iov_base = b->data + b->current_data;
191 iov->iov_len = b->current_length;
192 l += b->current_length;
194 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
197 if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
198 vec_len (tm->threads[thread_index].iovecs)) < l)
199 clib_unix_warning ("writev");
204 /* Update tuntap interface output stats. */
205 vlib_increment_combined_counter (im->combined_sw_if_counters
206 + VNET_INTERFACE_COUNTER_TX,
208 tm->sw_if_index, n_packets, n_bytes);
211 /** The normal interface path flattens the buffer chain */
212 if (tm->have_normal_interface)
213 vlib_buffer_free_no_next (vm, buffers, n_packets);
215 vlib_buffer_free (vm, buffers, n_packets);
221 VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
222 .function = tuntap_tx,
224 .type = VLIB_NODE_TYPE_INTERNAL,
230 * @brief TUNTAP receive node
233 * @param *vm - vlib_main_t
234 * @param *node - vlib_node_runtime_t
235 * @param *frame - vlib_frame_t
241 tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
243 tuntap_main_t *tm = &tuntap_main;
246 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
247 u16 thread_index = vm->thread_index;
249 /** Make sure we have some RX buffers. */
251 uword n_left = vec_len (tm->threads[thread_index].rx_buffers);
254 if (n_left < VLIB_FRAME_SIZE / 2)
256 if (!tm->threads[thread_index].rx_buffers)
257 vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE);
260 vlib_buffer_alloc (vm,
261 tm->threads[thread_index].rx_buffers + n_left,
262 VLIB_FRAME_SIZE - n_left);
263 _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc;
267 /** Allocate RX buffers from end of rx_buffers.
268 Turn them into iovecs to pass to readv. */
270 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
272 word i, n_bytes_left, n_bytes_in_packet;
274 /** We should have enough buffers left for an MTU sized packet. */
275 ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >=
278 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
279 for (i = 0; i < tm->mtu_buffers; i++)
283 tm->threads[thread_index].rx_buffers[i_rx - i]);
284 tm->threads[thread_index].iovecs[i].iov_base = b->data;
285 tm->threads[thread_index].iovecs[i].iov_len = buffer_size;
289 readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
291 n_bytes_in_packet = n_bytes_left;
292 if (n_bytes_left <= 0)
295 clib_unix_warning ("readv %d", n_bytes_left);
299 bi = tm->threads[thread_index].rx_buffers[i_rx];
303 b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]);
307 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
309 n_bytes_left -= buffer_size;
311 if (n_bytes_left <= 0)
317 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
318 b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx];
321 /** Interface counters for tuntap interface. */
322 vlib_increment_combined_counter
323 (vnet_main.interface_main.combined_sw_if_counters
324 + VNET_INTERFACE_COUNTER_RX,
325 thread_index, tm->sw_if_index, 1, n_bytes_in_packet);
327 _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
330 b = vlib_get_buffer (vm, bi);
334 uword n_trace = vlib_get_trace_count (vm, node);
336 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
337 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
340 * Turn this on if you run into
341 * "bad monkey" contexts, and you want to know exactly
342 * which nodes they've visited...
344 if (VLIB_BUFFER_TRACE_TRAJECTORY)
347 b->error = node->errors[0];
351 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
354 switch (b->data[0] & 0xf0)
357 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
360 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
363 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
367 /* The linux kernel couldn't care less if our interface is up */
368 if (tm->have_normal_interface)
370 vnet_main_t *vnm = vnet_get_main ();
371 vnet_sw_interface_t *si;
372 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
373 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
374 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
377 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b);
379 vlib_set_next_frame_buffer (vm, node, next_index, bi);
383 vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1);
384 vlib_set_trace_count (vm, node, n_trace - 1);
392 * @brief TUNTAP_RX error strings
394 static char *tuntap_rx_error_strings[] = {
395 "unknown packet type",
399 VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
400 .function = tuntap_rx,
401 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
403 .sibling_of = "device-input",
404 .type = VLIB_NODE_TYPE_INPUT,
405 .state = VLIB_NODE_STATE_INTERRUPT,
408 .error_strings = tuntap_rx_error_strings,
413 * @brief Gets called when file descriptor is ready from epoll.
415 * @param *uf - clib_file_t
417 * @return error - clib_error_t
419 static clib_error_t *
420 tuntap_read_ready (clib_file_t * uf)
422 vlib_main_t *vm = vlib_get_main ();
423 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
428 * @brief Clean up the tun/tap device
430 * @param *vm - vlib_main_t
432 * @return error - clib_error_t
435 static clib_error_t *
436 tuntap_exit (vlib_main_t * vm)
438 tuntap_main_t *tm = &tuntap_main;
443 if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
446 sfd = socket (AF_INET, SOCK_STREAM, 0);
448 clib_unix_warning ("provisioning socket");
450 clib_memset (&ifr, 0, sizeof (ifr));
451 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
453 /* get flags, modify to bring down interface... */
454 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
455 clib_unix_warning ("SIOCGIFFLAGS");
457 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
459 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
460 clib_unix_warning ("SIOCSIFFLAGS");
462 /* Turn off persistence */
463 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
464 clib_unix_warning ("TUNSETPERSIST");
465 close (tm->dev_tap_fd);
466 if (tm->dev_net_tun_fd >= 0)
467 close (tm->dev_net_tun_fd);
474 VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
477 * @brief CLI function for tun/tap config
479 * @param *vm - vlib_main_t
480 * @param *input - unformat_input_t
482 * @return error - clib_error_t
485 static clib_error_t *
486 tuntap_config (vlib_main_t * vm, unformat_input_t * input)
488 tuntap_main_t *tm = &tuntap_main;
489 clib_error_t *error = 0;
492 int flags = IFF_TUN | IFF_NO_PI;
493 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
494 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
496 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
498 if (unformat (input, "mtu %d", &tm->mtu_bytes))
500 else if (unformat (input, "enable"))
502 else if (unformat (input, "disable"))
504 else if (unformat (input, "ethernet") || unformat (input, "ether"))
506 else if (unformat (input, "have-normal-interface") ||
507 unformat (input, "have-normal"))
508 have_normal_interface = 1;
509 else if (unformat (input, "name %s", &name))
510 tm->tun_name = (char *) name;
512 return clib_error_return (0, "unknown input `%U'",
513 format_unformat_error, input);
516 tm->dev_net_tun_fd = -1;
524 clib_warning ("tuntap disabled: must be superuser");
528 tm->is_ether = is_ether;
529 tm->have_normal_interface = have_normal_interface;
532 flags = IFF_TAP | IFF_NO_PI;
534 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
536 error = clib_error_return_unix (0, "open /dev/net/tun");
540 clib_memset (&ifr, 0, sizeof (ifr));
541 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
542 ifr.ifr_flags = flags;
543 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
545 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
549 /* Make it persistent, at least until we split. */
550 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
552 error = clib_error_return_unix (0, "TUNSETPERSIST");
556 /* Open a provisioning socket */
557 if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
559 error = clib_error_return_unix (0, "socket");
563 /* Find the interface index. */
566 struct sockaddr_ll sll;
568 clib_memset (&ifr, 0, sizeof (ifr));
569 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
570 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
572 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
576 /* Bind the provisioning socket to the interface. */
577 clib_memset (&sll, 0, sizeof (sll));
578 sll.sll_family = AF_PACKET;
579 sll.sll_ifindex = ifr.ifr_ifindex;
580 sll.sll_protocol = htons (ETH_P_ALL);
582 if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
584 error = clib_error_return_unix (0, "bind");
589 /* non-blocking I/O on /dev/tapX */
592 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
594 error = clib_error_return_unix (0, "ioctl FIONBIO");
599 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
601 ifr.ifr_mtu = tm->mtu_bytes;
602 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
604 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
608 /* get flags, modify to bring up interface... */
609 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
611 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
615 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
617 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
619 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
625 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
627 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
631 clib_memcpy_fast (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
634 if (have_normal_interface)
636 vnet_main_t *vnm = vnet_get_main ();
637 error = ethernet_register_interface
638 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
639 tm->ether_dst_mac /* ethernet address */ ,
640 &tm->hw_if_index, 0 /* flag change */ );
642 clib_error_report (error);
643 tm->sw_if_index = tm->hw_if_index;
644 vm->os_punt_frame = tuntap_nopunt_frame;
648 vnet_main_t *vnm = vnet_get_main ();
649 vnet_hw_interface_t *hi;
651 vm->os_punt_frame = tuntap_punt_frame;
653 tm->hw_if_index = vnet_register_interface
654 (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
655 tuntap_interface_class.index, 0);
656 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
657 tm->sw_if_index = hi->sw_if_index;
659 /* Interface is always up. */
660 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
661 VNET_HW_INTERFACE_FLAG_LINK_UP);
662 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
663 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
667 clib_file_t template = { 0 };
668 template.read_function = tuntap_read_ready;
669 template.file_descriptor = tm->dev_net_tun_fd;
670 tm->clib_file_index = clib_file_add (&file_main, &template);
676 if (tm->dev_net_tun_fd >= 0)
677 close (tm->dev_net_tun_fd);
678 if (tm->dev_tap_fd >= 0)
679 close (tm->dev_tap_fd);
685 VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
688 * @brief Add or Del IP4 address to tun/tap interface
690 * @param *im - ip4_main_t
691 * @param opaque - uword
692 * @param sw_if_index - u32
693 * @param *address - ip4_address_t
694 * @param is_delete - u32
698 tuntap_ip4_add_del_interface_address (ip4_main_t * im,
701 ip4_address_t * address,
703 u32 if_address_index, u32 is_delete)
705 tuntap_main_t *tm = &tuntap_main;
707 subif_address_t subif_addr, *ap;
710 /** Tuntap disabled, or using a "normal" interface. */
711 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
714 /* if the address is being applied to an interface that is not in
715 * the same table/VRF as this tap, then ignore it.
716 * If we don't do this overlapping address spaces in the different tables
717 * breaks the linux host's routing tables */
718 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
720 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index))
723 /** See if we already know about this subif */
724 clib_memset (&subif_addr, 0, sizeof (subif_addr));
725 subif_addr.sw_if_index = sw_if_index;
726 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
728 p = mhash_get (&tm->subif_mhash, &subif_addr);
731 ap = pool_elt_at_index (tm->subifs, p[0]);
734 pool_get (tm->subifs, ap);
736 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
739 /* Use subif pool index to select alias device. */
740 clib_memset (&ifr, 0, sizeof (ifr));
741 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
742 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
744 /* the tuntap punt/inject is enabled for IPv4 RX so long as
745 * any vpp interface has an IPv4 address.
746 * this is also ref counted.
748 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
752 struct sockaddr_in *sin;
754 sin = (struct sockaddr_in *) &ifr.ifr_addr;
756 /* Set ipv4 address, netmask. */
757 sin->sin_family = AF_INET;
758 clib_memcpy_fast (&sin->sin_addr.s_addr, address, 4);
759 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
760 clib_unix_warning ("ioctl SIOCSIFADDR");
762 sin->sin_addr.s_addr = im->fib_masks[address_length];
763 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
764 clib_unix_warning ("ioctl SIOCSIFNETMASK");
768 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
769 pool_put (tm->subifs, ap);
772 /* get flags, modify to bring up interface... */
773 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
774 clib_unix_warning ("ioctl SIOCGIFFLAGS");
777 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
779 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
781 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
782 clib_unix_warning ("ioctl SIOCSIFFLAGS");
786 * @brief workaround for a known include file bug.
787 * including @c <linux/ipv6.h> causes multiple definitions if
788 * @c <netinet/in.h is also included.
792 struct in6_addr ifr6_addr;
798 * @brief Add or Del tun/tap interface address.
800 * Both the v6 interface address API and the way ifconfig
801 * displays subinterfaces differ from their v4 counterparts.
802 * The code given here seems to work but YMMV.
804 * @param *im - ip6_main_t
805 * @param opaque - uword
806 * @param sw_if_index - u32
807 * @param *address - ip6_address_t
808 * @param address_length - u32
809 * @param if_address_index - u32
810 * @param is_delete - u32
813 tuntap_ip6_add_del_interface_address (ip6_main_t * im,
816 ip6_address_t * address,
818 u32 if_address_index, u32 is_delete)
820 tuntap_main_t *tm = &tuntap_main;
822 struct in6_ifreq ifr6;
823 subif_address_t subif_addr, *ap;
826 /* Tuntap disabled, or using a "normal" interface. */
827 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
830 /* if the address is being applied to an interface that is not in
831 * the same table/VRF as this tap, then ignore it.
832 * If we don't do this overlapping address spaces in the different tables
833 * breaks the linux host's routing tables */
834 if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
836 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index))
839 /* See if we already know about this subif */
840 clib_memset (&subif_addr, 0, sizeof (subif_addr));
841 subif_addr.sw_if_index = sw_if_index;
842 subif_addr.is_v6 = 1;
843 clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address));
845 p = mhash_get (&tm->subif_mhash, &subif_addr);
848 ap = pool_elt_at_index (tm->subifs, p[0]);
851 pool_get (tm->subifs, ap);
853 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
856 /* Use subif pool index to select alias device. */
857 clib_memset (&ifr, 0, sizeof (ifr));
858 clib_memset (&ifr6, 0, sizeof (ifr6));
859 snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
860 "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
862 /* the tuntap punt/inject is enabled for IPv6 RX so long as
863 * any vpp interface has an IPv6 address.
864 * this is also ref counted.
866 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
870 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
872 clib_unix_warning ("get ifindex socket");
874 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
875 clib_unix_warning ("get ifindex");
877 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
878 ifr6.ifr6_prefixlen = address_length;
879 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
881 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
882 clib_unix_warning ("set address");
889 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
891 clib_unix_warning ("get ifindex socket");
893 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
894 clib_unix_warning ("get ifindex");
896 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
897 ifr6.ifr6_prefixlen = address_length;
898 clib_memcpy_fast (&ifr6.ifr6_addr, address, 16);
900 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
901 clib_unix_warning ("del address");
906 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
907 pool_put (tm->subifs, ap);
912 * @brief TX the tun/tap frame
914 * @param *vm - vlib_main_t
915 * @param *node - vlib_node_runtime_t
916 * @param *frame - vlib_frame_t
920 tuntap_punt_frame (vlib_main_t * vm,
921 vlib_node_runtime_t * node, vlib_frame_t * frame)
923 tuntap_tx (vm, node, frame);
924 vlib_frame_free (vm, node, frame);
928 * @brief Free the tun/tap frame
930 * @param *vm - vlib_main_t
931 * @param *node - vlib_node_runtime_t
932 * @param *frame - vlib_frame_t
936 tuntap_nopunt_frame (vlib_main_t * vm,
937 vlib_node_runtime_t * node, vlib_frame_t * frame)
939 u32 *buffers = vlib_frame_vector_args (frame);
940 uword n_packets = frame->n_vectors;
941 vlib_buffer_free (vm, buffers, n_packets);
942 vlib_frame_free (vm, node, frame);
946 VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
948 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
953 * @brief Format tun/tap interface name
955 * @param *s - u8 - formatter string
956 * @param *args - va_list
958 * @return *s - u8 - formatted string
962 format_tuntap_interface_name (u8 * s, va_list * args)
964 u32 i = va_arg (*args, u32);
966 s = format (s, "tuntap-%d", i);
971 * @brief TX packet out tun/tap
973 * @param *vm - vlib_main_t
974 * @param *node - vlib_node_runtime_t
975 * @param *frame - vlib_frame_t
977 * @return n_buffers - uword - Packets transmitted
981 tuntap_intfc_tx (vlib_main_t * vm,
982 vlib_node_runtime_t * node, vlib_frame_t * frame)
984 tuntap_main_t *tm = &tuntap_main;
985 u32 *buffers = vlib_frame_vector_args (frame);
986 uword n_buffers = frame->n_vectors;
988 /* Normal interface transmit happens only on the normal interface... */
989 if (tm->have_normal_interface)
990 return tuntap_tx (vm, node, frame);
992 vlib_buffer_free (vm, buffers, n_buffers);
997 VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
999 .tx_function = tuntap_intfc_tx,
1000 .format_device_name = format_tuntap_interface_name,
1005 * @brief tun/tap node init
1007 * @param *vm - vlib_main_t
1009 * @return error - clib_error_t
1012 static clib_error_t *
1013 tuntap_init (vlib_main_t * vm)
1015 ip4_main_t *im4 = &ip4_main;
1016 ip6_main_t *im6 = &ip6_main;
1017 ip4_add_del_interface_address_callback_t cb4;
1018 ip6_add_del_interface_address_callback_t cb6;
1019 tuntap_main_t *tm = &tuntap_main;
1020 vlib_thread_main_t *m = vlib_get_thread_main ();
1022 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t));
1024 cb4.function = tuntap_ip4_add_del_interface_address;
1025 cb4.function_opaque = 0;
1026 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1028 cb6.function = tuntap_ip6_add_del_interface_address;
1029 cb6.function_opaque = 0;
1030 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
1031 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1032 CLIB_CACHE_LINE_BYTES);
1038 VLIB_INIT_FUNCTION (tuntap_init) =
1040 .runs_after = VLIB_INITS("ip4_init"),
1045 * fd.io coding-style-patch-verification: ON
1048 * eval: (c-set-style "gnu")