nsim: basic reorder support 98/28098/16
authorFlorin Coras <fcoras@cisco.com>
Wed, 29 Jul 2020 02:20:40 +0000 (19:20 -0700)
committerDave Barach <openvpp@barachs.net>
Fri, 31 Jul 2020 19:34:20 +0000 (19:34 +0000)
Reorder delayed packets, i.e., flush instead of delay, with a configured
rate.

Type: feature

Change-Id: Ib1294f5f1c9b6e98a12b1bb0be655e54facfed3a
Signed-off-by: Florin Coras <fcoras@cisco.com>
src/plugins/nsim/node.c
src/plugins/nsim/nsim.api
src/plugins/nsim/nsim.c
src/plugins/nsim/nsim.h
src/plugins/nsim/nsim_test.c
src/vnet/api_errno.h

index 57afbd2..5d72a91 100644 (file)
@@ -54,7 +54,8 @@ vlib_node_registration_t nsim_node;
 #define foreach_nsim_error                              \
 _(BUFFERED, "Packets buffered")                         \
 _(DROPPED, "Packets dropped due to lack of space")     \
-_(LOSS, "Network loss simulation drop packets")
+_(LOSS, "Network loss simulation drop packets")                \
+_(REORDERED, "Packets reordered")
 
 typedef enum
 {
@@ -78,27 +79,124 @@ typedef enum
   NSIM_N_NEXT,
 } nsim_next_t;
 
