session: add support for application namespacing
[vpp.git] / src / vnet / unix / tapcli.c
index 9862a2b..d80cca3 100644 (file)
@@ -41,6 +41,7 @@
 
 #include <vnet/feature/feature.h>
 #include <vnet/devices/devices.h>
+#include <vnet/unix/tuntap.h>
 #include <vnet/unix/tapcli.h>
 
 static vnet_device_class_t tapcli_dev_class;
@@ -55,7 +56,7 @@ static void tapcli_nopunt_frame (vlib_main_t * vm,
  */
 typedef struct {
   u32 unix_fd;
-  u32 unix_file_index;
+  u32 clib_file_index;
   u32 provision_fd;
   /** For counters */
   u32 sw_if_index;
@@ -95,16 +96,25 @@ u8 * format_tapcli_rx_trace (u8 * s, va_list * va)
 }
 
 /**
- * @brief TAPCLI main state struct
+ * @brief TAPCLI per thread struct
  */
-typedef struct {
-  /** Vector of iovecs for readv/writev calls. */
-  struct iovec * iovecs;
-
+typedef struct
+{
   /** Vector of VLIB rx buffers to use.  We allocate them in blocks
      of VLIB_FRAME_SIZE (256). */
   u32 * rx_buffers;
 
+  /** Vector of iovecs for readv/writev calls. */
+  struct iovec * iovecs;  
+} tapcli_per_thread_t;
+
+/**
+ * @brief TAPCLI main state struct
+ */
+typedef struct {
+  /** per thread variables */
+  tapcli_per_thread_t * threads;
+
   /** tap device destination MAC address. Required, or Linux drops pkts */
   u8 ether_dst_mac[6];
 
@@ -136,8 +146,6 @@ typedef struct {
   vlib_main_t * vlib_main;
   /** convenience - vnet_main_t */
   vnet_main_t * vnet_main;
-  /** convenience - unix_main_t */
-  unix_main_t * unix_main;
 } tapcli_main_t;
 
 static tapcli_main_t tapcli_main;
@@ -166,6 +174,7 @@ tapcli_tx (vlib_main_t * vm,
   tapcli_main_t * tm = &tapcli_main;
   tapcli_interface_t * ti;
   int i;
+  u16 thread_index = vlib_get_thread_index ();
 
   for (i = 0; i < n_packets; i++)
     {
@@ -200,11 +209,11 @@ tapcli_tx (vlib_main_t * vm,
         ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]);
 
       /* Re-set iovecs if present. */
-      if (tm->iovecs)
-       _vec_len (tm->iovecs) = 0;
+      if (tm->threads[thread_index].iovecs)
+       _vec_len (tm->threads[thread_index].iovecs) = 0;
 
       /* VLIB buffer chain -> Unix iovec(s). */
-      vec_add2 (tm->iovecs, iov, 1);
+      vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
       iov->iov_base = b->data + b->current_data;
       iov->iov_len = l = b->current_length;
 
@@ -213,7 +222,7 @@ tapcli_tx (vlib_main_t * vm,
          do {
            b = vlib_get_buffer (vm, b->next_buffer);
 
-           vec_add2 (tm->iovecs, iov, 1);
+           vec_add2 (tm->threads[thread_index].iovecs, iov, 1);
 
            iov->iov_base = b->data + b->current_data;
            iov->iov_len = b->current_length;
@@ -221,7 +230,8 @@ tapcli_tx (vlib_main_t * vm,
          } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
        }
 
-      if (writev (ti->unix_fd, tm->iovecs, vec_len (tm->iovecs)) < l)
+      if (writev (ti->unix_fd, tm->threads[thread_index].iovecs,
+                 vec_len (tm->threads[thread_index].iovecs)) < l)
        clib_unix_warning ("writev");
     }
 
@@ -256,7 +266,7 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
   const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
   u32 n_trace = vlib_get_trace_count (vm, node);
   u8 set_trace = 0;
-
+  u16 thread_index = vlib_get_thread_index ();
   vnet_main_t *vnm;
   vnet_sw_interface_t * si;
   u8 admin_down;
@@ -276,31 +286,35 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
     word n_bytes_in_packet;
     int j, n_bytes_left;
 
-    if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
-      uword len = vec_len(tm->rx_buffers);
-      _vec_len(tm->rx_buffers) +=
-          vlib_buffer_alloc_from_free_list(vm, &tm->rx_buffers[len],
+    if (PREDICT_FALSE(vec_len(tm->threads[thread_index].rx_buffers) <
+                     tm->mtu_buffers)) {
+      uword len = vec_len(tm->threads[thread_index].rx_buffers);
+      _vec_len(tm->threads[thread_index].rx_buffers) +=
+          vlib_buffer_alloc_from_free_list(vm, &tm->threads[thread_index].rx_buffers[len],
                             VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-      if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
+      if (PREDICT_FALSE(vec_len(tm->threads[thread_index].rx_buffers) <
+                       tm->mtu_buffers)) {
           vlib_node_increment_counter(vm, tapcli_rx_node.index,
                                       TAPCLI_ERROR_BUFFER_ALLOC,
-                                      tm->mtu_buffers - vec_len(tm->rx_buffers));
+                                      tm->mtu_buffers -
+                                     vec_len(tm->threads[thread_index].rx_buffers));
         break;
       }
     }
 
-    uword i_rx = vec_len (tm->rx_buffers) - 1;
+    uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1;
 
     /* Allocate RX buffers from end of rx_buffers.
            Turn them into iovecs to pass to readv. */
-    vec_validate (tm->iovecs, tm->mtu_buffers - 1);
+    vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1);
     for (j = 0; j < tm->mtu_buffers; j++) {
-      b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
-      tm->iovecs[j].iov_base = b->data;
-      tm->iovecs[j].iov_len = buffer_size;
+      b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx - j]);
+      tm->threads[thread_index].iovecs[j].iov_base = b->data;
+      tm->threads[thread_index].iovecs[j].iov_len = buffer_size;
     }
 
-    n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
+    n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs,
+                         tm->mtu_buffers);
     n_bytes_in_packet = n_bytes_left;
     if (n_bytes_left <= 0) {
       if (errno != EAGAIN) {
@@ -310,8 +324,9 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
       break;
     }
 
-    bi_first = tm->rx_buffers[i_rx];
-    b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
+    bi_first = tm->threads[thread_index].rx_buffers[i_rx];
+    b = b_first = vlib_get_buffer (vm,
+                                  tm->threads[thread_index].rx_buffers[i_rx]);
     prev = NULL;
 
     while (1) {
@@ -329,11 +344,11 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
         break;
 
       i_rx--;
-      bi = tm->rx_buffers[i_rx];
+      bi = tm->threads[thread_index].rx_buffers[i_rx];
       b = vlib_get_buffer (vm, bi);
     }
 
-    _vec_len (tm->rx_buffers) = i_rx;
+    _vec_len (tm->threads[thread_index].rx_buffers) = i_rx;
 
     b_first->total_length_not_including_first_buffer =
         (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0;
@@ -354,8 +369,7 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
     to_next++;
     n_left_to_next--;
 
-    vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, 
-                                        b_first, 0);
+    vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, b_first);
 
     vlib_validate_buffer_enqueue_x1 (vm, node, next,
                                      to_next, n_left_to_next,
@@ -366,7 +380,7 @@ static uword tapcli_rx_iface(vlib_main_t * vm,
       vlib_increment_combined_counter (
           vnet_main.interface_main.combined_sw_if_counters
           + VNET_INTERFACE_COUNTER_RX,
-          os_get_cpu_number(), ti->sw_if_index,
+          thread_index, ti->sw_if_index,
           1, n_bytes_in_packet);
 
       if (PREDICT_FALSE(n_trace > 0)) {
@@ -453,12 +467,12 @@ VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
 /**
  * @brief Gets called when file descriptor is ready from epoll.
  *
- * @param *uf - unix_file_t
+ * @param *uf - clib_file_t
  *
  * @return error - clib_error_t
  *
  */
-static clib_error_t * tapcli_read_ready (unix_file_t * uf)
+static clib_error_t * tapcli_read_ready (clib_file_t * uf)
 {
   vlib_main_t * vm = vlib_get_main();
   tapcli_main_t * tm = &tapcli_main;
@@ -780,19 +794,23 @@ static tapcli_interface_t *tapcli_get_new_tapif()
   return ti;
 }
 
+typedef struct 
+{
+    ip6_address_t addr;
+    u32 mask_width;
+    unsigned int ifindex;
+} ip6_ifreq_t;
+
 /**
  * @brief Connect a TAP interface
  *
  * @param vm - vlib_main_t
- * @param intfc_name - u8
- * @param hwaddr_arg - u8
- * @param sw_if_indexp - u32
+ * @param ap - vnet_tap_connect_args_t 
  *
  * @return rc - int
  *
  */
-int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
-                      u32 * sw_if_indexp)
+int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
 {
   tapcli_main_t * tm = &tapcli_main;
   tapcli_interface_t * ti = NULL;
@@ -815,7 +833,7 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     return VNET_API_ERROR_SYSCALL_ERROR_1;
 
   memset (&ifr, 0, sizeof (ifr));
-  strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
+  strncpy(ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
   ifr.ifr_flags = flags;
   if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
     {
@@ -837,7 +855,7 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     struct sockaddr_ll sll;
 
     memset (&ifr, 0, sizeof(ifr));
-    strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
+    strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1);
     if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
       {
         rv = VNET_API_ERROR_SYSCALL_ERROR_4;
@@ -888,11 +906,85 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
       goto error;
     }
 
+  if (ap->ip4_address_set)
+    {
+      struct sockaddr_in sin;
+      /* ip4: mask defaults to /24 */
+      u32 mask = clib_host_to_net_u32 (0xFFFFFF00);
+
+      memset(&sin, 0, sizeof(sin));
+      sin.sin_family = AF_INET;
+      /* sin.sin_port = 0; */
+      sin.sin_addr.s_addr = ap->ip4_address->as_u32;
+      memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
+
+      if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0)
+        {
+          rv = VNET_API_ERROR_SYSCALL_ERROR_10;
+          goto error;
+        }
+
+      if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33)
+        {
+          mask = ~0;
+          mask <<= (32 - ap->ip4_mask_width);
+        }
+
+      mask = clib_host_to_net_u32(mask);
+      sin.sin_family = AF_INET;
+      sin.sin_port = 0;
+      sin.sin_addr.s_addr = mask;
+      memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin));
+
+      if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0)
+        {
+          rv = VNET_API_ERROR_SYSCALL_ERROR_10;
+          goto error;
+        }
+    }
+
+  if (ap->ip6_address_set)
+    {
+      struct ifreq ifr2;
+      ip6_ifreq_t ifr6;
+      int sockfd6;
+
+      sockfd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP);
+      if (sockfd6 < 0)
+        {
+          rv = VNET_API_ERROR_SYSCALL_ERROR_10;
+          goto error;
+        }
+
+      memset (&ifr2, 0, sizeof(ifr));
+      strncpy (ifr2.ifr_name, (char *) ap->intfc_name, 
+               sizeof (ifr2.ifr_name)-1);
+      if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0 )
+        {
+          close (sockfd6);
+          rv = VNET_API_ERROR_SYSCALL_ERROR_4;
+          goto error;
+        }
+      
+      memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t));
+      ifr6.mask_width = ap->ip6_mask_width;
+      ifr6.ifindex = ifr2.ifr_ifindex;
+      
+      if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0)
+        {
+          close (sockfd6);
+          clib_unix_warning ("ifr6");
+          rv = VNET_API_ERROR_SYSCALL_ERROR_10;
+          goto error;
+        }
+      close (sockfd6);
+    }
+
   ti = tapcli_get_new_tapif();
   ti->per_interface_next_index = ~0;
 
