FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
nsim.c
Go to the documentation of this file.
1 /*
2  * nsim.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /**
19  * @file
20  * @brief Network Delay Simulator
21  */
22 /*? %%clicmd:group_label Network Delay Simulator %% ?*/
23 
24 #include <vnet/vnet.h>
25 #include <vnet/plugin/plugin.h>
26 #include <nsim/nsim.h>
27 
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vpp/app/version.h>
31 
32 /* define message IDs */
33 #include <nsim/nsim_msg_enum.h>
34 
35 /* define message structures */
36 #define vl_typedefs
37 #include <nsim/nsim_all_api_h.h>
38 #undef vl_typedefs
39 
40 /* define generated endian-swappers */
41 #define vl_endianfun
42 #include <nsim/nsim_all_api_h.h>
43 #undef vl_endianfun
44 
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #define vl_printfun
48 #include <nsim/nsim_all_api_h.h>
49 #undef vl_printfun
50 
51 /* Get the API version number */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <nsim/nsim_all_api_h.h>
54 #undef vl_api_version
55 
56 #define REPLY_MSG_ID_BASE nsm->msg_id_base
58 
60 
61 /* List of message types that this plugin understands */
62 
63 #define foreach_nsim_plugin_api_msg \
64 _(NSIM_CROSS_CONNECT_ENABLE_DISABLE, nsim_cross_connect_enable_disable) \
65 _(NSIM_OUTPUT_FEATURE_ENABLE_DISABLE, nsim_output_feature_enable_disable) \
66 _(NSIM_CONFIGURE, nsim_configure)
67 
68 /* Action functions shared between message handlers and debug CLI */
69 
70 int
72  u32 sw_if_index1, int enable_disable)
73 {
76  int rv = 0;
77 
78  if (nsm->is_configured == 0)
79  return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
80 
81  /* Utterly wrong? */
83  sw_if_index0))
84  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
85 
87  sw_if_index1))
88  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
89 
90  /* Not a physical port? */
91  sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index0);
93  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
94 
95  sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index1);
97  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
98 
99  /* Add graph arcs for the input / wheel scraper node */
100  hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index0);
101  nsm->output_next_index0 =
104 
105  hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index1);
106  nsm->output_next_index1 =
109 
110  nsm->sw_if_index0 = sw_if_index0;
111  nsm->sw_if_index1 = sw_if_index1;
112 
113  vnet_feature_enable_disable ("device-input", "nsim",
114  sw_if_index0, enable_disable, 0, 0);
115  vnet_feature_enable_disable ("device-input", "nsim",
116  sw_if_index1, enable_disable, 0, 0);
117 
118  return rv;
119 }
120 
121 int
123  int enable_disable)
124 {
127  int rv = 0;
128 
129  if (nsm->is_configured == 0)
130  return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
131 
132  /* Utterly wrong? */
134  sw_if_index))
135  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
136 
137  /* Not a physical port? */
138  sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index);
140  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
141 
142  /* Add a graph arc for the input / wheel scraper node */
143  hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index);
145  ~0);
146  /* Note: use the tx node, this pkt has already visited the output node... */
149  hw->tx_node_index);
150 
151  vnet_feature_enable_disable ("interface-output", "nsim-output-feature",
152  sw_if_index, enable_disable, 0, 0);
153  return rv;
154 }
155 
156 static int
157 nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
158  f64 drop_fraction)
159 {
160  u64 total_buffer_size_in_bytes, per_worker_buffer_size;
161  u64 wheel_slots_per_worker;
162  int i;
163  int num_workers = vlib_num_workers ();
164  u32 pagesize = getpagesize ();
165  vlib_main_t *vm = nsm->vlib_main;
166 
167  if (bandwidth == 0.0)
168  return VNET_API_ERROR_INVALID_VALUE;
169 
170  if (delay == 0.0)
171  return VNET_API_ERROR_INVALID_VALUE_2;
172 
173  if (packet_size < 64.0 || packet_size > 9000.0)
174  return VNET_API_ERROR_INVALID_VALUE_3;
175 
176  /* Toss the old wheel(s)... */
177  if (nsm->is_configured)
178  {
179  for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
180  {
181  nsim_wheel_t *wp = nsm->wheel_by_thread[i];
182  munmap (wp, nsm->mmap_size);
183  nsm->wheel_by_thread[i] = 0;
184  }
185  }
186 
187  nsm->delay = delay;
188  nsm->drop_fraction = drop_fraction;
189 
190  /* delay in seconds, bandwidth in bits/sec */
191  total_buffer_size_in_bytes = ((delay * bandwidth) / 8.0) + 0.5;
192 
193  /*
194  * Work out how much buffering each worker needs, assuming decent
195  * RSS behavior.
196  */
197  if (num_workers)
198  per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
199  else
200  per_worker_buffer_size = total_buffer_size_in_bytes;
201 
202  wheel_slots_per_worker = per_worker_buffer_size / packet_size;
203  wheel_slots_per_worker++;
204 
205  /* Save these for the show command */
206  nsm->bandwidth = bandwidth;
207  nsm->packet_size = packet_size;
208 
209  vec_validate (nsm->wheel_by_thread, num_workers);
210 
211  /* Initialize the output scheduler wheels */
212  i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
213  for (; i < num_workers + 1; i++)
214  {
215  nsim_wheel_t *wp;
216 
217  nsm->mmap_size = sizeof (nsim_wheel_t)
218  + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
219 
220  nsm->mmap_size += pagesize - 1;
221  nsm->mmap_size &= ~(pagesize - 1);
222 
223  wp = clib_mem_vm_alloc (nsm->mmap_size);
224  ASSERT (wp != 0);
225  wp->wheel_size = wheel_slots_per_worker;
226  wp->cursize = 0;
227  wp->head = 0;
228  wp->tail = 0;
229  wp->entries = (void *) (wp + 1);
230  nsm->wheel_by_thread[i] = wp;
231  }
232 
234 
235  /* turn on the ring scrapers */
236  i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
237  for (; i < num_workers + 1; i++)
238  {
239  vlib_main_t *this_vm = vlib_mains[i];
240 
241  vlib_node_set_state (this_vm, nsim_input_node.index,
242  VLIB_NODE_STATE_POLLING);
243  }
244 
246 
247  nsm->is_configured = 1;
248  return 0;
249 }
250 
251 /*
252  * enable or disable the cross-connect
253  */
254 static clib_error_t *
256  unformat_input_t * input,
257  vlib_cli_command_t * cmd)
258 {
259  nsim_main_t *nsm = &nsim_main;
260  unformat_input_t _line_input, *line_input = &_line_input;
261  u32 sw_if_index0 = ~0;
262  u32 sw_if_index1 = ~0;
263  int enable_disable = 1;
264  u32 tmp;
265  int rv;
266 
267  /* Get a line of input. */
268  if (!unformat_user (input, unformat_line_input, line_input))
269  return 0;
270 
271  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
272  {
273  if (unformat (line_input, "disable"))
274  enable_disable = 0;
275  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
276  nsm->vnet_main, &tmp))
277  {
278  if (sw_if_index0 == ~0)
279  sw_if_index0 = tmp;
280  else
281  sw_if_index1 = tmp;
282  }
283  else
284  break;
285  }
286 
287  unformat_free (line_input);
288 
289  if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
290  return clib_error_return (0, "Please specify two interfaces...");
291 
292  rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0,
293  sw_if_index1, enable_disable);
294 
295  switch (rv)
296  {
297  case 0:
298  break;
299 
300  case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
301  return clib_error_return (0, "Not configured, please 'set nsim' first");
302 
303  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
304  return clib_error_return
305  (0, "Invalid interface, only works on physical ports");
306  break;
307 
308  case VNET_API_ERROR_UNIMPLEMENTED:
309  return clib_error_return (0,
310  "Device driver doesn't support redirection");
311  break;
312 
313  default:
314  return clib_error_return (0, "nsim_enable_disable returned %d", rv);
315  }
316  return 0;
317 }
318 
319 static clib_error_t *
321 {
322  nsim_main_t *nsm = &nsim_main;
323 
325  {
326  if (unformat (input, "poll-main-thread"))
327  {
328  nsm->poll_main_thread = 1;
329  }
330  else
331  {
332  return clib_error_return (0, "unknown input '%U'",
333  format_unformat_error, input);
334  }
335  }
336  return 0;
337 }
338 
340 
341 /*?
342  * Enable or disable network simulation cross-connect on two interfaces
343  * The network simulator must have already been configured, see
344  * the "nsim_configure" command.
345  *
346  * Place the interfaces into a bridge group, to ensure that
347  * interfaces are in promiscuous mode.
348  *
349  * @cliexpar
350  * To enable or disable network simulation cross-connect
351  * @clistart
352  * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
353  * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
354  * @cliend
355  * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
356 ?*/
357 /* *INDENT-OFF* */
358 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
359 {
360  .path = "nsim cross-connect enable-disable",
361  .short_help =
362  "nsim cross-connect enable-disable <interface-name-1> "
363  "<interface-name-2> [disable]",
365 };
366 /* *INDENT-ON* */
367 
368 /* API message handler */
371 {
372  vl_api_nsim_cross_connect_enable_disable_reply_t *rmp;
373  nsim_main_t *nsm = &nsim_main;
374  int rv;
375  u32 sw_if_index0, sw_if_index1;
376 
377  sw_if_index0 = clib_net_to_host_u32 (mp->sw_if_index0);
378  sw_if_index1 = clib_net_to_host_u32 (mp->sw_if_index1);
379 
380  if (!vnet_sw_if_index_is_api_valid (sw_if_index0))
381  {
382  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
383  goto bad_sw_if_index;
384  }
385  if (!vnet_sw_if_index_is_api_valid (sw_if_index1))
386  {
387  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
388  goto bad_sw_if_index;
389  }
390 
391  rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0, sw_if_index1,
392  (int) (mp->enable_disable));
393 
395  REPLY_MACRO (VL_API_NSIM_CROSS_CONNECT_ENABLE_DISABLE_REPLY);
396 }
397 
398 /* API message handler */
401 {
402  vl_api_nsim_output_feature_enable_disable_reply_t *rmp;
403  nsim_main_t *nsm = &nsim_main;
404  int rv;
406 
407  rv = nsim_output_feature_enable_disable (nsm, ntohl (mp->sw_if_index),
408  (int) (mp->enable_disable));
409 
411  REPLY_MACRO (VL_API_NSIM_OUTPUT_FEATURE_ENABLE_DISABLE_REPLY);
412 }
413 
414 /* API message handler */
415 static void
417 {
418  vl_api_nsim_configure_reply_t *rmp;
419  nsim_main_t *nsm = &nsim_main;
420  f64 delay, bandwidth, packet_size, drop_fraction;
421  u32 packets_per_drop;
422  int rv;
423 
424  delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
425  bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
426  packet_size = (f64) (ntohl (mp->average_packet_size));
427 
428  packets_per_drop = ntohl (mp->packets_per_drop);
429  if (packets_per_drop > 0)
430  drop_fraction = 1.0 / (f64) (packets_per_drop);
431  else
432  drop_fraction = 0.0;
433 
434  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
435 
436  REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
437 }
438 
439 
440 /*
441  * enable or disable the output_feature
442  */
443 static clib_error_t *
445  unformat_input_t * input,
446  vlib_cli_command_t * cmd)
447 {
448  nsim_main_t *nsm = &nsim_main;
449  unformat_input_t _line_input, *line_input = &_line_input;
450  u32 sw_if_index = ~0;
451  int enable_disable = 1;
452  int rv;
453 
454  /* Get a line of input. */
455  if (!unformat_user (input, unformat_line_input, line_input))
456  return 0;
457 
458  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
459  {
460  if (unformat (line_input, "disable"))
461  enable_disable = 0;
462  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
463  nsm->vnet_main, &sw_if_index))
464  ;
465  else
466  {
467  clib_error_t *error = clib_error_return (0, "unknown input `%U'",
469  line_input);
470  unformat_free (line_input);
471  return error;
472  }
473  }
474 
475  unformat_free (line_input);
476 
477  if (sw_if_index == ~0)
478  return clib_error_return (0, "Please specify one interface...");
479 
480  rv = nsim_output_feature_enable_disable (nsm, sw_if_index, enable_disable);
481 
482  switch (rv)
483  {
484  case 0:
485  break;
486 
487  case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
488  return clib_error_return (0, "Not configured, please 'set nsim' first");
489 
490  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
491  return clib_error_return
492  (0, "Invalid interface, only works on physical ports");
493  break;
494 
495  case VNET_API_ERROR_UNIMPLEMENTED:
496  return clib_error_return (0,
497  "Device driver doesn't support redirection");
498  break;
499 
500  default:
501  return clib_error_return
502  (0, "nsim_output_feature_enable_disable returned %d", rv);
503  }
504  return 0;
505 }
506 
507 /*?
508  * Enable or disable network simulation output feature on an interface
509  * The network simulator must have already been configured, see
510  * the "nsim_configure" command.
511  *
512  * @cliexpar
513  * To enable or disable network simulation output feature
514  * @clistart
515  * nsim output-feature enable-disable TenGigabitEthernet2/0/0
516  * nsim output-feature enable-disable TenGigabitEthernet2/0/0 disable
517  * @cliend
518  * @cliexcmd{nsim output-feature enable-disable <intfc> [disable]}
519 ?*/
520 /* *INDENT-OFF* */
521 VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
522 {
523  .path = "nsim output-feature enable-disable",
524  .short_help =
525  "nsim output-feature enable-disable <interface-name> [disable]",
527 };
528 /* *INDENT-ON* */
529 
530 /* Set up the API message handling tables */
531 static clib_error_t *
533 {
534  nsim_main_t *nsm = &nsim_main;
535 #define _(N,n) \
536  vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base), \
537  #n, \
538  vl_api_##n##_t_handler, \
539  vl_noop_handler, \
540  vl_api_##n##_t_endian, \
541  vl_api_##n##_t_print, \
542  sizeof(vl_api_##n##_t), 1);
544 #undef _
545 
546  return 0;
547 }
548 
549 #define vl_msg_name_crc_list
550 #include <nsim/nsim_all_api_h.h>
551 #undef vl_msg_name_crc_list
552 
553 static void
555 {
556 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nsm->msg_id_base);
557  foreach_vl_msg_name_crc_nsim;
558 #undef _
559 }
560 
561 static clib_error_t *
563 {
564  nsim_main_t *nsm = &nsim_main;
565  clib_error_t *error = 0;
566  u8 *name;
567 
568  nsm->vlib_main = vm;
569  nsm->vnet_main = vnet_get_main ();
570 
571  name = format (0, "nsim_%08x%c", api_version, 0);
572 
573  /* Ask for a correctly-sized block of API message decode slots */
575  ((char *) name, VL_MSG_FIRST_AVAILABLE);
576 
577  error = nsim_plugin_api_hookup (vm);
578 
579  /* Add our API messages to the global name_crc hash table */
581 
582  vec_free (name);
583 
584  return error;
585 }
586 
588 
589 /* *INDENT-OFF* */
590 VNET_FEATURE_INIT (nsim, static) =
591 {
592  .arc_name = "device-input",
593  .node_name = "nsim",
594  .runs_before = VNET_FEATURES ("ethernet-input"),
595 };
596 /* *INDENT-ON */
597 
598 /* *INDENT-OFF* */
599 VNET_FEATURE_INIT (nsim_feature, static) =
600 {
601  .arc_name = "interface-output",
602  .node_name = "nsim-output-feature",
603  .runs_before = VNET_FEATURES ("interface-tx"),
604 };
605 /* *INDENT-ON */
606 
607 /* *INDENT-OFF* */
609 {
610  .version = VPP_BUILD_VER,
611  .description = "Network Delay Simulator",
612 };
613 /* *INDENT-ON* */
614 
615 static uword
616 unformat_delay (unformat_input_t * input, va_list * args)
617 {
618  f64 *result = va_arg (*args, f64 *);
619  f64 tmp;
620 
621  if (unformat (input, "%f us", &tmp))
622  *result = tmp * 1e-6;
623  else if (unformat (input, "%f ms", &tmp))
624  *result = tmp * 1e-3;
625  else if (unformat (input, "%f sec", &tmp))
626  *result = tmp;
627  else
628  return 0;
629 
630  return 1;
631 }
632 
633 static uword
634 unformat_bandwidth (unformat_input_t * input, va_list * args)
635 {
636  f64 *result = va_arg (*args, f64 *);
637  f64 tmp;
638 
639  if (unformat (input, "%f gbit", &tmp))
640  *result = tmp * 1e9;
641  else if (unformat (input, "%f gbyte", &tmp))
642  *result = tmp * 8e9;
643  else
644  return 0;
645  return 1;
646 }
647 
648 static clib_error_t *
650  unformat_input_t * input, vlib_cli_command_t * cmd)
651 {
652  nsim_main_t *nsm = &nsim_main;
653  f64 delay, bandwidth;
654  f64 packet_size = 1500.0;
655  f64 drop_fraction = 0.0;
656  u32 packets_per_drop;
657  u32 num_workers = vlib_num_workers ();
658  int rv;
659 
661  {
662  if (unformat (input, "delay %U", unformat_delay, &delay))
663  ;
664  else if (unformat (input, "bandwidth %U", unformat_bandwidth,
665  &bandwidth))
666  ;
667  else if (unformat (input, "packet-size %f", &packet_size))
668  ;
669  else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
670  {
671  if (packets_per_drop > 0)
672  drop_fraction = 1.0 / ((f64) packets_per_drop);
673  }
674  else if (unformat (input, "drop-fraction %f", &drop_fraction))
675  {
676  if (drop_fraction < 0.0 || drop_fraction > 1.0)
677  return clib_error_return
678  (0, "drop fraction must be between zero and 1");
679  }
680  else if (unformat (input, "poll-main-thread"))
681  nsm->poll_main_thread = 1;
682  else
683  break;
684  }
685 
686  rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
687 
688  switch (rv)
689  {
690  case VNET_API_ERROR_INVALID_VALUE:
691  return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
692 
693  case VNET_API_ERROR_INVALID_VALUE_2:
694  return clib_error_return (0, "invalid delay %.2f", delay);
695 
696  case VNET_API_ERROR_INVALID_VALUE_3:
697  return clib_error_return (0, "invalid packet size %.2f", packet_size);
698 
699  default:
700  return clib_error_return (0, "error %d", rv);
701 
702  case 0:
703  break;
704  }
705 
706  vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
707  nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
708  if (nsm->drop_fraction > 0.0)
709  vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
710  nsm->drop_fraction);
711 
712 
713  if (num_workers)
714  vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
715  nsm->mmap_size, nsm->mmap_size * num_workers);
716  else
717  vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
718 
719  return 0;
720 }
721 
722 /*?
723  * Configure the network simulation cross-connect
724  * Once the simulator is configured, use the "nsim enable-disable" command
725  * to set up a cross-connect with the supplied delay characteristics.
726  *
727  * The cross connect configuration may be changed without restarting vpp
728  * but it is good practice to shut down the interfaces.
729  *
730  * @cliexpar
731  * To configure the network delay simulator:
732  * @clistart
733  * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
734  *
735  * @cliend
736  * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
737 ?*/
738 /* *INDENT-OFF* */
739 VLIB_CLI_COMMAND (set_nsim_command, static) =
740 {
741  .path = "set nsim",
742  .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>\n"
743  " [packets-per-drop <nn>][drop-fraction <f64: 0.0 - 1.0>]",
744  .function = set_nsim_command_fn,
745 };
746 /* *INDENT-ON*/
747 
748 
749 static clib_error_t *
751  unformat_input_t * input, vlib_cli_command_t * cmd)
752 {
753  nsim_main_t *nsm = &nsim_main;
754  u32 num_workers = vlib_num_workers ();
755  int verbose = 0;
756 
757  if (nsm->is_configured == 0)
758  return clib_error_return (0, "Network simulator not configured");
759 
760  if (nsm->sw_if_index0 == 0)
761  return clib_error_return (0, "Network simulator not enabled");
762 
763  if (unformat (input, "verbose"))
764  verbose = 1;
765 
766  vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
768  nsm->vnet_main, nsm->sw_if_index0,
770  nsm->vnet_main, nsm->sw_if_index1);
771 
772  vlib_cli_output (vm,
773  "...inserting link delay of %.2f ms, %.2f ms round-trip",
774  nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
775 
776  if (nsm->drop_fraction > 0.0)
777  vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
778  nsm->drop_fraction);
779 
780  if (verbose)
781  {
782 
783  vlib_cli_output (vm, " Configured bandwidth: %.2f gbit/sec",
784  nsm->bandwidth / 1e9);
785  vlib_cli_output (vm, " Configured packet size: %f", nsm->packet_size);
786  if (num_workers)
788  (vm, " Sim uses %llu bytes per thread, %llu bytes total",
789  nsm->mmap_size, nsm->mmap_size * num_workers);
790  else
791  vlib_cli_output (vm, " Sim uses %llu bytes total", nsm->mmap_size);
792  }
793 
794  return 0;
795 }
796 
797 /*?
798  * Display state info for the network delay simulator.
799  *
800  * @cliexpar
801  * To display the state of the network simulator
802  * @clistart
803  * show nsim verbose
804  * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
805  * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
806  * Configured bandwidth: 10.10 gbit/sec
807  * Configured packet size: 128
808  * Sim uses 157814784 bytes total
809  * @cliend
810  * @cliexcmd{show nsim}
811 ?*/
812 
813 /* *INDENT-OFF* */
814 VLIB_CLI_COMMAND (show_nsim_command, static) =
815 {
816  .path = "show nsim",
817  .short_help = "Display network delay simulator configuration",
818  .function = show_nsim_command_fn,
819 };
820 /* *INDENT-ON* */
821 
822 /*
823  * fd.io coding-style-patch-verification: ON
824  *
825  * Local Variables:
826  * eval: (c-set-style "gnu")
827  * End:
828  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 * output_next_index_by_sw_if_index
Definition: nsim.h:58
f64 packet_size
Definition: nsim.h:69
static clib_error_t * nsim_init(vlib_main_t *vm)
Definition: nsim.c:562
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
u16 msg_id_base
Definition: nsim.h:51
unsigned long u64
Definition: types.h:89
Definition: nsim.h:28
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
int i
enable / disable the network delay simulation cross-connect
Definition: nsim.api:15
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
configure the network delay simulation cross-connect
Definition: nsim.api:59
int nsim_cross_connect_enable_disable(nsim_main_t *nsm, u32 sw_if_index0, u32 sw_if_index1, int enable_disable)
Definition: nsim.c:71
u32 output_next_index0
Definition: nsim.h:55
vlib_main_t ** vlib_mains
Definition: buffer.c:332
format_function_t format_vnet_sw_if_index_name
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
u32 cursize
Definition: nsim.h:41
enable / disable the network delay simulation output feature
Definition: nsim.api:37
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vnet_main_t * vnet_main
Definition: nsim.h:80
int is_configured
Definition: nsim.h:76
static void vl_api_nsim_output_feature_enable_disable_t_handler(vl_api_nsim_output_feature_enable_disable_t *mp)
Definition: nsim.c:400
#define clib_error_return(e, args...)
Definition: error.h:99
static uword unformat_delay(unformat_input_t *input, va_list *args)
Definition: nsim.c:616
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_line_input
Definition: format.h:283
u64 mmap_size
Definition: nsim.h:73
struct _unformat_input_t unformat_input_t
VLIB_PLUGIN_REGISTER()
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:182
#define REPLY_MACRO(t)
u32 tail
Definition: nsim.h:43
static void vl_api_nsim_cross_connect_enable_disable_t_handler(vl_api_nsim_cross_connect_enable_disable_t *mp)
Definition: nsim.c:370
u8 name[64]
Definition: memclnt.api:152
static int nsim_configure(nsim_main_t *nsm, f64 bandwidth, f64 delay, f64 packet_size, f64 drop_fraction)
Definition: nsim.c:157
VNET_FEATURE_INIT(nsim, static)
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
u32 sw_if_index1
Definition: nsim.h:54
#define BAD_SW_IF_INDEX_LABEL
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
static void vl_api_nsim_configure_t_handler(vl_api_nsim_configure_t *mp)
Definition: nsim.c:416
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
vlib_main_t * vm
Definition: buffer.c:323
static clib_error_t * nsim_output_feature_enable_disable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nsim.c:444
static clib_error_t * nsim_config(vlib_main_t *vm, unformat_input_t *input)
Definition: nsim.c:320
vlib_node_registration_t nsim_input_node
(constructor) VLIB_REGISTER_NODE (nsim_input_node)
Definition: nsim_input.c:168
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
nsim_wheel_entry_t * entries
Definition: nsim.h:44
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
#define foreach_nsim_plugin_api_msg
Definition: nsim.c:63
u32 head
Definition: nsim.h:42
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
vlib_main_t * vlib_main
Definition: nsim.h:79
static clib_error_t * nsim_cross_connect_enable_disable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nsim.c:255
#define ASSERT(truth)
int nsim_output_feature_enable_disable(nsim_main_t *nsm, u32 sw_if_index, int enable_disable)
Definition: nsim.c:122
u64 bandwidth_in_bits_per_second
Definition: nsim.api:70
nsim_wheel_t ** wheel_by_thread
Definition: nsim.h:64
u32 poll_main_thread
Definition: nsim.h:71
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:148
#define VNET_FEATURES(...)
Definition: feature.h:442
u32 sw_if_index0
Definition: nsim.h:54
f64 bandwidth
Definition: nsim.h:68
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:837
static uword unformat_bandwidth(unformat_input_t *input, va_list *args)
Definition: nsim.c:634
f64 delay
Definition: nsim.h:67
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1520
vnet_sw_interface_type_t type
Definition: interface.h:701
static u32 vlib_num_workers()
Definition: threads.h:372
static clib_error_t * show_nsim_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nsim.c:750
f64 drop_fraction
Definition: nsim.h:70
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:486
static void * clib_mem_vm_alloc(uword size)
Definition: mem.h:339
u32 wheel_size
Definition: nsim.h:40
api_main_t api_main
Definition: api_shared.c:35
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
u32 output_next_index1
Definition: nsim.h:55
nsim_main_t nsim_main
Definition: nsim.c:59
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:275
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
#define VALIDATE_SW_IF_INDEX(mp)
static clib_error_t * set_nsim_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nsim.c:649
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957
static void setup_message_id_table(nsim_main_t *nsm, api_main_t *am)
Definition: nsim.c:554
static clib_error_t * nsim_plugin_api_hookup(vlib_main_t *vm)
Definition: nsim.c:532