+static void
+nsim_set_actions (nsim_main_t * nsm, vlib_buffer_t ** b,
+                 nsim_node_ctx_t * ctx, u32 n_actions)
+{
+  int i;
+
+  memset (ctx->action, 0, n_actions * sizeof (ctx->action[0]));
+
+  if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
+    {
+      for (i = 0; i < n_actions; i++)
+       if (random_f64 (&nsm->seed) <= nsm->drop_fraction)
+         ctx->action[i] |= NSIM_ACTION_DROP;
+    }
+
+  if (PREDICT_FALSE (nsm->reorder_fraction != 0.0))
+    {
+      for (i = 0; i < n_actions; i++)
+       if (random_f64 (&nsm->seed) <= nsm->reorder_fraction)
+         ctx->action[i] |= NSIM_ACTION_REORDER;
+    }
+}
+
+static void
+nsim_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
+                  vlib_buffer_t * b, nsim_node_ctx_t * ctx, u32 is_drop)
+{
+  if (b->flags & VLIB_BUFFER_IS_TRACED)
+    {
+      nsim_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
+      t->expires = ctx->expires;
+      t->is_drop = is_drop;
+      t->is_lost = ctx->action[0] & NSIM_ACTION_DROP;
+      t->tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
+    }
+}
+
+always_inline void
+nsim_buffer_fwd_lookup (nsim_main_t * nsm, vlib_buffer_t * b,
+                       u32 * next, u8 is_cross_connect)
+{
+  if (is_cross_connect)
+    {
+      vnet_buffer (b)->sw_if_index[VLIB_TX] =
+       (vnet_buffer (b)->sw_if_index[VLIB_RX] == nsm->sw_if_index0) ?
+       nsm->sw_if_index1 : nsm->sw_if_index0;
+      *next =
+       (vnet_buffer (b)->sw_if_index[VLIB_TX] == nsm->sw_if_index0) ?
+       nsm->output_next_index0 : nsm->output_next_index1;
+    }
+  else                         /* output feature, even easier... */
+    {
+      u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
+      *next = nsm->output_next_index_by_sw_if_index[sw_if_index];
+    }
+}
+
+always_inline void
+nsim_dispatch_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
+                     nsim_main_t * nsm, nsim_wheel_t * wp, vlib_buffer_t * b,
+                     u32 bi, nsim_node_ctx_t * ctx, u8 is_cross_connect,
+                     u8 is_trace)
+{
+  if (PREDICT_TRUE (!(ctx->action[0] & NSIM_ACTION_DROP)))
+    {
+      if (PREDICT_FALSE (ctx->action[0] & NSIM_ACTION_REORDER))
+       {
+         u32 next;
+         ctx->reord[0] = bi;
+         vnet_get_config_data (&ctx->fcm->config_main,
+                               &b->current_config_index, &next, 0);
+         ctx->reord_nexts[0] = next;
+         ctx->reord += 1;
+         ctx->reord_nexts += 1;
+         goto trace;
+       }
+
+      nsim_wheel_entry_t *ep = wp->entries + wp->tail;
+      wp->tail++;
+      if (wp->tail == wp->wheel_size)
+       wp->tail = 0;
+      wp->cursize++;
+
+      ep->tx_time = ctx->expires;
+      ep->rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
+      nsim_buffer_fwd_lookup (nsm, b, &ep->output_next_index,
+                             is_cross_connect);
+      ep->tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
+      ep->buffer_index = bi;
+      ctx->n_buffered += 1;
+    }
+  else
+    {
+      ctx->n_loss += 1;
+      ctx->drop[0] = bi;
+      ctx->drop += 1;
+    }
+
+trace:
+
+  if (PREDICT_FALSE (is_trace))
+    nsim_trace_buffer (vm, node, b, ctx, 0);
+
+  ctx->action += 1;
+}
+
 always_inline uword
 nsim_inline (vlib_main_t * vm,
             vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace,
             int is_cross_connect)
 {
   nsim_main_t *nsm = &nsim_main;
-  u32 n_left_from, *from;
-  u32 *to_next, n_left_to_next;
-  u32 drops[VLIB_FRAME_SIZE], *drop;
+  u32 n_left_from, *from, drops[VLIB_FRAME_SIZE], reorders[VLIB_FRAME_SIZE];
+  nsim_wheel_t *wp = nsm->wheel_by_thread[vm->thread_index];
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
-  u8 is_drop[4];
-  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;
-  f64 rnd[4];
-  u32 no_buffer_error = node->errors[NSIM_ERROR_DROPPED];
-  u32 loss_error = node->errors[NSIM_ERROR_LOSS];
-  u32 buffered = 0;
-  nsim_wheel_entry_t *ep = 0;
+  u16 reorders_nexts[VLIB_FRAME_SIZE];
+  u8 actions[VLIB_FRAME_SIZE];
+  nsim_node_ctx_t ctx;
 
   ASSERT (wp);
 
@@ -107,8 +205,17 @@ nsim_inline (vlib_main_t * vm,
 
   vlib_get_buffers (vm, from, bufs, n_left_from);
   b = bufs;
-  next = nexts;
-  drop = drops;
+
+  ctx.fcm = vnet_feature_get_config_main (nsm->arc_index);
+  ctx.n_loss = 0;
+  ctx.n_buffered = 0;
+  ctx.drop = drops;
+  ctx.reord = reorders;
+  ctx.reord_nexts = reorders_nexts;
+  ctx.action = actions;
+  ctx.expires = vlib_time_now (vm) + nsm->delay;
+
+  nsim_set_actions (nsm, b, &ctx, n_left_from);
 
   while (n_left_from >= 8)
     {
@@ -117,217 +224,19 @@ nsim_inline (vlib_main_t * vm,
       vlib_prefetch_buffer_header (b[6], STORE);
       vlib_prefetch_buffer_header (b[7], STORE);
 
-      memset (&is_drop, 0, sizeof (is_drop));
-      next[0] = next[1] = next[2] = next[3] = NSIM_NEXT_DROP;
       if (PREDICT_FALSE (wp->cursize + 4 >= wp->wheel_size))
        goto slow_path;
-      if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
-       {
-         rnd[0] = random_f64 (&nsm->seed);
-         rnd[1] = random_f64 (&nsm->seed);
-         rnd[2] = random_f64 (&nsm->seed);
-         rnd[3] = random_f64 (&nsm->seed);
-
-         if (rnd[0] <= nsm->drop_fraction)
-           {
-             b[0]->error = loss_error;
-             is_drop[0] = 1;
-           }
-         if (rnd[1] <= nsm->drop_fraction)
-           {
-             b[1]->error = loss_error;
-             is_drop[1] = 1;
-           }
-         if (rnd[2] <= nsm->drop_fraction)
-           {
-             b[2]->error = loss_error;
-             is_drop[2] = 1;
-           }
-         if (rnd[3] <= nsm->drop_fraction)
-           {
-             b[3]->error = loss_error;
-             is_drop[3] = 1;
-           }
-       }
-
-      if (PREDICT_TRUE (is_drop[0] == 0))
-       {
-         ep = wp->entries + wp->tail;
-         wp->tail++;
-         if (wp->tail == wp->wheel_size)
-           wp->tail = 0;
-         wp->cursize++;
-
-         ep->tx_time = expires;
-         ep->rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
-         if (is_cross_connect)
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
-               (vnet_buffer (b[0])->sw_if_index[VLIB_RX] ==
-                nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
-             ep->output_next_index =
-               (ep->tx_sw_if_index ==
-                nsm->sw_if_index0) ? nsm->
-               output_next_index0 : nsm->output_next_index1;
-           }
-         else                  /* output feature, even easier... */
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
-             ep->output_next_index =
-               nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
-           }
-         ep->buffer_index = from[0];
-         buffered++;
-       }
-      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_drop[0];
-             t->is_lost = b[0]->error == loss_error;
-             t->tx_sw_if_index = (is_drop[0] == 0) ? ep->tx_sw_if_index : 0;
-           }
-       }
 
-      if (PREDICT_TRUE (is_drop[1] == 0))
-       {
-         ep = wp->entries + wp->tail;
-         wp->tail++;
-         if (wp->tail == wp->wheel_size)
-           wp->tail = 0;
-         wp->cursize++;
-
-         ep->tx_time = expires;
-         ep->rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
-         if (is_cross_connect)
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
-               (vnet_buffer (b[1])->sw_if_index[VLIB_RX] ==
-                nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
-             ep->output_next_index =
-               (ep->tx_sw_if_index ==
-                nsm->sw_if_index0) ? nsm->
-               output_next_index0 : nsm->output_next_index1;
-           }
-         else                  /* output feature, even easier... */
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
-             ep->output_next_index =
-               nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
-           }
-         ep->buffer_index = from[1];
-         buffered++;
-       }
-
-      if (is_trace)
-       {
-         if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
-           {
-             nsim_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
-             t->expires = expires;
-             t->is_drop = is_drop[1];
-             t->is_lost = b[1]->error == loss_error;
-             t->tx_sw_if_index = (is_drop[1] == 0) ? ep->tx_sw_if_index : 0;
-           }
-       }
-
-      if (PREDICT_TRUE (is_drop[2] == 0))
-       {
-         ep = wp->entries + wp->tail;
-         wp->tail++;
-         if (wp->tail == wp->wheel_size)
-           wp->tail = 0;
-         wp->cursize++;
-
-         ep->tx_time = expires;
-         ep->rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
-         if (is_cross_connect)
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
-               (vnet_buffer (b[2])->sw_if_index[VLIB_RX] ==
-                nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
-             ep->output_next_index =
-               (ep->tx_sw_if_index ==
-                nsm->sw_if_index0) ? nsm->
-               output_next_index0 : nsm->output_next_index1;
-           }
-         else                  /* output feature, even easier... */
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
-             ep->output_next_index =
-               nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
-           }
-         ep->buffer_index = from[2];
-         buffered++;
-       }
-
-      if (is_trace)
-       {
-         if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
-           {
-             nsim_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
-             t->expires = expires;
-             t->is_drop = is_drop[2];
-             t->is_lost = b[2]->error == loss_error;
-             t->tx_sw_if_index = (is_drop[2] == 0) ? ep->tx_sw_if_index : 0;
-           }
-       }
-
-      if (PREDICT_TRUE (is_drop[3] == 0))
-       {
-         ep = wp->entries + wp->tail;
-         wp->tail++;
-         if (wp->tail == wp->wheel_size)
-           wp->tail = 0;
-         wp->cursize++;
-
-         ep->tx_time = expires;
-         ep->rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
-         if (is_cross_connect)
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
-               (vnet_buffer (b[3])->sw_if_index[VLIB_RX] ==
-                nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
-             ep->output_next_index =
-               (ep->tx_sw_if_index ==
-                nsm->sw_if_index0) ? nsm->
-               output_next_index0 : nsm->output_next_index1;
-           }
-         else                  /* output feature, even easier... */
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
-             ep->output_next_index =
-               nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
-           }
-         ep->buffer_index = from[3];
-         buffered++;
-       }
-
-      if (is_trace)
-       {
-         if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
-           {
-             nsim_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
-             t->expires = expires;
-             t->is_drop = is_drop[3];
-             t->is_lost = b[3]->error == loss_error;
-             t->tx_sw_if_index = (is_drop[3] == 0) ? ep->tx_sw_if_index : 0;
-           }
-       }
-
-      if (PREDICT_FALSE (is_drop[0]))
-       *drop++ = from[0];
-      if (PREDICT_FALSE (is_drop[1]))
-       *drop++ = from[1];
-      if (PREDICT_FALSE (is_drop[2]))
-       *drop++ = from[2];
-      if (PREDICT_FALSE (is_drop[3]))
-       *drop++ = from[3];
+      nsim_dispatch_buffer (vm, node, nsm, wp, b[0], from[0], &ctx,
+                           is_cross_connect, is_trace);
+      nsim_dispatch_buffer (vm, node, nsm, wp, b[1], from[1], &ctx,
+                           is_cross_connect, is_trace);
+      nsim_dispatch_buffer (vm, node, nsm, wp, b[2], from[2], &ctx,
+                           is_cross_connect, is_trace);
+      nsim_dispatch_buffer (vm, node, nsm, wp, b[3], from[3], &ctx,
+                           is_cross_connect, is_trace);
 
       b += 4;
