blob: 13154b3b0340b50b1a07910f9531722806f5875a [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,
52 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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057typedef struct {
58 u32 unix_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +020059 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 u32 provision_fd;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070061 /** For counters */
62 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 u32 hw_if_index;
64 u32 is_promisc;
65 struct ifreq ifr;
66 u32 per_interface_next_index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070067 /** for delete */
68 u8 active;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069} tapcli_interface_t;
70
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070071/**
72 * @brief Struct for RX trace
73 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074typedef struct {
Pierre Pfister3a8f32b2016-03-21 12:21:30 +000075 u16 sw_if_index;
76} tapcli_rx_trace_t;
77
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070078/**
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 Pfister3a8f32b2016-03-21 12:21:30 +000087u8 * 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)07203af2016-08-25 13:37:37 -070098/**
99 * @brief TAPCLI main state struct
100 */
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000101typedef struct {
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700102 /** Vector of iovecs for readv/writev calls. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 struct iovec * iovecs;
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). */
107 u32 * rx_buffers;
108
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700109 /** tap device destination MAC address. Required, or Linux drops pkts */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 u8 ether_dst_mac[6];
111
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700112 /** Interface MTU in bytes and # of default sized buffers. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 u32 mtu_bytes, mtu_buffers;
114
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700115 /** Vector of tap interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 tapcli_interface_t * tapcli_interfaces;
117
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700118 /** Vector of deleted tap interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 u32 * tapcli_inactive_interfaces;
120
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121 /** Bitmap of tap interfaces with pending reads */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 uword * pending_read_bitmap;
123
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700124 /** Hash table to find tapcli interface given hw_if_index */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 uword * tapcli_interface_index_by_sw_if_index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700126
127 /** Hash table to find tapcli interface given unix fd */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 uword * tapcli_interface_index_by_unix_fd;
129
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700130 /** renumbering table */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 u32 * show_dev_instance_by_real_dev_instance;
132
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700133 /** 1 => disable CLI */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 int is_disabled;
135
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700136 /** convenience - vlib_main_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 vlib_main_t * vlib_main;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700138 /** convenience - vnet_main_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 vnet_main_t * vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140} tapcli_main_t;
141
142static tapcli_main_t tapcli_main;
143
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700144/**
145 * @brief tapcli TX node function
146 * @node tap-cli-tx
147 *
148 * Output node, writes the buffers comprising the incoming frame
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 * to the tun/tap device, aka hands them to the Linux kernel stack.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700150 *
151 * @param *vm - vlib_main_t
152 * @param *node - vlib_node_runtime_t
153 * @param *frame - vlib_frame_t
154 *
155 * @return n_packets - uword
156 *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 */
158static uword
159tapcli_tx (vlib_main_t * vm,
160 vlib_node_runtime_t * node,
161 vlib_frame_t * frame)
162{
163 u32 * buffers = vlib_frame_args (frame);
164 uword n_packets = frame->n_vectors;
165 tapcli_main_t * tm = &tapcli_main;
166 tapcli_interface_t * ti;
167 int i;
168
169 for (i = 0; i < n_packets; i++)
170 {
171 struct iovec * iov;
172 vlib_buffer_t * b;
173 uword l;
174 vnet_hw_interface_t * hw;
175 uword * p;
176 u32 tx_sw_if_index;
177
178 b = vlib_get_buffer (vm, buffers[i]);
179
180 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
181 if (tx_sw_if_index == (u32)~0)
182 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
183
184 ASSERT(tx_sw_if_index != (u32)~0);
185
186 /* Use the sup intfc to finesse vlan subifs */
187 hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
188 tx_sw_if_index = hw->sw_if_index;
Ole Troan2df2e3d2016-03-02 10:01:43 +0100189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
191 tx_sw_if_index);
192 if (p == 0)
193 {
194 clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
195 /* $$$ leak, but this should never happen... */
196 continue;
197 }
198 else
199 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
200
201 /* Re-set iovecs if present. */
202 if (tm->iovecs)
203 _vec_len (tm->iovecs) = 0;
204
205 /* VLIB buffer chain -> Unix iovec(s). */
206 vec_add2 (tm->iovecs, iov, 1);
207 iov->iov_base = b->data + b->current_data;
208 iov->iov_len = l = b->current_length;
209
210 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
211 {
212 do {
213 b = vlib_get_buffer (vm, b->next_buffer);
214
215 vec_add2 (tm->iovecs, iov, 1);
216
217 iov->iov_base = b->data + b->current_data;
218 iov->iov_len = b->current_length;
219 l += b->current_length;
220 } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
221 }
222
223 if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
224 clib_unix_warning ("writev");
225 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700226
Ole Troan2df2e3d2016-03-02 10:01:43 +0100227 vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700228
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 return n_packets;
230}
231
232VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
233 .function = tapcli_tx,
234 .name = "tapcli-tx",
235 .type = VLIB_NODE_TYPE_INTERNAL,
236 .vector_size = 4,
237};
238
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700239/**
240 * @brief Dispatch tapcli RX node function for node tap_cli_rx
241 *
242 *
243 * @param *vm - vlib_main_t
244 * @param *node - vlib_node_runtime_t
245 * @param *ti - tapcli_interface_t
246 *
247 * @return n_packets - uword
248 *
249 */
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000250static uword tapcli_rx_iface(vlib_main_t * vm,
251 vlib_node_runtime_t * node,
252 tapcli_interface_t * ti)
253{
254 tapcli_main_t * tm = &tapcli_main;
Damjan Marion19010202016-03-24 17:17:47 +0100255 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000256 u32 n_trace = vlib_get_trace_count (vm, node);
257 u8 set_trace = 0;
258
259 vnet_main_t *vnm;
260 vnet_sw_interface_t * si;
261 u8 admin_down;
262 u32 next = node->cached_next_index;
263 u32 n_left_to_next, next_index;
264 u32 *to_next;
265
266 vnm = vnet_get_main();
267 si = vnet_get_sw_interface (vnm, ti->sw_if_index);
268 admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
269
270 vlib_get_next_frame(vm, node, next, to_next, n_left_to_next);
271
272 while (n_left_to_next) { // Fill at most one vector
273 vlib_buffer_t *b_first, *b, *prev;
274 u32 bi_first, bi;
275 word n_bytes_in_packet;
276 int j, n_bytes_left;
277
278 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
279 uword len = vec_len(tm->rx_buffers);
280 _vec_len(tm->rx_buffers) +=
281 vlib_buffer_alloc_from_free_list(vm, &tm->rx_buffers[len],
282 VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
283 if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
Pierre Pfisterae6725b2016-07-15 08:44:10 +0100284 vlib_node_increment_counter(vm, tapcli_rx_node.index,
285 TAPCLI_ERROR_BUFFER_ALLOC,
286 tm->mtu_buffers - vec_len(tm->rx_buffers));
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000287 break;
288 }
289 }
290
291 uword i_rx = vec_len (tm->rx_buffers) - 1;
292
293 /* Allocate RX buffers from end of rx_buffers.
294 Turn them into iovecs to pass to readv. */
295 vec_validate (tm->iovecs, tm->mtu_buffers - 1);
296 for (j = 0; j < tm->mtu_buffers; j++) {
297 b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000298 tm->iovecs[j].iov_base = b->data;
299 tm->iovecs[j].iov_len = buffer_size;
300 }
301
302 n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
303 n_bytes_in_packet = n_bytes_left;
304 if (n_bytes_left <= 0) {
305 if (errno != EAGAIN) {
306 vlib_node_increment_counter(vm, tapcli_rx_node.index,
307 TAPCLI_ERROR_READ, 1);
308 }
309 break;
310 }
311
312 bi_first = tm->rx_buffers[i_rx];
313 b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
314 prev = NULL;
315
316 while (1) {
317 b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
318 n_bytes_left -= buffer_size;
319
320 if (prev) {
321 prev->next_buffer = bi;
322 prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
323 }
324 prev = b;
325
326 /* last segment */
327 if (n_bytes_left <= 0)
328 break;
329
330 i_rx--;
331 bi = tm->rx_buffers[i_rx];
332 b = vlib_get_buffer (vm, bi);
333 }
334
335 _vec_len (tm->rx_buffers) = i_rx;
336
337 b_first->total_length_not_including_first_buffer =
338 (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
339 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
340
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000341 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
342
343 vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
344 vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
345
346 b_first->error = node->errors[TAPCLI_ERROR_NONE];
Dave Barach9f6186e2016-11-08 12:12:12 -0500347 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000348 next_index = (ti->per_interface_next_index != ~0) ?
349 ti->per_interface_next_index : next_index;
Dave Barach9f6186e2016-11-08 12:12:12 -0500350 next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000351
352 to_next[0] = bi_first;
353 to_next++;
354 n_left_to_next--;
355
Damjan Marion35af9e52017-03-06 12:02:50 +0100356 vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, b_first);
Dave Barach9f6186e2016-11-08 12:12:12 -0500357
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000358 vlib_validate_buffer_enqueue_x1 (vm, node, next,
359 to_next, n_left_to_next,
360 bi_first, next_index);
361
362 /* Interface counters for tapcli interface. */
363 if (PREDICT_TRUE(!admin_down)) {
364 vlib_increment_combined_counter (
365 vnet_main.interface_main.combined_sw_if_counters
366 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200367 vlib_get_thread_index(), ti->sw_if_index,
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000368 1, n_bytes_in_packet);
369
370 if (PREDICT_FALSE(n_trace > 0)) {
371 vlib_trace_buffer (vm, node, next_index,
372 b_first, /* follow_chain */ 1);
373 n_trace--;
374 set_trace = 1;
375 tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0));
376 t0->sw_if_index = si->sw_if_index;
377 }
378 }
379 }
380 vlib_put_next_frame (vm, node, next, n_left_to_next);
381 if (set_trace)
382 vlib_set_trace_count (vm, node, n_trace);
383 return VLIB_FRAME_SIZE - n_left_to_next;
384}
385
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700386/**
387 * @brief tapcli RX node function
388 * @node tap-cli-rx
389 *
390 * Input node from the Kernel tun/tap device
391 *
392 * @param *vm - vlib_main_t
393 * @param *node - vlib_node_runtime_t
394 * @param *frame - vlib_frame_t
395 *
396 * @return n_packets - uword
397 *
398 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399static uword
400tapcli_rx (vlib_main_t * vm,
401 vlib_node_runtime_t * node,
402 vlib_frame_t * frame)
403{
404 tapcli_main_t * tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 static u32 * ready_interface_indices;
406 tapcli_interface_t * ti;
407 int i;
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000408 u32 total_count = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
410 vec_reset_length (ready_interface_indices);
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000411 clib_bitmap_foreach (i, tm->pending_read_bitmap,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 ({
413 vec_add1 (ready_interface_indices, i);
414 }));
415
416 if (vec_len (ready_interface_indices) == 0)
Dave Barachb3ca6582016-06-10 18:31:55 -0400417 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
419 for (i = 0; i < vec_len(ready_interface_indices); i++)
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000420 {
421 tm->pending_read_bitmap =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 clib_bitmap_set (tm->pending_read_bitmap,
423 ready_interface_indices[i], 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000425 ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
426 total_count += tapcli_rx_iface(vm, node, ti);
427 }
428 return total_count; //This might return more than 256.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700431/** TAPCLI error strings */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432static char * tapcli_rx_error_strings[] = {
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000433#define _(sym,string) string,
434 foreach_tapcli_error
435#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436};
437
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000438VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 .function = tapcli_rx,
440 .name = "tapcli-rx",
Damjan Marion51327ac2016-11-09 11:59:42 +0100441 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 .type = VLIB_NODE_TYPE_INPUT,
443 .state = VLIB_NODE_STATE_INTERRUPT,
444 .vector_size = 4,
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000445 .n_errors = TAPCLI_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 .error_strings = tapcli_rx_error_strings,
Pierre Pfister3a8f32b2016-03-21 12:21:30 +0000447 .format_trace = format_tapcli_rx_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448};
449
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700450
451/**
452 * @brief Gets called when file descriptor is ready from epoll.
453 *
Damjan Marion56dd5432017-09-08 19:52:02 +0200454 * @param *uf - clib_file_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700455 *
456 * @return error - clib_error_t
457 *
458 */
Damjan Marion56dd5432017-09-08 19:52:02 +0200459static clib_error_t * tapcli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460{
461 vlib_main_t * vm = vlib_get_main();
462 tapcli_main_t * tm = &tapcli_main;
463 uword * p;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700464
465 /** Schedule the rx node */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
467
468 p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
469
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700470 /** Mark the specific tap interface ready-to-read */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 if (p)
472 tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
473 p[0], 1);
474 else
475 clib_warning ("fd %d not in hash table", uf->file_descriptor);
476
477 return 0;
478}
479
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700480/**
481 * @brief CLI function for TAPCLI configuration
482 *
483 * @param *vm - vlib_main_t
484 * @param *input - unformat_input_t
485 *
486 * @return error - clib_error_t
487 *
488 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489static clib_error_t *
490tapcli_config (vlib_main_t * vm, unformat_input_t * input)
491{
492 tapcli_main_t *tm = &tapcli_main;
Damjan Marion19010202016-03-24 17:17:47 +0100493 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
495 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
496 {
497 if (unformat (input, "mtu %d", &tm->mtu_bytes))
498 ;
499 else if (unformat (input, "disable"))
500 tm->is_disabled = 1;
501 else
502 return clib_error_return (0, "unknown input `%U'",
503 format_unformat_error, input);
504 }
505
506 if (tm->is_disabled)
507 return 0;
508
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700509 if (geteuid())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 {
511 clib_warning ("tapcli disabled: must be superuser");
512 tm->is_disabled = 1;
513 return 0;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700514 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
516 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700517
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 return 0;
519}
520
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700521/**
522 * @brief Renumber TAPCLI interface
523 *
524 * @param *hi - vnet_hw_interface_t
525 * @param new_dev_instance - u32
526 *
527 * @return rc - int
528 *
529 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530static int tap_name_renumber (vnet_hw_interface_t * hi,
531 u32 new_dev_instance)
532{
533 tapcli_main_t *tm = &tapcli_main;
534
535 vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
536 hi->dev_instance, ~0);
537
538 tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
539 new_dev_instance;
540
541 return 0;
542}
543
544VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
545
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700546/**
547 * @brief Free "no punt" frame
548 *
549 * @param *vm - vlib_main_t
550 * @param *node - vlib_node_runtime_t
551 * @param *frame - vlib_frame_t
552 *
553 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554static void
555tapcli_nopunt_frame (vlib_main_t * vm,
556 vlib_node_runtime_t * node,
557 vlib_frame_t * frame)
558{
559 u32 * buffers = vlib_frame_args (frame);
560 uword n_packets = frame->n_vectors;
561 vlib_buffer_free (vm, buffers, n_packets);
562 vlib_frame_free (vm, node, frame);
563}
564
565VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
566 .name = "tapcli",
Neale Rannsb80c5362016-10-08 13:03:40 +0100567 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568};
569
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700570/**
571 * @brief Formatter for TAPCLI interface name
572 *
573 * @param *s - formatter string
574 * @param *args - va_list
575 *
576 * @return *s - formatted string
577 *
578 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
580{
581 u32 i = va_arg (*args, u32);
582 u32 show_dev_instance = ~0;
583 tapcli_main_t * tm = &tapcli_main;
584
585 if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
586 show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
587
588 if (show_dev_instance != ~0)
589 i = show_dev_instance;
590
591 s = format (s, "tap-%d", i);
592 return s;
593}
594
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700595/**
596 * @brief Modify interface flags for TAPCLI interface
597 *
598 * @param *vnm - vnet_main_t
599 * @param *hw - vnet_hw_interface_t
600 * @param flags - u32
601 *
602 * @return rc - u32
603 *
604 */
605static u32 tapcli_flag_change (vnet_main_t * vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 vnet_hw_interface_t * hw,
607 u32 flags)
608{
609 tapcli_main_t *tm = &tapcli_main;
610 tapcli_interface_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200612 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200614 if (flags & ETHERNET_INTERFACE_FLAG_MTU)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615 {
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200616 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
617 tm->mtu_bytes = hw->max_packet_bytes;
618 tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619 }
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200620 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 {
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200622 struct ifreq ifr;
623 u32 want_promisc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +0200625 memcpy (&ifr, &ti->ifr, sizeof (ifr));
626
627 /* get flags, modify to bring up interface... */
628 if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
629 {
630 clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
631 return 0;
632 }
633
634 want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
635
636 if (want_promisc == ti->is_promisc)
637 return 0;
638
639 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
640 ifr.ifr_flags |= IFF_PROMISC;
641 else
642 ifr.ifr_flags &= ~(IFF_PROMISC);
643
644 /* get flags, modify to bring up interface... */
645 if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
646 {
647 clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
648 return 0;
649 }
650
651 ti->is_promisc = want_promisc;
652 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
654 return 0;
655}
656
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700657/**
658 * @brief Setting the TAP interface's next processing node
659 *
660 * @param *vnm - vnet_main_t
661 * @param hw_if_index - u32
662 * @param node_index - u32
663 *
664 */
665static void tapcli_set_interface_next_node (vnet_main_t *vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 u32 hw_if_index,
667 u32 node_index)
668{
669 tapcli_main_t *tm = &tapcli_main;
670 tapcli_interface_t *ti;
671 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
672
673 ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700674
675 /** Shut off redirection */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 if (node_index == ~0)
677 {
678 ti->per_interface_next_index = node_index;
679 return;
680 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700681
682 ti->per_interface_next_index =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
684}
685
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700686/**
687 * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
688 *
689 * @param *vnm - vnet_main_t
690 * @param hw_if_index - u32
691 * @param flags - u32
692 *
693 * @return error - clib_error_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 */
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700695static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
697{
698 uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
699 u32 hw_flags;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700700 u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 | VNET_HW_INTERFACE_FLAG_SPEED_1G;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700702
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 if (is_admin_up)
704 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
705 else
706 hw_flags = speed_duplex;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700707
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
709 return 0;
710}
711
712VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
713 .name = "tapcli",
714 .tx_function = tapcli_tx,
715 .format_device_name = format_tapcli_interface_name,
716 .rx_redirect_to_node = tapcli_set_interface_next_node,
717 .name_renumber = tap_name_renumber,
718 .admin_up_down_function = tapcli_interface_admin_up_down,
719};
720
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700721/**
722 * @brief Dump TAP interfaces
723 *
724 * @param **out_tapids - tapcli_interface_details_t
725 *
726 * @return rc - int
727 *
728 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
730{
731 tapcli_main_t * tm = &tapcli_main;
732 tapcli_interface_t * ti;
733
734 tapcli_interface_details_t * r_tapids = NULL;
735 tapcli_interface_details_t * tapid = NULL;
736
737 vec_foreach (ti, tm->tapcli_interfaces) {
738 if (!ti->active)
739 continue;
740 vec_add2(r_tapids, tapid, 1);
741 tapid->sw_if_index = ti->sw_if_index;
742 strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
743 }
744
745 *out_tapids = r_tapids;
746
747 return 0;
748}
749
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700750/**
751 * @brief Get tap interface from inactive interfaces or create new
752 *
753 * @return interface - tapcli_interface_t
754 *
755 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756static tapcli_interface_t *tapcli_get_new_tapif()
757{
758 tapcli_main_t * tm = &tapcli_main;
759 tapcli_interface_t *ti = NULL;
760
761 int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
762 // if there are any inactive ifaces
763 if (inactive_cnt > 0) {
764 // take last
765 u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
766 if (vec_len(tm->tapcli_interfaces) > ti_idx) {
767 ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
768 clib_warning("reusing tap interface");
769 }
770 // "remove" from inactive list
771 _vec_len(tm->tapcli_inactive_interfaces) -= 1;
772 }
773
774 // ti was not retrieved from inactive ifaces - create new
775 if (!ti)
776 vec_add2 (tm->tapcli_interfaces, ti, 1);
777
778 return ti;
779}
780
Dave Barach2feaffc2017-01-14 10:30:50 -0500781typedef struct
782{
783 ip6_address_t addr;
784 u32 mask_width;
785 unsigned int ifindex;
786} ip6_ifreq_t;
787
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700788/**
789 * @brief Connect a TAP interface
790 *
Chris Luked4024f52016-09-06 09:32:36 -0400791 * @param vm - vlib_main_t
Dave Barach2feaffc2017-01-14 10:30:50 -0500792 * @param ap - vnet_tap_connect_args_t
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700793 *
794 * @return rc - int
795 *
796 */
Dave Barach2feaffc2017-01-14 10:30:50 -0500797int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798{
799 tapcli_main_t * tm = &tapcli_main;
800 tapcli_interface_t * ti = NULL;
801 struct ifreq ifr;
802 int flags;
803 int dev_net_tun_fd;
804 int dev_tap_fd = -1;
805 clib_error_t * error;
806 u8 hwaddr [6];
807 int rv = 0;
808
809 if (tm->is_disabled)
810 {
811 return VNET_API_ERROR_FEATURE_DISABLED;
812 }
813
814 flags = IFF_TAP | IFF_NO_PI;
815
816 if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
817 return VNET_API_ERROR_SYSCALL_ERROR_1;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700818
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819 memset (&ifr, 0, sizeof (ifr));
Dave Barach2feaffc2017-01-14 10:30:50 -0500820 strncpy(ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 ifr.ifr_flags = flags;
822 if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
823 {
824 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
825 goto error;
826 }
827
828 /* Open a provisioning socket */
829 if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
830 htons(ETH_P_ALL))) < 0 )
831 {
832 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
833 goto error;
834 }
835
836 /* Find the interface index. */
837 {
838 struct ifreq ifr;
839 struct sockaddr_ll sll;
840
841 memset (&ifr, 0, sizeof(ifr));
Dave Barach2feaffc2017-01-14 10:30:50 -0500842 strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
844 {
845 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
846 goto error;
847 }
848
849 /* Bind the provisioning socket to the interface. */
850 memset(&sll, 0, sizeof(sll));
851 sll.sll_family = AF_PACKET;
852 sll.sll_ifindex = ifr.ifr_ifindex;
853 sll.sll_protocol = htons(ETH_P_ALL);
854
855 if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
856 {
857 rv = VNET_API_ERROR_SYSCALL_ERROR_5;
858 goto error;
859 }
860 }
861
862 /* non-blocking I/O on /dev/tapX */
863 {
864 int one = 1;
865 if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
866 {
867 rv = VNET_API_ERROR_SYSCALL_ERROR_6;
868 goto error;
869 }
870 }
871 ifr.ifr_mtu = tm->mtu_bytes;
872 if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
873 {
874 rv = VNET_API_ERROR_SYSCALL_ERROR_7;
875 goto error;
876 }
877
878 /* get flags, modify to bring up interface... */
879 if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
880 {
881 rv = VNET_API_ERROR_SYSCALL_ERROR_8;
882 goto error;
883 }
884
885 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
886
887 if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
888 {
889 rv = VNET_API_ERROR_SYSCALL_ERROR_9;
890 goto error;
891 }
892
Dave Barach2feaffc2017-01-14 10:30:50 -0500893 if (ap->ip4_address_set)
894 {
895 struct sockaddr_in sin;
896 /* ip4: mask defaults to /24 */
897 u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
898
Dave Barach8f544962017-01-18 10:23:22 -0500899 memset(&sin, 0, sizeof(sin));
Dave Barach2feaffc2017-01-14 10:30:50 -0500900 sin.sin_family = AF_INET;
Dave Barach8f544962017-01-18 10:23:22 -0500901 /* sin.sin_port = 0; */
Dave Barach2feaffc2017-01-14 10:30:50 -0500902 sin.sin_addr.s_addr = ap->ip4_address->as_u32;
903 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
904
905 if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
906 {
907 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
908 goto error;
909 }
910
911 if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
912 {
913 mask = ~0;
914 mask <<= (32 - ap->ip4_mask_width);
915 }
916
917 mask = clib_host_to_net_u32(mask);
918 sin.sin_family = AF_INET;
919 sin.sin_port = 0;
920 sin.sin_addr.s_addr = mask;
921 memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
922
923 if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
924 {
925 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
926 goto error;
927 }
928 }
929
930 if (ap->ip6_address_set)
931 {
932 struct ifreq ifr2;
933 ip6_ifreq_t ifr6;
934 int sockfd6;
935
936 sockfd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP);
937 if (sockfd6 < 0)
938 {
939 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
940 goto error;
941 }
942
943 memset (&ifr2, 0, sizeof(ifr));
944 strncpy (ifr2.ifr_name, (char *) ap->intfc_name,
945 sizeof (ifr2.ifr_name)-1);
946 if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0 )
947 {
948 close (sockfd6);
949 rv = VNET_API_ERROR_SYSCALL_ERROR_4;
950 goto error;
951 }
952
953 memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
954 ifr6.mask_width = ap->ip6_mask_width;
955 ifr6.ifindex = ifr2.ifr_ifindex;
956
957 if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
958 {
959 close (sockfd6);
960 clib_unix_warning ("ifr6");
961 rv = VNET_API_ERROR_SYSCALL_ERROR_10;
962 goto error;
963 }
964 close (sockfd6);
965 }
966
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967 ti = tapcli_get_new_tapif();
Ole Troanae65f5b2016-04-27 14:15:48 +0200968 ti->per_interface_next_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969
Dave Barach2feaffc2017-01-14 10:30:50 -0500970 if (ap->hwaddr_arg != 0)
971 clib_memcpy(hwaddr, ap->hwaddr_arg, 6);
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +0200972 else
973 {
974 f64 now = vlib_time_now(vm);
975 u32 rnd;
976 rnd = (u32) (now * 1e6);
977 rnd = random_u32 (&rnd);
978
979 memcpy (hwaddr+2, &rnd, sizeof(rnd));
980 hwaddr[0] = 2;
981 hwaddr[1] = 0xfe;
982 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
984 error = ethernet_register_interface
985 (tm->vnet_main,
986 tapcli_dev_class.index,
987 ti - tm->tapcli_interfaces /* device instance */,
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +0200988 hwaddr /* ethernet address */,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989 &ti->hw_if_index,
990 tapcli_flag_change);
991
992 if (error)
993 {
994 clib_error_report (error);
995 rv = VNET_API_ERROR_INVALID_REGISTRATION;
996 goto error;
997 }
998
999 {
Damjan Marion56dd5432017-09-08 19:52:02 +02001000 clib_file_t template = {0};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001 template.read_function = tapcli_read_ready;
1002 template.file_descriptor = dev_net_tun_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +02001003 ti->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 ti->unix_fd = dev_net_tun_fd;
1005 ti->provision_fd = dev_tap_fd;
Damjan Marionf1213b82016-03-13 02:22:06 +01001006 clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 }
1008
1009 {
1010 vnet_hw_interface_t * hw;
1011 hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001012 hw->min_supported_packet_bytes = TAP_MTU_MIN;
1013 hw->max_supported_packet_bytes = TAP_MTU_MAX;
1014 hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 ti->sw_if_index = hw->sw_if_index;
Dave Barach2feaffc2017-01-14 10:30:50 -05001016 if (ap->sw_if_indexp)
1017 *(ap->sw_if_indexp) = hw->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018 }
1019
1020 ti->active = 1;
1021
1022 hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1023 ti - tm->tapcli_interfaces);
1024
1025 hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1026 ti - tm->tapcli_interfaces);
1027
1028 return rv;
1029
1030 error:
1031 close (dev_net_tun_fd);
Dave Barachf9c231e2016-08-05 10:10:18 -04001032 if (dev_tap_fd >= 0)
1033 close (dev_tap_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
1035 return rv;
1036}
1037
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001038/**
1039 * @brief Renumber a TAP interface
1040 *
1041 * @param *vm - vlib_main_t
1042 * @param *intfc_name - u8
1043 * @param *hwaddr_arg - u8
1044 * @param *sw_if_indexp - u32
1045 * @param renumber - u8
1046 * @param custom_dev_instance - u32
1047 *
1048 * @return rc - int
1049 *
1050 */
Dave Barach2feaffc2017-01-14 10:30:50 -05001051int vnet_tap_connect_renumber (vlib_main_t * vm,
1052 vnet_tap_connect_args_t *ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053{
Dave Barach2feaffc2017-01-14 10:30:50 -05001054 int rv = vnet_tap_connect(vm, ap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Dave Barach2feaffc2017-01-14 10:30:50 -05001056 if (!rv && ap->renumber)
1057 vnet_interface_name_renumber (*(ap->sw_if_indexp), ap->custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Barach2feaffc2017-01-14 10:30:50 -05001059 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060}
1061
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001062/**
1063 * @brief Disconnect TAP CLI interface
1064 *
1065 * @param *ti - tapcli_interface_t
1066 *
1067 * @return rc - int
1068 *
1069 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070static int tapcli_tap_disconnect (tapcli_interface_t *ti)
1071{
1072 int rv = 0;
1073 vnet_main_t * vnm = vnet_get_main();
1074 tapcli_main_t * tm = &tapcli_main;
1075 u32 sw_if_index = ti->sw_if_index;
1076
1077 // bring interface down
1078 vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1079
Damjan Marion56dd5432017-09-08 19:52:02 +02001080 if (ti->clib_file_index != ~0) {
1081 clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index);
1082 ti->clib_file_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083 }
Eyal Barif298ecf2016-09-19 18:47:39 +03001084 else
1085 close(ti->unix_fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
1087 hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
1088 hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 close(ti->provision_fd);
1090 ti->unix_fd = -1;
1091 ti->provision_fd = -1;
1092
1093 return rv;
1094}
1095
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001096/**
1097 * @brief Delete TAP interface
1098 *
1099 * @param *vm - vlib_main_t
1100 * @param sw_if_index - u32
1101 *
1102 * @return rc - int
1103 *
1104 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
1106{
1107 int rv = 0;
1108 tapcli_main_t * tm = &tapcli_main;
1109 tapcli_interface_t *ti;
1110 uword *p = NULL;
1111
1112 p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
1113 sw_if_index);
1114 if (p == 0) {
1115 clib_warning ("sw_if_index %d unknown", sw_if_index);
1116 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1117 }
1118 ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1119
1120 // inactive
1121 ti->active = 0;
1122 tapcli_tap_disconnect(ti);
1123 // add to inactive list
1124 vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
1125
1126 // reset renumbered iface
1127 if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
1128 tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1129
1130 ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
1131 return rv;
1132}
1133
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001134/**
1135 * @brief CLI function to delete TAP interface
1136 *
1137 * @param *vm - vlib_main_t
1138 * @param *input - unformat_input_t
1139 * @param *cmd - vlib_cli_command_t
1140 *
1141 * @return error - clib_error_t
1142 *
1143 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144static clib_error_t *
1145tap_delete_command_fn (vlib_main_t * vm,
1146 unformat_input_t * input,
1147 vlib_cli_command_t * cmd)
1148{
1149 tapcli_main_t * tm = &tapcli_main;
1150 u32 sw_if_index = ~0;
1151
1152 if (tm->is_disabled)
1153 {
1154 return clib_error_return (0, "device disabled...");
1155 }
1156
1157 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1158 &sw_if_index))
1159 ;
1160 else
1161 return clib_error_return (0, "unknown input `%U'",
1162 format_unformat_error, input);
1163
1164
1165 int rc = vnet_tap_delete (vm, sw_if_index);
1166
1167 if (!rc) {
1168 vlib_cli_output (vm, "Deleted.");
1169 } else {
1170 vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
1171 }
1172
1173 return 0;
1174}
1175
1176VLIB_CLI_COMMAND (tap_delete_command, static) = {
1177 .path = "tap delete",
1178 .short_help = "tap delete <vpp-tap-intfc-name>",
1179 .function = tap_delete_command_fn,
1180};
1181
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001182/**
1183 * @brief Modifies tap interface - can result in new interface being created
1184 *
1185 * @param *vm - vlib_main_t
1186 * @param orig_sw_if_index - u32
1187 * @param *intfc_name - u8
1188 * @param *hwaddr_arg - u8
1189 * @param *sw_if_indexp - u32
1190 * @param renumber - u8
1191 * @param custom_dev_instance - u32
1192 *
1193 * @return rc - int
1194 *
1195 */
Dave Barach2feaffc2017-01-14 10:30:50 -05001196int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197{
Dave Barach2feaffc2017-01-14 10:30:50 -05001198 int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
1200 if (rv)
Dave Barach2feaffc2017-01-14 10:30:50 -05001201 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202
Dave Barach2feaffc2017-01-14 10:30:50 -05001203 rv = vnet_tap_connect_renumber(vm, ap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
1205 return rv;
1206}
1207
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001208/**
1209 * @brief CLI function to modify TAP interface
1210 *
1211 * @param *vm - vlib_main_t
1212 * @param *input - unformat_input_t
1213 * @param *cmd - vlib_cli_command_t
1214 *
1215 * @return error - clib_error_t
1216 *
1217 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218static clib_error_t *
1219tap_modify_command_fn (vlib_main_t * vm,
1220 unformat_input_t * input,
1221 vlib_cli_command_t * cmd)
1222{
1223 u8 * intfc_name;
1224 tapcli_main_t * tm = &tapcli_main;
1225 u32 sw_if_index = ~0;
1226 u32 new_sw_if_index = ~0;
1227 int user_hwaddr = 0;
1228 u8 hwaddr[6];
Dave Barach2feaffc2017-01-14 10:30:50 -05001229 vnet_tap_connect_args_t _a, *ap= &_a;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001230
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231 if (tm->is_disabled)
1232 {
1233 return clib_error_return (0, "device disabled...");
1234 }
1235
1236 if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1237 &sw_if_index))
1238 ;
1239 else
1240 return clib_error_return (0, "unknown input `%U'",
1241 format_unformat_error, input);
1242
1243 if (unformat (input, "%s", &intfc_name))
1244 ;
1245 else
1246 return clib_error_return (0, "unknown input `%U'",
1247 format_unformat_error, input);
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001248
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249 if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1250 &hwaddr))
1251 user_hwaddr = 1;
1252
1253
Dave Barach2feaffc2017-01-14 10:30:50 -05001254 memset (ap, 0, sizeof(*ap));
1255 ap->orig_sw_if_index = sw_if_index;
1256 ap->intfc_name = intfc_name;
1257 ap->sw_if_indexp = &new_sw_if_index;
1258 if (user_hwaddr)
1259 ap->hwaddr_arg = hwaddr;
1260
1261 int rc = vnet_tap_modify (vm, ap);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
1263 if (!rc) {
1264 vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1265 format_vnet_sw_if_index_name, tm->vnet_main,
Dave Barach2feaffc2017-01-14 10:30:50 -05001266 *(ap->sw_if_indexp), ap->intfc_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267 } else {
1268 vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
1269 }
1270
1271 return 0;
1272}
1273
1274VLIB_CLI_COMMAND (tap_modify_command, static) = {
1275 .path = "tap modify",
Mohsin KAZMI90d8e2c2016-07-18 16:57:40 +02001276 .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277 .function = tap_modify_command_fn,
1278};
1279
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001280/**
1281 * @brief CLI function to connect TAP interface
1282 *
1283 * @param *vm - vlib_main_t
1284 * @param *input - unformat_input_t
1285 * @param *cmd - vlib_cli_command_t
1286 *
1287 * @return error - clib_error_t
1288 *
1289 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290static clib_error_t *
1291tap_connect_command_fn (vlib_main_t * vm,
1292 unformat_input_t * input,
1293 vlib_cli_command_t * cmd)
1294{
Dave Barach8f544962017-01-18 10:23:22 -05001295 u8 * intfc_name = 0;
Dave Barach2feaffc2017-01-14 10:30:50 -05001296 unformat_input_t _line_input, *line_input = &_line_input;
1297 vnet_tap_connect_args_t _a, *ap= &_a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298 tapcli_main_t * tm = &tapcli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 u8 hwaddr[6];
Ole Troanae65f5b2016-04-27 14:15:48 +02001300 u8 *hwaddr_arg = 0;
1301 u32 sw_if_index;
Dave Barach2feaffc2017-01-14 10:30:50 -05001302 ip4_address_t ip4_address;
1303 int ip4_address_set = 0;
1304 ip6_address_t ip6_address;
1305 int ip6_address_set = 0;
1306 u32 ip4_mask_width = 0;
1307 u32 ip6_mask_width = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001308 clib_error_t *error = NULL;
Ole Troan2df2e3d2016-03-02 10:01:43 +01001309
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310 if (tm->is_disabled)
Dave Barach2feaffc2017-01-14 10:30:50 -05001311 return clib_error_return (0, "device disabled...");
1312
1313 if (!unformat_user (input, unformat_line_input, line_input))
1314 return 0;
1315
1316 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317 {
Dave Barach2feaffc2017-01-14 10:30:50 -05001318 if (unformat(line_input, "hwaddr %U", unformat_ethernet_address,
1319 &hwaddr))
1320 hwaddr_arg = hwaddr;
1321
1322 /* It is here for backward compatibility */
1323 else if (unformat(line_input, "hwaddr random"))
1324 ;
1325
1326 else if (unformat (line_input, "address %U/%d",
1327 unformat_ip4_address, &ip4_address, &ip4_mask_width))
1328 ip4_address_set = 1;
1329
1330 else if (unformat (line_input, "address %U/%d",
1331 unformat_ip6_address, &ip6_address, &ip6_mask_width))
1332 ip6_address_set = 1;
1333
1334 else if (unformat (line_input, "%s", &intfc_name))
1335 ;
1336 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001337 {
1338 error = clib_error_return (0, "unknown input `%U'",
1339 format_unformat_error, line_input);
1340 goto done;
1341 }
Dave Barach2feaffc2017-01-14 10:30:50 -05001342 }
1343
Dave Barach18b28162017-01-20 08:34:15 -05001344 if (intfc_name == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -05001345 {
1346 error = clib_error_return (0, "interface name must be specified");
1347 goto done;
1348 }
Dave Barach18b28162017-01-20 08:34:15 -05001349
Dave Barach2feaffc2017-01-14 10:30:50 -05001350 memset (ap, 0, sizeof (*ap));
1351
1352 ap->intfc_name = intfc_name;
1353 ap->hwaddr_arg = hwaddr_arg;
1354 if (ip4_address_set)
1355 {
1356 ap->ip4_address = &ip4_address;
1357 ap->ip4_mask_width = ip4_mask_width;
1358 ap->ip4_address_set = 1;
1359 }
1360 if (ip6_address_set)
1361 {
1362 ap->ip6_address = &ip6_address;
1363 ap->ip6_mask_width = ip6_mask_width;
1364 ap->ip6_address_set = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365 }
1366
Dave Barach2feaffc2017-01-14 10:30:50 -05001367 ap->sw_if_indexp = &sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368
Dave Barach2feaffc2017-01-14 10:30:50 -05001369 int rv = vnet_tap_connect(vm, ap);
Ole Troanae65f5b2016-04-27 14:15:48 +02001370
Dave Barach2feaffc2017-01-14 10:30:50 -05001371 switch (rv)
1372 {
Ole Troanae65f5b2016-04-27 14:15:48 +02001373 case VNET_API_ERROR_SYSCALL_ERROR_1:
Billy McFalla9a20e72017-02-15 11:39:12 -05001374 error = clib_error_return (0, "Couldn't open /dev/net/tun");
1375 goto done;
Dave Barach2feaffc2017-01-14 10:30:50 -05001376
Ole Troanae65f5b2016-04-27 14:15:48 +02001377 case VNET_API_ERROR_SYSCALL_ERROR_2:
Billy McFalla9a20e72017-02-15 11:39:12 -05001378 error = clib_error_return (0, "Error setting flags on '%s'", intfc_name);
1379 goto done;
1380
Ole Troanae65f5b2016-04-27 14:15:48 +02001381 case VNET_API_ERROR_SYSCALL_ERROR_3:
Billy McFalla9a20e72017-02-15 11:39:12 -05001382 error = clib_error_return (0, "Couldn't open provisioning socket");
1383 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001384
Ole Troanae65f5b2016-04-27 14:15:48 +02001385 case VNET_API_ERROR_SYSCALL_ERROR_4:
Billy McFalla9a20e72017-02-15 11:39:12 -05001386 error = clib_error_return (0, "Couldn't get if_index");
1387 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001388
1389 case VNET_API_ERROR_SYSCALL_ERROR_5:
Billy McFalla9a20e72017-02-15 11:39:12 -05001390 error = clib_error_return (0, "Couldn't bind provisioning socket");
1391 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392
Ole Troanae65f5b2016-04-27 14:15:48 +02001393 case VNET_API_ERROR_SYSCALL_ERROR_6:
Billy McFalla9a20e72017-02-15 11:39:12 -05001394 error = clib_error_return (0, "Couldn't set device non-blocking flag");
1395 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001396
1397 case VNET_API_ERROR_SYSCALL_ERROR_7:
Billy McFalla9a20e72017-02-15 11:39:12 -05001398 error = clib_error_return (0, "Couldn't set device MTU");
1399 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001400
1401 case VNET_API_ERROR_SYSCALL_ERROR_8:
Billy McFalla9a20e72017-02-15 11:39:12 -05001402 error = clib_error_return (0, "Couldn't get interface flags");
1403 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001404
1405 case VNET_API_ERROR_SYSCALL_ERROR_9:
Billy McFalla9a20e72017-02-15 11:39:12 -05001406 error = clib_error_return (0, "Couldn't set intfc admin state up");
1407 goto done;
Dave Barach2feaffc2017-01-14 10:30:50 -05001408
1409 case VNET_API_ERROR_SYSCALL_ERROR_10:
Billy McFalla9a20e72017-02-15 11:39:12 -05001410 error = clib_error_return (0, "Couldn't set intfc address/mask");
1411 goto done;
Ole Troanae65f5b2016-04-27 14:15:48 +02001412
1413 case VNET_API_ERROR_INVALID_REGISTRATION:
Billy McFalla9a20e72017-02-15 11:39:12 -05001414 error = clib_error_return (0, "Invalid registration");
1415 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416
Dave Barach2feaffc2017-01-14 10:30:50 -05001417 case 0:
1418 break;
1419
1420 default:
Billy McFalla9a20e72017-02-15 11:39:12 -05001421 error = clib_error_return (0, "Unknown error: %d", rv);
1422 goto done;
Dave Barach2feaffc2017-01-14 10:30:50 -05001423 }
1424
1425 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
1426 vnet_get_main(), sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05001427
1428done:
1429 unformat_free (line_input);
1430
1431 return error;
Dave Barach2feaffc2017-01-14 10:30:50 -05001432}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001433
1434VLIB_CLI_COMMAND (tap_connect_command, static) = {
1435 .path = "tap connect",
Florin Corasd79b41e2017-03-04 05:37:52 -08001436 .short_help =
1437 "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438 .function = tap_connect_command_fn,
1439};
1440
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -07001441/**
1442 * @brief TAPCLI main init
1443 *
1444 * @param *vm - vlib_main_t
1445 *
1446 * @return error - clib_error_t
1447 *
1448 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001449clib_error_t *
1450tapcli_init (vlib_main_t * vm)
1451{
1452 tapcli_main_t * tm = &tapcli_main;
1453
1454 tm->vlib_main = vm;
1455 tm->vnet_main = vnet_get_main();
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001456 tm->mtu_bytes = TAP_MTU_DEFAULT;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001457 tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1458 tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
Pierre Pfister3a8f32b2016-03-21 12:21:30 +00001459 tm->rx_buffers = 0;
1460 vec_alloc(tm->rx_buffers, VLIB_FRAME_SIZE);
1461 vec_reset_length(tm->rx_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 vm->os_punt_frame = tapcli_nopunt_frame;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463 return 0;
1464}
1465
1466VLIB_INIT_FUNCTION (tapcli_init);