-  if (hwaddr_arg != 0)
-    clib_memcpy(hwaddr, hwaddr_arg, 6);
+  if (ap->hwaddr_arg != 0)
+    clib_memcpy(hwaddr, ap->hwaddr_arg, 6);
   else
     {
       f64 now = vlib_time_now(vm);
@@ -921,10 +1013,10 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     }
 
   {
-    unix_file_t template = {0};
+    clib_file_t template = {0};
     template.read_function = tapcli_read_ready;
     template.file_descriptor = dev_net_tun_fd;
-    ti->unix_file_index = unix_file_add (&unix_main, &template);
+    ti->clib_file_index = clib_file_add (&file_main, &template);
     ti->unix_fd = dev_net_tun_fd;
     ti->provision_fd = dev_tap_fd;
     clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
@@ -937,8 +1029,8 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     hw->max_supported_packet_bytes = TAP_MTU_MAX;
     hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t);
     ti->sw_if_index = hw->sw_if_index;
-    if (sw_if_indexp)
-      *sw_if_indexp = hw->sw_if_index;
+    if (ap->sw_if_indexp)
+      *(ap->sw_if_indexp) = hw->sw_if_index;
   }
 
   ti->active = 1;
@@ -972,16 +1064,15 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
  * @return rc - int
  *
  */
