span: add tx functionality and support for multiple mirror ports

Change-Id: Ib6dd290085e6f9a434499af8d19f346220dc8428
Signed-off-by: Damjan Marion <damarion@cisco.com>
Signed-off-by: Pavel Kotucek <pkotucek@cisco.com>
diff --git a/vnet/Makefile.am b/vnet/Makefile.am
index a8e4867..a5f47fc 100644
--- a/vnet/Makefile.am
+++ b/vnet/Makefile.am
@@ -21,7 +21,9 @@
  vnet/l2/l2.api.h				\
  vnet/l2/l2.api.json				\
  vnet/map/map.api.h				\
- vnet/map/map.api.json
+ vnet/map/map.api.json				\
+ vnet/span/span.api.h				\
+ vnet/span/span.api.json
 
 libvnet_la_SOURCES =
 libvnetplugin_la_SOURCES =
@@ -667,10 +669,12 @@
 ########################################
 
 libvnet_la_SOURCES +=				\
-  vnet/span/span.c	\
+  vnet/span/span_api.c  \
+  vnet/span/span.c	    \
   vnet/span/node.c
 
 nobase_include_HEADERS += 			\
+  vnet/span/span.api.h  \
   vnet/span/span.h
 
 ########################################
diff --git a/vnet/vnet/buffer.h b/vnet/vnet/buffer.h
index 6da6993..898c94e 100644
--- a/vnet/vnet/buffer.h
+++ b/vnet/vnet/buffer.h
@@ -70,6 +70,9 @@
 #define LOG2_VNET_BUFFER_LOCALLY_ORIGINATED LOG2_VLIB_BUFFER_FLAG_USER(7)
 #define VNET_BUFFER_LOCALLY_ORIGINATED (1 << LOG2_VNET_BUFFER_LOCALLY_ORIGINATED)
 
+#define LOG2_VNET_BUFFER_SPAN_CLONE LOG2_VLIB_BUFFER_FLAG_USER(8)
+#define VNET_BUFFER_SPAN_CLONE (1 << LOG2_VNET_BUFFER_SPAN_CLONE)
+
 #define foreach_buffer_opaque_union_subtype     \
 _(ethernet)                                     \
 _(ip)                                           \
diff --git a/vnet/vnet/interface_output.c b/vnet/vnet/interface_output.c
index 3302e79..475b0b9 100644
--- a/vnet/vnet/interface_output.c
+++ b/vnet/vnet/interface_output.c
@@ -1223,9 +1223,16 @@
 {
   .arc_name  = "interface-output",
   .start_nodes = VNET_FEATURES (0),
+  .end_node = "interface-tx",
   .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
 };
 
+VNET_FEATURE_INIT (span_tx, static) = {
+  .arc_name = "interface-output",
+  .node_name = "span-output",
+  .runs_before = VNET_FEATURES ("interface-tx"),
+};
+
 VNET_FEATURE_INIT (interface_tx, static) = {
   .arc_name = "interface-output",
   .node_name = "interface-tx",
diff --git a/vnet/vnet/span/node.c b/vnet/vnet/span/node.c
index 32d4407..50d642c 100644
--- a/vnet/vnet/span/node.c
+++ b/vnet/vnet/span/node.c
@@ -57,23 +57,61 @@
 #undef _
 };
 
