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>
52 #include <vnet/devices/dpdk/dpdk.h>
55 static vnet_device_class_t tuntap_dev_class;
56 static vnet_hw_interface_class_t tuntap_interface_class;
58 static void tuntap_punt_frame (vlib_main_t * vm,
59 vlib_node_runtime_t * node,
60 vlib_frame_t * frame);
61 static void tuntap_nopunt_frame (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
63 vlib_frame_t * frame);
72 * @brief TUNTAP node main state
75 /** Vector of iovecs for readv/writev calls. */
76 struct iovec * iovecs;
78 /** Vector of VLIB rx buffers to use. We allocate them in blocks
79 of VLIB_FRAME_SIZE (256). */
82 /** File descriptors for /dev/net/tun and provisioning socket. */
83 int dev_net_tun_fd, dev_tap_fd;
85 /** Create a "tap" [ethernet] encaps device */
88 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
90 int have_normal_interface;
92 /** tap device destination MAC address. Required, or Linux drops pkts */
95 /** Interface MTU in bytes and # of default sized buffers. */
96 u32 mtu_bytes, mtu_buffers;
98 /** Linux interface name for tun device. */
101 /** Pool of subinterface addresses */
102 subif_address_t *subifs;
104 /** Hash for subif addresses */
107 /** Unix file index */
110 /** For the "normal" interface, if configured */
111 u32 hw_if_index, sw_if_index;
115 static tuntap_main_t tuntap_main = {
118 /** Suitable defaults for an Ethernet-like tun/tap device */
119 .mtu_bytes = 4096 + 256,
126 * Output node, writes the buffers comprising the incoming frame
127 * to the tun/tap device, aka hands them to the Linux kernel stack.
129 * @param *vm - vlib_main_t
130 * @param *node - vlib_node_runtime_t
131 * @param *frame - vlib_frame_t
137 tuntap_tx (vlib_main_t * vm,
138 vlib_node_runtime_t * node,
139 vlib_frame_t * frame)
141 u32 * buffers = vlib_frame_args (frame);
142 uword n_packets = frame->n_vectors;
143 tuntap_main_t * tm = &tuntap_main;
146 for (i = 0; i < n_packets; i++)
152 b = vlib_get_buffer (vm, buffers[i]);
154 if (tm->is_ether && (!tm->have_normal_interface))
156 vlib_buffer_reset(b);
157 clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
160 /* Re-set iovecs if present. */
162 _vec_len (tm->iovecs) = 0;
164 /** VLIB buffer chain -> Unix iovec(s). */
165 vec_add2 (tm->iovecs, iov, 1);
166 iov->iov_base = b->data + b->current_data;
167 iov->iov_len = l = b->current_length;
169 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
172 b = vlib_get_buffer (vm, b->next_buffer);
174 vec_add2 (tm->iovecs, iov, 1);
176 iov->iov_base = b->data + b->current_data;
177 iov->iov_len = b->current_length;
178 l += b->current_length;
179 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
182 if (writev (tm->dev_net_tun_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
183 clib_unix_warning ("writev");
186 /** The normal interface path flattens the buffer chain */
187 if (tm->have_normal_interface)
188 vlib_buffer_free_no_next (vm, buffers, n_packets);
190 vlib_buffer_free (vm, buffers, n_packets);
195 VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
196 .function = tuntap_tx,
198 .type = VLIB_NODE_TYPE_INTERNAL,
203 TUNTAP_RX_NEXT_IP4_INPUT,
204 TUNTAP_RX_NEXT_IP6_INPUT,
205 TUNTAP_RX_NEXT_ETHERNET_INPUT,
211 * @brief TUNTAP receive node
214 * @param *vm - vlib_main_t
215 * @param *node - vlib_node_runtime_t
216 * @param *frame - vlib_frame_t
222 tuntap_rx (vlib_main_t * vm,
223 vlib_node_runtime_t * node,
224 vlib_frame_t * frame)
226 tuntap_main_t * tm = &tuntap_main;
229 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
231 u32 free_list_index = VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX;
233 dpdk_main_t * dm = &dpdk_main;
234 u32 free_list_index = dm->vlib_buffer_free_list_index;
235 struct rte_mbuf *first_mb = NULL, *prev_mb = NULL;
238 /** Make sure we have some RX buffers. */
240 uword n_left = vec_len (tm->rx_buffers);
243 if (n_left < VLIB_FRAME_SIZE / 2)
245 if (! tm->rx_buffers)
246 vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
248 n_alloc = vlib_buffer_alloc_from_free_list
249 (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left,
251 _vec_len (tm->rx_buffers) = n_left + n_alloc;
255 /** Allocate RX buffers from end of rx_buffers.
256 Turn them into iovecs to pass to readv. */
258 uword i_rx = vec_len (tm->rx_buffers) - 1;
260 word i, n_bytes_left, n_bytes_in_packet;
262 /** We should have enough buffers left for an MTU sized packet. */
263 ASSERT (vec_len (tm->rx_buffers) >= tm->mtu_buffers);
265 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
266 for (i = 0; i < tm->mtu_buffers; i++)
268 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - i]);
269 tm->iovecs[i].iov_base = b->data;
270 tm->iovecs[i].iov_len = buffer_size;
273 n_bytes_left = readv (tm->dev_net_tun_fd, tm->iovecs, tm->mtu_buffers);
274 n_bytes_in_packet = n_bytes_left;
275 if (n_bytes_left <= 0)
278 clib_unix_warning ("readv %d", n_bytes_left);
282 bi = tm->rx_buffers[i_rx];
287 struct rte_mbuf * mb;
289 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
291 mb = rte_mbuf_from_vlib_buffer(b);
293 if (first_mb == NULL)
304 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
306 n_bytes_left -= buffer_size;
308 rte_pktmbuf_data_len (mb) = b->current_length;
309 mb->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
312 if (n_bytes_left <= 0)
315 rte_pktmbuf_pkt_len (first_mb) = n_bytes_in_packet;
321 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
322 b->next_buffer = tm->rx_buffers[i_rx];
328 /** Interface counters for tuntap interface. */
329 vlib_increment_combined_counter
330 (vnet_main.interface_main.combined_sw_if_counters
331 + VNET_INTERFACE_COUNTER_RX,
334 1, n_bytes_in_packet);
336 _vec_len (tm->rx_buffers) = i_rx;
339 b = vlib_get_buffer (vm, bi);
343 uword n_trace = vlib_get_trace_count (vm, node);
345 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
346 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
349 * Turn this on if you run into
350 * "bad monkey" contexts, and you want to know exactly
351 * which nodes they've visited...
353 if (VLIB_BUFFER_TRACE_TRAJECTORY)
356 b->error = node->errors[0];
360 next_index = TUNTAP_RX_NEXT_ETHERNET_INPUT;
363 switch (b->data[0] & 0xf0)
366 next_index = TUNTAP_RX_NEXT_IP4_INPUT;
369 next_index = TUNTAP_RX_NEXT_IP6_INPUT;
372 next_index = TUNTAP_RX_NEXT_DROP;
376 /* The linux kernel couldn't care less if our interface is up */
377 if (tm->have_normal_interface)
379 vnet_main_t *vnm = vnet_get_main();
380 vnet_sw_interface_t * si;
381 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
382 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
383 next_index = TUNTAP_RX_NEXT_DROP;
386 vlib_set_next_frame_buffer (vm, node, next_index, bi);
390 vlib_trace_buffer (vm, node, next_index,
391 b, /* follow_chain */ 1);
392 vlib_set_trace_count (vm, node, n_trace - 1);
400 * @brief TUNTAP_RX error strings
402 static char * tuntap_rx_error_strings[] = {
403 "unknown packet type",
406 VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
407 .function = tuntap_rx,
409 .type = VLIB_NODE_TYPE_INPUT,
410 .state = VLIB_NODE_STATE_INTERRUPT,
413 .error_strings = tuntap_rx_error_strings,
415 .n_next_nodes = TUNTAP_RX_N_NEXT,
417 [TUNTAP_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
418 [TUNTAP_RX_NEXT_IP6_INPUT] = "ip6-input",
419 [TUNTAP_RX_NEXT_DROP] = "error-drop",
420 [TUNTAP_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
425 * @brief Gets called when file descriptor is ready from epoll.
427 * @param *uf - unix_file_t
429 * @return error - clib_error_t
431 static clib_error_t * tuntap_read_ready (unix_file_t * uf)
433 vlib_main_t * vm = vlib_get_main();
434 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
439 * @brief Clean up the tun/tap device
441 * @param *vm - vlib_main_t
443 * @return error - clib_error_t
446 static clib_error_t *
447 tuntap_exit (vlib_main_t * vm)
449 tuntap_main_t *tm = &tuntap_main;
454 if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
457 sfd = socket (AF_INET, SOCK_STREAM, 0);
459 clib_unix_warning("provisioning socket");
461 memset(&ifr, 0, sizeof (ifr));
462 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1);
464 /* get flags, modify to bring down interface... */
465 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
466 clib_unix_warning ("SIOCGIFFLAGS");
468 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
470 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
471 clib_unix_warning ("SIOCSIFFLAGS");
473 /* Turn off persistence */
474 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
475 clib_unix_warning ("TUNSETPERSIST");
476 close(tm->dev_tap_fd);
477 if (tm->dev_net_tun_fd >= 0)
478 close(tm->dev_net_tun_fd);
485 VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
488 * @brief CLI function for tun/tap config
490 * @param *vm - vlib_main_t
491 * @param *input - unformat_input_t
493 * @return error - clib_error_t
496 static clib_error_t *
497 tuntap_config (vlib_main_t * vm, unformat_input_t * input)
499 tuntap_main_t *tm = &tuntap_main;
500 clib_error_t * error = 0;
503 int flags = IFF_TUN | IFF_NO_PI;
504 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
505 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
507 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509 if (unformat (input, "mtu %d", &tm->mtu_bytes))
511 else if (unformat (input, "enable"))
513 else if (unformat (input, "disable"))
515 else if (unformat (input, "ethernet") ||
516 unformat (input, "ether"))
518 else if (unformat (input, "have-normal-interface") ||
519 unformat (input, "have-normal"))
520 have_normal_interface = 1;
521 else if (unformat (input, "name %s", &name))
522 tm->tun_name = (char *) name;
524 return clib_error_return (0, "unknown input `%U'",
525 format_unformat_error, input);
528 tm->dev_net_tun_fd = -1;
536 clib_warning ("tuntap disabled: must be superuser");
540 tm->is_ether = is_ether;
541 tm->have_normal_interface = have_normal_interface;
544 flags = IFF_TAP | IFF_NO_PI;
546 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
548 error = clib_error_return_unix (0, "open /dev/net/tun");
552 memset (&ifr, 0, sizeof (ifr));
553 strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
554 ifr.ifr_flags = flags;
555 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
557 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
561 /* Make it persistent, at least until we split. */
562 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
564 error = clib_error_return_unix (0, "TUNSETPERSIST");
568 /* Open a provisioning socket */
569 if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
570 htons(ETH_P_ALL))) < 0 )
572 error = clib_error_return_unix (0, "socket");
576 /* Find the interface index. */
579 struct sockaddr_ll sll;
581 memset (&ifr, 0, sizeof(ifr));
582 strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
583 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
585 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
589 /* Bind the provisioning socket to the interface. */
590 memset(&sll, 0, sizeof(sll));
591 sll.sll_family = AF_PACKET;
592 sll.sll_ifindex = ifr.ifr_ifindex;
593 sll.sll_protocol = htons(ETH_P_ALL);
595 if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
597 error = clib_error_return_unix (0, "bind");
602 /* non-blocking I/O on /dev/tapX */
605 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
607 error = clib_error_return_unix (0, "ioctl FIONBIO");
612 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
614 ifr.ifr_mtu = tm->mtu_bytes;
615 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
617 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
621 /* get flags, modify to bring up interface... */
622 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
624 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
628 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
630 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
632 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
638 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
640 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
644 clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
647 if (have_normal_interface)
649 vnet_main_t *vnm = vnet_get_main();
650 error = ethernet_register_interface
652 tuntap_dev_class.index,
653 0 /* device instance */,
654 tm->ether_dst_mac /* ethernet address */,
656 0 /* flag change */);
658 clib_error_report (error);
659 tm->sw_if_index = tm->hw_if_index;
660 vm->os_punt_frame = tuntap_nopunt_frame;
664 vnet_main_t *vnm = vnet_get_main();
665 vnet_hw_interface_t * hi;
667 vm->os_punt_frame = tuntap_punt_frame;
669 tm->hw_if_index = vnet_register_interface
671 tuntap_dev_class.index, 0 /* device instance */,
672 tuntap_interface_class.index, 0);
673 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
674 tm->sw_if_index = hi->sw_if_index;
676 /* Interface is always up. */
677 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
678 VNET_HW_INTERFACE_FLAG_LINK_UP);
679 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
680 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
684 unix_file_t template = {0};
685 template.read_function = tuntap_read_ready;
686 template.file_descriptor = tm->dev_net_tun_fd;
687 tm->unix_file_index = unix_file_add (&unix_main, &template);
693 if (tm->dev_net_tun_fd >= 0)
694 close (tm->dev_net_tun_fd);
695 if (tm->dev_tap_fd >= 0)
696 close (tm->dev_tap_fd);
702 VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
705 * @brief Add or Del IP4 address to tun/tap interface
707 * @param *im - ip4_main_t
708 * @param opaque - uword
709 * @param sw_if_index - u32
710 * @param *address - ip4_address_t
711 * @param is_delete - u32
715 tuntap_ip4_add_del_interface_address (ip4_main_t * im,
718 ip4_address_t * address,
720 u32 if_address_index,
723 tuntap_main_t * tm = &tuntap_main;
725 subif_address_t subif_addr, * ap;
728 /** Tuntap disabled, or using a "normal" interface. */
729 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
732 /** See if we already know about this subif */
733 memset (&subif_addr, 0, sizeof (subif_addr));
734 subif_addr.sw_if_index = sw_if_index;
735 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
737 p = mhash_get (&tm->subif_mhash, &subif_addr);
740 ap = pool_elt_at_index (tm->subifs, p[0]);
743 pool_get (tm->subifs, ap);
745 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
748 /* Use subif pool index to select alias device. */
749 memset (&ifr, 0, sizeof (ifr));
750 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
751 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
755 struct sockaddr_in * sin;
757 sin = (struct sockaddr_in *)&ifr.ifr_addr;
759 /* Set ipv4 address, netmask. */
760 sin->sin_family = AF_INET;
761 clib_memcpy (&sin->sin_addr.s_addr, address, 4);
762 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
763 clib_unix_warning ("ioctl SIOCSIFADDR");
765 sin->sin_addr.s_addr = im->fib_masks[address_length];
766 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
767 clib_unix_warning ("ioctl SIOCSIFNETMASK");
771 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
772 pool_put (tm->subifs, ap);
775 /* get flags, modify to bring up interface... */
776 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
777 clib_unix_warning ("ioctl SIOCGIFFLAGS");
780 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
782 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
784 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
785 clib_unix_warning ("ioctl SIOCSIFFLAGS");
789 * @brief workaround for a known #include bug
790 * #include <linux/ipv6.h> causes multiple definitions if
791 * netinet/in.h is also included.
794 struct in6_addr ifr6_addr;
800 * @brief Add or Del tun/tap interface address
802 * Both the v6 interface address API and the way ifconfig
803 * displays subinterfaces differ from their v4 couterparts.
804 * The code given here seems to work but YMMV.
806 * @param *im - ip6_main_t
807 * @param opaque - uword
808 * @param sw_if_index - u32
809 * @param *address - ip6_address_t
810 * @param address_length - u32
811 * @param if_address_index - u32
812 * @param is_delete - u32
815 tuntap_ip6_add_del_interface_address (ip6_main_t * im,
818 ip6_address_t * address,
820 u32 if_address_index,
823 tuntap_main_t * tm = &tuntap_main;
825 struct in6_ifreq ifr6;
826 subif_address_t subif_addr, * ap;
829 /* Tuntap disabled, or using a "normal" interface. */
830 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
833 /* See if we already know about this subif */
834 memset (&subif_addr, 0, sizeof (subif_addr));
835 subif_addr.sw_if_index = sw_if_index;
836 subif_addr.is_v6 = 1;
837 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
839 p = mhash_get (&tm->subif_mhash, &subif_addr);
842 ap = pool_elt_at_index (tm->subifs, p[0]);
845 pool_get (tm->subifs, ap);
847 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
850 /* Use subif pool index to select alias device. */
851 memset (&ifr, 0, sizeof (ifr));
852 memset (&ifr6, 0, sizeof (ifr6));
853 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
854 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
858 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
860 clib_unix_warning ("get ifindex socket");
862 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
863 clib_unix_warning ("get ifindex");
865 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
866 ifr6.ifr6_prefixlen = address_length;
867 clib_memcpy (&ifr6.ifr6_addr, address, 16);
869 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
870 clib_unix_warning ("set address");
877 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
879 clib_unix_warning ("get ifindex socket");
881 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
882 clib_unix_warning ("get ifindex");
884 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
885 ifr6.ifr6_prefixlen = address_length;
886 clib_memcpy (&ifr6.ifr6_addr, address, 16);
888 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
889 clib_unix_warning ("del address");
894 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
895 pool_put (tm->subifs, ap);
900 * @brief TX the tun/tap frame
902 * @param *vm - vlib_main_t
903 * @param *node - vlib_node_runtime_t
904 * @param *frame - vlib_frame_t
908 tuntap_punt_frame (vlib_main_t * vm,
909 vlib_node_runtime_t * node,
910 vlib_frame_t * frame)
912 tuntap_tx (vm, node, frame);
913 vlib_frame_free (vm, node, frame);
917 * @brief Free the tun/tap frame
919 * @param *vm - vlib_main_t
920 * @param *node - vlib_node_runtime_t
921 * @param *frame - vlib_frame_t
925 tuntap_nopunt_frame (vlib_main_t * vm,
926 vlib_node_runtime_t * node,
927 vlib_frame_t * frame)
929 u32 * buffers = vlib_frame_args (frame);
930 uword n_packets = frame->n_vectors;
931 vlib_buffer_free (vm, buffers, n_packets);
932 vlib_frame_free (vm, node, frame);
935 VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
940 * @brief Format tun/tap interface name
942 * @param *s - u8 - formatter string
943 * @param *args - va_list
945 * @return *s - u8 - formatted string
948 static u8 * format_tuntap_interface_name (u8 * s, va_list * args)
950 u32 i = va_arg (*args, u32);
952 s = format (s, "tuntap-%d", i);
957 * @brief TX packet out tun/tap
959 * @param *vm - vlib_main_t
960 * @param *node - vlib_node_runtime_t
961 * @param *frame - vlib_frame_t
963 * @return n_buffers - uword - Packets transmitted
967 tuntap_intfc_tx (vlib_main_t * vm,
968 vlib_node_runtime_t * node,
969 vlib_frame_t * frame)
971 tuntap_main_t * tm = &tuntap_main;
972 u32 * buffers = vlib_frame_args (frame);
973 uword n_buffers = frame->n_vectors;
975 /* Normal interface transmit happens only on the normal interface... */
976 if (tm->have_normal_interface)
977 return tuntap_tx (vm, node, frame);
979 vlib_buffer_free (vm, buffers, n_buffers);
983 VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
985 .tx_function = tuntap_intfc_tx,
986 .format_device_name = format_tuntap_interface_name,
990 * @brief tun/tap node init
992 * @param *vm - vlib_main_t
994 * @return error - clib_error_t
997 static clib_error_t *
998 tuntap_init (vlib_main_t * vm)
1000 clib_error_t * error;
1001 ip4_main_t * im4 = &ip4_main;
1002 ip6_main_t * im6 = &ip6_main;
1003 ip4_add_del_interface_address_callback_t cb4;
1004 ip6_add_del_interface_address_callback_t cb6;
1005 tuntap_main_t * tm = &tuntap_main;
1007 error = vlib_call_init_function (vm, ip4_init);
1011 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t));
1013 cb4.function = tuntap_ip4_add_del_interface_address;
1014 cb4.function_opaque = 0;
1015 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1017 cb6.function = tuntap_ip6_add_del_interface_address;
1018 cb6.function_opaque = 0;
1019 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
1024 VLIB_INIT_FUNCTION (tuntap_init);