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