-static uword
-span_node_fn (vlib_main_t * vm,
-	      vlib_node_runtime_t * node, vlib_frame_t * frame)
+static_always_inline void
+span_mirror (vlib_main_t * vm, span_interface_t * si0, vlib_buffer_t * b0,
+	     vlib_frame_t ** mirror_frames, int is_rx)
+{
+  vlib_buffer_t *c0;
+  vnet_main_t *vnm = &vnet_main;
+  u32 *to_mirror_next = 0;
+  u32 i;
+
+  if (is_rx != 0 && si0->num_rx_mirror_ports == 0)
+    return;
+
+  if (is_rx == 0 && si0->num_tx_mirror_ports == 0)
+    return;
+
+  /* Don't do it again */
+  if (PREDICT_FALSE (b0->flags & VNET_BUFFER_SPAN_CLONE))
+    return;
+
+  /* *INDENT-OFF* */
+  clib_bitmap_foreach (i, is_rx ? si0->rx_mirror_ports : si0->tx_mirror_ports, (
+    {
+      if (mirror_frames[i] == 0)
+	mirror_frames[i] = vnet_get_frame_to_sw_interface (vnm, i);
+      to_mirror_next = vlib_frame_vector_args (mirror_frames[i]);
+      to_mirror_next += mirror_frames[i]->n_vectors;
+      c0 = vlib_buffer_copy (vm, b0);
+      vnet_buffer (c0)->sw_if_index[VLIB_TX] = i;
+      c0->flags |= VNET_BUFFER_SPAN_CLONE;
+      to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
+      mirror_frames[i]->n_vectors++;
+    }));
+  /* *INDENT-ON* */
+}
+
+static_always_inline uword
+span_node_inline_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+		     vlib_frame_t * frame, int is_rx)
 {
   span_main_t *sm = &span_main;
   vnet_main_t *vnm = &vnet_main;
-  u32 n_left_from, *from, *to_next, *to_mirror_next = 0;
+  u32 n_left_from, *from, *to_next;
   u32 n_span_packets = 0;
-  u32 next_index, mirror_sw_if_index0, mirror_sw_if_index1;
-  u32 last_mirror_sw_if_index = ~0;
-  vlib_frame_t *mirror_frame = 0;
+  u32 next_index;
+  u32 sw_if_index;
+  static __thread vlib_frame_t **mirror_frames = 0;
+  vlib_rx_or_tx_t rxtx = is_rx ? VLIB_RX : VLIB_TX;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
   next_index = node->cached_next_index;
 
-  /* TODO dual loop */
+  vec_validate_aligned (mirror_frames, sm->max_sw_if_index,
+			CLIB_CACHE_LINE_BYTES);
+
   while (n_left_from > 0)
     {
       u32 n_left_to_next;
@@ -84,8 +122,9 @@
 	{
 	  u32 bi0;
 	  u32 bi1;
-	  vlib_buffer_t *b0, *c0;
-	  vlib_buffer_t *b1, *c1;
+	  vlib_buffer_t *b0;
+	  vlib_buffer_t *b1;
+	  span_interface_t *si0, *si1;
 	  u32 sw_if_index0;
 	  u32 next0 = 0;
 	  u32 sw_if_index1;
@@ -100,68 +139,30 @@
 	  n_left_from -= 2;
 
 	  b0 = vlib_get_buffer (vm, bi0);
-	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
-	  mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
 	  b1 = vlib_get_buffer (vm, bi1);
-	  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
-	  mirror_sw_if_index1 = sm->dst_by_src_sw_if_index[sw_if_index1];
+	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
+	  sw_if_index1 = vnet_buffer (b1)->sw_if_index[rxtx];
+	  si0 = vec_elt_at_index (sm->interfaces, sw_if_index0);
+	  si1 = vec_elt_at_index (sm->interfaces, sw_if_index1);
 
-	  /* get frame to mirror interface */
-	  if (PREDICT_FALSE
-	      ((last_mirror_sw_if_index != mirror_sw_if_index0)
-	       || mirror_frame == 0))
-	    {
-	      if (mirror_frame)
-		vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
-						mirror_frame);
-	      last_mirror_sw_if_index = mirror_sw_if_index0;
-	      mirror_frame =
-		vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
-	      to_mirror_next = vlib_frame_vector_args (mirror_frame);
-	    }
-	  /* get frame to mirror interface */
-	  if (PREDICT_FALSE
-	      ((last_mirror_sw_if_index != mirror_sw_if_index1)
-	       || mirror_frame == 0))
-	    {
-	      if (mirror_frame)
-		vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
-						mirror_frame);
-	      last_mirror_sw_if_index = mirror_sw_if_index1;
-	      mirror_frame =
-		vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
-	      to_mirror_next = vlib_frame_vector_args (mirror_frame);
-	    }
-	  c0 = vlib_buffer_copy (vm, b0);
-	  vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
-	  to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
-	  to_mirror_next += 1;
-	  mirror_frame->n_vectors++;
+	  span_mirror (vm, si0, b0, mirror_frames, is_rx);
+	  span_mirror (vm, si1, b1, mirror_frames, is_rx);
 
 	  vnet_feature_next (sw_if_index0, &next0, b0);
-
-	  c1 = vlib_buffer_copy (vm, b1);
-	  vnet_buffer (c1)->sw_if_index[VLIB_TX] = mirror_sw_if_index1;
-	  to_mirror_next[0] = vlib_get_buffer_index (vm, c1);
-	  to_mirror_next += 1;
-	  mirror_frame->n_vectors++;
-
 	  vnet_feature_next (sw_if_index1, &next1, b1);
 
 	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
 	    {
 	      span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
 	      t->src_sw_if_index = sw_if_index0;
-	      t->mirror_sw_if_index =
-		sm->dst_by_src_sw_if_index[sw_if_index0];
+	      //t->mirror_sw_if_index = si0->mirror_sw_if_index;
 	    }
 
 	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
 	    {
 	      span_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
 	      t->src_sw_if_index = sw_if_index1;
-	      t->mirror_sw_if_index =
-		sm->dst_by_src_sw_if_index[sw_if_index1];
+	      //t->mirror_sw_if_index = si1->mirror_sw_if_index;
 	    }
 	  /* verify speculative enqueue, maybe switch current next frame */
 	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
@@ -171,7 +172,8 @@
       while (n_left_from > 0 && n_left_to_next > 0)
 	{
 	  u32 bi0;
-	  vlib_buffer_t *b0, *c0;
+	  vlib_buffer_t *b0;
+	  span_interface_t *si0;
 	  u32 sw_if_index0;
 	  u32 next0 = 0;
 
@@ -183,27 +185,9 @@
 	  n_left_from -= 1;
 
 	  b0 = vlib_get_buffer (vm, bi0);
-	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
-	  mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
-
-	  /* get frame to mirror interface */
-	  if (PREDICT_FALSE
-	      ((last_mirror_sw_if_index != mirror_sw_if_index0)
-	       || mirror_frame == 0))
-	    {
-	      if (mirror_frame)
-		vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
-						mirror_frame);
-	      last_mirror_sw_if_index = mirror_sw_if_index0;
-	      mirror_frame =
-		vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
-	      to_mirror_next = vlib_frame_vector_args (mirror_frame);
-	    }
-	  c0 = vlib_buffer_copy (vm, b0);
-	  vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
-	  to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
-	  to_mirror_next += 1;
-	  mirror_frame->n_vectors++;
+	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
+	  si0 = vec_elt_at_index (sm->interfaces, sw_if_index0);
+	  span_mirror (vm, si0, b0, mirror_frames, is_rx);
 
 	  vnet_feature_next (sw_if_index0, &next0, b0);
 
@@ -211,8 +195,6 @@
 	    {
 	      span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
 	      t->src_sw_if_index = sw_if_index0;
-	      t->mirror_sw_if_index =
-		sm->dst_by_src_sw_if_index[sw_if_index0];
 	    }
 	  /* verify speculative enqueue, maybe switch current next frame */
 	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
@@ -222,16 +204,39 @@
       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
     }
 
