VPP-81: Print interface name after creating an interface with CLI
[vpp.git] / vnet / vnet / unix / tapcli.c
index 4421ffb..1d5beba 100644 (file)
@@ -43,6 +43,7 @@
 
 static vnet_device_class_t tapcli_dev_class;
 static vnet_hw_interface_class_t tapcli_interface_class;
+static vlib_node_registration_t tapcli_rx_node;
 
 static void tapcli_nopunt_frame (vlib_main_t * vm,
                                  vlib_node_runtime_t * node,
@@ -59,6 +60,21 @@ typedef struct {
   u8 active;                    /* for delete */
 } tapcli_interface_t;
 
+typedef struct {
+  u16 sw_if_index;
+} tapcli_rx_trace_t;
+
+u8 * format_tapcli_rx_trace (u8 * s, va_list * va)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
+  vnet_main_t * vnm = vnet_get_main();
+  tapcli_rx_trace_t * t = va_arg (*va, tapcli_rx_trace_t *);
+  s = format (s, "%U", format_vnet_sw_if_index_name,
+                vnm, t->sw_if_index);
+  return s;
+}
+
 typedef struct {
   /* Vector of iovecs for readv/writev calls. */
   struct iovec * iovecs;
@@ -197,24 +213,157 @@ enum {
   TAPCLI_RX_N_NEXT,
 };
 
+
+
+static uword tapcli_rx_iface(vlib_main_t * vm,
+                            vlib_node_runtime_t * node,
+                            tapcli_interface_t * ti)
+{
+  tapcli_main_t * tm = &tapcli_main;
+  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
+  u32 n_trace = vlib_get_trace_count (vm, node);
+  u8 set_trace = 0;
+
+  vnet_main_t *vnm;
+  vnet_sw_interface_t * si;
+  u8 admin_down;
+  u32 next = node->cached_next_index;
+  u32 n_left_to_next, next_index;
+  u32 *to_next;
+
+  vnm = vnet_get_main();
+  si = vnet_get_sw_interface (vnm, ti->sw_if_index);
+  admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
+
+  vlib_get_next_frame(vm, node, next, to_next, n_left_to_next);
+
+  while (n_left_to_next) { // Fill at most one vector
+    vlib_buffer_t *b_first, *b, *prev;
+    u32 bi_first, bi;
+    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],
+                            VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+      if (PREDICT_FALSE(vec_len(tm->rx_buffers) < tm->mtu_buffers)) {
+        clib_warning("vlib_buffer_alloc failed");
+        break;
+      }
+    }
+
+    uword i_rx = vec_len (tm->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);
+    for (j = 0; j < tm->mtu_buffers; j++) {
+      b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
+      b->clone_count = 0;
+      tm->iovecs[j].iov_base = b->data;
+      tm->iovecs[j].iov_len = buffer_size;
+    }
+
+    n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
+    n_bytes_in_packet = n_bytes_left;
+    if (n_bytes_left <= 0) {
+      if (errno != EAGAIN) {
+        vlib_node_increment_counter(vm, tapcli_rx_node.index,
+                                    TAPCLI_ERROR_READ, 1);
+      }
+      break;
+    }
+
+    bi_first = tm->rx_buffers[i_rx];
+    b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
+    prev = NULL;
+
+    while (1) {
+      b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
+      n_bytes_left -= buffer_size;
+
+      if (prev) {
+        prev->next_buffer = bi;
+        prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
+      }
+      prev = b;
+
+      /* last segment */
+      if (n_bytes_left <= 0)
+        break;
+
+      i_rx--;
+      bi = tm->rx_buffers[i_rx];
+      b = vlib_get_buffer (vm, bi);
+    }
+
+    _vec_len (tm->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;
+    b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
+
+    /* Ensure mbufs are updated */
+    vlib_buffer_chain_validate(vm, b_first);
+
+    VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
+
+    vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
+    vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
+
+    b_first->error = node->errors[TAPCLI_ERROR_NONE];
+    next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
+    next_index = (ti->per_interface_next_index != ~0) ?
+        ti->per_interface_next_index : next_index;
+    next_index = admin_down ? TAPCLI_RX_NEXT_DROP : next_index;
+
+    to_next[0] = bi_first;
+    to_next++;
+    n_left_to_next--;
+
+    vlib_validate_buffer_enqueue_x1 (vm, node, next,
+                                     to_next, n_left_to_next,
+                                     bi_first, next_index);
+
+    /* Interface counters for tapcli interface. */
+    if (PREDICT_TRUE(!admin_down)) {
+      vlib_increment_combined_counter (
+          vnet_main.interface_main.combined_sw_if_counters
+          + VNET_INTERFACE_COUNTER_RX,
+          os_get_cpu_number(), ti->sw_if_index,
+          1, n_bytes_in_packet);
+
+      if (PREDICT_FALSE(n_trace > 0)) {
+        vlib_trace_buffer (vm, node, next_index,
+                           b_first, /* follow_chain */ 1);
+        n_trace--;
+        set_trace = 1;
+        tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0));
+        t0->sw_if_index = si->sw_if_index;
+      }
+    }
+  }
+  vlib_put_next_frame (vm, node, next, n_left_to_next);
+  if (set_trace)
+    vlib_set_trace_count (vm, node, n_trace);
+  return VLIB_FRAME_SIZE - n_left_to_next;
+}
+
 static uword
 tapcli_rx (vlib_main_t * vm,
           vlib_node_runtime_t * node,
           vlib_frame_t * frame)
 {
   tapcli_main_t * tm = &tapcli_main;
-  vlib_buffer_t *b_first;
-  u32 bi;
-  vlib_buffer_free_list_t *fl;
-  const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
   static u32 * ready_interface_indices;
   tapcli_interface_t * ti;
   int i;
-  word n_bytes_in_packet;
+  u32 total_count = 0;
 
   vec_reset_length (ready_interface_indices);
-
-  clib_bitmap_foreach (i, tm->pending_read_bitmap, 
+  clib_bitmap_foreach (i, tm->pending_read_bitmap,
   ({
     vec_add1 (ready_interface_indices, i);
   }));
@@ -222,161 +371,33 @@ tapcli_rx (vlib_main_t * vm,
   if (vec_len (ready_interface_indices) == 0)
     return 1;
 
-  fl = vlib_buffer_get_free_list(vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-
   for (i = 0; i < vec_len(ready_interface_indices); i++)
-    {
-      /* Clear the "interrupt" bit */
-      tm->pending_read_bitmap = 
+  {
+    tm->pending_read_bitmap =
         clib_bitmap_set (tm->pending_read_bitmap,
                          ready_interface_indices[i], 0);
-      
-      ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
-
-      /* Make sure we have some RX buffers. */
-      {
-        uword n_left = vec_len (tm->rx_buffers);
-        uword n_alloc;
-        if (n_left < VLIB_FRAME_SIZE / 2) {
-         vec_validate(tm->rx_buffers, VLIB_FRAME_SIZE + n_left - 1);
-         n_alloc = vlib_buffer_alloc(vm, &tm->rx_buffers[n_left], VLIB_FRAME_SIZE);
-         n_left += n_alloc;
-         _vec_len (tm->rx_buffers) = n_left;
-       }
-      }
-
-      /* Allocate RX buffers from end of rx_buffers.
-         Turn them into iovecs to pass to readv. */
-      {
-        uword i_rx = vec_len (tm->rx_buffers) - 1;
-        vlib_buffer_t * b, *prev = 0;
-        word j, n_bytes_left;
-
-        /* We need enough buffers left for an MTU sized packet. */
-        if (PREDICT_FALSE(vec_len (tm->rx_buffers) < tm->mtu_buffers))
-          {
-            clib_bitmap_set (tm->pending_read_bitmap,
-                             ready_interface_indices[i], 1);
-            clib_warning ("buffer allocation failure");
-            continue;
-          }
-
-        vec_validate (tm->iovecs, tm->mtu_buffers - 1);
-        for (j = 0; j < tm->mtu_buffers; j++)
-          {
-            b = vlib_get_buffer (vm, tm->rx_buffers[i_rx - j]);
-           vlib_buffer_init_for_free_list (b, fl);
-            tm->iovecs[j].iov_base = b->data;
-            tm->iovecs[j].iov_len = buffer_size;
-          }
-
-        n_bytes_left = readv (ti->unix_fd, tm->iovecs, tm->mtu_buffers);
-        n_bytes_in_packet = n_bytes_left;
-        if (n_bytes_left <= 0)
-          {
-            if (errno != EAGAIN)
-              clib_unix_warning ("readv %d", n_bytes_left);
-            return 0;
-          }
-        
-        bi = tm->rx_buffers[i_rx];
-       b = b_first = vlib_get_buffer (vm, tm->rx_buffers[i_rx]);
-
-        while (1) {
-         u32 bi;
-
-         vlib_buffer_init_for_free_list(b, fl);
-
-         b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
-         n_bytes_left -= buffer_size;
-
-         if (prev) {
-           prev->next_buffer = bi;
-           prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
-         }
-         prev = b;
-
-         /* last segment */
-         if (n_bytes_left <= 0) break;
-
-         i_rx--;
-         bi = tm->rx_buffers[i_rx];
-         b = vlib_get_buffer (vm, bi);
-       }
-
-        /* Interface counters for tapcli interface. */
-        vlib_increment_combined_counter 
-          (vnet_main.interface_main.combined_sw_if_counters
-           + VNET_INTERFACE_COUNTER_RX,
-           os_get_cpu_number(),
-           ti->sw_if_index,
-           1, n_bytes_in_packet);
-
-        _vec_len (tm->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;
-      b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
-
-      /* Ensure mbufs are updated */
-      vlib_buffer_chain_validate(vm, b_first);
-
-      /*
-       * Turn this on if you run into
-       * "bad monkey" contexts, and you want to know exactly
-       * which nodes they've visited... See .../vlib/vlib/buffer.h
-       */
-      VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first);
-
-      {
-        u32 next_index;
-        uword n_trace = vlib_get_trace_count (vm, node);
 
-        vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index;
-        vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0;
-
-        b_first->error = node->errors[0];
-
-        {
-          next_index = TAPCLI_RX_NEXT_ETHERNET_INPUT;
-
-          next_index = (ti->per_interface_next_index != ~0) ? 
-            ti->per_interface_next_index : next_index;
-        }
-        {
-          vnet_main_t *vnm = vnet_get_main();
-          vnet_sw_interface_t * si;
-          si = vnet_get_sw_interface (vnm, ti->sw_if_index);
-          if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
-            next_index = TAPCLI_RX_NEXT_DROP;
-        }
-
-        vlib_set_next_frame_buffer (vm, node, next_index, bi);
-        
-        if (n_trace > 0)
-          {
-            vlib_trace_buffer (vm, node, next_index,
-                               b_first, /* follow_chain */ 1);
-            vlib_set_trace_count (vm, node, n_trace - 1);
-          }
-      }
-    }
-
-  return 1;
+    ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]);
+    total_count += tapcli_rx_iface(vm, node, ti);
+  }
+  return total_count; //This might return more than 256.
 }
 
 static char * tapcli_rx_error_strings[] = {
-  "Interface down",
+#define _(sym,string) string,
+  foreach_tapcli_error
+#undef _
 };
 
-VLIB_REGISTER_NODE (tapcli_rx_node,static) = {
+VLIB_REGISTER_NODE (tapcli_rx_node, static) = {
   .function = tapcli_rx,
   .name = "tapcli-rx",
   .type = VLIB_NODE_TYPE_INPUT,
   .state = VLIB_NODE_STATE_INTERRUPT,
   .vector_size = 4,
-  .n_errors = 1,
+  .n_errors = TAPCLI_N_ERROR,
   .error_strings = tapcli_rx_error_strings,
+  .format_trace = format_tapcli_rx_trace,
 
   .n_next_nodes = TAPCLI_RX_N_NEXT,
   .next_nodes = {
@@ -413,7 +434,7 @@ static clib_error_t *
 tapcli_config (vlib_main_t * vm, unformat_input_t * input)
 {
   tapcli_main_t *tm = &tapcli_main;
-  const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
+  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -494,39 +515,48 @@ static u32 tapcli_flag_change (vnet_main_t * vnm,
 {
   tapcli_main_t *tm = &tapcli_main;
   tapcli_interface_t *ti;
-  struct ifreq ifr;
-  u32 want_promisc;
 
-  ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
+   ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance);
 
-  memcpy (&ifr, &ti->ifr, sizeof (ifr));
-
-  /* get flags, modify to bring up interface... */
-  if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
+  if (flags & ETHERNET_INTERFACE_FLAG_MTU)
     {
-      clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
-      return 0;
+      const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
+      tm->mtu_bytes = hw->max_packet_bytes;
+      tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size;
     }
+   else
+    {
+      struct ifreq ifr;
+      u32 want_promisc;
 
-  want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
+      memcpy (&ifr, &ti->ifr, sizeof (ifr));
 
-  if (want_promisc == ti->is_promisc)
-    return 0;
+      /* get flags, modify to bring up interface... */
+      if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
+        {
+          clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
+          return 0;
+        }
 
+      want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
 
-  if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
-    ifr.ifr_flags |= IFF_PROMISC;
-  else
-    ifr.ifr_flags &= ~(IFF_PROMISC);
+      if (want_promisc == ti->is_promisc)
+        return 0;
 
-  /* get flags, modify to bring up interface... */
-  if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
-    {
-      clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
-      return 0;
-    }
+      if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
+        ifr.ifr_flags |= IFF_PROMISC;
+      else
+        ifr.ifr_flags &= ~(IFF_PROMISC);
 
-  ti->is_promisc = want_promisc;
+      /* get flags, modify to bring up interface... */
+      if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
+        {
+          clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
+          return 0;
+        }
+
+      ti->is_promisc = want_promisc;
+    }
 
   return 0;
 }
@@ -734,9 +764,10 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     }
 
   ti = tapcli_get_new_tapif();
+  ti->per_interface_next_index = ~0;
 
   if (hwaddr_arg != 0)
-    memcpy(hwaddr, hwaddr_arg, 6);
+    clib_memcpy(hwaddr, hwaddr_arg, 6);
 
   error = ethernet_register_interface
         (tm->vnet_main,
@@ -761,13 +792,15 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
     ti->unix_file_index = unix_file_add (&unix_main, &template);
     ti->unix_fd = dev_net_tun_fd;
     ti->provision_fd = dev_tap_fd;
-    memcpy (&ti->ifr, &ifr, sizeof (ifr));
+    clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
   }
   
   {
     vnet_hw_interface_t * hw;
     hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
-    hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = tm->mtu_bytes - sizeof(ethernet_header_t);
+    hw->min_supported_packet_bytes = TAP_MTU_MIN;
+    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;
@@ -974,14 +1007,10 @@ tap_connect_command_fn (vlib_main_t * vm,
 {
   u8 * intfc_name;
   tapcli_main_t * tm = &tapcli_main;
-  tapcli_interface_t * ti;
-  struct ifreq ifr;
-  int flags;
-  int dev_net_tun_fd;
-  int dev_tap_fd = -1;
-  clib_error_t * error;
   int user_hwaddr = 0;
   u8 hwaddr[6];
+  u8 *hwaddr_arg = 0;
+  u32 sw_if_index;
 
   if (tm->is_disabled)
     {
@@ -998,97 +1027,6 @@ tap_connect_command_fn (vlib_main_t * vm,
                &hwaddr))
     user_hwaddr = 1;
 
-  flags = IFF_TAP | IFF_NO_PI;
-
-  if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
-    {
-      vlib_cli_output (vm, "Couldn't open /dev/net/tun");
-      return 0;
-    }
-  
-  memset (&ifr, 0, sizeof (ifr));
-  strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
-  ifr.ifr_flags = flags;
-  if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
-    {
-      vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
-      goto error;
-    }
-    
-  /* Open a provisioning socket */
-  if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
-                           htons(ETH_P_ALL))) < 0 )
-    {
-      vlib_cli_output (vm, "Couldn't open provisioning socket");
-      goto error;
-    }
-
-  /* Find the interface index. */
-  {
-    struct ifreq ifr;
-    struct sockaddr_ll sll;
-
-    memset (&ifr, 0, sizeof(ifr));
-    strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
-    if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
-      {
-        vlib_cli_output (vm, "Couldn't get if_index");
-        goto error;
-      }
-
-    /* Bind the provisioning socket to the interface. */
-    memset(&sll, 0, sizeof(sll));
-    sll.sll_family   = AF_PACKET;
-    sll.sll_ifindex  = ifr.ifr_ifindex;
-    sll.sll_protocol = htons(ETH_P_ALL);
-
-    if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
-      {
-        vlib_cli_output (vm, "Couldn't bind provisioning socket");
-        goto error;
-      }
-  }
-
-  /* non-blocking I/O on /dev/tapX */
-  {
-    int one = 1;
-    if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
-      {
-        vlib_cli_output (0, "Couldn't set device non-blocking flag");
-       goto error;
-      }
-  }
-  ifr.ifr_mtu = tm->mtu_bytes;
-  if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
-    {
-      vlib_cli_output (0, "Couldn't set device MTU");
-      goto error;
-    }
-
-  /* get flags, modify to bring up interface... */
-  if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
-    {
-      vlib_cli_output (0, "Couldn't get interface flags");
-      goto error;
-    }
-
-  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
-
-  if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
-    {
-      vlib_cli_output (0, "Couldn't set intfc admin state up");
-      goto error;
-    }
-
-  if (ioctl (dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0)
-    {
-      vlib_cli_output (0, "Couldn't get intfc MAC address");
-      goto error;
-    }
-
-  ti = tapcli_get_new_tapif();
-  ti->per_interface_next_index = ~0;
-
   if (unformat(input, "hwaddr random"))
     {
       f64 now = vlib_time_now(vm);
@@ -1096,61 +1034,65 @@ tap_connect_command_fn (vlib_main_t * vm,
       rnd = (u32) (now * 1e6);
       rnd = random_u32 (&rnd);
 
-      memcpy (hwaddr+2, &rnd, sizeof(rnd));
+      clib_memcpy (hwaddr+2, &rnd, sizeof(rnd));
       hwaddr[0] = 2;
       hwaddr[1] = 0xfe;
       user_hwaddr = 1;
     }
-  
-  error = ethernet_register_interface
-        (tm->vnet_main,
-         tapcli_dev_class.index,
-         ti - tm->tapcli_interfaces /* device instance */,
-         user_hwaddr ? hwaddr : 
-         (u8 *) ifr.ifr_hwaddr.sa_data /* ethernet address */,
-         &ti->hw_if_index, 
-         tapcli_flag_change);
+  if (user_hwaddr) hwaddr_arg = hwaddr;
 
-  if (error)
-    clib_error_report (error);
+  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;
 
-  {
-    unix_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->unix_fd = dev_net_tun_fd;
-    ti->provision_fd = dev_tap_fd;
-    memcpy (&ti->ifr, &ifr, sizeof (ifr));
-  }
+    case VNET_API_ERROR_SYSCALL_ERROR_2:
+      vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
+      break;
   
-  {
-    vnet_hw_interface_t * hw;
-    hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index);
-    ti->sw_if_index = hw->sw_if_index;
-    hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = tm->mtu_bytes - sizeof(ethernet_header_t);
-  }
+    case VNET_API_ERROR_SYSCALL_ERROR_3:
+      vlib_cli_output (vm, "Couldn't open provisioning socket");
+      break;
 
-  ti->active = 1;
+    case VNET_API_ERROR_SYSCALL_ERROR_4:
+      vlib_cli_output (vm, "Couldn't get if_index");
+      break;
+    
+    case VNET_API_ERROR_SYSCALL_ERROR_5:
+      vlib_cli_output (vm, "Couldn't bind provisioning socket");
+      break;
 
-  hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index,
-            ti - tm->tapcli_interfaces);
+    case VNET_API_ERROR_SYSCALL_ERROR_6:
+      vlib_cli_output (0, "Couldn't set device non-blocking flag");
+      break;
 
-  hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd,
-            ti - tm->tapcli_interfaces);
+    case VNET_API_ERROR_SYSCALL_ERROR_7:
+      vlib_cli_output (0, "Couldn't set device MTU");
+      break;
 
-  vlib_cli_output (vm, "Created %U for Linux tap '%s'",
-                   format_vnet_sw_if_index_name, tm->vnet_main, 
-                   ti->sw_if_index, intfc_name);
-                   
-  return 0;
+    case VNET_API_ERROR_SYSCALL_ERROR_8:
+      vlib_cli_output (0, "Couldn't get interface flags");
+      break;
 
- error:
-  close (dev_net_tun_fd);
-  close (dev_tap_fd);
+    case VNET_API_ERROR_SYSCALL_ERROR_9:
+      vlib_cli_output (0, "Couldn't set intfc admin state up");
+      break;
+
+    case VNET_API_ERROR_INVALID_REGISTRATION:
+      vlib_cli_output (0, "Invalid registration");
+      break;
+    default:
+      vlib_cli_output (0, "Unknown error: %d", rv);
+      break;
+    }
+    return 0;
+  }
 
+  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
   return 0;
-}
+  }
 
 VLIB_CLI_COMMAND (tap_connect_command, static) = {
     .path = "tap connect",
@@ -1166,11 +1108,13 @@ tapcli_init (vlib_main_t * vm)
   tm->vlib_main = vm;
   tm->vnet_main = vnet_get_main();
   tm->unix_main = &unix_main;
-  tm->mtu_bytes = 4096 + 256;
+  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;
-
   return 0;
 }