-      next += 4;
       from += 4;
       n_left_from -= 4;
     }
@@ -336,99 +245,47 @@ slow_path:
 
   while (n_left_from > 0)
     {
-      next[0] = NSIM_NEXT_DROP;
-      is_drop[0] = 0;
-      if (PREDICT_TRUE (wp->cursize < wp->wheel_size))
-       {
-         if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
-           {
-             /* Get a random number on the closed interval [0,1] */
-             rnd[0] = random_f64 (&nsm->seed);
-             /* Drop the pkt? */
-             if (rnd[0] <= nsm->drop_fraction)
-               {
-                 b[0]->error = loss_error;
-                 is_drop[0] = 1;
-                 goto do_trace;
-               }
-           }
-
-         ep = wp->entries + wp->tail;
-         wp->tail++;
-         if (wp->tail == wp->wheel_size)
-           wp->tail = 0;
-         wp->cursize++;
-
-         ep->tx_time = expires;
-         ep->rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
-         if (is_cross_connect)
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
-               (vnet_buffer (b[0])->sw_if_index[VLIB_RX] ==
-                nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
-             ep->output_next_index =
-               (ep->tx_sw_if_index ==
-                nsm->sw_if_index0) ? nsm->
-               output_next_index0 : nsm->output_next_index1;
-           }
-         else                  /* output feature, even easier... */
-           {
-             ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
-             ep->output_next_index =
-               nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
-           }
-         ep->buffer_index = from[0];
-         buffered++;
-       }
-      else                     /* out of wheel space, drop pkt */
+      /* Drop if out of wheel space and not drop or reorder */
+      if (PREDICT_TRUE (wp->cursize < wp->wheel_size
+                       || (ctx.action[0] & NSIM_ACTION_DROP)
+                       || (ctx.action[0] & NSIM_ACTION_REORDER)))
        {
-         b[0]->error = no_buffer_error;
-         is_drop[0] = 1;
+         nsim_dispatch_buffer (vm, node, nsm, wp, b[0], from[0], &ctx,
+                               is_cross_connect, is_trace);
        }
-
-    do_trace:
-      if (is_trace)
+      else
        {
-         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_drop[0];
-             t->is_lost = b[0]->error == loss_error;
-             t->tx_sw_if_index = (is_drop[0] == 0) ? ep->tx_sw_if_index : 0;
-           }
+         ctx.drop[0] = from[0];
+         ctx.drop += 1;
+         if (PREDICT_FALSE (is_trace))
+           nsim_trace_buffer (vm, node, b[0], &ctx, 1);
+         ctx.action += 1;
        }
 
       b += 1;