-  vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index, mirror_frame);
+
+  for (sw_if_index = 0; sw_if_index < vec_len (mirror_frames); sw_if_index++)
+    {
+      if (mirror_frames[sw_if_index] == 0)
+	continue;
+
+      vnet_put_frame_to_sw_interface (vnm, sw_if_index,
+				      mirror_frames[sw_if_index]);
+      mirror_frames[sw_if_index] = 0;
+    }
   vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS,
 			       n_span_packets);
 
   return frame->n_vectors;
 }
 
+static uword
+span_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+		    vlib_frame_t * frame)
+{
+  return span_node_inline_fn (vm, node, frame, 1);
+}
+
+static uword
+span_output_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+		     vlib_frame_t * frame)
+{
+  return span_node_inline_fn (vm, node, frame, 0);
+}
+
 /* *INDENT-OFF* */
-VLIB_REGISTER_NODE (span_node) = {
-  .function = span_node_fn,
+VLIB_REGISTER_NODE (span_input_node) = {
+  .function = span_input_node_fn,
   .name = "span-input",
   .vector_size = sizeof (u32),
   .format_trace = format_span_trace,
@@ -248,9 +253,30 @@
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (span_input_node, span_input_node_fn)
+
+VLIB_REGISTER_NODE (span_output_node) = {
+  .function = span_output_node_fn,
+  .name = "span-output",
+  .vector_size = sizeof (u32),
+  .format_trace = format_span_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(span_error_strings),
+  .error_strings = span_error_strings,
+
+  .n_next_nodes = 0,
+
+  /* edit / add dispositions here */
+  .next_nodes = {
+    [0] = "error-drop",
+  },
+};
+
+VLIB_NODE_FUNCTION_MULTIARCH (span_output_node, span_output_node_fn)
+
 /* *INDENT-ON* */
 
-VLIB_NODE_FUNCTION_MULTIARCH (span_node, span_node_fn)
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/vnet/vnet/span/span.api b/vnet/vnet/span/span.api
new file mode 100644
index 0000000..2f3b411
--- /dev/null
+++ b/vnet/vnet/span/span.api
@@ -0,0 +1,60 @@
+/* Hey Emacs use -*- mode: C -*- */
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ /** \brief Enable/Disable span to mirror traffic from one interface to another
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context which was passed in the request
+    @param sw_if_index_from - interface to be mirorred
+    @param sw_if_index_to - interface where the traffic is mirrored
+    @param state - 0 = disabled, 1 = rx enabled, 2 = tx enabled, 3 tx & rx enabled
+*/
+define sw_interface_span_enable_disable{
+    u32 client_index;
+    u32 context;
+    u32 sw_if_index_from;
+    u32 sw_if_index_to;
+    u8  state;
+};
+
+/** \brief Reply to SPAN enable/disable request
+    @param context - sender context which was passed in the request
+*/
+define sw_interface_span_enable_disable_reply {
+    u32 context;
+    i32 retval;
+};
+
+/** \brief SPAN dump request
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+*/
+define sw_interface_span_dump {
+    u32 client_index;
+    u32 context;
+};
+
+/** \brief Reply to SPAN dump request
+    @param context - sender context which was passed in the request
+    @param sw_if_index_from - mirorred interface
+    @param sw_if_index_to - interface where the traffic is mirrored
+    @param state - 0 = disabled, 1 = rx enabled, 2 = tx enabled, 3 tx & rx enabled
+*/
+define sw_interface_span_details {
+    u32 context;
+    u32 sw_if_index_from;
+    u32 sw_if_index_to;
+    u8 state;
+};
\ No newline at end of file
diff --git a/vnet/vnet/span/span.c b/vnet/vnet/span/span.c
index 5230045..7b5816c 100644
--- a/vnet/vnet/span/span.c
+++ b/vnet/vnet/span/span.c
@@ -21,22 +21,59 @@
 
 int
 span_add_delete_entry (vlib_main_t * vm,
-		       u32 src_sw_if_index, u32 dst_sw_if_index, u8 is_add)
+		       u32 src_sw_if_index, u32 dst_sw_if_index, u8 state)
 {
   span_main_t *sm = &span_main;
+  span_interface_t *si;
+  u32 new_num_rx_mirror_ports, new_num_tx_mirror_ports;
 
-  if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && is_add)
+  if (state > 3)
+    return VNET_API_ERROR_UNIMPLEMENTED;
+
+  if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
       || (src_sw_if_index == dst_sw_if_index))
     return VNET_API_ERROR_INVALID_INTERFACE;
 
