VPP-305: Documentation for vnet/vnet/unix
[vpp.git] / vnet / vnet / unix / tuntap.c
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
55 static vnet_device_class_t tuntap_dev_class;
56 static vnet_hw_interface_class_t tuntap_interface_class;
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 {
66   u32 sw_if_index;
67   u8 is_v6;
68   u8 addr[16];
69 } subif_address_t;
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). */
80   u32 * rx_buffers;
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
90   int have_normal_interface;
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 */
102   subif_address_t *subifs;
103
104   /** Hash for subif addresses */
105   mhash_t subif_mhash;
106
107   /** Unix file index */
108   u32 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
137 tuntap_tx (vlib_main_t * vm,
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         {
156           vlib_buffer_reset(b);
157           clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6);
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
169       if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
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
195 VLIB_REGISTER_NODE (tuntap_tx_node,static) = {
196   .function = tuntap_tx,
197   .name = "tuntap-tx",
198   .type = VLIB_NODE_TYPE_INTERNAL,
199   .vector_size = 4,
200 };
201
202 enum {
203   TUNTAP_RX_NEXT_IP4_INPUT,
204   TUNTAP_RX_NEXT_IP6_INPUT,
205   TUNTAP_RX_NEXT_ETHERNET_INPUT,
206   TUNTAP_RX_NEXT_DROP,
207   TUNTAP_RX_N_NEXT,
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
222 tuntap_rx (vlib_main_t * vm,
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)
246           vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
247
248         n_alloc = vlib_buffer_alloc_from_free_list 
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
291         mb = rte_mbuf_from_vlib_buffer(b);
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--;
321         b->flags |= VLIB_BUFFER_NEXT_PRESENT;
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. */
329     vlib_increment_combined_counter
330         (vnet_main.interface_main.combined_sw_if_counters
331          + VNET_INTERFACE_COUNTER_RX,
332          os_get_cpu_number(),
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      */
353     if (VLIB_BUFFER_TRACE_TRAJECTORY)
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);
382         if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
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
406 VLIB_REGISTER_NODE (tuntap_rx_node,static) = {
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  */
431 static clib_error_t * tuntap_read_ready (unix_file_t * uf)
432 {
433   vlib_main_t * vm = vlib_get_main();
434   vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index);
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 *
447 tuntap_exit (vlib_main_t * vm)
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
485 VLIB_MAIN_LOOP_EXIT_FUNCTION (tuntap_exit);
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 *
497 tuntap_config (vlib_main_t * vm, unformat_input_t * input)
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
507   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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();
650       error = ethernet_register_interface
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;
660       vm->os_punt_frame = tuntap_nopunt_frame;
661     }
662   else
663     {
664       vnet_main_t *vnm = vnet_get_main();
665       vnet_hw_interface_t * hi;
666       
667       vm->os_punt_frame = tuntap_punt_frame;
668       
669       tm->hw_if_index = vnet_register_interface
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. */
677       vnet_hw_interface_set_flags (vnm, tm->hw_if_index, 
678                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
679       vnet_sw_interface_set_flags (vnm, tm->sw_if_index, 
680                                    VNET_SW_INTERFACE_FLAG_ADMIN_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
702 VLIB_CONFIG_FUNCTION (tuntap_config, "tuntap");
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
715 tuntap_ip4_add_del_interface_address (ip4_main_t * im,
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 bug
790  * #include <linux/ipv6.h> causes multiple definitions if
791  * netinet/in.h is also included.
792  */
793 struct in6_ifreq {
794         struct in6_addr ifr6_addr;
795         u32             ifr6_prefixlen;
796         int             ifr6_ifindex;
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
815 tuntap_ip6_add_del_interface_address (ip6_main_t * im,
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
908 tuntap_punt_frame (vlib_main_t * vm,
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
925 tuntap_nopunt_frame (vlib_main_t * vm,
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
935 VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = {
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
967 tuntap_intfc_tx (vlib_main_t * vm,
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
983 VNET_DEVICE_CLASS (tuntap_dev_class,static) = {
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 *
998 tuntap_init (vlib_main_t * vm)
999 {
1000   clib_error_t * error;
1001   ip4_main_t * im4 = &ip4_main;
1002   ip6_main_t * im6 = &ip6_main;
1003   ip4_add_del_interface_address_callback_t cb4;
1004   ip6_add_del_interface_address_callback_t cb6;
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
1013   cb4.function = tuntap_ip4_add_del_interface_address;
1014   cb4.function_opaque = 0;
1015   vec_add1 (im4->add_del_interface_address_callbacks, cb4);
1016
1017   cb6.function = tuntap_ip6_add_del_interface_address;
1018   cb6.function_opaque = 0;
1019   vec_add1 (im6->add_del_interface_address_callbacks, cb6);
1020
1021   return 0;
1022 }
1023
1024 VLIB_INIT_FUNCTION (tuntap_init);