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