Initial commit of vpp code.
[vpp.git] / vnet / vnet / unix / tapcli.c
1 /* 
2  *------------------------------------------------------------------
3  * tapcli.c - dynamic tap interface hookup
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 #include <fcntl.h>              /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/types.h> 
25 #include <sys/uio.h>            /* for iovec */
26 #include <netinet/in.h>
27
28 #include <linux/if_arp.h>
29 #include <linux/if_tun.h>
30
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33
34 #include <vnet/ip/ip.h>
35
36 #include <vnet/ethernet/ethernet.h>
37
38 #if DPDK == 1
39 #include <vnet/devices/dpdk/dpdk.h>
40 #endif
41
42 #include <vnet/unix/tapcli.h>
43
44 static vnet_device_class_t tapcli_dev_class;
45 static vnet_hw_interface_class_t tapcli_interface_class;
46
47 static void tapcli_nopunt_frame (vlib_main_t * vm,
48                                  vlib_node_runtime_t * node,
49                                  vlib_frame_t * frame);
50 typedef struct {
51   u32 unix_fd;
52   u32 unix_file_index;
53   u32 provision_fd;
54   u32 sw_if_index;              /* for counters */
55   u32 hw_if_index;
56   u32 is_promisc;
57   struct ifreq ifr;
58   u32 per_interface_next_index;
59   u8 active;                    /* for delete */
60 } tapcli_interface_t;
61
62 typedef struct {
63   /* Vector of iovecs for readv/writev calls. */
64   struct iovec * iovecs;
65
66   /* Vector of VLIB rx buffers to use.  We allocate them in blocks
67      of VLIB_FRAME_SIZE (256). */
68   u32 * rx_buffers;
69
70   /* tap device destination MAC address. Required, or Linux drops pkts */
71   u8 ether_dst_mac[6];
72
73   /* Interface MTU in bytes and # of default sized buffers. */
74   u32 mtu_bytes, mtu_buffers;
75
76   /* Vector of tap interfaces */
77   tapcli_interface_t * tapcli_interfaces;
78
79   /* Vector of deleted tap interfaces */
80   u32 * tapcli_inactive_interfaces;
81
82   /* Bitmap of tap interfaces with pending reads */
83   uword * pending_read_bitmap;
84
85   /* Hash table to find tapcli interface given hw_if_index */
86   uword * tapcli_interface_index_by_sw_if_index;
87   
88   /* Hash table to find tapcli interface given unix fd */
89   uword * tapcli_interface_index_by_unix_fd;
90
91   /* renumbering table */
92   u32 * show_dev_instance_by_real_dev_instance;
93
94   /* 1 => disable CLI */
95   int is_disabled;
96
97   /* convenience */
98   vlib_main_t * vlib_main;
99   vnet_main_t * vnet_main;
100   unix_main_t * unix_main;
101 } tapcli_main_t;
102
103 static tapcli_main_t tapcli_main;
104
105 /*
106  * tapcli_tx
107  * Output node, writes the buffers comprising the incoming frame 
108  * to the tun/tap device, aka hands them to the Linux kernel stack.
109  * 
110  */
111 static uword
112 tapcli_tx (vlib_main_t * vm,
113            vlib_node_runtime_t * node,
114            vlib_frame_t * frame)
115 {
116   u32 * buffers = vlib_frame_args (frame);
117   uword n_packets = frame->n_vectors;
118   tapcli_main_t * tm = &tapcli_main;
119   tapcli_interface_t * ti;
120   int i;
121
122   for (i = 0; i < n_packets; i++)
123     {
124       struct iovec * iov;
125       vlib_buffer_t * b;
126       uword l;
127       vnet_hw_interface_t * hw;
128       uword * p;
129       u32 tx_sw_if_index;
130
131       b = vlib_get_buffer (vm, buffers[i]);
132
133       tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
134       if (tx_sw_if_index == (u32)~0)
135         tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
136         
137       ASSERT(tx_sw_if_index != (u32)~0);
138
139       /* Use the sup intfc to finesse vlan subifs */
140       hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index);
141       tx_sw_if_index = hw->sw_if_index;
142           
143       p = hash_get (tm->tapcli_interface_index_by_sw_if_index, 
144                     tx_sw_if_index);
145       if (p == 0)
146         {
147           clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
148           /* $$$ leak, but this should never happen... */
149           continue;
150         }
151       else
152         ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
153
154       /* Re-set iovecs if present. */
155       if (tm->iovecs)
156         _vec_len (tm->iovecs) = 0;
157
158       /* VLIB buffer chain -> Unix iovec(s). */
159       vec_add2 (tm->iovecs, iov, 1);
160       iov->iov_base = b->data + b->current_data;
161       iov->iov_len = l = b->current_length;
162
163       if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
164         {
165           do {
166             b = vlib_get_buffer (vm, b->next_buffer);
167
168             vec_add2 (tm->iovecs, iov, 1);
169
170             iov->iov_base = b->data + b->current_data;
171             iov->iov_len = b->current_length;
172             l += b->current_length;
173           } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
174         }
175
176       if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
177         clib_unix_warning ("writev");
178     }
179     
180   /* interface output path flattens buffer chains */
181   vlib_buffer_free_no_next (vm, buffers, n_packets);
182     
183   return n_packets;
184 }
185
186 VLIB_REGISTER_NODE (tapcli_tx_node,static) = {
187   .function = tapcli_tx,
188   .name = "tapcli-tx",
189   .type = VLIB_NODE_TYPE_INTERNAL,
190   .vector_size = 4,
191 };
192
193 enum {
194   TAPCLI_RX_NEXT_IP4_INPUT, 
195   TAPCLI_RX_NEXT_IP6_INPUT, 
196   TAPCLI_RX_NEXT_ETHERNET_INPUT,
197   TAPCLI_RX_NEXT_DROP,
198   TAPCLI_RX_N_NEXT,
199 };
200
201 static uword
202 tapcli_rx (vlib_main_t * vm,
203            vlib_node_runtime_t * node,
204            vlib_frame_t * frame)
205 {
206   tapcli_main_t * tm = &tapcli_main;
207   vlib_buffer_t * b;
208   u32 bi;
209 #if DPDK == 0
210   const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
211   u32 free_list_index = VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX;
212 #else
213   dpdk_main_t * dm = &dpdk_main;
214   const uword buffer_size = MBUF_SIZE;
215   u32 free_list_index = dm->vlib_buffer_free_list_index;
216 #endif
217   static u32 * ready_interface_indices;
218   tapcli_interface_t * ti;
219   int i;
220
221   vec_reset_length (ready_interface_indices);
222
223   clib_bitmap_foreach (i, tm->pending_read_bitmap, 
224   ({
225     vec_add1 (ready_interface_indices, i);
226   }));
227
228   if (vec_len (ready_interface_indices) == 0)
229     return 1;
230
231   for (i = 0; i < vec_len(ready_interface_indices); i++)
232     {
233       /* Clear the "interrupt" bit */
234       tm->pending_read_bitmap = 
235         clib_bitmap_set (tm->pending_read_bitmap,
236                          ready_interface_indices[i], 0);
237       
238       ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
239
240       /* Make sure we have some RX buffers. */
241       {
242         uword n_left = vec_len (tm->rx_buffers);
243         uword n_alloc;
244
245         if (n_left < VLIB_FRAME_SIZE / 2)
246           {
247             if (! tm->rx_buffers)
248               vec_alloc (tm->rx_buffers, VLIB_FRAME_SIZE);
249
250             n_alloc = vlib_buffer_alloc_from_free_list 
251               (vm, tm->rx_buffers + n_left, VLIB_FRAME_SIZE - n_left, 
252                free_list_index);
253             _vec_len (tm->rx_buffers) = n_left + n_alloc;
254           }
255       }
256
257       /* Allocate RX buffers from end of rx_buffers.
258          Turn them into iovecs to pass to readv. */
259       {
260         uword i_rx = vec_len (tm->rx_buffers) - 1;
261         vlib_buffer_t * b;
262         word j, n_bytes_left, n_bytes_in_packet;
263 #if DPDK == 1
264         u8 out_of_dpdk_buffers = 0;
265 #endif
266
267         /* We need enough buffers left for an MTU sized packet. */
268         if (PREDICT_FALSE(vec_len (tm->rx_buffers) < tm->mtu_buffers))
269           {
270             clib_bitmap_set (tm->pending_read_bitmap,
271                              ready_interface_indices[i], 1);
272             clib_warning ("buffer allocation failure");
273             continue;
274           }
275
276         vec_validate (tm->iovecs, tm->mtu_buffers - 1);
277         for (j = 0; j < tm->mtu_buffers; j++)
278           {
279             b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
280             tm->iovecs[j].iov_base = b->data;
281             tm->iovecs[j].iov_len = buffer_size;
282           }
283
284 #if DPDK == 1
285         if (PREDICT_FALSE(out_of_dpdk_buffers == 1))
286           continue;
287 #endif
288
289         n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
290         n_bytes_in_packet = n_bytes_left;
291         if (n_bytes_left <= 0)
292           {
293             if (errno != EAGAIN)
294               clib_unix_warning ("readv %d", n_bytes_left);
295             return 0;
296           }
297         
298         bi = tm->rx_buffers[i_rx];
299         while (1)
300           {
301             b = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
302
303             b->flags = 0;
304             b->current_data = 0;
305             b->current_length = n_bytes_left < buffer_size 
306               ? n_bytes_left : buffer_size;
307
308             n_bytes_left -= buffer_size;
309
310         if (n_bytes_left <= 0)
311           {
312 #if DPDK == 1
313               struct rte_mbuf *mb = (struct rte_mbuf *)(b - 1);
314               rte_pktmbuf_data_len (mb) = n_bytes_in_packet;
315               rte_pktmbuf_pkt_len (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             ASSERT(0); /* $$$$ fixme */
325             /* ((struct rte_pktmbuf *)(b->mb))->next = 
326                vlib_get_buffer (vm, tm->rx_buffers[i_rx])->mb; */
327 #endif
328           }
329
330         /* Interface counters for tapcli interface. */
331         vlib_increment_combined_counter 
332           (vnet_main.interface_main.combined_sw_if_counters
333            + VNET_INTERFACE_COUNTER_RX,
334            os_get_cpu_number(),
335            ti->sw_if_index,
336            1, n_bytes_in_packet);
337
338         _vec_len (tm->rx_buffers) = i_rx;
339       }
340
341       b = vlib_get_buffer (vm, bi);
342
343       /*
344        * Turn this on if you run into
345        * "bad monkey" contexts, and you want to know exactly
346        * which nodes they've visited... See .../vlib/vlib/buffer.h
347        */
348       VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b);
349
350       {
351         u32 next_index;
352         uword n_trace = vlib_get_trace_count (vm, node);
353
354         vnet_buffer (b)->sw_if_index[VLIB_RX] = ti->sw_if_index;
355         vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
356
357         b->error = node->errors[0];
358
359         {
360           next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
361
362           next_index = (ti->per_interface_next_index != ~0) ? 
363             ti->per_interface_next_index : next_index;
364         }
365         {
366           vnet_main_t *vnm = vnet_get_main();
367           vnet_sw_interface_t * si;
368           si = vnet_get_sw_interface (vnm, ti->sw_if_index);
369           if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
370             next_index = TAPCLI_RX_NEXT_DROP;
371         }
372
373
374         vlib_set_next_frame_buffer (vm, node, next_index, bi);
375         
376         if (n_trace > 0)
377           {
378             vlib_trace_buffer (vm, node, next_index,
379                                b, /* follow_chain */ 1);
380             vlib_set_trace_count (vm, node, n_trace - 1);
381           }
382       }
383     }
384   
385   return 1;
386 }
387
388 static char * tapcli_rx_error_strings[] = {
389   "Interface down",
390 };
391
392 VLIB_REGISTER_NODE (tapcli_rx_node,static) = {
393   .function = tapcli_rx,
394   .name = "tapcli-rx",
395   .type = VLIB_NODE_TYPE_INPUT,
396   .state = VLIB_NODE_STATE_INTERRUPT,
397   .vector_size = 4,
398   .n_errors = 1,
399   .error_strings = tapcli_rx_error_strings,
400
401   .n_next_nodes = TAPCLI_RX_N_NEXT,
402   .next_nodes = {
403     [TAPCLI_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
404     [TAPCLI_RX_NEXT_IP6_INPUT] = "ip6-input",
405     [TAPCLI_RX_NEXT_DROP] = "error-drop",
406     [TAPCLI_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
407   },
408 };
409
410 /* Gets called when file descriptor is ready from epoll. */
411 static clib_error_t * tapcli_read_ready (unix_file_t * uf)
412 {
413   vlib_main_t * vm = vlib_get_main();
414   tapcli_main_t * tm = &tapcli_main;
415   uword * p;
416   
417   /* Schedule the rx node */
418   vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index);
419
420   p = hash_get (tm->tapcli_interface_index_by_unix_fd, uf->file_descriptor);
421
422   /* Mark the specific tap interface ready-to-read */
423   if (p)
424     tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap,
425                                                p[0], 1);
426   else
427     clib_warning ("fd %d not in hash table", uf->file_descriptor);
428
429   return 0;
430 }
431
432 static clib_error_t *
433 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
434 {
435   tapcli_main_t *tm = &tapcli_main;
436 #if DPDK == 0
437   const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
438 #else
439   const uword buffer_size = MBUF_SIZE;
440 #endif
441
442   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
443     {
444       if (unformat (input, "mtu %d", &tm->mtu_bytes))
445         ;
446       else if (unformat (input, "disable"))
447         tm->is_disabled = 1;
448       else
449           return clib_error_return (0, "unknown input `%U'",
450                                     format_unformat_error, input);
451     }
452
453   if (tm->is_disabled)
454     return 0;
455
456   if (geteuid()) 
457     {
458       clib_warning ("tapcli disabled: must be superuser");
459       tm->is_disabled = 1;
460       return 0;
461     }    
462
463   tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
464   
465   return 0;
466 }
467
468 static int tap_name_renumber (vnet_hw_interface_t * hi,
469                               u32 new_dev_instance)
470 {
471   tapcli_main_t *tm = &tapcli_main;
472
473   vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance,
474                            hi->dev_instance, ~0);
475
476   tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
477     new_dev_instance;
478
479   return 0;
480 }
481
482 VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli");
483
484 static void
485 tapcli_nopunt_frame (vlib_main_t * vm,
486                    vlib_node_runtime_t * node,
487                    vlib_frame_t * frame)
488 {
489   u32 * buffers = vlib_frame_args (frame);
490   uword n_packets = frame->n_vectors;
491   vlib_buffer_free (vm, buffers, n_packets);
492   vlib_frame_free (vm, node, frame);
493 }
494
495 VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = {
496   .name = "tapcli",
497 };
498
499 static u8 * format_tapcli_interface_name (u8 * s, va_list * args)
500 {
501   u32 i = va_arg (*args, u32);
502   u32 show_dev_instance = ~0;
503   tapcli_main_t * tm = &tapcli_main;
504
505   if (i < vec_len (tm->show_dev_instance_by_real_dev_instance))
506     show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i];
507
508   if (show_dev_instance != ~0)
509     i = show_dev_instance;
510
511   s = format (s, "tap-%d", i);
512   return s;
513 }
514
515 static u32 tapcli_flag_change (vnet_main_t * vnm, 
516                                vnet_hw_interface_t * hw,
517                                u32 flags)
518 {
519   tapcli_main_t *tm = &tapcli_main;
520   tapcli_interface_t *ti;
521   struct ifreq ifr;
522   u32 want_promisc;
523
524   ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
525
526   memcpy (&ifr, &ti->ifr, sizeof (ifr));
527
528   /* get flags, modify to bring up interface... */
529   if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
530     {
531       clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
532       return 0;
533     }
534
535   want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
536
537   if (want_promisc == ti->is_promisc)
538     return 0;
539
540
541   if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
542     ifr.ifr_flags |= IFF_PROMISC;
543   else
544     ifr.ifr_flags &= ~(IFF_PROMISC);
545
546   /* get flags, modify to bring up interface... */
547   if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
548     {
549       clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
550       return 0;
551     }
552
553   ti->is_promisc = want_promisc;
554
555   return 0;
556 }
557
558 static void tapcli_set_interface_next_node (vnet_main_t *vnm, 
559                                             u32 hw_if_index,
560                                             u32 node_index)
561 {
562   tapcli_main_t *tm = &tapcli_main;
563   tapcli_interface_t *ti;
564   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
565
566   ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
567   
568   /* Shut off redirection */
569   if (node_index == ~0)
570     {
571       ti->per_interface_next_index = node_index;
572       return;
573     }
574   
575   ti->per_interface_next_index = 
576     vlib_node_add_next (tm->vlib_main, tapcli_rx_node.index, node_index);
577 }
578
579 /* 
580  * Mainly exists to set link_state == admin_state
581  * otherwise, e.g. ip6 neighbor discovery breaks
582  */
583 static clib_error_t * 
584 tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
585 {
586   uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
587   u32 hw_flags;
588   u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX 
589     | VNET_HW_INTERFACE_FLAG_SPEED_1G;
590     
591   if (is_admin_up)
592     hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
593   else
594     hw_flags = speed_duplex;
595   
596   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
597   return 0;
598 }
599
600 VNET_DEVICE_CLASS (tapcli_dev_class,static) = {
601   .name = "tapcli",
602   .tx_function = tapcli_tx,
603   .format_device_name = format_tapcli_interface_name,
604   .rx_redirect_to_node = tapcli_set_interface_next_node,
605   .name_renumber = tap_name_renumber,
606   .admin_up_down_function = tapcli_interface_admin_up_down,
607 };
608
609 int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids)
610 {
611   tapcli_main_t * tm = &tapcli_main;
612   tapcli_interface_t * ti;
613
614   tapcli_interface_details_t * r_tapids = NULL;
615   tapcli_interface_details_t * tapid = NULL;
616
617   vec_foreach (ti, tm->tapcli_interfaces) {
618     if (!ti->active)
619         continue;
620     vec_add2(r_tapids, tapid, 1);
621     tapid->sw_if_index = ti->sw_if_index;
622     strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1);
623   }
624
625   *out_tapids = r_tapids;
626
627   return 0;
628 }
629
630 /* get tap interface from inactive interfaces or create new */
631 static tapcli_interface_t *tapcli_get_new_tapif()
632 {
633   tapcli_main_t * tm = &tapcli_main;
634   tapcli_interface_t *ti = NULL;
635
636   int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces);
637   // if there are any inactive ifaces
638   if (inactive_cnt > 0) {
639     // take last
640     u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1];
641     if (vec_len(tm->tapcli_interfaces) > ti_idx) {
642       ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx);
643       clib_warning("reusing tap interface");
644     }
645     // "remove" from inactive list
646     _vec_len(tm->tapcli_inactive_interfaces) -= 1;
647   }
648
649   // ti was not retrieved from inactive ifaces - create new
650   if (!ti)
651     vec_add2 (tm->tapcli_interfaces, ti, 1);
652
653   return ti;
654 }
655
656 int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
657                       u32 * sw_if_indexp)
658 {
659   tapcli_main_t * tm = &tapcli_main;
660   tapcli_interface_t * ti = NULL;
661   struct ifreq ifr;
662   int flags;
663   int dev_net_tun_fd;
664   int dev_tap_fd = -1;
665   clib_error_t * error;
666   u8 hwaddr [6];
667   int rv = 0;
668
669   if (tm->is_disabled)
670     {
671       return VNET_API_ERROR_FEATURE_DISABLED;
672     }
673
674   flags = IFF_TAP | IFF_NO_PI;
675
676   if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
677     return VNET_API_ERROR_SYSCALL_ERROR_1;
678   
679   memset (&ifr, 0, sizeof (ifr));
680   strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
681   ifr.ifr_flags = flags;
682   if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
683     {
684       rv = VNET_API_ERROR_SYSCALL_ERROR_2;
685       goto error;
686     }
687     
688   /* Open a provisioning socket */
689   if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
690                            htons(ETH_P_ALL))) < 0 )
691     {
692       rv = VNET_API_ERROR_SYSCALL_ERROR_3;
693       goto error;
694     }
695
696   /* Find the interface index. */
697   {
698     struct ifreq ifr;
699     struct sockaddr_ll sll;
700
701     memset (&ifr, 0, sizeof(ifr));
702     strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
703     if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
704       {
705         rv = VNET_API_ERROR_SYSCALL_ERROR_4;
706         goto error;
707       }
708
709     /* Bind the provisioning socket to the interface. */
710     memset(&sll, 0, sizeof(sll));
711     sll.sll_family   = AF_PACKET;
712     sll.sll_ifindex  = ifr.ifr_ifindex;
713     sll.sll_protocol = htons(ETH_P_ALL);
714
715     if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
716       {
717         rv = VNET_API_ERROR_SYSCALL_ERROR_5;
718         goto error;
719       }
720   }
721
722   /* non-blocking I/O on /dev/tapX */
723   {
724     int one = 1;
725     if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
726       {
727         rv = VNET_API_ERROR_SYSCALL_ERROR_6;
728         goto error;
729       }
730   }
731   ifr.ifr_mtu = tm->mtu_bytes;
732   if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
733     {
734       rv = VNET_API_ERROR_SYSCALL_ERROR_7;
735       goto error;
736     }
737
738   /* get flags, modify to bring up interface... */
739   if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
740     {
741       rv = VNET_API_ERROR_SYSCALL_ERROR_8;
742       goto error;
743     }
744
745   ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
746
747   if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
748     {
749       rv = VNET_API_ERROR_SYSCALL_ERROR_9;
750       goto error;
751     }
752
753   if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
754     {
755       rv = VNET_API_ERROR_SYSCALL_ERROR_1;
756       goto error;
757     }
758
759   ti = tapcli_get_new_tapif();
760
761   if (hwaddr_arg != 0)
762     memcpy(hwaddr, hwaddr_arg, 6);
763
764   error = ethernet_register_interface
765         (tm->vnet_main,
766          tapcli_dev_class.index,
767          ti - tm->tapcli_interfaces /* device instance */,
768          hwaddr_arg != 0 ? hwaddr :
769          (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
770          &ti->hw_if_index, 
771          tapcli_flag_change);
772
773   if (error)
774     {
775       clib_error_report (error);
776       rv = VNET_API_ERROR_INVALID_REGISTRATION;
777       goto error;
778     }
779
780   {
781     unix_file_t template = {0};
782     template.read_function = tapcli_read_ready;
783     template.file_descriptor = dev_net_tun_fd;
784     ti->unix_file_index = unix_file_add (&unix_main, &template);
785     ti->unix_fd = dev_net_tun_fd;
786     ti->provision_fd = dev_tap_fd;
787     memcpy (&ti->ifr, &ifr, sizeof (ifr));
788   }
789   
790   {
791     vnet_hw_interface_t * hw;
792     hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
793     ti->sw_if_index = hw->sw_if_index;
794     if (sw_if_indexp)
795       *sw_if_indexp = hw->sw_if_index;
796   }
797
798   ti->active = 1;
799   
800   hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
801             ti - tm->tapcli_interfaces);
802   
803   hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
804             ti - tm->tapcli_interfaces);
805   
806   return rv;
807
808  error:
809   close (dev_net_tun_fd);
810   close (dev_tap_fd);
811
812   return rv;
813 }
814
815 int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
816                                u8 *hwaddr_arg, u32 * sw_if_indexp,
817                                u8 renumber, u32 custom_dev_instance)
818 {
819     int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
820
821     if (!rv && renumber)
822         vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
823
824     return rv;
825 }
826
827 static int tapcli_tap_disconnect (tapcli_interface_t *ti)
828 {
829   int rv = 0;
830   vnet_main_t * vnm = vnet_get_main();
831   tapcli_main_t * tm = &tapcli_main;
832   u32 sw_if_index = ti->sw_if_index;
833
834   // bring interface down
835   vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
836
837   if (ti->unix_file_index != ~0) {
838     unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
839     ti->unix_file_index = ~0;
840   }
841
842   hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
843   hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
844   close(ti->unix_fd);
845   close(ti->provision_fd);
846   ti->unix_fd = -1;
847   ti->provision_fd = -1;
848
849   return rv;
850 }
851
852 int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index)
853 {
854   int rv = 0;
855   tapcli_main_t * tm = &tapcli_main;
856   tapcli_interface_t *ti;
857   uword *p = NULL;
858
859   p = hash_get (tm->tapcli_interface_index_by_sw_if_index,
860                 sw_if_index);
861   if (p == 0) {
862     clib_warning ("sw_if_index %d unknown", sw_if_index);
863     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
864   }
865   ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
866
867   // inactive
868   ti->active = 0;
869   tapcli_tap_disconnect(ti);
870   // add to inactive list
871   vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces);
872
873   // reset renumbered iface
874   if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance))
875     tm->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
876
877   ethernet_delete_interface (tm->vnet_main, ti->hw_if_index);
878   return rv;
879 }
880
881 static clib_error_t *
882 tap_delete_command_fn (vlib_main_t * vm,
883                  unformat_input_t * input,
884                  vlib_cli_command_t * cmd)
885 {
886   tapcli_main_t * tm = &tapcli_main;
887   u32 sw_if_index = ~0;
888
889   if (tm->is_disabled)
890     {
891       return clib_error_return (0, "device disabled...");
892     }
893
894   if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
895                 &sw_if_index))
896       ;
897   else
898     return clib_error_return (0, "unknown input `%U'",
899                               format_unformat_error, input);
900
901
902   int rc = vnet_tap_delete (vm, sw_if_index);
903
904   if (!rc) {
905     vlib_cli_output (vm, "Deleted.");
906   } else {
907     vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
908   }
909
910   return 0;
911 }
912
913 VLIB_CLI_COMMAND (tap_delete_command, static) = {
914     .path = "tap delete",
915     .short_help = "tap delete <vpp-tap-intfc-name>",
916     .function = tap_delete_command_fn,
917 };
918
919 /* modifies tap interface - can result in new interface being created */
920 int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
921                      u8 * intfc_name, u8 *hwaddr_arg, 
922                      u32 * sw_if_indexp,
923                      u8 renumber, u32 custom_dev_instance)
924 {
925     int rv = vnet_tap_delete (vm, orig_sw_if_index);
926
927     if (rv)
928         return rv;
929
930     rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
931             renumber, custom_dev_instance);
932
933     return rv;
934 }
935
936 static clib_error_t *
937 tap_modify_command_fn (vlib_main_t * vm,
938                  unformat_input_t * input,
939                  vlib_cli_command_t * cmd)
940 {
941   u8 * intfc_name;
942   tapcli_main_t * tm = &tapcli_main;
943   u32 sw_if_index = ~0;
944   u32 new_sw_if_index = ~0;
945   int user_hwaddr = 0;
946   u8 hwaddr[6];
947     
948   if (tm->is_disabled)
949     {
950       return clib_error_return (0, "device disabled...");
951     }
952
953   if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main,
954                 &sw_if_index))
955       ;
956   else
957     return clib_error_return (0, "unknown input `%U'",
958                               format_unformat_error, input);
959
960   if (unformat (input, "%s", &intfc_name))
961     ;
962   else
963     return clib_error_return (0, "unknown input `%U'",
964                               format_unformat_error, input);
965   
966   if (unformat(input, "hwaddr %U", unformat_ethernet_address,
967                &hwaddr))
968     user_hwaddr = 1;
969
970
971   int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
972                             (user_hwaddr == 1 ? hwaddr : 0),
973                             &new_sw_if_index, 0, 0);
974
975   if (!rc) {
976     vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
977                    format_vnet_sw_if_index_name, tm->vnet_main, 
978                    new_sw_if_index, intfc_name);
979   } else {
980     vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
981   }
982
983   return 0;
984 }
985
986 VLIB_CLI_COMMAND (tap_modify_command, static) = {
987     .path = "tap modify",
988     .short_help = "tap modify <vpp-tap-intfc-name> <linux-intfc-name> [hwaddr [<addr> | random]]",
989     .function = tap_modify_command_fn,
990 };
991
992 static clib_error_t *
993 tap_connect_command_fn (vlib_main_t * vm,
994                  unformat_input_t * input,
995                  vlib_cli_command_t * cmd)
996 {
997   u8 * intfc_name;
998   tapcli_main_t * tm = &tapcli_main;
999   tapcli_interface_t * ti;
1000   struct ifreq ifr;
1001   int flags;
1002   int dev_net_tun_fd;
1003   int dev_tap_fd = -1;
1004   clib_error_t * error;
1005   int user_hwaddr = 0;
1006   u8 hwaddr[6];
1007     
1008   if (tm->is_disabled)
1009     {
1010       return clib_error_return (0, "device disabled...");
1011     }
1012
1013   if (unformat (input, "%s", &intfc_name))
1014     ;
1015   else
1016     return clib_error_return (0, "unknown input `%U'",
1017                               format_unformat_error, input);
1018   
1019   if (unformat(input, "hwaddr %U", unformat_ethernet_address,
1020                &hwaddr))
1021     user_hwaddr = 1;
1022
1023   flags = IFF_TAP | IFF_NO_PI;
1024
1025   if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
1026     {
1027       vlib_cli_output (vm, "Couldn't open /dev/net/tun");
1028       return 0;
1029     }
1030   
1031   memset (&ifr, 0, sizeof (ifr));
1032   strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1033   ifr.ifr_flags = flags;
1034   if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
1035     {
1036       vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
1037       goto error;
1038     }
1039     
1040   /* Open a provisioning socket */
1041   if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
1042                            htons(ETH_P_ALL))) < 0 )
1043     {
1044       vlib_cli_output (vm, "Couldn't open provisioning socket");
1045       goto error;
1046     }
1047
1048   /* Find the interface index. */
1049   {
1050     struct ifreq ifr;
1051     struct sockaddr_ll sll;
1052
1053     memset (&ifr, 0, sizeof(ifr));
1054     strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
1055     if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
1056       {
1057         vlib_cli_output (vm, "Couldn't get if_index");
1058         goto error;
1059       }
1060
1061     /* Bind the provisioning socket to the interface. */
1062     memset(&sll, 0, sizeof(sll));
1063     sll.sll_family   = AF_PACKET;
1064     sll.sll_ifindex  = ifr.ifr_ifindex;
1065     sll.sll_protocol = htons(ETH_P_ALL);
1066
1067     if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
1068       {
1069         vlib_cli_output (vm, "Couldn't bind provisioning socket");
1070         goto error;
1071       }
1072   }
1073
1074   /* non-blocking I/O on /dev/tapX */
1075   {
1076     int one = 1;
1077     if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
1078       {
1079         vlib_cli_output (0, "Couldn't set device non-blocking flag");
1080         goto error;
1081       }
1082   }
1083   ifr.ifr_mtu = tm->mtu_bytes;
1084   if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
1085     {
1086       vlib_cli_output (0, "Couldn't set device MTU");
1087       goto error;
1088     }
1089
1090   /* get flags, modify to bring up interface... */
1091   if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
1092     {
1093       vlib_cli_output (0, "Couldn't get interface flags");
1094       goto error;
1095     }
1096
1097   ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
1098
1099   if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
1100     {
1101       vlib_cli_output (0, "Couldn't set intfc admin state up");
1102       goto error;
1103     }
1104
1105   if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
1106     {
1107       vlib_cli_output (0, "Couldn't get intfc MAC address");
1108       goto error;
1109     }
1110
1111   ti = tapcli_get_new_tapif();
1112   ti->per_interface_next_index = ~0;
1113
1114   if (unformat(input, "hwaddr random"))
1115     {
1116       f64 now = vlib_time_now(vm);
1117       u32 rnd;
1118       rnd = (u32) (now * 1e6);
1119       rnd = random_u32 (&rnd);
1120
1121       memcpy (hwaddr+2, &rnd, sizeof(rnd));
1122       hwaddr[0] = 2;
1123       hwaddr[1] = 0xfe;
1124       user_hwaddr = 1;
1125     }
1126   
1127   error = ethernet_register_interface
1128         (tm->vnet_main,
1129          tapcli_dev_class.index,
1130          ti - tm->tapcli_interfaces /* device instance */,
1131          user_hwaddr ? hwaddr : 
1132          (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
1133          &ti->hw_if_index, 
1134          tapcli_flag_change);
1135
1136   if (error)
1137     clib_error_report (error);
1138
1139   {
1140     unix_file_t template = {0};
1141     template.read_function = tapcli_read_ready;
1142     template.file_descriptor = dev_net_tun_fd;
1143     ti->unix_file_index = unix_file_add (&unix_main, &template);
1144     ti->unix_fd = dev_net_tun_fd;
1145     ti->provision_fd = dev_tap_fd;
1146     memcpy (&ti->ifr, &ifr, sizeof (ifr));
1147   }
1148   
1149   {
1150     vnet_hw_interface_t * hw;
1151     hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
1152     ti->sw_if_index = hw->sw_if_index;
1153   }
1154
1155   ti->active = 1;
1156
1157   hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
1158             ti - tm->tapcli_interfaces);
1159
1160   hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
1161             ti - tm->tapcli_interfaces);
1162
1163   vlib_cli_output (vm, "Created %U for Linux tap '%s'",
1164                    format_vnet_sw_if_index_name, tm->vnet_main, 
1165                    ti->sw_if_index, intfc_name);
1166                    
1167   return 0;
1168
1169  error:
1170   close (dev_net_tun_fd);
1171   close (dev_tap_fd);
1172
1173   return 0;
1174 }
1175
1176 VLIB_CLI_COMMAND (tap_connect_command, static) = {
1177     .path = "tap connect",
1178     .short_help = "tap connect <intfc-name> [hwaddr [<addr> | random]]",
1179     .function = tap_connect_command_fn,
1180 };
1181
1182 clib_error_t *
1183 tapcli_init (vlib_main_t * vm)
1184 {
1185   tapcli_main_t * tm = &tapcli_main;
1186
1187   tm->vlib_main = vm;
1188   tm->vnet_main = vnet_get_main();
1189   tm->unix_main = &unix_main;
1190   tm->mtu_bytes = 4096 + 256;
1191   tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
1192   tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
1193   vm->os_punt_frame = tapcli_nopunt_frame;
1194
1195   return 0;
1196 }
1197
1198 VLIB_INIT_FUNCTION (tapcli_init);
1199
1200