blob: 00f093eb0671df1bba46bf351b76b23b53a91c37 [file] [log] [blame]
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
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)07203af2016-08-25 13:37:37 -070019/**
20 * @file
21 * @brief dynamic tap interface hookup
22 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <fcntl.h> /* for open */
25#include <sys/ioctl.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070028#include <sys/types.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029#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 Barach9f6186e2016-11-08 12:12:12 -050042#include <vnet/feature/feature.h>
43#include <vnet/devices/devices.h>
Dave Barach2feaffc2017-01-14 10:30:50 -050044#include <vnet/unix/tuntap.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#include <vnet/unix/tapcli.h>
46
47static vnet_device_class_t tapcli_dev_class;
48static vnet_hw_interface_class_t tapcli_interface_class;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +000049static vlib_node_registration_t tapcli_rx_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51static void tapcli_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +053052 vlib_node_runtime_t * node,
53 vlib_frame_t * frame);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070054/**
55 * @brief Struct for the tapcli interface
56 */
sharath reddy1b0c9832017-11-29 20:08:11 +053057typedef struct
58{
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 u32 unix_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +020060 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 u32 provision_fd;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070062 /** For counters */
63 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 u32 hw_if_index;
65 u32 is_promisc;
66 struct ifreq ifr;
67 u32 per_interface_next_index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070068 /** for delete */
69 u8 active;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070} tapcli_interface_t;
71
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070072/**
73 * @brief Struct for RX trace
74 */
sharath reddy1b0c9832017-11-29 20:08:11 +053075typedef struct
76{
Pierre Pfister3a8f32b2016-03-21 12:21:30 +000077 u16 sw_if_index;
78} tapcli_rx_trace_t;
79
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070080/**
81 * @brief Function to format TAP CLI trace
82 *
83 * @param *s - u8 - formatting string
84 * @param *va - va_list
85 *
86 * @return *s - u8 - formatted string
87 *
88 */
sharath reddy1b0c9832017-11-29 20:08:11 +053089u8 *
90format_tapcli_rx_trace (u8 * s, va_list * va)
Pierre Pfister3a8f32b2016-03-21 12:21:30 +000091{
92 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
93 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
sharath reddy1b0c9832017-11-29 20:08:11 +053094 vnet_main_t *vnm = vnet_get_main ();
95 tapcli_rx_trace_t *t = va_arg (*va, tapcli_rx_trace_t *);
96 s = format (s, "%U", format_vnet_sw_if_index_name, vnm, t->sw_if_index);
Pierre Pfister3a8f32b2016-03-21 12:21:30 +000097 return s;
98}
99
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700100/**
Steven4cd25762017-10-05 00:12:33 -0700101 * @brief TAPCLI per thread struct
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700102 */
Steven4cd25762017-10-05 00:12:33 -0700103typedef struct
104{
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700105 /** Vector of VLIB rx buffers to use. We allocate them in blocks
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 of VLIB_FRAME_SIZE (256). */
sharath reddy1b0c9832017-11-29 20:08:11 +0530107 u32 *rx_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Steven4cd25762017-10-05 00:12:33 -0700109 /** Vector of iovecs for readv/writev calls. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530110 struct iovec *iovecs;
Steven4cd25762017-10-05 00:12:33 -0700111} tapcli_per_thread_t;
112
113/**
114 * @brief TAPCLI main state struct
115 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530116typedef struct
117{
Steven4cd25762017-10-05 00:12:33 -0700118 /** per thread variables */
sharath reddy1b0c9832017-11-29 20:08:11 +0530119 tapcli_per_thread_t *threads;
Steven4cd25762017-10-05 00:12:33 -0700120
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121 /** tap device destination MAC address. Required, or Linux drops pkts */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 u8 ether_dst_mac[6];
123
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700124 /** Interface MTU in bytes and # of default sized buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 u32 mtu_bytes, mtu_buffers;
126
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700127 /** Vector of tap interfaces */
sharath reddy1b0c9832017-11-29 20:08:11 +0530128 tapcli_interface_t *tapcli_interfaces;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700130 /** Vector of deleted tap interfaces */
sharath reddy1b0c9832017-11-29 20:08:11 +0530131 u32 *tapcli_inactive_interfaces;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700133 /** Bitmap of tap interfaces with pending reads */
sharath reddy1b0c9832017-11-29 20:08:11 +0530134 uword *pending_read_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700136 /** Hash table to find tapcli interface given hw_if_index */
sharath reddy1b0c9832017-11-29 20:08:11 +0530137 uword *tapcli_interface_index_by_sw_if_index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700138
139 /** Hash table to find tapcli interface given unix fd */
sharath reddy1b0c9832017-11-29 20:08:11 +0530140 uword *tapcli_interface_index_by_unix_fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700142 /** renumbering table */
sharath reddy1b0c9832017-11-29 20:08:11 +0530143 u32 *show_dev_instance_by_real_dev_instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700145 /** 1 => disable CLI */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 int is_disabled;
147
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700148 /** convenience - vlib_main_t */
sharath reddy1b0c9832017-11-29 20:08:11 +0530149 vlib_main_t *vlib_main;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700150 /** convenience - vnet_main_t */
sharath reddy1b0c9832017-11-29 20:08:11 +0530151 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152} tapcli_main_t;
153
154static tapcli_main_t tapcli_main;
155
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700156/**
157 * @brief tapcli TX node function
158 * @node tap-cli-tx
159 *
160 * Output node, writes the buffers comprising the incoming frame
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 * to the tun/tap device, aka hands them to the Linux kernel stack.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700162 *
163 * @param *vm - vlib_main_t
164 * @param *node - vlib_node_runtime_t
165 * @param *frame - vlib_frame_t
166 *
167 * @return n_packets - uword
168 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 */
170static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530171tapcli_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172{
sharath reddy1b0c9832017-11-29 20:08:11 +0530173 u32 *buffers = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 uword n_packets = frame->n_vectors;
sharath reddy1b0c9832017-11-29 20:08:11 +0530175 tapcli_main_t *tm = &tapcli_main;
176 tapcli_interface_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 int i;
Damjan Marion067cd622018-07-11 12:47:43 +0200178 u16 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
180 for (i = 0; i < n_packets; i++)
181 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530182 struct iovec *iov;
183 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 uword l;
sharath reddy1b0c9832017-11-29 20:08:11 +0530185 vnet_hw_interface_t *hw;
186 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 u32 tx_sw_if_index;
188
189 b = vlib_get_buffer (vm, buffers[i]);
190
sharath reddy1b0c9832017-11-29 20:08:11 +0530191 tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
192 if (tx_sw_if_index == (u32) ~ 0)
193 tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
194
195 ASSERT (tx_sw_if_index != (u32) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
197 /* Use the sup intfc to finesse vlan subifs */
198 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
199 tx_sw_if_index = hw->sw_if_index;
Ole Troan2df2e3d2016-03-02 10:01:43 +0100200
sharath reddy1b0c9832017-11-29 20:08:11 +0530201 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
202 tx_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 if (p == 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530204 {
205 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
206 /* $$$ leak, but this should never happen... */
207 continue;
208 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530210 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 /* Re-set iovecs if present. */
Steven4cd25762017-10-05 00:12:33 -0700213 if (tm->threads[thread_index].iovecs)
214 _vec_len (tm->threads[thread_index].iovecs) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
216 /* VLIB buffer chain -> Unix iovec(s). */
Steven4cd25762017-10-05 00:12:33 -0700217 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 iov->iov_base = b->data + b->current_data;
219 iov->iov_len = l = b->current_length;
220
221 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
222 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530223 do
224 {
225 b = vlib_get_buffer (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
sharath reddy1b0c9832017-11-29 20:08:11 +0530227 vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
sharath reddy1b0c9832017-11-29 20:08:11 +0530229 iov->iov_base = b->data + b->current_data;
230 iov->iov_len = b->current_length;
231 l += b->current_length;
232 }
233 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 }
235
Steven4cd25762017-10-05 00:12:33 -0700236 if (writev (ti->unix_fd, tm->threads[thread_index].iovecs,
237 vec_len (tm->threads[thread_index].iovecs)) < l)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 clib_unix_warning ("writev");
239 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700240
sharath reddy1b0c9832017-11-29 20:08:11 +0530241 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700242
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 return n_packets;
244}
245
sharath reddy1b0c9832017-11-29 20:08:11 +0530246/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
248 .function = tapcli_tx,
249 .name = "tapcli-tx",
250 .type = VLIB_NODE_TYPE_INTERNAL,
251 .vector_size = 4,
252};
sharath reddy1b0c9832017-11-29 20:08:11 +0530253/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700255/**
256 * @brief Dispatch tapcli RX node function for node tap_cli_rx
257 *
258 *
259 * @param *vm - vlib_main_t
260 * @param *node - vlib_node_runtime_t
261 * @param *ti - tapcli_interface_t
262 *
263 * @return n_packets - uword
264 *
265 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530266static uword
267tapcli_rx_iface (vlib_main_t * vm,
268 vlib_node_runtime_t * node, tapcli_interface_t * ti)
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000269{
sharath reddy1b0c9832017-11-29 20:08:11 +0530270 tapcli_main_t *tm = &tapcli_main;
Damjan Marion19010202016-03-24 17:17:47 +0100271 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000272 u32 n_trace = vlib_get_trace_count (vm, node);
273 u8 set_trace = 0;
Damjan Marion067cd622018-07-11 12:47:43 +0200274 u16 thread_index = vm->thread_index;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000275 vnet_main_t *vnm;
sharath reddy1b0c9832017-11-29 20:08:11 +0530276 vnet_sw_interface_t *si;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000277 u8 admin_down;
Dave Barach635ec3b2018-07-13 20:12:45 -0400278 u32 next = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000279 u32 n_left_to_next, next_index;
280 u32 *to_next;
281
sharath reddy1b0c9832017-11-29 20:08:11 +0530282 vnm = vnet_get_main ();
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000283 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
284 admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
285
sharath reddy1b0c9832017-11-29 20:08:11 +0530286 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000287
sharath reddy1b0c9832017-11-29 20:08:11 +0530288 while (n_left_to_next)
289 { // Fill at most one vector
290 vlib_buffer_t *b_first, *b, *prev;
291 u32 bi_first, bi;
292 word n_bytes_in_packet;
293 int j, n_bytes_left;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000294
sharath reddy1b0c9832017-11-29 20:08:11 +0530295 if (PREDICT_FALSE (vec_len (tm->threads[thread_index].rx_buffers) <
296 tm->mtu_buffers))
297 {
298 uword len = vec_len (tm->threads[thread_index].rx_buffers);
299 _vec_len (tm->threads[thread_index].rx_buffers) +=
300 vlib_buffer_alloc_from_free_list (vm,
301 &tm->threads[thread_index].
302 rx_buffers[len],
303 VLIB_FRAME_SIZE - len,
304 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
305 if (PREDICT_FALSE
306 (vec_len (tm->threads[thread_index].rx_buffers) <
307 tm->mtu_buffers))
308 {
309 vlib_node_increment_counter (vm, tapcli_rx_node.index,
310 TAPCLI_ERROR_BUFFER_ALLOC,
311 tm->mtu_buffers -
312 vec_len (tm->threads
313 [thread_index].
314 rx_buffers));
315 break;
316 }
317 }
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000318
sharath reddy1b0c9832017-11-29 20:08:11 +0530319 uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000320
sharath reddy1b0c9832017-11-29 20:08:11 +0530321 /* Allocate RX buffers from end of rx_buffers.
322 Turn them into iovecs to pass to readv. */
323 vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
324 for (j = 0; j < tm->mtu_buffers; j++)
325 {
326 b =
327 vlib_get_buffer (vm,
328 tm->threads[thread_index].rx_buffers[i_rx - j]);
329 tm->threads[thread_index].iovecs[j].iov_base = b->data;
330 tm->threads[thread_index].iovecs[j].iov_len = buffer_size;
331 }
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000332
sharath reddy1b0c9832017-11-29 20:08:11 +0530333 n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs,
334 tm->mtu_buffers);
335 n_bytes_in_packet = n_bytes_left;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000336 if (n_bytes_left <= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530337 {
338 if (errno != EAGAIN)
339 {
340 vlib_node_increment_counter (vm, tapcli_rx_node.index,
341 TAPCLI_ERROR_READ, 1);
342 }
343 break;
344 }
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000345
sharath reddy1b0c9832017-11-29 20:08:11 +0530346 bi_first = tm->threads[thread_index].rx_buffers[i_rx];
347 b = b_first = vlib_get_buffer (vm,
348 tm->threads[thread_index].
349 rx_buffers[i_rx]);
350 prev = NULL;
351
352 while (1)
353 {
354 b->current_length =
355 n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
356 n_bytes_left -= buffer_size;
357
358 if (prev)
359 {
360 prev->next_buffer = bi;
361 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
362 }
363 prev = b;
364
365 /* last segment */
366 if (n_bytes_left <= 0)
367 break;
368
369 i_rx--;
370 bi = tm->threads[thread_index].rx_buffers[i_rx];
371 b = vlib_get_buffer (vm, bi);
372 }
373
374 _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
375
376 b_first->total_length_not_including_first_buffer =
377 (n_bytes_in_packet >
378 buffer_size) ? n_bytes_in_packet - buffer_size : 0;
379 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
380
381 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_first);
382
383 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
384 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32) ~ 0;
385
386 b_first->error = node->errors[TAPCLI_ERROR_NONE];
387 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
388 next_index = (ti->per_interface_next_index != ~0) ?
389 ti->per_interface_next_index : next_index;
390 next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index;
391
392 to_next[0] = bi_first;
393 to_next++;
394 n_left_to_next--;
395
396 vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index,
397 b_first);
398
399 vlib_validate_buffer_enqueue_x1 (vm, node, next,
400 to_next, n_left_to_next,
401 bi_first, next_index);
402
403 /* Interface counters for tapcli interface. */
404 if (PREDICT_TRUE (!admin_down))
405 {
406 vlib_increment_combined_counter (vnet_main.interface_main.
407 combined_sw_if_counters +
408 VNET_INTERFACE_COUNTER_RX,
409 thread_index, ti->sw_if_index, 1,
410 n_bytes_in_packet);
411
412 if (PREDICT_FALSE (n_trace > 0))
413 {
414 vlib_trace_buffer (vm, node, next_index,
415 b_first, /* follow_chain */ 1);
416 n_trace--;
417 set_trace = 1;
418 tapcli_rx_trace_t *t0 =
419 vlib_add_trace (vm, node, b_first, sizeof (*t0));
420 t0->sw_if_index = si->sw_if_index;
421 }
422 }
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000423 }
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000424 vlib_put_next_frame (vm, node, next, n_left_to_next);
425 if (set_trace)
426 vlib_set_trace_count (vm, node, n_trace);
427 return VLIB_FRAME_SIZE - n_left_to_next;
428}
429
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700430/**
431 * @brief tapcli RX node function
432 * @node tap-cli-rx
433 *
434 * Input node from the Kernel tun/tap device
435 *
436 * @param *vm - vlib_main_t
437 * @param *node - vlib_node_runtime_t
438 * @param *frame - vlib_frame_t
439 *
440 * @return n_packets - uword
441 *
442 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443static uword
sharath reddy1b0c9832017-11-29 20:08:11 +0530444tapcli_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445{
sharath reddy1b0c9832017-11-29 20:08:11 +0530446 tapcli_main_t *tm = &tapcli_main;
447 static u32 *ready_interface_indices;
448 tapcli_interface_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 int i;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000450 u32 total_count = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
452 vec_reset_length (ready_interface_indices);
sharath reddy1b0c9832017-11-29 20:08:11 +0530453 /* *INDENT-OFF* */
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000454 clib_bitmap_foreach (i, tm->pending_read_bitmap,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 ({
456 vec_add1 (ready_interface_indices, i);
457 }));
sharath reddy1b0c9832017-11-29 20:08:11 +0530458 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
460 if (vec_len (ready_interface_indices) == 0)
Dave Barachb3ca6582016-06-10 18:31:55 -0400461 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
sharath reddy1b0c9832017-11-29 20:08:11 +0530463 for (i = 0; i < vec_len (ready_interface_indices); i++)
464 {
465 tm->pending_read_bitmap =
466 clib_bitmap_set (tm->pending_read_bitmap,
467 ready_interface_indices[i], 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
sharath reddy1b0c9832017-11-29 20:08:11 +0530469 ti =
470 vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
471 total_count += tapcli_rx_iface (vm, node, ti);
472 }
473 return total_count; //This might return more than 256.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474}
475
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700476/** TAPCLI error strings */
sharath reddy1b0c9832017-11-29 20:08:11 +0530477static char *tapcli_rx_error_strings[] = {
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000478#define _(sym,string) string,
479 foreach_tapcli_error
480#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481};
482
sharath reddy1b0c9832017-11-29 20:08:11 +0530483/* *INDENT-OFF* */
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000484VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 .function = tapcli_rx,
486 .name = "tapcli-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100487 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 .type = VLIB_NODE_TYPE_INPUT,
489 .state = VLIB_NODE_STATE_INTERRUPT,
490 .vector_size = 4,
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000491 .n_errors = TAPCLI_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 .error_strings = tapcli_rx_error_strings,
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000493 .format_trace = format_tapcli_rx_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494};
sharath reddy1b0c9832017-11-29 20:08:11 +0530495/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700497
498/**
499 * @brief Gets called when file descriptor is ready from epoll.
500 *
Damjan Marion56dd5432017-09-08 19:52:02 +0200501 * @param *uf - clib_file_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700502 *
503 * @return error - clib_error_t
504 *
505 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530506static clib_error_t *
507tapcli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508{
sharath reddy1b0c9832017-11-29 20:08:11 +0530509 vlib_main_t *vm = vlib_get_main ();
510 tapcli_main_t *tm = &tapcli_main;
511 uword *p;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700512
513 /** Schedule the rx node */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
515
516 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
517
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700518 /** Mark the specific tap interface ready-to-read */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 if (p)
520 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
sharath reddy1b0c9832017-11-29 20:08:11 +0530521 p[0], 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 else
523 clib_warning ("fd %d not in hash table", uf->file_descriptor);
524
525 return 0;
526}
527
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700528/**
529 * @brief CLI function for TAPCLI configuration
530 *
531 * @param *vm - vlib_main_t
532 * @param *input - unformat_input_t
533 *
534 * @return error - clib_error_t
535 *
536 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537static clib_error_t *
538tapcli_config (vlib_main_t * vm, unformat_input_t * input)
539{
540 tapcli_main_t *tm = &tapcli_main;
Damjan Marion19010202016-03-24 17:17:47 +0100541 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
543 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
544 {
545 if (unformat (input, "mtu %d", &tm->mtu_bytes))
546 ;
547 else if (unformat (input, "disable"))
sharath reddy1b0c9832017-11-29 20:08:11 +0530548 tm->is_disabled = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530550 return clib_error_return (0, "unknown input `%U'",
551 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 }
553
554 if (tm->is_disabled)
555 return 0;
556
sharath reddy1b0c9832017-11-29 20:08:11 +0530557 if (geteuid ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 {
559 clib_warning ("tapcli disabled: must be superuser");
560 tm->is_disabled = 1;
561 return 0;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700562 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563
564 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700565
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 return 0;
567}
568
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700569/**
570 * @brief Renumber TAPCLI interface
571 *
572 * @param *hi - vnet_hw_interface_t
573 * @param new_dev_instance - u32
574 *
575 * @return rc - int
576 *
577 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530578static int
579tap_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
581 tapcli_main_t *tm = &tapcli_main;
582
583 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
sharath reddy1b0c9832017-11-29 20:08:11 +0530584 hi->dev_instance, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
sharath reddy1b0c9832017-11-29 20:08:11 +0530586 tm->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 new_dev_instance;
588
589 return 0;
590}
591
592VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
593
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700594/**
595 * @brief Free "no punt" frame
596 *
597 * @param *vm - vlib_main_t
598 * @param *node - vlib_node_runtime_t
599 * @param *frame - vlib_frame_t
600 *
601 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602static void
603tapcli_nopunt_frame (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530604 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605{
sharath reddy1b0c9832017-11-29 20:08:11 +0530606 u32 *buffers = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 uword n_packets = frame->n_vectors;
608 vlib_buffer_free (vm, buffers, n_packets);
609 vlib_frame_free (vm, node, frame);
610}
611
sharath reddy1b0c9832017-11-29 20:08:11 +0530612/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
614 .name = "tapcli",
Neale Rannsb80c5362016-10-08 13:03:40 +0100615 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616};
sharath reddy1b0c9832017-11-29 20:08:11 +0530617/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700619/**
620 * @brief Formatter for TAPCLI interface name
621 *
622 * @param *s - formatter string
623 * @param *args - va_list
624 *
625 * @return *s - formatted string
626 *
627 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530628static u8 *
629format_tapcli_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630{
631 u32 i = va_arg (*args, u32);
632 u32 show_dev_instance = ~0;
sharath reddy1b0c9832017-11-29 20:08:11 +0530633 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
635 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
636 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
637
638 if (show_dev_instance != ~0)
639 i = show_dev_instance;
640
Damjan Marionffffb152017-12-04 15:14:52 +0100641 s = format (s, "tapcli-%d", i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642 return s;
643}
644
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700645/**
646 * @brief Modify interface flags for TAPCLI interface
647 *
648 * @param *vnm - vnet_main_t
649 * @param *hw - vnet_hw_interface_t
650 * @param flags - u32
651 *
652 * @return rc - u32
653 *
654 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530655static u32
656tapcli_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
658 tapcli_main_t *tm = &tapcli_main;
659 tapcli_interface_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
sharath reddy1b0c9832017-11-29 20:08:11 +0530661 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200663 if (flags & ETHERNET_INTERFACE_FLAG_MTU)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 {
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200665 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
666 tm->mtu_bytes = hw->max_packet_bytes;
667 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530669 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 {
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200671 struct ifreq ifr;
672 u32 want_promisc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200674 memcpy (&ifr, &ti->ifr, sizeof (ifr));
675
676 /* get flags, modify to bring up interface... */
677 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530678 {
679 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
680 return 0;
681 }
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200682
683 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
684
685 if (want_promisc == ti->is_promisc)
sharath reddy1b0c9832017-11-29 20:08:11 +0530686 return 0;
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200687
688 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
sharath reddy1b0c9832017-11-29 20:08:11 +0530689 ifr.ifr_flags |= IFF_PROMISC;
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200690 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530691 ifr.ifr_flags &= ~(IFF_PROMISC);
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200692
693 /* get flags, modify to bring up interface... */
694 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530695 {
696 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
697 return 0;
698 }
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200699
700 ti->is_promisc = want_promisc;
701 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
703 return 0;
704}
705
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700706/**
707 * @brief Setting the TAP interface's next processing node
708 *
709 * @param *vnm - vnet_main_t
710 * @param hw_if_index - u32
711 * @param node_index - u32
712 *
713 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530714static void
715tapcli_set_interface_next_node (vnet_main_t * vnm,
716 u32 hw_if_index, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717{
718 tapcli_main_t *tm = &tapcli_main;
719 tapcli_interface_t *ti;
720 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
721
722 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700723
724 /** Shut off redirection */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 if (node_index == ~0)
726 {
727 ti->per_interface_next_index = node_index;
728 return;
729 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700730
731 ti->per_interface_next_index =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
733}
734
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700735/**
736 * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
737 *
738 * @param *vnm - vnet_main_t
739 * @param hw_if_index - u32
740 * @param flags - u32
741 *
742 * @return error - clib_error_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 */
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700744static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
746{
747 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
748 u32 hw_flags;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700749 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700751
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 if (is_admin_up)
753 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
754 else
755 hw_flags = speed_duplex;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700756
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
758 return 0;
759}
760
sharath reddy1b0c9832017-11-29 20:08:11 +0530761/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
763 .name = "tapcli",
764 .tx_function = tapcli_tx,
765 .format_device_name = format_tapcli_interface_name,
766 .rx_redirect_to_node = tapcli_set_interface_next_node,
767 .name_renumber = tap_name_renumber,
768 .admin_up_down_function = tapcli_interface_admin_up_down,
769};
sharath reddy1b0c9832017-11-29 20:08:11 +0530770/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700772/**
773 * @brief Dump TAP interfaces
774 *
775 * @param **out_tapids - tapcli_interface_details_t
776 *
777 * @return rc - int
778 *
779 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530780int
781vnet_tap_dump_ifs (tapcli_interface_details_t ** out_tapids)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782{
sharath reddy1b0c9832017-11-29 20:08:11 +0530783 tapcli_main_t *tm = &tapcli_main;
784 tapcli_interface_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
sharath reddy1b0c9832017-11-29 20:08:11 +0530786 tapcli_interface_details_t *r_tapids = NULL;
787 tapcli_interface_details_t *tapid = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
sharath reddy1b0c9832017-11-29 20:08:11 +0530789 vec_foreach (ti, tm->tapcli_interfaces)
790 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 if (!ti->active)
sharath reddy1b0c9832017-11-29 20:08:11 +0530792 continue;
793 vec_add2 (r_tapids, tapid, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794 tapid->sw_if_index = ti->sw_if_index;
sharath reddy1b0c9832017-11-29 20:08:11 +0530795 strncpy ((char *) tapid->dev_name, ti->ifr.ifr_name,
796 sizeof (ti->ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 }
798
799 *out_tapids = r_tapids;
800
801 return 0;
802}
803
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700804/**
805 * @brief Get tap interface from inactive interfaces or create new
806 *
807 * @return interface - tapcli_interface_t
808 *
809 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530810static tapcli_interface_t *
811tapcli_get_new_tapif ()
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812{
sharath reddy1b0c9832017-11-29 20:08:11 +0530813 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 tapcli_interface_t *ti = NULL;
815
sharath reddy1b0c9832017-11-29 20:08:11 +0530816 int inactive_cnt = vec_len (tm->tapcli_inactive_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 // if there are any inactive ifaces
sharath reddy1b0c9832017-11-29 20:08:11 +0530818 if (inactive_cnt > 0)
819 {
820 // take last
821 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
822 if (vec_len (tm->tapcli_interfaces) > ti_idx)
823 {
824 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
825 clib_warning ("reusing tap interface");
826 }
827 // "remove" from inactive list
828 _vec_len (tm->tapcli_inactive_interfaces) -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830
831 // ti was not retrieved from inactive ifaces - create new
832 if (!ti)
833 vec_add2 (tm->tapcli_interfaces, ti, 1);
834
835 return ti;
836}
837
sharath reddy1b0c9832017-11-29 20:08:11 +0530838typedef struct
Dave Barach2feaffc2017-01-14 10:30:50 -0500839{
sharath reddy1b0c9832017-11-29 20:08:11 +0530840 ip6_address_t addr;
841 u32 mask_width;
842 unsigned int ifindex;
Dave Barach2feaffc2017-01-14 10:30:50 -0500843} ip6_ifreq_t;
844
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700845/**
846 * @brief Connect a TAP interface
847 *
Chris Luked4024f52016-09-06 09:32:36 -0400848 * @param vm - vlib_main_t
sharath reddy1b0c9832017-11-29 20:08:11 +0530849 * @param ap - vnet_tap_connect_args_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700850 *
851 * @return rc - int
852 *
853 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530854int
855vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856{
sharath reddy1b0c9832017-11-29 20:08:11 +0530857 tapcli_main_t *tm = &tapcli_main;
858 tapcli_interface_t *ti = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 struct ifreq ifr;
860 int flags;
861 int dev_net_tun_fd;
862 int dev_tap_fd = -1;
sharath reddy1b0c9832017-11-29 20:08:11 +0530863 clib_error_t *error;
864 u8 hwaddr[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 int rv = 0;
866
867 if (tm->is_disabled)
868 {
869 return VNET_API_ERROR_FEATURE_DISABLED;
870 }
871
872 flags = IFF_TAP | IFF_NO_PI;
873
874 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
875 return VNET_API_ERROR_SYSCALL_ERROR_1;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700876
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877 memset (&ifr, 0, sizeof (ifr));
sharath reddy1b0c9832017-11-29 20:08:11 +0530878 strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 ifr.ifr_flags = flags;
sharath reddy1b0c9832017-11-29 20:08:11 +0530880 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881 {
882 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
883 goto error;
884 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530885
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 /* Open a provisioning socket */
sharath reddy1b0c9832017-11-29 20:08:11 +0530887 if ((dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 {
889 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
890 goto error;
891 }
892
893 /* Find the interface index. */
894 {
895 struct ifreq ifr;
896 struct sockaddr_ll sll;
897
sharath reddy1b0c9832017-11-29 20:08:11 +0530898 memset (&ifr, 0, sizeof (ifr));
899 strncpy (ifr.ifr_name, (char *) ap->intfc_name,
900 sizeof (ifr.ifr_name) - 1);
901 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530903 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
904 goto error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 }
906
907 /* Bind the provisioning socket to the interface. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530908 memset (&sll, 0, sizeof (sll));
909 sll.sll_family = AF_PACKET;
910 sll.sll_ifindex = ifr.ifr_ifindex;
911 sll.sll_protocol = htons (ETH_P_ALL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
sharath reddy1b0c9832017-11-29 20:08:11 +0530913 if (bind (dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530915 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
916 goto error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 }
918 }
919
920 /* non-blocking I/O on /dev/tapX */
921 {
922 int one = 1;
923 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
924 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530925 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 goto error;
927 }
928 }
929 ifr.ifr_mtu = tm->mtu_bytes;
930 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
931 {
932 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
933 goto error;
934 }
935
936 /* get flags, modify to bring up interface... */
937 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
938 {
939 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
940 goto error;
941 }
942
943 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
944
945 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
946 {
947 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
948 goto error;
949 }
950
Dave Barach2feaffc2017-01-14 10:30:50 -0500951 if (ap->ip4_address_set)
952 {
953 struct sockaddr_in sin;
954 /* ip4: mask defaults to /24 */
955 u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
956
sharath reddy1b0c9832017-11-29 20:08:11 +0530957 memset (&sin, 0, sizeof (sin));
Dave Barach2feaffc2017-01-14 10:30:50 -0500958 sin.sin_family = AF_INET;
Dave Barach8f544962017-01-18 10:23:22 -0500959 /* sin.sin_port = 0; */
Dave Barach2feaffc2017-01-14 10:30:50 -0500960 sin.sin_addr.s_addr = ap->ip4_address->as_u32;
961 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
962
963 if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530964 {
965 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
966 goto error;
967 }
Dave Barach2feaffc2017-01-14 10:30:50 -0500968
969 if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
sharath reddy1b0c9832017-11-29 20:08:11 +0530970 {
971 mask = ~0;
972 mask <<= (32 - ap->ip4_mask_width);
973 }
Dave Barach2feaffc2017-01-14 10:30:50 -0500974
sharath reddy1b0c9832017-11-29 20:08:11 +0530975 mask = clib_host_to_net_u32 (mask);
Dave Barach2feaffc2017-01-14 10:30:50 -0500976 sin.sin_family = AF_INET;
977 sin.sin_port = 0;
978 sin.sin_addr.s_addr = mask;
979 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
980
981 if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530982 {
983 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
984 goto error;
985 }
Dave Barach2feaffc2017-01-14 10:30:50 -0500986 }
987
988 if (ap->ip6_address_set)
989 {
990 struct ifreq ifr2;
991 ip6_ifreq_t ifr6;
992 int sockfd6;
993
sharath reddy1b0c9832017-11-29 20:08:11 +0530994 sockfd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_IP);
Dave Barach2feaffc2017-01-14 10:30:50 -0500995 if (sockfd6 < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530996 {
997 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
998 goto error;
999 }
Dave Barach2feaffc2017-01-14 10:30:50 -05001000
sharath reddy1b0c9832017-11-29 20:08:11 +05301001 memset (&ifr2, 0, sizeof (ifr));
1002 strncpy (ifr2.ifr_name, (char *) ap->intfc_name,
1003 sizeof (ifr2.ifr_name) - 1);
1004 if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0)
1005 {
1006 close (sockfd6);
1007 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
1008 goto error;
1009 }
1010
Dave Barach2feaffc2017-01-14 10:30:50 -05001011 memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
1012 ifr6.mask_width = ap->ip6_mask_width;
1013 ifr6.ifindex = ifr2.ifr_ifindex;
sharath reddy1b0c9832017-11-29 20:08:11 +05301014
Dave Barach2feaffc2017-01-14 10:30:50 -05001015 if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +05301016 {
1017 close (sockfd6);
1018 clib_unix_warning ("ifr6");
1019 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
1020 goto error;
1021 }
Dave Barach2feaffc2017-01-14 10:30:50 -05001022 close (sockfd6);
1023 }
1024
sharath reddy1b0c9832017-11-29 20:08:11 +05301025 ti = tapcli_get_new_tapif ();
Ole Troanae65f5b2016-04-27 14:15:48 +02001026 ti->per_interface_next_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027
Dave Barach2feaffc2017-01-14 10:30:50 -05001028 if (ap->hwaddr_arg != 0)
sharath reddy1b0c9832017-11-29 20:08:11 +05301029 clib_memcpy (hwaddr, ap->hwaddr_arg, 6);
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +02001030 else
1031 {
sharath reddy1b0c9832017-11-29 20:08:11 +05301032 f64 now = vlib_time_now (vm);
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +02001033 u32 rnd;
1034 rnd = (u32) (now * 1e6);
1035 rnd = random_u32 (&rnd);
1036
sharath reddy1b0c9832017-11-29 20:08:11 +05301037 memcpy (hwaddr + 2, &rnd, sizeof (rnd));
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +02001038 hwaddr[0] = 2;
1039 hwaddr[1] = 0xfe;
1040 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
1042 error = ethernet_register_interface
sharath reddy1b0c9832017-11-29 20:08:11 +05301043 (tm->vnet_main,
1044 tapcli_dev_class.index,
1045 ti - tm->tapcli_interfaces /* device instance */ ,
1046 hwaddr /* ethernet address */ ,
1047 &ti->hw_if_index, tapcli_flag_change);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
1049 if (error)
1050 {
1051 clib_error_report (error);
1052 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1053 goto error;
1054 }
1055
1056 {
sharath reddy1b0c9832017-11-29 20:08:11 +05301057 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 template.read_function = tapcli_read_ready;
1059 template.file_descriptor = dev_net_tun_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +02001060 ti->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 ti->unix_fd = dev_net_tun_fd;
1062 ti->provision_fd = dev_tap_fd;
Damjan Marionf1213b82016-03-13 02:22:06 +01001063 clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 }
sharath reddy1b0c9832017-11-29 20:08:11 +05301065
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066 {
sharath reddy1b0c9832017-11-29 20:08:11 +05301067 vnet_hw_interface_t *hw;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001069 hw->min_supported_packet_bytes = TAP_MTU_MIN;
1070 hw->max_supported_packet_bytes = TAP_MTU_MAX;
Ole Troand7231612018-06-07 10:17:57 +02001071 vnet_sw_interface_set_mtu (tm->vnet_main, hw->sw_if_index, 9000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 ti->sw_if_index = hw->sw_if_index;
Dave Barach2feaffc2017-01-14 10:30:50 -05001073 if (ap->sw_if_indexp)
1074 *(ap->sw_if_indexp) = hw->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 }
1076
1077 ti->active = 1;
sharath reddy1b0c9832017-11-29 20:08:11 +05301078
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
sharath reddy1b0c9832017-11-29 20:08:11 +05301080 ti - tm->tapcli_interfaces);
1081
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
sharath reddy1b0c9832017-11-29 20:08:11 +05301083 ti - tm->tapcli_interfaces);
1084
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085 return rv;
1086
sharath reddy1b0c9832017-11-29 20:08:11 +05301087error:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 close (dev_net_tun_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -04001089 if (dev_tap_fd >= 0)
sharath reddy1b0c9832017-11-29 20:08:11 +05301090 close (dev_tap_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
1092 return rv;
1093}
1094
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001095/**
1096 * @brief Renumber a TAP interface
1097 *
1098 * @param *vm - vlib_main_t
1099 * @param *intfc_name - u8
1100 * @param *hwaddr_arg - u8
1101 * @param *sw_if_indexp - u32
1102 * @param renumber - u8
1103 * @param custom_dev_instance - u32
1104 *
1105 * @return rc - int
1106 *
1107 */
sharath reddy1b0c9832017-11-29 20:08:11 +05301108int
1109vnet_tap_connect_renumber (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110{
sharath reddy1b0c9832017-11-29 20:08:11 +05301111 int rv = vnet_tap_connect (vm, ap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112
Dave Barach2feaffc2017-01-14 10:30:50 -05001113 if (!rv && ap->renumber)
sharath reddy1b0c9832017-11-29 20:08:11 +05301114 vnet_interface_name_renumber (*(ap->sw_if_indexp),
1115 ap->custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
Dave Barach2feaffc2017-01-14 10:30:50 -05001117 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118}
1119
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001120/**
1121 * @brief Disconnect TAP CLI interface
1122 *
1123 * @param *ti - tapcli_interface_t
1124 *
1125 * @return rc - int
1126 *
1127 */
sharath reddy1b0c9832017-11-29 20:08:11 +05301128static int
1129tapcli_tap_disconnect (tapcli_interface_t * ti)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130{
1131 int rv = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +05301132 vnet_main_t *vnm = vnet_get_main ();
1133 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 u32 sw_if_index = ti->sw_if_index;
1135
1136 // bring interface down
1137 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1138
sharath reddy1b0c9832017-11-29 20:08:11 +05301139 if (ti->clib_file_index != ~0)
1140 {
1141 clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index);
1142 ti->clib_file_index = ~0;
1143 }
Eyal Barif298ecf2016-09-19 18:47:39 +03001144 else
sharath reddy1b0c9832017-11-29 20:08:11 +05301145 close (ti->unix_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146
1147 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
1148 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
sharath reddy1b0c9832017-11-29 20:08:11 +05301149 close (ti->provision_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 ti->unix_fd = -1;
1151 ti->provision_fd = -1;
1152
1153 return rv;
1154}
1155
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001156/**
1157 * @brief Delete TAP interface
1158 *
1159 * @param *vm - vlib_main_t
1160 * @param sw_if_index - u32
1161 *
1162 * @return rc - int
1163 *
1164 */
sharath reddy1b0c9832017-11-29 20:08:11 +05301165int
1166vnet_tap_delete (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167{
1168 int rv = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +05301169 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170 tapcli_interface_t *ti;
1171 uword *p = NULL;
1172
sharath reddy1b0c9832017-11-29 20:08:11 +05301173 p = hash_get (tm->tapcli_interface_index_by_sw_if_index, sw_if_index);
1174 if (p == 0)
1175 {
1176 clib_warning ("sw_if_index %d unknown", sw_if_index);
1177 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1178 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1180
1181 // inactive
1182 ti->active = 0;
sharath reddy1b0c9832017-11-29 20:08:11 +05301183 tapcli_tap_disconnect (ti);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 // add to inactive list
sharath reddy1b0c9832017-11-29 20:08:11 +05301185 vec_add1 (tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
1187 // reset renumbered iface
1188 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
1189 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1190
1191 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
1192 return rv;
1193}
1194
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001195/**
1196 * @brief CLI function to delete TAP interface
1197 *
1198 * @param *vm - vlib_main_t
1199 * @param *input - unformat_input_t
1200 * @param *cmd - vlib_cli_command_t
1201 *
1202 * @return error - clib_error_t
1203 *
1204 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205static clib_error_t *
1206tap_delete_command_fn (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +05301207 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208{
sharath reddy1b0c9832017-11-29 20:08:11 +05301209 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210 u32 sw_if_index = ~0;
1211
1212 if (tm->is_disabled)
1213 {
1214 return clib_error_return (0, "device disabled...");
1215 }
1216
1217 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
sharath reddy1b0c9832017-11-29 20:08:11 +05301218 &sw_if_index))
1219 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220 else
1221 return clib_error_return (0, "unknown input `%U'",
sharath reddy1b0c9832017-11-29 20:08:11 +05301222 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
1224
1225 int rc = vnet_tap_delete (vm, sw_if_index);
1226
sharath reddy1b0c9832017-11-29 20:08:11 +05301227 if (!rc)
1228 {
1229 vlib_cli_output (vm, "Deleted.");
1230 }
1231 else
1232 {
1233 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)",
1234 rc);
1235 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236
1237 return 0;
1238}
1239
sharath reddy1b0c9832017-11-29 20:08:11 +05301240/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241VLIB_CLI_COMMAND (tap_delete_command, static) = {
1242 .path = "tap delete",
1243 .short_help = "tap delete <vpp-tap-intfc-name>",
1244 .function = tap_delete_command_fn,
1245};
sharath reddy1b0c9832017-11-29 20:08:11 +05301246/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001248/**
1249 * @brief Modifies tap interface - can result in new interface being created
1250 *
1251 * @param *vm - vlib_main_t
1252 * @param orig_sw_if_index - u32
1253 * @param *intfc_name - u8
1254 * @param *hwaddr_arg - u8
1255 * @param *sw_if_indexp - u32
1256 * @param renumber - u8
1257 * @param custom_dev_instance - u32
1258 *
1259 * @return rc - int
1260 *
1261 */
sharath reddy1b0c9832017-11-29 20:08:11 +05301262int
1263vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t * ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264{
sharath reddy1b0c9832017-11-29 20:08:11 +05301265 int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
sharath reddy1b0c9832017-11-29 20:08:11 +05301267 if (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268 return rv;
sharath reddy1b0c9832017-11-29 20:08:11 +05301269
1270 rv = vnet_tap_connect_renumber (vm, ap);
1271
1272 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273}
1274
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001275/**
1276 * @brief CLI function to modify TAP interface
1277 *
1278 * @param *vm - vlib_main_t
1279 * @param *input - unformat_input_t
1280 * @param *cmd - vlib_cli_command_t
1281 *
1282 * @return error - clib_error_t
1283 *
1284 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285static clib_error_t *
1286tap_modify_command_fn (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +05301287 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001288{
sharath reddy1b0c9832017-11-29 20:08:11 +05301289 u8 *intfc_name;
1290 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291 u32 sw_if_index = ~0;
1292 u32 new_sw_if_index = ~0;
1293 int user_hwaddr = 0;
1294 u8 hwaddr[6];
sharath reddy1b0c9832017-11-29 20:08:11 +05301295 vnet_tap_connect_args_t _a, *ap = &_a;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001296
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297 if (tm->is_disabled)
1298 {
1299 return clib_error_return (0, "device disabled...");
1300 }
1301
1302 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
sharath reddy1b0c9832017-11-29 20:08:11 +05301303 &sw_if_index))
1304 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305 else
1306 return clib_error_return (0, "unknown input `%U'",
sharath reddy1b0c9832017-11-29 20:08:11 +05301307 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308
1309 if (unformat (input, "%s", &intfc_name))
1310 ;
1311 else
1312 return clib_error_return (0, "unknown input `%U'",
sharath reddy1b0c9832017-11-29 20:08:11 +05301313 format_unformat_error, input);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001314
sharath reddy1b0c9832017-11-29 20:08:11 +05301315 if (unformat (input, "hwaddr %U", unformat_ethernet_address, &hwaddr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 user_hwaddr = 1;
1317
1318
sharath reddy1b0c9832017-11-29 20:08:11 +05301319 memset (ap, 0, sizeof (*ap));
Dave Barach2feaffc2017-01-14 10:30:50 -05001320 ap->orig_sw_if_index = sw_if_index;
1321 ap->intfc_name = intfc_name;
1322 ap->sw_if_indexp = &new_sw_if_index;
1323 if (user_hwaddr)
1324 ap->hwaddr_arg = hwaddr;
1325
1326 int rc = vnet_tap_modify (vm, ap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327
sharath reddy1b0c9832017-11-29 20:08:11 +05301328 if (!rc)
1329 {
1330 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1331 format_vnet_sw_if_index_name, tm->vnet_main,
1332 *(ap->sw_if_indexp), ap->intfc_name);
1333 }
1334 else
1335 {
1336 vlib_cli_output (vm,
1337 "Error during modification of tap interface. (rc: %d)",
1338 rc);
1339 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340
1341 return 0;
1342}
1343
sharath reddy1b0c9832017-11-29 20:08:11 +05301344/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345VLIB_CLI_COMMAND (tap_modify_command, static) = {
1346 .path = "tap modify",
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +02001347 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001348 .function = tap_modify_command_fn,
1349};
sharath reddy1b0c9832017-11-29 20:08:11 +05301350/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001352/**
1353 * @brief CLI function to connect TAP interface
1354 *
1355 * @param *vm - vlib_main_t
1356 * @param *input - unformat_input_t
1357 * @param *cmd - vlib_cli_command_t
1358 *
1359 * @return error - clib_error_t
1360 *
1361 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001362static clib_error_t *
1363tap_connect_command_fn (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +05301364 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365{
sharath reddy1b0c9832017-11-29 20:08:11 +05301366 u8 *intfc_name = 0;
Dave Barach2feaffc2017-01-14 10:30:50 -05001367 unformat_input_t _line_input, *line_input = &_line_input;
sharath reddy1b0c9832017-11-29 20:08:11 +05301368 vnet_tap_connect_args_t _a, *ap = &_a;
1369 tapcli_main_t *tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001370 u8 hwaddr[6];
Ole Troanae65f5b2016-04-27 14:15:48 +02001371 u8 *hwaddr_arg = 0;
1372 u32 sw_if_index;
Dave Barach2feaffc2017-01-14 10:30:50 -05001373 ip4_address_t ip4_address;
1374 int ip4_address_set = 0;
1375 ip6_address_t ip6_address;
1376 int ip6_address_set = 0;
1377 u32 ip4_mask_width = 0;
1378 u32 ip6_mask_width = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001379 clib_error_t *error = NULL;
Ole Troan2df2e3d2016-03-02 10:01:43 +01001380
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381 if (tm->is_disabled)
Dave Barach2feaffc2017-01-14 10:30:50 -05001382 return clib_error_return (0, "device disabled...");
1383
1384 if (!unformat_user (input, unformat_line_input, line_input))
1385 return 0;
1386
1387 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388 {
sharath reddy1b0c9832017-11-29 20:08:11 +05301389 if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1390 &hwaddr))
1391 hwaddr_arg = hwaddr;
1392
Dave Barach2feaffc2017-01-14 10:30:50 -05001393 /* It is here for backward compatibility */
sharath reddy1b0c9832017-11-29 20:08:11 +05301394 else if (unformat (line_input, "hwaddr random"))
1395 ;
1396
Dave Barach2feaffc2017-01-14 10:30:50 -05001397 else if (unformat (line_input, "address %U/%d",
sharath reddy1b0c9832017-11-29 20:08:11 +05301398 unformat_ip4_address, &ip4_address, &ip4_mask_width))
1399 ip4_address_set = 1;
1400
Dave Barach2feaffc2017-01-14 10:30:50 -05001401 else if (unformat (line_input, "address %U/%d",
sharath reddy1b0c9832017-11-29 20:08:11 +05301402 unformat_ip6_address, &ip6_address, &ip6_mask_width))
1403 ip6_address_set = 1;
1404
Dave Barach2feaffc2017-01-14 10:30:50 -05001405 else if (unformat (line_input, "%s", &intfc_name))
sharath reddy1b0c9832017-11-29 20:08:11 +05301406 ;
Dave Barach2feaffc2017-01-14 10:30:50 -05001407 else
sharath reddy1b0c9832017-11-29 20:08:11 +05301408 {
1409 error = clib_error_return (0, "unknown input `%U'",
1410 format_unformat_error, line_input);
1411 goto done;
1412 }
Dave Barach2feaffc2017-01-14 10:30:50 -05001413 }
sharath reddy1b0c9832017-11-29 20:08:11 +05301414
Dave Barach18b28162017-01-20 08:34:15 -05001415 if (intfc_name == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -05001416 {
1417 error = clib_error_return (0, "interface name must be specified");
1418 goto done;
1419 }
Dave Barach18b28162017-01-20 08:34:15 -05001420
Dave Barach2feaffc2017-01-14 10:30:50 -05001421 memset (ap, 0, sizeof (*ap));
1422
1423 ap->intfc_name = intfc_name;
1424 ap->hwaddr_arg = hwaddr_arg;
1425 if (ip4_address_set)
1426 {
1427 ap->ip4_address = &ip4_address;
1428 ap->ip4_mask_width = ip4_mask_width;
1429 ap->ip4_address_set = 1;
1430 }
1431 if (ip6_address_set)
1432 {
1433 ap->ip6_address = &ip6_address;
1434 ap->ip6_mask_width = ip6_mask_width;
1435 ap->ip6_address_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436 }
1437
Dave Barach2feaffc2017-01-14 10:30:50 -05001438 ap->sw_if_indexp = &sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439
sharath reddy1b0c9832017-11-29 20:08:11 +05301440 int rv = vnet_tap_connect (vm, ap);
Ole Troanae65f5b2016-04-27 14:15:48 +02001441
sharath reddy1b0c9832017-11-29 20:08:11 +05301442 switch (rv)
Dave Barach2feaffc2017-01-14 10:30:50 -05001443 {
Ole Troanae65f5b2016-04-27 14:15:48 +02001444 case VNET_API_ERROR_SYSCALL_ERROR_1:
Billy McFalla9a20e72017-02-15 11:39:12 -05001445 error = clib_error_return (0, "Couldn't open /dev/net/tun");
1446 goto done;
sharath reddy1b0c9832017-11-29 20:08:11 +05301447
Ole Troanae65f5b2016-04-27 14:15:48 +02001448 case VNET_API_ERROR_SYSCALL_ERROR_2:
sharath reddy1b0c9832017-11-29 20:08:11 +05301449 error =
1450 clib_error_return (0, "Error setting flags on '%s'", intfc_name);
Billy McFalla9a20e72017-02-15 11:39:12 -05001451 goto done;
1452
Ole Troanae65f5b2016-04-27 14:15:48 +02001453 case VNET_API_ERROR_SYSCALL_ERROR_3:
sharath reddy1b0c9832017-11-29 20:08:11 +05301454 error = clib_error_return (0, "Couldn't open provisioning socket");
Billy McFalla9a20e72017-02-15 11:39:12 -05001455 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001456
Ole Troanae65f5b2016-04-27 14:15:48 +02001457 case VNET_API_ERROR_SYSCALL_ERROR_4:
sharath reddy1b0c9832017-11-29 20:08:11 +05301458 error = clib_error_return (0, "Couldn't get if_index");
Billy McFalla9a20e72017-02-15 11:39:12 -05001459 goto done;
sharath reddy1b0c9832017-11-29 20:08:11 +05301460
Ole Troanae65f5b2016-04-27 14:15:48 +02001461 case VNET_API_ERROR_SYSCALL_ERROR_5:
sharath reddy1b0c9832017-11-29 20:08:11 +05301462 error = clib_error_return (0, "Couldn't bind provisioning socket");
Billy McFalla9a20e72017-02-15 11:39:12 -05001463 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464
Ole Troanae65f5b2016-04-27 14:15:48 +02001465 case VNET_API_ERROR_SYSCALL_ERROR_6:
sharath reddy1b0c9832017-11-29 20:08:11 +05301466 error = clib_error_return (0, "Couldn't set device non-blocking flag");
Billy McFalla9a20e72017-02-15 11:39:12 -05001467 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001468
1469 case VNET_API_ERROR_SYSCALL_ERROR_7:
sharath reddy1b0c9832017-11-29 20:08:11 +05301470 error = clib_error_return (0, "Couldn't set device MTU");
Billy McFalla9a20e72017-02-15 11:39:12 -05001471 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001472
1473 case VNET_API_ERROR_SYSCALL_ERROR_8:
sharath reddy1b0c9832017-11-29 20:08:11 +05301474 error = clib_error_return (0, "Couldn't get interface flags");
Billy McFalla9a20e72017-02-15 11:39:12 -05001475 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001476
1477 case VNET_API_ERROR_SYSCALL_ERROR_9:
sharath reddy1b0c9832017-11-29 20:08:11 +05301478 error = clib_error_return (0, "Couldn't set intfc admin state up");
Billy McFalla9a20e72017-02-15 11:39:12 -05001479 goto done;
Dave Barach2feaffc2017-01-14 10:30:50 -05001480
1481 case VNET_API_ERROR_SYSCALL_ERROR_10:
sharath reddy1b0c9832017-11-29 20:08:11 +05301482 error = clib_error_return (0, "Couldn't set intfc address/mask");
Billy McFalla9a20e72017-02-15 11:39:12 -05001483 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001484
1485 case VNET_API_ERROR_INVALID_REGISTRATION:
sharath reddy1b0c9832017-11-29 20:08:11 +05301486 error = clib_error_return (0, "Invalid registration");
Billy McFalla9a20e72017-02-15 11:39:12 -05001487 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488
Dave Barach2feaffc2017-01-14 10:30:50 -05001489 case 0:
1490 break;
1491
1492 default:
sharath reddy1b0c9832017-11-29 20:08:11 +05301493 error = clib_error_return (0, "Unknown error: %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -05001494 goto done;
Dave Barach2feaffc2017-01-14 10:30:50 -05001495 }
1496
sharath reddy1b0c9832017-11-29 20:08:11 +05301497 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
1498 vnet_get_main (), sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05001499
1500done:
1501 unformat_free (line_input);
1502
1503 return error;
Dave Barach2feaffc2017-01-14 10:30:50 -05001504}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505
sharath reddy1b0c9832017-11-29 20:08:11 +05301506/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507VLIB_CLI_COMMAND (tap_connect_command, static) = {
1508 .path = "tap connect",
Florin Corasd79b41e2017-03-04 05:37:52 -08001509 .short_help =
sharath reddy1b0c9832017-11-29 20:08:11 +05301510 "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 .function = tap_connect_command_fn,
1512};
sharath reddy1b0c9832017-11-29 20:08:11 +05301513/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001514
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001515/**
1516 * @brief TAPCLI main init
1517 *
1518 * @param *vm - vlib_main_t
1519 *
1520 * @return error - clib_error_t
1521 *
1522 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523clib_error_t *
1524tapcli_init (vlib_main_t * vm)
1525{
sharath reddy1b0c9832017-11-29 20:08:11 +05301526 tapcli_main_t *tm = &tapcli_main;
1527 vlib_thread_main_t *m = vlib_get_thread_main ();
1528 tapcli_per_thread_t *thread;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529
1530 tm->vlib_main = vm;
sharath reddy1b0c9832017-11-29 20:08:11 +05301531 tm->vnet_main = vnet_get_main ();
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001532 tm->mtu_bytes = TAP_MTU_DEFAULT;
sharath reddy1b0c9832017-11-29 20:08:11 +05301533 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1535 vm->os_punt_frame = tapcli_nopunt_frame;
Steven4cd25762017-10-05 00:12:33 -07001536 vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
1537 CLIB_CACHE_LINE_BYTES);
1538 vec_foreach (thread, tm->threads)
sharath reddy1b0c9832017-11-29 20:08:11 +05301539 {
1540 thread->iovecs = 0;
1541 thread->rx_buffers = 0;
1542 vec_alloc (thread->rx_buffers, VLIB_FRAME_SIZE);
1543 vec_reset_length (thread->rx_buffers);
1544 }
Steven4cd25762017-10-05 00:12:33 -07001545
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 return 0;
1547}
1548
1549VLIB_INIT_FUNCTION (tapcli_init);
sharath reddy1b0c9832017-11-29 20:08:11 +05301550
1551/*
1552 * fd.io coding-style-patch-verification: ON
1553 *
1554 * Local Variables:
1555 * eval: (c-set-style "gnu")
1556 * End:
1557 */