-int vnet_tap_connect_renumber (vlib_main_t * vm, u8 * intfc_name,
-                               u8 *hwaddr_arg, u32 * sw_if_indexp,
-                               u8 renumber, u32 custom_dev_instance)
+int vnet_tap_connect_renumber (vlib_main_t * vm, 
+                               vnet_tap_connect_args_t *ap)
 {
-    int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, sw_if_indexp);
+  int rv = vnet_tap_connect(vm, ap);
 
-    if (!rv && renumber)
-        vnet_interface_name_renumber (*sw_if_indexp, custom_dev_instance);
+  if (!rv && ap->renumber)
+    vnet_interface_name_renumber (*(ap->sw_if_indexp), ap->custom_dev_instance);
 
-    return rv;
+  return rv;
 }
 
 /**
@@ -1002,9 +1093,9 @@ static int tapcli_tap_disconnect (tapcli_interface_t *ti)
   // bring interface down
   vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
 
-  if (ti->unix_file_index != ~0) {
-    unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
-    ti->unix_file_index = ~0;
+  if (ti->clib_file_index != ~0) {
+    clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index);
+    ti->clib_file_index = ~0;
   }
   else
     close(ti->unix_fd);
@@ -1118,18 +1209,14 @@ VLIB_CLI_COMMAND (tap_delete_command, static) = {
  * @return rc - int
  *
  */
