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