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
66 u32 per_interface_next_index;
72 * @brief Struct for RX trace
79 * @brief Function to format TAP CLI trace
81 * @param *s - u8 - formatting string
82 * @param *va - va_list
84 * @return *s - u8 - formatted string
87 u8 * format_tapcli_rx_trace (u8 * s, va_list * va)
89 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
90 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
91 vnet_main_t * vnm = vnet_get_main();
92 tapcli_rx_trace_t * t = va_arg (*va, tapcli_rx_trace_t *);
93 s = format (s, "%U", format_vnet_sw_if_index_name,
99 * @brief TAPCLI main state struct
102 /** Vector of iovecs for readv/writev calls. */
103 struct iovec * iovecs;
105 /** Vector of VLIB rx buffers to use. We allocate them in blocks
106 of VLIB_FRAME_SIZE (256). */
109 /** tap device destination MAC address. Required, or Linux drops pkts */
112 /** Interface MTU in bytes and # of default sized buffers. */
113 u32 mtu_bytes, mtu_buffers;
115 /** Vector of tap interfaces */
116 tapcli_interface_t * tapcli_interfaces;
118 /** Vector of deleted tap interfaces */
119 u32 * tapcli_inactive_interfaces;
121 /** Bitmap of tap interfaces with pending reads */
122 uword * pending_read_bitmap;
124 /** Hash table to find tapcli interface given hw_if_index */
125 uword * tapcli_interface_index_by_sw_if_index;
127 /** Hash table to find tapcli interface given unix fd */
128 uword * tapcli_interface_index_by_unix_fd;
130 /** renumbering table */
131 u32 * show_dev_instance_by_real_dev_instance;
133 /** 1 => disable CLI */
136 /** convenience - vlib_main_t */
137 vlib_main_t * vlib_main;
138 /** convenience - vnet_main_t */
139 vnet_main_t * vnet_main;
140 /** convenience - unix_main_t */
141 unix_main_t * unix_main;
144 static tapcli_main_t tapcli_main;
147 * @brief tapcli TX node function
150 * Output node, writes the buffers comprising the incoming frame
151 * to the tun/tap device, aka hands them to the Linux kernel stack.
153 * @param *vm - vlib_main_t
154 * @param *node - vlib_node_runtime_t
155 * @param *frame - vlib_frame_t
157 * @return n_packets - uword
161 tapcli_tx (vlib_main_t * vm,
162 vlib_node_runtime_t * node,
163 vlib_frame_t * frame)
165 u32 * buffers = vlib_frame_args (frame);
166 uword n_packets = frame->n_vectors;
167 tapcli_main_t * tm = &tapcli_main;
168 tapcli_interface_t * ti;
171 for (i = 0; i < n_packets; i++)
176 vnet_hw_interface_t * hw;
180 b = vlib_get_buffer (vm, buffers[i]);
182 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
183 if (tx_sw_if_index == (u32)~0)
184 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
186 ASSERT(tx_sw_if_index != (u32)~0);
188 /* Use the sup intfc to finesse vlan subifs */
189 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
190 tx_sw_if_index = hw->sw_if_index;
192 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
196 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
197 /* $$$ leak, but this should never happen... */
201 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
203 /* Re-set iovecs if present. */
205 _vec_len (tm->iovecs) = 0;
207 /* VLIB buffer chain -> Unix iovec(s). */
208 vec_add2 (tm->iovecs, iov, 1);
209 iov->iov_base = b->data + b->current_data;
210 iov->iov_len = l = b->current_length;
212 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
215 b = vlib_get_buffer (vm, b->next_buffer);
217 vec_add2 (tm->iovecs, iov, 1);
219 iov->iov_base = b->data + b->current_data;
220 iov->iov_len = b->current_length;
221 l += b->current_length;
222 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
225 if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
226 clib_unix_warning ("writev");
229 vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
234 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
235 .function = tapcli_tx,
237 .type = VLIB_NODE_TYPE_INTERNAL,
242 * @brief Dispatch tapcli RX node function for node tap_cli_rx
245 * @param *vm - vlib_main_t
246 * @param *node - vlib_node_runtime_t
247 * @param *ti - tapcli_interface_t
249 * @return n_packets - uword
252 static uword tapcli_rx_iface(vlib_main_t * vm,
253 vlib_node_runtime_t * node,
254 tapcli_interface_t * ti)
256 tapcli_main_t * tm = &tapcli_main;
257 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
258 u32 n_trace = vlib_get_trace_count (vm, node);
262 vnet_sw_interface_t * si;
264 u32 next = node->cached_next_index;
265 u32 n_left_to_next, next_index;
268 vnm = vnet_get_main();
269 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
270 admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
272 vlib_get_next_frame(vm, node, next, to_next, n_left_to_next);
274 while (n_left_to_next) { // Fill at most one vector
275 vlib_buffer_t *b_first, *b, *prev;
277 word n_bytes_in_packet;
280 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
281 uword len = vec_len(tm->rx_buffers);
282 _vec_len(tm->rx_buffers) +=
283 vlib_buffer_alloc_from_free_list(vm, &tm->rx_buffers[len],
284 VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
285 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
286 vlib_node_increment_counter(vm, tapcli_rx_node.index,
287 TAPCLI_ERROR_BUFFER_ALLOC,
288 tm->mtu_buffers - vec_len(tm->rx_buffers));
293 uword i_rx = vec_len (tm->rx_buffers) - 1;
295 /* Allocate RX buffers from end of rx_buffers.
296 Turn them into iovecs to pass to readv. */
297 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
298 for (j = 0; j < tm->mtu_buffers; j++) {
299 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
300 tm->iovecs[j].iov_base = b->data;
301 tm->iovecs[j].iov_len = buffer_size;
304 n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
305 n_bytes_in_packet = n_bytes_left;
306 if (n_bytes_left <= 0) {
307 if (errno != EAGAIN) {
308 vlib_node_increment_counter(vm, tapcli_rx_node.index,
309 TAPCLI_ERROR_READ, 1);
314 bi_first = tm->rx_buffers[i_rx];
315 b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
319 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
320 n_bytes_left -= buffer_size;
323 prev->next_buffer = bi;
324 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
329 if (n_bytes_left <= 0)
333 bi = tm->rx_buffers[i_rx];
334 b = vlib_get_buffer (vm, bi);
337 _vec_len (tm->rx_buffers) = i_rx;
339 b_first->total_length_not_including_first_buffer =
340 (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
341 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
343 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
345 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
346 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
348 b_first->error = node->errors[TAPCLI_ERROR_NONE];
349 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
350 next_index = (ti->per_interface_next_index != ~0) ?
351 ti->per_interface_next_index : next_index;
352 next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index;
354 to_next[0] = bi_first;
358 vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index,
361 vlib_validate_buffer_enqueue_x1 (vm, node, next,
362 to_next, n_left_to_next,
363 bi_first, next_index);
365 /* Interface counters for tapcli interface. */
366 if (PREDICT_TRUE(!admin_down)) {
367 vlib_increment_combined_counter (
368 vnet_main.interface_main.combined_sw_if_counters
369 + VNET_INTERFACE_COUNTER_RX,
370 os_get_cpu_number(), ti->sw_if_index,
371 1, n_bytes_in_packet);
373 if (PREDICT_FALSE(n_trace > 0)) {
374 vlib_trace_buffer (vm, node, next_index,
375 b_first, /* follow_chain */ 1);
378 tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0));
379 t0->sw_if_index = si->sw_if_index;
383 vlib_put_next_frame (vm, node, next, n_left_to_next);
385 vlib_set_trace_count (vm, node, n_trace);
386 return VLIB_FRAME_SIZE - n_left_to_next;
390 * @brief tapcli RX node function
393 * Input node from the Kernel tun/tap device
395 * @param *vm - vlib_main_t
396 * @param *node - vlib_node_runtime_t
397 * @param *frame - vlib_frame_t
399 * @return n_packets - uword
403 tapcli_rx (vlib_main_t * vm,
404 vlib_node_runtime_t * node,
405 vlib_frame_t * frame)
407 tapcli_main_t * tm = &tapcli_main;
408 static u32 * ready_interface_indices;
409 tapcli_interface_t * ti;
413 vec_reset_length (ready_interface_indices);
414 clib_bitmap_foreach (i, tm->pending_read_bitmap,
416 vec_add1 (ready_interface_indices, i);
419 if (vec_len (ready_interface_indices) == 0)
422 for (i = 0; i < vec_len(ready_interface_indices); i++)
424 tm->pending_read_bitmap =
425 clib_bitmap_set (tm->pending_read_bitmap,
426 ready_interface_indices[i], 0);
428 ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
429 total_count += tapcli_rx_iface(vm, node, ti);
431 return total_count; //This might return more than 256.
434 /** TAPCLI error strings */
435 static char * tapcli_rx_error_strings[] = {
436 #define _(sym,string) string,
441 VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
442 .function = tapcli_rx,
444 .sibling_of = "device-input",
445 .type = VLIB_NODE_TYPE_INPUT,
446 .state = VLIB_NODE_STATE_INTERRUPT,
448 .n_errors = TAPCLI_N_ERROR,
449 .error_strings = tapcli_rx_error_strings,
450 .format_trace = format_tapcli_rx_trace,
455 * @brief Gets called when file descriptor is ready from epoll.
457 * @param *uf - unix_file_t
459 * @return error - clib_error_t
462 static clib_error_t * tapcli_read_ready (unix_file_t * uf)
464 vlib_main_t * vm = vlib_get_main();
465 tapcli_main_t * tm = &tapcli_main;
468 /** Schedule the rx node */
469 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
471 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
473 /** Mark the specific tap interface ready-to-read */
475 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
478 clib_warning ("fd %d not in hash table", uf->file_descriptor);
484 * @brief CLI function for TAPCLI configuration
486 * @param *vm - vlib_main_t
487 * @param *input - unformat_input_t
489 * @return error - clib_error_t
492 static clib_error_t *
493 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
495 tapcli_main_t *tm = &tapcli_main;
496 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
498 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
500 if (unformat (input, "mtu %d", &tm->mtu_bytes))
502 else if (unformat (input, "disable"))
505 return clib_error_return (0, "unknown input `%U'",
506 format_unformat_error, input);
514 clib_warning ("tapcli disabled: must be superuser");
519 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
525 * @brief Renumber TAPCLI interface
527 * @param *hi - vnet_hw_interface_t
528 * @param new_dev_instance - u32
533 static int tap_name_renumber (vnet_hw_interface_t * hi,
534 u32 new_dev_instance)
536 tapcli_main_t *tm = &tapcli_main;
538 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
539 hi->dev_instance, ~0);
541 tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
547 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
550 * @brief Free "no punt" frame
552 * @param *vm - vlib_main_t
553 * @param *node - vlib_node_runtime_t
554 * @param *frame - vlib_frame_t
558 tapcli_nopunt_frame (vlib_main_t * vm,
559 vlib_node_runtime_t * node,
560 vlib_frame_t * frame)
562 u32 * buffers = vlib_frame_args (frame);
563 uword n_packets = frame->n_vectors;
564 vlib_buffer_free (vm, buffers, n_packets);
565 vlib_frame_free (vm, node, frame);
568 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
570 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
574 * @brief Formatter for TAPCLI interface name
576 * @param *s - formatter string
577 * @param *args - va_list
579 * @return *s - formatted string
582 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
584 u32 i = va_arg (*args, u32);
585 u32 show_dev_instance = ~0;
586 tapcli_main_t * tm = &tapcli_main;
588 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
589 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
591 if (show_dev_instance != ~0)
592 i = show_dev_instance;
594 s = format (s, "tap-%d", i);
599 * @brief Modify interface flags for TAPCLI interface
601 * @param *vnm - vnet_main_t
602 * @param *hw - vnet_hw_interface_t
608 static u32 tapcli_flag_change (vnet_main_t * vnm,
609 vnet_hw_interface_t * hw,
612 tapcli_main_t *tm = &tapcli_main;
613 tapcli_interface_t *ti;
615 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
617 if (flags & ETHERNET_INTERFACE_FLAG_MTU)
619 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
620 tm->mtu_bytes = hw->max_packet_bytes;
621 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
628 memcpy (&ifr, &ti->ifr, sizeof (ifr));
630 /* get flags, modify to bring up interface... */
631 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
633 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
637 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
639 if (want_promisc == ti->is_promisc)
642 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
643 ifr.ifr_flags |= IFF_PROMISC;
645 ifr.ifr_flags &= ~(IFF_PROMISC);
647 /* get flags, modify to bring up interface... */
648 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
650 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
654 ti->is_promisc = want_promisc;
661 * @brief Setting the TAP interface's next processing node
663 * @param *vnm - vnet_main_t
664 * @param hw_if_index - u32
665 * @param node_index - u32
668 static void tapcli_set_interface_next_node (vnet_main_t *vnm,
672 tapcli_main_t *tm = &tapcli_main;
673 tapcli_interface_t *ti;
674 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
676 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
678 /** Shut off redirection */
679 if (node_index == ~0)
681 ti->per_interface_next_index = node_index;
685 ti->per_interface_next_index =
686 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
690 * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
692 * @param *vnm - vnet_main_t
693 * @param hw_if_index - u32
696 * @return error - clib_error_t
698 static clib_error_t *
699 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
701 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
703 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
704 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
707 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
709 hw_flags = speed_duplex;
711 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
715 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
717 .tx_function = tapcli_tx,
718 .format_device_name = format_tapcli_interface_name,
719 .rx_redirect_to_node = tapcli_set_interface_next_node,
720 .name_renumber = tap_name_renumber,
721 .admin_up_down_function = tapcli_interface_admin_up_down,
725 * @brief Dump TAP interfaces
727 * @param **out_tapids - tapcli_interface_details_t
732 int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
734 tapcli_main_t * tm = &tapcli_main;
735 tapcli_interface_t * ti;
737 tapcli_interface_details_t * r_tapids = NULL;
738 tapcli_interface_details_t * tapid = NULL;
740 vec_foreach (ti, tm->tapcli_interfaces) {
743 vec_add2(r_tapids, tapid, 1);
744 tapid->sw_if_index = ti->sw_if_index;
745 strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
748 *out_tapids = r_tapids;
754 * @brief Get tap interface from inactive interfaces or create new
756 * @return interface - tapcli_interface_t
759 static tapcli_interface_t *tapcli_get_new_tapif()
761 tapcli_main_t * tm = &tapcli_main;
762 tapcli_interface_t *ti = NULL;
764 int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
765 // if there are any inactive ifaces
766 if (inactive_cnt > 0) {
768 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
769 if (vec_len(tm->tapcli_interfaces) > ti_idx) {
770 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
771 clib_warning("reusing tap interface");
773 // "remove" from inactive list
774 _vec_len(tm->tapcli_inactive_interfaces) -= 1;
777 // ti was not retrieved from inactive ifaces - create new
779 vec_add2 (tm->tapcli_interfaces, ti, 1);
788 unsigned int ifindex;
792 * @brief Connect a TAP interface
794 * @param vm - vlib_main_t
795 * @param ap - vnet_tap_connect_args_t
800 int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
802 tapcli_main_t * tm = &tapcli_main;
803 tapcli_interface_t * ti = NULL;
808 clib_error_t * error;
814 return VNET_API_ERROR_FEATURE_DISABLED;
817 flags = IFF_TAP | IFF_NO_PI;
819 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
820 return VNET_API_ERROR_SYSCALL_ERROR_1;
822 memset (&ifr, 0, sizeof (ifr));
823 strncpy(ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
824 ifr.ifr_flags = flags;
825 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
827 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
831 /* Open a provisioning socket */
832 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
833 htons(ETH_P_ALL))) < 0 )
835 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
839 /* Find the interface index. */
842 struct sockaddr_ll sll;
844 memset (&ifr, 0, sizeof(ifr));
845 strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
846 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
848 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
852 /* Bind the provisioning socket to the interface. */
853 memset(&sll, 0, sizeof(sll));
854 sll.sll_family = AF_PACKET;
855 sll.sll_ifindex = ifr.ifr_ifindex;
856 sll.sll_protocol = htons(ETH_P_ALL);
858 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
860 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
865 /* non-blocking I/O on /dev/tapX */
868 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
870 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
874 ifr.ifr_mtu = tm->mtu_bytes;
875 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
877 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
881 /* get flags, modify to bring up interface... */
882 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
884 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
888 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
890 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
892 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
896 if (ap->ip4_address_set)
898 struct sockaddr_in sin;
899 /* ip4: mask defaults to /24 */
900 u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
902 sin.sin_family = AF_INET;
904 sin.sin_addr.s_addr = ap->ip4_address->as_u32;
905 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
907 if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
909 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
913 if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
916 mask <<= (32 - ap->ip4_mask_width);
919 mask = clib_host_to_net_u32(mask);
920 sin.sin_family = AF_INET;
922 sin.sin_addr.s_addr = mask;
923 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
925 if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
927 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
932 if (ap->ip6_address_set)
938 sockfd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP);
941 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
945 memset (&ifr2, 0, sizeof(ifr));
946 strncpy (ifr2.ifr_name, (char *) ap->intfc_name,
947 sizeof (ifr2.ifr_name)-1);
948 if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0 )
951 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
955 memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
956 ifr6.mask_width = ap->ip6_mask_width;
957 ifr6.ifindex = ifr2.ifr_ifindex;
959 if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
962 clib_unix_warning ("ifr6");
963 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
969 ti = tapcli_get_new_tapif();
970 ti->per_interface_next_index = ~0;
972 if (ap->hwaddr_arg != 0)
973 clib_memcpy(hwaddr, ap->hwaddr_arg, 6);
976 f64 now = vlib_time_now(vm);
978 rnd = (u32) (now * 1e6);
979 rnd = random_u32 (&rnd);
981 memcpy (hwaddr+2, &rnd, sizeof(rnd));
986 error = ethernet_register_interface
988 tapcli_dev_class.index,
989 ti - tm->tapcli_interfaces /* device instance */,
990 hwaddr /* ethernet address */,
996 clib_error_report (error);
997 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1002 unix_file_t template = {0};
1003 template.read_function = tapcli_read_ready;
1004 template.file_descriptor = dev_net_tun_fd;
1005 ti->unix_file_index = unix_file_add (&unix_main, &template);
1006 ti->unix_fd = dev_net_tun_fd;
1007 ti->provision_fd = dev_tap_fd;
1008 clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
1012 vnet_hw_interface_t * hw;
1013 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
1014 hw->min_supported_packet_bytes = TAP_MTU_MIN;
1015 hw->max_supported_packet_bytes = TAP_MTU_MAX;
1016 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t);
1017 ti->sw_if_index = hw->sw_if_index;
1018 if (ap->sw_if_indexp)
1019 *(ap->sw_if_indexp) = hw->sw_if_index;
1024 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1025 ti - tm->tapcli_interfaces);
1027 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1028 ti - tm->tapcli_interfaces);
1033 close (dev_net_tun_fd);
1034 if (dev_tap_fd >= 0)
1041 * @brief Renumber a TAP interface
1043 * @param *vm - vlib_main_t
1044 * @param *intfc_name - u8
1045 * @param *hwaddr_arg - u8
1046 * @param *sw_if_indexp - u32
1047 * @param renumber - u8
1048 * @param custom_dev_instance - u32
1053 int vnet_tap_connect_renumber (vlib_main_t * vm,
1054 vnet_tap_connect_args_t *ap)
1056 int rv = vnet_tap_connect(vm, ap);
1058 if (!rv && ap->renumber)
1059 vnet_interface_name_renumber (*(ap->sw_if_indexp), ap->custom_dev_instance);
1065 * @brief Disconnect TAP CLI interface
1067 * @param *ti - tapcli_interface_t
1072 static int tapcli_tap_disconnect (tapcli_interface_t *ti)
1075 vnet_main_t * vnm = vnet_get_main();
1076 tapcli_main_t * tm = &tapcli_main;
1077 u32 sw_if_index = ti->sw_if_index;
1079 // bring interface down
1080 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1082 if (ti->unix_file_index != ~0) {
1083 unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
1084 ti->unix_file_index = ~0;
1089 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
1090 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
1091 close(ti->provision_fd);
1093 ti->provision_fd = -1;
1099 * @brief Delete TAP interface
1101 * @param *vm - vlib_main_t
1102 * @param sw_if_index - u32
1107 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
1110 tapcli_main_t * tm = &tapcli_main;
1111 tapcli_interface_t *ti;
1114 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
1117 clib_warning ("sw_if_index %d unknown", sw_if_index);
1118 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1120 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1124 tapcli_tap_disconnect(ti);
1125 // add to inactive list
1126 vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
1128 // reset renumbered iface
1129 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
1130 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1132 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
1137 * @brief CLI function to delete TAP interface
1139 * @param *vm - vlib_main_t
1140 * @param *input - unformat_input_t
1141 * @param *cmd - vlib_cli_command_t
1143 * @return error - clib_error_t
1146 static clib_error_t *
1147 tap_delete_command_fn (vlib_main_t * vm,
1148 unformat_input_t * input,
1149 vlib_cli_command_t * cmd)
1151 tapcli_main_t * tm = &tapcli_main;
1152 u32 sw_if_index = ~0;
1154 if (tm->is_disabled)
1156 return clib_error_return (0, "device disabled...");
1159 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1163 return clib_error_return (0, "unknown input `%U'",
1164 format_unformat_error, input);
1167 int rc = vnet_tap_delete (vm, sw_if_index);
1170 vlib_cli_output (vm, "Deleted.");
1172 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
1178 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1179 .path = "tap delete",
1180 .short_help = "tap delete <vpp-tap-intfc-name>",
1181 .function = tap_delete_command_fn,
1185 * @brief Modifies tap interface - can result in new interface being created
1187 * @param *vm - vlib_main_t
1188 * @param orig_sw_if_index - u32
1189 * @param *intfc_name - u8
1190 * @param *hwaddr_arg - u8
1191 * @param *sw_if_indexp - u32
1192 * @param renumber - u8
1193 * @param custom_dev_instance - u32
1198 int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
1200 int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
1205 rv = vnet_tap_connect_renumber(vm, ap);
1211 * @brief CLI function to modify TAP interface
1213 * @param *vm - vlib_main_t
1214 * @param *input - unformat_input_t
1215 * @param *cmd - vlib_cli_command_t
1217 * @return error - clib_error_t
1220 static clib_error_t *
1221 tap_modify_command_fn (vlib_main_t * vm,
1222 unformat_input_t * input,
1223 vlib_cli_command_t * cmd)
1226 tapcli_main_t * tm = &tapcli_main;
1227 u32 sw_if_index = ~0;
1228 u32 new_sw_if_index = ~0;
1229 int user_hwaddr = 0;
1231 vnet_tap_connect_args_t _a, *ap= &_a;
1233 if (tm->is_disabled)
1235 return clib_error_return (0, "device disabled...");
1238 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1242 return clib_error_return (0, "unknown input `%U'",
1243 format_unformat_error, input);
1245 if (unformat (input, "%s", &intfc_name))
1248 return clib_error_return (0, "unknown input `%U'",
1249 format_unformat_error, input);
1251 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1256 memset (ap, 0, sizeof(*ap));
1257 ap->orig_sw_if_index = sw_if_index;
1258 ap->intfc_name = intfc_name;
1259 ap->sw_if_indexp = &new_sw_if_index;
1261 ap->hwaddr_arg = hwaddr;
1263 int rc = vnet_tap_modify (vm, ap);
1266 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1267 format_vnet_sw_if_index_name, tm->vnet_main,
1268 *(ap->sw_if_indexp), ap->intfc_name);
1270 vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
1276 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1277 .path = "tap modify",
1278 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1279 .function = tap_modify_command_fn,
1283 * @brief CLI function to connect TAP interface
1285 * @param *vm - vlib_main_t
1286 * @param *input - unformat_input_t
1287 * @param *cmd - vlib_cli_command_t
1289 * @return error - clib_error_t
1292 static clib_error_t *
1293 tap_connect_command_fn (vlib_main_t * vm,
1294 unformat_input_t * input,
1295 vlib_cli_command_t * cmd)
1298 unformat_input_t _line_input, *line_input = &_line_input;
1299 vnet_tap_connect_args_t _a, *ap= &_a;
1300 tapcli_main_t * tm = &tapcli_main;
1304 ip4_address_t ip4_address;
1305 int ip4_address_set = 0;
1306 ip6_address_t ip6_address;
1307 int ip6_address_set = 0;
1308 u32 ip4_mask_width = 0;
1309 u32 ip6_mask_width = 0;
1311 if (tm->is_disabled)
1312 return clib_error_return (0, "device disabled...");
1314 if (!unformat_user (input, unformat_line_input, line_input))
1317 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1319 if (unformat(line_input, "hwaddr %U", unformat_ethernet_address,
1321 hwaddr_arg = hwaddr;
1323 /* It is here for backward compatibility */
1324 else if (unformat(line_input, "hwaddr random"))
1327 else if (unformat (line_input, "address %U/%d",
1328 unformat_ip4_address, &ip4_address, &ip4_mask_width))
1329 ip4_address_set = 1;
1331 else if (unformat (line_input, "address %U/%d",
1332 unformat_ip6_address, &ip6_address, &ip6_mask_width))
1333 ip6_address_set = 1;
1335 else if (unformat (line_input, "%s", &intfc_name))
1338 return clib_error_return (0, "unknown input `%U'",
1339 format_unformat_error, line_input);
1342 memset (ap, 0, sizeof (*ap));
1344 ap->intfc_name = intfc_name;
1345 ap->hwaddr_arg = hwaddr_arg;
1346 if (ip4_address_set)
1348 ap->ip4_address = &ip4_address;
1349 ap->ip4_mask_width = ip4_mask_width;
1350 ap->ip4_address_set = 1;
1352 if (ip6_address_set)
1354 ap->ip6_address = &ip6_address;
1355 ap->ip6_mask_width = ip6_mask_width;
1356 ap->ip6_address_set = 1;
1359 ap->sw_if_indexp = &sw_if_index;
1361 int rv = vnet_tap_connect(vm, ap);
1365 case VNET_API_ERROR_SYSCALL_ERROR_1:
1366 return clib_error_return (0, "Couldn't open /dev/net/tun");
1368 case VNET_API_ERROR_SYSCALL_ERROR_2:
1369 return clib_error_return (0, "Error setting flags on '%s'", intfc_name);
1371 case VNET_API_ERROR_SYSCALL_ERROR_3:
1372 return clib_error_return (0, "Couldn't open provisioning socket");
1374 case VNET_API_ERROR_SYSCALL_ERROR_4:
1375 return clib_error_return (0, "Couldn't get if_index");
1377 case VNET_API_ERROR_SYSCALL_ERROR_5:
1378 return clib_error_return (0, "Couldn't bind provisioning socket");
1380 case VNET_API_ERROR_SYSCALL_ERROR_6:
1381 return clib_error_return (0, "Couldn't set device non-blocking flag");
1383 case VNET_API_ERROR_SYSCALL_ERROR_7:
1384 return clib_error_return (0, "Couldn't set device MTU");
1386 case VNET_API_ERROR_SYSCALL_ERROR_8:
1387 return clib_error_return (0, "Couldn't get interface flags");
1389 case VNET_API_ERROR_SYSCALL_ERROR_9:
1390 return clib_error_return (0, "Couldn't set intfc admin state up");
1392 case VNET_API_ERROR_SYSCALL_ERROR_10:
1393 return clib_error_return (0, "Couldn't set intfc address/mask");
1395 case VNET_API_ERROR_INVALID_REGISTRATION:
1396 return clib_error_return (0, "Invalid registration");
1402 return clib_error_return (0, "Unknown error: %d", rv);
1405 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
1406 vnet_get_main(), sw_if_index);
1410 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1411 .path = "tap connect",
1412 .short_help = "tap connect <intfc-name> [hwaddr <addr>]",
1413 .function = tap_connect_command_fn,
1417 * @brief TAPCLI main init
1419 * @param *vm - vlib_main_t
1421 * @return error - clib_error_t
1425 tapcli_init (vlib_main_t * vm)
1427 tapcli_main_t * tm = &tapcli_main;
1430 tm->vnet_main = vnet_get_main();
1431 tm->unix_main = &unix_main;
1432 tm->mtu_bytes = TAP_MTU_DEFAULT;
1433 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1434 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1436 vec_alloc(tm->rx_buffers, VLIB_FRAME_SIZE);
1437 vec_reset_length(tm->rx_buffers);
1438 vm->os_punt_frame = tapcli_nopunt_frame;
1442 VLIB_INIT_FUNCTION (tapcli_init);