FD.io VPP  v18.04-17-g3a0d853
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_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 = vlib_get_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 = vlib_get_thread_index ();
275  vnet_main_t *vnm;
277  u8 admin_down;
278  u32 next = node->cached_next_index;
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) +=
301  &tm->threads[thread_index].
302  rx_buffers[len],
303  VLIB_FRAME_SIZE - len,
305  if (PREDICT_FALSE
306  (vec_len (tm->threads[thread_index].rx_buffers) <
307  tm->mtu_buffers))
308  {
310  TAPCLI_ERROR_BUFFER_ALLOC,
311  tm->mtu_buffers -
312  vec_len (tm->threads
313  [thread_index].
314  rx_buffers));
315  break;
316  }
317  }
318 
319  uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
320 
321  /* Allocate RX buffers from end of rx_buffers.
322  Turn them into iovecs to pass to readv. */
323  vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
324  for (j = 0; j < tm->mtu_buffers; j++)
325  {
326  b =
327  vlib_get_buffer (vm,
328  tm->threads[thread_index].rx_buffers[i_rx - j]);
329  tm->threads[thread_index].iovecs[j].iov_base = b->data;
330  tm->threads[thread_index].iovecs[j].iov_len = buffer_size;
331  }
332 
333  n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs,
334  tm->mtu_buffers);
335  n_bytes_in_packet = n_bytes_left;
336  if (n_bytes_left <= 0)
337  {
338  if (errno != EAGAIN)
339  {
341  TAPCLI_ERROR_READ, 1);
342  }
343  break;
344  }
345 
346  bi_first = tm->threads[thread_index].rx_buffers[i_rx];
347  b = b_first = vlib_get_buffer (vm,
348  tm->threads[thread_index].
349  rx_buffers[i_rx]);
350  prev = NULL;
351 
352  while (1)
353  {
354  b->current_length =
355  n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
356  n_bytes_left -= buffer_size;
357 
358  if (prev)
359  {
360  prev->next_buffer = bi;
361  prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
362  }
363  prev = b;
364 
365  /* last segment */
366  if (n_bytes_left <= 0)
367  break;
368 
369  i_rx--;
370  bi = tm->threads[thread_index].rx_buffers[i_rx];
371  b = vlib_get_buffer (vm, bi);
372  }
373 
374  _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
375 
377  (n_bytes_in_packet >
378  buffer_size) ? n_bytes_in_packet - buffer_size : 0;
379  b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
380 
382 
383  vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
384  vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32) ~ 0;
385 
386  b_first->error = node->errors[TAPCLI_ERROR_NONE];
388  next_index = (ti->per_interface_next_index != ~0) ?
389  ti->per_interface_next_index : next_index;
390  next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index;
391 
392  to_next[0] = bi_first;
393  to_next++;
394  n_left_to_next--;
395 
397  b_first);
398 
399  vlib_validate_buffer_enqueue_x1 (vm, node, next,
400  to_next, n_left_to_next,
401  bi_first, next_index);
402 
403  /* Interface counters for tapcli interface. */
404  if (PREDICT_TRUE (!admin_down))
405  {
407  combined_sw_if_counters +
409  thread_index, ti->sw_if_index, 1,
410  n_bytes_in_packet);
411 
412  if (PREDICT_FALSE (n_trace > 0))
413  {
414  vlib_trace_buffer (vm, node, next_index,
415  b_first, /* follow_chain */ 1);
416  n_trace--;
417  set_trace = 1;
418  tapcli_rx_trace_t *t0 =
419  vlib_add_trace (vm, node, b_first, sizeof (*t0));
420  t0->sw_if_index = si->sw_if_index;
421  }
422  }
423  }
424  vlib_put_next_frame (vm, node, next, n_left_to_next);
425  if (set_trace)
426  vlib_set_trace_count (vm, node, n_trace);
427  return VLIB_FRAME_SIZE - n_left_to_next;
428 }
429 
430 /**
431  * @brief tapcli RX node function
432  * @node tap-cli-rx
433  *
434  * Input node from the Kernel tun/tap device
435  *
436  * @param *vm - vlib_main_t
437  * @param *node - vlib_node_runtime_t
438  * @param *frame - vlib_frame_t
439  *
440  * @return n_packets - uword
441  *
442  */
443 static uword
445 {
446  tapcli_main_t *tm = &tapcli_main;
447  static u32 *ready_interface_indices;
448  tapcli_interface_t *ti;
449  int i;
450  u32 total_count = 0;
451 
452  vec_reset_length (ready_interface_indices);
453  /* *INDENT-OFF* */
455  ({
456  vec_add1 (ready_interface_indices, i);
457  }));
458  /* *INDENT-ON* */
459 
460  if (vec_len (ready_interface_indices) == 0)
461  return 0;
462 
463  for (i = 0; i < vec_len (ready_interface_indices); i++)
464  {
465  tm->pending_read_bitmap =
467  ready_interface_indices[i], 0);
468 
469  ti =
470  vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
471  total_count += tapcli_rx_iface (vm, node, ti);
472  }
473  return total_count; //This might return more than 256.
474 }
475 
476 /** TAPCLI error strings */
477 static char *tapcli_rx_error_strings[] = {
478 #define _(sym,string) string,
480 #undef _
481 };
482 
483 /* *INDENT-OFF* */
485  .function = tapcli_rx,
486  .name = "tapcli-rx",
487  .sibling_of = "device-input",
488  .type = VLIB_NODE_TYPE_INPUT,
489  .state = VLIB_NODE_STATE_INTERRUPT,
490  .vector_size = 4,
491  .n_errors = TAPCLI_N_ERROR,
492  .error_strings = tapcli_rx_error_strings,
493  .format_trace = format_tapcli_rx_trace,
494 };
495 /* *INDENT-ON* */
496 
497 
498 /**
499  * @brief Gets called when file descriptor is ready from epoll.
500  *
501  * @param *uf - clib_file_t
502  *
503  * @return error - clib_error_t
504  *
505  */
506 static clib_error_t *
508 {
510  tapcli_main_t *tm = &tapcli_main;
511  uword *p;
512 
513  /** Schedule the rx node */
515 
517 
518  /** Mark the specific tap interface ready-to-read */
519  if (p)
521  p[0], 1);
522  else
523  clib_warning ("fd %d not in hash table", uf->file_descriptor);
524 
525  return 0;
526 }
527 
528 /**
529  * @brief CLI function for TAPCLI configuration
530  *
531  * @param *vm - vlib_main_t
532  * @param *input - unformat_input_t
533  *
534  * @return error - clib_error_t
535  *
536  */
537 static clib_error_t *
539 {
540  tapcli_main_t *tm = &tapcli_main;
541  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
542 
544  {
545  if (unformat (input, "mtu %d", &tm->mtu_bytes))
546  ;
547  else if (unformat (input, "disable"))
548  tm->is_disabled = 1;
549  else
550  return clib_error_return (0, "unknown input `%U'",
551  format_unformat_error, input);
552  }
553 
554  if (tm->is_disabled)
555  return 0;
556 
557  if (geteuid ())
558  {
559  clib_warning ("tapcli disabled: must be superuser");
560  tm->is_disabled = 1;
561  return 0;
562  }
563 
564  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
565 
566  return 0;
567 }
568 
569 /**
570  * @brief Renumber TAPCLI interface
571  *
572  * @param *hi - vnet_hw_interface_t
573  * @param new_dev_instance - u32
574  *
575  * @return rc - int
576  *
577  */
578 static int
580 {
581  tapcli_main_t *tm = &tapcli_main;
582 
584  hi->dev_instance, ~0);
585 
587  new_dev_instance;
588 
589  return 0;
590 }
591 
593 
594 /**
595  * @brief Free "no punt" frame
596  *
597  * @param *vm - vlib_main_t
598  * @param *node - vlib_node_runtime_t
599  * @param *frame - vlib_frame_t
600  *
601  */
602 static void
604  vlib_node_runtime_t * node, vlib_frame_t * frame)
605 {
606  u32 *buffers = vlib_frame_args (frame);
607  uword n_packets = frame->n_vectors;
608  vlib_buffer_free (vm, buffers, n_packets);
609  vlib_frame_free (vm, node, frame);
610 }
611 
612 /* *INDENT-OFF* */
614  .name = "tapcli",
616 };
617 /* *INDENT-ON* */
618 
619 /**
620  * @brief Formatter for TAPCLI interface name
621  *
622  * @param *s - formatter string
623  * @param *args - va_list
624  *
625  * @return *s - formatted string
626  *
627  */
628 static u8 *
629 format_tapcli_interface_name (u8 * s, va_list * args)
630 {
631  u32 i = va_arg (*args, u32);
632  u32 show_dev_instance = ~0;
633  tapcli_main_t *tm = &tapcli_main;
634 
636  show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
637 
638  if (show_dev_instance != ~0)
639  i = show_dev_instance;
640 
641  s = format (s, "tapcli-%d", i);
642  return s;
643 }
644 
645 /**
646  * @brief Modify interface flags for TAPCLI interface
647  *
648  * @param *vnm - vnet_main_t
649  * @param *hw - vnet_hw_interface_t
650  * @param flags - u32
651  *
652  * @return rc - u32
653  *
654  */
655 static u32
657 {
658  tapcli_main_t *tm = &tapcli_main;
659  tapcli_interface_t *ti;
660 
662 
663  if (flags & ETHERNET_INTERFACE_FLAG_MTU)
664  {
665  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
666  tm->mtu_bytes = hw->max_packet_bytes;
667  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
668  }
669  else
670  {
671  struct ifreq ifr;
672  u32 want_promisc;
673 
674  memcpy (&ifr, &ti->ifr, sizeof (ifr));
675 
676  /* get flags, modify to bring up interface... */
677  if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
678  {
679  clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
680  return 0;
681  }
682 
683  want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
684 
685  if (want_promisc == ti->is_promisc)
686  return 0;
687 
689  ifr.ifr_flags |= IFF_PROMISC;
690  else
691  ifr.ifr_flags &= ~(IFF_PROMISC);
692 
693  /* get flags, modify to bring up interface... */
694  if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
695  {
696  clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
697  return 0;
698  }
699 
700  ti->is_promisc = want_promisc;
701  }
702 
703  return 0;
704 }
705 
706 /**
707  * @brief Setting the TAP interface's next processing node
708  *
709  * @param *vnm - vnet_main_t
710  * @param hw_if_index - u32
711  * @param node_index - u32
712  *
713  */
714 static void
716  u32 hw_if_index, u32 node_index)
717 {
718  tapcli_main_t *tm = &tapcli_main;
719  tapcli_interface_t *ti;
720  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
721 
723 
724  /** Shut off redirection */
725  if (node_index == ~0)
726  {
727  ti->per_interface_next_index = node_index;
728  return;
729  }
730 
732  vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
733 }
734 
735 /**
736  * @brief Set link_state == admin_state otherwise things like ip6 neighbor discovery breaks
737  *
738  * @param *vnm - vnet_main_t
739  * @param hw_if_index - u32
740  * @param flags - u32
741  *
742  * @return error - clib_error_t
743  */
744 static clib_error_t *
746 {
747  uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
748  u32 hw_flags;
751 
752  if (is_admin_up)
753  hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
754  else
755  hw_flags = speed_duplex;
756 
757  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
758  return 0;
759 }
760 
761 /* *INDENT-OFF* */
763  .name = "tapcli",
764  .tx_function = tapcli_tx,
765  .format_device_name = format_tapcli_interface_name,
766  .rx_redirect_to_node = tapcli_set_interface_next_node,
767  .name_renumber = tap_name_renumber,
768  .admin_up_down_function = tapcli_interface_admin_up_down,
769 };
770 /* *INDENT-ON* */
771 
772 /**
773  * @brief Dump TAP interfaces
774  *
775  * @param **out_tapids - tapcli_interface_details_t
776  *
777  * @return rc - int
778  *
779  */
780 int
782 {
783  tapcli_main_t *tm = &tapcli_main;
784  tapcli_interface_t *ti;
785 
786  tapcli_interface_details_t *r_tapids = NULL;
788 
790  {
791  if (!ti->active)
792  continue;
793  vec_add2 (r_tapids, tapid, 1);
794  tapid->sw_if_index = ti->sw_if_index;
795  strncpy ((char *) tapid->dev_name, ti->ifr.ifr_name,
796  sizeof (ti->ifr.ifr_name) - 1);
797  }
798 
799  *out_tapids = r_tapids;
800 
801  return 0;
802 }
803 
804 /**
805  * @brief Get tap interface from inactive interfaces or create new
806  *
807  * @return interface - tapcli_interface_t
808  *
809  */
810 static tapcli_interface_t *
812 {
813  tapcli_main_t *tm = &tapcli_main;
814  tapcli_interface_t *ti = NULL;
815 
816  int inactive_cnt = vec_len (tm->tapcli_inactive_interfaces);
817  // if there are any inactive ifaces
818  if (inactive_cnt > 0)
819  {
820  // take last
821  u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
822  if (vec_len (tm->tapcli_interfaces) > ti_idx)
823  {
824  ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
825  clib_warning ("reusing tap interface");
826  }
827  // "remove" from inactive list
828  _vec_len (tm->tapcli_inactive_interfaces) -= 1;
829  }
830 
831  // ti was not retrieved from inactive ifaces - create new
832  if (!ti)
833  vec_add2 (tm->tapcli_interfaces, ti, 1);
834 
835  return ti;
836 }
837 
838 typedef struct
839 {
842  unsigned int ifindex;
843 } ip6_ifreq_t;
844 
845 /**
846  * @brief Connect a TAP interface
847  *
848  * @param vm - vlib_main_t
849  * @param ap - vnet_tap_connect_args_t
850  *
851  * @return rc - int
852  *
853  */
854 int
856 {
857  tapcli_main_t *tm = &tapcli_main;
858  tapcli_interface_t *ti = NULL;
859  struct ifreq ifr;
860  int flags;
861  int dev_net_tun_fd;
862  int dev_tap_fd = -1;
863  clib_error_t *error;
864  u8 hwaddr[6];
865  int rv = 0;
866 
867  if (tm->is_disabled)
868  {
869  return VNET_API_ERROR_FEATURE_DISABLED;
870  }
871 
872  flags = IFF_TAP | IFF_NO_PI;
873 
874  if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
875  return VNET_API_ERROR_SYSCALL_ERROR_1;
876 
877  memset (&ifr, 0, sizeof (ifr));
878  strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name) - 1);
879  ifr.ifr_flags = flags;
880  if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
881  {
882  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
883  goto error;
884  }
885 
886  /* Open a provisioning socket */
887  if ((dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
888  {
889  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
890  goto error;
891  }
892 
893  /* Find the interface index. */
894  {
895  struct ifreq ifr;
896  struct sockaddr_ll sll;
897 
898  memset (&ifr, 0, sizeof (ifr));
899  strncpy (ifr.ifr_name, (char *) ap->intfc_name,
900  sizeof (ifr.ifr_name) - 1);
901  if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
902  {
903  rv = VNET_API_ERROR_SYSCALL_ERROR_4;
904  goto error;
905  }
906 
907  /* Bind the provisioning socket to the interface. */
908  memset (&sll, 0, sizeof (sll));
909  sll.sll_family = AF_PACKET;
910  sll.sll_ifindex = ifr.ifr_ifindex;
911  sll.sll_protocol = htons (ETH_P_ALL);
912 
913  if (bind (dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
914  {
915  rv = VNET_API_ERROR_SYSCALL_ERROR_5;
916  goto error;
917  }
918  }
919 
920  /* non-blocking I/O on /dev/tapX */
921  {
922  int one = 1;
923  if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
924  {
925  rv = VNET_API_ERROR_SYSCALL_ERROR_6;
926  goto error;
927  }
928  }
929  ifr.ifr_mtu = tm->mtu_bytes;
930  if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
931  {
932  rv = VNET_API_ERROR_SYSCALL_ERROR_7;
933  goto error;
934  }
935 
936  /* get flags, modify to bring up interface... */
937  if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
938  {
939  rv = VNET_API_ERROR_SYSCALL_ERROR_8;
940  goto error;
941  }
942 
943  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
944 
945  if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
946  {
947  rv = VNET_API_ERROR_SYSCALL_ERROR_9;
948  goto error;
949  }
950 
951  if (ap->ip4_address_set)
952  {
953  struct sockaddr_in sin;
954  /* ip4: mask defaults to /24 */
955  u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
956 
957  memset (&sin, 0, sizeof (sin));
958  sin.sin_family = AF_INET;
959  /* sin.sin_port = 0; */
960  sin.sin_addr.s_addr = ap->ip4_address->as_u32;
961  memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
962 
963  if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
964  {
965  rv = VNET_API_ERROR_SYSCALL_ERROR_10;
966  goto error;
967  }
968 
969  if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
970  {
971  mask = ~0;
972  mask <<= (32 - ap->ip4_mask_width);
973  }
974 
975  mask = clib_host_to_net_u32 (mask);
976  sin.sin_family = AF_INET;
977  sin.sin_port = 0;
978  sin.sin_addr.s_addr = mask;
979  memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
980 
981  if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
982  {
983  rv = VNET_API_ERROR_SYSCALL_ERROR_10;
984  goto error;
985  }
986  }
987 
988  if (ap->ip6_address_set)
989  {
990  struct ifreq ifr2;
991  ip6_ifreq_t ifr6;
992  int sockfd6;
993 
994  sockfd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_IP);
995  if (sockfd6 < 0)
996  {
997  rv = VNET_API_ERROR_SYSCALL_ERROR_10;
998  goto error;
999  }
1000 
1001  memset (&ifr2, 0, sizeof (ifr));
1002  strncpy (ifr2.ifr_name, (char *) ap->intfc_name,
1003  sizeof (ifr2.ifr_name) - 1);
1004  if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0)
1005  {
1006  close (sockfd6);
1007  rv = VNET_API_ERROR_SYSCALL_ERROR_4;
1008  goto error;
1009  }
1010 
1011  memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
1012  ifr6.mask_width = ap->ip6_mask_width;
1013  ifr6.ifindex = ifr2.ifr_ifindex;
1014 
1015  if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
1016  {
1017  close (sockfd6);
1018  clib_unix_warning ("ifr6");
1019  rv = VNET_API_ERROR_SYSCALL_ERROR_10;
1020  goto error;
1021  }
1022  close (sockfd6);
1023  }
1024 
1025  ti = tapcli_get_new_tapif ();
1026  ti->per_interface_next_index = ~0;
1027 
1028  if (ap->hwaddr_arg != 0)
1029  clib_memcpy (hwaddr, ap->hwaddr_arg, 6);
1030  else
1031  {
1032  f64 now = vlib_time_now (vm);
1033  u32 rnd;
1034  rnd = (u32) (now * 1e6);
1035  rnd = random_u32 (&rnd);
1036 
1037  memcpy (hwaddr + 2, &rnd, sizeof (rnd));
1038  hwaddr[0] = 2;
1039  hwaddr[1] = 0xfe;
1040  }
1041 
1043  (tm->vnet_main,
1044  tapcli_dev_class.index,
1045  ti - tm->tapcli_interfaces /* device instance */ ,
1046  hwaddr /* ethernet address */ ,
1048 
1049  if (error)
1050  {
1051  clib_error_report (error);
1052  rv = VNET_API_ERROR_INVALID_REGISTRATION;
1053  goto error;
1054  }
1055 
1056  {
1057  clib_file_t template = { 0 };
1058  template.read_function = tapcli_read_ready;
1059  template.file_descriptor = dev_net_tun_fd;
1060  ti->clib_file_index = clib_file_add (&file_main, &template);
1061  ti->unix_fd = dev_net_tun_fd;
1062  ti->provision_fd = dev_tap_fd;
1063  clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
1064  }
1065 
1066  {
1067  vnet_hw_interface_t *hw;
1073  ti->sw_if_index = hw->sw_if_index;
1074  if (ap->sw_if_indexp)
1075  *(ap->sw_if_indexp) = hw->sw_if_index;
1076  }
1077 
1078  ti->active = 1;
1079 
1081  ti - tm->tapcli_interfaces);
1082 
1084  ti - tm->tapcli_interfaces);
1085 
1086  return rv;
1087 
1088 error:
1089  close (dev_net_tun_fd);
1090  if (dev_tap_fd >= 0)
1091  close (dev_tap_fd);
1092 
1093  return rv;
1094 }
1095 
1096 /**
1097  * @brief Renumber a TAP interface
1098  *
1099  * @param *vm - vlib_main_t
1100  * @param *intfc_name - u8
1101  * @param *hwaddr_arg - u8
1102  * @param *sw_if_indexp - u32
1103  * @param renumber - u8
1104  * @param custom_dev_instance - u32
1105  *
1106  * @return rc - int
1107  *
1108  */
1109 int
1111 {
1112  int rv = vnet_tap_connect (vm, ap);
1113 
1114  if (!rv && ap->renumber)
1116  ap->custom_dev_instance);
1117 
1118  return rv;
1119 }
1120 
1121 /**
1122  * @brief Disconnect TAP CLI interface
1123  *
1124  * @param *ti - tapcli_interface_t
1125  *
1126  * @return rc - int
1127  *
1128  */
1129 static int
1131 {
1132  int rv = 0;
1133  vnet_main_t *vnm = vnet_get_main ();
1134  tapcli_main_t *tm = &tapcli_main;
1135  u32 sw_if_index = ti->sw_if_index;
1136 
1137  // bring interface down
1138  vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
1139 
1140  if (ti->clib_file_index != ~0)
1141  {
1143  ti->clib_file_index = ~0;
1144  }
1145  else
1146  close (ti->unix_fd);
1147 
1150  close (ti->provision_fd);
1151  ti->unix_fd = -1;
1152  ti->provision_fd = -1;
1153 
1154  return rv;
1155 }
1156 
1157 /**
1158  * @brief Delete TAP interface
1159  *
1160  * @param *vm - vlib_main_t
1161  * @param sw_if_index - u32
1162  *
1163  * @return rc - int
1164  *
1165  */
1166 int
1168 {
1169  int rv = 0;
1170  tapcli_main_t *tm = &tapcli_main;
1171  tapcli_interface_t *ti;
1172  uword *p = NULL;
1173 
1174  p = hash_get (tm->tapcli_interface_index_by_sw_if_index, sw_if_index);
1175  if (p == 0)
1176  {
1177  clib_warning ("sw_if_index %d unknown", sw_if_index);
1178  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1179  }
1180  ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
1181 
1182  // inactive
1183  ti->active = 0;
1184  tapcli_tap_disconnect (ti);
1185  // add to inactive list
1187 
1188  // reset renumbered iface
1191 
1193  return rv;
1194 }
1195 
1196 /**
1197  * @brief CLI function to delete TAP interface
1198  *
1199  * @param *vm - vlib_main_t
1200  * @param *input - unformat_input_t
1201  * @param *cmd - vlib_cli_command_t
1202  *
1203  * @return error - clib_error_t
1204  *
1205  */
1206 static clib_error_t *
1208  unformat_input_t * input, vlib_cli_command_t * cmd)
1209 {
1210  tapcli_main_t *tm = &tapcli_main;
1211  u32 sw_if_index = ~0;
1212 
1213  if (tm->is_disabled)
1214  {
1215  return clib_error_return (0, "device disabled...");
1216  }
1217 
1218  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1219  &sw_if_index))
1220  ;
1221  else
1222  return clib_error_return (0, "unknown input `%U'",
1223  format_unformat_error, input);
1224 
1225 
1226  int rc = vnet_tap_delete (vm, sw_if_index);
1227 
1228  if (!rc)
1229  {
1230  vlib_cli_output (vm, "Deleted.");
1231  }
1232  else
1233  {
1234  vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)",
1235  rc);
1236  }
1237 
1238  return 0;
1239 }
1240 
1241 /* *INDENT-OFF* */
1242 VLIB_CLI_COMMAND (tap_delete_command, static) = {
1243  .path = "tap delete",
1244  .short_help = "tap delete <vpp-tap-intfc-name>",
1245  .function = tap_delete_command_fn,
1246 };
1247 /* *INDENT-ON* */
1248 
1249 /**
1250  * @brief Modifies tap interface - can result in new interface being created
1251  *
1252  * @param *vm - vlib_main_t
1253  * @param orig_sw_if_index - u32
1254  * @param *intfc_name - u8
1255  * @param *hwaddr_arg - u8
1256  * @param *sw_if_indexp - u32
1257  * @param renumber - u8
1258  * @param custom_dev_instance - u32
1259  *
1260  * @return rc - int
1261  *
1262  */
1263 int
1265 {
1266  int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
1267 
1268  if (rv)
1269  return rv;
1270 
1271  rv = vnet_tap_connect_renumber (vm, ap);
1272 
1273  return rv;
1274 }
1275 
1276 /**
1277  * @brief CLI function to modify TAP interface
1278  *
1279  * @param *vm - vlib_main_t
1280  * @param *input - unformat_input_t
1281  * @param *cmd - vlib_cli_command_t
1282  *
1283  * @return error - clib_error_t
1284  *
1285  */
1286 static clib_error_t *
1288  unformat_input_t * input, vlib_cli_command_t * cmd)
1289 {
1290  u8 *intfc_name;
1291  tapcli_main_t *tm = &tapcli_main;
1292  u32 sw_if_index = ~0;
1293  u32 new_sw_if_index = ~0;
1294  int user_hwaddr = 0;
1295  u8 hwaddr[6];
1296  vnet_tap_connect_args_t _a, *ap = &_a;
1297 
1298  if (tm->is_disabled)
1299  {
1300  return clib_error_return (0, "device disabled...");
1301  }
1302 
1303  if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
1304  &sw_if_index))
1305  ;
1306  else
1307  return clib_error_return (0, "unknown input `%U'",
1308  format_unformat_error, input);
1309 
1310  if (unformat (input, "%s", &intfc_name))
1311  ;
1312  else
1313  return clib_error_return (0, "unknown input `%U'",
1314  format_unformat_error, input);
1315 
1316  if (unformat (input, "hwaddr %U", unformat_ethernet_address, &hwaddr))
1317  user_hwaddr = 1;
1318 
1319 
1320  memset (ap, 0, sizeof (*ap));
1321  ap->orig_sw_if_index = sw_if_index;
1322  ap->intfc_name = intfc_name;
1323  ap->sw_if_indexp = &new_sw_if_index;
1324  if (user_hwaddr)
1325  ap->hwaddr_arg = hwaddr;
1326 
1327  int rc = vnet_tap_modify (vm, ap);
1328 
1329  if (!rc)
1330  {
1331  vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
1333  *(ap->sw_if_indexp), ap->intfc_name);
1334  }
1335  else
1336  {
1337  vlib_cli_output (vm,
1338  "Error during modification of tap interface. (rc: %d)",
1339  rc);
1340  }
1341 
1342  return 0;
1343 }
1344 
1345 /* *INDENT-OFF* */
1346 VLIB_CLI_COMMAND (tap_modify_command, static) = {
1347  .path = "tap modify",
1348  .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr <addr>]",
1349  .function = tap_modify_command_fn,
1350 };
1351 /* *INDENT-ON* */
1352 
1353 /**
1354  * @brief CLI function to connect TAP interface
1355  *
1356  * @param *vm - vlib_main_t
1357  * @param *input - unformat_input_t
1358  * @param *cmd - vlib_cli_command_t
1359  *
1360  * @return error - clib_error_t
1361  *
1362  */
1363 static clib_error_t *
1365  unformat_input_t * input, vlib_cli_command_t * cmd)
1366 {
1367  u8 *intfc_name = 0;
1368  unformat_input_t _line_input, *line_input = &_line_input;
1369  vnet_tap_connect_args_t _a, *ap = &_a;
1370  tapcli_main_t *tm = &tapcli_main;
1371  u8 hwaddr[6];
1372  u8 *hwaddr_arg = 0;
1373  u32 sw_if_index;
1374  ip4_address_t ip4_address;
1375  int ip4_address_set = 0;
1376  ip6_address_t ip6_address;
1377  int ip6_address_set = 0;
1378  u32 ip4_mask_width = 0;
1379  u32 ip6_mask_width = 0;
1380  clib_error_t *error = NULL;
1381 
1382  if (tm->is_disabled)
1383  return clib_error_return (0, "device disabled...");
1384 
1385  if (!unformat_user (input, unformat_line_input, line_input))
1386  return 0;
1387 
1388  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1389  {
1390  if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1391  &hwaddr))
1392  hwaddr_arg = hwaddr;
1393 
1394  /* It is here for backward compatibility */
1395  else if (unformat (line_input, "hwaddr random"))
1396  ;
1397 
1398  else if (unformat (line_input, "address %U/%d",
1399  unformat_ip4_address, &ip4_address, &ip4_mask_width))
1400  ip4_address_set = 1;
1401 
1402  else if (unformat (line_input, "address %U/%d",
1403  unformat_ip6_address, &ip6_address, &ip6_mask_width))
1404  ip6_address_set = 1;
1405 
1406  else if (unformat (line_input, "%s", &intfc_name))
1407  ;
1408  else
1409  {
1410  error = clib_error_return (0, "unknown input `%U'",
1411  format_unformat_error, line_input);
1412  goto done;
1413  }
1414  }
1415 
1416  if (intfc_name == 0)
1417  {
1418  error = clib_error_return (0, "interface name must be specified");
1419  goto done;
1420  }
1421 
1422  memset (ap, 0, sizeof (*ap));
1423 
1424  ap->intfc_name = intfc_name;
1425  ap->hwaddr_arg = hwaddr_arg;
1426  if (ip4_address_set)
1427  {
1428  ap->ip4_address = &ip4_address;
1429  ap->ip4_mask_width = ip4_mask_width;
1430  ap->ip4_address_set = 1;
1431  }
1432  if (ip6_address_set)
1433  {
1434  ap->ip6_address = &ip6_address;
1435  ap->ip6_mask_width = ip6_mask_width;
1436  ap->ip6_address_set = 1;
1437  }
1438 
1439  ap->sw_if_indexp = &sw_if_index;
1440 
1441  int rv = vnet_tap_connect (vm, ap);
1442 
1443  switch (rv)
1444  {
1445  case VNET_API_ERROR_SYSCALL_ERROR_1:
1446  error = clib_error_return (0, "Couldn't open /dev/net/tun");
1447  goto done;
1448 
1449  case VNET_API_ERROR_SYSCALL_ERROR_2:
1450  error =
1451  clib_error_return (0, "Error setting flags on '%s'", intfc_name);
1452  goto done;
1453 
1454  case VNET_API_ERROR_SYSCALL_ERROR_3:
1455  error = clib_error_return (0, "Couldn't open provisioning socket");
1456  goto done;
1457 
1458  case VNET_API_ERROR_SYSCALL_ERROR_4:
1459  error = clib_error_return (0, "Couldn't get if_index");
1460  goto done;
1461 
1462  case VNET_API_ERROR_SYSCALL_ERROR_5:
1463  error = clib_error_return (0, "Couldn't bind provisioning socket");
1464  goto done;
1465 
1466  case VNET_API_ERROR_SYSCALL_ERROR_6:
1467  error = clib_error_return (0, "Couldn't set device non-blocking flag");
1468  goto done;
1469 
1470  case VNET_API_ERROR_SYSCALL_ERROR_7:
1471  error = clib_error_return (0, "Couldn't set device MTU");
1472  goto done;
1473 
1474  case VNET_API_ERROR_SYSCALL_ERROR_8:
1475  error = clib_error_return (0, "Couldn't get interface flags");
1476  goto done;
1477 
1478  case VNET_API_ERROR_SYSCALL_ERROR_9:
1479  error = clib_error_return (0, "Couldn't set intfc admin state up");
1480  goto done;
1481 
1482  case VNET_API_ERROR_SYSCALL_ERROR_10:
1483  error = clib_error_return (0, "Couldn't set intfc address/mask");
1484  goto done;
1485 
1486  case VNET_API_ERROR_INVALID_REGISTRATION:
1487  error = clib_error_return (0, "Invalid registration");
1488  goto done;
1489 
1490  case 0:
1491  break;
1492 
1493  default:
1494  error = clib_error_return (0, "Unknown error: %d", rv);
1495  goto done;
1496  }
1497 
1499  vnet_get_main (), sw_if_index);
1500 
1501 done:
1502  unformat_free (line_input);
1503 
1504  return error;
1505 }
1506 
1507 /* *INDENT-OFF* */
1508 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1509  .path = "tap connect",
1510  .short_help =
1511  "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]",
1512  .function = tap_connect_command_fn,
1513 };
1514 /* *INDENT-ON* */
1515 
1516 /**
1517  * @brief TAPCLI main init
1518  *
1519  * @param *vm - vlib_main_t
1520  *
1521  * @return error - clib_error_t
1522  *
1523  */
1524 clib_error_t *
1526 {
1527  tapcli_main_t *tm = &tapcli_main;
1530 
1531  tm->vlib_main = vm;
1532  tm->vnet_main = vnet_get_main ();
1533  tm->mtu_bytes = TAP_MTU_DEFAULT;
1539  vec_foreach (thread, tm->threads)
1540  {
1541  thread->iovecs = 0;
1542  thread->rx_buffers = 0;
1544  vec_reset_length (thread->rx_buffers);
1545  }
1546 
1547  return 0;
1548 }
1549 
1551 
1552 /*
1553  * fd.io coding-style-patch-verification: ON
1554  *
1555  * Local Variables:
1556  * eval: (c-set-style "gnu")
1557  * End:
1558  */
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:434
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:579
vmrglw vmrglh hi
u32 mask_width
Definition: tapcli.c:841
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:254
#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:140
#define VNET_HW_INTERFACE_FLAG_SPEED_1G
Definition: interface.h:403
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:554
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:143
#define hash_unset(h, key)
Definition: hash.h:260
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:211
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:391
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:321
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:1110
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:1207
#define PREDICT_TRUE(x)
Definition: clib.h:106
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:196
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
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:507
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1317
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:559
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 VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:390
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:445
struct _vnet_device_class vnet_device_class_t
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
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:1264
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:275
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:1110
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
pthread_t thread[MAX_CONNS]
Definition: main.c:125
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
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:855
#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:104
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:1287
TAPCLI definitions.
unformat_function_t unformat_ip4_address
Definition: format.h:76
static vnet_device_class_t tapcli_dev_class
Definition: tapcli.c:47
u8 ip6_address_set
Please set the indicated ip4 address/mask on the interface.
Definition: tuntap.h:37
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
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:538
static tapcli_interface_t * tapcli_get_new_tapif()
Get tap interface from inactive interfaces or create new.
Definition: tapcli.c:811
struct iovec * iovecs
Vector of iovecs for readv/writev calls.
Definition: tapcli.c:110
#define VLIB_FRAME_SIZE
Definition: node.h:328
unformat_function_t unformat_line_input
Definition: format.h:281
u32 max_supported_packet_bytes
Definition: interface.h:466
#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
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
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
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
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:446
void vlib_frame_free(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_frame_t *f)
Definition: main.c:211
static char * tapcli_rx_error_strings[]
TAPCLI error strings.
Definition: tapcli.c:477
ip6_address_t addr
Definition: tapcli.c:840
#define PREDICT_FALSE(x)
Definition: clib.h:105
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:119
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:656
#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:364
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:130
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:1166
#define TAP_MTU_DEFAULT
Definition: tapcli.h:52
u32 mtu_buffers
Definition: tapcli.c:125
unformat_function_t unformat_ip6_address
Definition: format.h:94
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u16 n_vectors
Definition: node.h:344
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
u8 renumber
Renumber the (existing) interface.
Definition: tuntap.h:39
vlib_main_t * vm
Definition: buffer.c:294
static uword tapcli_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
tapcli RX node function
Definition: tapcli.c:444
static int tapcli_tap_disconnect(tapcli_interface_t *ti)
Disconnect TAP CLI interface.
Definition: tapcli.c:1130
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:603
unsigned int ifindex
Definition: tapcli.c:842
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
tapcli_interface_t * tapcli_interfaces
Vector of tap interfaces.
Definition: tapcli.c:128
#define ETHERNET_INTERFACE_FLAG_MTU
Definition: ethernet.h:152
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:147
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:454
#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:629
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define hash_create(elts, value_bytes)
Definition: hash.h:681
#define VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
Definition: interface.h:394
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:588
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:480
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:227
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static 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:126
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:273
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
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:284
int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
Delete TAP interface.
Definition: tapcli.c:1167
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:745
VNET_HW_INTERFACE_CLASS(tapcli_interface_class, static)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static u32 vlib_buffer_alloc_from_free_list(vlib_main_t *vm, u32 *buffers, u32 n_buffers, vlib_buffer_free_list_index_t index)
Allocate buffers from specific freelist into supplied array.
Definition: buffer_funcs.h:288
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:159
struct _vlib_node_registration vlib_node_registration_t
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
struct _vnet_hw_interface_class vnet_hw_interface_class_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:553
u32 * tapcli_inactive_interfaces
Vector of deleted tap interfaces.
Definition: tapcli.c:131
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
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:267
#define clib_unix_warning(format, args...)
Definition: error.h:68
a point 2 point interface
Definition: interface.h:285
#define vnet_buffer(b)
Definition: buffer.h:372
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:463
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:234
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:1525
u8 data[0]
Packet data.
Definition: buffer.h:179
void(* os_punt_frame)(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: main.h:145
#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:562
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:159
#define TAP_MTU_MIN
Definition: tapcli.h:50
u32 flags
Definition: vhost-user.h:77
#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:483
#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:111
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:781
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
uword * pending_read_bitmap
Bitmap of tap interfaces with pending reads.
Definition: tapcli.c: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:57
u32 orig_sw_if_index
original sw_if_index (renumber)
Definition: tuntap.h:53
ip6_address_t * ip6_address
(optional) ip6 address to set
Definition: tuntap.h:45
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
TAPCLI per thread struct.
Definition: tapcli.c: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:715
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:169
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:1364