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;
352 rv = nsim_cross_connect_enable_disable (nsm, ntohl (mp->sw_if_index0),
353 ntohl (mp->sw_if_index1),
354 (int) (mp->enable_disable));
356 REPLY_MACRO (VL_API_NSIM_CROSS_CONNECT_ENABLE_DISABLE_REPLY);
359 /* API message handler */
360 static void vl_api_nsim_output_feature_enable_disable_t_handler
361 (vl_api_nsim_output_feature_enable_disable_t * mp)
363 vl_api_nsim_output_feature_enable_disable_reply_t *rmp;
364 nsim_main_t *nsm = &nsim_main;
367 rv = nsim_output_feature_enable_disable (nsm, ntohl (mp->sw_if_index),
368 (int) (mp->enable_disable));
370 REPLY_MACRO (VL_API_NSIM_OUTPUT_FEATURE_ENABLE_DISABLE_REPLY);
373 /* API message handler */
375 vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
377 vl_api_nsim_configure_reply_t *rmp;
378 nsim_main_t *nsm = &nsim_main;
379 f64 delay, bandwidth, packet_size, drop_fraction;
380 u32 packets_per_drop;
383 delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
384 bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
385 packet_size = (f64) (ntohl (mp->average_packet_size));
387 packets_per_drop = ntohl (mp->packets_per_drop);
388 if (packets_per_drop > 0)
389 drop_fraction = 1.0 / (f64) (packets_per_drop);
393 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
395 REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
400 * enable or disable the output_feature
402 static clib_error_t *
403 nsim_output_feature_enable_disable_command_fn (vlib_main_t * vm,
404 unformat_input_t * input,
405 vlib_cli_command_t * cmd)
407 nsim_main_t *nsm = &nsim_main;
408 unformat_input_t _line_input, *line_input = &_line_input;
409 u32 sw_if_index = ~0;
410 int enable_disable = 1;
413 /* Get a line of input. */
414 if (!unformat_user (input, unformat_line_input, line_input))
417 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
419 if (unformat (line_input, "disable"))
421 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
422 nsm->vnet_main, &sw_if_index))
426 clib_error_t *error = clib_error_return (0, "unknown input `%U'",
427 format_unformat_error,
429 unformat_free (line_input);
434 unformat_free (line_input);
436 if (sw_if_index == ~0)
437 return clib_error_return (0, "Please specify one interface...");
439 rv = nsim_output_feature_enable_disable (nsm, sw_if_index, enable_disable);
446 case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
447 return clib_error_return (0, "Not configured, please 'set nsim' first");
449 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
450 return clib_error_return
451 (0, "Invalid interface, only works on physical ports");
454 case VNET_API_ERROR_UNIMPLEMENTED:
455 return clib_error_return (0,
456 "Device driver doesn't support redirection");
460 return clib_error_return
461 (0, "nsim_output_feature_enable_disable returned %d", rv);
467 * Enable or disable network simulation output feature on an interface
468 * The network simulator must have already been configured, see
469 * the "nsim_configure" command.
472 * To enable or disable network simulation output feature
474 * nsim output-feature enable-disable TenGigabitEthernet2/0/0
475 * nsim output-feature enable-disable TenGigabitEthernet2/0/0 disable
477 * @cliexcmd{nsim output-feature enable-disable <intfc> [disable]}
480 VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
482 .path = "nsim output-feature enable-disable",
484 "nsim output-feature enable-disable <interface-name> [disable]",
485 .function = nsim_output_feature_enable_disable_command_fn,
489 /* Set up the API message handling tables */
490 static clib_error_t *
491 nsim_plugin_api_hookup (vlib_main_t * vm)
493 nsim_main_t *nsm = &nsim_main;
495 vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base), \
497 vl_api_##n##_t_handler, \
499 vl_api_##n##_t_endian, \
500 vl_api_##n##_t_print, \
501 sizeof(vl_api_##n##_t), 1);
502 foreach_nsim_plugin_api_msg;
508 #define vl_msg_name_crc_list
509 #include <nsim/nsim_all_api_h.h>
510 #undef vl_msg_name_crc_list
513 setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
515 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nsm->msg_id_base);
516 foreach_vl_msg_name_crc_nsim;
520 static clib_error_t *
521 nsim_init (vlib_main_t * vm)
523 nsim_main_t *nsm = &nsim_main;
524 clib_error_t *error = 0;
528 nsm->vnet_main = vnet_get_main ();
530 name = format (0, "nsim_%08x%c", api_version, 0);
532 /* Ask for a correctly-sized block of API message decode slots */
533 nsm->msg_id_base = vl_msg_api_get_msg_ids
534 ((char *) name, VL_MSG_FIRST_AVAILABLE);
536 error = nsim_plugin_api_hookup (vm);
538 /* Add our API messages to the global name_crc hash table */
539 setup_message_id_table (nsm, &api_main);
546 VLIB_INIT_FUNCTION (nsim_init);
549 VNET_FEATURE_INIT (nsim, static) =
551 .arc_name = "device-input",
553 .runs_before = VNET_FEATURES ("ethernet-input"),
558 VNET_FEATURE_INIT (nsim_feature, static) =
560 .arc_name = "interface-output",
561 .node_name = "nsim-output-feature",
562 .runs_before = VNET_FEATURES ("interface-tx"),
567 VLIB_PLUGIN_REGISTER () =
569 .version = VPP_BUILD_VER,
570 .description = "network delay simulator plugin",
575 unformat_delay (unformat_input_t * input, va_list * args)
577 f64 *result = va_arg (*args, f64 *);
580 if (unformat (input, "%f us", &tmp))
581 *result = tmp * 1e-6;
582 else if (unformat (input, "%f ms", &tmp))
583 *result = tmp * 1e-3;
584 else if (unformat (input, "%f sec", &tmp))
593 unformat_bandwidth (unformat_input_t * input, va_list * args)
595 f64 *result = va_arg (*args, f64 *);
598 if (unformat (input, "%f gbit", &tmp))
600 else if (unformat (input, "%f gbyte", &tmp))
607 static clib_error_t *
608 set_nsim_command_fn (vlib_main_t * vm,
609 unformat_input_t * input, vlib_cli_command_t * cmd)
611 nsim_main_t *nsm = &nsim_main;
612 f64 delay, bandwidth;
613 f64 packet_size = 1500.0;
614 f64 drop_fraction = 0.0;
615 u32 packets_per_drop;
616 u32 num_workers = vlib_num_workers ();
619 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
621 if (unformat (input, "delay %U", unformat_delay, &delay))
623 else if (unformat (input, "bandwidth %U", unformat_bandwidth,
626 else if (unformat (input, "packet-size %f", &packet_size))
628 else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
630 if (packets_per_drop > 0)
631 drop_fraction = 1.0 / ((f64) packets_per_drop);
633 else if (unformat (input, "drop-fraction %f", &drop_fraction))
635 if (drop_fraction < 0.0 || drop_fraction > 1.0)
636 return clib_error_return
637 (0, "drop fraction must be between zero and 1");
643 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
647 case VNET_API_ERROR_INVALID_VALUE:
648 return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
650 case VNET_API_ERROR_INVALID_VALUE_2:
651 return clib_error_return (0, "invalid delay %.2f", delay);
653 case VNET_API_ERROR_INVALID_VALUE_3:
654 return clib_error_return (0, "invalid packet size %.2f", packet_size);
657 return clib_error_return (0, "error %d", rv);
663 vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
664 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
665 if (nsm->drop_fraction > 0.0)
666 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
671 vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
672 nsm->mmap_size, nsm->mmap_size * num_workers);
674 vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
680 * Configure the network simulation cross-connect
681 * Once the simulator is configured, use the "nsim enable-disable" command
682 * to set up a cross-connect with the supplied delay characteristics.
684 * The cross connect configuration may be changed without restarting vpp
685 * but it is good practice to shut down the interfaces.
688 * To configure the network delay simulator:
690 * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
693 * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
696 VLIB_CLI_COMMAND (set_nsim_command, static) =
699 .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>\n"
700 " [packets-per-drop <nn>][drop-fraction <f64: 0.0 - 1.0>]",
701 .function = set_nsim_command_fn,
706 static clib_error_t *
707 show_nsim_command_fn (vlib_main_t * vm,
708 unformat_input_t * input, vlib_cli_command_t * cmd)
710 nsim_main_t *nsm = &nsim_main;
711 u32 num_workers = vlib_num_workers ();
714 if (nsm->is_configured == 0)
715 return clib_error_return (0, "Network simulator not configured");
717 if (nsm->sw_if_index0 == 0)
718 return clib_error_return (0, "Network simulator not enabled");
720 if (unformat (input, "verbose"))
723 vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
724 format_vnet_sw_if_index_name,
725 nsm->vnet_main, nsm->sw_if_index0,
726 format_vnet_sw_if_index_name,
727 nsm->vnet_main, nsm->sw_if_index1);
730 "...inserting link delay of %.2f ms, %.2f ms round-trip",
731 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
733 if (nsm->drop_fraction > 0.0)
734 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
740 vlib_cli_output (vm, " Configured bandwidth: %.2f gbit/sec",
741 nsm->bandwidth / 1e9);
742 vlib_cli_output (vm, " Configured packet size: %f", nsm->packet_size);
745 (vm, " Sim uses %llu bytes per thread, %llu bytes total",
746 nsm->mmap_size, nsm->mmap_size * num_workers);
748 vlib_cli_output (vm, " Sim uses %llu bytes total", nsm->mmap_size);
755 * Display state info for the network delay simulator.
758 * To display the state of the network simulator
761 * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
762 * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
763 * Configured bandwidth: 10.10 gbit/sec
764 * Configured packet size: 128
765 * Sim uses 157814784 bytes total
767 * @cliexcmd{show nsim}
771 VLIB_CLI_COMMAND (show_nsim_command, static) =
774 .short_help = "Display network delay simulator configuration",
775 .function = show_nsim_command_fn,
780 * fd.io coding-style-patch-verification: ON
783 * eval: (c-set-style "gnu")