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