-      next += 1;
-      if (PREDICT_FALSE (is_drop[0]))
-       {
-         drop[0] = from[0];
-         drop++;
-       }
-      from++;
+      from += 1;
       n_left_from -= 1;
     }
-  if (PREDICT_FALSE (drop > drops))
-    {
-      u32 n_left_to_drop = drop - drops;
-      drop = drops;
 
-      while (n_left_to_drop > 0)
-       {
-         u32 this_copy_size;
-         vlib_get_next_frame (vm, node, NSIM_NEXT_DROP, to_next,
-                              n_left_to_next);
-         this_copy_size = clib_min (n_left_to_drop, n_left_to_next);
-         clib_memcpy_fast (to_next, drop, this_copy_size * sizeof (u32));
-         n_left_to_next -= this_copy_size;
-         vlib_put_next_frame (vm, node, NSIM_NEXT_DROP, n_left_to_next);
-         drop += this_copy_size;
-         n_left_to_drop -= this_copy_size;
-       }
+  if (PREDICT_FALSE (ctx.drop > drops))
+    {
+      u32 n_left_to_drop = ctx.drop - drops;
+      vlib_buffer_free (vm, drops, n_left_to_drop);
+      vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_LOSS,
+                                  ctx.n_loss);
+      vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_DROPPED,
+                                  n_left_to_drop - ctx.n_loss);
+    }
+  if (PREDICT_FALSE (ctx.reord > reorders))
+    {
+      u32 n_reordered = ctx.reord - reorders;
+      vlib_buffer_enqueue_to_next (vm, node, reorders, reorders_nexts,
+                                  n_reordered);
+      vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_REORDERED,
+                                  n_reordered);
     }
   vlib_node_increment_counter (vm, node->node_index,
-                              NSIM_ERROR_BUFFERED, buffered);
+                              NSIM_ERROR_BUFFERED, ctx.n_buffered);
   return frame->n_vectors;
 }
 
