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>
53 static vnet_device_class_t tuntap_dev_class;
54 static vnet_hw_interface_class_t tuntap_interface_class;
56 static void tuntap_punt_frame (vlib_main_t * vm,
57 vlib_node_runtime_t * node,
58 vlib_frame_t * frame);
59 static void tuntap_nopunt_frame (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
61 vlib_frame_t * frame);
70 * @brief TUNTAP node main state
73 /** Vector of iovecs for readv/writev calls. */
74 struct iovec * iovecs;
76 /** Vector of VLIB rx buffers to use. We allocate them in blocks
77 of VLIB_FRAME_SIZE (256). */
80 /** File descriptors for /dev/net/tun and provisioning socket. */
81 int dev_net_tun_fd, dev_tap_fd;
83 /** Create a "tap" [ethernet] encaps device */
86 /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
88 int have_normal_interface;
90 /** tap device destination MAC address. Required, or Linux drops pkts */
93 /** Interface MTU in bytes and # of default sized buffers. */
94 u32 mtu_bytes, mtu_buffers;
96 /** Linux interface name for tun device. */
99 /** Pool of subinterface addresses */
100 subif_address_t *subifs;
102 /** Hash for subif addresses */
105 /** Unix file index */
108 /** For the "normal" interface, if configured */
109 u32 hw_if_index, sw_if_index;
113 static tuntap_main_t tuntap_main = {
116 /** Suitable defaults for an Ethernet-like tun/tap device */
117 .mtu_bytes = 4096 + 256,
124 * Output node, writes the buffers comprising the incoming frame
125 * to the tun/tap device, aka hands them to the Linux kernel stack.
127 * @param *vm - vlib_main_t
128 * @param *node - vlib_node_runtime_t
129 * @param *frame - vlib_frame_t
135 tuntap_tx (vlib_main_t * vm,
136 vlib_node_runtime_t * node,
137 vlib_frame_t * frame)
139 u32 * buffers = vlib_frame_args (frame);
140 uword n_packets = frame->n_vectors;
141 tuntap_main_t * tm = &tuntap_main;
142 vnet_main_t *vnm = vnet_get_main ();
143 vnet_interface_main_t *im = &vnm->interface_main;
147 for (i = 0; i < n_packets; i++)
153 b = vlib_get_buffer (vm, buffers[i]);
155 if (tm->is_ether && (!tm->have_normal_interface))
157 vlib_buffer_reset(b);
158 clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
161 /* Re-set iovecs if present. */
163 _vec_len (tm->iovecs) = 0;
165 /** VLIB buffer chain -> Unix iovec(s). */
166 vec_add2 (tm->iovecs, iov, 1);
167 iov->iov_base = b->data + b->current_data;
168 iov->iov_len = l = b->current_length;
170 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
173 b = vlib_get_buffer (vm, b->next_buffer);
175 vec_add2 (tm->iovecs, iov, 1);
177 iov->iov_base = b->data + b->current_data;
178 iov->iov_len = b->current_length;
179 l += b->current_length;
180 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
183 if (writev (tm->dev_net_tun_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
184 clib_unix_warning ("writev");
189 /* Update tuntap interface output stats. */
190 vlib_increment_combined_counter (im->combined_sw_if_counters
191 + VNET_INTERFACE_COUNTER_TX,
193 tm->sw_if_index, n_packets, n_bytes);
196 /** The normal interface path flattens the buffer chain */
197 if (tm->have_normal_interface)
198 vlib_buffer_free_no_next (vm, buffers, n_packets);
200 vlib_buffer_free (vm, buffers, n_packets);
205 VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
206 .function = tuntap_tx,
208 .type = VLIB_NODE_TYPE_INTERNAL,
213 * @brief TUNTAP receive node
216 * @param *vm - vlib_main_t
217 * @param *node - vlib_node_runtime_t
218 * @param *frame - vlib_frame_t
224 tuntap_rx (vlib_main_t * vm,
225 vlib_node_runtime_t * node,
226 vlib_frame_t * frame)
228 tuntap_main_t * tm = &tuntap_main;
231 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
233 /** Make sure we have some RX buffers. */
235 uword n_left = vec_len (tm->rx_buffers);
238 if (n_left < VLIB_FRAME_SIZE / 2)
240 if (! tm->rx_buffers)
241 vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
243 n_alloc = vlib_buffer_alloc (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left);
244 _vec_len (tm->rx_buffers) = n_left + n_alloc;
248 /** Allocate RX buffers from end of rx_buffers.
249 Turn them into iovecs to pass to readv. */
251 uword i_rx = vec_len (tm->rx_buffers) - 1;
253 word i, n_bytes_left, n_bytes_in_packet;
255 /** We should have enough buffers left for an MTU sized packet. */
256 ASSERT (vec_len (tm->rx_buffers) >= tm->mtu_buffers);
258 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
259 for (i = 0; i < tm->mtu_buffers; i++)
261 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - i]);
262 tm->iovecs[i].iov_base = b->data;
263 tm->iovecs[i].iov_len = buffer_size;
266 n_bytes_left = readv (tm->dev_net_tun_fd, tm->iovecs, tm->mtu_buffers);
267 n_bytes_in_packet = n_bytes_left;
268 if (n_bytes_left <= 0)
271 clib_unix_warning ("readv %d", n_bytes_left);
275 bi = tm->rx_buffers[i_rx];
279 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
282 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
284 n_bytes_left -= buffer_size;
286 if (n_bytes_left <= 0)
292 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
293 b->next_buffer = tm->rx_buffers[i_rx];
296 /** Interface counters for tuntap interface. */
297 vlib_increment_combined_counter
298 (vnet_main.interface_main.combined_sw_if_counters
299 + VNET_INTERFACE_COUNTER_RX,
302 1, n_bytes_in_packet);
304 _vec_len (tm->rx_buffers) = i_rx;
307 b = vlib_get_buffer (vm, bi);
311 uword n_trace = vlib_get_trace_count (vm, node);
313 vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
314 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
317 * Turn this on if you run into
318 * "bad monkey" contexts, and you want to know exactly
319 * which nodes they've visited...
321 if (VLIB_BUFFER_TRACE_TRAJECTORY)
324 b->error = node->errors[0];
328 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
331 switch (b->data[0] & 0xf0)
334 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
337 next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
340 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
344 /* The linux kernel couldn't care less if our interface is up */
345 if (tm->have_normal_interface)
347 vnet_main_t *vnm = vnet_get_main();
348 vnet_sw_interface_t * si;
349 si = vnet_get_sw_interface (vnm, tm->sw_if_index);
350 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
351 next_index = VNET_DEVICE_INPUT_NEXT_DROP;
354 vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b, 0);
356 vlib_set_next_frame_buffer (vm, node, next_index, bi);
360 vlib_trace_buffer (vm, node, next_index,
361 b, /* follow_chain */ 1);
362 vlib_set_trace_count (vm, node, n_trace - 1);
370 * @brief TUNTAP_RX error strings
372 static char * tuntap_rx_error_strings[] = {
373 "unknown packet type",
376 VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
377 .function = tuntap_rx,
379 .sibling_of = "device-input",
380 .type = VLIB_NODE_TYPE_INPUT,
381 .state = VLIB_NODE_STATE_INTERRUPT,
384 .error_strings = tuntap_rx_error_strings,
388 * @brief Gets called when file descriptor is ready from epoll.
390 * @param *uf - unix_file_t
392 * @return error - clib_error_t
394 static clib_error_t * tuntap_read_ready (unix_file_t * uf)
396 vlib_main_t * vm = vlib_get_main();
397 vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
402 * @brief Clean up the tun/tap device
404 * @param *vm - vlib_main_t
406 * @return error - clib_error_t
409 static clib_error_t *
410 tuntap_exit (vlib_main_t * vm)
412 tuntap_main_t *tm = &tuntap_main;
417 if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
420 sfd = socket (AF_INET, SOCK_STREAM, 0);
422 clib_unix_warning("provisioning socket");
424 memset(&ifr, 0, sizeof (ifr));
425 strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1);
427 /* get flags, modify to bring down interface... */
428 if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
429 clib_unix_warning ("SIOCGIFFLAGS");
431 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
433 if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
434 clib_unix_warning ("SIOCSIFFLAGS");
436 /* Turn off persistence */
437 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
438 clib_unix_warning ("TUNSETPERSIST");
439 close(tm->dev_tap_fd);
440 if (tm->dev_net_tun_fd >= 0)
441 close(tm->dev_net_tun_fd);
448 VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
451 * @brief CLI function for tun/tap config
453 * @param *vm - vlib_main_t
454 * @param *input - unformat_input_t
456 * @return error - clib_error_t
459 static clib_error_t *
460 tuntap_config (vlib_main_t * vm, unformat_input_t * input)
462 tuntap_main_t *tm = &tuntap_main;
463 clib_error_t * error = 0;
466 int flags = IFF_TUN | IFF_NO_PI;
467 int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
468 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
470 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
472 if (unformat (input, "mtu %d", &tm->mtu_bytes))
474 else if (unformat (input, "enable"))
476 else if (unformat (input, "disable"))
478 else if (unformat (input, "ethernet") ||
479 unformat (input, "ether"))
481 else if (unformat (input, "have-normal-interface") ||
482 unformat (input, "have-normal"))
483 have_normal_interface = 1;
484 else if (unformat (input, "name %s", &name))
485 tm->tun_name = (char *) name;
487 return clib_error_return (0, "unknown input `%U'",
488 format_unformat_error, input);
491 tm->dev_net_tun_fd = -1;
499 clib_warning ("tuntap disabled: must be superuser");
503 tm->is_ether = is_ether;
504 tm->have_normal_interface = have_normal_interface;
507 flags = IFF_TAP | IFF_NO_PI;
509 if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
511 error = clib_error_return_unix (0, "open /dev/net/tun");
515 memset (&ifr, 0, sizeof (ifr));
516 strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
517 ifr.ifr_flags = flags;
518 if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
520 error = clib_error_return_unix (0, "ioctl TUNSETIFF");
524 /* Make it persistent, at least until we split. */
525 if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
527 error = clib_error_return_unix (0, "TUNSETPERSIST");
531 /* Open a provisioning socket */
532 if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
533 htons(ETH_P_ALL))) < 0 )
535 error = clib_error_return_unix (0, "socket");
539 /* Find the interface index. */
542 struct sockaddr_ll sll;
544 memset (&ifr, 0, sizeof(ifr));
545 strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1);
546 if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
548 error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
552 /* Bind the provisioning socket to the interface. */
553 memset(&sll, 0, sizeof(sll));
554 sll.sll_family = AF_PACKET;
555 sll.sll_ifindex = ifr.ifr_ifindex;
556 sll.sll_protocol = htons(ETH_P_ALL);
558 if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
560 error = clib_error_return_unix (0, "bind");
565 /* non-blocking I/O on /dev/tapX */
568 if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
570 error = clib_error_return_unix (0, "ioctl FIONBIO");
575 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
577 ifr.ifr_mtu = tm->mtu_bytes;
578 if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
580 error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
584 /* get flags, modify to bring up interface... */
585 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
587 error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
591 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
593 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
595 error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
601 if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
603 error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
607 clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
610 if (have_normal_interface)
612 vnet_main_t *vnm = vnet_get_main();
613 error = ethernet_register_interface
615 tuntap_dev_class.index,
616 0 /* device instance */,
617 tm->ether_dst_mac /* ethernet address */,
619 0 /* flag change */);
621 clib_error_report (error);
622 tm->sw_if_index = tm->hw_if_index;
623 vm->os_punt_frame = tuntap_nopunt_frame;
627 vnet_main_t *vnm = vnet_get_main();
628 vnet_hw_interface_t * hi;
630 vm->os_punt_frame = tuntap_punt_frame;
632 tm->hw_if_index = vnet_register_interface
634 tuntap_dev_class.index, 0 /* device instance */,
635 tuntap_interface_class.index, 0);
636 hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
637 tm->sw_if_index = hi->sw_if_index;
639 /* Interface is always up. */
640 vnet_hw_interface_set_flags (vnm, tm->hw_if_index,
641 VNET_HW_INTERFACE_FLAG_LINK_UP);
642 vnet_sw_interface_set_flags (vnm, tm->sw_if_index,
643 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
647 unix_file_t template = {0};
648 template.read_function = tuntap_read_ready;
649 template.file_descriptor = tm->dev_net_tun_fd;
650 tm->unix_file_index = unix_file_add (&unix_main, &template);
656 if (tm->dev_net_tun_fd >= 0)
657 close (tm->dev_net_tun_fd);
658 if (tm->dev_tap_fd >= 0)
659 close (tm->dev_tap_fd);
665 VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
668 * @brief Add or Del IP4 address to tun/tap interface
670 * @param *im - ip4_main_t
671 * @param opaque - uword
672 * @param sw_if_index - u32
673 * @param *address - ip4_address_t
674 * @param is_delete - u32
678 tuntap_ip4_add_del_interface_address (ip4_main_t * im,
681 ip4_address_t * address,
683 u32 if_address_index,
686 tuntap_main_t * tm = &tuntap_main;
688 subif_address_t subif_addr, * ap;
691 /** Tuntap disabled, or using a "normal" interface. */
692 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
695 /** See if we already know about this subif */
696 memset (&subif_addr, 0, sizeof (subif_addr));
697 subif_addr.sw_if_index = sw_if_index;
698 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
700 p = mhash_get (&tm->subif_mhash, &subif_addr);
703 ap = pool_elt_at_index (tm->subifs, p[0]);
706 pool_get (tm->subifs, ap);
708 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
711 /* Use subif pool index to select alias device. */
712 memset (&ifr, 0, sizeof (ifr));
713 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
714 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
716 /* the tuntap punt/inject is enabled for IPv4 RX so long as
717 * any vpp interface has an IPv4 address.
718 * this is also ref counted.
720 ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
724 struct sockaddr_in * sin;
726 sin = (struct sockaddr_in *)&ifr.ifr_addr;
728 /* Set ipv4 address, netmask. */
729 sin->sin_family = AF_INET;
730 clib_memcpy (&sin->sin_addr.s_addr, address, 4);
731 if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
732 clib_unix_warning ("ioctl SIOCSIFADDR");
734 sin->sin_addr.s_addr = im->fib_masks[address_length];
735 if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
736 clib_unix_warning ("ioctl SIOCSIFNETMASK");
740 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
741 pool_put (tm->subifs, ap);
744 /* get flags, modify to bring up interface... */
745 if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
746 clib_unix_warning ("ioctl SIOCGIFFLAGS");
749 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
751 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
753 if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
754 clib_unix_warning ("ioctl SIOCSIFFLAGS");
758 * @brief workaround for a known include file bug.
759 * including @c <linux/ipv6.h> causes multiple definitions if
760 * @c <netinet/in.h is also included.
763 struct in6_addr ifr6_addr;
769 * @brief Add or Del tun/tap interface address.
771 * Both the v6 interface address API and the way ifconfig
772 * displays subinterfaces differ from their v4 couterparts.
773 * The code given here seems to work but YMMV.
775 * @param *im - ip6_main_t
776 * @param opaque - uword
777 * @param sw_if_index - u32
778 * @param *address - ip6_address_t
779 * @param address_length - u32
780 * @param if_address_index - u32
781 * @param is_delete - u32
784 tuntap_ip6_add_del_interface_address (ip6_main_t * im,
787 ip6_address_t * address,
789 u32 if_address_index,
792 tuntap_main_t * tm = &tuntap_main;
794 struct in6_ifreq ifr6;
795 subif_address_t subif_addr, * ap;
798 /* Tuntap disabled, or using a "normal" interface. */
799 if (tm->have_normal_interface || tm->dev_tap_fd < 0)
802 /* See if we already know about this subif */
803 memset (&subif_addr, 0, sizeof (subif_addr));
804 subif_addr.sw_if_index = sw_if_index;
805 subif_addr.is_v6 = 1;
806 clib_memcpy (&subif_addr.addr, address, sizeof (*address));
808 p = mhash_get (&tm->subif_mhash, &subif_addr);
811 ap = pool_elt_at_index (tm->subifs, p[0]);
814 pool_get (tm->subifs, ap);
816 mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
819 /* Use subif pool index to select alias device. */
820 memset (&ifr, 0, sizeof (ifr));
821 memset (&ifr6, 0, sizeof (ifr6));
822 snprintf (ifr.ifr_name, sizeof(ifr.ifr_name),
823 "%s:%d", tm->tun_name, (int)(ap - tm->subifs));
825 /* the tuntap punt/inject is enabled for IPv6 RX so long as
826 * any vpp interface has an IPv6 address.
827 * this is also ref counted.
829 ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete);
833 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
835 clib_unix_warning ("get ifindex socket");
837 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
838 clib_unix_warning ("get ifindex");
840 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
841 ifr6.ifr6_prefixlen = address_length;
842 clib_memcpy (&ifr6.ifr6_addr, address, 16);
844 if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
845 clib_unix_warning ("set address");
852 int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
854 clib_unix_warning ("get ifindex socket");
856 if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
857 clib_unix_warning ("get ifindex");
859 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
860 ifr6.ifr6_prefixlen = address_length;
861 clib_memcpy (&ifr6.ifr6_addr, address, 16);
863 if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
864 clib_unix_warning ("del address");
869 mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
870 pool_put (tm->subifs, ap);
875 * @brief TX the tun/tap frame
877 * @param *vm - vlib_main_t
878 * @param *node - vlib_node_runtime_t
879 * @param *frame - vlib_frame_t
883 tuntap_punt_frame (vlib_main_t * vm,
884 vlib_node_runtime_t * node,
885 vlib_frame_t * frame)
887 tuntap_tx (vm, node, frame);
888 vlib_frame_free (vm, node, frame);
892 * @brief Free the tun/tap frame
894 * @param *vm - vlib_main_t
895 * @param *node - vlib_node_runtime_t
896 * @param *frame - vlib_frame_t
900 tuntap_nopunt_frame (vlib_main_t * vm,
901 vlib_node_runtime_t * node,
902 vlib_frame_t * frame)
904 u32 * buffers = vlib_frame_args (frame);
905 uword n_packets = frame->n_vectors;
906 vlib_buffer_free (vm, buffers, n_packets);
907 vlib_frame_free (vm, node, frame);
910 VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
912 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
916 * @brief Format tun/tap interface name
918 * @param *s - u8 - formatter string
919 * @param *args - va_list
921 * @return *s - u8 - formatted string
924 static u8 * format_tuntap_interface_name (u8 * s, va_list * args)
926 u32 i = va_arg (*args, u32);
928 s = format (s, "tuntap-%d", i);
933 * @brief TX packet out tun/tap
935 * @param *vm - vlib_main_t
936 * @param *node - vlib_node_runtime_t
937 * @param *frame - vlib_frame_t
939 * @return n_buffers - uword - Packets transmitted
943 tuntap_intfc_tx (vlib_main_t * vm,
944 vlib_node_runtime_t * node,
945 vlib_frame_t * frame)
947 tuntap_main_t * tm = &tuntap_main;
948 u32 * buffers = vlib_frame_args (frame);
949 uword n_buffers = frame->n_vectors;
951 /* Normal interface transmit happens only on the normal interface... */
952 if (tm->have_normal_interface)
953 return tuntap_tx (vm, node, frame);
955 vlib_buffer_free (vm, buffers, n_buffers);
959 VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
961 .tx_function = tuntap_intfc_tx,
962 .format_device_name = format_tuntap_interface_name,
966 * @brief tun/tap node init
968 * @param *vm - vlib_main_t
970 * @return error - clib_error_t
973 static clib_error_t *
974 tuntap_init (vlib_main_t * vm)
976 clib_error_t * error;
977 ip4_main_t * im4 = &ip4_main;
978 ip6_main_t * im6 = &ip6_main;
979 ip4_add_del_interface_address_callback_t cb4;
980 ip6_add_del_interface_address_callback_t cb6;
981 tuntap_main_t * tm = &tuntap_main;
983 error = vlib_call_init_function (vm, ip4_init);
987 mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t));
989 cb4.function = tuntap_ip4_add_del_interface_address;
990 cb4.function_opaque = 0;
991 vec_add1 (im4->add_del_interface_address_callbacks, cb4);
993 cb6.function = tuntap_ip6_add_del_interface_address;
994 cb6.function_opaque = 0;
995 vec_add1 (im6->add_del_interface_address_callbacks, cb6);
1000 VLIB_INIT_FUNCTION (tuntap_init);