Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * tapcli.c - dynamic tap interface hookup |
| 4 | * |
| 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: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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 | *------------------------------------------------------------------ |
| 18 | */ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 19 | /** |
| 20 | * @file |
| 21 | * @brief dynamic tap interface hookup |
| 22 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include <fcntl.h> /* for open */ |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/stat.h> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | #include <sys/uio.h> /* for iovec */ |
| 30 | #include <netinet/in.h> |
| 31 | |
| 32 | #include <linux/if_arp.h> |
| 33 | #include <linux/if_tun.h> |
| 34 | |
| 35 | #include <vlib/vlib.h> |
| 36 | #include <vlib/unix/unix.h> |
| 37 | |
| 38 | #include <vnet/ip/ip.h> |
| 39 | |
| 40 | #include <vnet/ethernet/ethernet.h> |
| 41 | |
Dave Barach | 9f6186e | 2016-11-08 12:12:12 -0500 | [diff] [blame] | 42 | #include <vnet/feature/feature.h> |
| 43 | #include <vnet/devices/devices.h> |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 44 | #include <vnet/unix/tuntap.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #include <vnet/unix/tapcli.h> |
| 46 | |
| 47 | static vnet_device_class_t tapcli_dev_class; |
| 48 | static vnet_hw_interface_class_t tapcli_interface_class; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 49 | static vlib_node_registration_t tapcli_rx_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
| 51 | static void tapcli_nopunt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 52 | vlib_node_runtime_t * node, |
| 53 | vlib_frame_t * frame); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @brief Struct for the tapcli interface |
| 56 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | u32 unix_fd; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 60 | u32 clib_file_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | u32 provision_fd; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 62 | /** For counters */ |
| 63 | u32 sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | u32 hw_if_index; |
| 65 | u32 is_promisc; |
| 66 | struct ifreq ifr; |
| 67 | u32 per_interface_next_index; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 68 | /** for delete */ |
| 69 | u8 active; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } tapcli_interface_t; |
| 71 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 72 | /** |
| 73 | * @brief Struct for RX trace |
| 74 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 75 | typedef struct |
| 76 | { |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 77 | u16 sw_if_index; |
| 78 | } tapcli_rx_trace_t; |
| 79 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 80 | /** |
| 81 | * @brief Function to format TAP CLI trace |
| 82 | * |
| 83 | * @param *s - u8 - formatting string |
| 84 | * @param *va - va_list |
| 85 | * |
| 86 | * @return *s - u8 - formatted string |
| 87 | * |
| 88 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 89 | u8 * |
| 90 | format_tapcli_rx_trace (u8 * s, va_list * va) |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 91 | { |
| 92 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 93 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 94 | vnet_main_t *vnm = vnet_get_main (); |
| 95 | tapcli_rx_trace_t *t = va_arg (*va, tapcli_rx_trace_t *); |
| 96 | s = format (s, "%U", format_vnet_sw_if_index_name, vnm, t->sw_if_index); |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 97 | return s; |
| 98 | } |
| 99 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 100 | /** |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 101 | * @brief TAPCLI per thread struct |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 102 | */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 103 | typedef struct |
| 104 | { |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 105 | /** Vector of VLIB rx buffers to use. We allocate them in blocks |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | of VLIB_FRAME_SIZE (256). */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 107 | u32 *rx_buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 109 | /** Vector of iovecs for readv/writev calls. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 110 | struct iovec *iovecs; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 111 | } tapcli_per_thread_t; |
| 112 | |
| 113 | /** |
| 114 | * @brief TAPCLI main state struct |
| 115 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 116 | typedef struct |
| 117 | { |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 118 | /** per thread variables */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 119 | tapcli_per_thread_t *threads; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 120 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 121 | /** tap device destination MAC address. Required, or Linux drops pkts */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | u8 ether_dst_mac[6]; |
| 123 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 124 | /** Interface MTU in bytes and # of default sized buffers. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | u32 mtu_bytes, mtu_buffers; |
| 126 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 127 | /** Vector of tap interfaces */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 128 | tapcli_interface_t *tapcli_interfaces; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 130 | /** Vector of deleted tap interfaces */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 131 | u32 *tapcli_inactive_interfaces; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 133 | /** Bitmap of tap interfaces with pending reads */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 134 | uword *pending_read_bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 136 | /** Hash table to find tapcli interface given hw_if_index */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 137 | uword *tapcli_interface_index_by_sw_if_index; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 138 | |
| 139 | /** Hash table to find tapcli interface given unix fd */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 140 | uword *tapcli_interface_index_by_unix_fd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 142 | /** renumbering table */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 143 | u32 *show_dev_instance_by_real_dev_instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 145 | /** 1 => disable CLI */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | int is_disabled; |
| 147 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 148 | /** convenience - vlib_main_t */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 149 | vlib_main_t *vlib_main; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 150 | /** convenience - vnet_main_t */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 151 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | } tapcli_main_t; |
| 153 | |
| 154 | static tapcli_main_t tapcli_main; |
| 155 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 156 | /** |
| 157 | * @brief tapcli TX node function |
| 158 | * @node tap-cli-tx |
| 159 | * |
| 160 | * Output node, writes the buffers comprising the incoming frame |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | * to the tun/tap device, aka hands them to the Linux kernel stack. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 162 | * |
| 163 | * @param *vm - vlib_main_t |
| 164 | * @param *node - vlib_node_runtime_t |
| 165 | * @param *frame - vlib_frame_t |
| 166 | * |
| 167 | * @return n_packets - uword |
| 168 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | */ |
| 170 | static uword |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 171 | tapcli_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | { |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 173 | u32 *buffers = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | uword n_packets = frame->n_vectors; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 175 | tapcli_main_t *tm = &tapcli_main; |
| 176 | tapcli_interface_t *ti; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | int i; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 178 | u16 thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
| 180 | for (i = 0; i < n_packets; i++) |
| 181 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 182 | struct iovec *iov; |
| 183 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | uword l; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 185 | vnet_hw_interface_t *hw; |
| 186 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | u32 tx_sw_if_index; |
| 188 | |
| 189 | b = vlib_get_buffer (vm, buffers[i]); |
| 190 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 191 | tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX]; |
| 192 | if (tx_sw_if_index == (u32) ~ 0) |
| 193 | tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 194 | |
| 195 | ASSERT (tx_sw_if_index != (u32) ~ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | |
| 197 | /* Use the sup intfc to finesse vlan subifs */ |
| 198 | hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index); |
| 199 | tx_sw_if_index = hw->sw_if_index; |
Ole Troan | 2df2e3d | 2016-03-02 10:01:43 +0100 | [diff] [blame] | 200 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 201 | p = hash_get (tm->tapcli_interface_index_by_sw_if_index, |
| 202 | tx_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | if (p == 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 204 | { |
| 205 | clib_warning ("sw_if_index %d unknown", tx_sw_if_index); |
| 206 | /* $$$ leak, but this should never happen... */ |
| 207 | continue; |
| 208 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 210 | ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
| 212 | /* Re-set iovecs if present. */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 213 | if (tm->threads[thread_index].iovecs) |
| 214 | _vec_len (tm->threads[thread_index].iovecs) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
| 216 | /* VLIB buffer chain -> Unix iovec(s). */ |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 217 | vec_add2 (tm->threads[thread_index].iovecs, iov, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | iov->iov_base = b->data + b->current_data; |
| 219 | iov->iov_len = l = b->current_length; |
| 220 | |
| 221 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 222 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 223 | do |
| 224 | { |
| 225 | b = vlib_get_buffer (vm, b->next_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 227 | vec_add2 (tm->threads[thread_index].iovecs, iov, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 229 | iov->iov_base = b->data + b->current_data; |
| 230 | iov->iov_len = b->current_length; |
| 231 | l += b->current_length; |
| 232 | } |
| 233 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 236 | if (writev (ti->unix_fd, tm->threads[thread_index].iovecs, |
| 237 | vec_len (tm->threads[thread_index].iovecs)) < l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | clib_unix_warning ("writev"); |
| 239 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 240 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 241 | vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 242 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | return n_packets; |
| 244 | } |
| 245 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 246 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | VLIB_REGISTER_NODE (tapcli_tx_node,static) = { |
| 248 | .function = tapcli_tx, |
| 249 | .name = "tapcli-tx", |
| 250 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 251 | .vector_size = 4, |
| 252 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 253 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 255 | /** |
| 256 | * @brief Dispatch tapcli RX node function for node tap_cli_rx |
| 257 | * |
| 258 | * |
| 259 | * @param *vm - vlib_main_t |
| 260 | * @param *node - vlib_node_runtime_t |
| 261 | * @param *ti - tapcli_interface_t |
| 262 | * |
| 263 | * @return n_packets - uword |
| 264 | * |
| 265 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 266 | static uword |
| 267 | tapcli_rx_iface (vlib_main_t * vm, |
| 268 | vlib_node_runtime_t * node, tapcli_interface_t * ti) |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 269 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 270 | tapcli_main_t *tm = &tapcli_main; |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 271 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 272 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 273 | u8 set_trace = 0; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 274 | u16 thread_index = vm->thread_index; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 275 | vnet_main_t *vnm; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 276 | vnet_sw_interface_t *si; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 277 | u8 admin_down; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 278 | u32 next = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 279 | u32 n_left_to_next, next_index; |
| 280 | u32 *to_next; |
| 281 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 282 | vnm = vnet_get_main (); |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 283 | si = vnet_get_sw_interface (vnm, ti->sw_if_index); |
| 284 | admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
| 285 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 286 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 287 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 288 | while (n_left_to_next) |
| 289 | { // Fill at most one vector |
| 290 | vlib_buffer_t *b_first, *b, *prev; |
| 291 | u32 bi_first, bi; |
| 292 | word n_bytes_in_packet; |
| 293 | int j, n_bytes_left; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 294 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 295 | if (PREDICT_FALSE (vec_len (tm->threads[thread_index].rx_buffers) < |
| 296 | tm->mtu_buffers)) |
| 297 | { |
| 298 | uword len = vec_len (tm->threads[thread_index].rx_buffers); |
| 299 | _vec_len (tm->threads[thread_index].rx_buffers) += |
| 300 | vlib_buffer_alloc_from_free_list (vm, |
| 301 | &tm->threads[thread_index]. |
| 302 | rx_buffers[len], |
| 303 | VLIB_FRAME_SIZE - len, |
| 304 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 305 | if (PREDICT_FALSE |
| 306 | (vec_len (tm->threads[thread_index].rx_buffers) < |
| 307 | tm->mtu_buffers)) |
| 308 | { |
| 309 | vlib_node_increment_counter (vm, tapcli_rx_node.index, |
| 310 | TAPCLI_ERROR_BUFFER_ALLOC, |
| 311 | tm->mtu_buffers - |
| 312 | vec_len (tm->threads |
| 313 | [thread_index]. |
| 314 | rx_buffers)); |
| 315 | break; |
| 316 | } |
| 317 | } |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 318 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 319 | uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 320 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 321 | /* Allocate RX buffers from end of rx_buffers. |
| 322 | Turn them into iovecs to pass to readv. */ |
| 323 | vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); |
| 324 | for (j = 0; j < tm->mtu_buffers; j++) |
| 325 | { |
| 326 | b = |
| 327 | vlib_get_buffer (vm, |
| 328 | tm->threads[thread_index].rx_buffers[i_rx - j]); |
| 329 | tm->threads[thread_index].iovecs[j].iov_base = b->data; |
| 330 | tm->threads[thread_index].iovecs[j].iov_len = buffer_size; |
| 331 | } |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 332 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 333 | n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs, |
| 334 | tm->mtu_buffers); |
| 335 | n_bytes_in_packet = n_bytes_left; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 336 | if (n_bytes_left <= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 337 | { |
| 338 | if (errno != EAGAIN) |
| 339 | { |
| 340 | vlib_node_increment_counter (vm, tapcli_rx_node.index, |
| 341 | TAPCLI_ERROR_READ, 1); |
| 342 | } |
| 343 | break; |
| 344 | } |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 345 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 346 | bi_first = tm->threads[thread_index].rx_buffers[i_rx]; |
| 347 | b = b_first = vlib_get_buffer (vm, |
| 348 | tm->threads[thread_index]. |
| 349 | rx_buffers[i_rx]); |
| 350 | prev = NULL; |
| 351 | |
| 352 | while (1) |
| 353 | { |
| 354 | b->current_length = |
| 355 | n_bytes_left < buffer_size ? n_bytes_left : buffer_size; |
| 356 | n_bytes_left -= buffer_size; |
| 357 | |
| 358 | if (prev) |
| 359 | { |
| 360 | prev->next_buffer = bi; |
| 361 | prev->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 362 | } |
| 363 | prev = b; |
| 364 | |
| 365 | /* last segment */ |
| 366 | if (n_bytes_left <= 0) |
| 367 | break; |
| 368 | |
| 369 | i_rx--; |
| 370 | bi = tm->threads[thread_index].rx_buffers[i_rx]; |
| 371 | b = vlib_get_buffer (vm, bi); |
| 372 | } |
| 373 | |
| 374 | _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; |
| 375 | |
| 376 | b_first->total_length_not_including_first_buffer = |
| 377 | (n_bytes_in_packet > |
| 378 | buffer_size) ? n_bytes_in_packet - buffer_size : 0; |
| 379 | b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 380 | |
| 381 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_first); |
| 382 | |
| 383 | vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index; |
| 384 | vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 385 | |
| 386 | b_first->error = node->errors[TAPCLI_ERROR_NONE]; |
| 387 | next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 388 | next_index = (ti->per_interface_next_index != ~0) ? |
| 389 | ti->per_interface_next_index : next_index; |
| 390 | next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index; |
| 391 | |
| 392 | to_next[0] = bi_first; |
| 393 | to_next++; |
| 394 | n_left_to_next--; |
| 395 | |
| 396 | vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, |
| 397 | b_first); |
| 398 | |
| 399 | vlib_validate_buffer_enqueue_x1 (vm, node, next, |
| 400 | to_next, n_left_to_next, |
| 401 | bi_first, next_index); |
| 402 | |
| 403 | /* Interface counters for tapcli interface. */ |
| 404 | if (PREDICT_TRUE (!admin_down)) |
| 405 | { |
| 406 | vlib_increment_combined_counter (vnet_main.interface_main. |
| 407 | combined_sw_if_counters + |
| 408 | VNET_INTERFACE_COUNTER_RX, |
| 409 | thread_index, ti->sw_if_index, 1, |
| 410 | n_bytes_in_packet); |
| 411 | |
| 412 | if (PREDICT_FALSE (n_trace > 0)) |
| 413 | { |
| 414 | vlib_trace_buffer (vm, node, next_index, |
| 415 | b_first, /* follow_chain */ 1); |
| 416 | n_trace--; |
| 417 | set_trace = 1; |
| 418 | tapcli_rx_trace_t *t0 = |
| 419 | vlib_add_trace (vm, node, b_first, sizeof (*t0)); |
| 420 | t0->sw_if_index = si->sw_if_index; |
| 421 | } |
| 422 | } |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 423 | } |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 424 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 425 | if (set_trace) |
| 426 | vlib_set_trace_count (vm, node, n_trace); |
| 427 | return VLIB_FRAME_SIZE - n_left_to_next; |
| 428 | } |
| 429 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 430 | /** |
| 431 | * @brief tapcli RX node function |
| 432 | * @node tap-cli-rx |
| 433 | * |
| 434 | * Input node from the Kernel tun/tap device |
| 435 | * |
| 436 | * @param *vm - vlib_main_t |
| 437 | * @param *node - vlib_node_runtime_t |
| 438 | * @param *frame - vlib_frame_t |
| 439 | * |
| 440 | * @return n_packets - uword |
| 441 | * |
| 442 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | static uword |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 444 | tapcli_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 446 | tapcli_main_t *tm = &tapcli_main; |
| 447 | static u32 *ready_interface_indices; |
| 448 | tapcli_interface_t *ti; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | int i; |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 450 | u32 total_count = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
| 452 | vec_reset_length (ready_interface_indices); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 453 | /* *INDENT-OFF* */ |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 454 | clib_bitmap_foreach (i, tm->pending_read_bitmap, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | ({ |
| 456 | vec_add1 (ready_interface_indices, i); |
| 457 | })); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 458 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | |
| 460 | if (vec_len (ready_interface_indices) == 0) |
Dave Barach | b3ca658 | 2016-06-10 18:31:55 -0400 | [diff] [blame] | 461 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 463 | for (i = 0; i < vec_len (ready_interface_indices); i++) |
| 464 | { |
| 465 | tm->pending_read_bitmap = |
| 466 | clib_bitmap_set (tm->pending_read_bitmap, |
| 467 | ready_interface_indices[i], 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 469 | ti = |
| 470 | vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]); |
| 471 | total_count += tapcli_rx_iface (vm, node, ti); |
| 472 | } |
| 473 | return total_count; //This might return more than 256. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 476 | /** TAPCLI error strings */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 477 | static char *tapcli_rx_error_strings[] = { |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 478 | #define _(sym,string) string, |
| 479 | foreach_tapcli_error |
| 480 | #undef _ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | }; |
| 482 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 483 | /* *INDENT-OFF* */ |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 484 | VLIB_REGISTER_NODE (tapcli_rx_node, static) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | .function = tapcli_rx, |
| 486 | .name = "tapcli-rx", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 487 | .sibling_of = "device-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | .type = VLIB_NODE_TYPE_INPUT, |
| 489 | .state = VLIB_NODE_STATE_INTERRUPT, |
| 490 | .vector_size = 4, |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 491 | .n_errors = TAPCLI_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | .error_strings = tapcli_rx_error_strings, |
Pierre Pfister | 3a8f32b | 2016-03-21 12:21:30 +0000 | [diff] [blame] | 493 | .format_trace = format_tapcli_rx_trace, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 495 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 497 | |
| 498 | /** |
| 499 | * @brief Gets called when file descriptor is ready from epoll. |
| 500 | * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 501 | * @param *uf - clib_file_t |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 502 | * |
| 503 | * @return error - clib_error_t |
| 504 | * |
| 505 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 506 | static clib_error_t * |
| 507 | tapcli_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 509 | vlib_main_t *vm = vlib_get_main (); |
| 510 | tapcli_main_t *tm = &tapcli_main; |
| 511 | uword *p; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 512 | |
| 513 | /** Schedule the rx node */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index); |
| 515 | |
| 516 | p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor); |
| 517 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 518 | /** Mark the specific tap interface ready-to-read */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | if (p) |
| 520 | tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 521 | p[0], 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | else |
| 523 | clib_warning ("fd %d not in hash table", uf->file_descriptor); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 528 | /** |
| 529 | * @brief CLI function for TAPCLI configuration |
| 530 | * |
| 531 | * @param *vm - vlib_main_t |
| 532 | * @param *input - unformat_input_t |
| 533 | * |
| 534 | * @return error - clib_error_t |
| 535 | * |
| 536 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | static clib_error_t * |
| 538 | tapcli_config (vlib_main_t * vm, unformat_input_t * input) |
| 539 | { |
| 540 | tapcli_main_t *tm = &tapcli_main; |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 541 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | |
| 543 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 544 | { |
| 545 | if (unformat (input, "mtu %d", &tm->mtu_bytes)) |
| 546 | ; |
| 547 | else if (unformat (input, "disable")) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 548 | tm->is_disabled = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 550 | return clib_error_return (0, "unknown input `%U'", |
| 551 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (tm->is_disabled) |
| 555 | return 0; |
| 556 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 557 | if (geteuid ()) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | { |
| 559 | clib_warning ("tapcli disabled: must be superuser"); |
| 560 | tm->is_disabled = 1; |
| 561 | return 0; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 562 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | |
| 564 | tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 565 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 569 | /** |
| 570 | * @brief Renumber TAPCLI interface |
| 571 | * |
| 572 | * @param *hi - vnet_hw_interface_t |
| 573 | * @param new_dev_instance - u32 |
| 574 | * |
| 575 | * @return rc - int |
| 576 | * |
| 577 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 578 | static int |
| 579 | tap_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | { |
| 581 | tapcli_main_t *tm = &tapcli_main; |
| 582 | |
| 583 | vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 584 | hi->dev_instance, ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 585 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 586 | tm->show_dev_instance_by_real_dev_instance[hi->dev_instance] = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | new_dev_instance; |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli"); |
| 593 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 594 | /** |
| 595 | * @brief Free "no punt" frame |
| 596 | * |
| 597 | * @param *vm - vlib_main_t |
| 598 | * @param *node - vlib_node_runtime_t |
| 599 | * @param *frame - vlib_frame_t |
| 600 | * |
| 601 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | static void |
| 603 | tapcli_nopunt_frame (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 604 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | { |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 606 | u32 *buffers = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 607 | uword n_packets = frame->n_vectors; |
| 608 | vlib_buffer_free (vm, buffers, n_packets); |
| 609 | vlib_frame_free (vm, node, frame); |
| 610 | } |
| 611 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 612 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = { |
| 614 | .name = "tapcli", |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 615 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 617 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 619 | /** |
| 620 | * @brief Formatter for TAPCLI interface name |
| 621 | * |
| 622 | * @param *s - formatter string |
| 623 | * @param *args - va_list |
| 624 | * |
| 625 | * @return *s - formatted string |
| 626 | * |
| 627 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 628 | static u8 * |
| 629 | format_tapcli_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | { |
| 631 | u32 i = va_arg (*args, u32); |
| 632 | u32 show_dev_instance = ~0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 633 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | |
| 635 | if (i < vec_len (tm->show_dev_instance_by_real_dev_instance)) |
| 636 | show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i]; |
| 637 | |
| 638 | if (show_dev_instance != ~0) |
| 639 | i = show_dev_instance; |
| 640 | |
Damjan Marion | ffffb15 | 2017-12-04 15:14:52 +0100 | [diff] [blame] | 641 | s = format (s, "tapcli-%d", i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | return s; |
| 643 | } |
| 644 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 645 | /** |
| 646 | * @brief Modify interface flags for TAPCLI interface |
| 647 | * |
| 648 | * @param *vnm - vnet_main_t |
| 649 | * @param *hw - vnet_hw_interface_t |
| 650 | * @param flags - u32 |
| 651 | * |
| 652 | * @return rc - u32 |
| 653 | * |
| 654 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 655 | static u32 |
| 656 | tapcli_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | { |
| 658 | tapcli_main_t *tm = &tapcli_main; |
| 659 | tapcli_interface_t *ti; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 661 | ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 662 | |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 663 | if (flags & ETHERNET_INTERFACE_FLAG_MTU) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | { |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 665 | const uword buffer_size = VLIB_BUFFER_DATA_SIZE; |
| 666 | tm->mtu_bytes = hw->max_packet_bytes; |
| 667 | tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 669 | else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | { |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 671 | struct ifreq ifr; |
| 672 | u32 want_promisc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 673 | |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 674 | memcpy (&ifr, &ti->ifr, sizeof (ifr)); |
| 675 | |
| 676 | /* get flags, modify to bring up interface... */ |
| 677 | if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 678 | { |
| 679 | clib_unix_warning ("Couldn't get interface flags for %s", hw->name); |
| 680 | return 0; |
| 681 | } |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 682 | |
| 683 | want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0; |
| 684 | |
| 685 | if (want_promisc == ti->is_promisc) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 686 | return 0; |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 687 | |
| 688 | if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 689 | ifr.ifr_flags |= IFF_PROMISC; |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 690 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 691 | ifr.ifr_flags &= ~(IFF_PROMISC); |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 692 | |
| 693 | /* get flags, modify to bring up interface... */ |
| 694 | if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 695 | { |
| 696 | clib_unix_warning ("Couldn't set interface flags for %s", hw->name); |
| 697 | return 0; |
| 698 | } |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 699 | |
| 700 | ti->is_promisc = want_promisc; |
| 701 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 706 | /** |
| 707 | * @brief Setting the TAP interface's next processing node |
| 708 | * |
| 709 | * @param *vnm - vnet_main_t |
| 710 | * @param hw_if_index - u32 |
| 711 | * @param node_index - u32 |
| 712 | * |
| 713 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 714 | static void |
| 715 | tapcli_set_interface_next_node (vnet_main_t * vnm, |
| 716 | u32 hw_if_index, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | { |
| 718 | tapcli_main_t *tm = &tapcli_main; |
| 719 | tapcli_interface_t *ti; |
| 720 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 721 | |
| 722 | ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 723 | |
| 724 | /** Shut off redirection */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 725 | if (node_index == ~0) |
| 726 | { |
| 727 | ti->per_interface_next_index = node_index; |
| 728 | return; |
| 729 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 730 | |
| 731 | ti->per_interface_next_index = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index); |
| 733 | } |
| 734 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 735 | /** |
| 736 | * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks |
| 737 | * |
| 738 | * @param *vnm - vnet_main_t |
| 739 | * @param hw_if_index - u32 |
| 740 | * @param flags - u32 |
| 741 | * |
| 742 | * @return error - clib_error_t |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | */ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 744 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 746 | { |
| 747 | uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 748 | u32 hw_flags; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 749 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | if (is_admin_up) |
Damjan Marion | 5100aa9 | 2018-11-08 15:30:16 +0100 | [diff] [blame] | 751 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 752 | else |
Damjan Marion | 5100aa9 | 2018-11-08 15:30:16 +0100 | [diff] [blame] | 753 | hw_flags = 0; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 754 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
| 756 | return 0; |
| 757 | } |
| 758 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 759 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | VNET_DEVICE_CLASS (tapcli_dev_class,static) = { |
| 761 | .name = "tapcli", |
| 762 | .tx_function = tapcli_tx, |
| 763 | .format_device_name = format_tapcli_interface_name, |
| 764 | .rx_redirect_to_node = tapcli_set_interface_next_node, |
| 765 | .name_renumber = tap_name_renumber, |
| 766 | .admin_up_down_function = tapcli_interface_admin_up_down, |
| 767 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 768 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 769 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 770 | /** |
| 771 | * @brief Dump TAP interfaces |
| 772 | * |
| 773 | * @param **out_tapids - tapcli_interface_details_t |
| 774 | * |
| 775 | * @return rc - int |
| 776 | * |
| 777 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 778 | int |
| 779 | vnet_tap_dump_ifs (tapcli_interface_details_t ** out_tapids) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 780 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 781 | tapcli_main_t *tm = &tapcli_main; |
| 782 | tapcli_interface_t *ti; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 784 | tapcli_interface_details_t *r_tapids = NULL; |
| 785 | tapcli_interface_details_t *tapid = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 787 | vec_foreach (ti, tm->tapcli_interfaces) |
| 788 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | if (!ti->active) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 790 | continue; |
| 791 | vec_add2 (r_tapids, tapid, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 792 | tapid->sw_if_index = ti->sw_if_index; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 793 | strncpy ((char *) tapid->dev_name, ti->ifr.ifr_name, |
| 794 | sizeof (ti->ifr.ifr_name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | *out_tapids = r_tapids; |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 802 | /** |
| 803 | * @brief Get tap interface from inactive interfaces or create new |
| 804 | * |
| 805 | * @return interface - tapcli_interface_t |
| 806 | * |
| 807 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 808 | static tapcli_interface_t * |
| 809 | tapcli_get_new_tapif () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 811 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 812 | tapcli_interface_t *ti = NULL; |
| 813 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 814 | int inactive_cnt = vec_len (tm->tapcli_inactive_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 815 | // if there are any inactive ifaces |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 816 | if (inactive_cnt > 0) |
| 817 | { |
| 818 | // take last |
| 819 | u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1]; |
| 820 | if (vec_len (tm->tapcli_interfaces) > ti_idx) |
| 821 | { |
| 822 | ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx); |
| 823 | clib_warning ("reusing tap interface"); |
| 824 | } |
| 825 | // "remove" from inactive list |
| 826 | _vec_len (tm->tapcli_inactive_interfaces) -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | |
| 829 | // ti was not retrieved from inactive ifaces - create new |
| 830 | if (!ti) |
| 831 | vec_add2 (tm->tapcli_interfaces, ti, 1); |
| 832 | |
| 833 | return ti; |
| 834 | } |
| 835 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 836 | typedef struct |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 837 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 838 | ip6_address_t addr; |
| 839 | u32 mask_width; |
| 840 | unsigned int ifindex; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 841 | } ip6_ifreq_t; |
| 842 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 843 | /** |
| 844 | * @brief Connect a TAP interface |
| 845 | * |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 846 | * @param vm - vlib_main_t |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 847 | * @param ap - vnet_tap_connect_args_t |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 848 | * |
| 849 | * @return rc - int |
| 850 | * |
| 851 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 852 | int |
| 853 | vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t * ap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 855 | tapcli_main_t *tm = &tapcli_main; |
| 856 | tapcli_interface_t *ti = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | struct ifreq ifr; |
| 858 | int flags; |
| 859 | int dev_net_tun_fd; |
| 860 | int dev_tap_fd = -1; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 861 | clib_error_t *error; |
| 862 | u8 hwaddr[6]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 863 | int rv = 0; |
| 864 | |
| 865 | if (tm->is_disabled) |
| 866 | { |
| 867 | return VNET_API_ERROR_FEATURE_DISABLED; |
| 868 | } |
| 869 | |
| 870 | flags = IFF_TAP | IFF_NO_PI; |
| 871 | |
| 872 | if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0) |
| 873 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 874 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 875 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 876 | strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 877 | ifr.ifr_flags = flags; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 878 | if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 879 | { |
| 880 | rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 881 | goto error; |
| 882 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 883 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 884 | /* Open a provisioning socket */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 885 | if ((dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 886 | { |
| 887 | rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 888 | goto error; |
| 889 | } |
| 890 | |
| 891 | /* Find the interface index. */ |
| 892 | { |
| 893 | struct ifreq ifr; |
| 894 | struct sockaddr_ll sll; |
| 895 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 896 | clib_memset (&ifr, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 897 | strncpy (ifr.ifr_name, (char *) ap->intfc_name, |
| 898 | sizeof (ifr.ifr_name) - 1); |
| 899 | if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 901 | rv = VNET_API_ERROR_SYSCALL_ERROR_4; |
| 902 | goto error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Bind the provisioning socket to the interface. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 906 | clib_memset (&sll, 0, sizeof (sll)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 907 | sll.sll_family = AF_PACKET; |
| 908 | sll.sll_ifindex = ifr.ifr_ifindex; |
| 909 | sll.sll_protocol = htons (ETH_P_ALL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 910 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 911 | if (bind (dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 913 | rv = VNET_API_ERROR_SYSCALL_ERROR_5; |
| 914 | goto error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
| 918 | /* non-blocking I/O on /dev/tapX */ |
| 919 | { |
| 920 | int one = 1; |
| 921 | if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0) |
| 922 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 923 | rv = VNET_API_ERROR_SYSCALL_ERROR_6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | goto error; |
| 925 | } |
| 926 | } |
| 927 | ifr.ifr_mtu = tm->mtu_bytes; |
| 928 | if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0) |
| 929 | { |
| 930 | rv = VNET_API_ERROR_SYSCALL_ERROR_7; |
| 931 | goto error; |
| 932 | } |
| 933 | |
| 934 | /* get flags, modify to bring up interface... */ |
| 935 | if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0) |
| 936 | { |
| 937 | rv = VNET_API_ERROR_SYSCALL_ERROR_8; |
| 938 | goto error; |
| 939 | } |
| 940 | |
| 941 | ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); |
| 942 | |
| 943 | if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0) |
| 944 | { |
| 945 | rv = VNET_API_ERROR_SYSCALL_ERROR_9; |
| 946 | goto error; |
| 947 | } |
| 948 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 949 | if (ap->ip4_address_set) |
| 950 | { |
| 951 | struct sockaddr_in sin; |
| 952 | /* ip4: mask defaults to /24 */ |
| 953 | u32 mask = clib_host_to_net_u32 (0xFFFFFF00); |
| 954 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 955 | clib_memset (&sin, 0, sizeof (sin)); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 956 | sin.sin_family = AF_INET; |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 957 | /* sin.sin_port = 0; */ |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 958 | sin.sin_addr.s_addr = ap->ip4_address->as_u32; |
| 959 | memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin)); |
| 960 | |
| 961 | if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 962 | { |
| 963 | rv = VNET_API_ERROR_SYSCALL_ERROR_10; |
| 964 | goto error; |
| 965 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 966 | |
| 967 | if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 968 | { |
| 969 | mask = ~0; |
| 970 | mask <<= (32 - ap->ip4_mask_width); |
| 971 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 972 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 973 | mask = clib_host_to_net_u32 (mask); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 974 | sin.sin_family = AF_INET; |
| 975 | sin.sin_port = 0; |
| 976 | sin.sin_addr.s_addr = mask; |
| 977 | memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin)); |
| 978 | |
| 979 | if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 980 | { |
| 981 | rv = VNET_API_ERROR_SYSCALL_ERROR_10; |
| 982 | goto error; |
| 983 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | if (ap->ip6_address_set) |
| 987 | { |
| 988 | struct ifreq ifr2; |
| 989 | ip6_ifreq_t ifr6; |
| 990 | int sockfd6; |
| 991 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 992 | sockfd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_IP); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 993 | if (sockfd6 < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 994 | { |
| 995 | rv = VNET_API_ERROR_SYSCALL_ERROR_10; |
| 996 | goto error; |
| 997 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 998 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 999 | clib_memset (&ifr2, 0, sizeof (ifr)); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1000 | strncpy (ifr2.ifr_name, (char *) ap->intfc_name, |
| 1001 | sizeof (ifr2.ifr_name) - 1); |
| 1002 | if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0) |
| 1003 | { |
| 1004 | close (sockfd6); |
| 1005 | rv = VNET_API_ERROR_SYSCALL_ERROR_4; |
| 1006 | goto error; |
| 1007 | } |
| 1008 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1009 | memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t)); |
| 1010 | ifr6.mask_width = ap->ip6_mask_width; |
| 1011 | ifr6.ifindex = ifr2.ifr_ifindex; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1012 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1013 | if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1014 | { |
| 1015 | close (sockfd6); |
| 1016 | clib_unix_warning ("ifr6"); |
| 1017 | rv = VNET_API_ERROR_SYSCALL_ERROR_10; |
| 1018 | goto error; |
| 1019 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1020 | close (sockfd6); |
| 1021 | } |
| 1022 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1023 | ti = tapcli_get_new_tapif (); |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1024 | ti->per_interface_next_index = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1025 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1026 | if (ap->hwaddr_arg != 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1027 | clib_memcpy (hwaddr, ap->hwaddr_arg, 6); |
Mohsin KAZMI | 90d8e2c | 2016-07-18 16:57:40 +0200 | [diff] [blame] | 1028 | else |
| 1029 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1030 | f64 now = vlib_time_now (vm); |
Mohsin KAZMI | 90d8e2c | 2016-07-18 16:57:40 +0200 | [diff] [blame] | 1031 | u32 rnd; |
| 1032 | rnd = (u32) (now * 1e6); |
| 1033 | rnd = random_u32 (&rnd); |
| 1034 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1035 | memcpy (hwaddr + 2, &rnd, sizeof (rnd)); |
Mohsin KAZMI | 90d8e2c | 2016-07-18 16:57:40 +0200 | [diff] [blame] | 1036 | hwaddr[0] = 2; |
| 1037 | hwaddr[1] = 0xfe; |
| 1038 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1039 | |
| 1040 | error = ethernet_register_interface |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1041 | (tm->vnet_main, |
| 1042 | tapcli_dev_class.index, |
| 1043 | ti - tm->tapcli_interfaces /* device instance */ , |
| 1044 | hwaddr /* ethernet address */ , |
| 1045 | &ti->hw_if_index, tapcli_flag_change); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (error) |
| 1048 | { |
| 1049 | clib_error_report (error); |
| 1050 | rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 1051 | goto error; |
| 1052 | } |
| 1053 | |
| 1054 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1055 | clib_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1056 | template.read_function = tapcli_read_ready; |
| 1057 | template.file_descriptor = dev_net_tun_fd; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1058 | ti->clib_file_index = clib_file_add (&file_main, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1059 | ti->unix_fd = dev_net_tun_fd; |
| 1060 | ti->provision_fd = dev_tap_fd; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1061 | clib_memcpy (&ti->ifr, &ifr, sizeof (ifr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1062 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1063 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1064 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1065 | vnet_hw_interface_t *hw; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1066 | hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index); |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 1067 | hw->min_supported_packet_bytes = TAP_MTU_MIN; |
| 1068 | hw->max_supported_packet_bytes = TAP_MTU_MAX; |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 1069 | vnet_sw_interface_set_mtu (tm->vnet_main, hw->sw_if_index, 9000); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1070 | ti->sw_if_index = hw->sw_if_index; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1071 | if (ap->sw_if_indexp) |
| 1072 | *(ap->sw_if_indexp) = hw->sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | ti->active = 1; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1076 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1077 | hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1078 | ti - tm->tapcli_interfaces); |
| 1079 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1080 | hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1081 | ti - tm->tapcli_interfaces); |
| 1082 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1083 | return rv; |
| 1084 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1085 | error: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1086 | close (dev_net_tun_fd); |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 1087 | if (dev_tap_fd >= 0) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1088 | close (dev_tap_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1089 | |
| 1090 | return rv; |
| 1091 | } |
| 1092 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1093 | /** |
| 1094 | * @brief Renumber a TAP interface |
| 1095 | * |
| 1096 | * @param *vm - vlib_main_t |
| 1097 | * @param *intfc_name - u8 |
| 1098 | * @param *hwaddr_arg - u8 |
| 1099 | * @param *sw_if_indexp - u32 |
| 1100 | * @param renumber - u8 |
| 1101 | * @param custom_dev_instance - u32 |
| 1102 | * |
| 1103 | * @return rc - int |
| 1104 | * |
| 1105 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1106 | int |
| 1107 | vnet_tap_connect_renumber (vlib_main_t * vm, vnet_tap_connect_args_t * ap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1108 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1109 | int rv = vnet_tap_connect (vm, ap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1110 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1111 | if (!rv && ap->renumber) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1112 | vnet_interface_name_renumber (*(ap->sw_if_indexp), |
| 1113 | ap->custom_dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1114 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1115 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1118 | /** |
| 1119 | * @brief Disconnect TAP CLI interface |
| 1120 | * |
| 1121 | * @param *ti - tapcli_interface_t |
| 1122 | * |
| 1123 | * @return rc - int |
| 1124 | * |
| 1125 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1126 | static int |
| 1127 | tapcli_tap_disconnect (tapcli_interface_t * ti) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1128 | { |
| 1129 | int rv = 0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1130 | vnet_main_t *vnm = vnet_get_main (); |
| 1131 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1132 | u32 sw_if_index = ti->sw_if_index; |
| 1133 | |
| 1134 | // bring interface down |
| 1135 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0); |
| 1136 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1137 | if (ti->clib_file_index != ~0) |
| 1138 | { |
| 1139 | clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index); |
| 1140 | ti->clib_file_index = ~0; |
| 1141 | } |
Eyal Bari | f298ecf | 2016-09-19 18:47:39 +0300 | [diff] [blame] | 1142 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1143 | close (ti->unix_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1144 | |
| 1145 | hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd); |
| 1146 | hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1147 | close (ti->provision_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1148 | ti->unix_fd = -1; |
| 1149 | ti->provision_fd = -1; |
| 1150 | |
| 1151 | return rv; |
| 1152 | } |
| 1153 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1154 | /** |
| 1155 | * @brief Delete TAP interface |
| 1156 | * |
| 1157 | * @param *vm - vlib_main_t |
| 1158 | * @param sw_if_index - u32 |
| 1159 | * |
| 1160 | * @return rc - int |
| 1161 | * |
| 1162 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1163 | int |
| 1164 | vnet_tap_delete (vlib_main_t * vm, u32 sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | { |
| 1166 | int rv = 0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1167 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1168 | tapcli_interface_t *ti; |
| 1169 | uword *p = NULL; |
| 1170 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1171 | p = hash_get (tm->tapcli_interface_index_by_sw_if_index, sw_if_index); |
| 1172 | if (p == 0) |
| 1173 | { |
| 1174 | clib_warning ("sw_if_index %d unknown", sw_if_index); |
| 1175 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 1176 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1177 | ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]); |
| 1178 | |
| 1179 | // inactive |
| 1180 | ti->active = 0; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1181 | tapcli_tap_disconnect (ti); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1182 | // add to inactive list |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1183 | vec_add1 (tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | |
| 1185 | // reset renumbered iface |
| 1186 | if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance)) |
| 1187 | tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0; |
| 1188 | |
| 1189 | ethernet_delete_interface (tm->vnet_main, ti->hw_if_index); |
| 1190 | return rv; |
| 1191 | } |
| 1192 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1193 | /** |
| 1194 | * @brief CLI function to delete TAP interface |
| 1195 | * |
| 1196 | * @param *vm - vlib_main_t |
| 1197 | * @param *input - unformat_input_t |
| 1198 | * @param *cmd - vlib_cli_command_t |
| 1199 | * |
| 1200 | * @return error - clib_error_t |
| 1201 | * |
| 1202 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1203 | static clib_error_t * |
| 1204 | tap_delete_command_fn (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1205 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1206 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1207 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | u32 sw_if_index = ~0; |
| 1209 | |
| 1210 | if (tm->is_disabled) |
| 1211 | { |
| 1212 | return clib_error_return (0, "device disabled..."); |
| 1213 | } |
| 1214 | |
| 1215 | if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1216 | &sw_if_index)) |
| 1217 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1218 | else |
| 1219 | return clib_error_return (0, "unknown input `%U'", |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1220 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1221 | |
| 1222 | |
| 1223 | int rc = vnet_tap_delete (vm, sw_if_index); |
| 1224 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1225 | if (!rc) |
| 1226 | { |
| 1227 | vlib_cli_output (vm, "Deleted."); |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", |
| 1232 | rc); |
| 1233 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1238 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | VLIB_CLI_COMMAND (tap_delete_command, static) = { |
| 1240 | .path = "tap delete", |
| 1241 | .short_help = "tap delete <vpp-tap-intfc-name>", |
| 1242 | .function = tap_delete_command_fn, |
| 1243 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1244 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1245 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1246 | /** |
| 1247 | * @brief Modifies tap interface - can result in new interface being created |
| 1248 | * |
| 1249 | * @param *vm - vlib_main_t |
| 1250 | * @param orig_sw_if_index - u32 |
| 1251 | * @param *intfc_name - u8 |
| 1252 | * @param *hwaddr_arg - u8 |
| 1253 | * @param *sw_if_indexp - u32 |
| 1254 | * @param renumber - u8 |
| 1255 | * @param custom_dev_instance - u32 |
| 1256 | * |
| 1257 | * @return rc - int |
| 1258 | * |
| 1259 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1260 | int |
| 1261 | vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t * ap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1263 | int rv = vnet_tap_delete (vm, ap->orig_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1264 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1265 | if (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1266 | return rv; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1267 | |
| 1268 | rv = vnet_tap_connect_renumber (vm, ap); |
| 1269 | |
| 1270 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1273 | /** |
| 1274 | * @brief CLI function to modify TAP interface |
| 1275 | * |
| 1276 | * @param *vm - vlib_main_t |
| 1277 | * @param *input - unformat_input_t |
| 1278 | * @param *cmd - vlib_cli_command_t |
| 1279 | * |
| 1280 | * @return error - clib_error_t |
| 1281 | * |
| 1282 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1283 | static clib_error_t * |
| 1284 | tap_modify_command_fn (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1285 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1286 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1287 | u8 *intfc_name; |
| 1288 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1289 | u32 sw_if_index = ~0; |
| 1290 | u32 new_sw_if_index = ~0; |
| 1291 | int user_hwaddr = 0; |
| 1292 | u8 hwaddr[6]; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1293 | vnet_tap_connect_args_t _a, *ap = &_a; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1294 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1295 | if (tm->is_disabled) |
| 1296 | { |
| 1297 | return clib_error_return (0, "device disabled..."); |
| 1298 | } |
| 1299 | |
| 1300 | if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1301 | &sw_if_index)) |
| 1302 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1303 | else |
| 1304 | return clib_error_return (0, "unknown input `%U'", |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1305 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1306 | |
| 1307 | if (unformat (input, "%s", &intfc_name)) |
| 1308 | ; |
| 1309 | else |
| 1310 | return clib_error_return (0, "unknown input `%U'", |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1311 | format_unformat_error, input); |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1312 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1313 | if (unformat (input, "hwaddr %U", unformat_ethernet_address, &hwaddr)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1314 | user_hwaddr = 1; |
| 1315 | |
| 1316 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1317 | clib_memset (ap, 0, sizeof (*ap)); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1318 | ap->orig_sw_if_index = sw_if_index; |
| 1319 | ap->intfc_name = intfc_name; |
| 1320 | ap->sw_if_indexp = &new_sw_if_index; |
| 1321 | if (user_hwaddr) |
| 1322 | ap->hwaddr_arg = hwaddr; |
| 1323 | |
| 1324 | int rc = vnet_tap_modify (vm, ap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1325 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1326 | if (!rc) |
| 1327 | { |
| 1328 | vlib_cli_output (vm, "Modified %U for Linux tap '%s'", |
| 1329 | format_vnet_sw_if_index_name, tm->vnet_main, |
| 1330 | *(ap->sw_if_indexp), ap->intfc_name); |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | vlib_cli_output (vm, |
| 1335 | "Error during modification of tap interface. (rc: %d)", |
| 1336 | rc); |
| 1337 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1342 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1343 | VLIB_CLI_COMMAND (tap_modify_command, static) = { |
| 1344 | .path = "tap modify", |
Mohsin KAZMI | 90d8e2c | 2016-07-18 16:57:40 +0200 | [diff] [blame] | 1345 | .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1346 | .function = tap_modify_command_fn, |
| 1347 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1348 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1349 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1350 | /** |
| 1351 | * @brief CLI function to connect TAP interface |
| 1352 | * |
| 1353 | * @param *vm - vlib_main_t |
| 1354 | * @param *input - unformat_input_t |
| 1355 | * @param *cmd - vlib_cli_command_t |
| 1356 | * |
| 1357 | * @return error - clib_error_t |
| 1358 | * |
| 1359 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1360 | static clib_error_t * |
| 1361 | tap_connect_command_fn (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1362 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1363 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1364 | u8 *intfc_name = 0; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1365 | unformat_input_t _line_input, *line_input = &_line_input; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1366 | vnet_tap_connect_args_t _a, *ap = &_a; |
| 1367 | tapcli_main_t *tm = &tapcli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1368 | u8 hwaddr[6]; |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1369 | u8 *hwaddr_arg = 0; |
| 1370 | u32 sw_if_index; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1371 | ip4_address_t ip4_address; |
| 1372 | int ip4_address_set = 0; |
| 1373 | ip6_address_t ip6_address; |
| 1374 | int ip6_address_set = 0; |
| 1375 | u32 ip4_mask_width = 0; |
| 1376 | u32 ip6_mask_width = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1377 | clib_error_t *error = NULL; |
Ole Troan | 2df2e3d | 2016-03-02 10:01:43 +0100 | [diff] [blame] | 1378 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1379 | if (tm->is_disabled) |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1380 | return clib_error_return (0, "device disabled..."); |
| 1381 | |
| 1382 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1383 | return 0; |
| 1384 | |
| 1385 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1386 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1387 | if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, |
| 1388 | &hwaddr)) |
| 1389 | hwaddr_arg = hwaddr; |
| 1390 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1391 | /* It is here for backward compatibility */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1392 | else if (unformat (line_input, "hwaddr random")) |
| 1393 | ; |
| 1394 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1395 | else if (unformat (line_input, "address %U/%d", |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1396 | unformat_ip4_address, &ip4_address, &ip4_mask_width)) |
| 1397 | ip4_address_set = 1; |
| 1398 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1399 | else if (unformat (line_input, "address %U/%d", |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1400 | unformat_ip6_address, &ip6_address, &ip6_mask_width)) |
| 1401 | ip6_address_set = 1; |
| 1402 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1403 | else if (unformat (line_input, "%s", &intfc_name)) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1404 | ; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1405 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1406 | { |
| 1407 | error = clib_error_return (0, "unknown input `%U'", |
| 1408 | format_unformat_error, line_input); |
| 1409 | goto done; |
| 1410 | } |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1411 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1412 | |
Dave Barach | 18b2816 | 2017-01-20 08:34:15 -0500 | [diff] [blame] | 1413 | if (intfc_name == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1414 | { |
| 1415 | error = clib_error_return (0, "interface name must be specified"); |
| 1416 | goto done; |
| 1417 | } |
Dave Barach | 18b2816 | 2017-01-20 08:34:15 -0500 | [diff] [blame] | 1418 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1419 | clib_memset (ap, 0, sizeof (*ap)); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1420 | |
| 1421 | ap->intfc_name = intfc_name; |
| 1422 | ap->hwaddr_arg = hwaddr_arg; |
| 1423 | if (ip4_address_set) |
| 1424 | { |
| 1425 | ap->ip4_address = &ip4_address; |
| 1426 | ap->ip4_mask_width = ip4_mask_width; |
| 1427 | ap->ip4_address_set = 1; |
| 1428 | } |
| 1429 | if (ip6_address_set) |
| 1430 | { |
| 1431 | ap->ip6_address = &ip6_address; |
| 1432 | ap->ip6_mask_width = ip6_mask_width; |
| 1433 | ap->ip6_address_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1436 | ap->sw_if_indexp = &sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1437 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1438 | int rv = vnet_tap_connect (vm, ap); |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1439 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1440 | switch (rv) |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1441 | { |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1442 | case VNET_API_ERROR_SYSCALL_ERROR_1: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1443 | error = clib_error_return (0, "Couldn't open /dev/net/tun"); |
| 1444 | goto done; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1445 | |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1446 | case VNET_API_ERROR_SYSCALL_ERROR_2: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1447 | error = |
| 1448 | clib_error_return (0, "Error setting flags on '%s'", intfc_name); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1449 | goto done; |
| 1450 | |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1451 | case VNET_API_ERROR_SYSCALL_ERROR_3: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1452 | error = clib_error_return (0, "Couldn't open provisioning socket"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1453 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1454 | |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1455 | case VNET_API_ERROR_SYSCALL_ERROR_4: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1456 | error = clib_error_return (0, "Couldn't get if_index"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1457 | goto done; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1458 | |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1459 | case VNET_API_ERROR_SYSCALL_ERROR_5: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1460 | error = clib_error_return (0, "Couldn't bind provisioning socket"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1461 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1462 | |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1463 | case VNET_API_ERROR_SYSCALL_ERROR_6: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1464 | error = clib_error_return (0, "Couldn't set device non-blocking flag"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1465 | goto done; |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1466 | |
| 1467 | case VNET_API_ERROR_SYSCALL_ERROR_7: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1468 | error = clib_error_return (0, "Couldn't set device MTU"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1469 | goto done; |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1470 | |
| 1471 | case VNET_API_ERROR_SYSCALL_ERROR_8: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1472 | error = clib_error_return (0, "Couldn't get interface flags"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1473 | goto done; |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1474 | |
| 1475 | case VNET_API_ERROR_SYSCALL_ERROR_9: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1476 | error = clib_error_return (0, "Couldn't set intfc admin state up"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1477 | goto done; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1478 | |
| 1479 | case VNET_API_ERROR_SYSCALL_ERROR_10: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1480 | error = clib_error_return (0, "Couldn't set intfc address/mask"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1481 | goto done; |
Ole Troan | ae65f5b | 2016-04-27 14:15:48 +0200 | [diff] [blame] | 1482 | |
| 1483 | case VNET_API_ERROR_INVALID_REGISTRATION: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1484 | error = clib_error_return (0, "Invalid registration"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1485 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1486 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1487 | case 0: |
| 1488 | break; |
| 1489 | |
| 1490 | default: |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1491 | error = clib_error_return (0, "Unknown error: %d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1492 | goto done; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1495 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 1496 | vnet_get_main (), sw_if_index); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 1497 | |
| 1498 | done: |
| 1499 | unformat_free (line_input); |
| 1500 | |
| 1501 | return error; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 1502 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1503 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1504 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1505 | VLIB_CLI_COMMAND (tap_connect_command, static) = { |
| 1506 | .path = "tap connect", |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1507 | .short_help = |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1508 | "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1509 | .function = tap_connect_command_fn, |
| 1510 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1511 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1512 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 1513 | /** |
| 1514 | * @brief TAPCLI main init |
| 1515 | * |
| 1516 | * @param *vm - vlib_main_t |
| 1517 | * |
| 1518 | * @return error - clib_error_t |
| 1519 | * |
| 1520 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1521 | clib_error_t * |
| 1522 | tapcli_init (vlib_main_t * vm) |
| 1523 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1524 | tapcli_main_t *tm = &tapcli_main; |
| 1525 | vlib_thread_main_t *m = vlib_get_thread_main (); |
| 1526 | tapcli_per_thread_t *thread; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1527 | |
| 1528 | tm->vlib_main = vm; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1529 | tm->vnet_main = vnet_get_main (); |
Mohsin Kazmi | f2ba9aa | 2016-04-24 18:53:42 +0200 | [diff] [blame] | 1530 | tm->mtu_bytes = TAP_MTU_DEFAULT; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1531 | tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1532 | tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword)); |
| 1533 | vm->os_punt_frame = tapcli_nopunt_frame; |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 1534 | vec_validate_aligned (tm->threads, m->n_vlib_mains - 1, |
| 1535 | CLIB_CACHE_LINE_BYTES); |
| 1536 | vec_foreach (thread, tm->threads) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1537 | { |
| 1538 | thread->iovecs = 0; |
| 1539 | thread->rx_buffers = 0; |
| 1540 | vec_alloc (thread->rx_buffers, VLIB_FRAME_SIZE); |
| 1541 | vec_reset_length (thread->rx_buffers); |
| 1542 | } |
Steven | 4cd2576 | 2017-10-05 00:12:33 -0700 | [diff] [blame] | 1543 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | VLIB_INIT_FUNCTION (tapcli_init); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 1548 | |
| 1549 | /* |
| 1550 | * fd.io coding-style-patch-verification: ON |
| 1551 | * |
| 1552 | * Local Variables: |
| 1553 | * eval: (c-set-style "gnu") |
| 1554 | * End: |
| 1555 | */ |