FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
tuntap.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * tuntap.c - kernel stack (reverse) punt/inject path
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 TunTap Kernel stack (reverse) punt/inject path.
22  *
23  * This driver runs in one of two distinct modes:
24  * - "punt/inject" mode, where we send pkts not otherwise processed
25  * by the forwarding to the Linux kernel stack, and
26  *
27  * - "normal interface" mode, where we treat the Linux kernel stack
28  * as a peer.
29  *
30  * By default, we select punt/inject mode.
31  */
32 
33 #include <fcntl.h> /* for open */
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/uio.h> /* for iovec */
39 #include <netinet/in.h>
40 
41 #include <linux/if_arp.h>
42 #include <linux/if_tun.h>
43 
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
46 
47 #include <vnet/ip/ip.h>
48 #include <vnet/fib/fib_table.h>
49 
50 #include <vnet/ethernet/ethernet.h>
51 #include <vnet/devices/devices.h>
52 #include <vnet/feature/feature.h>
53 
56 
57 static void tuntap_punt_frame (vlib_main_t * vm,
58  vlib_node_runtime_t * node,
59  vlib_frame_t * frame);
60 static void tuntap_nopunt_frame (vlib_main_t * vm,
61  vlib_node_runtime_t * node,
62  vlib_frame_t * frame);
63 
64 typedef struct
65 {
68  u8 addr[16];
70 
71 /**
72  * @brief TUNTAP per thread struct
73  */
74 typedef struct
75 {
76  /** Vector of VLIB rx buffers to use. We allocate them in blocks
77  of VLIB_FRAME_SIZE (256). */
79 
80  /** Vector of iovecs for readv/writev calls. */
81  struct iovec *iovecs;
83 
84 /**
85  * @brief TUNTAP node main state
86  */
87 typedef struct
88 {
89  /** per thread variables */
91 
92  /** File descriptors for /dev/net/tun and provisioning socket. */
93  int dev_net_tun_fd, dev_tap_fd;
94 
95  /** Create a "tap" [ethernet] encaps device */
96  int is_ether;
97 
98  /** 1 if a "normal" routed intfc, 0 if a punt/inject interface */
99 
101 
102  /** tap device destination MAC address. Required, or Linux drops pkts */
103  u8 ether_dst_mac[6];
104 
105  /** Interface MTU in bytes and # of default sized buffers. */
106  u32 mtu_bytes, mtu_buffers;
107 
108  /** Linux interface name for tun device. */
109  char *tun_name;
110 
111  /** Pool of subinterface addresses */
113 
114  /** Hash for subif addresses */
116 
117  /** Unix file index */
119 
120  /** For the "normal" interface, if configured */
121  u32 hw_if_index, sw_if_index;
122 
123 } tuntap_main_t;
124 
125 static tuntap_main_t tuntap_main = {
126  .tun_name = "vnet",
127 
128  /** Suitable defaults for an Ethernet-like tun/tap device */
129  .mtu_bytes = 4096 + 256,
130 };
131 
132 /**
133  * @brief tuntap_tx
134  * @node tuntap-tx
135  *
136  * Output node, writes the buffers comprising the incoming frame
137  * to the tun/tap device, aka hands them to the Linux kernel stack.
138  *
139  * @param *vm - vlib_main_t
140  * @param *node - vlib_node_runtime_t
141  * @param *frame - vlib_frame_t
142  *
143  * @return rc - uword
144  *
145  */
146 static uword
148 {
149  u32 *buffers = vlib_frame_args (frame);
150  uword n_packets = frame->n_vectors;
151  tuntap_main_t *tm = &tuntap_main;
152  vnet_main_t *vnm = vnet_get_main ();
154  u32 n_bytes = 0;
155  int i;
156  u16 thread_index = vlib_get_thread_index ();
157 
158  for (i = 0; i < n_packets; i++)
159  {
160  struct iovec *iov;
161  vlib_buffer_t *b;
162  uword l;
163 
164  b = vlib_get_buffer (vm, buffers[i]);
165 
166  if (tm->is_ether && (!tm->have_normal_interface))
167  {
168  vlib_buffer_reset (b);
170  }
171 
172  /* Re-set iovecs if present. */
173  if (tm->threads[thread_index].iovecs)
174  _vec_len (tm->threads[thread_index].iovecs) = 0;
175 
176  /** VLIB buffer chain -> Unix iovec(s). */
177  vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
178  iov->iov_base = b->data + b->current_data;
179  iov->iov_len = l = b->current_length;
180 
182  {
183  do
184  {
185  b = vlib_get_buffer (vm, b->next_buffer);
186 
187  vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
188 
189  iov->iov_base = b->data + b->current_data;
190  iov->iov_len = b->current_length;
191  l += b->current_length;
192  }
193  while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
194  }
195 
196  if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
197  vec_len (tm->threads[thread_index].iovecs)) < l)
198  clib_unix_warning ("writev");
199 
200  n_bytes += l;
201  }
202 
203  /* Update tuntap interface output stats. */
206  vm->thread_index,
207  tm->sw_if_index, n_packets, n_bytes);
208 
209 
210  /** The normal interface path flattens the buffer chain */
211  if (tm->have_normal_interface)
212  vlib_buffer_free_no_next (vm, buffers, n_packets);
213  else
214  vlib_buffer_free (vm, buffers, n_packets);
215 
216  return n_packets;
217 }
218 
219 /* *INDENT-OFF* */
221  .function = tuntap_tx,
222  .name = "tuntap-tx",
223  .type = VLIB_NODE_TYPE_INTERNAL,
224  .vector_size = 4,
225 };
226 /* *INDENT-ON* */
227 
228 /**
229  * @brief TUNTAP receive node
230  * @node tuntap-rx
231  *
232  * @param *vm - vlib_main_t
233  * @param *node - vlib_node_runtime_t
234  * @param *frame - vlib_frame_t
235  *
236  * @return rc - uword
237  *
238  */
239 static uword
241 {
242  tuntap_main_t *tm = &tuntap_main;
243  vlib_buffer_t *b;
244  u32 bi;
245  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
246  u16 thread_index = vlib_get_thread_index ();
247 
248  /** Make sure we have some RX buffers. */
249  {
250  uword n_left = vec_len (tm->threads[thread_index].rx_buffers);
251  uword n_alloc;
252 
253  if (n_left < VLIB_FRAME_SIZE / 2)
254  {
255  if (!tm->threads[thread_index].rx_buffers)
256  vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE);
257 
258  n_alloc =
259  vlib_buffer_alloc (vm,
260  tm->threads[thread_index].rx_buffers + n_left,
261  VLIB_FRAME_SIZE - n_left);
262  _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc;
263  }
264  }
265 
266  /** Allocate RX buffers from end of rx_buffers.
267  Turn them into iovecs to pass to readv. */
268  {
269  uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
270  vlib_buffer_t *b;
271  word i, n_bytes_left, n_bytes_in_packet;
272 
273  /** We should have enough buffers left for an MTU sized packet. */
274  ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >=
275  tm->mtu_buffers);
276 
277  vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
278  for (i = 0; i < tm->mtu_buffers; i++)
279  {
280  b =
281  vlib_get_buffer (vm,
282  tm->threads[thread_index].rx_buffers[i_rx - i]);
283  tm->threads[thread_index].iovecs[i].iov_base = b->data;
284  tm->threads[thread_index].iovecs[i].iov_len = buffer_size;
285  }
286 
287  n_bytes_left =
288  readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs,
289  tm->mtu_buffers);
290  n_bytes_in_packet = n_bytes_left;
291  if (n_bytes_left <= 0)
292  {
293  if (errno != EAGAIN)
294  clib_unix_warning ("readv %d", n_bytes_left);
295  return 0;
296  }
297 
298  bi = tm->threads[thread_index].rx_buffers[i_rx];
299 
300  while (1)
301  {
302  b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]);
303  b->flags = 0;
304  b->current_data = 0;
305  b->current_length =
306  n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
307 
308  n_bytes_left -= buffer_size;
309 
310  if (n_bytes_left <= 0)
311  {
312  break;
313  }
314 
315  i_rx--;
317  b->next_buffer = tm->threads[thread_index].rx_buffers[i_rx];
318  }
319 
320  /** Interface counters for tuntap interface. */
324  thread_index, tm->sw_if_index, 1, n_bytes_in_packet);
325 
326  _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
327  }
328 
329  b = vlib_get_buffer (vm, bi);
330 
331  {
332  u32 next_index;
333  uword n_trace = vlib_get_trace_count (vm, node);
334 
335  vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index;
336  vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
337 
338  /*
339  * Turn this on if you run into
340  * "bad monkey" contexts, and you want to know exactly
341  * which nodes they've visited...
342  */
344  b->pre_data[0] = 0;
345 
346  b->error = node->errors[0];
347 
348  if (tm->is_ether)
349  {
351  }
352  else
353  switch (b->data[0] & 0xf0)
354  {
355  case 0x40:
357  break;
358  case 0x60:
360  break;
361  default:
362  next_index = VNET_DEVICE_INPUT_NEXT_DROP;
363  break;
364  }
365 
366  /* The linux kernel couldn't care less if our interface is up */
367  if (tm->have_normal_interface)
368  {
369  vnet_main_t *vnm = vnet_get_main ();
371  si = vnet_get_sw_interface (vnm, tm->sw_if_index);
373  next_index = VNET_DEVICE_INPUT_NEXT_DROP;
374  }
375 
376  vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b);
377 
378  vlib_set_next_frame_buffer (vm, node, next_index, bi);
379 
380  if (n_trace > 0)
381  {
382  vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1);
383  vlib_set_trace_count (vm, node, n_trace - 1);
384  }
385  }
386 
387  return 1;
388 }
389 
390 /**
391  * @brief TUNTAP_RX error strings
392  */
393 static char *tuntap_rx_error_strings[] = {
394  "unknown packet type",
395 };
396 
397 /* *INDENT-OFF* */
399  .function = tuntap_rx,
400  .name = "tuntap-rx",
401  .sibling_of = "device-input",
402  .type = VLIB_NODE_TYPE_INPUT,
403  .state = VLIB_NODE_STATE_INTERRUPT,
404  .vector_size = 4,
405  .n_errors = 1,
406  .error_strings = tuntap_rx_error_strings,
407 };
408 /* *INDENT-ON* */
409 
410 /**
411  * @brief Gets called when file descriptor is ready from epoll.
412  *
413  * @param *uf - clib_file_t
414  *
415  * @return error - clib_error_t
416  */
417 static clib_error_t *
419 {
422  return 0;
423 }
424 
425 /**
426  * @brief Clean up the tun/tap device
427  *
428  * @param *vm - vlib_main_t
429  *
430  * @return error - clib_error_t
431  *
432  */
433 static clib_error_t *
435 {
436  tuntap_main_t *tm = &tuntap_main;
437  struct ifreq ifr;
438  int sfd;
439 
440  /* Not present. */
441  if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0)
442  return 0;
443 
444  sfd = socket (AF_INET, SOCK_STREAM, 0);
445  if (sfd < 0)
446  clib_unix_warning ("provisioning socket");
447 
448  memset (&ifr, 0, sizeof (ifr));
449  strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
450 
451  /* get flags, modify to bring down interface... */
452  if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
453  clib_unix_warning ("SIOCGIFFLAGS");
454 
455  ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
456 
457  if (ioctl (sfd, SIOCSIFFLAGS, &ifr) < 0)
458  clib_unix_warning ("SIOCSIFFLAGS");
459 
460  /* Turn off persistence */
461  if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
462  clib_unix_warning ("TUNSETPERSIST");
463  close (tm->dev_tap_fd);
464  if (tm->dev_net_tun_fd >= 0)
465  close (tm->dev_net_tun_fd);
466  if (sfd >= 0)
467  close (sfd);
468 
469  return 0;
470 }
471 
473 
474 /**
475  * @brief CLI function for tun/tap config
476  *
477  * @param *vm - vlib_main_t
478  * @param *input - unformat_input_t
479  *
480  * @return error - clib_error_t
481  *
482  */
483 static clib_error_t *
485 {
486  tuntap_main_t *tm = &tuntap_main;
487  clib_error_t *error = 0;
488  struct ifreq ifr;
489  u8 *name;
490  int flags = IFF_TUN | IFF_NO_PI;
491  int is_enabled = 0, is_ether = 0, have_normal_interface = 0;
492  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
493 
495  {
496  if (unformat (input, "mtu %d", &tm->mtu_bytes))
497  ;
498  else if (unformat (input, "enable"))
499  is_enabled = 1;
500  else if (unformat (input, "disable"))
501  is_enabled = 0;
502  else if (unformat (input, "ethernet") || unformat (input, "ether"))
503  is_ether = 1;
504  else if (unformat (input, "have-normal-interface") ||
505  unformat (input, "have-normal"))
506  have_normal_interface = 1;
507  else if (unformat (input, "name %s", &name))
508  tm->tun_name = (char *) name;
509  else
510  return clib_error_return (0, "unknown input `%U'",
511  format_unformat_error, input);
512  }
513 
514  tm->dev_net_tun_fd = -1;
515  tm->dev_tap_fd = -1;
516 
517  if (is_enabled == 0)
518  return 0;
519 
520  if (geteuid ())
521  {
522  clib_warning ("tuntap disabled: must be superuser");
523  return 0;
524  }
525 
526  tm->is_ether = is_ether;
527  tm->have_normal_interface = have_normal_interface;
528 
529  if (is_ether)
530  flags = IFF_TAP | IFF_NO_PI;
531 
532  if ((tm->dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
533  {
534  error = clib_error_return_unix (0, "open /dev/net/tun");
535  goto done;
536  }
537 
538  memset (&ifr, 0, sizeof (ifr));
539  strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
540  ifr.ifr_flags = flags;
541  if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0)
542  {
543  error = clib_error_return_unix (0, "ioctl TUNSETIFF");
544  goto done;
545  }
546 
547  /* Make it persistent, at least until we split. */
548  if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0)
549  {
550  error = clib_error_return_unix (0, "TUNSETPERSIST");
551  goto done;
552  }
553 
554  /* Open a provisioning socket */
555  if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
556  {
557  error = clib_error_return_unix (0, "socket");
558  goto done;
559  }
560 
561  /* Find the interface index. */
562  {
563  struct ifreq ifr;
564  struct sockaddr_ll sll;
565 
566  memset (&ifr, 0, sizeof (ifr));
567  strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1);
568  if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0)
569  {
570  error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX");
571  goto done;
572  }
573 
574  /* Bind the provisioning socket to the interface. */
575  memset (&sll, 0, sizeof (sll));
576  sll.sll_family = AF_PACKET;
577  sll.sll_ifindex = ifr.ifr_ifindex;
578  sll.sll_protocol = htons (ETH_P_ALL);
579 
580  if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
581  {
582  error = clib_error_return_unix (0, "bind");
583  goto done;
584  }
585  }
586 
587  /* non-blocking I/O on /dev/tapX */
588  {
589  int one = 1;
590  if (ioctl (tm->dev_net_tun_fd, FIONBIO, &one) < 0)
591  {
592  error = clib_error_return_unix (0, "ioctl FIONBIO");
593  goto done;
594  }
595  }
596 
597  tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
598 
599  ifr.ifr_mtu = tm->mtu_bytes;
600  if (ioctl (tm->dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
601  {
602  error = clib_error_return_unix (0, "ioctl SIOCSIFMTU");
603  goto done;
604  }
605 
606  /* get flags, modify to bring up interface... */
607  if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
608  {
609  error = clib_error_return_unix (0, "ioctl SIOCGIFFLAGS");
610  goto done;
611  }
612 
613  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
614 
615  if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
616  {
617  error = clib_error_return_unix (0, "ioctl SIOCSIFFLAGS");
618  goto done;
619  }
620 
621  if (is_ether)
622  {
623  if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
624  {
625  error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR");
626  goto done;
627  }
628  else
629  clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6);
630  }
631 
632  if (have_normal_interface)
633  {
634  vnet_main_t *vnm = vnet_get_main ();
636  (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
637  tm->ether_dst_mac /* ethernet address */ ,
638  &tm->hw_if_index, 0 /* flag change */ );
639  if (error)
640  clib_error_report (error);
641  tm->sw_if_index = tm->hw_if_index;
643  }
644  else
645  {
646  vnet_main_t *vnm = vnet_get_main ();
648 
650 
652  (vnm, tuntap_dev_class.index, 0 /* device instance */ ,
653  tuntap_interface_class.index, 0);
654  hi = vnet_get_hw_interface (vnm, tm->hw_if_index);
655  tm->sw_if_index = hi->sw_if_index;
656 
657  /* Interface is always up. */
662  }
663 
664  {
665  clib_file_t template = { 0 };
666  template.read_function = tuntap_read_ready;
667  template.file_descriptor = tm->dev_net_tun_fd;
668  tm->clib_file_index = clib_file_add (&file_main, &template);
669  }
670 
671 done:
672  if (error)
673  {
674  if (tm->dev_net_tun_fd >= 0)
675  close (tm->dev_net_tun_fd);
676  if (tm->dev_tap_fd >= 0)
677  close (tm->dev_tap_fd);
678  }
679 
680  return error;
681 }
682 
684 
685 /**
686  * @brief Add or Del IP4 address to tun/tap interface
687  *
688  * @param *im - ip4_main_t
689  * @param opaque - uword
690  * @param sw_if_index - u32
691  * @param *address - ip4_address_t
692  * @param is_delete - u32
693  *
694  */
695 void
697  uword opaque,
698  u32 sw_if_index,
699  ip4_address_t * address,
700  u32 address_length,
701  u32 if_address_index, u32 is_delete)
702 {
703  tuntap_main_t *tm = &tuntap_main;
704  struct ifreq ifr;
705  subif_address_t subif_addr, *ap;
706  uword *p;
707 
708  /** Tuntap disabled, or using a "normal" interface. */
709  if (tm->have_normal_interface || tm->dev_tap_fd < 0)
710  return;
711 
712  /* if the address is being applied to an interface that is not in
713  * the same table/VRF as this tap, then ignore it.
714  * If we don't do this overlapping address spaces in the diferent tables
715  * breaks the linux host's routing tables */
717  sw_if_index) !=
719  return;
720 
721  /** See if we already know about this subif */
722  memset (&subif_addr, 0, sizeof (subif_addr));
723  subif_addr.sw_if_index = sw_if_index;
724  clib_memcpy (&subif_addr.addr, address, sizeof (*address));
725 
726  p = mhash_get (&tm->subif_mhash, &subif_addr);
727 
728  if (p)
729  ap = pool_elt_at_index (tm->subifs, p[0]);
730  else
731  {
732  pool_get (tm->subifs, ap);
733  *ap = subif_addr;
734  mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
735  }
736 
737  /* Use subif pool index to select alias device. */
738  memset (&ifr, 0, sizeof (ifr));
739  snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
740  "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
741 
742  /* the tuntap punt/inject is enabled for IPv4 RX so long as
743  * any vpp interface has an IPv4 address.
744  * this is also ref counted.
745  */
747 
748  if (!is_delete)
749  {
750  struct sockaddr_in *sin;
751 
752  sin = (struct sockaddr_in *) &ifr.ifr_addr;
753 
754  /* Set ipv4 address, netmask. */
755  sin->sin_family = AF_INET;
756  clib_memcpy (&sin->sin_addr.s_addr, address, 4);
757  if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
758  clib_unix_warning ("ioctl SIOCSIFADDR");
759 
760  sin->sin_addr.s_addr = im->fib_masks[address_length];
761  if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
762  clib_unix_warning ("ioctl SIOCSIFNETMASK");
763  }
764  else
765  {
766  mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
767  pool_put (tm->subifs, ap);
768  }
769 
770  /* get flags, modify to bring up interface... */
771  if (ioctl (tm->dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
772  clib_unix_warning ("ioctl SIOCGIFFLAGS");
773 
774  if (is_delete)
775  ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
776  else
777  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
778 
779  if (ioctl (tm->dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
780  clib_unix_warning ("ioctl SIOCSIFFLAGS");
781 }
782 
783 /**
784  * @brief workaround for a known include file bug.
785  * including @c <linux/ipv6.h> causes multiple definitions if
786  * @c <netinet/in.h is also included.
787  */
788 struct in6_ifreq
789 {
790  struct in6_addr ifr6_addr;
793 };
794 
795 /**
796  * @brief Add or Del tun/tap interface address.
797  *
798  * Both the v6 interface address API and the way ifconfig
799  * displays subinterfaces differ from their v4 couterparts.
800  * The code given here seems to work but YMMV.
801  *
802  * @param *im - ip6_main_t
803  * @param opaque - uword
804  * @param sw_if_index - u32
805  * @param *address - ip6_address_t
806  * @param address_length - u32
807  * @param if_address_index - u32
808  * @param is_delete - u32
809  */
810 void
812  uword opaque,
813  u32 sw_if_index,
814  ip6_address_t * address,
815  u32 address_length,
816  u32 if_address_index, u32 is_delete)
817 {
818  tuntap_main_t *tm = &tuntap_main;
819  struct ifreq ifr;
820  struct in6_ifreq ifr6;
821  subif_address_t subif_addr, *ap;
822  uword *p;
823 
824  /* Tuntap disabled, or using a "normal" interface. */
825  if (tm->have_normal_interface || tm->dev_tap_fd < 0)
826  return;
827 
828  /* if the address is being applied to an interface that is not in
829  * the same table/VRF as this tap, then ignore it.
830  * If we don't do this overlapping address spaces in the diferent tables
831  * breaks the linux host's routing tables */
833  sw_if_index) !=
835  return;
836 
837  /* See if we already know about this subif */
838  memset (&subif_addr, 0, sizeof (subif_addr));
839  subif_addr.sw_if_index = sw_if_index;
840  subif_addr.is_v6 = 1;
841  clib_memcpy (&subif_addr.addr, address, sizeof (*address));
842 
843  p = mhash_get (&tm->subif_mhash, &subif_addr);
844 
845  if (p)
846  ap = pool_elt_at_index (tm->subifs, p[0]);
847  else
848  {
849  pool_get (tm->subifs, ap);
850  *ap = subif_addr;
851  mhash_set (&tm->subif_mhash, ap, ap - tm->subifs, 0);
852  }
853 
854  /* Use subif pool index to select alias device. */
855  memset (&ifr, 0, sizeof (ifr));
856  memset (&ifr6, 0, sizeof (ifr6));
857  snprintf (ifr.ifr_name, sizeof (ifr.ifr_name),
858  "%s:%d", tm->tun_name, (int) (ap - tm->subifs));
859 
860  /* the tuntap punt/inject is enabled for IPv6 RX so long as
861  * any vpp interface has an IPv6 address.
862  * this is also ref counted.
863  */
865 
866  if (!is_delete)
867  {
868  int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
869  if (sockfd < 0)
870  clib_unix_warning ("get ifindex socket");
871 
872  if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
873  clib_unix_warning ("get ifindex");
874 
875  ifr6.ifr6_ifindex = ifr.ifr_ifindex;
876  ifr6.ifr6_prefixlen = address_length;
877  clib_memcpy (&ifr6.ifr6_addr, address, 16);
878 
879  if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0)
880  clib_unix_warning ("set address");
881 
882  if (sockfd >= 0)
883  close (sockfd);
884  }
885  else
886  {
887  int sockfd = socket (AF_INET6, SOCK_STREAM, 0);
888  if (sockfd < 0)
889  clib_unix_warning ("get ifindex socket");
890 
891  if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0)
892  clib_unix_warning ("get ifindex");
893 
894  ifr6.ifr6_ifindex = ifr.ifr_ifindex;
895  ifr6.ifr6_prefixlen = address_length;
896  clib_memcpy (&ifr6.ifr6_addr, address, 16);
897 
898  if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
899  clib_unix_warning ("del address");
900 
901  if (sockfd >= 0)
902  close (sockfd);
903 
904  mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ );
905  pool_put (tm->subifs, ap);
906  }
907 }
908 
909 /**
910  * @brief TX the tun/tap frame
911  *
912  * @param *vm - vlib_main_t
913  * @param *node - vlib_node_runtime_t
914  * @param *frame - vlib_frame_t
915  *
916  */
917 static void
919  vlib_node_runtime_t * node, vlib_frame_t * frame)
920 {
921  tuntap_tx (vm, node, frame);
922  vlib_frame_free (vm, node, frame);
923 }
924 
925 /**
926  * @brief Free the tun/tap frame
927  *
928  * @param *vm - vlib_main_t
929  * @param *node - vlib_node_runtime_t
930  * @param *frame - vlib_frame_t
931  *
932  */
933 static void
935  vlib_node_runtime_t * node, vlib_frame_t * frame)
936 {
937  u32 *buffers = vlib_frame_args (frame);
938  uword n_packets = frame->n_vectors;
939  vlib_buffer_free (vm, buffers, n_packets);
940  vlib_frame_free (vm, node, frame);
941 }
942 
943 /* *INDENT-OFF* */
945  .name = "tuntap",
947 };
948 /* *INDENT-ON* */
949 
950 /**
951  * @brief Format tun/tap interface name
952  *
953  * @param *s - u8 - formatter string
954  * @param *args - va_list
955  *
956  * @return *s - u8 - formatted string
957  *
958  */
959 static u8 *
960 format_tuntap_interface_name (u8 * s, va_list * args)
961 {
962  u32 i = va_arg (*args, u32);
963 
964  s = format (s, "tuntap-%d", i);
965  return s;
966 }
967 
968 /**
969  * @brief TX packet out tun/tap
970  *
971  * @param *vm - vlib_main_t
972  * @param *node - vlib_node_runtime_t
973  * @param *frame - vlib_frame_t
974  *
975  * @return n_buffers - uword - Packets transmitted
976  *
977  */
978 static uword
980  vlib_node_runtime_t * node, vlib_frame_t * frame)
981 {
982  tuntap_main_t *tm = &tuntap_main;
983  u32 *buffers = vlib_frame_args (frame);
984  uword n_buffers = frame->n_vectors;
985 
986  /* Normal interface transmit happens only on the normal interface... */
987  if (tm->have_normal_interface)
988  return tuntap_tx (vm, node, frame);
989 
990  vlib_buffer_free (vm, buffers, n_buffers);
991  return n_buffers;
992 }
993 
994 /* *INDENT-OFF* */
996  .name = "tuntap",
997  .tx_function = tuntap_intfc_tx,
998  .format_device_name = format_tuntap_interface_name,
999 };
1000 /* *INDENT-ON* */
1001 
1002 /**
1003  * @brief tun/tap node init
1004  *
1005  * @param *vm - vlib_main_t
1006  *
1007  * @return error - clib_error_t
1008  *
1009  */
1010 static clib_error_t *
1012 {
1013  clib_error_t *error;
1014  ip4_main_t *im4 = &ip4_main;
1015  ip6_main_t *im6 = &ip6_main;
1018  tuntap_main_t *tm = &tuntap_main;
1020 
1021  error = vlib_call_init_function (vm, ip4_init);
1022  if (error)
1023  return error;
1024 
1025  mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t));
1026 
1028  cb4.function_opaque = 0;
1030 
1032  cb6.function_opaque = 0;
1036 
1037  return 0;
1038 }
1039 
1041 
1042 /*
1043  * fd.io coding-style-patch-verification: ON
1044  *
1045  * Local Variables:
1046  * eval: (c-set-style "gnu")
1047  * End:
1048  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
static clib_error_t * tuntap_exit(vlib_main_t *vm)
Clean up the tun/tap device.
Definition: tuntap.c:434
vmrglw vmrglh hi
Definition: mhash.h:46
static uword tuntap_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
TUNTAP receive node.
Definition: tuntap.c:240
static vlib_node_registration_t tuntap_tx_node
(constructor) VLIB_REGISTER_NODE (tuntap_tx_node)
Definition: tuntap.c:220
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:538
static void vlib_buffer_reset(vlib_buffer_t *b)
Reset current header & length to state they were in when packet was received.
Definition: buffer.h:234
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
ip4_add_del_interface_address_callback_t * add_del_interface_address_callbacks
Functions to call when interface address changes.
Definition: ip4.h:129
void tuntap_ip4_add_del_interface_address(ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address, u32 address_length, u32 if_address_index, u32 is_delete)
Add or Del IP4 address to tun/tap interface.
Definition: tuntap.c:696
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:356
static void vlib_set_next_frame_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 buffer_index)
Definition: node_funcs.h:397
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:196
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
TUNTAP per thread struct.
Definition: tuntap.c:74
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
Definition: fib_table.c:931
int ifr6_ifindex
Definition: tuntap.c:792
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 thread_index
Definition: main.h:173
mhash_t subif_mhash
Hash for subif addresses.
Definition: tuntap.c:115
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
char * tun_name
Linux interface name for tun device.
Definition: tuntap.c:109
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:557
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
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:394
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:443
TUNTAP node main state.
Definition: tuntap.c:87
struct _vnet_device_class vnet_device_class_t
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:225
struct in6_addr ifr6_addr
Definition: tuntap.c:790
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:275
int dev_net_tun_fd
File descriptors for /dev/net/tun and provisioning socket.
Definition: tuntap.c:93
static clib_error_t * tuntap_read_ready(clib_file_t *uf)
Gets called when file descriptor is ready from epoll.
Definition: tuntap.c:418
clib_file_function_t * read_function
Definition: file.h:63
int dev_tap_fd
Definition: tuntap.c:93
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
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
u32 hw_if_index
For the "normal" interface, if configured.
Definition: tuntap.c:121
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:68
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static clib_error_t * ip4_init(vlib_main_t *vm)
Definition: ip4_input.c:325
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:672
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
u8 pre_data[VLIB_BUFFER_PRE_DATA_SIZE]
Space for inserting data before buffer start.
Definition: buffer.h:151
static vnet_device_class_t tuntap_dev_class
Definition: tuntap.c:54
#define vlib_call_init_function(vm, x)
Definition: init.h:162
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:689
static u8 * format_tuntap_interface_name(u8 *s, va_list *args)
Format tun/tap interface name.
Definition: tuntap.c:960
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
void ip4_sw_interface_enable_disable(u32 sw_if_index, u32 is_enable)
Definition: ip4_forward.c:866
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:72
subif_address_t * subifs
Pool of subinterface addresses.
Definition: tuntap.c:112
tuntap_per_thread_t * threads
per thread variables
Definition: tuntap.c:90
struct _unformat_input_t unformat_input_t
#define clib_error_return_unix(e, args...)
Definition: error.h:102
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:271
void vlib_frame_free(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_frame_t *f)
Definition: main.c:211
static tuntap_main_t tuntap_main
Definition: tuntap.c:125
#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
#define VLIB_FRAME_SIZE
Definition: node.h:328
static uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:117
static uword tuntap_intfc_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
TX packet out tun/tap.
Definition: tuntap.c:979
ip6_add_del_interface_address_callback_t * add_del_interface_address_callbacks
Definition: ip6.h:194
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
void tuntap_ip6_add_del_interface_address(ip6_main_t *im, uword opaque, u32 sw_if_index, ip6_address_t *address, u32 address_length, u32 if_address_index, u32 is_delete)
Add or Del tun/tap interface address.
Definition: tuntap.c:811
static void tuntap_nopunt_frame(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Free the tun/tap frame.
Definition: tuntap.c:934
static void vlib_buffer_free_no_next(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers, does not free the buffer chain for each buffer.
Definition: buffer_funcs.h:377
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
void mhash_init(mhash_t *h, uword n_value_bytes, uword n_key_bytes)
Definition: mhash.c:168
u16 n_vectors
Definition: node.h:344
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:283
ip4_add_del_interface_address_function_t * function
Definition: ip4.h:72
u8 addr[16]
Definition: tuntap.c:68
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:116
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define VLIB_BUFFER_TRACE_TRAJECTORY
Compile time buffer trajectory tracing option Turn this on if you run into "bad monkey" contexts...
Definition: buffer.h:525
static void tuntap_punt_frame(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
TX the tun/tap frame.
Definition: tuntap.c:918
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:51
u32 * rx_buffers
Vector of VLIB rx buffers to use.
Definition: tuntap.c:78
ip6_add_del_interface_address_function_t * function
Definition: ip6.h:102
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:576
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u32 mtu_buffers
Definition: tuntap.c:106
u8 ether_dst_mac[6]
tap device destination MAC address.
Definition: tuntap.c:103
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
ip6_main_t ip6_main
Definition: ip6_forward.c:3009
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:84
u32 mtu_bytes
Interface MTU in bytes and # of default sized buffers.
Definition: tuntap.c:106
IPv4 main type.
Definition: ip4.h:95
static vlib_node_registration_t tuntap_rx_node
(constructor) VLIB_REGISTER_NODE (tuntap_rx_node)
Definition: tuntap.c:398
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:273
int have_normal_interface
1 if a "normal" routed intfc, 0 if a punt/inject interface
Definition: tuntap.c:100
#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
u32 ifr6_prefixlen
Definition: tuntap.c:791
workaround for a known include file bug.
Definition: tuntap.c:788
VNET_DEVICE_CLASS(tuntap_dev_class, static)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
int is_ether
Create a "tap" [ethernet] encaps device.
Definition: tuntap.c:96
u64 uword
Definition: types.h:112
u32 clib_file_index
Unix file index.
Definition: tuntap.c:118
static uword tuntap_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
tuntap_tx
Definition: tuntap.c:147
Definition: defs.h:47
static clib_error_t * tuntap_config(vlib_main_t *vm, unformat_input_t *input)
CLI function for tun/tap config.
Definition: tuntap.c:484
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)
unsigned char u8
Definition: types.h:56
struct iovec * iovecs
Vector of iovecs for readv/writev calls.
Definition: tuntap.c:81
#define clib_unix_warning(format, args...)
Definition: error.h:68
a point 2 point interface
Definition: interface.h:289
#define vnet_buffer(b)
Definition: buffer.h:326
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:227
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1181
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u8 data[0]
Packet data.
Definition: buffer.h:159
void(* os_punt_frame)(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: main.h:142
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:546
Definition: file.h:50
u32 sw_if_index
Definition: tuntap.c:121
vhost_vring_addr_t addr
Definition: vhost-user.h:83
static clib_error_t * tuntap_init(vlib_main_t *vm)
tun/tap node init
Definition: tuntap.c:1011
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
VNET_HW_INTERFACE_CLASS(tuntap_interface_class, static)
static vnet_hw_interface_class_t tuntap_interface_class
Definition: tuntap.c:55
static char * tuntap_rx_error_strings[]
TUNTAP_RX error strings.
Definition: tuntap.c:393
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
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:75
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:341
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 sw_if_index
Definition: tuntap.c:66
void ip6_sw_interface_enable_disable(u32 sw_if_index, u32 is_enable)
Definition: ip6_forward.c:416
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
Definition: defs.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
u32 fib_masks[33]
Definition: ip4.h:108