Network delay simulator plugin 37/14937/6
authorDave Barach <dave@barachs.net>
Fri, 21 Sep 2018 17:10:28 +0000 (13:10 -0400)
committerDamjan Marion <dmarion@me.com>
Mon, 24 Sep 2018 08:08:34 +0000 (08:08 +0000)
Change-Id: I4a70c7df8f0cb368a4e1cb16f30eeef5c6058c79
Signed-off-by: Dave Barach <dave@barachs.net>
src/plugins/nsim/CMakeLists.txt [new file with mode: 0644]
src/plugins/nsim/node.c [new file with mode: 0644]
src/plugins/nsim/nsim.api [new file with mode: 0644]
src/plugins/nsim/nsim.c [new file with mode: 0644]
src/plugins/nsim/nsim.h [new file with mode: 0644]
src/plugins/nsim/nsim_all_api_h.h [new file with mode: 0644]
src/plugins/nsim/nsim_input.c [new file with mode: 0644]
src/plugins/nsim/nsim_msg_enum.h [new file with mode: 0644]
src/plugins/nsim/nsim_test.c [new file with mode: 0644]
src/vnet/api_errno.h

diff --git a/src/plugins/nsim/CMakeLists.txt b/src/plugins/nsim/CMakeLists.txt
new file mode 100644 (file)
index 0000000..6b261f8
--- /dev/null
@@ -0,0 +1,33 @@
+# Copyright (c) 2018 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.
+
+add_vpp_plugin(nsim
+  SOURCES
+  nsim.c
+  node.c
+  nsim_input.c
+
+  MULTIARCH_SOURCES
+  nsim_input.c
+  node.c
+
+  API_FILES
+  nsim.api
+
+  INSTALL_HEADERS
+  nsim_all_api_h.h
+  nsim_msg_enum.h
+
+  API_TEST_SOURCES
+  nsim_test.c
+)
diff --git a/src/plugins/nsim/node.c b/src/plugins/nsim/node.c
new file mode 100644 (file)
index 0000000..69dc9be
--- /dev/null
@@ -0,0 +1,187 @@
+/*
+ * node.c - skeleton vpp engine plug-in dual-loop node skeleton
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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 <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <vppinfra/error.h>
+#include <nsim/nsim.h>
+
+typedef struct
+{
+  f64 expires;
+  u32 tx_sw_if_index;
+  int is_drop;
+} nsim_trace_t;
+
+#ifndef CLIB_MARCH_VARIANT
+
+/* packet trace format function */
+static u8 *
+format_nsim_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  nsim_trace_t *t = va_arg (*args, nsim_trace_t *);
+
+  if (t->is_drop)
+    s = format (s, "NSIM: ring drop");
+  else
+    s = format (s, "NSIM: tx time %.6f sw_if_index %d",
+               t->expires, t->tx_sw_if_index);
+
+  return s;
+}
+#endif /* CLIB_MARCH_VARIANT */
+
+vlib_node_registration_t nsim_node;
+
+#define foreach_nsim_error                              \
+_(BUFFERED, "Packets buffered")                         \
+_(DROPPED, "Packets dropped due to lack of space")
+
+typedef enum
+{
+#define _(sym,str) NSIM_ERROR_##sym,
+  foreach_nsim_error
+#undef _
+    NSIM_N_ERROR,
+} nsim_error_t;
+
+#ifndef CLIB_MARCH_VARIANT
+static char *nsim_error_strings[] = {
+#define _(sym,string) string,
+  foreach_nsim_error
+#undef _
+};
+#endif /* CLIB_MARCH_VARIANT */
+
+typedef enum
+{
+  NSIM_NEXT_DROP,
+  NSIM_N_NEXT,
+} nsim_next_t;
+
+always_inline uword
+nsim_inline (vlib_main_t * vm,
+            vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace)
+{
+  nsim_main_t *nsm = &nsim_main;
+  u32 n_left_from, *from;
+  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
+  u16 nexts[VLIB_FRAME_SIZE], *next;
+  u32 my_thread_index = vm->thread_index;
+  nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
+  f64 now = vlib_time_now (vm);
+  f64 expires = now + nsm->delay;
+  int is_drop0;
+  u32 no_error = node->errors[NSIM_ERROR_BUFFERED];
+  u32 no_buffer_error = node->errors[NSIM_ERROR_DROPPED];
+  nsim_wheel_entry_t *ep = 0;
+
+  ASSERT (wp);
+
+  from = vlib_frame_vector_args (frame);
+  n_left_from = frame->n_vectors;
+
+  vlib_get_buffers (vm, from, bufs, n_left_from);
+  b = bufs;
+  next = nexts;
+
+  /* There is no point in trying to do more than 1 pkt here */
+  while (n_left_from > 0)
+    {
+      b[0]->error = no_error;
+      next[0] = NSIM_NEXT_DROP;
+      is_drop0 = 0;
+      if (PREDICT_TRUE (wp->cursize < wp->wheel_size))
+       {
+         ep = wp->entries + wp->tail;
+         wp->tail++;
+         if (wp->tail == wp->wheel_size)
+           wp->tail = 0;
+         wp->cursize++;
+
+         ep->tx_time = expires;
+         ep->tx_sw_if_index =
+           (vnet_buffer (b[0])->sw_if_index[VLIB_RX] == nsm->sw_if_index0)
+           ? nsm->sw_if_index1 : nsm->sw_if_index0;
+         ep->current_length = vlib_buffer_length_in_chain (vm, b[0]);
+         ASSERT (ep->current_length <= WHEEL_ENTRY_DATA_SIZE);
+         clib_memcpy (ep->data, vlib_buffer_get_current (b[0]),
+                      ep->current_length);
+       }
+      else                     /* out of wheel space, drop pkt */
+       b[0]->error = no_buffer_error;
+
+      if (is_trace)
+       {
+         if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
+           {
+             nsim_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
+             t->expires = expires;
+             t->is_drop = is_drop0;
+             if (is_drop0 == 0)
+               t->tx_sw_if_index = ep->tx_sw_if_index;
+           }
+       }
+
+      b += 1;
+      next += 1;
+      n_left_from -= 1;
+    }
+  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
+  return frame->n_vectors;
+}
+
+VLIB_NODE_FN (nsim_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
+                         vlib_frame_t * frame)
+{
+  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
+    return nsim_inline (vm, node, frame, 1 /* is_trace */ );
+  else
+    return nsim_inline (vm, node, frame, 0 /* is_trace */ );
+}
+
+/* *INDENT-OFF* */
+#ifndef CLIB_MARCH_VARIANT
+VLIB_REGISTER_NODE (nsim_node) =
+{
+  .name = "nsim",
+  .vector_size = sizeof (u32),
+  .format_trace = format_nsim_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(nsim_error_strings),
+  .error_strings = nsim_error_strings,
+
+  .n_next_nodes = NSIM_N_NEXT,
+
+  /* edit / add dispositions here */
+  .next_nodes = {
+        [NSIM_NEXT_DROP] = "error-drop",
+  },
+};
+#endif /* CLIB_MARCH_VARIANT */
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nsim/nsim.api b/src/plugins/nsim/nsim.api
new file mode 100644 (file)
index 0000000..8917426
--- /dev/null
@@ -0,0 +1,50 @@
+/**
+ * @file nsim.api
+ * @brief VPP control-plane API messages for the network delay simulator
+ */
+
+option version = "1.0.0";
+
+/** \brief enable / disable the network delay simulation cross-connect
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param enable_disable - enable or disable the feature
+    @param sw_if_index0 - one interface to cross-connect
+    @param sw_if_index1 - the other interface to cross-connect
+*/
+autoreply define nsim_enable_disable 
+{
+  /* Client identifier, set from api_main.my_client_index */
+  u32 client_index;
+  
+  /* Arbitrary context, so client can match reply to request */
+  u32 context;
+  
+  /* Enable / disable the feature on the interfaces */
+  u8 enable_disable;
+  
+  /* Interface handles */
+  u32 sw_if_index0;
+  u32 sw_if_index1;
+};
+
+/** \brief configure the network delay simulation cross-connect
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param delay_in_usec - microseconds of link delay to simulate
+    @param average_packet_size - average packet size for wheel sizing
+    @param bandwidth_in_bits_per_second - bps for wheel sizing
+*/
+autoreply define nsim_configure
+{
+  /* Client identifier, set from api_main.my_client_index */
+  u32 client_index;
+  
+  /* Arbitrary context, so client can match reply to request */
+  u32 context;
+
+  /* Configuration parameters */
+  u32 delay_in_usec;
+  u32 average_packet_size;
+  u64 bandwidth_in_bits_per_second;
+};
diff --git a/src/plugins/nsim/nsim.c b/src/plugins/nsim/nsim.c
new file mode 100644 (file)
index 0000000..34a5039
--- /dev/null
@@ -0,0 +1,599 @@
+/*
+ * nsim.c - skeleton vpp engine plug-in
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief Network Delay Simulator
+ */
+/*? %%clicmd:group_label Network Delay Simulator %% ?*/
+
+#include <vnet/vnet.h>
+#include <vnet/plugin/plugin.h>
+#include <nsim/nsim.h>
+
+#include <vlibapi/api.h>
+#include <vlibmemory/api.h>
+#include <vpp/app/version.h>
+
+/* define message IDs */
+#include <nsim/nsim_msg_enum.h>
+
+/* define message structures */
+#define vl_typedefs
+#include <nsim/nsim_all_api_h.h>
+#undef vl_typedefs
+
+/* define generated endian-swappers */
+#define vl_endianfun
+#include <nsim/nsim_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 <nsim/nsim_all_api_h.h>
+#undef vl_printfun
+
+/* Get the API version number */
+#define vl_api_version(n,v) static u32 api_version=(v);
+#include <nsim/nsim_all_api_h.h>
+#undef vl_api_version
+
+#define REPLY_MSG_ID_BASE nsm->msg_id_base
+#include <vlibapi/api_helper_macros.h>
+
+nsim_main_t nsim_main;
+
+/* List of message types that this plugin understands */
+
+#define foreach_nsim_plugin_api_msg             \
+_(NSIM_ENABLE_DISABLE, nsim_enable_disable)     \
+_(NSIM_CONFIGURE, nsim_configure)
+
+/* Action function shared between message handler and debug CLI */
+
+int
+nsim_enable_disable (nsim_main_t * nsm, u32 sw_if_index0,
+                    u32 sw_if_index1, int enable_disable)
+{
+  vnet_sw_interface_t *sw;
+  vnet_hw_interface_t *hw;
+  int rv = 0;
+
+  if (nsm->is_configured == 0)
+    return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
+
+  /* Utterly wrong? */
+  if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
+                         sw_if_index0))
+    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+  if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
+                         sw_if_index1))
+    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+  /* Not a physical port? */
+  sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index0);
+  if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
+    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+  sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index1);
+  if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
+    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+  /* Add graph arcs for the input / wheel scraper node */
+  hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index0);
+  nsm->output_next_index0 =
+    vlib_node_add_next (nsm->vlib_main,
+                       nsim_input_node.index, hw->output_node_index);
+
+  hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index1);
+  nsm->output_next_index1 =
+    vlib_node_add_next (nsm->vlib_main,
+                       nsim_input_node.index, hw->output_node_index);
+
+  nsm->sw_if_index0 = sw_if_index0;
+  nsm->sw_if_index1 = sw_if_index1;
+
+  vnet_feature_enable_disable ("device-input", "nsim",
+                              sw_if_index0, enable_disable, 0, 0);
+  vnet_feature_enable_disable ("device-input", "nsim",
+                              sw_if_index1, enable_disable, 0, 0);
+
+  return rv;
+}
+
+static int
+nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size)
+{
+  u64 total_buffer_size_in_bytes, per_worker_buffer_size;
+  u64 wheel_slots_per_worker;
+  int i;
+  int num_workers = vlib_num_workers ();
+  u32 pagesize = getpagesize ();
+  vlib_main_t *vm = nsm->vlib_main;
+
+  if (bandwidth == 0.0)
+    return VNET_API_ERROR_INVALID_VALUE;
+
+  if (delay == 0.0)
+    return VNET_API_ERROR_INVALID_VALUE_2;
+
+  if (packet_size < 64.0 || packet_size > (f64) WHEEL_ENTRY_DATA_SIZE)
+    return VNET_API_ERROR_INVALID_VALUE_3;
+
+  /* Toss the old wheel(s)... */
+  if (nsm->is_configured)
+    {
+      for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
+       {
+         nsim_wheel_t *wp = nsm->wheel_by_thread[i];
+         munmap (wp, nsm->mmap_size);
+         nsm->wheel_by_thread[i] = 0;
+       }
+    }
+
+  nsm->delay = delay;
+
+  /* delay in seconds, bandwidth in bits/sec */
+  total_buffer_size_in_bytes = (u32) ((delay * bandwidth) / 8.0) + 0.5;
+
+  /*
+   * Work out how much buffering each worker needs, assuming decent
+   * RSS behavior.
+   */
+  if (num_workers)
+    per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
+  else
+    per_worker_buffer_size = total_buffer_size_in_bytes;
+
+  wheel_slots_per_worker = per_worker_buffer_size / packet_size;
+  wheel_slots_per_worker++;
+
+  /* Save these for the show command */
+  nsm->bandwidth = bandwidth;
+  nsm->packet_size = packet_size;
+
+  vec_validate (nsm->wheel_by_thread, num_workers);
+  vec_validate (nsm->buffer_indices_by_thread, num_workers);
+
+  /* Initialize the output scheduler wheels */
+  for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
+    {
+      nsim_wheel_t *wp;
+
+      nsm->mmap_size = sizeof (nsim_wheel_t)
+       + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
+
+      nsm->mmap_size += pagesize - 1;
+      nsm->mmap_size &= ~(pagesize - 1);
+
+      wp = clib_mem_vm_alloc (nsm->mmap_size);
+      ASSERT (wp != 0);
+      wp->wheel_size = wheel_slots_per_worker;
+      wp->cursize = 0;
+      wp->head = 0;
+      wp->tail = 0;
+      wp->entries = (void *) (wp + 1);
+      nsm->wheel_by_thread[i] = wp;
+      vec_validate (nsm->buffer_indices_by_thread[i], VLIB_FRAME_SIZE - 1);
+      _vec_len (nsm->buffer_indices_by_thread[i]) = 0;
+    }
+
+  vlib_worker_thread_barrier_sync (vm);
+
+  /* turn on the ring scrapers */
+  for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
+    {
+      vlib_main_t *this_vm = vlib_mains[i];
+
+      vlib_node_set_state (this_vm, nsim_input_node.index,
+                          VLIB_NODE_STATE_POLLING);
+    }
+
+  vlib_worker_thread_barrier_release (vm);
+
+  nsm->is_configured = 1;
+  return 0;
+}
+
+/*
+ * enable or disable the cross-connect
+ */
+static clib_error_t *
+nsim_enable_disable_command_fn (vlib_main_t * vm,
+                               unformat_input_t * input,
+                               vlib_cli_command_t * cmd)
+{
+  nsim_main_t *nsm = &nsim_main;
+  u32 sw_if_index0 = ~0;
+  u32 sw_if_index1 = ~0;
+  int enable_disable = 1;
+  u32 tmp;
+  int rv;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "disable"))
+       enable_disable = 0;
+      else if (unformat (input, "%U", unformat_vnet_sw_interface,
+                        nsm->vnet_main, &tmp))
+       {
+         if (sw_if_index0 == ~0)
+           sw_if_index0 = tmp;
+         else
+           sw_if_index1 = tmp;
+       }
+      else
+       break;
+    }
+
+  if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
+    return clib_error_return (0, "Please specify two interfaces...");
+
+  rv = nsim_enable_disable (nsm, sw_if_index0, sw_if_index1, enable_disable);
+
+  switch (rv)
+    {
+    case 0:
+      break;
+
+    case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
+      return clib_error_return (0, "Not configured, please 'set nsim' first");
+
+    case VNET_API_ERROR_INVALID_SW_IF_INDEX:
+      return clib_error_return
+       (0, "Invalid interface, only works on physical ports");
+      break;
+
+    case VNET_API_ERROR_UNIMPLEMENTED:
+      return clib_error_return (0,
+                               "Device driver doesn't support redirection");
+      break;
+
+    default:
+      return clib_error_return (0, "nsim_enable_disable returned %d", rv);
+    }
+  return 0;
+}
+
+/*?
+ * Enable or disable network simulation cross-connect on two interfaces
+ * The network simulator must have already been configured, see
+ * the "nsim_configure" command.
+ *
+ * Place the interfaces into a bridge group, to ensure that
+ * interfaces are in promiscuous mode.
+ *
+ * @cliexpar
+ * To enable or disable network simulation cross-connect
+ * @clistart
+ * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
+ * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
+ * @cliend
+ * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
+{
+  .path = "nsim enable-disable",
+  .short_help =
+  "nsim enable-disable <interface-name-1> <interface-name-2> [disable]",
+  .function = nsim_enable_disable_command_fn,
+};
+/* *INDENT-ON* */
+
+/* API message handler */
+static void vl_api_nsim_enable_disable_t_handler
+  (vl_api_nsim_enable_disable_t * mp)
+{
+  vl_api_nsim_enable_disable_reply_t *rmp;
+  nsim_main_t *nsm = &nsim_main;
+  int rv;
+
+  rv = nsim_enable_disable (nsm, ntohl (mp->sw_if_index0),
+                           ntohl (mp->sw_if_index1),
+                           (int) (mp->enable_disable));
+
+  REPLY_MACRO (VL_API_NSIM_ENABLE_DISABLE_REPLY);
+}
+
+/* API message handler */
+static void
+vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
+{
+  vl_api_nsim_configure_reply_t *rmp;
+  nsim_main_t *nsm = &nsim_main;
+  f64 delay, bandwidth, packet_size;
+  int rv;
+
+  delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
+  bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
+  packet_size = (f64) (ntohl (mp->average_packet_size));
+
+  rv = nsim_configure (nsm, bandwidth, delay, packet_size);
+
+  REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
+}
+
+
+/* Set up the API message handling tables */
+static clib_error_t *
+nsim_plugin_api_hookup (vlib_main_t * vm)
+{
+  nsim_main_t *nsm = &nsim_main;
+#define _(N,n)                                                  \
+    vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base),     \
+                           #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_nsim_plugin_api_msg;
+#undef _
+
+  return 0;
+}
+
+#define vl_msg_name_crc_list
+#include <nsim/nsim_all_api_h.h>
+#undef vl_msg_name_crc_list
+
+static void
+setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
+{
+#define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n  #crc, id + nsm->msg_id_base);
+  foreach_vl_msg_name_crc_nsim;
+#undef _
+}
+
+static clib_error_t *
+nsim_init (vlib_main_t * vm)
+{
+  nsim_main_t *nsm = &nsim_main;
+  clib_error_t *error = 0;
+  u8 *name;
+
+  nsm->vlib_main = vm;
+  nsm->vnet_main = vnet_get_main ();
+
+  name = format (0, "nsim_%08x%c", api_version, 0);
+
+  /* Ask for a correctly-sized block of API message decode slots */
+  nsm->msg_id_base = vl_msg_api_get_msg_ids
+    ((char *) name, VL_MSG_FIRST_AVAILABLE);
+
+  error = nsim_plugin_api_hookup (vm);
+
+  /* Add our API messages to the global name_crc hash table */
+  setup_message_id_table (nsm, &api_main);
+
+  vec_free (name);
+
+  return error;
+}
+
+VLIB_INIT_FUNCTION (nsim_init);
+
+/* *INDENT-OFF* */
+VNET_FEATURE_INIT (nsim, static) =
+{
+  .arc_name = "device-input",
+  .node_name = "nsim",
+  .runs_before = VNET_FEATURES ("ethernet-input"),
+};
+/* *INDENT-ON */
+
+/* *INDENT-OFF* */
+VLIB_PLUGIN_REGISTER () =
+{
+  .version = VPP_BUILD_VER,
+  .description = "network delay simulator plugin",
+};
+/* *INDENT-ON* */
+
+static uword
+unformat_delay (unformat_input_t * input, va_list * args)
+{
+  f64 *result = va_arg (*args, f64 *);
+  f64 tmp;
+
+  if (unformat (input, "%f us", &tmp))
+    *result = tmp * 1e-6;
+  else if (unformat (input, "%f ms", &tmp))
+    *result = tmp * 1e-3;
+  else if (unformat (input, "%f sec", &tmp))
+    *result = tmp;
+  else
+    return 0;
+
+  return 1;
+}
+
+static uword
+unformat_bandwidth (unformat_input_t * input, va_list * args)
+{
+  f64 *result = va_arg (*args, f64 *);
+  f64 tmp;
+
+  if (unformat (input, "%f gbit", &tmp))
+    *result = tmp * 1e9;
+  else if (unformat (input, "%f gbyte", &tmp))
+    *result = tmp * 8e9;
+  else
+    return 0;
+  return 1;
+}
+
+static clib_error_t *
+set_nsim_command_fn (vlib_main_t * vm,
+                    unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  nsim_main_t *nsm = &nsim_main;
+  f64 delay, bandwidth;
+  f64 packet_size = 1500.0;
+  u32 num_workers = vlib_num_workers ();
+  int rv;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "delay %U", unformat_delay, &delay))
+       ;
+      else if (unformat (input, "bandwidth %U", unformat_bandwidth,
+                        &bandwidth))
+       ;
+      else if (unformat (input, "packet-size %f", &packet_size))
+       ;
+      else
+       break;
+    }
+
+  rv = nsim_configure (nsm, bandwidth, delay, packet_size);
+
+  switch (rv)
+    {
+    case VNET_API_ERROR_INVALID_VALUE:
+      return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
+
+    case VNET_API_ERROR_INVALID_VALUE_2:
+      return clib_error_return (0, "invalid delay %.2f", delay);
+
+    case VNET_API_ERROR_INVALID_VALUE_3:
+      return clib_error_return (0, "invalid packet size %.2f", packet_size);
+
+    default:
+      return clib_error_return (0, "error %d", rv);
+
+    case 0:
+      break;
+    }
+
+  vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
+                  nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
+
+  if (num_workers)
+    vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
+                    nsm->mmap_size, nsm->mmap_size * num_workers);
+  else
+    vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
+
+  return 0;
+}
+
+/*?
+ * Configure the network simulation cross-connect
+ * Once the simulator is configured, use the "nsim enable-disable" command
+ * to set up a cross-connect with the supplied delay characteristics.
+ *
+ * The cross connect configuration may be changed without restarting vpp
+ * but it is good practice to shut down the interfaces.
+ *
+ * @cliexpar
+ * To configure the network delay simulator:
+ * @clistart
+ * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
+ *
+ * @cliend
+ * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (set_nsim_command, static) =
+{
+  .path = "set nsim",
+  .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>",
+  .function = set_nsim_command_fn,
+};
+/* *INDENT-ON*/
+
+
+static clib_error_t *
+show_nsim_command_fn (vlib_main_t * vm,
+                     unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  nsim_main_t *nsm = &nsim_main;
+  u32 num_workers = vlib_num_workers ();
+  int verbose = 0;
+
+  if (nsm->is_configured == 0)
+    return clib_error_return (0, "Network simulator not configured");
+
+  if (nsm->sw_if_index0 == 0)
+    return clib_error_return (0, "Network simulator not enabled");
+
+  if (unformat (input, "verbose"))
+    verbose = 1;
+
+  vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
+                  format_vnet_sw_if_index_name,
+                  nsm->vnet_main, nsm->sw_if_index0,
+                  format_vnet_sw_if_index_name,
+                  nsm->vnet_main, nsm->sw_if_index1);
+
+  vlib_cli_output (vm,
+                  "...inserting link delay of %.2f ms, %.2f ms round-trip",
+                  nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
+
+  if (verbose)
+    {
+
+      vlib_cli_output (vm, "  Configured bandwidth: %.2f gbit/sec",
+                      nsm->bandwidth / 1e9);
+      vlib_cli_output (vm, "  Configured packet size: %f", nsm->packet_size);
+      if (num_workers)
+       vlib_cli_output
+         (vm, "  Sim uses %llu bytes per thread, %llu bytes total",
+          nsm->mmap_size, nsm->mmap_size * num_workers);
+      else
+       vlib_cli_output (vm, "  Sim uses %llu bytes total", nsm->mmap_size);
+    }
+
+  return 0;
+}
+
+/*?
+ * Display state info for the network delay simulator.
+ *
+ * @cliexpar
+ * To display the state of the network simulator
+ * @clistart
+ * show nsim verbose
+ * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
+ * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
+ *  Configured bandwidth: 10.10 gbit/sec
+ *  Configured packet size: 128
+ *  Sim uses 157814784 bytes total
+ * @cliend
+ * @cliexcmd{show nsim}
+?*/
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_nsim_command, static) =
+{
+  .path = "show nsim",
+  .short_help = "Display network delay simulator configuration",
+  .function = show_nsim_command_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nsim/nsim.h b/src/plugins/nsim/nsim.h
new file mode 100644 (file)
index 0000000..d0b5ed3
--- /dev/null
@@ -0,0 +1,90 @@
+
+/*
+ * nsim.h - skeleton vpp engine plug-in header file
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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.
+ */
+#ifndef __included_nsim_h__
+#define __included_nsim_h__
+
+#include <vnet/vnet.h>
+#include <vnet/ip/ip.h>
+#include <vnet/ethernet/ethernet.h>
+
+#include <vppinfra/hash.h>
+#include <vppinfra/error.h>
+
+#define WHEEL_ENTRY_DATA_SIZE 1536     /* an even multiple of 64, pls */
+
+typedef struct
+{
+  f64 tx_time;
+  u32 tx_sw_if_index;
+  u32 current_length;
+    CLIB_CACHE_LINE_ALIGN_MARK (pad);
+  u8 data[WHEEL_ENTRY_DATA_SIZE];
+} nsim_wheel_entry_t;
+
+typedef struct
+{
+  u32 wheel_size;
+  u32 cursize;
+  u32 head;
+  u32 tail;
+  nsim_wheel_entry_t *entries;
+    CLIB_CACHE_LINE_ALIGN_MARK (pad);
+} nsim_wheel_t;
+
+typedef struct
+{
+  /* API message ID base */
+  u16 msg_id_base;
+
+  /* Two interfaces, cross-connected with delay */
+  u32 sw_if_index0, sw_if_index1;
+  u32 output_next_index0, output_next_index1;
+
+  /* Per-thread buffer / scheduler wheels */
+  nsim_wheel_t **wheel_by_thread;
+  u32 **buffer_indices_by_thread;
+
+  /* Config parameters */
+  f64 delay;
+  f64 bandwidth;
+  f64 packet_size;
+
+  u64 mmap_size;
+
+  /* Wheels are configured */
+  int is_configured;
+
+  /* convenience */
+  vlib_main_t *vlib_main;
+  vnet_main_t *vnet_main;
+} nsim_main_t;
+
+extern nsim_main_t nsim_main;
+
+extern vlib_node_registration_t nsim_node;
+extern vlib_node_registration_t nsim_input_node;
+
+#endif /* __included_nsim_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nsim/nsim_all_api_h.h b/src/plugins/nsim/nsim_all_api_h.h
new file mode 100644 (file)
index 0000000..ca54bba
--- /dev/null
@@ -0,0 +1,19 @@
+
+/*
+ * nsim_all_api_h.h - skeleton vpp engine plug-in api #include file
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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 the generated file, see BUILT_SOURCES in Makefile.am */
+#include <nsim/nsim.api.h>
diff --git a/src/plugins/nsim/nsim_input.c b/src/plugins/nsim/nsim_input.c
new file mode 100644 (file)
index 0000000..50dbcb9
--- /dev/null
@@ -0,0 +1,228 @@
+/*
+ * nsim.c - skeleton vpp engine plug-in
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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 <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <vppinfra/error.h>
+#include <nsim/nsim.h>
+
+typedef struct
+{
+  f64 expired;
+  u32 tx_sw_if_index;
+} nsim_tx_trace_t;
+
+#ifndef CLIB_MARCH_VARIANT
+/* packet trace format function */
+static u8 *
+format_nsim_tx_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  nsim_tx_trace_t *t = va_arg (*args, nsim_tx_trace_t *);
+
+  s = format (s, "NSIM: tx at %.6f sw_if_index %d",
+             t->expired, t->tx_sw_if_index);
+  return s;
+}
+#endif /* CLIB_MARCH_VARIANT */
+
+vlib_node_registration_t nsim_node;
+
+#define foreach_nsim_tx_error                      \
+_(TX, "Packets transmitted")                    \
+_(DROPPED, "No buffer drops")
+
+typedef enum
+{
+#define _(sym,str) NSIM_TX_ERROR_##sym,
+  foreach_nsim_tx_error
+#undef _
+    NSIM_N_ERROR,
+} nsim_tx_error_t;
+
+#ifndef CLIB_MARCH_VARIANT
+static char *nsim_tx_error_strings[] = {
+#define _(sym,string) string,
+  foreach_nsim_tx_error
+#undef _
+};
+#endif /* CLIB_MARCH_VARIANT */
+
+typedef enum
+{
+  NSIM_NEXT_DROP,
+  NSIM_N_NEXT,
+} nsim_next_t;
+
+always_inline uword
+nsim_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
+                  vlib_frame_t * f, int is_trace)
+{
+  nsim_main_t *nsm = &nsim_main;
+  u32 my_thread_index = vm->thread_index;
+  u32 *my_buffer_cache = nsm->buffer_indices_by_thread[my_thread_index];
+  nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
+  f64 now = vlib_time_now (vm);
+  uword n_rx_packets = 0;
+  vlib_buffer_t *b0;
+  u32 bi0, next0;
+  vlib_buffer_free_list_t *fl;
+  u32 *to_next;
+  u32 next_index;
+  u32 n_left_to_next;
+  nsim_wheel_entry_t *ep;
+
+  /* Nothing on the scheduler wheel? */
+  if (wp->cursize == 0)
+    return 0;
+
+  /* First entry on the wheel isn't expired? */
+  ep = wp->entries + wp->head;
+  if (ep->tx_time > now)
+    return n_rx_packets;
+
+  /*
+   * We use per-thread buffer caches, so we need the freelist to
+   * initialize them...
+   */
+  fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+  next_index = node->cached_next_index;
+
+  while (wp->cursize)
+    {
+      /* Be aware: this is not the usual coding pattern */
+      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+
+      while (n_left_to_next > 0 && ep->tx_time <= now)
+       {
+         /* Out of local buffer cache? */
+         if (PREDICT_FALSE (_vec_len (my_buffer_cache) == 0))
+           {
+             u32 n =
+               vlib_buffer_alloc (vm, my_buffer_cache, VLIB_FRAME_SIZE);
+             _vec_len (my_buffer_cache) = n;
+
+             /* Ugh, drop the rest of the expired entries */
+             if (n == 0)
+               {
+                 u32 drops = 0;
+                 while (ep->tx_time <= now && wp->cursize)
+                   {
+                     wp->head++;
+                     if (wp->head == wp->wheel_size)
+                       wp->head = 0;
+                     ep = wp->entries + wp->head;
+                     wp->cursize--;
+                     drops++;
+                   }
+                 /* Count the drops */
+                 vlib_node_increment_counter (vm, node->node_index,
+                                              NSIM_TX_ERROR_DROPPED, drops);
+                 /* Ship any pkts we already processed */
+                 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+                 return n_rx_packets + drops;
+               }
+           }
+
+         /* Allocate a buffer */
+         bi0 = my_buffer_cache[_vec_len (my_buffer_cache) - 1];
+         _vec_len (my_buffer_cache) -= 1;
+
+         to_next[0] = bi0;
+         to_next += 1;
+         n_left_to_next -= 1;
+
+         b0 = vlib_get_buffer (vm, bi0);
+         /* Initialize the buffer */
+         vlib_buffer_init_for_free_list (b0, fl);
+
+         b0->current_data = 0;
+         b0->current_length = ep->current_length;
+
+         /* Copy data from the ring */
+         clib_memcpy (b0->data, ep->data, ep->current_length);
+         b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
+         vnet_buffer (b0)->sw_if_index[VLIB_TX] = ep->tx_sw_if_index;
+         vnet_buffer (b0)->sw_if_index[VLIB_RX] =
+           (ep->tx_sw_if_index == nsm->sw_if_index0) ? nsm->sw_if_index1 :
+           nsm->sw_if_index0;
+         next0 = (ep->tx_sw_if_index == nsm->sw_if_index0) ?
+           nsm->output_next_index0 : nsm->output_next_index1;
+
+         /* verify speculative enqueue, maybe switch current next frame */
+         vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+                                          to_next, n_left_to_next,
+                                          bi0, next0);
+         /* Advance to the next ring entry */
+         wp->head++;
+         if (wp->head == wp->wheel_size)
+           wp->head = 0;
+         wp->cursize--;
+         ep = wp->entries + wp->head;
+         n_rx_packets++;
+
+         /* Out of ring entries? */
+         if (PREDICT_FALSE (wp->cursize == 0))
+           break;
+       }
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+
+      /* If the current entry hasn't expired, we're done */
+      if (ep->tx_time > now)
+       break;
+    }
+  return n_rx_packets;
+}
+
+VLIB_NODE_FN (nsim_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
+                               vlib_frame_t * frame)
+{
+  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
+    return nsim_input_inline (vm, node, frame, 1 /* is_trace */ );
+  else
+    return nsim_input_inline (vm, node, frame, 0 /* is_trace */ );
+
+}
+
+/* *INDENT-OFF* */
+#ifndef CLIB_MARCH_VARIANT
+VLIB_REGISTER_NODE (nsim_input_node) =
+{
+  .type = VLIB_NODE_TYPE_INPUT,
+  .name = "nsim-wheel",
+
+  /* Will be enabled if/when the feature is configured */
+  .state = VLIB_NODE_STATE_DISABLED,
+
+  .format_trace = format_nsim_tx_trace,
+
+  .n_errors = NSIM_N_ERROR,
+  .error_strings = nsim_tx_error_strings,
+};
+#endif /* CLIB_MARCH_VARIANT */
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nsim/nsim_msg_enum.h b/src/plugins/nsim/nsim_msg_enum.h
new file mode 100644 (file)
index 0000000..116146f
--- /dev/null
@@ -0,0 +1,31 @@
+
+/*
+ * nsim_msg_enum.h - skeleton vpp engine plug-in message enumeration
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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.
+ */
+#ifndef included_nsim_msg_enum_h
+#define included_nsim_msg_enum_h
+
+#include <vppinfra/byte_order.h>
+
+#define vl_msg_id(n,h) n,
+typedef enum {
+#include <nsim/nsim_all_api_h.h>
+    /* We'll want to know how many messages IDs we need... */
+    VL_MSG_FIRST_AVAILABLE,
+} vl_msg_id_t;
+#undef vl_msg_id
+
+#endif /* included_nsim_msg_enum_h */
diff --git a/src/plugins/nsim/nsim_test.c b/src/plugins/nsim/nsim_test.c
new file mode 100644 (file)
index 0000000..0e2a26a
--- /dev/null
@@ -0,0 +1,285 @@
+/*
+ * nsim.c - skeleton vpp-api-test plug-in
+ *
+ * Copyright (c) <current-year> <your-organization>
+ * 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 <vat/vat.h>
+#include <vlibapi/api.h>
+#include <vlibmemory/api.h>
+#include <vppinfra/error.h>
+
+uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
+
+/* Declare message IDs */
+#include <nsim/nsim_msg_enum.h>
+
+/* define message structures */
+#define vl_typedefs
+#include <nsim/nsim_all_api_h.h>
+#undef vl_typedefs
+
+/* declare message handlers for each api */
+
+#define vl_endianfun           /* define message structures */
+#include <nsim/nsim_all_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...)
+#define vl_printfun
+#include <nsim/nsim_all_api_h.h>
+#undef vl_printfun
+
+/* Get the API version number. */
+#define vl_api_version(n,v) static u32 api_version=(v);
+#include <nsim/nsim_all_api_h.h>
+#undef vl_api_version
+
+
+typedef struct
+{
+  /* API message ID base */
+  u16 msg_id_base;
+  vat_main_t *vat_main;
+} nsim_test_main_t;
+
+nsim_test_main_t nsim_test_main;
+
+#define __plugin_msg_base nsim_test_main.msg_id_base
+#include <vlibapi/vat_helper_macros.h>
+
+#define foreach_standard_reply_retval_handler   \
+_(nsim_enable_disable_reply)                    \
+_(nsim_configure_reply)
+
+#define _(n)                                            \
+    static void vl_api_##n##_t_handler                  \
+    (vl_api_##n##_t * mp)                               \
+    {                                                   \
+        vat_main_t * vam = nsim_test_main.vat_main;   \
+        i32 retval = ntohl(mp->retval);                 \
+        if (vam->async_mode) {                          \
+            vam->async_errors += (retval < 0);          \
+        } else {                                        \
+            vam->retval = retval;                       \
+            vam->result_ready = 1;                      \
+        }                                               \
+    }
+foreach_standard_reply_retval_handler;
+#undef _
+
+/*
+ * Table of message reply handlers, must include boilerplate handlers
+ * we just generated
+ */
+#define foreach_vpe_api_reply_msg                       \
+_(NSIM_ENABLE_DISABLE_REPLY, nsim_enable_disable_reply) \
+_(NSIM_CONFIGURE_REPLY, nsim_configure_reply)
+
+static int
+api_nsim_enable_disable (vat_main_t * vam)
+{
+  unformat_input_t *i = vam->input;
+  int enable_disable = 1;
+  u32 sw_if_index0 = ~0;
+  u32 sw_if_index1 = ~0;
+  u32 tmp;
+  vl_api_nsim_enable_disable_t *mp;
+  int ret;
+
+  /* Parse args required to build the message */
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "%U", unformat_sw_if_index, vam, &tmp))
+       {
+         if (sw_if_index0 == ~0)
+           sw_if_index0 = tmp;
+         else
+           sw_if_index1 = tmp;
+       }
+      else if (unformat (i, "sw_if_index %d", &tmp))
+       {
+         if (sw_if_index0 == ~0)
+           sw_if_index0 = tmp;
+         else
+           sw_if_index1 = tmp;
+       }
+      else if (unformat (i, "disable"))
+       enable_disable = 0;
+      else
+       break;
+    }
+
+  if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
+    {
+      errmsg ("missing interface name / explicit sw_if_index number \n");
+      return -99;
+    }
+
+  /* Construct the API message */
+  M (NSIM_ENABLE_DISABLE, mp);
+  mp->sw_if_index0 = ntohl (sw_if_index0);
+  mp->sw_if_index1 = ntohl (sw_if_index1);
+  mp->enable_disable = enable_disable;
+
+  /* send it... */
+  S (mp);
+
+  /* Wait for a reply... */
+  W (ret);
+  return ret;
+}
+
+static uword
+unformat_delay (unformat_input_t * input, va_list * args)
+{
+  f64 *result = va_arg (*args, f64 *);
+  f64 tmp;
+
+  if (unformat (input, "%f us", &tmp))
+    *result = tmp * 1e-6;
+  else if (unformat (input, "%f ms", &tmp))
+    *result = tmp * 1e-3;
+  else if (unformat (input, "%f sec", &tmp))
+    *result = tmp;
+  else
+    return 0;
+
+  return 1;
+}
+
+static uword
+unformat_bandwidth (unformat_input_t * input, va_list * args)
+{
+  f64 *result = va_arg (*args, f64 *);
+  f64 tmp;
+
+  if (unformat (input, "%f gbit", &tmp))
+    *result = tmp * 1e9;
+  else if (unformat (input, "%f gbyte", &tmp))
+    *result = tmp * 8e9;
+  else
+    return 0;
+  return 1;
+}
+
+static int
+api_nsim_configure (vat_main_t * vam)
+{
+  vl_api_nsim_configure_t *mp;
+  unformat_input_t *i = vam->input;
+  f64 delay = 0.0, bandwidth = 0.0;
+  f64 packet_size = 1500.0;
+  u32 num_workers = vlib_num_workers ();
+  int ret;
+
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "delay %U", unformat_delay, &delay))
+       ;
+      else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
+       ;
+      else if (unformat (i, "packet-size %f", &packet_size))
+       ;
+      else
+       break;
+    }
+
+  if (delay == 0.0 || bandwidth == 0.0)
+    {
+      errmsg ("must specify delay and bandwidth");
+      return -99;
+    }
+
+  /* Construct the API message */
+  M (NSIM_CONFIGURE, mp);
+  mp->delay_in_usec = (u32) (delay * 1e6);
+  mp->delay_in_usec = ntohl (mp->delay_in_usec);
+  mp->average_packet_size = (u32) (packet_size);
+  mp->average_packet_size = ntohl (mp->average_packet_size);
+  mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
+  mp->bandwidth_in_bits_per_second =
+    clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
+
+  /* send it... */
+  S (mp);
+
+  /* Wait for a reply... */
+  W (ret);
+  return ret;
+}
+
+/*
+ * List of messages that the api test plugin sends,
+ * and that the data plane plugin processes
+ */
+#define foreach_vpe_api_msg                                             \
+_(nsim_enable_disable,                                                  \
+"[<intfc0> | sw_if_index <swif0>] [<intfc1> | sw_if_index <swif1>] [disable]") \
+_(nsim_configure, "delay <time> bandwidth <bw> [packet-size <nn>]")
+
+static void
+nsim_api_hookup (vat_main_t * vam)
+{
+  nsim_test_main_t *sm = &nsim_test_main;
+  /* Hook up handlers for replies from the data plane plug-in */
+#define _(N,n)                                                  \
+    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
+                           #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_reply_msg;
+#undef _
+
+  /* API messages we can send */
+#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
+  foreach_vpe_api_msg;
+#undef _
+
+  /* Help strings */
+#define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
+  foreach_vpe_api_msg;
+#undef _
+}
+
+clib_error_t *
+vat_plugin_register (vat_main_t * vam)
+{
+  nsim_test_main_t *sm = &nsim_test_main;
+  u8 *name;
+
+  sm->vat_main = vam;
+
+  /* Ask the vpp engine for the first assigned message-id */
+  name = format (0, "nsim_%08x%c", api_version, 0);
+  sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
+
+  if (sm->msg_id_base != (u16) ~ 0)
+    nsim_api_hookup (vam);
+
+  vec_free (name);
+
+  return 0;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index 5859bb9..05a4a45 100644 (file)
@@ -143,6 +143,7 @@ _(BIER_BSL_UNSUP, -146, "BIER bit-string-length unsupported")               \
 _(INSTANCE_IN_USE, -147, "Instance in use")                            \
 _(INVALID_SESSION_ID, -148, "session ID out of range")                 \
 _(ACL_IN_USE_BY_LOOKUP_CONTEXT, -149, "ACL in use by a lookup context")        \
+_(INVALID_VALUE_3, -150, "Invalid value #3")
 
 typedef enum
 {