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