index b9623a1..6549325 100644 (file)
@@ -3,7 +3,7 @@
  * @brief VPP control-plane API messages for the network delay simulator
  */
 
-option version = "2.1.1";
+option version = "2.2.1";
 import "vnet/interface_types.api";
 
 /** \brief enable / disable the network delay simulation cross-connect
@@ -61,6 +61,7 @@ autoreply define nsim_output_feature_enable_disable
 */
 autoreply define nsim_configure
 {
+  option deprecated="v21.01";
   /* Client identifier, set from api_main.my_client_index */
   u32 client_index;
   
@@ -75,4 +76,27 @@ autoreply define nsim_configure
   option vat_help = "delay <time> bandwidth <bw> [packet-size <nn>] [packets-per-drop <nnnn>]";
 };
 
+/** \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_configure2
+{
+  /* 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;
+  u32 packets_per_drop;
+  u32 packets_per_reorder;
+  option vat_help = "delay <time> bandwidth <bw> [packet-size <nn>] [packets-per-drop <nnnn>]";
+};
 
index c641490..00bf84e 100644 (file)
@@ -126,15 +126,35 @@ nsim_output_feature_enable_disable (nsim_main_t * nsm, u32 sw_if_index,
   return rv;
 }
 
+static nsim_wheel_t *
+nsim_wheel_alloc (nsim_main_t * nsm, u32 wheel_slots)
+{
+  u32 pagesize = getpagesize ();
+  nsim_wheel_t *wp;
+
+  nsm->mmap_size = sizeof (nsim_wheel_t)
+    + wheel_slots * 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->cursize = 0;
+  wp->head = 0;
+  wp->tail = 0;
+  wp->entries = (void *) (wp + 1);
+
+  return wp;
+}
+
 static int
 nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
-               f64 drop_fraction)
+               f64 drop_fraction, f64 reorder_fraction)
 {
-  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 ();
+  u64 total_buffer_size_in_bytes, per_worker_buffer_size, wheel_slots_per_wrk;
+  int i, num_workers = vlib_num_workers ();
   vlib_main_t *vm = nsm->vlib_main;
 
   if (bandwidth == 0.0)
@@ -146,19 +166,22 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   if (packet_size < 64.0 || packet_size > 9000.0)
     return VNET_API_ERROR_INVALID_VALUE_3;
 
+  if (reorder_fraction > 0.0 && delay == 0.0)
+    return VNET_API_ERROR_INVALID_VALUE_4;
+
   /* 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);
+         clib_mem_vm_free (nsm->wheel_by_thread[i], nsm->mmap_size);
          nsm->wheel_by_thread[i] = 0;
        }
     }
 
   nsm->delay = delay;
   nsm->drop_fraction = drop_fraction;
+  nsm->reorder_fraction = reorder_fraction;
 
   /* delay in seconds, bandwidth in bits/sec */
   total_buffer_size_in_bytes = ((delay * bandwidth) / 8.0) + 0.5;
