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