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