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 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/uio.h> /* for iovec */
26 #include <netinet/in.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_tun.h>
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
34 #include <vnet/ip/ip.h>
36 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/dpdk/dpdk.h>
42 #include <vnet/unix/tapcli.h>
44 static vnet_device_class_t tapcli_dev_class;
45 static vnet_hw_interface_class_t tapcli_interface_class;
47 static void tapcli_nopunt_frame (vlib_main_t * vm,
48 vlib_node_runtime_t * node,
49 vlib_frame_t * frame);
54 u32 sw_if_index; /* for counters */
58 u32 per_interface_next_index;
59 u8 active; /* for delete */
63 /* Vector of iovecs for readv/writev calls. */
64 struct iovec * iovecs;
66 /* Vector of VLIB rx buffers to use. We allocate them in blocks
67 of VLIB_FRAME_SIZE (256). */
70 /* tap device destination MAC address. Required, or Linux drops pkts */
73 /* Interface MTU in bytes and # of default sized buffers. */
74 u32 mtu_bytes, mtu_buffers;
76 /* Vector of tap interfaces */
77 tapcli_interface_t * tapcli_interfaces;
79 /* Vector of deleted tap interfaces */
80 u32 * tapcli_inactive_interfaces;
82 /* Bitmap of tap interfaces with pending reads */
83 uword * pending_read_bitmap;
85 /* Hash table to find tapcli interface given hw_if_index */
86 uword * tapcli_interface_index_by_sw_if_index;
88 /* Hash table to find tapcli interface given unix fd */
89 uword * tapcli_interface_index_by_unix_fd;
91 /* renumbering table */
92 u32 * show_dev_instance_by_real_dev_instance;
94 /* 1 => disable CLI */
98 vlib_main_t * vlib_main;
99 vnet_main_t * vnet_main;
100 unix_main_t * unix_main;
103 static tapcli_main_t tapcli_main;
107 * Output node, writes the buffers comprising the incoming frame
108 * to the tun/tap device, aka hands them to the Linux kernel stack.
112 tapcli_tx (vlib_main_t * vm,
113 vlib_node_runtime_t * node,
114 vlib_frame_t * frame)
116 u32 * buffers = vlib_frame_args (frame);
117 uword n_packets = frame->n_vectors;
118 tapcli_main_t * tm = &tapcli_main;
119 tapcli_interface_t * ti;
122 for (i = 0; i < n_packets; i++)
127 vnet_hw_interface_t * hw;
131 b = vlib_get_buffer (vm, buffers[i]);
133 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
134 if (tx_sw_if_index == (u32)~0)
135 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
137 ASSERT(tx_sw_if_index != (u32)~0);
139 /* Use the sup intfc to finesse vlan subifs */
140 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
141 tx_sw_if_index = hw->sw_if_index;
143 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
147 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
148 /* $$$ leak, but this should never happen... */
152 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
154 /* Re-set iovecs if present. */
156 _vec_len (tm->iovecs) = 0;
158 /* VLIB buffer chain -> Unix iovec(s). */
159 vec_add2 (tm->iovecs, iov, 1);
160 iov->iov_base = b->data + b->current_data;
161 iov->iov_len = l = b->current_length;
163 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
166 b = vlib_get_buffer (vm, b->next_buffer);
168 vec_add2 (tm->iovecs, iov, 1);
170 iov->iov_base = b->data + b->current_data;
171 iov->iov_len = b->current_length;
172 l += b->current_length;
173 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
176 if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
177 clib_unix_warning ("writev");
180 /* interface output path flattens buffer chains */
181 vlib_buffer_free_no_next (vm, buffers, n_packets);
186 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
187 .function = tapcli_tx,
189 .type = VLIB_NODE_TYPE_INTERNAL,
194 TAPCLI_RX_NEXT_IP4_INPUT,
195 TAPCLI_RX_NEXT_IP6_INPUT,
196 TAPCLI_RX_NEXT_ETHERNET_INPUT,
202 tapcli_rx (vlib_main_t * vm,
203 vlib_node_runtime_t * node,
204 vlib_frame_t * frame)
206 tapcli_main_t * tm = &tapcli_main;
210 const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
211 u32 free_list_index = VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX;
213 dpdk_main_t * dm = &dpdk_main;
214 const uword buffer_size = MBUF_SIZE;
215 u32 free_list_index = dm->vlib_buffer_free_list_index;
217 static u32 * ready_interface_indices;
218 tapcli_interface_t * ti;
221 vec_reset_length (ready_interface_indices);
223 clib_bitmap_foreach (i, tm->pending_read_bitmap,
225 vec_add1 (ready_interface_indices, i);
228 if (vec_len (ready_interface_indices) == 0)
231 for (i = 0; i < vec_len(ready_interface_indices); i++)
233 /* Clear the "interrupt" bit */
234 tm->pending_read_bitmap =
235 clib_bitmap_set (tm->pending_read_bitmap,
236 ready_interface_indices[i], 0);
238 ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
240 /* Make sure we have some RX buffers. */
242 uword n_left = vec_len (tm->rx_buffers);
245 if (n_left < VLIB_FRAME_SIZE / 2)
247 if (! tm->rx_buffers)
248 vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
250 n_alloc = vlib_buffer_alloc_from_free_list
251 (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left,
253 _vec_len (tm->rx_buffers) = n_left + n_alloc;
257 /* Allocate RX buffers from end of rx_buffers.
258 Turn them into iovecs to pass to readv. */
260 uword i_rx = vec_len (tm->rx_buffers) - 1;
262 word j, n_bytes_left, n_bytes_in_packet;
264 u8 out_of_dpdk_buffers = 0;
267 /* We need enough buffers left for an MTU sized packet. */
268 if (PREDICT_FALSE(vec_len (tm->rx_buffers) < tm->mtu_buffers))
270 clib_bitmap_set (tm->pending_read_bitmap,
271 ready_interface_indices[i], 1);
272 clib_warning ("buffer allocation failure");
276 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
277 for (j = 0; j < tm->mtu_buffers; j++)
279 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
280 tm->iovecs[j].iov_base = b->data;
281 tm->iovecs[j].iov_len = buffer_size;
285 if (PREDICT_FALSE(out_of_dpdk_buffers == 1))
289 n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
290 n_bytes_in_packet = n_bytes_left;
291 if (n_bytes_left <= 0)
294 clib_unix_warning ("readv %d", n_bytes_left);
298 bi = tm->rx_buffers[i_rx];
301 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
305 b->current_length = n_bytes_left < buffer_size
306 ? n_bytes_left : buffer_size;
308 n_bytes_left -= buffer_size;
310 if (n_bytes_left <= 0)
313 struct rte_mbuf *mb = (struct rte_mbuf *)(b - 1);
314 rte_pktmbuf_data_len (mb) = n_bytes_in_packet;
315 rte_pktmbuf_pkt_len (mb) = n_bytes_in_packet;
321 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
322 b->next_buffer = tm->rx_buffers[i_rx];
324 ASSERT(0); /* $$$$ fixme */
325 /* ((struct rte_pktmbuf *)(b->mb))->next =
326 vlib_get_buffer (vm, tm->rx_buffers[i_rx])->mb; */
330 /* Interface counters for tapcli interface. */
331 vlib_increment_combined_counter
332 (vnet_main.interface_main.combined_sw_if_counters
333 + VNET_INTERFACE_COUNTER_RX,
336 1, n_bytes_in_packet);
338 _vec_len (tm->rx_buffers) = i_rx;
341 b = vlib_get_buffer (vm, bi);
344 * Turn this on if you run into
345 * "bad monkey" contexts, and you want to know exactly
346 * which nodes they've visited... See .../vlib/vlib/buffer.h
348 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b);
352 uword n_trace = vlib_get_trace_count (vm, node);
354 vnet_buffer (b)->sw_if_index[VLIB_RX] = ti->sw_if_index;
355 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
357 b->error = node->errors[0];
360 next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
362 next_index = (ti->per_interface_next_index != ~0) ?
363 ti->per_interface_next_index : next_index;
366 vnet_main_t *vnm = vnet_get_main();
367 vnet_sw_interface_t * si;
368 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
369 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
370 next_index = TAPCLI_RX_NEXT_DROP;
374 vlib_set_next_frame_buffer (vm, node, next_index, bi);
378 vlib_trace_buffer (vm, node, next_index,
379 b, /* follow_chain */ 1);
380 vlib_set_trace_count (vm, node, n_trace - 1);
388 static char * tapcli_rx_error_strings[] = {
392 VLIB_REGISTER_NODE (tapcli_rx_node,static) = {
393 .function = tapcli_rx,
395 .type = VLIB_NODE_TYPE_INPUT,
396 .state = VLIB_NODE_STATE_INTERRUPT,
399 .error_strings = tapcli_rx_error_strings,
401 .n_next_nodes = TAPCLI_RX_N_NEXT,
403 [TAPCLI_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
404 [TAPCLI_RX_NEXT_IP6_INPUT] = "ip6-input",
405 [TAPCLI_RX_NEXT_DROP] = "error-drop",
406 [TAPCLI_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
410 /* Gets called when file descriptor is ready from epoll. */
411 static clib_error_t * tapcli_read_ready (unix_file_t * uf)
413 vlib_main_t * vm = vlib_get_main();
414 tapcli_main_t * tm = &tapcli_main;
417 /* Schedule the rx node */
418 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
420 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
422 /* Mark the specific tap interface ready-to-read */
424 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
427 clib_warning ("fd %d not in hash table", uf->file_descriptor);
432 static clib_error_t *
433 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
435 tapcli_main_t *tm = &tapcli_main;
437 const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
439 const uword buffer_size = MBUF_SIZE;
442 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
444 if (unformat (input, "mtu %d", &tm->mtu_bytes))
446 else if (unformat (input, "disable"))
449 return clib_error_return (0, "unknown input `%U'",
450 format_unformat_error, input);
458 clib_warning ("tapcli disabled: must be superuser");
463 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
468 static int tap_name_renumber (vnet_hw_interface_t * hi,
469 u32 new_dev_instance)
471 tapcli_main_t *tm = &tapcli_main;
473 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
474 hi->dev_instance, ~0);
476 tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
482 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
485 tapcli_nopunt_frame (vlib_main_t * vm,
486 vlib_node_runtime_t * node,
487 vlib_frame_t * frame)
489 u32 * buffers = vlib_frame_args (frame);
490 uword n_packets = frame->n_vectors;
491 vlib_buffer_free (vm, buffers, n_packets);
492 vlib_frame_free (vm, node, frame);
495 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
499 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
501 u32 i = va_arg (*args, u32);
502 u32 show_dev_instance = ~0;
503 tapcli_main_t * tm = &tapcli_main;
505 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
506 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
508 if (show_dev_instance != ~0)
509 i = show_dev_instance;
511 s = format (s, "tap-%d", i);
515 static u32 tapcli_flag_change (vnet_main_t * vnm,
516 vnet_hw_interface_t * hw,
519 tapcli_main_t *tm = &tapcli_main;
520 tapcli_interface_t *ti;
524 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
526 memcpy (&ifr, &ti->ifr, sizeof (ifr));
528 /* get flags, modify to bring up interface... */
529 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
531 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
535 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
537 if (want_promisc == ti->is_promisc)
541 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
542 ifr.ifr_flags |= IFF_PROMISC;
544 ifr.ifr_flags &= ~(IFF_PROMISC);
546 /* get flags, modify to bring up interface... */
547 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
549 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
553 ti->is_promisc = want_promisc;
558 static void tapcli_set_interface_next_node (vnet_main_t *vnm,
562 tapcli_main_t *tm = &tapcli_main;
563 tapcli_interface_t *ti;
564 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
566 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
568 /* Shut off redirection */
569 if (node_index == ~0)
571 ti->per_interface_next_index = node_index;
575 ti->per_interface_next_index =
576 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
580 * Mainly exists to set link_state == admin_state
581 * otherwise, e.g. ip6 neighbor discovery breaks
583 static clib_error_t *
584 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
586 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
588 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
589 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
592 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
594 hw_flags = speed_duplex;
596 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
600 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
602 .tx_function = tapcli_tx,
603 .format_device_name = format_tapcli_interface_name,
604 .rx_redirect_to_node = tapcli_set_interface_next_node,
605 .name_renumber = tap_name_renumber,
606 .admin_up_down_function = tapcli_interface_admin_up_down,
609 int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
611 tapcli_main_t * tm = &tapcli_main;
612 tapcli_interface_t * ti;
614 tapcli_interface_details_t * r_tapids = NULL;
615 tapcli_interface_details_t * tapid = NULL;
617 vec_foreach (ti, tm->tapcli_interfaces) {
620 vec_add2(r_tapids, tapid, 1);
621 tapid->sw_if_index = ti->sw_if_index;
622 strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
625 *out_tapids = r_tapids;
630 /* get tap interface from inactive interfaces or create new */
631 static tapcli_interface_t *tapcli_get_new_tapif()
633 tapcli_main_t * tm = &tapcli_main;
634 tapcli_interface_t *ti = NULL;
636 int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
637 // if there are any inactive ifaces
638 if (inactive_cnt > 0) {
640 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
641 if (vec_len(tm->tapcli_interfaces) > ti_idx) {
642 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
643 clib_warning("reusing tap interface");
645 // "remove" from inactive list
646 _vec_len(tm->tapcli_inactive_interfaces) -= 1;
649 // ti was not retrieved from inactive ifaces - create new
651 vec_add2 (tm->tapcli_interfaces, ti, 1);
656 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
659 tapcli_main_t * tm = &tapcli_main;
660 tapcli_interface_t * ti = NULL;
665 clib_error_t * error;
671 return VNET_API_ERROR_FEATURE_DISABLED;
674 flags = IFF_TAP | IFF_NO_PI;
676 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
677 return VNET_API_ERROR_SYSCALL_ERROR_1;
679 memset (&ifr, 0, sizeof (ifr));
680 strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
681 ifr.ifr_flags = flags;
682 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
684 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
688 /* Open a provisioning socket */
689 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
690 htons(ETH_P_ALL))) < 0 )
692 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
696 /* Find the interface index. */
699 struct sockaddr_ll sll;
701 memset (&ifr, 0, sizeof(ifr));
702 strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
703 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
705 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
709 /* Bind the provisioning socket to the interface. */
710 memset(&sll, 0, sizeof(sll));
711 sll.sll_family = AF_PACKET;
712 sll.sll_ifindex = ifr.ifr_ifindex;
713 sll.sll_protocol = htons(ETH_P_ALL);
715 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
717 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
722 /* non-blocking I/O on /dev/tapX */
725 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
727 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
731 ifr.ifr_mtu = tm->mtu_bytes;
732 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
734 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
738 /* get flags, modify to bring up interface... */
739 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
741 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
745 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
747 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
749 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
753 if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
755 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
759 ti = tapcli_get_new_tapif();
762 memcpy(hwaddr, hwaddr_arg, 6);
764 error = ethernet_register_interface
766 tapcli_dev_class.index,
767 ti - tm->tapcli_interfaces /* device instance */,
768 hwaddr_arg != 0 ? hwaddr :
769 (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
775 clib_error_report (error);
776 rv = VNET_API_ERROR_INVALID_REGISTRATION;
781 unix_file_t template = {0};
782 template.read_function = tapcli_read_ready;
783 template.file_descriptor = dev_net_tun_fd;
784 ti->unix_file_index = unix_file_add (&unix_main, &template);
785 ti->unix_fd = dev_net_tun_fd;
786 ti->provision_fd = dev_tap_fd;
787 memcpy (&ti->ifr, &ifr, sizeof (ifr));
791 vnet_hw_interface_t * hw;
792 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
793 ti->sw_if_index = hw->sw_if_index;
795 *sw_if_indexp = hw->sw_if_index;
800 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
801 ti - tm->tapcli_interfaces);
803 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
804 ti - tm->tapcli_interfaces);
809 close (dev_net_tun_fd);
815 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
816 u8 *hwaddr_arg, u32 * sw_if_indexp,
817 u8 renumber, u32 custom_dev_instance)
819 int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
822 vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
827 static int tapcli_tap_disconnect (tapcli_interface_t *ti)
830 vnet_main_t * vnm = vnet_get_main();
831 tapcli_main_t * tm = &tapcli_main;
832 u32 sw_if_index = ti->sw_if_index;
834 // bring interface down
835 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
837 if (ti->unix_file_index != ~0) {
838 unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
839 ti->unix_file_index = ~0;
842 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
843 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
845 close(ti->provision_fd);
847 ti->provision_fd = -1;
852 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
855 tapcli_main_t * tm = &tapcli_main;
856 tapcli_interface_t *ti;
859 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
862 clib_warning ("sw_if_index %d unknown", sw_if_index);
863 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
865 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
869 tapcli_tap_disconnect(ti);
870 // add to inactive list
871 vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
873 // reset renumbered iface
874 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
875 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
877 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
881 static clib_error_t *
882 tap_delete_command_fn (vlib_main_t * vm,
883 unformat_input_t * input,
884 vlib_cli_command_t * cmd)
886 tapcli_main_t * tm = &tapcli_main;
887 u32 sw_if_index = ~0;
891 return clib_error_return (0, "device disabled...");
894 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
898 return clib_error_return (0, "unknown input `%U'",
899 format_unformat_error, input);
902 int rc = vnet_tap_delete (vm, sw_if_index);
905 vlib_cli_output (vm, "Deleted.");
907 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
913 VLIB_CLI_COMMAND (tap_delete_command, static) = {
914 .path = "tap delete",
915 .short_help = "tap delete <vpp-tap-intfc-name>",
916 .function = tap_delete_command_fn,
919 /* modifies tap interface - can result in new interface being created */
920 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
921 u8 * intfc_name, u8 *hwaddr_arg,
923 u8 renumber, u32 custom_dev_instance)
925 int rv = vnet_tap_delete (vm, orig_sw_if_index);
930 rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
931 renumber, custom_dev_instance);
936 static clib_error_t *
937 tap_modify_command_fn (vlib_main_t * vm,
938 unformat_input_t * input,
939 vlib_cli_command_t * cmd)
942 tapcli_main_t * tm = &tapcli_main;
943 u32 sw_if_index = ~0;
944 u32 new_sw_if_index = ~0;
950 return clib_error_return (0, "device disabled...");
953 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
957 return clib_error_return (0, "unknown input `%U'",
958 format_unformat_error, input);
960 if (unformat (input, "%s", &intfc_name))
963 return clib_error_return (0, "unknown input `%U'",
964 format_unformat_error, input);
966 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
971 int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
972 (user_hwaddr == 1 ? hwaddr : 0),
973 &new_sw_if_index, 0, 0);
976 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
977 format_vnet_sw_if_index_name, tm->vnet_main,
978 new_sw_if_index, intfc_name);
980 vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
986 VLIB_CLI_COMMAND (tap_modify_command, static) = {
987 .path = "tap modify",
988 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr [<addr> | random]]",
989 .function = tap_modify_command_fn,
992 static clib_error_t *
993 tap_connect_command_fn (vlib_main_t * vm,
994 unformat_input_t * input,
995 vlib_cli_command_t * cmd)
998 tapcli_main_t * tm = &tapcli_main;
999 tapcli_interface_t * ti;
1003 int dev_tap_fd = -1;
1004 clib_error_t * error;
1005 int user_hwaddr = 0;
1008 if (tm->is_disabled)
1010 return clib_error_return (0, "device disabled...");
1013 if (unformat (input, "%s", &intfc_name))
1016 return clib_error_return (0, "unknown input `%U'",
1017 format_unformat_error, input);
1019 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1023 flags = IFF_TAP | IFF_NO_PI;
1025 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
1027 vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1031 memset (&ifr, 0, sizeof (ifr));
1032 strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1033 ifr.ifr_flags = flags;
1034 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
1036 vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1040 /* Open a provisioning socket */
1041 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
1042 htons(ETH_P_ALL))) < 0 )
1044 vlib_cli_output (vm, "Couldn't open provisioning socket");
1048 /* Find the interface index. */
1051 struct sockaddr_ll sll;
1053 memset (&ifr, 0, sizeof(ifr));
1054 strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1055 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
1057 vlib_cli_output (vm, "Couldn't get if_index");
1061 /* Bind the provisioning socket to the interface. */
1062 memset(&sll, 0, sizeof(sll));
1063 sll.sll_family = AF_PACKET;
1064 sll.sll_ifindex = ifr.ifr_ifindex;
1065 sll.sll_protocol = htons(ETH_P_ALL);
1067 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
1069 vlib_cli_output (vm, "Couldn't bind provisioning socket");
1074 /* non-blocking I/O on /dev/tapX */
1077 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
1079 vlib_cli_output (0, "Couldn't set device non-blocking flag");
1083 ifr.ifr_mtu = tm->mtu_bytes;
1084 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
1086 vlib_cli_output (0, "Couldn't set device MTU");
1090 /* get flags, modify to bring up interface... */
1091 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
1093 vlib_cli_output (0, "Couldn't get interface flags");
1097 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
1099 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
1101 vlib_cli_output (0, "Couldn't set intfc admin state up");
1105 if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
1107 vlib_cli_output (0, "Couldn't get intfc MAC address");
1111 ti = tapcli_get_new_tapif();
1112 ti->per_interface_next_index = ~0;
1114 if (unformat(input, "hwaddr random"))
1116 f64 now = vlib_time_now(vm);
1118 rnd = (u32) (now * 1e6);
1119 rnd = random_u32 (&rnd);
1121 memcpy (hwaddr+2, &rnd, sizeof(rnd));
1127 error = ethernet_register_interface
1129 tapcli_dev_class.index,
1130 ti - tm->tapcli_interfaces /* device instance */,
1131 user_hwaddr ? hwaddr :
1132 (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
1134 tapcli_flag_change);
1137 clib_error_report (error);
1140 unix_file_t template = {0};
1141 template.read_function = tapcli_read_ready;
1142 template.file_descriptor = dev_net_tun_fd;
1143 ti->unix_file_index = unix_file_add (&unix_main, &template);
1144 ti->unix_fd = dev_net_tun_fd;
1145 ti->provision_fd = dev_tap_fd;
1146 memcpy (&ti->ifr, &ifr, sizeof (ifr));
1150 vnet_hw_interface_t * hw;
1151 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
1152 ti->sw_if_index = hw->sw_if_index;
1157 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1158 ti - tm->tapcli_interfaces);
1160 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1161 ti - tm->tapcli_interfaces);
1163 vlib_cli_output (vm, "Created %U for Linux tap '%s'",
1164 format_vnet_sw_if_index_name, tm->vnet_main,
1165 ti->sw_if_index, intfc_name);
1170 close (dev_net_tun_fd);
1176 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1177 .path = "tap connect",
1178 .short_help = "tap connect <intfc-name> [hwaddr [<addr> | random]]",
1179 .function = tap_connect_command_fn,
1183 tapcli_init (vlib_main_t * vm)
1185 tapcli_main_t * tm = &tapcli_main;
1188 tm->vnet_main = vnet_get_main();
1189 tm->unix_main = &unix_main;
1190 tm->mtu_bytes = 4096 + 256;
1191 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1192 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1193 vm->os_punt_frame = tapcli_nopunt_frame;
1198 VLIB_INIT_FUNCTION (tapcli_init);