-  vnet_sw_interface_t *sw =
-    vnet_get_sw_interface (sm->vnet_main, src_sw_if_index);
+  vnet_sw_interface_t *sw_if;
 
-  vec_validate_aligned (sm->dst_by_src_sw_if_index, sw->sw_if_index,
+  sw_if = vnet_get_sw_interface (vnet_get_main (), src_sw_if_index);
+  if (sw_if->type == VNET_SW_INTERFACE_TYPE_SUB)
+    return VNET_API_ERROR_UNIMPLEMENTED;
+
+  vec_validate_aligned (sm->interfaces, src_sw_if_index,
 			CLIB_CACHE_LINE_BYTES);
-  sm->dst_by_src_sw_if_index[sw->sw_if_index] = is_add ? dst_sw_if_index : 0;
-  vnet_feature_enable_disable ("device-input", "span-input",
-			       sw->sw_if_index, is_add, 0, 0);
+  si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
+
+  si->rx_mirror_ports = clib_bitmap_set (si->rx_mirror_ports, dst_sw_if_index,
+					 (state & 1) != 0);
+  si->tx_mirror_ports = clib_bitmap_set (si->tx_mirror_ports, dst_sw_if_index,
+					 (state & 2) != 0);
+
+  new_num_rx_mirror_ports = clib_bitmap_count_set_bits (si->rx_mirror_ports);
+  new_num_tx_mirror_ports = clib_bitmap_count_set_bits (si->tx_mirror_ports);
+
+  if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0)
+    vnet_feature_enable_disable ("device-input", "span-input",
+				 src_sw_if_index, 1, 0, 0);
+
+  if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1)
+    vnet_feature_enable_disable ("device-input", "span-input",
+				 src_sw_if_index, 0, 0, 0);
+
+  if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0)
+    vnet_feature_enable_disable ("device-input", "span-output",
+				 src_sw_if_index, 1, 0, 0);
+
+  if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1)
+    vnet_feature_enable_disable ("device-input", "span-output",
+				 src_sw_if_index, 0, 0, 0);
+
+  si->num_rx_mirror_ports = new_num_rx_mirror_ports;
+  si->num_tx_mirror_ports = new_num_tx_mirror_ports;
+
+  if (dst_sw_if_index > sm->max_sw_if_index)
+    sm->max_sw_if_index = dst_sw_if_index;
+
   return 0;
 }
 
