FD.io VPP  v16.12-rc0-308-g931be3a
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",
589 };
590 
591 /**
592  * @brief Formatter for TAPCLI interface name
593  *
594  * @param *s - formatter string
595  * @param *args - va_list
596  *
597  * @return *s - formatted string
598  *
599  */
600 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
601 {
602  u32 i = va_arg (*args, u32);
603  u32 show_dev_instance = ~0;
604  tapcli_main_t * tm = &tapcli_main;
605 
607  show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
608 
609  if (show_dev_instance != ~0)
610  i = show_dev_instance;
611 
612  s = format (s, "tap-%d", i);
613  return s;
614 }
615 
616 /**
617  * @brief Modify interface flags for TAPCLI interface
618  *
619  * @param *vnm - vnet_main_t
620  * @param *hw - vnet_hw_interface_t
621  * @param flags - u32
622  *
623  * @return rc - u32
624  *
625  */
627  vnet_hw_interface_t * hw,
628  u32 flags)
629 {
630  tapcli_main_t *tm = &tapcli_main;
631  tapcli_interface_t *ti;
632 
634 
635  if (flags & ETHERNET_INTERFACE_FLAG_MTU)
636  {
637  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
638  tm->mtu_bytes = hw->max_packet_bytes;
639  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
640  }
641  else
642  {
643  struct ifreq ifr;
644  u32 want_promisc;
645 
646  memcpy (&ifr, &ti->ifr, sizeof (ifr));
647 
648  /* get flags, modify to bring up interface... */
649  if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
650  {
651  clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
652  return 0;
653  }
654 
655  want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
656 
657  if (want_promisc == ti->is_promisc)
658  return 0;
659 
661  ifr.ifr_flags |= IFF_PROMISC;
662  else
663  ifr.ifr_flags &= ~(IFF_PROMISC);
664 
665  /* get flags, modify to bring up interface... */
666  if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
667  {
668  clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
669  return 0;
670  }
671 
672  ti->is_promisc = want_promisc;
673  }
674 
675  return 0;
676 }
677 
678 /**
679  * @brief Setting the TAP interface's next processing node
680  *
681  * @param *vnm - vnet_main_t
682  * @param hw_if_index - u32
683  * @param node_index - u32
684  *
685  */
687  u32 hw_if_index,
688  u32 node_index)
689 {
690  tapcli_main_t *tm = &tapcli_main;
691  tapcli_interface_t *ti;
692  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
693 
695 
696  /** Shut off redirection */
697  if (node_index == ~0)
698  {
699  ti->per_interface_next_index = node_index;
700  return;
701  }
702 
704  vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
705 }
706 
707 /**
708  * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
709  *
710  * @param *vnm - vnet_main_t
711  * @param hw_if_index - u32
712  * @param flags - u32
713  *
714  * @return error - clib_error_t
715  */
716 static clib_error_t *
718 {
719  uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
720  u32 hw_flags;
723 
724  if (is_admin_up)
725  hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
726  else
727  hw_flags = speed_duplex;
728 
729  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
730  return 0;
731 }
732 
734  .name = "tapcli",
735  .tx_function = tapcli_tx,
736  .format_device_name = format_tapcli_interface_name,
737  .rx_redirect_to_node = tapcli_set_interface_next_node,
738  .name_renumber = tap_name_renumber,
739  .admin_up_down_function = tapcli_interface_admin_up_down,
740  .no_flatten_output_chains = 1,
741 };
742 
743 /**
744  * @brief Dump TAP interfaces
745  *
746  * @param **out_tapids - tapcli_interface_details_t
747  *
748  * @return rc - int
749  *
750  */
752 {
753  tapcli_main_t * tm = &tapcli_main;
754  tapcli_interface_t * ti;
755 
756  tapcli_interface_details_t * r_tapids = NULL;
758 
759  vec_foreach (ti, tm->tapcli_interfaces) {
760  if (!ti->active)
761  continue;
762  vec_add2(r_tapids, tapid, 1);
763  tapid->sw_if_index = ti->sw_if_index;
764  strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
765  }
766 
767  *out_tapids = r_tapids;
768 
769  return 0;
770 }
771 
772 /**
773  * @brief Get tap interface from inactive interfaces or create new
774  *
775  * @return interface - tapcli_interface_t
776  *
777  */
779 {
780  tapcli_main_t * tm = &tapcli_main;
781  tapcli_interface_t *ti = NULL;
782 
783  int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
784  // if there are any inactive ifaces
785  if (inactive_cnt > 0) {
786  // take last
787  u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
788  if (vec_len(tm->tapcli_interfaces) > ti_idx) {
789  ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
790  clib_warning("reusing tap interface");
791  }
792  // "remove" from inactive list
793  _vec_len(tm->tapcli_inactive_interfaces) -= 1;
794  }
795 
796  // ti was not retrieved from inactive ifaces - create new
797  if (!ti)
798  vec_add2 (tm->tapcli_interfaces, ti, 1);
799 
800  return ti;
801 }
802 
803 /**
804  * @brief Connect a TAP interface
805  *
806  * @param vm - vlib_main_t
807  * @param intfc_name - u8
808  * @param hwaddr_arg - u8
809  * @param sw_if_indexp - u32
810  *
811  * @return rc - int
812  *
813  */
814 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
815  u32 * sw_if_indexp)
816 {
817  tapcli_main_t * tm = &tapcli_main;
818  tapcli_interface_t * ti = NULL;
819  struct ifreq ifr;
820  int flags;
821  int dev_net_tun_fd;
822  int dev_tap_fd = -1;
823  clib_error_t * error;
824  u8 hwaddr [6];
825  int rv = 0;
826 
827  if (tm->is_disabled)
828  {
829  return VNET_API_ERROR_FEATURE_DISABLED;
830  }
831 
832  flags = IFF_TAP | IFF_NO_PI;
833 
834  if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
835  return VNET_API_ERROR_SYSCALL_ERROR_1;
836 
837  memset (&ifr, 0, sizeof (ifr));
838  strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
839  ifr.ifr_flags = flags;
840  if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
841  {
842  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
843  goto error;
844  }
845 
846  /* Open a provisioning socket */
847  if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
848  htons(ETH_P_ALL))) < 0 )
849  {
850  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
851  goto error;
852  }
853 
854  /* Find the interface index. */
855  {
856  struct ifreq ifr;
857  struct sockaddr_ll sll;
858 
859  memset (&ifr, 0, sizeof(ifr));
860  strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
861  if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
862  {
863  rv = VNET_API_ERROR_SYSCALL_ERROR_4;
864  goto error;
865  }
866 
867  /* Bind the provisioning socket to the interface. */
868  memset(&sll, 0, sizeof(sll));
869  sll.sll_family = AF_PACKET;
870  sll.sll_ifindex = ifr.ifr_ifindex;
871  sll.sll_protocol = htons(ETH_P_ALL);
872 
873  if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
874  {
875  rv = VNET_API_ERROR_SYSCALL_ERROR_5;
876  goto error;
877  }
878  }
879 
880  /* non-blocking I/O on /dev/tapX */
881  {
882  int one = 1;
883  if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
884  {
885  rv = VNET_API_ERROR_SYSCALL_ERROR_6;
886  goto error;
887  }
888  }
889  ifr.ifr_mtu = tm->mtu_bytes;
890  if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
891  {
892  rv = VNET_API_ERROR_SYSCALL_ERROR_7;
893  goto error;
894  }
895 
896  /* get flags, modify to bring up interface... */
897  if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
898  {
899  rv = VNET_API_ERROR_SYSCALL_ERROR_8;
900  goto error;
901  }
902 
903  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
904 
905  if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
906  {
907  rv = VNET_API_ERROR_SYSCALL_ERROR_9;
908  goto error;
909  }
910 
911  ti = tapcli_get_new_tapif();
912  ti->per_interface_next_index = ~0;
913 
914  if (hwaddr_arg != 0)
915  clib_memcpy(hwaddr, hwaddr_arg, 6);
916  else
917  {
918  f64 now = vlib_time_now(vm);
919  u32 rnd;
920  rnd = (u32) (now * 1e6);
921  rnd = random_u32 (&rnd);
922 
923  memcpy (hwaddr+2, &rnd, sizeof(rnd));
924  hwaddr[0] = 2;
925  hwaddr[1] = 0xfe;
926  }
927 
929  (tm->vnet_main,
930  tapcli_dev_class.index,
931  ti - tm->tapcli_interfaces /* device instance */,
932  hwaddr /* ethernet address */,
933  &ti->hw_if_index,
935 
936  if (error)
937  {
938  clib_error_report (error);
939  rv = VNET_API_ERROR_INVALID_REGISTRATION;
940  goto error;
941  }
942 
943  {
944  unix_file_t template = {0};
945  template.read_function = tapcli_read_ready;
946  template.file_descriptor = dev_net_tun_fd;
947  ti->unix_file_index = unix_file_add (&unix_main, &template);
948  ti->unix_fd = dev_net_tun_fd;
949  ti->provision_fd = dev_tap_fd;
950  clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
951  }
952 
953  {
954  vnet_hw_interface_t * hw;
959  ti->sw_if_index = hw->sw_if_index;
960  if (sw_if_indexp)
961  *sw_if_indexp = hw->sw_if_index;
962  }
963 
964  ti->active = 1;
965 
967  ti - tm->tapcli_interfaces);
968 
970  ti - tm->tapcli_interfaces);
971 
972  return rv;
973 
974  error:
975  close (dev_net_tun_fd);
976  if (dev_tap_fd >= 0)
977  close (dev_tap_fd);
978 
979  return rv;
980 }
981 
982 /**
983  * @brief Renumber a TAP interface
984  *
985  * @param *vm - vlib_main_t
986  * @param *intfc_name - u8
987  * @param *hwaddr_arg - u8
988  * @param *sw_if_indexp - u32
989  * @param renumber - u8
990  * @param custom_dev_instance - u32
991  *
992  * @return rc - int
993  *
994  */
995 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
996  u8 *hwaddr_arg, u32 * sw_if_indexp,
997  u8 renumber, u32 custom_dev_instance)
998 {
999  int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
1000 
1001  if (!rv && renumber)
1002  vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
1003 
1004  return rv;
1005 }
1006 
1007 /**
1008  * @brief Disconnect TAP CLI interface
1009  *
1010  * @param *ti - tapcli_interface_t
1011  *
1012  * @return rc - int
1013  *
1014  */
1016 {
1017  int rv = 0;
1018  vnet_main_t * vnm = vnet_get_main();
1019  tapcli_main_t * tm = &tapcli_main;
1020  u32 sw_if_index = ti->sw_if_index;
1021 
1022  // bring interface down
1023  vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1024 
1025  if (ti->unix_file_index != ~0) {
1027  ti->unix_file_index = ~0;
1028  }
1029  else
1030  close(ti->unix_fd);
1031 
1034  close(ti->provision_fd);
1035  ti->unix_fd = -1;
1036  ti->provision_fd = -1;
1037 
1038  return rv;
1039 }
1040 
1041 /**
1042  * @brief Delete TAP interface
1043  *
1044  * @param *vm - vlib_main_t
1045  * @param sw_if_index - u32
1046  *
1047  * @return rc - int
1048  *
1049  */
1050 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
1051 {
1052  int rv = 0;
1053  tapcli_main_t * tm = &tapcli_main;
1054  tapcli_interface_t *ti;
1055  uword *p = NULL;
1056 
1058  sw_if_index);
1059  if (p == 0) {
1060  clib_warning ("sw_if_index %d unknown", sw_if_index);
1061  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1062  }
1063  ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1064 
1065  // inactive
1066  ti->active = 0;
1068  // add to inactive list
1070 
1071  // reset renumbered iface
1074 
1076  return rv;
1077 }
1078 
1079 /**
1080  * @brief CLI function to delete TAP interface
1081  *
1082  * @param *vm - vlib_main_t
1083  * @param *input - unformat_input_t
1084  * @param *cmd - vlib_cli_command_t
1085  *
1086  * @return error - clib_error_t
1087  *
1088  */
1089 static clib_error_t *
1091  unformat_input_t * input,
1092  vlib_cli_command_t * cmd)
1093 {
1094  tapcli_main_t * tm = &tapcli_main;
1095  u32 sw_if_index = ~0;
1096 
1097  if (tm->is_disabled)
1098  {
1099  return clib_error_return (0, "device disabled...");
1100  }
1101 
1102  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1103  &sw_if_index))
1104  ;
1105  else
1106  return clib_error_return (0, "unknown input `%U'",
1107  format_unformat_error, input);
1108 
1109 
1110  int rc = vnet_tap_delete (vm, sw_if_index);
1111 
1112  if (!rc) {
1113  vlib_cli_output (vm, "Deleted.");
1114  } else {
1115  vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
1116  }
1117 
1118  return 0;
1119 }
1120 
1121 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1122  .path = "tap delete",
1123  .short_help = "tap delete <vpp-tap-intfc-name>",
1124  .function = tap_delete_command_fn,
1125 };
1126 
1127 /**
1128  * @brief Modifies tap interface - can result in new interface being created
1129  *
1130  * @param *vm - vlib_main_t
1131  * @param orig_sw_if_index - u32
1132  * @param *intfc_name - u8
1133  * @param *hwaddr_arg - u8
1134  * @param *sw_if_indexp - u32
1135  * @param renumber - u8
1136  * @param custom_dev_instance - u32
1137  *
1138  * @return rc - int
1139  *
1140  */
1141 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
1142  u8 * intfc_name, u8 *hwaddr_arg,
1143  u32 * sw_if_indexp,
1144  u8 renumber, u32 custom_dev_instance)
1145 {
1146  int rv = vnet_tap_delete (vm, orig_sw_if_index);
1147 
1148  if (rv)
1149  return rv;
1150 
1151  rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
1152  renumber, custom_dev_instance);
1153 
1154  return rv;
1155 }
1156 
1157 /**
1158  * @brief CLI function to modify TAP interface
1159  *
1160  * @param *vm - vlib_main_t
1161  * @param *input - unformat_input_t
1162  * @param *cmd - vlib_cli_command_t
1163  *
1164  * @return error - clib_error_t
1165  *
1166  */
1167 static clib_error_t *
1169  unformat_input_t * input,
1170  vlib_cli_command_t * cmd)
1171 {
1172  u8 * intfc_name;
1173  tapcli_main_t * tm = &tapcli_main;
1174  u32 sw_if_index = ~0;
1175  u32 new_sw_if_index = ~0;
1176  int user_hwaddr = 0;
1177  u8 hwaddr[6];
1178 
1179  if (tm->is_disabled)
1180  {
1181  return clib_error_return (0, "device disabled...");
1182  }
1183 
1184  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1185  &sw_if_index))
1186  ;
1187  else
1188  return clib_error_return (0, "unknown input `%U'",
1189  format_unformat_error, input);
1190 
1191  if (unformat (input, "%s", &intfc_name))
1192  ;
1193  else
1194  return clib_error_return (0, "unknown input `%U'",
1195  format_unformat_error, input);
1196 
1197  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1198  &hwaddr))
1199  user_hwaddr = 1;
1200 
1201 
1202  int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
1203  (user_hwaddr == 1 ? hwaddr : 0),
1204  &new_sw_if_index, 0, 0);
1205 
1206  if (!rc) {
1207  vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1209  new_sw_if_index, intfc_name);
1210  } else {
1211  vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
1212  }
1213 
1214  return 0;
1215 }
1216 
1217 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1218  .path = "tap modify",
1219  .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1220  .function = tap_modify_command_fn,
1221 };
1222 
1223 /**
1224  * @brief CLI function to connect TAP interface
1225  *
1226  * @param *vm - vlib_main_t
1227  * @param *input - unformat_input_t
1228  * @param *cmd - vlib_cli_command_t
1229  *
1230  * @return error - clib_error_t
1231  *
1232  */
1233 static clib_error_t *
1235  unformat_input_t * input,
1236  vlib_cli_command_t * cmd)
1237 {
1238  u8 * intfc_name;
1239  tapcli_main_t * tm = &tapcli_main;
1240  u8 hwaddr[6];
1241  u8 *hwaddr_arg = 0;
1242  u32 sw_if_index;
1243 
1244  if (tm->is_disabled)
1245  {
1246  return clib_error_return (0, "device disabled...");
1247  }
1248 
1249  if (unformat (input, "%s", &intfc_name))
1250  ;
1251  else
1252  return clib_error_return (0, "unknown input `%U'",
1253  format_unformat_error, input);
1254 
1255  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1256  &hwaddr))
1257  hwaddr_arg = hwaddr;
1258 
1259  /* It is here for backward compatibility */
1260  if (unformat(input, "hwaddr random"))
1261  ;
1262 
1263  int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
1264  if (rv) {
1265  switch (rv) {
1266  case VNET_API_ERROR_SYSCALL_ERROR_1:
1267  vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1268  break;
1269 
1270  case VNET_API_ERROR_SYSCALL_ERROR_2:
1271  vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1272  break;
1273 
1274  case VNET_API_ERROR_SYSCALL_ERROR_3:
1275  vlib_cli_output (vm, "Couldn't open provisioning socket");
1276  break;
1277 
1278  case VNET_API_ERROR_SYSCALL_ERROR_4:
1279  vlib_cli_output (vm, "Couldn't get if_index");
1280  break;
1281 
1282  case VNET_API_ERROR_SYSCALL_ERROR_5:
1283  vlib_cli_output (vm, "Couldn't bind provisioning socket");
1284  break;
1285 
1286  case VNET_API_ERROR_SYSCALL_ERROR_6:
1287  vlib_cli_output (0, "Couldn't set device non-blocking flag");
1288  break;
1289 
1290  case VNET_API_ERROR_SYSCALL_ERROR_7:
1291  vlib_cli_output (0, "Couldn't set device MTU");
1292  break;
1293 
1294  case VNET_API_ERROR_SYSCALL_ERROR_8:
1295  vlib_cli_output (0, "Couldn't get interface flags");
1296  break;
1297 
1298  case VNET_API_ERROR_SYSCALL_ERROR_9:
1299  vlib_cli_output (0, "Couldn't set intfc admin state up");
1300  break;
1301 
1302  case VNET_API_ERROR_INVALID_REGISTRATION:
1303  vlib_cli_output (0, "Invalid registration");
1304  break;
1305  default:
1306  vlib_cli_output (0, "Unknown error: %d", rv);
1307  break;
1308  }
1309  return 0;
1310  }
1311 
1312  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
1313  return 0;
1314  }
1315 
1316 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1317  .path = "tap connect",
1318  .short_help = "tap connect <intfc-name> [hwaddr <addr>]",
1319  .function = tap_connect_command_fn,
1320 };
1321 
1322 /**
1323  * @brief TAPCLI main init
1324  *
1325  * @param *vm - vlib_main_t
1326  *
1327  * @return error - clib_error_t
1328  *
1329  */
1330 clib_error_t *
1332 {
1333  tapcli_main_t * tm = &tapcli_main;
1334 
1335  tm->vlib_main = vm;
1336  tm->vnet_main = vnet_get_main();
1337  tm->unix_main = &unix_main;
1338  tm->mtu_bytes = TAP_MTU_DEFAULT;
1341  tm->rx_buffers = 0;
1345  return 0;
1346 }
1347 
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:1141
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
#define VNET_HW_INTERFACE_FLAG_SPEED_1G
Definition: interface.h:361
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:522
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:254
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:72
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:402
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:1090
#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
#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:1177
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:348
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:419
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:1063
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:46
#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:1168
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:577
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:778
u32 unix_file_index
Definition: tapcli.c:60
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:97
u32 max_supported_packet_bytes
Definition: interface.h:406
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:43
#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:626
#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
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:121
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
#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:1015
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:64
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:99
#define ETHERNET_INTERFACE_FLAG_MTU
Definition: ethernet.h:118
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:113
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:600
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:306
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
Definition: interface.h:352
u16 cached_next_index
Definition: node.h:463
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:490
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:420
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:245
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:333
#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:117
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:205
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:270
int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
Delete TAP interface.
Definition: tapcli.c:1050
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:717
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:112
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
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:253
Definition: unix.h:49
a point 2 point interface
Definition: interface.h:246
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:814
#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:403
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
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:995
clib_error_t * tapcli_init(vlib_main_t *vm)
TAPCLI main init.
Definition: tapcli.c:1331
u8 data[0]
Packet data.
Definition: buffer.h:154
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:530
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:75
#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:751
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:686
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:1234