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