span: add tx functionality and support for multiple mirror ports 49/4049/11
authorPavel Kotucek <pkotucek@cisco.com>
Tue, 6 Dec 2016 09:10:10 +0000 (10:10 +0100)
committerDave Barach <openvpp@barachs.net>
Tue, 6 Dec 2016 17:26:48 +0000 (17:26 +0000)
Change-Id: Ib6dd290085e6f9a434499af8d19f346220dc8428
Signed-off-by: Damjan Marion <damarion@cisco.com>
Signed-off-by: Pavel Kotucek <pkotucek@cisco.com>
13 files changed:
vnet/Makefile.am
vnet/vnet/buffer.h
vnet/vnet/interface_output.c
vnet/vnet/span/node.c
vnet/vnet/span/span.api [new file with mode: 0644]
vnet/vnet/span/span.c
vnet/vnet/span/span.h
vnet/vnet/span/span_api.c [new file with mode: 0644]
vnet/vnet/vnet_all_api_h.h
vpp-api-test/vat/api_format.c
vpp/vpp-api/api.c
vpp/vpp-api/custom_dump.c
vpp/vpp-api/vpe.api

index a8e4867..a5f47fc 100644 (file)
@@ -21,7 +21,9 @@ BUILT_SOURCES =                                       \
  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 @@ nobase_include_HEADERS +=                         \
 ########################################
 
 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
 
 ########################################