@@ -48,7 +85,7 @@
   span_main_t *sm = &span_main;
   u32 src_sw_if_index = ~0;
   u32 dst_sw_if_index = ~0;
-  u8 is_add = 1;
+  u8 state = 3;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -59,13 +96,19 @@
 			 sm->vnet_main, &dst_sw_if_index))
 	;
       else if (unformat (input, "disable"))
-	is_add = 0;
+	state = 0;
+      else if (unformat (input, "rx"))
+	state = 1;
+      else if (unformat (input, "tx"))
+	state = 2;
+      else if (unformat (input, "both"))
+	state = 3;
       else
 	break;
     }
 
   int rv =
-    span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, is_add);
+    span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state);
   if (rv == VNET_API_ERROR_INVALID_INTERFACE)
     return clib_error_return (0, "Invalid interface");
   return 0;
@@ -74,7 +117,7 @@
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (set_interface_span_command, static) = {
   .path = "set interface span",
-  .short_help = "set interface span <if-name> [disable | destination <if-name>]",
+  .short_help = "set interface span <if-name> [disable | destination <if-name> [both|rx|tx]]",
   .function = set_interface_span_command_fn,
 };
 /* *INDENT-ON* */
@@ -84,28 +127,43 @@
 				 unformat_input_t * input,
 				 vlib_cli_command_t * cmd)
 {
-
   span_main_t *sm = &span_main;
+  span_interface_t *si;
   vnet_main_t *vnm = &vnet_main;
-  u32 src_sw_if_index = 0, *dst_sw_if_index;
   u8 header = 1;
+  char *states[] = { "none", "rx", "tx", "both" };
+  u8 *s = 0;
 
-  vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
-  {
-    if (*dst_sw_if_index > 0)	// && *dst_sw_if_index != ~0)
+  /* *INDENT-OFF* */
+  vec_foreach (si, sm->interfaces)
+    if (si->num_rx_mirror_ports || si->num_tx_mirror_ports)
       {
+	clib_bitmap_t *b;
+	u32 i;
+	b = clib_bitmap_dup_or (si->rx_mirror_ports, si->tx_mirror_ports);
 	if (header)
 	  {
-	    vlib_cli_output (vm,
-			     "SPAN source interface to destination interface table");
+	    vlib_cli_output (vm, "%-40s %s", "Source interface",
+			     "Mirror interface (direction)");
 	    header = 0;
 	  }
-	vlib_cli_output (vm, "%32U => %-32U",
-			 format_vnet_sw_if_index_name, vnm, src_sw_if_index,
-			 format_vnet_sw_if_index_name, vnm, *dst_sw_if_index);
+	s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
+		    si - sm->interfaces);
+	clib_bitmap_foreach (i, b, (
+	  {
+	    int state;
+	    state = (clib_bitmap_get (si->rx_mirror_ports, i) +
+		     clib_bitmap_get (si->tx_mirror_ports, i) * 2);
+
+	    vlib_cli_output (vm, "%-40v %U (%s)", s,
+			     format_vnet_sw_if_index_name, vnm, i,
+			     states[state]);
+	    vec_reset_length (s);
+	  }));
+	clib_bitmap_free (b);
       }
-    src_sw_if_index++;
-  }
+  /* *INDENT-ON* */
+  vec_free (s);
   return 0;
 }
 
