hs-test: fix CPU alloc when running in parallel
[vpp.git] / src / plugins / nsim / nsim.c
index 00bf84e..1c5b26b 100644 (file)
@@ -127,20 +127,20 @@ nsim_output_feature_enable_disable (nsim_main_t * nsm, u32 sw_if_index,
 }
 
 static nsim_wheel_t *
-nsim_wheel_alloc (nsim_main_t * nsm, u32 wheel_slots)
+nsim_wheel_alloc (nsim_main_t *nsm)
 {
   u32 pagesize = getpagesize ();
   nsim_wheel_t *wp;
 
-  nsm->mmap_size = sizeof (nsim_wheel_t)
-    + wheel_slots * sizeof (nsim_wheel_entry_t);
+  nsm->mmap_size = sizeof (nsim_wheel_t) +
+                  nsm->wheel_slots_per_wrk * 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;
+  wp->wheel_size = nsm->wheel_slots_per_wrk;
   wp->cursize = 0;
   wp->head = 0;
   wp->tail = 0;
@@ -150,7 +150,7 @@ nsim_wheel_alloc (nsim_main_t * nsm, u32 wheel_slots)
 }
 
 static int
-nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
+nsim_configure (nsim_main_t *nsm, f64 bandwidth, f64 delay, u32 packet_size,
                f64 drop_fraction, f64 reorder_fraction)
 {
   u64 total_buffer_size_in_bytes, per_worker_buffer_size, wheel_slots_per_wrk;
@@ -163,7 +163,7 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   if (delay == 0.0)
     return VNET_API_ERROR_INVALID_VALUE_2;
 
-  if (packet_size < 64.0 || packet_size > 9000.0)
+  if (packet_size < 64 || packet_size > 9000)
     return VNET_API_ERROR_INVALID_VALUE_3;
 
   if (reorder_fraction > 0.0 && delay == 0.0)
@@ -201,13 +201,14 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   /* Save these for the show command */
   nsm->bandwidth = bandwidth;
   nsm->packet_size = packet_size;
+  nsm->wheel_slots_per_wrk = wheel_slots_per_wrk;
 
   vec_validate (nsm->wheel_by_thread, num_workers);
 
   /* Initialize the output scheduler wheels */
   i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
   for (; i < num_workers + 1; i++)
-    nsm->wheel_by_thread[i] = nsim_wheel_alloc (nsm, wheel_slots_per_wrk);
+    nsm->wheel_by_thread[i] = nsim_wheel_alloc (nsm);
 
   vlib_worker_thread_barrier_sync (vm);
 
@@ -215,7 +216,7 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
   for (; i < num_workers + 1; i++)
     {
-      vlib_main_t *this_vm = vlib_mains[i];
+      vlib_main_t *this_vm = vlib_get_main_by_index (i);
 
       vlib_node_set_state (this_vm, nsim_input_node.index,
                           VLIB_NODE_STATE_POLLING);
@@ -333,7 +334,6 @@ VLIB_CONFIG_FUNCTION (nsim_config, "nsim");
  * @cliend
  * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
 {
   .path = "nsim cross-connect enable-disable",
@@ -342,7 +342,6 @@ VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
   "<interface-name-2> [disable]",
   .function = nsim_cross_connect_enable_disable_command_fn,
 };
-/* *INDENT-ON* */
 
 /* API message handler */
 static void vl_api_nsim_cross_connect_enable_disable_t_handler
@@ -522,7 +521,6 @@ nsim_output_feature_enable_disable_command_fn (vlib_main_t * vm,
  * @cliend
  * @cliexcmd{nsim output-feature enable-disable <intfc> [disable]}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
 {
   .path = "nsim output-feature enable-disable",
@@ -530,7 +528,6 @@ VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
   "nsim output-feature enable-disable <interface-name> [disable]",
   .function = nsim_output_feature_enable_disable_command_fn,
 };
-/* *INDENT-ON* */
 
 #include <nsim/nsim.api.c>
 static clib_error_t *
@@ -549,31 +546,24 @@ nsim_init (vlib_main_t * vm)
 
 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* */
-VNET_FEATURE_INIT (nsim_feature, static) =
-{
+VNET_FEATURE_INIT (nsim_feature, static) = {
   .arc_name = "interface-output",
   .node_name = "nsim-output-feature",
-  .runs_before = VNET_FEATURES ("interface-tx"),
+  .runs_before = VNET_FEATURES ("interface-output-arc-end"),
 };
-/* *INDENT-ON */
 
-/* *INDENT-OFF* */
 VLIB_PLUGIN_REGISTER () =
 {
   .version = VPP_BUILD_VER,
   .description = "Network Delay Simulator",
 };
-/* *INDENT-ON* */
 
 static uword
 unformat_delay (unformat_input_t * input, va_list * args)
@@ -603,11 +593,51 @@ unformat_bandwidth (unformat_input_t * input, va_list * args)
     *result = tmp * 1e9;
   else if (unformat (input, "%f gbyte", &tmp))
     *result = tmp * 8e9;
+  else if (unformat (input, "%f gbps", &tmp))
+    *result = tmp * 1e9;
+  else if (unformat (input, "%f mbps", &tmp))
+    *result = tmp * 1e6;
+  else if (unformat (input, "%f kbps", &tmp))
+    *result = tmp * 1e3;
+  else if (unformat (input, "%f bps", &tmp))
+    *result = tmp;
   else
     return 0;
   return 1;
 }
 
+static u8 *
+format_delay (u8 *s, va_list *args)
+{
+  f64 delay = va_arg (*args, f64);
+
+  if (delay < 1e-3)
+    s = format (s, "%.1f us", delay * 1e6);
+  else if (delay < 1)
+    s = format (s, "%.1f ms", delay * 1e3);
+  else
+    s = format (s, "%f sec", delay);
+
+  return s;
+}
+
+static u8 *
+format_bandwidth (u8 *s, va_list *args)
+{
+  f64 bandwidth = va_arg (*args, f64);
+
+  if (bandwidth >= 1e9)
+    s = format (s, "%.1f gbps", bandwidth / 1e9);
+  else if (bandwidth >= 1e6)
+    s = format (s, "%.1f mbps", bandwidth / 1e6);
+  else if (bandwidth >= 1e3)
+    s = format (s, "%.1f kbps", bandwidth / 1e3);
+  else
+    s = format (s, "%f bps", bandwidth);
+
+  return s;
+}
+
 static u8 *
 format_nsim_config (u8 * s, va_list * args)
 {
@@ -615,7 +645,7 @@ format_nsim_config (u8 * s, va_list * args)
   nsim_main_t *nsm = &nsim_main;
 
   s = format (s, "configuration\n");
-  s = format (s, " delay (ms): %.2f\n", nsm->delay * 1e3);
+  s = format (s, " delay: %U\n", format_delay, nsm->delay);
   if (nsm->drop_fraction)
     s = format (s, " drop fraction: %.5f\n", nsm->drop_fraction);
   else
@@ -625,7 +655,8 @@ format_nsim_config (u8 * s, va_list * args)
   else
     s = format (s, " reorder fraction: 0\n");
   s = format (s, " packet size: %u\n", nsm->packet_size);
-  s = format (s, " throughput (Gbps): %.2f\n", nsm->bandwidth / 1e9);
+  s = format (s, " worker wheel size: %u\n", nsm->wheel_slots_per_wrk);
+  s = format (s, " throughput: %U\n", format_bandwidth, nsm->bandwidth);
 
   if (verbose)
     {
@@ -667,9 +698,8 @@ static clib_error_t *
 set_nsim_command_fn (vlib_main_t * vm,
                     unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  f64 drop_fraction = 0.0, reorder_fraction = 0.0;
-  f64 delay, bandwidth, packet_size = 1500.0;
-  u32 packets_per_drop, packets_per_reorder;
+  f64 drop_fraction = 0.0, reorder_fraction = 0.0, delay, bandwidth;
+  u32 packets_per_drop, packets_per_reorder, packet_size = 1500;
   nsim_main_t *nsm = &nsim_main;
   int rv;
 
@@ -680,7 +710,7 @@ set_nsim_command_fn (vlib_main_t * vm,
       else if (unformat (input, "bandwidth %U", unformat_bandwidth,
                         &bandwidth))
        ;
-      else if (unformat (input, "packet-size %f", &packet_size))
+      else if (unformat (input, "packet-size %u", &packet_size))
        ;
       else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
        {
@@ -757,7 +787,6 @@ set_nsim_command_fn (vlib_main_t * vm,
  * @cliend
  * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (set_nsim_command, static) =
 {
   .path = "set nsim",
@@ -765,7 +794,6 @@ VLIB_CLI_COMMAND (set_nsim_command, static) =
   "    [packets-per-drop <nn>][drop-fraction <f64: 0.0 - 1.0>]",
   .function = set_nsim_command_fn,
 };
-/* *INDENT-ON*/
 
 
 static clib_error_t *
@@ -802,14 +830,12 @@ show_nsim_command_fn (vlib_main_t * vm,
  * @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