@@ -172,8 +195,8 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   else
     per_worker_buffer_size = total_buffer_size_in_bytes;
 
-  wheel_slots_per_worker = per_worker_buffer_size / packet_size;
-  wheel_slots_per_worker++;
+  wheel_slots_per_wrk = per_worker_buffer_size / packet_size;
+  wheel_slots_per_wrk++;
 
   /* Save these for the show command */
   nsm->bandwidth = bandwidth;
@@ -184,24 +207,7 @@ nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
   /* Initialize the output scheduler wheels */
   i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
   for (; 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;
-    }
+    nsm->wheel_by_thread[i] = nsim_wheel_alloc (nsm, wheel_slots_per_wrk);
 
   vlib_worker_thread_barrier_sync (vm);
 
@@ -390,7 +396,7 @@ 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, drop_fraction;
+  f64 delay, bandwidth, packet_size, drop_fraction = 0.0, reorder_rate = 0.0;
   u32 packets_per_drop;
   int rv;
 
@@ -401,14 +407,40 @@ vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
   packets_per_drop = ntohl (mp->packets_per_drop);
   if (packets_per_drop > 0)
     drop_fraction = 1.0 / (f64) (packets_per_drop);
-  else
-    drop_fraction = 0.0;
 
-  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
+  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
+                      reorder_rate);
 
   REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
 }
 
+static void
+vl_api_nsim_configure2_t_handler (vl_api_nsim_configure2_t * mp)
+{
+  vl_api_nsim_configure_reply_t *rmp;
+  nsim_main_t *nsm = &nsim_main;
+  f64 delay, bandwidth, packet_size, drop_fraction = 0.0, reorder_rate = 0.0;
+  u32 packets_per_drop, packets_per_reorder;
+  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));
+
+  packets_per_drop = ntohl (mp->packets_per_drop);
+  if (packets_per_drop > 0)
+    drop_fraction = 1.0 / (f64) (packets_per_drop);
+
+  packets_per_reorder = ntohl (mp->packets_per_reorder);
+  if (packets_per_reorder > 0)
+    reorder_rate = 1.0 / (f64) packets_per_reorder;
+
+  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
+                      reorder_rate);
+
+  REPLY_MACRO (VL_API_NSIM_CONFIGURE2_REPLY);
+}
+
 
 /*
  * enable or disable the output_feature
@@ -511,7 +543,7 @@ nsim_init (vlib_main_t * vm)
 
   /* Ask for a correctly-sized block of API message decode slots */
   nsm->msg_id_base = setup_message_id_table ();
-
+  nsm->arc_index = nsm->vnet_main->interface_main.output_feature_arc_index;
   return 0;
 }
 
