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>
43 #include <vnet/devices/dpdk/dpdk.h>
46 #include <vnet/unix/tapcli.h>
48 static vnet_device_class_t tapcli_dev_class;
49 static vnet_hw_interface_class_t tapcli_interface_class;
50 static vlib_node_registration_t tapcli_rx_node;
52 static void tapcli_nopunt_frame (vlib_main_t * vm,
53 vlib_node_runtime_t * node,
54 vlib_frame_t * frame);
56 * @brief Struct for the tapcli interface
67 u32 per_interface_next_index;
73 * @brief Struct for RX trace
80 * @brief Function to format TAP CLI trace
82 * @param *s - u8 - formatting string
83 * @param *va - va_list
85 * @return *s - u8 - formatted string
88 u8 * format_tapcli_rx_trace (u8 * s, va_list * va)
90 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
91 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
92 vnet_main_t * vnm = vnet_get_main();
93 tapcli_rx_trace_t * t = va_arg (*va, tapcli_rx_trace_t *);
94 s = format (s, "%U", format_vnet_sw_if_index_name,
100 * @brief TAPCLI main state struct
103 /** Vector of iovecs for readv/writev calls. */
104 struct iovec * iovecs;
106 /** Vector of VLIB rx buffers to use. We allocate them in blocks
107 of VLIB_FRAME_SIZE (256). */
110 /** tap device destination MAC address. Required, or Linux drops pkts */
113 /** Interface MTU in bytes and # of default sized buffers. */
114 u32 mtu_bytes, mtu_buffers;
116 /** Vector of tap interfaces */
117 tapcli_interface_t * tapcli_interfaces;
119 /** Vector of deleted tap interfaces */
120 u32 * tapcli_inactive_interfaces;
122 /** Bitmap of tap interfaces with pending reads */
123 uword * pending_read_bitmap;
125 /** Hash table to find tapcli interface given hw_if_index */
126 uword * tapcli_interface_index_by_sw_if_index;
128 /** Hash table to find tapcli interface given unix fd */
129 uword * tapcli_interface_index_by_unix_fd;
131 /** renumbering table */
132 u32 * show_dev_instance_by_real_dev_instance;
134 /** 1 => disable CLI */
137 /** convenience - vlib_main_t */
138 vlib_main_t * vlib_main;
139 /** convenience - vnet_main_t */
140 vnet_main_t * vnet_main;
141 /** convenience - unix_main_t */
142 unix_main_t * unix_main;
145 static tapcli_main_t tapcli_main;
148 * @brief tapcli TX node function
151 * Output node, writes the buffers comprising the incoming frame
152 * to the tun/tap device, aka hands them to the Linux kernel stack.
154 * @param *vm - vlib_main_t
155 * @param *node - vlib_node_runtime_t
156 * @param *frame - vlib_frame_t
158 * @return n_packets - uword
162 tapcli_tx (vlib_main_t * vm,
163 vlib_node_runtime_t * node,
164 vlib_frame_t * frame)
166 u32 * buffers = vlib_frame_args (frame);
167 uword n_packets = frame->n_vectors;
168 tapcli_main_t * tm = &tapcli_main;
169 tapcli_interface_t * ti;
172 for (i = 0; i < n_packets; i++)
177 vnet_hw_interface_t * hw;
181 b = vlib_get_buffer (vm, buffers[i]);
183 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
184 if (tx_sw_if_index == (u32)~0)
185 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
187 ASSERT(tx_sw_if_index != (u32)~0);
189 /* Use the sup intfc to finesse vlan subifs */
190 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
191 tx_sw_if_index = hw->sw_if_index;
193 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
197 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
198 /* $$$ leak, but this should never happen... */
202 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
204 /* Re-set iovecs if present. */
206 _vec_len (tm->iovecs) = 0;
208 /* VLIB buffer chain -> Unix iovec(s). */
209 vec_add2 (tm->iovecs, iov, 1);
210 iov->iov_base = b->data + b->current_data;
211 iov->iov_len = l = b->current_length;
213 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
216 b = vlib_get_buffer (vm, b->next_buffer);
218 vec_add2 (tm->iovecs, iov, 1);
220 iov->iov_base = b->data + b->current_data;
221 iov->iov_len = b->current_length;
222 l += b->current_length;
223 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
226 if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
227 clib_unix_warning ("writev");
230 vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
235 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
236 .function = tapcli_tx,
238 .type = VLIB_NODE_TYPE_INTERNAL,
243 TAPCLI_RX_NEXT_IP4_INPUT,
244 TAPCLI_RX_NEXT_IP6_INPUT,
245 TAPCLI_RX_NEXT_ETHERNET_INPUT,
253 * @brief Dispatch tapcli RX node function for node tap_cli_rx
256 * @param *vm - vlib_main_t
257 * @param *node - vlib_node_runtime_t
258 * @param *ti - tapcli_interface_t
260 * @return n_packets - uword
263 static uword tapcli_rx_iface(vlib_main_t * vm,
264 vlib_node_runtime_t * node,
265 tapcli_interface_t * ti)
267 tapcli_main_t * tm = &tapcli_main;
268 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
269 u32 n_trace = vlib_get_trace_count (vm, node);
273 vnet_sw_interface_t * si;
275 u32 next = node->cached_next_index;
276 u32 n_left_to_next, next_index;
279 vnm = vnet_get_main();
280 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
281 admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
283 vlib_get_next_frame(vm, node, next, to_next, n_left_to_next);
285 while (n_left_to_next) { // Fill at most one vector
286 vlib_buffer_t *b_first, *b, *prev;
288 word n_bytes_in_packet;
291 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
292 uword len = vec_len(tm->rx_buffers);
293 _vec_len(tm->rx_buffers) +=
294 vlib_buffer_alloc_from_free_list(vm, &tm->rx_buffers[len],
295 VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
296 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
297 vlib_node_increment_counter(vm, tapcli_rx_node.index,
298 TAPCLI_ERROR_BUFFER_ALLOC,
299 tm->mtu_buffers - vec_len(tm->rx_buffers));
304 uword i_rx = vec_len (tm->rx_buffers) - 1;
306 /* Allocate RX buffers from end of rx_buffers.
307 Turn them into iovecs to pass to readv. */
308 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
309 for (j = 0; j < tm->mtu_buffers; j++) {
310 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
311 tm->iovecs[j].iov_base = b->data;
312 tm->iovecs[j].iov_len = buffer_size;
315 n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
316 n_bytes_in_packet = n_bytes_left;
317 if (n_bytes_left <= 0) {
318 if (errno != EAGAIN) {
319 vlib_node_increment_counter(vm, tapcli_rx_node.index,
320 TAPCLI_ERROR_READ, 1);
325 bi_first = tm->rx_buffers[i_rx];
326 b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
330 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
331 n_bytes_left -= buffer_size;
334 prev->next_buffer = bi;
335 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
340 if (n_bytes_left <= 0)
344 bi = tm->rx_buffers[i_rx];
345 b = vlib_get_buffer (vm, bi);
348 _vec_len (tm->rx_buffers) = i_rx;
350 b_first->total_length_not_including_first_buffer =
351 (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
352 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
354 /* Ensure mbufs are updated */
355 vlib_buffer_chain_validate(vm, b_first);
357 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
359 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
360 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
362 b_first->error = node->errors[TAPCLI_ERROR_NONE];
363 next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
364 next_index = (ti->per_interface_next_index != ~0) ?
365 ti->per_interface_next_index : next_index;
366 next_index = admin_down ? TAPCLI_RX_NEXT_DROP : next_index;
368 to_next[0] = bi_first;
372 vlib_validate_buffer_enqueue_x1 (vm, node, next,
373 to_next, n_left_to_next,
374 bi_first, next_index);
376 /* Interface counters for tapcli interface. */
377 if (PREDICT_TRUE(!admin_down)) {
378 vlib_increment_combined_counter (
379 vnet_main.interface_main.combined_sw_if_counters
380 + VNET_INTERFACE_COUNTER_RX,
381 os_get_cpu_number(), ti->sw_if_index,
382 1, n_bytes_in_packet);
384 if (PREDICT_FALSE(n_trace > 0)) {
385 vlib_trace_buffer (vm, node, next_index,
386 b_first, /* follow_chain */ 1);
389 tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0));
390 t0->sw_if_index = si->sw_if_index;
394 vlib_put_next_frame (vm, node, next, n_left_to_next);
396 vlib_set_trace_count (vm, node, n_trace);
397 return VLIB_FRAME_SIZE - n_left_to_next;
401 * @brief tapcli RX node function
404 * Input node from the Kernel tun/tap device
406 * @param *vm - vlib_main_t
407 * @param *node - vlib_node_runtime_t
408 * @param *frame - vlib_frame_t
410 * @return n_packets - uword
414 tapcli_rx (vlib_main_t * vm,
415 vlib_node_runtime_t * node,
416 vlib_frame_t * frame)
418 tapcli_main_t * tm = &tapcli_main;
419 static u32 * ready_interface_indices;
420 tapcli_interface_t * ti;
424 vec_reset_length (ready_interface_indices);
425 clib_bitmap_foreach (i, tm->pending_read_bitmap,
427 vec_add1 (ready_interface_indices, i);
430 if (vec_len (ready_interface_indices) == 0)
433 for (i = 0; i < vec_len(ready_interface_indices); i++)
435 tm->pending_read_bitmap =
436 clib_bitmap_set (tm->pending_read_bitmap,
437 ready_interface_indices[i], 0);
439 ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
440 total_count += tapcli_rx_iface(vm, node, ti);
442 return total_count; //This might return more than 256.
445 /** TAPCLI error strings */
446 static char * tapcli_rx_error_strings[] = {
447 #define _(sym,string) string,
452 VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
453 .function = tapcli_rx,
455 .type = VLIB_NODE_TYPE_INPUT,
456 .state = VLIB_NODE_STATE_INTERRUPT,
458 .n_errors = TAPCLI_N_ERROR,
459 .error_strings = tapcli_rx_error_strings,
460 .format_trace = format_tapcli_rx_trace,
462 .n_next_nodes = TAPCLI_RX_N_NEXT,
464 [TAPCLI_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
465 [TAPCLI_RX_NEXT_IP6_INPUT] = "ip6-input",
466 [TAPCLI_RX_NEXT_DROP] = "error-drop",
467 [TAPCLI_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
473 * @brief Gets called when file descriptor is ready from epoll.
475 * @param *uf - unix_file_t
477 * @return error - clib_error_t
480 static clib_error_t * tapcli_read_ready (unix_file_t * uf)
482 vlib_main_t * vm = vlib_get_main();
483 tapcli_main_t * tm = &tapcli_main;
486 /** Schedule the rx node */
487 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
489 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
491 /** Mark the specific tap interface ready-to-read */
493 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
496 clib_warning ("fd %d not in hash table", uf->file_descriptor);
502 * @brief CLI function for TAPCLI configuration
504 * @param *vm - vlib_main_t
505 * @param *input - unformat_input_t
507 * @return error - clib_error_t
510 static clib_error_t *
511 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
513 tapcli_main_t *tm = &tapcli_main;
514 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
516 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
518 if (unformat (input, "mtu %d", &tm->mtu_bytes))
520 else if (unformat (input, "disable"))
523 return clib_error_return (0, "unknown input `%U'",
524 format_unformat_error, input);
532 clib_warning ("tapcli disabled: must be superuser");
537 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
543 * @brief Renumber TAPCLI interface
545 * @param *hi - vnet_hw_interface_t
546 * @param new_dev_instance - u32
551 static int tap_name_renumber (vnet_hw_interface_t * hi,
552 u32 new_dev_instance)
554 tapcli_main_t *tm = &tapcli_main;
556 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
557 hi->dev_instance, ~0);
559 tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
565 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
568 * @brief Free "no punt" frame
570 * @param *vm - vlib_main_t
571 * @param *node - vlib_node_runtime_t
572 * @param *frame - vlib_frame_t
576 tapcli_nopunt_frame (vlib_main_t * vm,
577 vlib_node_runtime_t * node,
578 vlib_frame_t * frame)
580 u32 * buffers = vlib_frame_args (frame);
581 uword n_packets = frame->n_vectors;
582 vlib_buffer_free (vm, buffers, n_packets);
583 vlib_frame_free (vm, node, frame);
586 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
591 * @brief Formatter for TAPCLI interface name
593 * @param *s - formatter string
594 * @param *args - va_list
596 * @return *s - formatted string
599 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
601 u32 i = va_arg (*args, u32);
602 u32 show_dev_instance = ~0;
603 tapcli_main_t * tm = &tapcli_main;
605 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
606 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
608 if (show_dev_instance != ~0)
609 i = show_dev_instance;
611 s = format (s, "tap-%d", i);
616 * @brief Modify interface flags for TAPCLI interface
618 * @param *vnm - vnet_main_t
619 * @param *hw - vnet_hw_interface_t
625 static u32 tapcli_flag_change (vnet_main_t * vnm,
626 vnet_hw_interface_t * hw,
629 tapcli_main_t *tm = &tapcli_main;
630 tapcli_interface_t *ti;
632 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
634 if (flags & ETHERNET_INTERFACE_FLAG_MTU)
636 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
637 tm->mtu_bytes = hw->max_packet_bytes;
638 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
645 memcpy (&ifr, &ti->ifr, sizeof (ifr));
647 /* get flags, modify to bring up interface... */
648 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
650 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
654 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
656 if (want_promisc == ti->is_promisc)
659 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
660 ifr.ifr_flags |= IFF_PROMISC;
662 ifr.ifr_flags &= ~(IFF_PROMISC);
664 /* get flags, modify to bring up interface... */
665 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
667 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
671 ti->is_promisc = want_promisc;
678 * @brief Setting the TAP interface's next processing node
680 * @param *vnm - vnet_main_t
681 * @param hw_if_index - u32
682 * @param node_index - u32
685 static void tapcli_set_interface_next_node (vnet_main_t *vnm,
689 tapcli_main_t *tm = &tapcli_main;
690 tapcli_interface_t *ti;
691 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
693 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
695 /** Shut off redirection */
696 if (node_index == ~0)
698 ti->per_interface_next_index = node_index;
702 ti->per_interface_next_index =
703 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
707 * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
709 * @param *vnm - vnet_main_t
710 * @param hw_if_index - u32
713 * @return error - clib_error_t
715 static clib_error_t *
716 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
718 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
720 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
721 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
724 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
726 hw_flags = speed_duplex;
728 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
732 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
734 .tx_function = tapcli_tx,
735 .format_device_name = format_tapcli_interface_name,
736 .rx_redirect_to_node = tapcli_set_interface_next_node,
737 .name_renumber = tap_name_renumber,
738 .admin_up_down_function = tapcli_interface_admin_up_down,
739 .no_flatten_output_chains = 1,
743 * @brief Dump TAP interfaces
745 * @param **out_tapids - tapcli_interface_details_t
750 int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
752 tapcli_main_t * tm = &tapcli_main;
753 tapcli_interface_t * ti;
755 tapcli_interface_details_t * r_tapids = NULL;
756 tapcli_interface_details_t * tapid = NULL;
758 vec_foreach (ti, tm->tapcli_interfaces) {
761 vec_add2(r_tapids, tapid, 1);
762 tapid->sw_if_index = ti->sw_if_index;
763 strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
766 *out_tapids = r_tapids;
772 * @brief Get tap interface from inactive interfaces or create new
774 * @return interface - tapcli_interface_t
777 static tapcli_interface_t *tapcli_get_new_tapif()
779 tapcli_main_t * tm = &tapcli_main;
780 tapcli_interface_t *ti = NULL;
782 int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
783 // if there are any inactive ifaces
784 if (inactive_cnt > 0) {
786 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
787 if (vec_len(tm->tapcli_interfaces) > ti_idx) {
788 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
789 clib_warning("reusing tap interface");
791 // "remove" from inactive list
792 _vec_len(tm->tapcli_inactive_interfaces) -= 1;
795 // ti was not retrieved from inactive ifaces - create new
797 vec_add2 (tm->tapcli_interfaces, ti, 1);
803 * @brief Connect a TAP interface
805 * @param *vm - vlib_main_t
806 * @param *intfc_name - u8
807 * @param *hwaddr_arg - u8
808 * @param *sw_if_index - u32
813 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
816 tapcli_main_t * tm = &tapcli_main;
817 tapcli_interface_t * ti = NULL;
822 clib_error_t * error;
828 return VNET_API_ERROR_FEATURE_DISABLED;
831 flags = IFF_TAP | IFF_NO_PI;
833 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
834 return VNET_API_ERROR_SYSCALL_ERROR_1;
836 memset (&ifr, 0, sizeof (ifr));
837 strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
838 ifr.ifr_flags = flags;
839 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
841 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
845 /* Open a provisioning socket */
846 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
847 htons(ETH_P_ALL))) < 0 )
849 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
853 /* Find the interface index. */
856 struct sockaddr_ll sll;
858 memset (&ifr, 0, sizeof(ifr));
859 strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
860 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
862 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
866 /* Bind the provisioning socket to the interface. */
867 memset(&sll, 0, sizeof(sll));
868 sll.sll_family = AF_PACKET;
869 sll.sll_ifindex = ifr.ifr_ifindex;
870 sll.sll_protocol = htons(ETH_P_ALL);
872 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
874 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
879 /* non-blocking I/O on /dev/tapX */
882 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
884 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
888 ifr.ifr_mtu = tm->mtu_bytes;
889 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
891 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
895 /* get flags, modify to bring up interface... */
896 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
898 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
902 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
904 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
906 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
910 ti = tapcli_get_new_tapif();
911 ti->per_interface_next_index = ~0;
914 clib_memcpy(hwaddr, hwaddr_arg, 6);
917 f64 now = vlib_time_now(vm);
919 rnd = (u32) (now * 1e6);
920 rnd = random_u32 (&rnd);
922 memcpy (hwaddr+2, &rnd, sizeof(rnd));
927 error = ethernet_register_interface
929 tapcli_dev_class.index,
930 ti - tm->tapcli_interfaces /* device instance */,
931 hwaddr /* ethernet address */,
937 clib_error_report (error);
938 rv = VNET_API_ERROR_INVALID_REGISTRATION;
943 unix_file_t template = {0};
944 template.read_function = tapcli_read_ready;
945 template.file_descriptor = dev_net_tun_fd;
946 ti->unix_file_index = unix_file_add (&unix_main, &template);
947 ti->unix_fd = dev_net_tun_fd;
948 ti->provision_fd = dev_tap_fd;
949 clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
953 vnet_hw_interface_t * hw;
954 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
955 hw->min_supported_packet_bytes = TAP_MTU_MIN;
956 hw->max_supported_packet_bytes = TAP_MTU_MAX;
957 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t);
958 ti->sw_if_index = hw->sw_if_index;
960 *sw_if_indexp = hw->sw_if_index;
965 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
966 ti - tm->tapcli_interfaces);
968 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
969 ti - tm->tapcli_interfaces);
974 close (dev_net_tun_fd);
982 * @brief Renumber a TAP interface
984 * @param *vm - vlib_main_t
985 * @param *intfc_name - u8
986 * @param *hwaddr_arg - u8
987 * @param *sw_if_indexp - u32
988 * @param renumber - u8
989 * @param custom_dev_instance - u32
994 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
995 u8 *hwaddr_arg, u32 * sw_if_indexp,
996 u8 renumber, u32 custom_dev_instance)
998 int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
1000 if (!rv && renumber)
1001 vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
1007 * @brief Disconnect TAP CLI interface
1009 * @param *ti - tapcli_interface_t
1014 static int tapcli_tap_disconnect (tapcli_interface_t *ti)
1017 vnet_main_t * vnm = vnet_get_main();
1018 tapcli_main_t * tm = &tapcli_main;
1019 u32 sw_if_index = ti->sw_if_index;
1021 // bring interface down
1022 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1024 if (ti->unix_file_index != ~0) {
1025 unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
1026 ti->unix_file_index = ~0;
1029 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
1030 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
1032 close(ti->provision_fd);
1034 ti->provision_fd = -1;
1040 * @brief Delete TAP interface
1042 * @param *vm - vlib_main_t
1043 * @param sw_if_index - u32
1048 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
1051 tapcli_main_t * tm = &tapcli_main;
1052 tapcli_interface_t *ti;
1055 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
1058 clib_warning ("sw_if_index %d unknown", sw_if_index);
1059 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1061 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1065 tapcli_tap_disconnect(ti);
1066 // add to inactive list
1067 vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
1069 // reset renumbered iface
1070 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
1071 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1073 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
1078 * @brief CLI function to delete TAP interface
1080 * @param *vm - vlib_main_t
1081 * @param *input - unformat_input_t
1082 * @param *cmd - vlib_cli_command_t
1084 * @return error - clib_error_t
1087 static clib_error_t *
1088 tap_delete_command_fn (vlib_main_t * vm,
1089 unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1092 tapcli_main_t * tm = &tapcli_main;
1093 u32 sw_if_index = ~0;
1095 if (tm->is_disabled)
1097 return clib_error_return (0, "device disabled...");
1100 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1104 return clib_error_return (0, "unknown input `%U'",
1105 format_unformat_error, input);
1108 int rc = vnet_tap_delete (vm, sw_if_index);
1111 vlib_cli_output (vm, "Deleted.");
1113 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
1119 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1120 .path = "tap delete",
1121 .short_help = "tap delete <vpp-tap-intfc-name>",
1122 .function = tap_delete_command_fn,
1126 * @brief Modifies tap interface - can result in new interface being created
1128 * @param *vm - vlib_main_t
1129 * @param orig_sw_if_index - u32
1130 * @param *intfc_name - u8
1131 * @param *hwaddr_arg - u8
1132 * @param *sw_if_indexp - u32
1133 * @param renumber - u8
1134 * @param custom_dev_instance - u32
1139 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
1140 u8 * intfc_name, u8 *hwaddr_arg,
1142 u8 renumber, u32 custom_dev_instance)
1144 int rv = vnet_tap_delete (vm, orig_sw_if_index);
1149 rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
1150 renumber, custom_dev_instance);
1156 * @brief CLI function to modify TAP interface
1158 * @param *vm - vlib_main_t
1159 * @param *input - unformat_input_t
1160 * @param *cmd - vlib_cli_command_t
1162 * @return error - clib_error_t
1165 static clib_error_t *
1166 tap_modify_command_fn (vlib_main_t * vm,
1167 unformat_input_t * input,
1168 vlib_cli_command_t * cmd)
1171 tapcli_main_t * tm = &tapcli_main;
1172 u32 sw_if_index = ~0;
1173 u32 new_sw_if_index = ~0;
1174 int user_hwaddr = 0;
1177 if (tm->is_disabled)
1179 return clib_error_return (0, "device disabled...");
1182 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1186 return clib_error_return (0, "unknown input `%U'",
1187 format_unformat_error, input);
1189 if (unformat (input, "%s", &intfc_name))
1192 return clib_error_return (0, "unknown input `%U'",
1193 format_unformat_error, input);
1195 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1200 int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
1201 (user_hwaddr == 1 ? hwaddr : 0),
1202 &new_sw_if_index, 0, 0);
1205 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1206 format_vnet_sw_if_index_name, tm->vnet_main,
1207 new_sw_if_index, intfc_name);
1209 vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
1215 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1216 .path = "tap modify",
1217 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1218 .function = tap_modify_command_fn,
1222 * @brief CLI function to connect TAP interface
1224 * @param *vm - vlib_main_t
1225 * @param *input - unformat_input_t
1226 * @param *cmd - vlib_cli_command_t
1228 * @return error - clib_error_t
1231 static clib_error_t *
1232 tap_connect_command_fn (vlib_main_t * vm,
1233 unformat_input_t * input,
1234 vlib_cli_command_t * cmd)
1237 tapcli_main_t * tm = &tapcli_main;
1242 if (tm->is_disabled)
1244 return clib_error_return (0, "device disabled...");
1247 if (unformat (input, "%s", &intfc_name))
1250 return clib_error_return (0, "unknown input `%U'",
1251 format_unformat_error, input);
1253 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1255 hwaddr_arg = hwaddr;
1257 /* It is here for backward compatibility */
1258 if (unformat(input, "hwaddr random"))
1261 int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
1264 case VNET_API_ERROR_SYSCALL_ERROR_1:
1265 vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1268 case VNET_API_ERROR_SYSCALL_ERROR_2:
1269 vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1272 case VNET_API_ERROR_SYSCALL_ERROR_3:
1273 vlib_cli_output (vm, "Couldn't open provisioning socket");
1276 case VNET_API_ERROR_SYSCALL_ERROR_4:
1277 vlib_cli_output (vm, "Couldn't get if_index");
1280 case VNET_API_ERROR_SYSCALL_ERROR_5:
1281 vlib_cli_output (vm, "Couldn't bind provisioning socket");
1284 case VNET_API_ERROR_SYSCALL_ERROR_6:
1285 vlib_cli_output (0, "Couldn't set device non-blocking flag");
1288 case VNET_API_ERROR_SYSCALL_ERROR_7:
1289 vlib_cli_output (0, "Couldn't set device MTU");
1292 case VNET_API_ERROR_SYSCALL_ERROR_8:
1293 vlib_cli_output (0, "Couldn't get interface flags");
1296 case VNET_API_ERROR_SYSCALL_ERROR_9:
1297 vlib_cli_output (0, "Couldn't set intfc admin state up");
1300 case VNET_API_ERROR_INVALID_REGISTRATION:
1301 vlib_cli_output (0, "Invalid registration");
1304 vlib_cli_output (0, "Unknown error: %d", rv);
1310 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
1314 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1315 .path = "tap connect",
1316 .short_help = "tap connect <intfc-name> [hwaddr <addr>]",
1317 .function = tap_connect_command_fn,
1321 * @brief TAPCLI main init
1323 * @param *vm - vlib_main_t
1325 * @return error - clib_error_t
1329 tapcli_init (vlib_main_t * vm)
1331 tapcli_main_t * tm = &tapcli_main;
1334 tm->vnet_main = vnet_get_main();
1335 tm->unix_main = &unix_main;
1336 tm->mtu_bytes = TAP_MTU_DEFAULT;
1337 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1338 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1340 vec_alloc(tm->rx_buffers, VLIB_FRAME_SIZE);
1341 vec_reset_length(tm->rx_buffers);
1342 vm->os_punt_frame = tapcli_nopunt_frame;
1346 VLIB_INIT_FUNCTION (tapcli_init);