-int vnet_tap_modify (vlib_main_t * vm, u32 orig_sw_if_index,
-                     u8 * intfc_name, u8 *hwaddr_arg, 
-                     u32 * sw_if_indexp,
-                     u8 renumber, u32 custom_dev_instance)
+int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *ap)
 {
-    int rv = vnet_tap_delete (vm, orig_sw_if_index);
+    int rv = vnet_tap_delete (vm, ap->orig_sw_if_index);
 
     if (rv)
-        return rv;
+      return rv;
 
-    rv = vnet_tap_connect_renumber(vm, intfc_name, hwaddr_arg, sw_if_indexp,
-            renumber, custom_dev_instance);
+    rv = vnet_tap_connect_renumber(vm, ap);
 
     return rv;
 }
@@ -1155,6 +1242,7 @@ tap_modify_command_fn (vlib_main_t * vm,
   u32 new_sw_if_index = ~0;
   int user_hwaddr = 0;
   u8 hwaddr[6];
+  vnet_tap_connect_args_t _a, *ap= &_a;
 
   if (tm->is_disabled)
     {
@@ -1179,14 +1267,19 @@ tap_modify_command_fn (vlib_main_t * vm,
     user_hwaddr = 1;
 
 
-  int rc = vnet_tap_modify (vm, sw_if_index, intfc_name,
-                            (user_hwaddr == 1 ? hwaddr : 0),
-                            &new_sw_if_index, 0, 0);
+  memset (ap, 0, sizeof(*ap));
+  ap->orig_sw_if_index = sw_if_index;
+  ap->intfc_name = intfc_name;
+  ap->sw_if_indexp = &new_sw_if_index;
+  if (user_hwaddr)
+    ap->hwaddr_arg = hwaddr;
+
+  int rc = vnet_tap_modify (vm, ap);
 
   if (!rc) {
     vlib_cli_output (vm, "Modified %U for Linux tap '%s'",
                    format_vnet_sw_if_index_name, tm->vnet_main, 
-                   new_sw_if_index, intfc_name);
+                     *(ap->sw_if_indexp), ap->intfc_name);
   } else {
     vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc);
   }
@@ -1215,87 +1308,149 @@ tap_connect_command_fn (vlib_main_t * vm,
                 unformat_input_t * input,
                 vlib_cli_command_t * cmd)
 {
-  u8 * intfc_name;
+  u8 * intfc_name = 0;
+  unformat_input_t _line_input, *line_input = &_line_input;
+  vnet_tap_connect_args_t _a, *ap= &_a;
   tapcli_main_t * tm = &tapcli_main;
   u8 hwaddr[6];
   u8 *hwaddr_arg = 0;
   u32 sw_if_index;
+  ip4_address_t ip4_address;
+  int ip4_address_set = 0;
+  ip6_address_t ip6_address;
+  int ip6_address_set = 0;
+  u32 ip4_mask_width = 0;
+  u32 ip6_mask_width = 0;
+  clib_error_t *error = NULL;
 
   if (tm->is_disabled)
+    return clib_error_return (0, "device disabled...");
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      return clib_error_return (0, "device disabled...");
+      if (unformat(line_input, "hwaddr %U", unformat_ethernet_address,
+                   &hwaddr))
+        hwaddr_arg = hwaddr;
+      
+      /* It is here for backward compatibility */
+      else if (unformat(line_input, "hwaddr random"))
+        ;
+      
+      else if (unformat (line_input, "address %U/%d",
+                         unformat_ip4_address, &ip4_address, &ip4_mask_width))
+        ip4_address_set = 1;
+      
+      else if (unformat (line_input, "address %U/%d",
+                         unformat_ip6_address, &ip6_address, &ip6_mask_width))
+        ip6_address_set = 1;
+      
+      else if (unformat (line_input, "%s", &intfc_name))
+        ;
+      else
+        {
+          error = clib_error_return (0, "unknown input `%U'",
+                                     format_unformat_error, line_input);
+          goto done;
+        }
     }
-
-  if (unformat (input, "%s", &intfc_name))
-    ;
-  else
-    return clib_error_return (0, "unknown input `%U'",
-                              format_unformat_error, input);
   
-  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
-               &hwaddr))
-    hwaddr_arg = hwaddr;
+  if (intfc_name == 0)
+    {
+      error = clib_error_return (0, "interface name must be specified");
+      goto done;
+    }
 