@@ -576,16 +608,69 @@ unformat_bandwidth (unformat_input_t * input, va_list * args)
   return 1;
 }
 
+static u8 *
+format_nsim_config (u8 * s, va_list * args)
+{
+  int verbose = va_arg (*args, int);
+  nsim_main_t *nsm = &nsim_main;
+
+  s = format (s, "configuration\n");
+  s = format (s, " delay (ms): %.2f\n", nsm->delay * 1e3);
+  if (nsm->drop_fraction)
+    s = format (s, " drop fraction: %.5f\n", nsm->drop_fraction);
+  else
+    s = format (s, " drop fraction: 0\n");
+  if (nsm->reorder_fraction)
+    s = format (s, " reorder fraction: %.5f\n", nsm->reorder_fraction);
+  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);
+
+  if (verbose)
+    {
+      s = format (s, " poll main thread: %u\n", nsm->poll_main_thread);
+      s = format (s, " memory: %U bytes per thread %U bytes total\n",
+                 format_memory_size, nsm->mmap_size, format_memory_size,
+                 nsm->mmap_size * vlib_num_workers ());
+    }
+
+  s = format (s, "\n");
+
+  if (nsm->sw_if_index0 != 0)
+    {
+      s = format (s, "cross-connect\n %U and %U\n",
+                 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);
+    }
+  else if (vec_len (nsm->output_next_index_by_sw_if_index))
+    {
+      int i;
+      s = format (s, "output feature arcs to:\n");
+      for (i = 0; i < vec_len (nsm->output_next_index_by_sw_if_index); i++)
+       {
+         if (nsm->output_next_index_by_sw_if_index[i] != ~0)
+           s = format (s, " %U %u\n", format_vnet_sw_if_index_name,
+                       nsm->vnet_main, i, i);
+       }
+    }
+  else
+    {
+      s = format (s, " nsim not enabled\n");
+    }
+
+  return s;
+}
+
 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;
   nsim_main_t *nsm = &nsim_main;
-  f64 delay, bandwidth;
-  f64 packet_size = 1500.0;
-  f64 drop_fraction = 0.0;
-  u32 packets_per_drop;
-  u32 num_workers = vlib_num_workers ();
   int rv;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
@@ -602,19 +687,32 @@ set_nsim_command_fn (vlib_main_t * vm,
          if (packets_per_drop > 0)
            drop_fraction = 1.0 / ((f64) packets_per_drop);
        }
+      else if (unformat (input, "packets-per-reorder %d",
+                        &packets_per_reorder))
+       {
+         if (packets_per_reorder > 0)
+           reorder_fraction = 1.0 / ((f64) packets_per_reorder);
+       }
       else if (unformat (input, "drop-fraction %f", &drop_fraction))
        {
          if (drop_fraction < 0.0 || drop_fraction > 1.0)
            return clib_error_return
              (0, "drop fraction must be between zero and 1");
        }
+      else if (unformat (input, "reorder-fraction %f", &reorder_fraction))
+       {
+         if (reorder_fraction < 0.0 || reorder_fraction > 1.0)
+           return clib_error_return
+             (0, "reorder fraction must be between zero and 1");
+       }
       else if (unformat (input, "poll-main-thread"))
        nsm->poll_main_thread = 1;
       else
        break;
     }
 
-  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
+  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
+                      reorder_fraction);
 
   switch (rv)
     {
@@ -627,6 +725,10 @@ set_nsim_command_fn (vlib_main_t * vm,
     case VNET_API_ERROR_INVALID_VALUE_3:
       return clib_error_return (0, "invalid packet size %.2f", packet_size);
 
+    case VNET_API_ERROR_INVALID_VALUE_4:
+      return clib_error_return (0, "invalid reorder fraction %.3f for "
+                               "delay %.2f", reorder_fraction, delay);
+
     default:
       return clib_error_return (0, "error %d", rv);
 
@@ -634,18 +736,7 @@ set_nsim_command_fn (vlib_main_t * vm,
       break;
     }
 
-  vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
-                  nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
-  if (nsm->drop_fraction > 0.0)
-    vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
-                    nsm->drop_fraction);
-
-
-  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);
+  vlib_cli_output (vm, "%U", format_nsim_config, 1);
 
   return 0;
 }
