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