2 *------------------------------------------------------------------
3 * tapcli.c - dynamic tap interface hookup
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 dynamic tap interface hookup
24 #include <fcntl.h> /* for open */
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <sys/uio.h> /* for iovec */
30 #include <netinet/in.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_tun.h>
35 #include <vlib/vlib.h>
36 #include <vlib/unix/unix.h>
38 #include <vnet/ip/ip.h>
40 #include <vnet/ethernet/ethernet.h>
42 #include <vnet/feature/feature.h>
43 #include <vnet/devices/devices.h>
44 #include <vnet/unix/tuntap.h>
45 #include <vnet/unix/tapcli.h>
47 static vnet_device_class_t tapcli_dev_class;
48 static vnet_hw_interface_class_t tapcli_interface_class;
49 static vlib_node_registration_t tapcli_rx_node;
51 static void tapcli_nopunt_frame (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
53 vlib_frame_t * frame);
55 * @brief Struct for the tapcli interface
67 u32 per_interface_next_index;
73 * @brief Struct for RX trace
81 * @brief Function to format TAP CLI trace
83 * @param *s - u8 - formatting string
84 * @param *va - va_list
86 * @return *s - u8 - formatted string
90 format_tapcli_rx_trace (u8 * s, va_list * va)
92 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
93 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
94 vnet_main_t *vnm = vnet_get_main ();
95 tapcli_rx_trace_t *t = va_arg (*va, tapcli_rx_trace_t *);
96 s = format (s, "%U", format_vnet_sw_if_index_name, vnm, t->sw_if_index);
101 * @brief TAPCLI per thread struct
105 /** Vector of VLIB rx buffers to use. We allocate them in blocks
106 of VLIB_FRAME_SIZE (256). */
109 /** Vector of iovecs for readv/writev calls. */
110 struct iovec *iovecs;
111 } tapcli_per_thread_t;
114 * @brief TAPCLI main state struct
118 /** per thread variables */
119 tapcli_per_thread_t *threads;
121 /** tap device destination MAC address. Required, or Linux drops pkts */
124 /** Interface MTU in bytes and # of default sized buffers. */
125 u32 mtu_bytes, mtu_buffers;
127 /** Vector of tap interfaces */
128 tapcli_interface_t *tapcli_interfaces;
130 /** Vector of deleted tap interfaces */
131 u32 *tapcli_inactive_interfaces;
133 /** Bitmap of tap interfaces with pending reads */
134 uword *pending_read_bitmap;
136 /** Hash table to find tapcli interface given hw_if_index */
137 uword *tapcli_interface_index_by_sw_if_index;
139 /** Hash table to find tapcli interface given unix fd */
140 uword *tapcli_interface_index_by_unix_fd;
142 /** renumbering table */
143 u32 *show_dev_instance_by_real_dev_instance;
145 /** 1 => disable CLI */
148 /** convenience - vlib_main_t */
149 vlib_main_t *vlib_main;
150 /** convenience - vnet_main_t */
151 vnet_main_t *vnet_main;
154 static tapcli_main_t tapcli_main;
157 * @brief tapcli TX node function
160 * Output node, writes the buffers comprising the incoming frame
161 * to the tun/tap device, aka hands them to the Linux kernel stack.
163 * @param *vm - vlib_main_t
164 * @param *node - vlib_node_runtime_t
165 * @param *frame - vlib_frame_t
167 * @return n_packets - uword
171 tapcli_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
173 u32 *buffers = vlib_frame_args (frame);
174 uword n_packets = frame->n_vectors;
175 tapcli_main_t *tm = &tapcli_main;
176 tapcli_interface_t *ti;
178 u16 thread_index = vlib_get_thread_index ();
180 for (i = 0; i < n_packets; i++)
185 vnet_hw_interface_t *hw;
189 b = vlib_get_buffer (vm, buffers[i]);
191 tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
192 if (tx_sw_if_index == (u32) ~ 0)
193 tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
195 ASSERT (tx_sw_if_index != (u32) ~ 0);
197 /* Use the sup intfc to finesse vlan subifs */
198 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
199 tx_sw_if_index = hw->sw_if_index;
201 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
205 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
206 /* $$$ leak, but this should never happen... */
210 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
212 /* Re-set iovecs if present. */
213 if (tm->threads[thread_index].iovecs)
214 _vec_len (tm->threads[thread_index].iovecs) = 0;
216 /* VLIB buffer chain -> Unix iovec(s). */
217 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
218 iov->iov_base = b->data + b->current_data;
219 iov->iov_len = l = b->current_length;
221 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
225 b = vlib_get_buffer (vm, b->next_buffer);
227 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
229 iov->iov_base = b->data + b->current_data;
230 iov->iov_len = b->current_length;
231 l += b->current_length;
233 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
236 if (writev (ti->unix_fd, tm->threads[thread_index].iovecs,
237 vec_len (tm->threads[thread_index].iovecs)) < l)
238 clib_unix_warning ("writev");
241 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
247 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
248 .function = tapcli_tx,
250 .type = VLIB_NODE_TYPE_INTERNAL,
256 * @brief Dispatch tapcli RX node function for node tap_cli_rx
259 * @param *vm - vlib_main_t
260 * @param *node - vlib_node_runtime_t
261 * @param *ti - tapcli_interface_t
263 * @return n_packets - uword
267 tapcli_rx_iface (vlib_main_t * vm,
268 vlib_node_runtime_t * node, tapcli_interface_t * ti)
270 tapcli_main_t *tm = &tapcli_main;
271 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
272 u32 n_trace = vlib_get_trace_count (vm, node);
274 u16 thread_index = vlib_get_thread_index ();
276 vnet_sw_interface_t *si;
278 u32 next = node->cached_next_index;
279 u32 n_left_to_next, next_index;
282 vnm = vnet_get_main ();
283 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
284 admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
286 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
288 while (n_left_to_next)
289 { // Fill at most one vector
290 vlib_buffer_t *b_first, *b, *prev;
292 word n_bytes_in_packet;
295 if (PREDICT_FALSE (vec_len (tm->threads[thread_index].rx_buffers) <
298 uword len = vec_len (tm->threads[thread_index].rx_buffers);
299 _vec_len (tm->threads[thread_index].rx_buffers) +=
300 vlib_buffer_alloc_from_free_list (vm,
301 &tm->threads[thread_index].
303 VLIB_FRAME_SIZE - len,
304 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
306 (vec_len (tm->threads[thread_index].rx_buffers) <
309 vlib_node_increment_counter (vm, tapcli_rx_node.index,
310 TAPCLI_ERROR_BUFFER_ALLOC,
319 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
321 /* Allocate RX buffers from end of rx_buffers.
322 Turn them into iovecs to pass to readv. */
323 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
324 for (j = 0; j < tm->mtu_buffers; j++)
328 tm->threads[thread_index].rx_buffers[i_rx - j]);
329 tm->threads[thread_index].iovecs[j].iov_base = b->data;
330 tm->threads[thread_index].iovecs[j].iov_len = buffer_size;
333 n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs,
335 n_bytes_in_packet = n_bytes_left;
336 if (n_bytes_left <= 0)
340 vlib_node_increment_counter (vm, tapcli_rx_node.index,
341 TAPCLI_ERROR_READ, 1);
346 bi_first = tm->threads[thread_index].rx_buffers[i_rx];
347 b = b_first = vlib_get_buffer (vm,
348 tm->threads[thread_index].
355 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
356 n_bytes_left -= buffer_size;
360 prev->next_buffer = bi;
361 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
366 if (n_bytes_left <= 0)
370 bi = tm->threads[thread_index].rx_buffers[i_rx];
371 b = vlib_get_buffer (vm, bi);
374 _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
376 b_first->total_length_not_including_first_buffer =
378 buffer_size) ? n_bytes_in_packet - buffer_size : 0;
379 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
381 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_first);
383 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
384 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32) ~ 0;
386 b_first->error = node->errors[TAPCLI_ERROR_NONE];
387 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
388 next_index = (ti->per_interface_next_index != ~0) ?
389 ti->per_interface_next_index : next_index;
390 next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index;
392 to_next[0] = bi_first;
396 vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index,
399 vlib_validate_buffer_enqueue_x1 (vm, node, next,
400 to_next, n_left_to_next,
401 bi_first, next_index);
403 /* Interface counters for tapcli interface. */
404 if (PREDICT_TRUE (!admin_down))
406 vlib_increment_combined_counter (vnet_main.interface_main.
407 combined_sw_if_counters +
408 VNET_INTERFACE_COUNTER_RX,
409 thread_index, ti->sw_if_index, 1,
412 if (PREDICT_FALSE (n_trace > 0))
414 vlib_trace_buffer (vm, node, next_index,
415 b_first, /* follow_chain */ 1);
418 tapcli_rx_trace_t *t0 =
419 vlib_add_trace (vm, node, b_first, sizeof (*t0));
420 t0->sw_if_index = si->sw_if_index;
424 vlib_put_next_frame (vm, node, next, n_left_to_next);
426 vlib_set_trace_count (vm, node, n_trace);
427 return VLIB_FRAME_SIZE - n_left_to_next;
431 * @brief tapcli RX node function
434 * Input node from the Kernel tun/tap device
436 * @param *vm - vlib_main_t
437 * @param *node - vlib_node_runtime_t
438 * @param *frame - vlib_frame_t
440 * @return n_packets - uword
444 tapcli_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
446 tapcli_main_t *tm = &tapcli_main;
447 static u32 *ready_interface_indices;
448 tapcli_interface_t *ti;
452 vec_reset_length (ready_interface_indices);
454 clib_bitmap_foreach (i, tm->pending_read_bitmap,
456 vec_add1 (ready_interface_indices, i);
460 if (vec_len (ready_interface_indices) == 0)
463 for (i = 0; i < vec_len (ready_interface_indices); i++)
465 tm->pending_read_bitmap =
466 clib_bitmap_set (tm->pending_read_bitmap,
467 ready_interface_indices[i], 0);
470 vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
471 total_count += tapcli_rx_iface (vm, node, ti);
473 return total_count; //This might return more than 256.
476 /** TAPCLI error strings */
477 static char *tapcli_rx_error_strings[] = {
478 #define _(sym,string) string,
484 VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
485 .function = tapcli_rx,
487 .sibling_of = "device-input",
488 .type = VLIB_NODE_TYPE_INPUT,
489 .state = VLIB_NODE_STATE_INTERRUPT,
491 .n_errors = TAPCLI_N_ERROR,
492 .error_strings = tapcli_rx_error_strings,
493 .format_trace = format_tapcli_rx_trace,
499 * @brief Gets called when file descriptor is ready from epoll.
501 * @param *uf - clib_file_t
503 * @return error - clib_error_t
506 static clib_error_t *
507 tapcli_read_ready (clib_file_t * uf)
509 vlib_main_t *vm = vlib_get_main ();
510 tapcli_main_t *tm = &tapcli_main;
513 /** Schedule the rx node */
514 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
516 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
518 /** Mark the specific tap interface ready-to-read */
520 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
523 clib_warning ("fd %d not in hash table", uf->file_descriptor);
529 * @brief CLI function for TAPCLI configuration
531 * @param *vm - vlib_main_t
532 * @param *input - unformat_input_t
534 * @return error - clib_error_t
537 static clib_error_t *
538 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
540 tapcli_main_t *tm = &tapcli_main;
541 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
543 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
545 if (unformat (input, "mtu %d", &tm->mtu_bytes))
547 else if (unformat (input, "disable"))
550 return clib_error_return (0, "unknown input `%U'",
551 format_unformat_error, input);
559 clib_warning ("tapcli disabled: must be superuser");
564 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
570 * @brief Renumber TAPCLI interface
572 * @param *hi - vnet_hw_interface_t
573 * @param new_dev_instance - u32
579 tap_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
581 tapcli_main_t *tm = &tapcli_main;
583 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
584 hi->dev_instance, ~0);
586 tm->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
592 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
595 * @brief Free "no punt" frame
597 * @param *vm - vlib_main_t
598 * @param *node - vlib_node_runtime_t
599 * @param *frame - vlib_frame_t
603 tapcli_nopunt_frame (vlib_main_t * vm,
604 vlib_node_runtime_t * node, vlib_frame_t * frame)
606 u32 *buffers = vlib_frame_args (frame);
607 uword n_packets = frame->n_vectors;
608 vlib_buffer_free (vm, buffers, n_packets);
609 vlib_frame_free (vm, node, frame);
613 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
615 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
620 * @brief Formatter for TAPCLI interface name
622 * @param *s - formatter string
623 * @param *args - va_list
625 * @return *s - formatted string
629 format_tapcli_interface_name (u8 * s, va_list * args)
631 u32 i = va_arg (*args, u32);
632 u32 show_dev_instance = ~0;
633 tapcli_main_t *tm = &tapcli_main;
635 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
636 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
638 if (show_dev_instance != ~0)
639 i = show_dev_instance;
641 s = format (s, "tapcli-%d", i);
646 * @brief Modify interface flags for TAPCLI interface
648 * @param *vnm - vnet_main_t
649 * @param *hw - vnet_hw_interface_t
656 tapcli_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
658 tapcli_main_t *tm = &tapcli_main;
659 tapcli_interface_t *ti;
661 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
663 if (flags & ETHERNET_INTERFACE_FLAG_MTU)
665 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
666 tm->mtu_bytes = hw->max_packet_bytes;
667 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
674 memcpy (&ifr, &ti->ifr, sizeof (ifr));
676 /* get flags, modify to bring up interface... */
677 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
679 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
683 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
685 if (want_promisc == ti->is_promisc)
688 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
689 ifr.ifr_flags |= IFF_PROMISC;
691 ifr.ifr_flags &= ~(IFF_PROMISC);
693 /* get flags, modify to bring up interface... */
694 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
696 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
700 ti->is_promisc = want_promisc;
707 * @brief Setting the TAP interface's next processing node
709 * @param *vnm - vnet_main_t
710 * @param hw_if_index - u32
711 * @param node_index - u32
715 tapcli_set_interface_next_node (vnet_main_t * vnm,
716 u32 hw_if_index, u32 node_index)
718 tapcli_main_t *tm = &tapcli_main;
719 tapcli_interface_t *ti;
720 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
722 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
724 /** Shut off redirection */
725 if (node_index == ~0)
727 ti->per_interface_next_index = node_index;
731 ti->per_interface_next_index =
732 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
736 * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
738 * @param *vnm - vnet_main_t
739 * @param hw_if_index - u32
742 * @return error - clib_error_t
744 static clib_error_t *
745 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
747 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
749 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
750 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
753 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
755 hw_flags = speed_duplex;
757 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
762 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
764 .tx_function = tapcli_tx,
765 .format_device_name = format_tapcli_interface_name,
766 .rx_redirect_to_node = tapcli_set_interface_next_node,
767 .name_renumber = tap_name_renumber,
768 .admin_up_down_function = tapcli_interface_admin_up_down,
773 * @brief Dump TAP interfaces
775 * @param **out_tapids - tapcli_interface_details_t
781 vnet_tap_dump_ifs (tapcli_interface_details_t ** out_tapids)
783 tapcli_main_t *tm = &tapcli_main;
784 tapcli_interface_t *ti;
786 tapcli_interface_details_t *r_tapids = NULL;
787 tapcli_interface_details_t *tapid = NULL;
789 vec_foreach (ti, tm->tapcli_interfaces)
793 vec_add2 (r_tapids, tapid, 1);
794 tapid->sw_if_index = ti->sw_if_index;
795 strncpy ((char *) tapid->dev_name, ti->ifr.ifr_name,
796 sizeof (ti->ifr.ifr_name) - 1);
799 *out_tapids = r_tapids;
805 * @brief Get tap interface from inactive interfaces or create new
807 * @return interface - tapcli_interface_t
810 static tapcli_interface_t *
811 tapcli_get_new_tapif ()
813 tapcli_main_t *tm = &tapcli_main;
814 tapcli_interface_t *ti = NULL;
816 int inactive_cnt = vec_len (tm->tapcli_inactive_interfaces);
817 // if there are any inactive ifaces
818 if (inactive_cnt > 0)
821 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
822 if (vec_len (tm->tapcli_interfaces) > ti_idx)
824 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
825 clib_warning ("reusing tap interface");
827 // "remove" from inactive list
828 _vec_len (tm->tapcli_inactive_interfaces) -= 1;
831 // ti was not retrieved from inactive ifaces - create new
833 vec_add2 (tm->tapcli_interfaces, ti, 1);
842 unsigned int ifindex;
846 * @brief Connect a TAP interface
848 * @param vm - vlib_main_t
849 * @param ap - vnet_tap_connect_args_t
855 vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
857 tapcli_main_t *tm = &tapcli_main;
858 tapcli_interface_t *ti = NULL;
869 return VNET_API_ERROR_FEATURE_DISABLED;
872 flags = IFF_TAP | IFF_NO_PI;
874 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
875 return VNET_API_ERROR_SYSCALL_ERROR_1;
877 memset (&ifr, 0, sizeof (ifr));
878 strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name) - 1);
879 ifr.ifr_flags = flags;
880 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
882 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
886 /* Open a provisioning socket */
887 if ((dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
889 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
893 /* Find the interface index. */
896 struct sockaddr_ll sll;
898 memset (&ifr, 0, sizeof (ifr));
899 strncpy (ifr.ifr_name, (char *) ap->intfc_name,
900 sizeof (ifr.ifr_name) - 1);
901 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
903 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
907 /* Bind the provisioning socket to the interface. */
908 memset (&sll, 0, sizeof (sll));
909 sll.sll_family = AF_PACKET;
910 sll.sll_ifindex = ifr.ifr_ifindex;
911 sll.sll_protocol = htons (ETH_P_ALL);
913 if (bind (dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
915 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
920 /* non-blocking I/O on /dev/tapX */
923 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
925 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
929 ifr.ifr_mtu = tm->mtu_bytes;
930 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
932 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
936 /* get flags, modify to bring up interface... */
937 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
939 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
943 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
945 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
947 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
951 if (ap->ip4_address_set)
953 struct sockaddr_in sin;
954 /* ip4: mask defaults to /24 */
955 u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
957 memset (&sin, 0, sizeof (sin));
958 sin.sin_family = AF_INET;
959 /* sin.sin_port = 0; */
960 sin.sin_addr.s_addr = ap->ip4_address->as_u32;
961 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
963 if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
965 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
969 if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
972 mask <<= (32 - ap->ip4_mask_width);
975 mask = clib_host_to_net_u32 (mask);
976 sin.sin_family = AF_INET;
978 sin.sin_addr.s_addr = mask;
979 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
981 if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
983 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
988 if (ap->ip6_address_set)
994 sockfd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_IP);
997 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
1001 memset (&ifr2, 0, sizeof (ifr));
1002 strncpy (ifr2.ifr_name, (char *) ap->intfc_name,
1003 sizeof (ifr2.ifr_name) - 1);
1004 if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0)
1007 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
1011 memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
1012 ifr6.mask_width = ap->ip6_mask_width;
1013 ifr6.ifindex = ifr2.ifr_ifindex;
1015 if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
1018 clib_unix_warning ("ifr6");
1019 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
1025 ti = tapcli_get_new_tapif ();
1026 ti->per_interface_next_index = ~0;
1028 if (ap->hwaddr_arg != 0)
1029 clib_memcpy (hwaddr, ap->hwaddr_arg, 6);
1032 f64 now = vlib_time_now (vm);
1034 rnd = (u32) (now * 1e6);
1035 rnd = random_u32 (&rnd);
1037 memcpy (hwaddr + 2, &rnd, sizeof (rnd));
1042 error = ethernet_register_interface
1044 tapcli_dev_class.index,
1045 ti - tm->tapcli_interfaces /* device instance */ ,
1046 hwaddr /* ethernet address */ ,
1047 &ti->hw_if_index, tapcli_flag_change);
1051 clib_error_report (error);
1052 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1057 clib_file_t template = { 0 };
1058 template.read_function = tapcli_read_ready;
1059 template.file_descriptor = dev_net_tun_fd;
1060 ti->clib_file_index = clib_file_add (&file_main, &template);
1061 ti->unix_fd = dev_net_tun_fd;
1062 ti->provision_fd = dev_tap_fd;
1063 clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
1067 vnet_hw_interface_t *hw;
1068 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
1069 hw->min_supported_packet_bytes = TAP_MTU_MIN;
1070 hw->max_supported_packet_bytes = TAP_MTU_MAX;
1071 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] =
1072 hw->max_supported_packet_bytes - sizeof (ethernet_header_t);
1073 ti->sw_if_index = hw->sw_if_index;
1074 if (ap->sw_if_indexp)
1075 *(ap->sw_if_indexp) = hw->sw_if_index;
1080 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1081 ti - tm->tapcli_interfaces);
1083 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1084 ti - tm->tapcli_interfaces);
1089 close (dev_net_tun_fd);
1090 if (dev_tap_fd >= 0)
1097 * @brief Renumber a TAP interface
1099 * @param *vm - vlib_main_t
1100 * @param *intfc_name - u8
1101 * @param *hwaddr_arg - u8
1102 * @param *sw_if_indexp - u32
1103 * @param renumber - u8
1104 * @param custom_dev_instance - u32
1110 vnet_tap_connect_renumber (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
1112 int rv = vnet_tap_connect (vm, ap);
1114 if (!rv && ap->renumber)
1115 vnet_interface_name_renumber (*(ap->sw_if_indexp),
1116 ap->custom_dev_instance);
1122 * @brief Disconnect TAP CLI interface
1124 * @param *ti - tapcli_interface_t
1130 tapcli_tap_disconnect (tapcli_interface_t * ti)
1133 vnet_main_t *vnm = vnet_get_main ();
1134 tapcli_main_t *tm = &tapcli_main;
1135 u32 sw_if_index = ti->sw_if_index;
1137 // bring interface down
1138 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1140 if (ti->clib_file_index != ~0)
1142 clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index);
1143 ti->clib_file_index = ~0;
1146 close (ti->unix_fd);
1148 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
1149 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
1150 close (ti->provision_fd);
1152 ti->provision_fd = -1;
1158 * @brief Delete TAP interface
1160 * @param *vm - vlib_main_t
1161 * @param sw_if_index - u32
1167 vnet_tap_delete (vlib_main_t * vm, u32 sw_if_index)
1170 tapcli_main_t *tm = &tapcli_main;
1171 tapcli_interface_t *ti;
1174 p = hash_get (tm->tapcli_interface_index_by_sw_if_index, sw_if_index);
1177 clib_warning ("sw_if_index %d unknown", sw_if_index);
1178 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1180 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1184 tapcli_tap_disconnect (ti);
1185 // add to inactive list
1186 vec_add1 (tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
1188 // reset renumbered iface
1189 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
1190 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1192 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
1197 * @brief CLI function to delete TAP interface
1199 * @param *vm - vlib_main_t
1200 * @param *input - unformat_input_t
1201 * @param *cmd - vlib_cli_command_t
1203 * @return error - clib_error_t
1206 static clib_error_t *
1207 tap_delete_command_fn (vlib_main_t * vm,
1208 unformat_input_t * input, vlib_cli_command_t * cmd)
1210 tapcli_main_t *tm = &tapcli_main;
1211 u32 sw_if_index = ~0;
1213 if (tm->is_disabled)
1215 return clib_error_return (0, "device disabled...");
1218 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1222 return clib_error_return (0, "unknown input `%U'",
1223 format_unformat_error, input);
1226 int rc = vnet_tap_delete (vm, sw_if_index);
1230 vlib_cli_output (vm, "Deleted.");
1234 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)",
1242 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1243 .path = "tap delete",
1244 .short_help = "tap delete <vpp-tap-intfc-name>",
1245 .function = tap_delete_command_fn,
1250 * @brief Modifies tap interface - can result in new interface being created
1252 * @param *vm - vlib_main_t
1253 * @param orig_sw_if_index - u32
1254 * @param *intfc_name - u8
1255 * @param *hwaddr_arg - u8
1256 * @param *sw_if_indexp - u32
1257 * @param renumber - u8
1258 * @param custom_dev_instance - u32
1264 vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
1266 int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
1271 rv = vnet_tap_connect_renumber (vm, ap);
1277 * @brief CLI function to modify TAP interface
1279 * @param *vm - vlib_main_t
1280 * @param *input - unformat_input_t
1281 * @param *cmd - vlib_cli_command_t
1283 * @return error - clib_error_t
1286 static clib_error_t *
1287 tap_modify_command_fn (vlib_main_t * vm,
1288 unformat_input_t * input, vlib_cli_command_t * cmd)
1291 tapcli_main_t *tm = &tapcli_main;
1292 u32 sw_if_index = ~0;
1293 u32 new_sw_if_index = ~0;
1294 int user_hwaddr = 0;
1296 vnet_tap_connect_args_t _a, *ap = &_a;
1298 if (tm->is_disabled)
1300 return clib_error_return (0, "device disabled...");
1303 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1307 return clib_error_return (0, "unknown input `%U'",
1308 format_unformat_error, input);
1310 if (unformat (input, "%s", &intfc_name))
1313 return clib_error_return (0, "unknown input `%U'",
1314 format_unformat_error, input);
1316 if (unformat (input, "hwaddr %U", unformat_ethernet_address, &hwaddr))
1320 memset (ap, 0, sizeof (*ap));
1321 ap->orig_sw_if_index = sw_if_index;
1322 ap->intfc_name = intfc_name;
1323 ap->sw_if_indexp = &new_sw_if_index;
1325 ap->hwaddr_arg = hwaddr;
1327 int rc = vnet_tap_modify (vm, ap);
1331 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1332 format_vnet_sw_if_index_name, tm->vnet_main,
1333 *(ap->sw_if_indexp), ap->intfc_name);
1337 vlib_cli_output (vm,
1338 "Error during modification of tap interface. (rc: %d)",
1346 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1347 .path = "tap modify",
1348 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1349 .function = tap_modify_command_fn,
1354 * @brief CLI function to connect TAP interface
1356 * @param *vm - vlib_main_t
1357 * @param *input - unformat_input_t
1358 * @param *cmd - vlib_cli_command_t
1360 * @return error - clib_error_t
1363 static clib_error_t *
1364 tap_connect_command_fn (vlib_main_t * vm,
1365 unformat_input_t * input, vlib_cli_command_t * cmd)
1368 unformat_input_t _line_input, *line_input = &_line_input;
1369 vnet_tap_connect_args_t _a, *ap = &_a;
1370 tapcli_main_t *tm = &tapcli_main;
1374 ip4_address_t ip4_address;
1375 int ip4_address_set = 0;
1376 ip6_address_t ip6_address;
1377 int ip6_address_set = 0;
1378 u32 ip4_mask_width = 0;
1379 u32 ip6_mask_width = 0;
1380 clib_error_t *error = NULL;
1382 if (tm->is_disabled)
1383 return clib_error_return (0, "device disabled...");
1385 if (!unformat_user (input, unformat_line_input, line_input))
1388 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1390 if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1392 hwaddr_arg = hwaddr;
1394 /* It is here for backward compatibility */
1395 else if (unformat (line_input, "hwaddr random"))
1398 else if (unformat (line_input, "address %U/%d",
1399 unformat_ip4_address, &ip4_address, &ip4_mask_width))
1400 ip4_address_set = 1;
1402 else if (unformat (line_input, "address %U/%d",
1403 unformat_ip6_address, &ip6_address, &ip6_mask_width))
1404 ip6_address_set = 1;
1406 else if (unformat (line_input, "%s", &intfc_name))
1410 error = clib_error_return (0, "unknown input `%U'",
1411 format_unformat_error, line_input);
1416 if (intfc_name == 0)
1418 error = clib_error_return (0, "interface name must be specified");
1422 memset (ap, 0, sizeof (*ap));
1424 ap->intfc_name = intfc_name;
1425 ap->hwaddr_arg = hwaddr_arg;
1426 if (ip4_address_set)
1428 ap->ip4_address = &ip4_address;
1429 ap->ip4_mask_width = ip4_mask_width;
1430 ap->ip4_address_set = 1;
1432 if (ip6_address_set)
1434 ap->ip6_address = &ip6_address;
1435 ap->ip6_mask_width = ip6_mask_width;
1436 ap->ip6_address_set = 1;
1439 ap->sw_if_indexp = &sw_if_index;
1441 int rv = vnet_tap_connect (vm, ap);
1445 case VNET_API_ERROR_SYSCALL_ERROR_1:
1446 error = clib_error_return (0, "Couldn't open /dev/net/tun");
1449 case VNET_API_ERROR_SYSCALL_ERROR_2:
1451 clib_error_return (0, "Error setting flags on '%s'", intfc_name);
1454 case VNET_API_ERROR_SYSCALL_ERROR_3:
1455 error = clib_error_return (0, "Couldn't open provisioning socket");
1458 case VNET_API_ERROR_SYSCALL_ERROR_4:
1459 error = clib_error_return (0, "Couldn't get if_index");
1462 case VNET_API_ERROR_SYSCALL_ERROR_5:
1463 error = clib_error_return (0, "Couldn't bind provisioning socket");
1466 case VNET_API_ERROR_SYSCALL_ERROR_6:
1467 error = clib_error_return (0, "Couldn't set device non-blocking flag");
1470 case VNET_API_ERROR_SYSCALL_ERROR_7:
1471 error = clib_error_return (0, "Couldn't set device MTU");
1474 case VNET_API_ERROR_SYSCALL_ERROR_8:
1475 error = clib_error_return (0, "Couldn't get interface flags");
1478 case VNET_API_ERROR_SYSCALL_ERROR_9:
1479 error = clib_error_return (0, "Couldn't set intfc admin state up");
1482 case VNET_API_ERROR_SYSCALL_ERROR_10:
1483 error = clib_error_return (0, "Couldn't set intfc address/mask");
1486 case VNET_API_ERROR_INVALID_REGISTRATION:
1487 error = clib_error_return (0, "Invalid registration");
1494 error = clib_error_return (0, "Unknown error: %d", rv);
1498 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
1499 vnet_get_main (), sw_if_index);
1502 unformat_free (line_input);
1508 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1509 .path = "tap connect",
1511 "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]",
1512 .function = tap_connect_command_fn,
1517 * @brief TAPCLI main init
1519 * @param *vm - vlib_main_t
1521 * @return error - clib_error_t
1525 tapcli_init (vlib_main_t * vm)
1527 tapcli_main_t *tm = &tapcli_main;
1528 vlib_thread_main_t *m = vlib_get_thread_main ();
1529 tapcli_per_thread_t *thread;
1532 tm->vnet_main = vnet_get_main ();
1533 tm->mtu_bytes = TAP_MTU_DEFAULT;
1534 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
1535 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1536 vm->os_punt_frame = tapcli_nopunt_frame;
1537 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1538 CLIB_CACHE_LINE_BYTES);
1539 vec_foreach (thread, tm->threads)
1542 thread->rx_buffers = 0;
1543 vec_alloc (thread->rx_buffers, VLIB_FRAME_SIZE);
1544 vec_reset_length (thread->rx_buffers);
1550 VLIB_INIT_FUNCTION (tapcli_init);
1553 * fd.io coding-style-patch-verification: ON
1556 * eval: (c-set-style "gnu")