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