-  /* It is here for backward compatibility */
-  if (unformat(input, "hwaddr random"))
-    ;
+  memset (ap, 0, sizeof (*ap));
 
-  int rv = vnet_tap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
-  if (rv) {
-    switch (rv) {
-    case VNET_API_ERROR_SYSCALL_ERROR_1:
-      vlib_cli_output (vm, "Couldn't open /dev/net/tun");
-      break;
+  ap->intfc_name = intfc_name;
+  ap->hwaddr_arg = hwaddr_arg;
+  if (ip4_address_set)
+    {
+      ap->ip4_address = &ip4_address;
+      ap->ip4_mask_width = ip4_mask_width;
+      ap->ip4_address_set = 1;
+    }
+  if (ip6_address_set)
+    {
+      ap->ip6_address = &ip6_address;
+      ap->ip6_mask_width = ip6_mask_width;
+      ap->ip6_address_set = 1;
+    }
+
+  ap->sw_if_indexp = &sw_if_index;
 
+  int rv = vnet_tap_connect(vm, ap);
+
+  switch (rv) 
+    {
+    case VNET_API_ERROR_SYSCALL_ERROR_1:
+      error = clib_error_return (0, "Couldn't open /dev/net/tun");
+      goto done;
+      
     case VNET_API_ERROR_SYSCALL_ERROR_2:
-      vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
-      break;
-  
+      error = clib_error_return (0, "Error setting flags on '%s'", intfc_name);
+      goto done;
+
     case VNET_API_ERROR_SYSCALL_ERROR_3:
-      vlib_cli_output (vm, "Couldn't open provisioning socket");
-      break;
+      error = clib_error_return (0,  "Couldn't open provisioning socket");
+      goto done;
 
     case VNET_API_ERROR_SYSCALL_ERROR_4:
-      vlib_cli_output (vm, "Couldn't get if_index");
-      break;
+      error = clib_error_return (0,  "Couldn't get if_index");
+      goto done;
     
     case VNET_API_ERROR_SYSCALL_ERROR_5:
-      vlib_cli_output (vm, "Couldn't bind provisioning socket");
-      break;
+      error = clib_error_return (0,  "Couldn't bind provisioning socket");
+      goto done;
 
     case VNET_API_ERROR_SYSCALL_ERROR_6:
-      vlib_cli_output (0, "Couldn't set device non-blocking flag");
-      break;
+      error = clib_error_return (0,  "Couldn't set device non-blocking flag");
+      goto done;
 
     case VNET_API_ERROR_SYSCALL_ERROR_7:
-      vlib_cli_output (0, "Couldn't set device MTU");
-      break;
+      error = clib_error_return (0,  "Couldn't set device MTU");
+      goto done;
 
     case VNET_API_ERROR_SYSCALL_ERROR_8:
-      vlib_cli_output (0, "Couldn't get interface flags");
-      break;
+      error = clib_error_return (0,  "Couldn't get interface flags");
+      goto done;
 
     case VNET_API_ERROR_SYSCALL_ERROR_9:
-      vlib_cli_output (0, "Couldn't set intfc admin state up");
-      break;
+      error = clib_error_return (0,  "Couldn't set intfc admin state up");
+      goto done;
+
+    case VNET_API_ERROR_SYSCALL_ERROR_10:
+      error = clib_error_return (0,  "Couldn't set intfc address/mask");
+      goto done;
 
     case VNET_API_ERROR_INVALID_REGISTRATION:
-      vlib_cli_output (0, "Invalid registration");
+      error = clib_error_return (0,  "Invalid registration");
+      goto done;
+
+    case 0:
       break;
+
     default:
-      vlib_cli_output (0, "Unknown error: %d", rv);
-      break;
+      error = clib_error_return (0,  "Unknown error: %d", rv);
+      goto done;
     }
-    return 0;
-  }
 
-  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
-  return 0;
-  }
+  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, 
+                  vnet_get_main(), sw_if_index);
+
+done:
+  unformat_free (line_input);
+
+  return error;
+}
 
 VLIB_CLI_COMMAND (tap_connect_command, static) = {
     .path = "tap connect",
-    .short_help = "tap connect <intfc-name> [hwaddr <addr>]",
+    .short_help =
+       "tap connect <intfc-name> [address <ip-addr>/mw] [hwaddr <addr>]",
     .function = tap_connect_command_fn,
 };
 
@@ -1311,17 +1466,25 @@ clib_error_t *
 tapcli_init (vlib_main_t * vm)
 {
   tapcli_main_t * tm = &tapcli_main;
+  vlib_thread_main_t * m = vlib_get_thread_main ();
+  tapcli_per_thread_t * thread;
 
   tm->vlib_main = vm;
   tm->vnet_main = vnet_get_main();
-  tm->unix_main = &unix_main;
   tm->mtu_bytes = TAP_MTU_DEFAULT;
   tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
   tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
-  tm->rx_buffers = 0;
-  vec_alloc(tm->rx_buffers, VLIB_FRAME_SIZE);
-  vec_reset_length(tm->rx_buffers);
   vm->os_punt_frame = tapcli_nopunt_frame;
+  vec_validate_aligned (tm->threads, m->n_vlib_mains - 1,
+                       CLIB_CACHE_LINE_BYTES);
+  vec_foreach (thread, tm->threads)
+    {
+      thread->iovecs = 0;
+      thread->rx_buffers = 0;
+      vec_alloc(thread->rx_buffers, VLIB_FRAME_SIZE);
+      vec_reset_length(thread->rx_buffers);
+    }
+
   return 0;
 }