index 6da6993..898c94e 100644 (file)
@@ -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)                                           \
index 3302e79..475b0b9 100644 (file)
@@ -1223,9 +1223,16 @@ VNET_FEATURE_ARC_INIT (interface_output, static) =
 {
   .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",
index 32d4407..50d642c 100644 (file)
@@ -57,23 +57,61 @@ static char *span_error_strings[] = {
 #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 @@ span_node_fn (vlib_main_t * vm,
        {
          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 @@ span_node_fn (vlib_main_t * vm,
          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 @@ span_node_fn (vlib_main_t * vm,
       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 @@ span_node_fn (vlib_main_t * vm,
          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_node_fn (vlib_main_t * vm,
            {
              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 @@ span_node_fn (vlib_main_t * vm,
       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_REGISTER_NODE (span_node) = {
   },
 };
 
+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 (file)
index 0000000..2f3b411
--- /dev/null
@@ -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
index 5230045..7b5816c 100644 (file)
 
 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;
+
+  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->dst_by_src_sw_if_index, sw->sw_if_index,
+  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 @@ set_interface_span_command_fn (vlib_main_t * vm,
   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 @@ set_interface_span_command_fn (vlib_main_t * vm,
                         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 @@ set_interface_span_command_fn (vlib_main_t * vm,
 /* *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 @@ show_interfaces_span_command_fn (vlib_main_t * vm,
                                 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;
 }
 
index 751bebf..a98b010 100644 (file)
 
 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 (file)
index 0000000..eacd6ec
--- /dev/null
@@ -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:
+ */
index 887b1b0..c8ead56 100644 (file)
@@ -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
index 94495de..83fe57e 100644 (file)
@@ -15881,7 +15881,7 @@ api_sw_interface_span_enable_disable (vat_main_t * vam)
   f64 timeout;
   u32 src_sw_if_index = ~0;
   u32 dst_sw_if_index = ~0;
-  u8 enable = 1;
+  u8 state = 3;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -15896,7 +15896,13 @@ api_sw_interface_span_enable_disable (vat_main_t * vam)
       else if (unformat (i, "dst_sw_if_index %d", &dst_sw_if_index))
        ;
       else if (unformat (i, "disable"))
-       enable = 0;
+       state = 0;
+      else if (unformat (i, "rx"))
+       state = 1;
+      else if (unformat (i, "tx"))
+       state = 2;
+      else if (unformat (i, "both"))
+       state = 3;
       else
        break;
     }
@@ -15905,7 +15911,7 @@ api_sw_interface_span_enable_disable (vat_main_t * vam)
 
   mp->sw_if_index_from = htonl (src_sw_if_index);
   mp->sw_if_index_to = htonl (dst_sw_if_index);
-  mp->enable = enable;
+  mp->state = state;
 
   S;
   W;
@@ -15918,9 +15924,32 @@ vl_api_sw_interface_span_details_t_handler (vl_api_sw_interface_span_details_t
                                            * mp)
 {
   vat_main_t *vam = &vat_main;
+  u8 *sw_if_from_name = 0;
+  u8 *sw_if_to_name = 0;
+  u32 sw_if_index_from = ntohl (mp->sw_if_index_from);
+  u32 sw_if_index_to = ntohl (mp->sw_if_index_to);
+  char *states[] = { "none", "rx", "tx", "both" };
+  hash_pair_t *p;
 
-  fformat (vam->ofp, "%u => %u\n",
-          ntohl (mp->sw_if_index_from), ntohl (mp->sw_if_index_to));
+  /* *INDENT-OFF* */
+  hash_foreach_pair (p, vam->sw_if_index_by_interface_name,
+  ({
+    if ((u32) p->value[0] == sw_if_index_from)
+      {
+        sw_if_from_name = (u8 *)(p->key);
+        if (sw_if_to_name)
+          break;
+      }
+    if ((u32) p->value[0] == sw_if_index_to)
+      {
+        sw_if_to_name = (u8 *)(p->key);
+        if (sw_if_from_name)
+          break;
+      }
+  }));
+  /* *INDENT-ON* */
+  fformat (vam->ofp, "%20s => %20s (%s)\n",
+          sw_if_from_name, sw_if_to_name, states[mp->state]);
 }
 
 static void
@@ -15929,6 +15958,29 @@ static void
 {
   vat_main_t *vam = &vat_main;
   vat_json_node_t *node = NULL;
+  u8 *sw_if_from_name = 0;
+  u8 *sw_if_to_name = 0;
+  u32 sw_if_index_from = ntohl (mp->sw_if_index_from);
+  u32 sw_if_index_to = ntohl (mp->sw_if_index_to);
+  hash_pair_t *p;
+
+  /* *INDENT-OFF* */
+  hash_foreach_pair (p, vam->sw_if_index_by_interface_name,
+  ({
+    if ((u32) p->value[0] == sw_if_index_from)
+      {
+        sw_if_from_name = (u8 *)(p->key);
+        if (sw_if_to_name)
+          break;
+      }
+    if ((u32) p->value[0] == sw_if_index_to)
+      {
+        sw_if_to_name = (u8 *)(p->key);
+        if (sw_if_from_name)
+          break;
+      }
+  }));
+  /* *INDENT-ON* */
 
   if (VAT_JSON_ARRAY != vam->json_tree.type)
     {
@@ -15938,9 +15990,11 @@ static void
   node = vat_json_array_add (&vam->json_tree);
 
   vat_json_init_object (node);
-  vat_json_object_add_uint (node, "src-if-index",
-                           ntohl (mp->sw_if_index_from));
-  vat_json_object_add_uint (node, "dst-if-index", ntohl (mp->sw_if_index_to));
+  vat_json_object_add_uint (node, "src-if-index", sw_if_index_from);
+  vat_json_object_add_string_copy (node, "src-if-name", sw_if_from_name);
+  vat_json_object_add_uint (node, "dst-if-index", sw_if_index_to);
+  vat_json_object_add_string_copy (node, "dst-if-name", sw_if_to_name);
+  vat_json_object_add_uint (node, "state", mp->state);
 }
 
 static int
@@ -17542,7 +17596,7 @@ _(set_ipfix_classify_stream, "[domain <domain-id>] [src_port <src-port>]") \
 _(ipfix_classify_stream_dump, "")                                       \
 _(ipfix_classify_table_add_del, "table <table-index> ip4|ip6 [tcp|udp]")\
 _(ipfix_classify_table_dump, "")                                        \
-_(sw_interface_span_enable_disable, "[src <intfc> | src_sw_if_index <id>] [[dst <intfc> | dst_sw_if_index <id>] | disable]") \
+_(sw_interface_span_enable_disable, "[src <intfc> | src_sw_if_index <id>] [disable | [[dst <intfc> | dst_sw_if_index <id>] [both|rx|tx]]]") \
 _(sw_interface_span_dump, "")                                           \
 _(get_next_index, "node-name <node-name> next-node-name <node-name>")   \
 _(pg_create_interface, "if_id <nn>")                                    \
index 4a3ec06..ec52f44 100644 (file)
@@ -301,8 +301,6 @@ _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream)                 \
 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump)               \
 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del)           \
 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)                 \
-_(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable)   \
-_(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump)                       \
 _(GET_NEXT_INDEX, get_next_index)                                       \
 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
 _(PG_CAPTURE, pg_capture)                                               \
@@ -7577,52 +7575,6 @@ static void
       send_ipfix_classify_table_details (i, q, mp->context);
 }
 
-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->enable);
-
-  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;
-  vl_api_sw_interface_span_details_t *rmp;
-  span_main_t *sm = &span_main;
-  u32 src_sw_if_index = 0, *dst_sw_if_index;
-
-  q = vl_api_client_index_to_input_queue (mp->client_index);
-  if (!q)
-    return;
-
-  vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
-  {
-    if (*dst_sw_if_index > 0)
-      {
-       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 (src_sw_if_index);
-       rmp->sw_if_index_to = htonl (*dst_sw_if_index);
-
-       vl_msg_api_send_shmem (q, (u8 *) & rmp);
-      }
-    src_sw_if_index++;
-  }
-}
-
 static void
 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
 {
index d0a8eca..82195f6 100644 (file)
@@ -2172,8 +2172,23 @@ static void *vl_api_sw_interface_span_enable_disable_t_print
   s = format (0, "SCRIPT: sw_interface_span_enable_disable ");
   s = format (s, "src_sw_if_index %u ", ntohl (mp->sw_if_index_from));
   s = format (s, "dst_sw_if_index %u ", ntohl (mp->sw_if_index_to));
-  if (!mp->enable)
-    s = format (s, "disable ");
+
+  switch (mp->state)
+    {
+    case 0:
+      s = format (s, "disable ");
+      break;
+    case 1:
+      s = format (s, "rx ");
+      break;
+    case 2:
+      s = format (s, "tx ");
+      break;
+    case 3:
+    default:
+      s = format (s, "both ");
+      break;
+    }
 
   FINISH;
 }
index 487ce3a..6d20dab 100644 (file)
@@ -4633,49 +4633,6 @@ define flow_classify_details {
     u32 table_index;
 };
 
-/** \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 enable - 1 enable SPAN, 0 SPAN on given interface
-*/
-define sw_interface_span_enable_disable{
-    u32 client_index;
-    u32 context;
-    u32 sw_if_index_from;
-    u32 sw_if_index_to;
-    u8  enable;
-};
-
-/** \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
-*/
-define sw_interface_span_details {
-    u32 context;
-    u32 sw_if_index_from;
-    u32 sw_if_index_to;
-};
-
 /** \brief Query relative index via node names
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request