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