diff --git a/vnet/vnet/span/span.h b/vnet/vnet/span/span.h
index 751bebf..a98b010 100644
--- a/vnet/vnet/span/span.h
+++ b/vnet/vnet/span/span.h
@@ -21,8 +21,19 @@
 
 typedef struct
 {
-  /* destination interface index by source interface index */
-  u32 *dst_by_src_sw_if_index;
+  clib_bitmap_t *rx_mirror_ports;
+  clib_bitmap_t *tx_mirror_ports;
+  u32 num_rx_mirror_ports;
+  u32 num_tx_mirror_ports;
+} span_interface_t;
+
+typedef struct
+{
+  /* per-interface vector of span instances */
+  span_interface_t *interfaces;
+
+  /* biggest sw_if_index used so far */
+  u32 max_sw_if_index;
 
   /* convenience */
   vlib_main_t *vlib_main;
diff --git a/vnet/vnet/span/span_api.c b/vnet/vnet/span/span_api.c
new file mode 100644
index 0000000..eacd6ec
--- /dev/null
+++ b/vnet/vnet/span/span_api.c
@@ -0,0 +1,153 @@
+/*
+ *------------------------------------------------------------------
+ * span_api.c - span mirroring api
+ *
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <vnet/vnet.h>
+#include <vlibmemory/api.h>
+
+#include <vnet/interface.h>
+#include <vnet/api_errno.h>
+#include <vnet/span/span.h>
+
+#include <vnet/vnet_msg_enum.h>
+
+#define vl_typedefs		/* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_typedefs
+
+#define vl_endianfun		/* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
+#define vl_printfun
+#include <vnet/vnet_all_api_h.h>
+#undef vl_printfun
+
+#include <vlibapi/api_helper_macros.h>
+
+#define foreach_vpe_api_msg                             \
+_(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable)   \
+_(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump)                       \
+
+static void
+  vl_api_sw_interface_span_enable_disable_t_handler
+  (vl_api_sw_interface_span_enable_disable_t * mp)
+{
+  vl_api_sw_interface_span_enable_disable_reply_t *rmp;
+  int rv;
+
+  vlib_main_t *vm = vlib_get_main ();
+
+  rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
+			      ntohl (mp->sw_if_index_to), mp->state);
+
+  REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
+}
+
+static void
+vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
+{
+
+  unix_shared_memory_queue_t *q;
+  span_interface_t *si;
+  vl_api_sw_interface_span_details_t *rmp;
+  span_main_t *sm = &span_main;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (!q)
+    return;
+
+  /* *INDENT-OFF* */
+  vec_foreach (si, sm->interfaces)
+    if (si->num_rx_mirror_ports || si->num_tx_mirror_ports)
+    {
+      clib_bitmap_t *b;
+      u32 i;
+      b = clib_bitmap_dup_or (si->rx_mirror_ports, si->tx_mirror_ports);
+      clib_bitmap_foreach (i, b, (
+        {
+          rmp = vl_msg_api_alloc (sizeof (*rmp));
+          memset (rmp, 0, sizeof (*rmp));
+          rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
+          rmp->context = mp->context;
+
+          rmp->sw_if_index_from = htonl (si - sm->interfaces);
+          rmp->sw_if_index_to = htonl (i);
+          rmp->state = (u8) (clib_bitmap_get (si->rx_mirror_ports, i) +
+                             clib_bitmap_get (si->tx_mirror_ports, i) * 2);
+
+          vl_msg_api_send_shmem (q, (u8 *) & rmp);
+        }));
+      clib_bitmap_free (b);
+    }
+  /* *INDENT-ON* */
+}
+
+/*
+ * vpe_api_hookup
+ * Add vpe's API message handlers to the table.
+ * vlib has alread mapped shared memory and
+ * added the client registration handlers.
+ * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
+ */
+#define vl_msg_name_crc_list
+#include <vnet/vnet_all_api_h.h>
+#undef vl_msg_name_crc_list
+
+static void
+setup_message_id_table (api_main_t * am)
+{
+#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
+  foreach_vl_msg_name_crc_l2;
+#undef _
+}
+
+static clib_error_t *
+span_api_hookup (vlib_main_t * vm)
+{
+  api_main_t *am = &api_main;
+
+#define _(N,n)                                                  \
+    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
+                           vl_api_##n##_t_handler,              \
+                           vl_noop_handler,                     \
+                           vl_api_##n##_t_endian,               \
+                           vl_api_##n##_t_print,                \
+                           sizeof(vl_api_##n##_t), 1);
+  foreach_vpe_api_msg;
+#undef _
+
+  /*
+   * Set up the (msg_name, crc, message-id) table
+   */
+  setup_message_id_table (am);
+
+  return 0;
+}
+
+VLIB_API_INIT_FUNCTION (span_api_hookup);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/vnet/vnet/vnet_all_api_h.h b/vnet/vnet/vnet_all_api_h.h
index 887b1b0..c8ead56 100644
--- a/vnet/vnet/vnet_all_api_h.h
+++ b/vnet/vnet/vnet_all_api_h.h
@@ -32,6 +32,7 @@
 #include <vnet/interface.api.h>
 #include <vnet/map/map.api.h>
 #include <vnet/l2/l2.api.h>
+#include <vnet/span/span.api.h>
 
 /*
  * fd.io coding-style-patch-verification: ON