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 vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
185 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
186 .function = tapcli_tx,
188 .type = VLIB_NODE_TYPE_INTERNAL,
193 TAPCLI_RX_NEXT_IP4_INPUT,
194 TAPCLI_RX_NEXT_IP6_INPUT,
195 TAPCLI_RX_NEXT_ETHERNET_INPUT,
201 tapcli_rx (vlib_main_t * vm,
202 vlib_node_runtime_t * node,
203 vlib_frame_t * frame)
205 tapcli_main_t * tm = &tapcli_main;
206 vlib_buffer_t *b_first;
208 vlib_buffer_free_list_t *fl;
209 const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
210 static u32 * ready_interface_indices;
211 tapcli_interface_t * ti;
213 word n_bytes_in_packet;
215 vec_reset_length (ready_interface_indices);
217 clib_bitmap_foreach (i, tm->pending_read_bitmap,
219 vec_add1 (ready_interface_indices, i);
222 if (vec_len (ready_interface_indices) == 0)
225 fl = vlib_buffer_get_free_list(vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
227 for (i = 0; i < vec_len(ready_interface_indices); i++)
229 /* Clear the "interrupt" bit */
230 tm->pending_read_bitmap =
231 clib_bitmap_set (tm->pending_read_bitmap,
232 ready_interface_indices[i], 0);
234 ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
236 /* Make sure we have some RX buffers. */
238 uword n_left = vec_len (tm->rx_buffers);
240 if (n_left < VLIB_FRAME_SIZE / 2) {
241 vec_validate(tm->rx_buffers, VLIB_FRAME_SIZE + n_left - 1);
242 n_alloc = vlib_buffer_alloc(vm, &tm->rx_buffers[n_left], VLIB_FRAME_SIZE);
244 _vec_len (tm->rx_buffers) = n_left;
248 /* Allocate RX buffers from end of rx_buffers.
249 Turn them into iovecs to pass to readv. */
251 uword i_rx = vec_len (tm->rx_buffers) - 1;
252 vlib_buffer_t * b, *prev = 0;
253 word j, n_bytes_left;
255 /* We need enough buffers left for an MTU sized packet. */
256 if (PREDICT_FALSE(vec_len (tm->rx_buffers) < tm->mtu_buffers))
258 clib_bitmap_set (tm->pending_read_bitmap,
259 ready_interface_indices[i], 1);
260 clib_warning ("buffer allocation failure");
264 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
265 for (j = 0; j < tm->mtu_buffers; j++)
267 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
268 vlib_buffer_init_for_free_list (b, fl);
269 tm->iovecs[j].iov_base = b->data;
270 tm->iovecs[j].iov_len = buffer_size;
273 n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
274 n_bytes_in_packet = n_bytes_left;
275 if (n_bytes_left <= 0)
278 clib_unix_warning ("readv %d", n_bytes_left);
282 bi = tm->rx_buffers[i_rx];
283 b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
288 vlib_buffer_init_for_free_list(b, fl);
290 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
291 n_bytes_left -= buffer_size;
294 prev->next_buffer = bi;
295 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
300 if (n_bytes_left <= 0) break;
303 bi = tm->rx_buffers[i_rx];
304 b = vlib_get_buffer (vm, bi);
307 /* Interface counters for tapcli interface. */
308 vlib_increment_combined_counter
309 (vnet_main.interface_main.combined_sw_if_counters
310 + VNET_INTERFACE_COUNTER_RX,
313 1, n_bytes_in_packet);
315 _vec_len (tm->rx_buffers) = i_rx;
318 b_first->total_length_not_including_first_buffer = (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
319 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
321 /* Ensure mbufs are updated */
322 vlib_buffer_chain_validate(vm, b_first);
325 * Turn this on if you run into
326 * "bad monkey" contexts, and you want to know exactly
327 * which nodes they've visited... See .../vlib/vlib/buffer.h
329 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
333 uword n_trace = vlib_get_trace_count (vm, node);
335 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
336 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
338 b_first->error = node->errors[0];
341 next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
343 next_index = (ti->per_interface_next_index != ~0) ?
344 ti->per_interface_next_index : next_index;
347 vnet_main_t *vnm = vnet_get_main();
348 vnet_sw_interface_t * si;
349 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
350 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
351 next_index = TAPCLI_RX_NEXT_DROP;
354 vlib_set_next_frame_buffer (vm, node, next_index, bi);
358 vlib_trace_buffer (vm, node, next_index,
359 b_first, /* follow_chain */ 1);
360 vlib_set_trace_count (vm, node, n_trace - 1);
368 static char * tapcli_rx_error_strings[] = {
372 VLIB_REGISTER_NODE (tapcli_rx_node,static) = {
373 .function = tapcli_rx,
375 .type = VLIB_NODE_TYPE_INPUT,
376 .state = VLIB_NODE_STATE_INTERRUPT,
379 .error_strings = tapcli_rx_error_strings,
381 .n_next_nodes = TAPCLI_RX_N_NEXT,
383 [TAPCLI_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
384 [TAPCLI_RX_NEXT_IP6_INPUT] = "ip6-input",
385 [TAPCLI_RX_NEXT_DROP] = "error-drop",
386 [TAPCLI_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
390 /* Gets called when file descriptor is ready from epoll. */
391 static clib_error_t * tapcli_read_ready (unix_file_t * uf)
393 vlib_main_t * vm = vlib_get_main();
394 tapcli_main_t * tm = &tapcli_main;
397 /* Schedule the rx node */
398 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
400 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
402 /* Mark the specific tap interface ready-to-read */
404 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
407 clib_warning ("fd %d not in hash table", uf->file_descriptor);
412 static clib_error_t *
413 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
415 tapcli_main_t *tm = &tapcli_main;
416 const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
418 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
420 if (unformat (input, "mtu %d", &tm->mtu_bytes))
422 else if (unformat (input, "disable"))
425 return clib_error_return (0, "unknown input `%U'",
426 format_unformat_error, input);
434 clib_warning ("tapcli disabled: must be superuser");
439 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
444 static int tap_name_renumber (vnet_hw_interface_t * hi,
445 u32 new_dev_instance)
447 tapcli_main_t *tm = &tapcli_main;
449 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
450 hi->dev_instance, ~0);
452 tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
458 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
461 tapcli_nopunt_frame (vlib_main_t * vm,
462 vlib_node_runtime_t * node,
463 vlib_frame_t * frame)
465 u32 * buffers = vlib_frame_args (frame);
466 uword n_packets = frame->n_vectors;
467 vlib_buffer_free (vm, buffers, n_packets);
468 vlib_frame_free (vm, node, frame);
471 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
475 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
477 u32 i = va_arg (*args, u32);
478 u32 show_dev_instance = ~0;
479 tapcli_main_t * tm = &tapcli_main;
481 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
482 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
484 if (show_dev_instance != ~0)
485 i = show_dev_instance;
487 s = format (s, "tap-%d", i);
491 static u32 tapcli_flag_change (vnet_main_t * vnm,
492 vnet_hw_interface_t * hw,
495 tapcli_main_t *tm = &tapcli_main;
496 tapcli_interface_t *ti;
500 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
502 memcpy (&ifr, &ti->ifr, sizeof (ifr));
504 /* get flags, modify to bring up interface... */
505 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
507 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
511 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
513 if (want_promisc == ti->is_promisc)
517 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
518 ifr.ifr_flags |= IFF_PROMISC;
520 ifr.ifr_flags &= ~(IFF_PROMISC);
522 /* get flags, modify to bring up interface... */
523 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
525 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
529 ti->is_promisc = want_promisc;
534 static void tapcli_set_interface_next_node (vnet_main_t *vnm,
538 tapcli_main_t *tm = &tapcli_main;
539 tapcli_interface_t *ti;
540 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
542 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
544 /* Shut off redirection */
545 if (node_index == ~0)
547 ti->per_interface_next_index = node_index;
551 ti->per_interface_next_index =
552 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
556 * Mainly exists to set link_state == admin_state
557 * otherwise, e.g. ip6 neighbor discovery breaks
559 static clib_error_t *
560 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
562 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
564 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
565 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
568 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
570 hw_flags = speed_duplex;
572 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
576 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
578 .tx_function = tapcli_tx,
579 .format_device_name = format_tapcli_interface_name,
580 .rx_redirect_to_node = tapcli_set_interface_next_node,
581 .name_renumber = tap_name_renumber,
582 .admin_up_down_function = tapcli_interface_admin_up_down,
583 .no_flatten_output_chains = 1,
586 int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
588 tapcli_main_t * tm = &tapcli_main;
589 tapcli_interface_t * ti;
591 tapcli_interface_details_t * r_tapids = NULL;
592 tapcli_interface_details_t * tapid = NULL;
594 vec_foreach (ti, tm->tapcli_interfaces) {
597 vec_add2(r_tapids, tapid, 1);
598 tapid->sw_if_index = ti->sw_if_index;
599 strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
602 *out_tapids = r_tapids;
607 /* get tap interface from inactive interfaces or create new */
608 static tapcli_interface_t *tapcli_get_new_tapif()
610 tapcli_main_t * tm = &tapcli_main;
611 tapcli_interface_t *ti = NULL;
613 int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
614 // if there are any inactive ifaces
615 if (inactive_cnt > 0) {
617 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
618 if (vec_len(tm->tapcli_interfaces) > ti_idx) {
619 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
620 clib_warning("reusing tap interface");
622 // "remove" from inactive list
623 _vec_len(tm->tapcli_inactive_interfaces) -= 1;
626 // ti was not retrieved from inactive ifaces - create new
628 vec_add2 (tm->tapcli_interfaces, ti, 1);
633 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
636 tapcli_main_t * tm = &tapcli_main;
637 tapcli_interface_t * ti = NULL;
642 clib_error_t * error;
648 return VNET_API_ERROR_FEATURE_DISABLED;
651 flags = IFF_TAP | IFF_NO_PI;
653 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
654 return VNET_API_ERROR_SYSCALL_ERROR_1;
656 memset (&ifr, 0, sizeof (ifr));
657 strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
658 ifr.ifr_flags = flags;
659 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
661 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
665 /* Open a provisioning socket */
666 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
667 htons(ETH_P_ALL))) < 0 )
669 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
673 /* Find the interface index. */
676 struct sockaddr_ll sll;
678 memset (&ifr, 0, sizeof(ifr));
679 strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
680 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
682 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
686 /* Bind the provisioning socket to the interface. */
687 memset(&sll, 0, sizeof(sll));
688 sll.sll_family = AF_PACKET;
689 sll.sll_ifindex = ifr.ifr_ifindex;
690 sll.sll_protocol = htons(ETH_P_ALL);
692 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
694 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
699 /* non-blocking I/O on /dev/tapX */
702 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
704 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
708 ifr.ifr_mtu = tm->mtu_bytes;
709 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
711 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
715 /* get flags, modify to bring up interface... */
716 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
718 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
722 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
724 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
726 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
730 if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
732 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
736 ti = tapcli_get_new_tapif();
739 memcpy(hwaddr, hwaddr_arg, 6);
741 error = ethernet_register_interface
743 tapcli_dev_class.index,
744 ti - tm->tapcli_interfaces /* device instance */,
745 hwaddr_arg != 0 ? hwaddr :
746 (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
752 clib_error_report (error);
753 rv = VNET_API_ERROR_INVALID_REGISTRATION;
758 unix_file_t template = {0};
759 template.read_function = tapcli_read_ready;
760 template.file_descriptor = dev_net_tun_fd;
761 ti->unix_file_index = unix_file_add (&unix_main, &template);
762 ti->unix_fd = dev_net_tun_fd;
763 ti->provision_fd = dev_tap_fd;
764 memcpy (&ti->ifr, &ifr, sizeof (ifr));
768 vnet_hw_interface_t * hw;
769 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
770 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = tm->mtu_bytes - sizeof(ethernet_header_t);
771 ti->sw_if_index = hw->sw_if_index;
773 *sw_if_indexp = hw->sw_if_index;
778 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
779 ti - tm->tapcli_interfaces);
781 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
782 ti - tm->tapcli_interfaces);
787 close (dev_net_tun_fd);
793 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
794 u8 *hwaddr_arg, u32 * sw_if_indexp,
795 u8 renumber, u32 custom_dev_instance)
797 int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
800 vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
805 static int tapcli_tap_disconnect (tapcli_interface_t *ti)
808 vnet_main_t * vnm = vnet_get_main();
809 tapcli_main_t * tm = &tapcli_main;
810 u32 sw_if_index = ti->sw_if_index;
812 // bring interface down
813 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
815 if (ti->unix_file_index != ~0) {
816 unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
817 ti->unix_file_index = ~0;
820 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
821 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
823 close(ti->provision_fd);
825 ti->provision_fd = -1;
830 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
833 tapcli_main_t * tm = &tapcli_main;
834 tapcli_interface_t *ti;
837 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
840 clib_warning ("sw_if_index %d unknown", sw_if_index);
841 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
843 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
847 tapcli_tap_disconnect(ti);
848 // add to inactive list
849 vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
851 // reset renumbered iface
852 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
853 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
855 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
859 static clib_error_t *
860 tap_delete_command_fn (vlib_main_t * vm,
861 unformat_input_t * input,
862 vlib_cli_command_t * cmd)
864 tapcli_main_t * tm = &tapcli_main;
865 u32 sw_if_index = ~0;
869 return clib_error_return (0, "device disabled...");
872 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
876 return clib_error_return (0, "unknown input `%U'",
877 format_unformat_error, input);
880 int rc = vnet_tap_delete (vm, sw_if_index);
883 vlib_cli_output (vm, "Deleted.");
885 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
891 VLIB_CLI_COMMAND (tap_delete_command, static) = {
892 .path = "tap delete",
893 .short_help = "tap delete <vpp-tap-intfc-name>",
894 .function = tap_delete_command_fn,
897 /* modifies tap interface - can result in new interface being created */
898 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
899 u8 * intfc_name, u8 *hwaddr_arg,
901 u8 renumber, u32 custom_dev_instance)
903 int rv = vnet_tap_delete (vm, orig_sw_if_index);
908 rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
909 renumber, custom_dev_instance);
914 static clib_error_t *
915 tap_modify_command_fn (vlib_main_t * vm,
916 unformat_input_t * input,
917 vlib_cli_command_t * cmd)
920 tapcli_main_t * tm = &tapcli_main;
921 u32 sw_if_index = ~0;
922 u32 new_sw_if_index = ~0;
928 return clib_error_return (0, "device disabled...");
931 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
935 return clib_error_return (0, "unknown input `%U'",
936 format_unformat_error, input);
938 if (unformat (input, "%s", &intfc_name))
941 return clib_error_return (0, "unknown input `%U'",
942 format_unformat_error, input);
944 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
949 int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
950 (user_hwaddr == 1 ? hwaddr : 0),
951 &new_sw_if_index, 0, 0);
954 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
955 format_vnet_sw_if_index_name, tm->vnet_main,
956 new_sw_if_index, intfc_name);
958 vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
964 VLIB_CLI_COMMAND (tap_modify_command, static) = {
965 .path = "tap modify",
966 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr [<addr> | random]]",
967 .function = tap_modify_command_fn,
970 static clib_error_t *
971 tap_connect_command_fn (vlib_main_t * vm,
972 unformat_input_t * input,
973 vlib_cli_command_t * cmd)
976 tapcli_main_t * tm = &tapcli_main;
977 tapcli_interface_t * ti;
982 clib_error_t * error;
988 return clib_error_return (0, "device disabled...");
991 if (unformat (input, "%s", &intfc_name))
994 return clib_error_return (0, "unknown input `%U'",
995 format_unformat_error, input);
997 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1001 flags = IFF_TAP | IFF_NO_PI;
1003 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
1005 vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1009 memset (&ifr, 0, sizeof (ifr));
1010 strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1011 ifr.ifr_flags = flags;
1012 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
1014 vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1018 /* Open a provisioning socket */
1019 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
1020 htons(ETH_P_ALL))) < 0 )
1022 vlib_cli_output (vm, "Couldn't open provisioning socket");
1026 /* Find the interface index. */
1029 struct sockaddr_ll sll;
1031 memset (&ifr, 0, sizeof(ifr));
1032 strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1033 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
1035 vlib_cli_output (vm, "Couldn't get if_index");
1039 /* Bind the provisioning socket to the interface. */
1040 memset(&sll, 0, sizeof(sll));
1041 sll.sll_family = AF_PACKET;
1042 sll.sll_ifindex = ifr.ifr_ifindex;
1043 sll.sll_protocol = htons(ETH_P_ALL);
1045 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
1047 vlib_cli_output (vm, "Couldn't bind provisioning socket");
1052 /* non-blocking I/O on /dev/tapX */
1055 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
1057 vlib_cli_output (0, "Couldn't set device non-blocking flag");
1061 ifr.ifr_mtu = tm->mtu_bytes;
1062 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
1064 vlib_cli_output (0, "Couldn't set device MTU");
1068 /* get flags, modify to bring up interface... */
1069 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
1071 vlib_cli_output (0, "Couldn't get interface flags");
1075 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
1077 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
1079 vlib_cli_output (0, "Couldn't set intfc admin state up");
1083 if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
1085 vlib_cli_output (0, "Couldn't get intfc MAC address");
1089 ti = tapcli_get_new_tapif();
1090 ti->per_interface_next_index = ~0;
1092 if (unformat(input, "hwaddr random"))
1094 f64 now = vlib_time_now(vm);
1096 rnd = (u32) (now * 1e6);
1097 rnd = random_u32 (&rnd);
1099 memcpy (hwaddr+2, &rnd, sizeof(rnd));
1105 error = ethernet_register_interface
1107 tapcli_dev_class.index,
1108 ti - tm->tapcli_interfaces /* device instance */,
1109 user_hwaddr ? hwaddr :
1110 (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
1112 tapcli_flag_change);
1115 clib_error_report (error);
1118 unix_file_t template = {0};
1119 template.read_function = tapcli_read_ready;
1120 template.file_descriptor = dev_net_tun_fd;
1121 ti->unix_file_index = unix_file_add (&unix_main, &template);
1122 ti->unix_fd = dev_net_tun_fd;
1123 ti->provision_fd = dev_tap_fd;
1124 memcpy (&ti->ifr, &ifr, sizeof (ifr));
1128 vnet_hw_interface_t * hw;
1129 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
1130 ti->sw_if_index = hw->sw_if_index;
1131 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = tm->mtu_bytes - sizeof(ethernet_header_t);
1136 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1137 ti - tm->tapcli_interfaces);
1139 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1140 ti - tm->tapcli_interfaces);
1142 vlib_cli_output (vm, "Created %U for Linux tap '%s'",
1143 format_vnet_sw_if_index_name, tm->vnet_main,
1144 ti->sw_if_index, intfc_name);
1149 close (dev_net_tun_fd);
1155 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1156 .path = "tap connect",
1157 .short_help = "tap connect <intfc-name> [hwaddr [<addr> | random]]",
1158 .function = tap_connect_command_fn,
1162 tapcli_init (vlib_main_t * vm)
1164 tapcli_main_t * tm = &tapcli_main;
1167 tm->vnet_main = vnet_get_main();
1168 tm->unix_main = &unix_main;
1169 tm->mtu_bytes = 4096 + 256;
1170 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1171 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1172 vm->os_punt_frame = tapcli_nopunt_frame;
1177 VLIB_INIT_FUNCTION (tapcli_init);