FD.io VPP  v16.09
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 #if DPDK == 1
43 #include <vnet/devices/dpdk/dpdk.h>
44 #endif
45 
46 #include <vnet/unix/tapcli.h>
47 
51 
52 static void tapcli_nopunt_frame (vlib_main_t * vm,
53  vlib_node_runtime_t * node,
54  vlib_frame_t * frame);
55 /**
56  * @brief Struct for the tapcli interface
57  */
58 typedef struct {
62  /** For counters */
66  struct ifreq ifr;
68  /** for delete */
71 
72 /**
73  * @brief Struct for RX trace
74  */
75 typedef struct {
78 
79 /**
80  * @brief Function to format TAP CLI trace
81  *
82  * @param *s - u8 - formatting string
83  * @param *va - va_list
84  *
85  * @return *s - u8 - formatted string
86  *
87  */
88 u8 * format_tapcli_rx_trace (u8 * s, va_list * va)
89 {
90  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
91  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
92  vnet_main_t * vnm = vnet_get_main();
93  tapcli_rx_trace_t * t = va_arg (*va, tapcli_rx_trace_t *);
95  vnm, t->sw_if_index);
96  return s;
97 }
98 
99 /**
100  * @brief TAPCLI main state struct
101  */
102 typedef struct {
103  /** Vector of iovecs for readv/writev calls. */
104  struct iovec * iovecs;
105 
106  /** Vector of VLIB rx buffers to use. We allocate them in blocks
107  of VLIB_FRAME_SIZE (256). */
109 
110  /** tap device destination MAC address. Required, or Linux drops pkts */
111  u8 ether_dst_mac[6];
112 
113  /** Interface MTU in bytes and # of default sized buffers. */
114  u32 mtu_bytes, mtu_buffers;
115 
116  /** Vector of tap interfaces */
118 
119  /** Vector of deleted tap interfaces */
121 
122  /** Bitmap of tap interfaces with pending reads */
124 
125  /** Hash table to find tapcli interface given hw_if_index */
127 
128  /** Hash table to find tapcli interface given unix fd */
130 
131  /** renumbering table */
133 
134  /** 1 => disable CLI */
136 
137  /** convenience - vlib_main_t */
139  /** convenience - vnet_main_t */
141  /** convenience - unix_main_t */
143 } tapcli_main_t;
144 
146 
147 /**
148  * @brief tapcli TX node function
149  * @node tap-cli-tx
150  *
151  * Output node, writes the buffers comprising the incoming frame
152  * to the tun/tap device, aka hands them to the Linux kernel stack.
153  *
154  * @param *vm - vlib_main_t
155  * @param *node - vlib_node_runtime_t
156  * @param *frame - vlib_frame_t
157  *
158  * @return n_packets - uword
159  *
160  */
161 static uword
163  vlib_node_runtime_t * node,
164  vlib_frame_t * frame)
165 {
166  u32 * buffers = vlib_frame_args (frame);
167  uword n_packets = frame->n_vectors;
168  tapcli_main_t * tm = &tapcli_main;
169  tapcli_interface_t * ti;
170  int i;
171 
172  for (i = 0; i < n_packets; i++)
173  {
174  struct iovec * iov;
175  vlib_buffer_t * b;
176  uword l;
177  vnet_hw_interface_t * hw;
178  uword * p;
179  u32 tx_sw_if_index;
180 
181  b = vlib_get_buffer (vm, buffers[i]);
182 
183  tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
184  if (tx_sw_if_index == (u32)~0)
185  tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
186 
187  ASSERT(tx_sw_if_index != (u32)~0);
188 
189  /* Use the sup intfc to finesse vlan subifs */
190  hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
191  tx_sw_if_index = hw->sw_if_index;
192 
194  tx_sw_if_index);
195  if (p == 0)
196  {
197  clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
198  /* $$$ leak, but this should never happen... */
199  continue;
200  }
201  else
202  ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
203 
204  /* Re-set iovecs if present. */
205  if (tm->iovecs)
206  _vec_len (tm->iovecs) = 0;
207 
208  /* VLIB buffer chain -> Unix iovec(s). */
209  vec_add2 (tm->iovecs, iov, 1);
210  iov->iov_base = b->data + b->current_data;
211  iov->iov_len = l = b->current_length;
212 
214  {
215  do {
216  b = vlib_get_buffer (vm, b->next_buffer);
217 
218  vec_add2 (tm->iovecs, iov, 1);
219 
220  iov->iov_base = b->data + b->current_data;
221  iov->iov_len = b->current_length;
222  l += b->current_length;
223  } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
224  }
225 
226  if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
227  clib_unix_warning ("writev");
228  }
229 
231 
232  return n_packets;
233 }
234 
236  .function = tapcli_tx,
237  .name = "tapcli-tx",
238  .type = VLIB_NODE_TYPE_INTERNAL,
239  .vector_size = 4,
240 };
241 
242 enum {
248 };
249 
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 
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->rx_buffers) < tm->mtu_buffers)) {
292  uword len = vec_len(tm->rx_buffers);
293  _vec_len(tm->rx_buffers) +=
296  if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
298  TAPCLI_ERROR_BUFFER_ALLOC,
299  tm->mtu_buffers - vec_len(tm->rx_buffers));
300  break;
301  }
302  }
303 
304  uword i_rx = vec_len (tm->rx_buffers) - 1;
305 
306  /* Allocate RX buffers from end of rx_buffers.
307  Turn them into iovecs to pass to readv. */
308  vec_validate (tm->iovecs, tm->mtu_buffers - 1);
309  for (j = 0; j < tm->mtu_buffers; j++) {
310  b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
311  tm->iovecs[j].iov_base = b->data;
312  tm->iovecs[j].iov_len = buffer_size;
313  }
314 
315  n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
316  n_bytes_in_packet = n_bytes_left;
317  if (n_bytes_left <= 0) {
318  if (errno != EAGAIN) {
320  TAPCLI_ERROR_READ, 1);
321  }
322  break;
323  }
324 
325  bi_first = tm->rx_buffers[i_rx];
326  b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
327  prev = NULL;
328 
329  while (1) {
330  b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
331  n_bytes_left -= buffer_size;
332 
333  if (prev) {
334  prev->next_buffer = bi;
336  }
337  prev = b;
338 
339  /* last segment */
340  if (n_bytes_left <= 0)
341  break;
342 
343  i_rx--;
344  bi = tm->rx_buffers[i_rx];
345  b = vlib_get_buffer (vm, bi);
346  }
347 
348  _vec_len (tm->rx_buffers) = i_rx;
349 
351  (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
353 
354  /* Ensure mbufs are updated */
355  vlib_buffer_chain_validate(vm, b_first);
356 
358 
359  vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
360  vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
361 
362  b_first->error = node->errors[TAPCLI_ERROR_NONE];
363  next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
364  next_index = (ti->per_interface_next_index != ~0) ?
365  ti->per_interface_next_index : next_index;
366  next_index = admin_down ? TAPCLI_RX_NEXT_DROP : next_index;
367 
368  to_next[0] = bi_first;
369  to_next++;
370  n_left_to_next--;
371 
372  vlib_validate_buffer_enqueue_x1 (vm, node, next,
373  to_next, n_left_to_next,
374  bi_first, next_index);
375 
376  /* Interface counters for tapcli interface. */
377  if (PREDICT_TRUE(!admin_down)) {
382  1, n_bytes_in_packet);
383 
384  if (PREDICT_FALSE(n_trace > 0)) {
385  vlib_trace_buffer (vm, node, next_index,
386  b_first, /* follow_chain */ 1);
387  n_trace--;
388  set_trace = 1;
389  tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0));
390  t0->sw_if_index = si->sw_if_index;
391  }
392  }
393  }
394  vlib_put_next_frame (vm, node, next, n_left_to_next);
395  if (set_trace)
396  vlib_set_trace_count (vm, node, n_trace);
397  return VLIB_FRAME_SIZE - n_left_to_next;
398 }
399 
400 /**
401  * @brief tapcli RX node function
402  * @node tap-cli-rx
403  *
404  * Input node from the Kernel tun/tap device
405  *
406  * @param *vm - vlib_main_t
407  * @param *node - vlib_node_runtime_t
408  * @param *frame - vlib_frame_t
409  *
410  * @return n_packets - uword
411  *
412  */
413 static uword
415  vlib_node_runtime_t * node,
416  vlib_frame_t * frame)
417 {
418  tapcli_main_t * tm = &tapcli_main;
419  static u32 * ready_interface_indices;
420  tapcli_interface_t * ti;
421  int i;
422  u32 total_count = 0;
423 
424  vec_reset_length (ready_interface_indices);
426  ({
427  vec_add1 (ready_interface_indices, i);
428  }));
429 
430  if (vec_len (ready_interface_indices) == 0)
431  return 0;
432 
433  for (i = 0; i < vec_len(ready_interface_indices); i++)
434  {
435  tm->pending_read_bitmap =
437  ready_interface_indices[i], 0);
438 
439  ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
440  total_count += tapcli_rx_iface(vm, node, ti);
441  }
442  return total_count; //This might return more than 256.
443 }
444 
445 /** TAPCLI error strings */
446 static char * tapcli_rx_error_strings[] = {
447 #define _(sym,string) string,
449 #undef _
450 };
451 
453  .function = tapcli_rx,
454  .name = "tapcli-rx",
455  .type = VLIB_NODE_TYPE_INPUT,
456  .state = VLIB_NODE_STATE_INTERRUPT,
457  .vector_size = 4,
458  .n_errors = TAPCLI_N_ERROR,
459  .error_strings = tapcli_rx_error_strings,
460  .format_trace = format_tapcli_rx_trace,
461 
462  .n_next_nodes = TAPCLI_RX_N_NEXT,
463  .next_nodes = {
464  [TAPCLI_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
465  [TAPCLI_RX_NEXT_IP6_INPUT] = "ip6-input",
466  [TAPCLI_RX_NEXT_DROP] = "error-drop",
467  [TAPCLI_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
468  },
469 };
470 
471 
472 /**
473  * @brief Gets called when file descriptor is ready from epoll.
474  *
475  * @param *uf - unix_file_t
476  *
477  * @return error - clib_error_t
478  *
479  */
481 {
482  vlib_main_t * vm = vlib_get_main();
483  tapcli_main_t * tm = &tapcli_main;
484  uword * p;
485 
486  /** Schedule the rx node */
488 
490 
491  /** Mark the specific tap interface ready-to-read */
492  if (p)
494  p[0], 1);
495  else
496  clib_warning ("fd %d not in hash table", uf->file_descriptor);
497 
498  return 0;
499 }
500 
501 /**
502  * @brief CLI function for TAPCLI configuration
503  *
504  * @param *vm - vlib_main_t
505  * @param *input - unformat_input_t
506  *
507  * @return error - clib_error_t
508  *
509  */
510 static clib_error_t *
512 {
513  tapcli_main_t *tm = &tapcli_main;
514  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
515 
517  {
518  if (unformat (input, "mtu %d", &tm->mtu_bytes))
519  ;
520  else if (unformat (input, "disable"))
521  tm->is_disabled = 1;
522  else
523  return clib_error_return (0, "unknown input `%U'",
524  format_unformat_error, input);
525  }
526 
527  if (tm->is_disabled)
528  return 0;
529 
530  if (geteuid())
531  {
532  clib_warning ("tapcli disabled: must be superuser");
533  tm->is_disabled = 1;
534  return 0;
535  }
536 
537  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
538 
539  return 0;
540 }
541 
542 /**
543  * @brief Renumber TAPCLI interface
544  *
545  * @param *hi - vnet_hw_interface_t
546  * @param new_dev_instance - u32
547  *
548  * @return rc - int
549  *
550  */
552  u32 new_dev_instance)
553 {
554  tapcli_main_t *tm = &tapcli_main;
555 
557  hi->dev_instance, ~0);
558 
560  new_dev_instance;
561 
562  return 0;
563 }
564 
566 
567 /**
568  * @brief Free "no punt" frame
569  *
570  * @param *vm - vlib_main_t
571  * @param *node - vlib_node_runtime_t
572  * @param *frame - vlib_frame_t
573  *
574  */
575 static void
577  vlib_node_runtime_t * node,
578  vlib_frame_t * frame)
579 {
580  u32 * buffers = vlib_frame_args (frame);
581  uword n_packets = frame->n_vectors;
582  vlib_buffer_free (vm, buffers, n_packets);
583  vlib_frame_free (vm, node, frame);
584 }
585 
587  .name = "tapcli",
588 };
589 
590 /**
591  * @brief Formatter for TAPCLI interface name
592  *
593  * @param *s - formatter string
594  * @param *args - va_list
595  *
596  * @return *s - formatted string
597  *
598  */
599 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
600 {
601  u32 i = va_arg (*args, u32);
602  u32 show_dev_instance = ~0;
603  tapcli_main_t * tm = &tapcli_main;
604 
606  show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
607 
608  if (show_dev_instance != ~0)
609  i = show_dev_instance;
610 
611  s = format (s, "tap-%d", i);
612  return s;
613 }
614 
615 /**
616  * @brief Modify interface flags for TAPCLI interface
617  *
618  * @param *vnm - vnet_main_t
619  * @param *hw - vnet_hw_interface_t
620  * @param flags - u32
621  *
622  * @return rc - u32
623  *
624  */
626  vnet_hw_interface_t * hw,
627  u32 flags)
628 {
629  tapcli_main_t *tm = &tapcli_main;
630  tapcli_interface_t *ti;
631 
633 
634  if (flags & ETHERNET_INTERFACE_FLAG_MTU)
635  {
636  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
637  tm->mtu_bytes = hw->max_packet_bytes;
638  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
639  }
640  else
641  {
642  struct ifreq ifr;
643  u32 want_promisc;
644 
645  memcpy (&ifr, &ti->ifr, sizeof (ifr));
646 
647  /* get flags, modify to bring up interface... */
648  if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
649  {
650  clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
651  return 0;
652  }
653 
654  want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
655 
656  if (want_promisc == ti->is_promisc)
657  return 0;
658 
660  ifr.ifr_flags |= IFF_PROMISC;
661  else
662  ifr.ifr_flags &= ~(IFF_PROMISC);
663 
664  /* get flags, modify to bring up interface... */
665  if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
666  {
667  clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
668  return 0;
669  }
670 
671  ti->is_promisc = want_promisc;
672  }
673 
674  return 0;
675 }
676 
677 /**
678  * @brief Setting the TAP interface's next processing node
679  *
680  * @param *vnm - vnet_main_t
681  * @param hw_if_index - u32
682  * @param node_index - u32
683  *
684  */
686  u32 hw_if_index,
687  u32 node_index)
688 {
689  tapcli_main_t *tm = &tapcli_main;
690  tapcli_interface_t *ti;
691  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
692 
694 
695  /** Shut off redirection */
696  if (node_index == ~0)
697  {
698  ti->per_interface_next_index = node_index;
699  return;
700  }
701 
703  vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
704 }
705 
706 /**
707  * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
708  *
709  * @param *vnm - vnet_main_t
710  * @param hw_if_index - u32
711  * @param flags - u32
712  *
713  * @return error - clib_error_t
714  */
715 static clib_error_t *
717 {
718  uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
719  u32 hw_flags;
722 
723  if (is_admin_up)
724  hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
725  else
726  hw_flags = speed_duplex;
727 
728  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
729  return 0;
730 }
731 
733  .name = "tapcli",
734  .tx_function = tapcli_tx,
735  .format_device_name = format_tapcli_interface_name,
736  .rx_redirect_to_node = tapcli_set_interface_next_node,
737  .name_renumber = tap_name_renumber,
738  .admin_up_down_function = tapcli_interface_admin_up_down,
739  .no_flatten_output_chains = 1,
740 };
741 
742 /**
743  * @brief Dump TAP interfaces
744  *
745  * @param **out_tapids - tapcli_interface_details_t
746  *
747  * @return rc - int
748  *
749  */
751 {
752  tapcli_main_t * tm = &tapcli_main;
753  tapcli_interface_t * ti;
754 
755  tapcli_interface_details_t * r_tapids = NULL;
757 
758  vec_foreach (ti, tm->tapcli_interfaces) {
759  if (!ti->active)
760  continue;
761  vec_add2(r_tapids, tapid, 1);
762  tapid->sw_if_index = ti->sw_if_index;
763  strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
764  }
765 
766  *out_tapids = r_tapids;
767 
768  return 0;
769 }
770 
771 /**
772  * @brief Get tap interface from inactive interfaces or create new
773  *
774  * @return interface - tapcli_interface_t
775  *
776  */
778 {
779  tapcli_main_t * tm = &tapcli_main;
780  tapcli_interface_t *ti = NULL;
781 
782  int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
783  // if there are any inactive ifaces
784  if (inactive_cnt > 0) {
785  // take last
786  u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
787  if (vec_len(tm->tapcli_interfaces) > ti_idx) {
788  ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
789  clib_warning("reusing tap interface");
790  }
791  // "remove" from inactive list
792  _vec_len(tm->tapcli_inactive_interfaces) -= 1;
793  }
794 
795  // ti was not retrieved from inactive ifaces - create new
796  if (!ti)
797  vec_add2 (tm->tapcli_interfaces, ti, 1);
798 
799  return ti;
800 }
801 
802 /**
803  * @brief Connect a TAP interface
804  *
805  * @param vm - vlib_main_t
806  * @param intfc_name - u8
807  * @param hwaddr_arg - u8
808  * @param sw_if_indexp - u32
809  *
810  * @return rc - int
811  *
812  */
813 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
814  u32 * sw_if_indexp)
815 {
816  tapcli_main_t * tm = &tapcli_main;
817  tapcli_interface_t * ti = NULL;
818  struct ifreq ifr;
819  int flags;
820  int dev_net_tun_fd;
821  int dev_tap_fd = -1;
822  clib_error_t * error;
823  u8 hwaddr [6];
824  int rv = 0;
825 
826  if (tm->is_disabled)
827  {
828  return VNET_API_ERROR_FEATURE_DISABLED;
829  }
830 
831  flags = IFF_TAP | IFF_NO_PI;
832 
833  if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
834  return VNET_API_ERROR_SYSCALL_ERROR_1;
835 
836  memset (&ifr, 0, sizeof (ifr));
837  strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
838  ifr.ifr_flags = flags;
839  if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
840  {
841  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
842  goto error;
843  }
844 
845  /* Open a provisioning socket */
846  if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
847  htons(ETH_P_ALL))) < 0 )
848  {
849  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
850  goto error;
851  }
852 
853  /* Find the interface index. */
854  {
855  struct ifreq ifr;
856  struct sockaddr_ll sll;
857 
858  memset (&ifr, 0, sizeof(ifr));
859  strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
860  if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
861  {
862  rv = VNET_API_ERROR_SYSCALL_ERROR_4;
863  goto error;
864  }
865 
866  /* Bind the provisioning socket to the interface. */
867  memset(&sll, 0, sizeof(sll));
868  sll.sll_family = AF_PACKET;
869  sll.sll_ifindex = ifr.ifr_ifindex;
870  sll.sll_protocol = htons(ETH_P_ALL);
871 
872  if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
873  {
874  rv = VNET_API_ERROR_SYSCALL_ERROR_5;
875  goto error;
876  }
877  }
878 
879  /* non-blocking I/O on /dev/tapX */
880  {
881  int one = 1;
882  if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
883  {
884  rv = VNET_API_ERROR_SYSCALL_ERROR_6;
885  goto error;
886  }
887  }
888  ifr.ifr_mtu = tm->mtu_bytes;
889  if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
890  {
891  rv = VNET_API_ERROR_SYSCALL_ERROR_7;
892  goto error;
893  }
894 
895  /* get flags, modify to bring up interface... */
896  if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
897  {
898  rv = VNET_API_ERROR_SYSCALL_ERROR_8;
899  goto error;
900  }
901 
902  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
903 
904  if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
905  {
906  rv = VNET_API_ERROR_SYSCALL_ERROR_9;
907  goto error;
908  }
909 
910  ti = tapcli_get_new_tapif();
911  ti->per_interface_next_index = ~0;
912 
913  if (hwaddr_arg != 0)
914  clib_memcpy(hwaddr, hwaddr_arg, 6);
915  else
916  {
917  f64 now = vlib_time_now(vm);
918  u32 rnd;
919  rnd = (u32) (now * 1e6);
920  rnd = random_u32 (&rnd);
921 
922  memcpy (hwaddr+2, &rnd, sizeof(rnd));
923  hwaddr[0] = 2;
924  hwaddr[1] = 0xfe;
925  }
926 
928  (tm->vnet_main,
929  tapcli_dev_class.index,
930  ti - tm->tapcli_interfaces /* device instance */,
931  hwaddr /* ethernet address */,
932  &ti->hw_if_index,
934 
935  if (error)
936  {
937  clib_error_report (error);
938  rv = VNET_API_ERROR_INVALID_REGISTRATION;
939  goto error;
940  }
941 
942  {
943  unix_file_t template = {0};
944  template.read_function = tapcli_read_ready;
945  template.file_descriptor = dev_net_tun_fd;
946  ti->unix_file_index = unix_file_add (&unix_main, &template);
947  ti->unix_fd = dev_net_tun_fd;
948  ti->provision_fd = dev_tap_fd;
949  clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
950  }
951 
952  {
953  vnet_hw_interface_t * hw;
958  ti->sw_if_index = hw->sw_if_index;
959  if (sw_if_indexp)
960  *sw_if_indexp = hw->sw_if_index;
961  }
962 
963  ti->active = 1;
964 
966  ti - tm->tapcli_interfaces);
967 
969  ti - tm->tapcli_interfaces);
970 
971  return rv;
972 
973  error:
974  close (dev_net_tun_fd);
975  if (dev_tap_fd >= 0)
976  close (dev_tap_fd);
977 
978  return rv;
979 }
980 
981 /**
982  * @brief Renumber a TAP interface
983  *
984  * @param *vm - vlib_main_t
985  * @param *intfc_name - u8
986  * @param *hwaddr_arg - u8
987  * @param *sw_if_indexp - u32
988  * @param renumber - u8
989  * @param custom_dev_instance - u32
990  *
991  * @return rc - int
992  *
993  */
994 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
995  u8 *hwaddr_arg, u32 * sw_if_indexp,
996  u8 renumber, u32 custom_dev_instance)
997 {
998  int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
999 
1000  if (!rv && renumber)
1001  vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
1002 
1003  return rv;
1004 }
1005 
1006 /**
1007  * @brief Disconnect TAP CLI interface
1008  *
1009  * @param *ti - tapcli_interface_t
1010  *
1011  * @return rc - int
1012  *
1013  */
1015 {
1016  int rv = 0;
1017  vnet_main_t * vnm = vnet_get_main();
1018  tapcli_main_t * tm = &tapcli_main;
1019  u32 sw_if_index = ti->sw_if_index;
1020 
1021  // bring interface down
1022  vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1023 
1024  if (ti->unix_file_index != ~0) {
1026  ti->unix_file_index = ~0;
1027  }
1028 
1031  close(ti->unix_fd);
1032  close(ti->provision_fd);
1033  ti->unix_fd = -1;
1034  ti->provision_fd = -1;
1035 
1036  return rv;
1037 }
1038 
1039 /**
1040  * @brief Delete TAP interface
1041  *
1042  * @param *vm - vlib_main_t
1043  * @param sw_if_index - u32
1044  *
1045  * @return rc - int
1046  *
1047  */
1048 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
1049 {
1050  int rv = 0;
1051  tapcli_main_t * tm = &tapcli_main;
1052  tapcli_interface_t *ti;
1053  uword *p = NULL;
1054 
1056  sw_if_index);
1057  if (p == 0) {
1058  clib_warning ("sw_if_index %d unknown", sw_if_index);
1059  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1060  }
1061  ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1062 
1063  // inactive
1064  ti->active = 0;
1066  // add to inactive list
1068 
1069  // reset renumbered iface
1072 
1074  return rv;
1075 }
1076 
1077 /**
1078  * @brief CLI function to delete TAP interface
1079  *
1080  * @param *vm - vlib_main_t
1081  * @param *input - unformat_input_t
1082  * @param *cmd - vlib_cli_command_t
1083  *
1084  * @return error - clib_error_t
1085  *
1086  */
1087 static clib_error_t *
1089  unformat_input_t * input,
1090  vlib_cli_command_t * cmd)
1091 {
1092  tapcli_main_t * tm = &tapcli_main;
1093  u32 sw_if_index = ~0;
1094 
1095  if (tm->is_disabled)
1096  {
1097  return clib_error_return (0, "device disabled...");
1098  }
1099 
1100  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1101  &sw_if_index))
1102  ;
1103  else
1104  return clib_error_return (0, "unknown input `%U'",
1105  format_unformat_error, input);
1106 
1107 
1108  int rc = vnet_tap_delete (vm, sw_if_index);
1109 
1110  if (!rc) {
1111  vlib_cli_output (vm, "Deleted.");
1112  } else {
1113  vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
1114  }
1115 
1116  return 0;
1117 }
1118 
1119 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1120  .path = "tap delete",
1121  .short_help = "tap delete <vpp-tap-intfc-name>",
1122  .function = tap_delete_command_fn,
1123 };
1124 
1125 /**
1126  * @brief Modifies tap interface - can result in new interface being created
1127  *
1128  * @param *vm - vlib_main_t
1129  * @param orig_sw_if_index - u32
1130  * @param *intfc_name - u8
1131  * @param *hwaddr_arg - u8
1132  * @param *sw_if_indexp - u32
1133  * @param renumber - u8
1134  * @param custom_dev_instance - u32
1135  *
1136  * @return rc - int
1137  *
1138  */
1139 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
1140  u8 * intfc_name, u8 *hwaddr_arg,
1141  u32 * sw_if_indexp,
1142  u8 renumber, u32 custom_dev_instance)
1143 {
1144  int rv = vnet_tap_delete (vm, orig_sw_if_index);
1145 
1146  if (rv)
1147  return rv;
1148 
1149  rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
1150  renumber, custom_dev_instance);
1151 
1152  return rv;
1153 }
1154 
1155 /**
1156  * @brief CLI function to modify TAP interface
1157  *
1158  * @param *vm - vlib_main_t
1159  * @param *input - unformat_input_t
1160  * @param *cmd - vlib_cli_command_t
1161  *
1162  * @return error - clib_error_t
1163  *
1164  */
1165 static clib_error_t *
1167  unformat_input_t * input,
1168  vlib_cli_command_t * cmd)
1169 {
1170  u8 * intfc_name;
1171  tapcli_main_t * tm = &tapcli_main;
1172  u32 sw_if_index = ~0;
1173  u32 new_sw_if_index = ~0;
1174  int user_hwaddr = 0;
1175  u8 hwaddr[6];
1176 
1177  if (tm->is_disabled)
1178  {
1179  return clib_error_return (0, "device disabled...");
1180  }
1181 
1182  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1183  &sw_if_index))
1184  ;
1185  else
1186  return clib_error_return (0, "unknown input `%U'",
1187  format_unformat_error, input);
1188 
1189  if (unformat (input, "%s", &intfc_name))
1190  ;
1191  else
1192  return clib_error_return (0, "unknown input `%U'",
1193  format_unformat_error, input);
1194 
1195  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1196  &hwaddr))
1197  user_hwaddr = 1;
1198 
1199 
1200  int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
1201  (user_hwaddr == 1 ? hwaddr : 0),
1202  &new_sw_if_index, 0, 0);
1203 
1204  if (!rc) {
1205  vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1207  new_sw_if_index, intfc_name);
1208  } else {
1209  vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
1210  }
1211 
1212  return 0;
1213 }
1214 
1215 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1216  .path = "tap modify",
1217  .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1218  .function = tap_modify_command_fn,
1219 };
1220 
1221 /**
1222  * @brief CLI function to connect TAP interface
1223  *
1224  * @param *vm - vlib_main_t
1225  * @param *input - unformat_input_t
1226  * @param *cmd - vlib_cli_command_t
1227  *
1228  * @return error - clib_error_t
1229  *
1230  */
1231 static clib_error_t *
1233  unformat_input_t * input,
1234  vlib_cli_command_t * cmd)
1235 {
1236  u8 * intfc_name;
1237  tapcli_main_t * tm = &tapcli_main;
1238  u8 hwaddr[6];
1239  u8 *hwaddr_arg = 0;
1240  u32 sw_if_index;
1241 
1242  if (tm->is_disabled)
1243  {
1244  return clib_error_return (0, "device disabled...");
1245  }
1246 
1247  if (unformat (input, "%s", &intfc_name))
1248  ;
1249  else
1250  return clib_error_return (0, "unknown input `%U'",
1251  format_unformat_error, input);
1252 
1253  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1254  &hwaddr))
1255  hwaddr_arg = hwaddr;
1256 
1257  /* It is here for backward compatibility */
1258  if (unformat(input, "hwaddr random"))
1259  ;
1260 
1261  int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
1262  if (rv) {
1263  switch (rv) {
1264  case VNET_API_ERROR_SYSCALL_ERROR_1:
1265  vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1266  break;
1267 
1268  case VNET_API_ERROR_SYSCALL_ERROR_2:
1269  vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1270  break;
1271 
1272  case VNET_API_ERROR_SYSCALL_ERROR_3:
1273  vlib_cli_output (vm, "Couldn't open provisioning socket");
1274  break;
1275 
1276  case VNET_API_ERROR_SYSCALL_ERROR_4:
1277  vlib_cli_output (vm, "Couldn't get if_index");
1278  break;
1279 
1280  case VNET_API_ERROR_SYSCALL_ERROR_5:
1281  vlib_cli_output (vm, "Couldn't bind provisioning socket");
1282  break;
1283 
1284  case VNET_API_ERROR_SYSCALL_ERROR_6:
1285  vlib_cli_output (0, "Couldn't set device non-blocking flag");
1286  break;
1287 
1288  case VNET_API_ERROR_SYSCALL_ERROR_7:
1289  vlib_cli_output (0, "Couldn't set device MTU");
1290  break;
1291 
1292  case VNET_API_ERROR_SYSCALL_ERROR_8:
1293  vlib_cli_output (0, "Couldn't get interface flags");
1294  break;
1295 
1296  case VNET_API_ERROR_SYSCALL_ERROR_9:
1297  vlib_cli_output (0, "Couldn't set intfc admin state up");
1298  break;
1299 
1300  case VNET_API_ERROR_INVALID_REGISTRATION:
1301  vlib_cli_output (0, "Invalid registration");
1302  break;
1303  default:
1304  vlib_cli_output (0, "Unknown error: %d", rv);
1305  break;
1306  }
1307  return 0;
1308  }
1309 
1310  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
1311  return 0;
1312  }
1313 
1314 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1315  .path = "tap connect",
1316  .short_help = "tap connect <intfc-name> [hwaddr <addr>]",
1317  .function = tap_connect_command_fn,
1318 };
1319 
1320 /**
1321  * @brief TAPCLI main init
1322  *
1323  * @param *vm - vlib_main_t
1324  *
1325  * @return error - clib_error_t
1326  *
1327  */
1328 clib_error_t *
1330 {
1331  tapcli_main_t * tm = &tapcli_main;
1332 
1333  tm->vlib_main = vm;
1334  tm->vnet_main = vnet_get_main();
1335  tm->unix_main = &unix_main;
1336  tm->mtu_bytes = TAP_MTU_DEFAULT;
1339  tm->rx_buffers = 0;
1343  return 0;
1344 }
1345 
u32 per_interface_next_index
Definition: tapcli.c:67
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
unix_file_t * file_pool
Definition: unix.h:89
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:457
vlib_main_t * vlib_main
convenience - vlib_main_t
Definition: tapcli.c:138
static int tap_name_renumber(vnet_hw_interface_t *hi, u32 new_dev_instance)
Renumber TAPCLI interface.
Definition: tapcli.c:551
vmrglw vmrglh hi
u32 * show_dev_instance_by_real_dev_instance
renumbering table
Definition: tapcli.c:132
#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:129
int vnet_tap_modify(vlib_main_t *vm, u32 orig_sw_if_index, u8 *intfc_name, u8 *hwaddr_arg, u32 *sw_if_indexp, u8 renumber, u32 custom_dev_instance)
Modifies tap interface - can result in new interface being created.
Definition: tapcli.c:1139
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
#define VNET_HW_INTERFACE_FLAG_SPEED_1G
Definition: interface.h:286
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:513
static vlib_node_registration_t tapcli_tx_node
(constructor) VLIB_REGISTER_NODE (tapcli_tx_node)
Definition: tapcli.c:235
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
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:230
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
vnet_interface_main_t interface_main
Definition: vnet.h:64
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:399
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:1088
#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:179
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define NULL
Definition: clib.h:55
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: dpdk_buffer.c:655
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1151
struct _vlib_node_registration vlib_node_registration_t
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
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
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
unformat_function_t unformat_vnet_sw_interface
#define clib_error_report(e)
Definition: error.h:125
VNET_DEVICE_CLASS(tapcli_dev_class, static)
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:273
static clib_error_t * tapcli_read_ready(unix_file_t *uf)
Gets called when file descriptor is ready from epoll.
Definition: tapcli.c:480
struct _vnet_device_class vnet_device_class_t
vlib_error_t * errors
Definition: node.h:418
TAP CLI interface details struct.
Definition: tapcli.h:41
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:239
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:1061
struct iovec * iovecs
Vector of iovecs for readv/writev calls.
Definition: tapcli.c:104
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Struct for RX trace.
Definition: tapcli.c:75
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
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#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:78
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:1166
TAPCLI definitions.
static vnet_device_class_t tapcli_dev_class
Definition: tapcli.c:48
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:501
u8 active
for delete
Definition: tapcli.c:69
#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:145
#define clib_warning(format, args...)
Definition: error.h:59
static clib_error_t * tapcli_config(vlib_main_t *vm, unformat_input_t *input)
CLI function for TAPCLI configuration.
Definition: tapcli.c:511
static tapcli_interface_t * tapcli_get_new_tapif()
Get tap interface from inactive interfaces or create new.
Definition: tapcli.c:777
u32 unix_file_index
Definition: tapcli.c:60
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
u32 max_supported_packet_bytes
Definition: interface.h:331
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146
#define hash_get(h, key)
Definition: hash.h:248
#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:82
static vnet_hw_interface_class_t tapcli_interface_class
Definition: tapcli.c:49
void vlib_frame_free(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_frame_t *f)
Definition: main.c:214
uword * tapcli_interface_index_by_sw_if_index
Hash table to find tapcli interface given hw_if_index.
Definition: tapcli.c:126
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:162
static char * tapcli_rx_error_strings[]
TAPCLI error strings.
Definition: tapcli.c:446
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:118
vnet_main_t vnet_main
Definition: misc.c:42
#define VLIB_FRAME_SIZE
Definition: node.h:328
struct ifreq ifr
Definition: tapcli.c:66
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:625
#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:130
#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:348
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
u32 mtu_bytes
Interface MTU in bytes and # of default sized buffers.
Definition: tapcli.c:114
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:118
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
#define TAP_MTU_DEFAULT
Definition: tapcli.h:50
u32 mtu_buffers
Definition: tapcli.c:114
u16 n_vectors
Definition: node.h:344
static uword tapcli_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
tapcli RX node function
Definition: tapcli.c:414
static int tapcli_tap_disconnect(tapcli_interface_t *ti)
Disconnect TAP CLI interface.
Definition: tapcli.c:1014
void vlib_buffer_chain_validate(vlib_main_t *vm, vlib_buffer_t *first)
Definition: dpdk_buffer.c:946
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:576
#define clib_memcpy(a, b, c)
Definition: string.h:63
tapcli_interface_t * tapcli_interfaces
Vector of tap interfaces.
Definition: tapcli.c:117
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:97
#define ETHERNET_INTERFACE_FLAG_MTU
Definition: ethernet.h:90
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:85
u32 * rx_buffers
Vector of VLIB rx buffers to use.
Definition: tapcli.c:108
static u8 * format_tapcli_interface_name(u8 *s, va_list *args)
Formatter for TAPCLI interface name.
Definition: tapcli.c:599
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:303
#define hash_create(elts, value_bytes)
Definition: hash.h:647
#define VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
Definition: interface.h:277
u16 cached_next_index
Definition: node.h:462
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:415
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:345
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:206
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define vnet_buffer(b)
Definition: buffer.h:335
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
#define TAP_MTU_MAX
Definition: tapcli.h:49
void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: dpdk_buffer.c:766
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
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:114
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:181
u8 * format_tapcli_rx_trace(u8 *s, va_list *va)
Function to format TAP CLI trace.
Definition: tapcli.c:88
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:268
int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
Delete TAP interface.
Definition: tapcli.c:1048
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:716
unix_main_t unix_main
Definition: main.c:57
VNET_HW_INTERFACE_CLASS(tapcli_interface_class, static)
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:109
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
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
u32 * tapcli_inactive_interfaces
Vector of deleted tap interfaces.
Definition: tapcli.c:120
int is_disabled
1 => disable CLI
Definition: tapcli.c:135
vnet_main_t * vnet_main
convenience - vnet_main_t
Definition: tapcli.c:140
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
Definition: unix.h:49
int vnet_tap_connect(vlib_main_t *vm, u8 *intfc_name, u8 *hwaddr_arg, u32 *sw_if_indexp)
Connect a TAP interface.
Definition: tapcli.c:813
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:51
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u32 min_supported_packet_bytes
Definition: interface.h:328
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
int vnet_tap_connect_renumber(vlib_main_t *vm, u8 *intfc_name, u8 *hwaddr_arg, u32 *sw_if_indexp, u8 renumber, u32 custom_dev_instance)
Renumber a TAP interface.
Definition: tapcli.c:994
clib_error_t * tapcli_init(vlib_main_t *vm)
TAPCLI main init.
Definition: tapcli.c:1329
u8 data[0]
Packet data.
Definition: buffer.h:151
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:521
u32 sw_if_index
For counters.
Definition: tapcli.c:63
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
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:445
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
Struct for the tapcli interface.
Definition: tapcli.c:58
int vnet_tap_dump_ifs(tapcli_interface_details_t **out_tapids)
Dump TAP interfaces.
Definition: tapcli.c:750
uword * pending_read_bitmap
Bitmap of tap interfaces with pending reads.
Definition: tapcli.c:123
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
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:685
Definition: defs.h:46
TAPCLI main state struct.
Definition: tapcli.c:102
static vlib_node_registration_t tapcli_rx_node
(constructor) VLIB_REGISTER_NODE (tapcli_rx_node)
Definition: tapcli.c:50
unix_main_t * unix_main
convenience - unix_main_t
Definition: tapcli.c:142
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:1232