@@ -682,45 +773,15 @@ 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 (nsm->drop_fraction > 0.0)
-    vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
-                    nsm->drop_fraction);
-
-  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);
-    }
+  vlib_cli_output (vm, "%U", format_nsim_config, verbose);
 
   return 0;
 }
index 0ed9b25..7222a96 100644 (file)
@@ -45,11 +45,44 @@ typedef struct
     CLIB_CACHE_LINE_ALIGN_MARK (pad);
 } nsim_wheel_t;
 
+typedef struct nsim_node_ctx
+{
+  vnet_feature_config_main_t *fcm;
+  f64 expires;
+  u32 *drop;
+  u32 *reord;
+  u16 *reord_nexts;
+  u8 *action;
+  u64 n_buffered;
+  u64 n_loss;
+} nsim_node_ctx_t;
+
+#define foreach_nsm_action                     \
+  _(DROP, "Packet loss")                       \
+  _(REORDER, "Packet reorder")
+
+enum nsm_action_bit
+{
+#define _(sym, str) NSIM_ACTION_##sym##_BIT,
+  foreach_nsm_action
+#undef _
+};
+
+typedef enum nsm_action
+{
+#define _(sym, str) NSIM_ACTION_##sym = 1 << NSIM_ACTION_##sym##_BIT,
+  foreach_nsm_action
+#undef _
+} nsm_action_e;
+
 typedef struct
 {
   /* API message ID base */
   u16 msg_id_base;
 
+  /* output feature arc index */
+  u16 arc_index;
+
   /* Two interfaces, cross-connected with delay */
   u32 sw_if_index0, sw_if_index1;
   u32 output_next_index0, output_next_index1;
@@ -68,6 +101,7 @@ typedef struct
   f64 bandwidth;
   f64 packet_size;
   f64 drop_fraction;
+  f64 reorder_fraction;
   u32 poll_main_thread;
 
   u64 mmap_size;
index b155c92..68426d0 100644 (file)
@@ -214,6 +214,58 @@ api_nsim_configure (vat_main_t * vam)
   return ret;
 }
 
+static int
+api_nsim_configure2 (vat_main_t * vam)
+{
+  vl_api_nsim_configure2_t *mp;
+  unformat_input_t *i = vam->input;
+  f64 delay = 0.0, bandwidth = 0.0;
+  f64 packet_size = 1500.0;
+  u32 packets_per_drop = 0, packets_per_reorder;
+  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 if (unformat (i, "packets-per-drop %u", &packets_per_drop))
+       ;
+      else if (unformat (i, "packets-per-reorder %u", &packets_per_reorder))
+       ;
+      else
+       break;
+    }
+
+  if (delay == 0.0 || bandwidth == 0.0)
+    {
+      errmsg ("must specify delay and bandwidth");
+      return -99;
+    }
+
+  /* Construct the API message */
+  M (NSIM_CONFIGURE2, 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);
+  mp->packets_per_drop = ntohl (packets_per_drop);
+  mp->packets_per_reorder = ntohl (packets_per_reorder);
+
+  /* send it... */
+  S (mp);
+
+  /* Wait for a reply... */
+  W (ret);
+  return ret;
+}
+
 #include <nsim/nsim.api_test.c>
 
 /*
index dd206ce..df3806a 100644 (file)
@@ -157,6 +157,7 @@ _(LIMIT_EXCEEDED, -162, "limit exceeded")                               \
 _(IKE_NO_PORT, -163, "port not managed by IKE")                         \
 _(UDP_PORT_TAKEN, -164, "UDP port already taken")                       \
 _(EAGAIN, -165, "Retry stream call with cursor")                        \
+_(INVALID_VALUE_4, -166, "Invalid value #4")                                   \
 
 typedef enum
 {