2 * nsim.c - skeleton vpp engine plug-in
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief Network Delay Simulator
22 /*? %%clicmd:group_label Network Delay Simulator %% ?*/
24 #include <vnet/vnet.h>
25 #include <vnet/plugin/plugin.h>
26 #include <nsim/nsim.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vpp/app/version.h>
32 /* define message IDs */
33 #include <nsim/nsim_msg_enum.h>
35 /* define message structures */
37 #include <nsim/nsim_all_api_h.h>
40 /* define generated endian-swappers */
42 #include <nsim/nsim_all_api_h.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 #include <nsim/nsim_all_api_h.h>
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>
56 #define REPLY_MSG_ID_BASE nsm->msg_id_base
57 #include <vlibapi/api_helper_macros.h>
59 nsim_main_t nsim_main;
61 /* List of message types that this plugin understands */
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)
68 /* Action functions shared between message handlers and debug CLI */
71 nsim_cross_connect_enable_disable (nsim_main_t * nsm, u32 sw_if_index0,
72 u32 sw_if_index1, int enable_disable)
74 vnet_sw_interface_t *sw;
75 vnet_hw_interface_t *hw;
78 if (nsm->is_configured == 0)
79 return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
82 if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
84 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
86 if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
88 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
90 /* Not a physical port? */
91 sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index0);
92 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
93 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
95 sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index1);
96 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
97 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
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 =
102 vlib_node_add_next (nsm->vlib_main,
103 nsim_input_node.index, hw->output_node_index);
105 hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index1);
106 nsm->output_next_index1 =
107 vlib_node_add_next (nsm->vlib_main,
108 nsim_input_node.index, hw->output_node_index);
110 nsm->sw_if_index0 = sw_if_index0;
111 nsm->sw_if_index1 = sw_if_index1;
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);
122 nsim_output_feature_enable_disable (nsim_main_t * nsm, u32 sw_if_index,
125 vnet_sw_interface_t *sw;
126 vnet_hw_interface_t *hw;
129 if (nsm->is_configured == 0)
130 return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
133 if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
135 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
137 /* Not a physical port? */
138 sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index);
139 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
140 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
142 /* Add a graph arc for the input / wheel scraper node */
143 hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index);
144 vec_validate_init_empty (nsm->output_next_index_by_sw_if_index, sw_if_index,
146 /* Note: use the tx node, this pkt has already visited the output node... */
147 nsm->output_next_index_by_sw_if_index[sw_if_index] =
148 vlib_node_add_next (nsm->vlib_main, nsim_input_node.index,
151 vnet_feature_enable_disable ("interface-output", "nsim-output-feature",
152 sw_if_index, enable_disable, 0, 0);
157 nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
160 u64 total_buffer_size_in_bytes, per_worker_buffer_size;
161 u64 wheel_slots_per_worker;
163 int num_workers = vlib_num_workers ();
164 u32 pagesize = getpagesize ();
165 vlib_main_t *vm = nsm->vlib_main;
167 if (bandwidth == 0.0)
168 return VNET_API_ERROR_INVALID_VALUE;
171 return VNET_API_ERROR_INVALID_VALUE_2;
173 if (packet_size < 64.0 || packet_size > 9000.0)
174 return VNET_API_ERROR_INVALID_VALUE_3;
176 /* Toss the old wheel(s)... */
177 if (nsm->is_configured)
179 for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
181 nsim_wheel_t *wp = nsm->wheel_by_thread[i];
182 munmap (wp, nsm->mmap_size);
183 nsm->wheel_by_thread[i] = 0;
188 nsm->drop_fraction = drop_fraction;
190 /* delay in seconds, bandwidth in bits/sec */
191 total_buffer_size_in_bytes = (u32) ((delay * bandwidth) / 8.0) + 0.5;
194 * Work out how much buffering each worker needs, assuming decent
198 per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
200 per_worker_buffer_size = total_buffer_size_in_bytes;
202 wheel_slots_per_worker = per_worker_buffer_size / packet_size;
203 wheel_slots_per_worker++;
205 /* Save these for the show command */
206 nsm->bandwidth = bandwidth;
207 nsm->packet_size = packet_size;
209 vec_validate (nsm->wheel_by_thread, num_workers);
211 /* Initialize the output scheduler wheels */
212 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
216 nsm->mmap_size = sizeof (nsim_wheel_t)
217 + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
219 nsm->mmap_size += pagesize - 1;
220 nsm->mmap_size &= ~(pagesize - 1);
222 wp = clib_mem_vm_alloc (nsm->mmap_size);
224 wp->wheel_size = wheel_slots_per_worker;
228 wp->entries = (void *) (wp + 1);
229 nsm->wheel_by_thread[i] = wp;
232 vlib_worker_thread_barrier_sync (vm);
234 /* turn on the ring scrapers */
235 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
237 vlib_main_t *this_vm = vlib_mains[i];
239 vlib_node_set_state (this_vm, nsim_input_node.index,
240 VLIB_NODE_STATE_POLLING);
243 vlib_worker_thread_barrier_release (vm);
245 nsm->is_configured = 1;
250 * enable or disable the cross-connect
252 static clib_error_t *
253 nsim_cross_connect_enable_disable_command_fn (vlib_main_t * vm,
254 unformat_input_t * input,
255 vlib_cli_command_t * cmd)
257 nsim_main_t *nsm = &nsim_main;
258 unformat_input_t _line_input, *line_input = &_line_input;
259 u32 sw_if_index0 = ~0;
260 u32 sw_if_index1 = ~0;
261 int enable_disable = 1;
265 /* Get a line of input. */
266 if (!unformat_user (input, unformat_line_input, line_input))
269 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
271 if (unformat (line_input, "disable"))
273 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
274 nsm->vnet_main, &tmp))
276 if (sw_if_index0 == ~0)
285 unformat_free (line_input);
287 if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
288 return clib_error_return (0, "Please specify two interfaces...");
290 rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0,
291 sw_if_index1, enable_disable);
298 case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
299 return clib_error_return (0, "Not configured, please 'set nsim' first");
301 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
302 return clib_error_return
303 (0, "Invalid interface, only works on physical ports");
306 case VNET_API_ERROR_UNIMPLEMENTED:
307 return clib_error_return (0,
308 "Device driver doesn't support redirection");
312 return clib_error_return (0, "nsim_enable_disable returned %d", rv);
318 * Enable or disable network simulation cross-connect on two interfaces
319 * The network simulator must have already been configured, see
320 * the "nsim_configure" command.
322 * Place the interfaces into a bridge group, to ensure that
323 * interfaces are in promiscuous mode.
326 * To enable or disable network simulation cross-connect
328 * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
329 * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
331 * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
334 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
336 .path = "nsim cross-connect enable-disable",
338 "nsim cross-connect enable-disable <interface-name-1> "
339 "<interface-name-2> [disable]",
340 .function = nsim_cross_connect_enable_disable_command_fn,
344 /* API message handler */
345 static void vl_api_nsim_cross_connect_enable_disable_t_handler
346 (vl_api_nsim_cross_connect_enable_disable_t * mp)
348 vl_api_nsim_cross_connect_enable_disable_reply_t *rmp;
349 nsim_main_t *nsm = &nsim_main;
351 u32 sw_if_index0, sw_if_index1;
353 sw_if_index0 = clib_net_to_host_u32 (mp->sw_if_index0);
354 sw_if_index1 = clib_net_to_host_u32 (mp->sw_if_index1);
356 if (!vnet_sw_if_index_is_api_valid (sw_if_index0))
358 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
359 goto bad_sw_if_index;
361 if (!vnet_sw_if_index_is_api_valid (sw_if_index1))
363 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
364 goto bad_sw_if_index;
367 rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0, sw_if_index1,
368 (int) (mp->enable_disable));
370 BAD_SW_IF_INDEX_LABEL;
371 REPLY_MACRO (VL_API_NSIM_CROSS_CONNECT_ENABLE_DISABLE_REPLY);
374 /* API message handler */
375 static void vl_api_nsim_output_feature_enable_disable_t_handler
376 (vl_api_nsim_output_feature_enable_disable_t * mp)
378 vl_api_nsim_output_feature_enable_disable_reply_t *rmp;
379 nsim_main_t *nsm = &nsim_main;
381 VALIDATE_SW_IF_INDEX (mp);
383 rv = nsim_output_feature_enable_disable (nsm, ntohl (mp->sw_if_index),
384 (int) (mp->enable_disable));
386 BAD_SW_IF_INDEX_LABEL;
387 REPLY_MACRO (VL_API_NSIM_OUTPUT_FEATURE_ENABLE_DISABLE_REPLY);
390 /* API message handler */
392 vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
394 vl_api_nsim_configure_reply_t *rmp;
395 nsim_main_t *nsm = &nsim_main;
396 f64 delay, bandwidth, packet_size, drop_fraction;
397 u32 packets_per_drop;
400 delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
401 bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
402 packet_size = (f64) (ntohl (mp->average_packet_size));
404 packets_per_drop = ntohl (mp->packets_per_drop);
405 if (packets_per_drop > 0)
406 drop_fraction = 1.0 / (f64) (packets_per_drop);
410 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
412 REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
417 * enable or disable the output_feature
419 static clib_error_t *
420 nsim_output_feature_enable_disable_command_fn (vlib_main_t * vm,
421 unformat_input_t * input,
422 vlib_cli_command_t * cmd)
424 nsim_main_t *nsm = &nsim_main;
425 unformat_input_t _line_input, *line_input = &_line_input;
426 u32 sw_if_index = ~0;
427 int enable_disable = 1;
430 /* Get a line of input. */
431 if (!unformat_user (input, unformat_line_input, line_input))
434 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
436 if (unformat (line_input, "disable"))
438 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
439 nsm->vnet_main, &sw_if_index))
443 clib_error_t *error = clib_error_return (0, "unknown input `%U'",
444 format_unformat_error,
446 unformat_free (line_input);
451 unformat_free (line_input);
453 if (sw_if_index == ~0)
454 return clib_error_return (0, "Please specify one interface...");
456 rv = nsim_output_feature_enable_disable (nsm, sw_if_index, enable_disable);
463 case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
464 return clib_error_return (0, "Not configured, please 'set nsim' first");
466 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
467 return clib_error_return
468 (0, "Invalid interface, only works on physical ports");
471 case VNET_API_ERROR_UNIMPLEMENTED:
472 return clib_error_return (0,
473 "Device driver doesn't support redirection");
477 return clib_error_return
478 (0, "nsim_output_feature_enable_disable returned %d", rv);
484 * Enable or disable network simulation output feature on an interface
485 * The network simulator must have already been configured, see
486 * the "nsim_configure" command.
489 * To enable or disable network simulation output feature
491 * nsim output-feature enable-disable TenGigabitEthernet2/0/0
492 * nsim output-feature enable-disable TenGigabitEthernet2/0/0 disable
494 * @cliexcmd{nsim output-feature enable-disable <intfc> [disable]}
497 VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
499 .path = "nsim output-feature enable-disable",
501 "nsim output-feature enable-disable <interface-name> [disable]",
502 .function = nsim_output_feature_enable_disable_command_fn,
506 /* Set up the API message handling tables */
507 static clib_error_t *
508 nsim_plugin_api_hookup (vlib_main_t * vm)
510 nsim_main_t *nsm = &nsim_main;
512 vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base), \
514 vl_api_##n##_t_handler, \
516 vl_api_##n##_t_endian, \
517 vl_api_##n##_t_print, \
518 sizeof(vl_api_##n##_t), 1);
519 foreach_nsim_plugin_api_msg;
525 #define vl_msg_name_crc_list
526 #include <nsim/nsim_all_api_h.h>
527 #undef vl_msg_name_crc_list
530 setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
532 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nsm->msg_id_base);
533 foreach_vl_msg_name_crc_nsim;
537 static clib_error_t *
538 nsim_init (vlib_main_t * vm)
540 nsim_main_t *nsm = &nsim_main;
541 clib_error_t *error = 0;
545 nsm->vnet_main = vnet_get_main ();
547 name = format (0, "nsim_%08x%c", api_version, 0);
549 /* Ask for a correctly-sized block of API message decode slots */
550 nsm->msg_id_base = vl_msg_api_get_msg_ids
551 ((char *) name, VL_MSG_FIRST_AVAILABLE);
553 error = nsim_plugin_api_hookup (vm);
555 /* Add our API messages to the global name_crc hash table */
556 setup_message_id_table (nsm, &api_main);
563 VLIB_INIT_FUNCTION (nsim_init);
566 VNET_FEATURE_INIT (nsim, static) =
568 .arc_name = "device-input",
570 .runs_before = VNET_FEATURES ("ethernet-input"),
575 VNET_FEATURE_INIT (nsim_feature, static) =
577 .arc_name = "interface-output",
578 .node_name = "nsim-output-feature",
579 .runs_before = VNET_FEATURES ("interface-tx"),
584 VLIB_PLUGIN_REGISTER () =
586 .version = VPP_BUILD_VER,
587 .description = "network delay simulator plugin",
592 unformat_delay (unformat_input_t * input, va_list * args)
594 f64 *result = va_arg (*args, f64 *);
597 if (unformat (input, "%f us", &tmp))
598 *result = tmp * 1e-6;
599 else if (unformat (input, "%f ms", &tmp))
600 *result = tmp * 1e-3;
601 else if (unformat (input, "%f sec", &tmp))
610 unformat_bandwidth (unformat_input_t * input, va_list * args)
612 f64 *result = va_arg (*args, f64 *);
615 if (unformat (input, "%f gbit", &tmp))
617 else if (unformat (input, "%f gbyte", &tmp))
624 static clib_error_t *
625 set_nsim_command_fn (vlib_main_t * vm,
626 unformat_input_t * input, vlib_cli_command_t * cmd)
628 nsim_main_t *nsm = &nsim_main;
629 f64 delay, bandwidth;
630 f64 packet_size = 1500.0;
631 f64 drop_fraction = 0.0;
632 u32 packets_per_drop;
633 u32 num_workers = vlib_num_workers ();
636 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
638 if (unformat (input, "delay %U", unformat_delay, &delay))
640 else if (unformat (input, "bandwidth %U", unformat_bandwidth,
643 else if (unformat (input, "packet-size %f", &packet_size))
645 else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
647 if (packets_per_drop > 0)
648 drop_fraction = 1.0 / ((f64) packets_per_drop);
650 else if (unformat (input, "drop-fraction %f", &drop_fraction))
652 if (drop_fraction < 0.0 || drop_fraction > 1.0)
653 return clib_error_return
654 (0, "drop fraction must be between zero and 1");
660 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
664 case VNET_API_ERROR_INVALID_VALUE:
665 return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
667 case VNET_API_ERROR_INVALID_VALUE_2:
668 return clib_error_return (0, "invalid delay %.2f", delay);
670 case VNET_API_ERROR_INVALID_VALUE_3:
671 return clib_error_return (0, "invalid packet size %.2f", packet_size);
674 return clib_error_return (0, "error %d", rv);
680 vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
681 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
682 if (nsm->drop_fraction > 0.0)
683 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
688 vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
689 nsm->mmap_size, nsm->mmap_size * num_workers);
691 vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
697 * Configure the network simulation cross-connect
698 * Once the simulator is configured, use the "nsim enable-disable" command
699 * to set up a cross-connect with the supplied delay characteristics.
701 * The cross connect configuration may be changed without restarting vpp
702 * but it is good practice to shut down the interfaces.
705 * To configure the network delay simulator:
707 * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
710 * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
713 VLIB_CLI_COMMAND (set_nsim_command, static) =
716 .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>\n"
717 " [packets-per-drop <nn>][drop-fraction <f64: 0.0 - 1.0>]",
718 .function = set_nsim_command_fn,
723 static clib_error_t *
724 show_nsim_command_fn (vlib_main_t * vm,
725 unformat_input_t * input, vlib_cli_command_t * cmd)
727 nsim_main_t *nsm = &nsim_main;
728 u32 num_workers = vlib_num_workers ();
731 if (nsm->is_configured == 0)
732 return clib_error_return (0, "Network simulator not configured");
734 if (nsm->sw_if_index0 == 0)
735 return clib_error_return (0, "Network simulator not enabled");
737 if (unformat (input, "verbose"))
740 vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
741 format_vnet_sw_if_index_name,
742 nsm->vnet_main, nsm->sw_if_index0,
743 format_vnet_sw_if_index_name,
744 nsm->vnet_main, nsm->sw_if_index1);
747 "...inserting link delay of %.2f ms, %.2f ms round-trip",
748 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
750 if (nsm->drop_fraction > 0.0)
751 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
757 vlib_cli_output (vm, " Configured bandwidth: %.2f gbit/sec",
758 nsm->bandwidth / 1e9);
759 vlib_cli_output (vm, " Configured packet size: %f", nsm->packet_size);
762 (vm, " Sim uses %llu bytes per thread, %llu bytes total",
763 nsm->mmap_size, nsm->mmap_size * num_workers);
765 vlib_cli_output (vm, " Sim uses %llu bytes total", nsm->mmap_size);
772 * Display state info for the network delay simulator.
775 * To display the state of the network simulator
778 * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
779 * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
780 * Configured bandwidth: 10.10 gbit/sec
781 * Configured packet size: 128
782 * Sim uses 157814784 bytes total
784 * @cliexcmd{show nsim}
788 VLIB_CLI_COMMAND (show_nsim_command, static) =
791 .short_help = "Display network delay simulator configuration",
792 .function = show_nsim_command_fn,
797 * fd.io coding-style-patch-verification: ON
800 * eval: (c-set-style "gnu")