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