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>
49 #include <vnet/ethernet/ethernet.h>
50 #include <vnet/devices/devices.h>
51 #include <vnet/feature/feature.h>
54 #include <vnet/devices/dpdk/dpdk.h>
57 static vnet_device_class_t tuntap_dev_class;
58 static vnet_hw_interface_class_t tuntap_interface_class;
60 static void tuntap_punt_frame (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
62 vlib_frame_t * frame);
63 static void tuntap_nopunt_frame (vlib_main_t * vm,
64 vlib_node_runtime_t * node,
65 vlib_frame_t * frame);
74 * @brief TUNTAP node main state
77 /** Vector of iovecs for readv/writev calls. */
78 struct iovec * iovecs;
80 /** Vector of VLIB rx buffers to use. We allocate them in blocks
81 of VLIB_FRAME_SIZE (256). */
84 /** File descriptors for /dev/net/tun and provisioning socket. */
85 int dev_net_tun_fd, dev_tap_fd;
87 /** Create a "tap" [ethernet] encaps device */
90 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
92 int have_normal_interface;
94 /** tap device destination MAC address. Required, or Linux drops pkts */
97 /** Interface MTU in bytes and # of default sized buffers. */
98 u32 mtu_bytes, mtu_buffers;
100 /** Linux interface name for tun device. */
103 /** Pool of subinterface addresses */
104 subif_address_t *subifs;
106 /** Hash for subif addresses */
109 /** Unix file index */
112 /** For the "normal" interface, if configured */
113 u32 hw_if_index, sw_if_index;
117 static tuntap_main_t tuntap_main = {
120 /** Suitable defaults for an Ethernet-like tun/tap device */
121 .mtu_bytes = 4096 + 256,
128 * Output node, writes the buffers comprising the incoming frame
129 * to the tun/tap device, aka hands them to the Linux kernel stack.
131 * @param *vm - vlib_main_t
132 * @param *node - vlib_node_runtime_t
133 * @param *frame - vlib_frame_t
139 tuntap_tx (vlib_main_t * vm,
140 vlib_node_runtime_t * node,
141 vlib_frame_t * frame)
143 u32 * buffers = vlib_frame_args (frame);
144 uword n_packets = frame->n_vectors;
145 tuntap_main_t * tm = &tuntap_main;
146 vnet_main_t *vnm = vnet_get_main ();
147 vnet_interface_main_t *im = &vnm->interface_main;
151 for (i = 0; i < n_packets; i++)
157 b = vlib_get_buffer (vm, buffers[i]);
159 if (tm->is_ether && (!tm->have_normal_interface))
161 vlib_buffer_reset(b);
162 clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
165 /* Re-set iovecs if present. */
167 _vec_len (tm->iovecs) = 0;
169 /** VLIB buffer chain -> Unix iovec(s). */
170 vec_add2 (tm->iovecs, iov, 1);
171 iov->iov_base = b->data + b->current_data;
172 iov->iov_len = l = b->current_length;
174 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
177 b = vlib_get_buffer (vm, b->next_buffer);
179 vec_add2 (tm->iovecs, iov, 1);
181 iov->iov_base = b->data + b->current_data;
182 iov->iov_len = b->current_length;
183 l += b->current_length;
184 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
187 if (writev (tm->dev_net_tun_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
188 clib_unix_warning ("writev");
193 /* Update tuntap interface output stats. */
194 vlib_increment_combined_counter (im->combined_sw_if_counters
195 + VNET_INTERFACE_COUNTER_TX,
197 tm->sw_if_index, n_packets, n_bytes);
200 /** The normal interface path flattens the buffer chain */
201 if (tm->have_normal_interface)
202 vlib_buffer_free_no_next (vm, buffers, n_packets);
204 vlib_buffer_free (vm, buffers, n_packets);
209 VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
210 .function = tuntap_tx,
212 .type = VLIB_NODE_TYPE_INTERNAL,
217 * @brief TUNTAP receive node
220 * @param *vm - vlib_main_t
221 * @param *node - vlib_node_runtime_t
222 * @param *frame - vlib_frame_t
228 tuntap_rx (vlib_main_t * vm,
229 vlib_node_runtime_t * node,
230 vlib_frame_t * frame)
232 tuntap_main_t * tm = &tuntap_main;
235 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
237 u32 free_list_index = VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX;
239 dpdk_main_t * dm = &dpdk_main;
240 u32 free_list_index = dm->vlib_buffer_free_list_index;
241 struct rte_mbuf *first_mb = NULL, *prev_mb = NULL;
244 /** Make sure we have some RX buffers. */
246 uword n_left = vec_len (tm->rx_buffers);
249 if (n_left < VLIB_FRAME_SIZE / 2)
251 if (! tm->rx_buffers)
252 vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
254 n_alloc = vlib_buffer_alloc_from_free_list
255 (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left,
257 _vec_len (tm->rx_buffers) = n_left + n_alloc;
261 /** Allocate RX buffers from end of rx_buffers.
262 Turn them into iovecs to pass to readv. */
264 uword i_rx = vec_len (tm->rx_buffers) - 1;
266 word i, n_bytes_left, n_bytes_in_packet;
268 /** We should have enough buffers left for an MTU sized packet. */
269 ASSERT (vec_len (tm->rx_buffers) >= tm->mtu_buffers);
271 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
272 for (i = 0; i < tm->mtu_buffers; i++)
274 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - i]);
275 tm->iovecs[i].iov_base = b->data;
276 tm->iovecs[i].iov_len = buffer_size;
279 n_bytes_left = readv (tm->dev_net_tun_fd, tm->iovecs, tm->mtu_buffers);
280 n_bytes_in_packet = n_bytes_left;
281 if (n_bytes_left <= 0)
284 clib_unix_warning ("readv %d", n_bytes_left);
288 bi = tm->rx_buffers[i_rx];
293 struct rte_mbuf * mb;
295 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
297 mb = rte_mbuf_from_vlib_buffer(b);
299 if (first_mb == NULL)
310 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
312 n_bytes_left -= buffer_size;
314 rte_pktmbuf_data_len (mb) = b->current_length;
315 mb->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
318 if (n_bytes_left <= 0)
321 rte_pktmbuf_pkt_len (first_mb) = n_bytes_in_packet;
327 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
328 b->next_buffer = tm->rx_buffers[i_rx];
334 /** Interface counters for tuntap interface. */
335 vlib_increment_combined_counter
336 (vnet_main.interface_main.combined_sw_if_counters
337 + VNET_INTERFACE_COUNTER_RX,
340 1, n_bytes_in_packet);
342 _vec_len (tm->rx_buffers) = i_rx;
345 b = vlib_get_buffer (vm, bi);
349 uword n_trace = vlib_get_trace_count (vm, node);
351 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
352 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
355 * Turn this on if you run into
356 * "bad monkey" contexts, and you want to know exactly
357 * which nodes they've visited...
359 if (VLIB_BUFFER_TRACE_TRAJECTORY)
362 b->error = node->errors[0];
366 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
369 switch (b->data[0] & 0xf0)
372 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
375 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
378 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
382 /* The linux kernel couldn't care less if our interface is up */
383 if (tm->have_normal_interface)
385 vnet_main_t *vnm = vnet_get_main();
386 vnet_sw_interface_t * si;
387 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
388 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
389 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
392 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b, 0);
394 vlib_set_next_frame_buffer (vm, node, next_index, bi);
398 vlib_trace_buffer (vm, node, next_index,
399 b, /* follow_chain */ 1);
400 vlib_set_trace_count (vm, node, n_trace - 1);
408 * @brief TUNTAP_RX error strings
410 static char * tuntap_rx_error_strings[] = {
411 "unknown packet type",
414 VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
415 .function = tuntap_rx,
417 .sibling_of = "device-input",
418 .type = VLIB_NODE_TYPE_INPUT,
419 .state = VLIB_NODE_STATE_INTERRUPT,
422 .error_strings = tuntap_rx_error_strings,
426 * @brief Gets called when file descriptor is ready from epoll.
428 * @param *uf - unix_file_t
430 * @return error - clib_error_t
432 static clib_error_t * tuntap_read_ready (unix_file_t * uf)
434 vlib_main_t * vm = vlib_get_main();
435 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
440 * @brief Clean up the tun/tap device
442 * @param *vm - vlib_main_t
444 * @return error - clib_error_t
447 static clib_error_t *
448 tuntap_exit (vlib_main_t * vm)
450 tuntap_main_t *tm = &tuntap_main;
455 if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
458 sfd = socket (AF_INET, SOCK_STREAM, 0);
460 clib_unix_warning("provisioning socket");
462 memset(&ifr, 0, sizeof (ifr));
463 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1);
465 /* get flags, modify to bring down interface... */
466 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
467 clib_unix_warning ("SIOCGIFFLAGS");
469 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
471 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
472 clib_unix_warning ("SIOCSIFFLAGS");
474 /* Turn off persistence */
475 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
476 clib_unix_warning ("TUNSETPERSIST");
477 close(tm->dev_tap_fd);
478 if (tm->dev_net_tun_fd >= 0)
479 close(tm->dev_net_tun_fd);
486 VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
489 * @brief CLI function for tun/tap config
491 * @param *vm - vlib_main_t
492 * @param *input - unformat_input_t
494 * @return error - clib_error_t
497 static clib_error_t *
498 tuntap_config (vlib_main_t * vm, unformat_input_t * input)
500 tuntap_main_t *tm = &tuntap_main;
501 clib_error_t * error = 0;
504 int flags = IFF_TUN | IFF_NO_PI;
505 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
506 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
508 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510 if (unformat (input, "mtu %d", &tm->mtu_bytes))
512 else if (unformat (input, "enable"))
514 else if (unformat (input, "disable"))
516 else if (unformat (input, "ethernet") ||
517 unformat (input, "ether"))
519 else if (unformat (input, "have-normal-interface") ||
520 unformat (input, "have-normal"))
521 have_normal_interface = 1;
522 else if (unformat (input, "name %s", &name))
523 tm->tun_name = (char *) name;
525 return clib_error_return (0, "unknown input `%U'",
526 format_unformat_error, input);
529 tm->dev_net_tun_fd = -1;
537 clib_warning ("tuntap disabled: must be superuser");
541 tm->is_ether = is_ether;
542 tm->have_normal_interface = have_normal_interface;
545 flags = IFF_TAP | IFF_NO_PI;
547 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
549 error = clib_error_return_unix (0, "open /dev/net/tun");
553 memset (&ifr, 0, sizeof (ifr));
554 strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
555 ifr.ifr_flags = flags;
556 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
558 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
562 /* Make it persistent, at least until we split. */
563 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
565 error = clib_error_return_unix (0, "TUNSETPERSIST");
569 /* Open a provisioning socket */
570 if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
571 htons(ETH_P_ALL))) < 0 )
573 error = clib_error_return_unix (0, "socket");
577 /* Find the interface index. */
580 struct sockaddr_ll sll;
582 memset (&ifr, 0, sizeof(ifr));
583 strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
584 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
586 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
590 /* Bind the provisioning socket to the interface. */
591 memset(&sll, 0, sizeof(sll));
592 sll.sll_family = AF_PACKET;
593 sll.sll_ifindex = ifr.ifr_ifindex;
594 sll.sll_protocol = htons(ETH_P_ALL);
596 if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
598 error = clib_error_return_unix (0, "bind");
603 /* non-blocking I/O on /dev/tapX */
606 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
608 error = clib_error_return_unix (0, "ioctl FIONBIO");
613 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
615 ifr.ifr_mtu = tm->mtu_bytes;
616 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
618 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
622 /* get flags, modify to bring up interface... */
623 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
625 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
629 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
631 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
633 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
639 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
641 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
645 clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
648 if (have_normal_interface)
650 vnet_main_t *vnm = vnet_get_main();
651 error = ethernet_register_interface
653 tuntap_dev_class.index,
654 0 /* device instance */,
655 tm->ether_dst_mac /* ethernet address */,
657 0 /* flag change */);
659 clib_error_report (error);
660 tm->sw_if_index = tm->hw_if_index;
661 vm->os_punt_frame = tuntap_nopunt_frame;
665 vnet_main_t *vnm = vnet_get_main();
666 vnet_hw_interface_t * hi;
668 vm->os_punt_frame = tuntap_punt_frame;
670 tm->hw_if_index = vnet_register_interface
672 tuntap_dev_class.index, 0 /* device instance */,
673 tuntap_interface_class.index, 0);
674 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
675 tm->sw_if_index = hi->sw_if_index;
677 /* Interface is always up. */
678 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
679 VNET_HW_INTERFACE_FLAG_LINK_UP);
680 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
681 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
685 unix_file_t template = {0};
686 template.read_function = tuntap_read_ready;
687 template.file_descriptor = tm->dev_net_tun_fd;
688 tm->unix_file_index = unix_file_add (&unix_main, &template);
694 if (tm->dev_net_tun_fd >= 0)
695 close (tm->dev_net_tun_fd);
696 if (tm->dev_tap_fd >= 0)
697 close (tm->dev_tap_fd);
703 VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
706 * @brief Add or Del IP4 address to tun/tap interface
708 * @param *im - ip4_main_t
709 * @param opaque - uword
710 * @param sw_if_index - u32
711 * @param *address - ip4_address_t
712 * @param is_delete - u32
716 tuntap_ip4_add_del_interface_address (ip4_main_t * im,
719 ip4_address_t * address,
721 u32 if_address_index,
724 tuntap_main_t * tm = &tuntap_main;
726 subif_address_t subif_addr, * ap;
729 /** Tuntap disabled, or using a "normal" interface. */
730 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
733 /** See if we already know about this subif */
734 memset (&subif_addr, 0, sizeof (subif_addr));
735 subif_addr.sw_if_index = sw_if_index;
736 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
738 p = mhash_get (&tm->subif_mhash, &subif_addr);
741 ap = pool_elt_at_index (tm->subifs, p[0]);
744 pool_get (tm->subifs, ap);
746 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
749 /* Use subif pool index to select alias device. */
750 memset (&ifr, 0, sizeof (ifr));
751 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
752 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
754 /* the tuntap punt/inject is enabled for IPv4 RX so long as
755 * any vpp interface has an IPv4 address.
756 * this is also ref counted.
758 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
762 struct sockaddr_in * sin;
764 sin = (struct sockaddr_in *)&ifr.ifr_addr;
766 /* Set ipv4 address, netmask. */
767 sin->sin_family = AF_INET;
768 clib_memcpy (&sin->sin_addr.s_addr, address, 4);
769 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
770 clib_unix_warning ("ioctl SIOCSIFADDR");
772 sin->sin_addr.s_addr = im->fib_masks[address_length];
773 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
774 clib_unix_warning ("ioctl SIOCSIFNETMASK");
778 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
779 pool_put (tm->subifs, ap);
782 /* get flags, modify to bring up interface... */
783 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
784 clib_unix_warning ("ioctl SIOCGIFFLAGS");
787 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
789 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
791 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
792 clib_unix_warning ("ioctl SIOCSIFFLAGS");
796 * @brief workaround for a known include file bug.
797 * including @c <linux/ipv6.h> causes multiple definitions if
798 * @c <netinet/in.h is also included.
801 struct in6_addr ifr6_addr;
807 * @brief Add or Del tun/tap interface address.
809 * Both the v6 interface address API and the way ifconfig
810 * displays subinterfaces differ from their v4 couterparts.
811 * The code given here seems to work but YMMV.
813 * @param *im - ip6_main_t
814 * @param opaque - uword
815 * @param sw_if_index - u32
816 * @param *address - ip6_address_t
817 * @param address_length - u32
818 * @param if_address_index - u32
819 * @param is_delete - u32
822 tuntap_ip6_add_del_interface_address (ip6_main_t * im,
825 ip6_address_t * address,
827 u32 if_address_index,
830 tuntap_main_t * tm = &tuntap_main;
832 struct in6_ifreq ifr6;
833 subif_address_t subif_addr, * ap;
836 /* Tuntap disabled, or using a "normal" interface. */
837 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
840 /* See if we already know about this subif */
841 memset (&subif_addr, 0, sizeof (subif_addr));
842 subif_addr.sw_if_index = sw_if_index;
843 subif_addr.is_v6 = 1;
844 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
846 p = mhash_get (&tm->subif_mhash, &subif_addr);
849 ap = pool_elt_at_index (tm->subifs, p[0]);
852 pool_get (tm->subifs, ap);
854 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
857 /* Use subif pool index to select alias device. */
858 memset (&ifr, 0, sizeof (ifr));
859 memset (&ifr6, 0, sizeof (ifr6));
860 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
861 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
863 /* the tuntap punt/inject is enabled for IPv6 RX so long as
864 * any vpp interface has an IPv6 address.
865 * this is also ref counted.
867 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
871 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
873 clib_unix_warning ("get ifindex socket");
875 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
876 clib_unix_warning ("get ifindex");
878 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
879 ifr6.ifr6_prefixlen = address_length;
880 clib_memcpy (&ifr6.ifr6_addr, address, 16);
882 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
883 clib_unix_warning ("set address");
890 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
892 clib_unix_warning ("get ifindex socket");
894 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
895 clib_unix_warning ("get ifindex");
897 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
898 ifr6.ifr6_prefixlen = address_length;
899 clib_memcpy (&ifr6.ifr6_addr, address, 16);
901 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
902 clib_unix_warning ("del address");
907 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
908 pool_put (tm->subifs, ap);
913 * @brief TX the tun/tap frame
915 * @param *vm - vlib_main_t
916 * @param *node - vlib_node_runtime_t
917 * @param *frame - vlib_frame_t
921 tuntap_punt_frame (vlib_main_t * vm,
922 vlib_node_runtime_t * node,
923 vlib_frame_t * frame)
925 tuntap_tx (vm, node, frame);
926 vlib_frame_free (vm, node, frame);
930 * @brief Free the tun/tap frame
932 * @param *vm - vlib_main_t
933 * @param *node - vlib_node_runtime_t
934 * @param *frame - vlib_frame_t
938 tuntap_nopunt_frame (vlib_main_t * vm,
939 vlib_node_runtime_t * node,
940 vlib_frame_t * frame)
942 u32 * buffers = vlib_frame_args (frame);
943 uword n_packets = frame->n_vectors;
944 vlib_buffer_free (vm, buffers, n_packets);
945 vlib_frame_free (vm, node, frame);
948 VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
950 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
954 * @brief Format tun/tap interface name
956 * @param *s - u8 - formatter string
957 * @param *args - va_list
959 * @return *s - u8 - formatted string
962 static u8 * 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,
983 vlib_frame_t * frame)
985 tuntap_main_t * tm = &tuntap_main;
986 u32 * buffers = vlib_frame_args (frame);
987 uword n_buffers = frame->n_vectors;
989 /* Normal interface transmit happens only on the normal interface... */
990 if (tm->have_normal_interface)
991 return tuntap_tx (vm, node, frame);
993 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,
1004 * @brief tun/tap node init
1006 * @param *vm - vlib_main_t
1008 * @return error - clib_error_t
1011 static clib_error_t *
1012 tuntap_init (vlib_main_t * vm)
1014 clib_error_t * error;
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;
1021 error = vlib_call_init_function (vm, ip4_init);
1025 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t));
1027 cb4.function = tuntap_ip4_add_del_interface_address;
1028 cb4.function_opaque = 0;
1029 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1031 cb6.function = tuntap_ip6_add_del_interface_address;
1032 cb6.function_opaque = 0;
1033 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
1038 VLIB_INIT_FUNCTION (tuntap_init);