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_ENABLE_DISABLE, nsim_enable_disable) \
65 _(NSIM_CONFIGURE, nsim_configure)
67 /* Action function shared between message handler and debug CLI */
70 nsim_enable_disable (nsim_main_t * nsm, u32 sw_if_index0,
71 u32 sw_if_index1, int enable_disable)
73 vnet_sw_interface_t *sw;
74 vnet_hw_interface_t *hw;
77 if (nsm->is_configured == 0)
78 return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
81 if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
83 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
85 if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
87 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
89 /* Not a physical port? */
90 sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index0);
91 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
92 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
94 sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index1);
95 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
96 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
98 /* Add graph arcs for the input / wheel scraper node */
99 hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index0);
100 nsm->output_next_index0 =
101 vlib_node_add_next (nsm->vlib_main,
102 nsim_input_node.index, hw->output_node_index);
104 hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index1);
105 nsm->output_next_index1 =
106 vlib_node_add_next (nsm->vlib_main,
107 nsim_input_node.index, hw->output_node_index);
109 nsm->sw_if_index0 = sw_if_index0;
110 nsm->sw_if_index1 = sw_if_index1;
112 vnet_feature_enable_disable ("device-input", "nsim",
113 sw_if_index0, enable_disable, 0, 0);
114 vnet_feature_enable_disable ("device-input", "nsim",
115 sw_if_index1, enable_disable, 0, 0);
121 nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size,
124 u64 total_buffer_size_in_bytes, per_worker_buffer_size;
125 u64 wheel_slots_per_worker;
127 int num_workers = vlib_num_workers ();
128 u32 pagesize = getpagesize ();
129 vlib_main_t *vm = nsm->vlib_main;
131 if (bandwidth == 0.0)
132 return VNET_API_ERROR_INVALID_VALUE;
135 return VNET_API_ERROR_INVALID_VALUE_2;
137 if (packet_size < 64.0 || packet_size > (f64) WHEEL_ENTRY_DATA_SIZE)
138 return VNET_API_ERROR_INVALID_VALUE_3;
140 /* Toss the old wheel(s)... */
141 if (nsm->is_configured)
143 for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
145 nsim_wheel_t *wp = nsm->wheel_by_thread[i];
146 munmap (wp, nsm->mmap_size);
147 nsm->wheel_by_thread[i] = 0;
152 nsm->drop_fraction = drop_fraction;
154 /* delay in seconds, bandwidth in bits/sec */
155 total_buffer_size_in_bytes = (u32) ((delay * bandwidth) / 8.0) + 0.5;
158 * Work out how much buffering each worker needs, assuming decent
162 per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
164 per_worker_buffer_size = total_buffer_size_in_bytes;
166 wheel_slots_per_worker = per_worker_buffer_size / packet_size;
167 wheel_slots_per_worker++;
169 /* Save these for the show command */
170 nsm->bandwidth = bandwidth;
171 nsm->packet_size = packet_size;
173 vec_validate (nsm->wheel_by_thread, num_workers);
174 vec_validate (nsm->buffer_indices_by_thread, num_workers);
176 /* Initialize the output scheduler wheels */
177 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
181 nsm->mmap_size = sizeof (nsim_wheel_t)
182 + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
184 nsm->mmap_size += pagesize - 1;
185 nsm->mmap_size &= ~(pagesize - 1);
187 wp = clib_mem_vm_alloc (nsm->mmap_size);
189 wp->wheel_size = wheel_slots_per_worker;
193 wp->entries = (void *) (wp + 1);
194 nsm->wheel_by_thread[i] = wp;
195 vec_validate (nsm->buffer_indices_by_thread[i], VLIB_FRAME_SIZE - 1);
196 _vec_len (nsm->buffer_indices_by_thread[i]) = 0;
199 vlib_worker_thread_barrier_sync (vm);
201 /* turn on the ring scrapers */
202 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
204 vlib_main_t *this_vm = vlib_mains[i];
206 vlib_node_set_state (this_vm, nsim_input_node.index,
207 VLIB_NODE_STATE_POLLING);
210 vlib_worker_thread_barrier_release (vm);
212 nsm->is_configured = 1;
217 * enable or disable the cross-connect
219 static clib_error_t *
220 nsim_enable_disable_command_fn (vlib_main_t * vm,
221 unformat_input_t * input,
222 vlib_cli_command_t * cmd)
224 nsim_main_t *nsm = &nsim_main;
225 u32 sw_if_index0 = ~0;
226 u32 sw_if_index1 = ~0;
227 int enable_disable = 1;
231 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
233 if (unformat (input, "disable"))
235 else if (unformat (input, "%U", unformat_vnet_sw_interface,
236 nsm->vnet_main, &tmp))
238 if (sw_if_index0 == ~0)
247 if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
248 return clib_error_return (0, "Please specify two interfaces...");
250 rv = nsim_enable_disable (nsm, sw_if_index0, sw_if_index1, enable_disable);
257 case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
258 return clib_error_return (0, "Not configured, please 'set nsim' first");
260 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
261 return clib_error_return
262 (0, "Invalid interface, only works on physical ports");
265 case VNET_API_ERROR_UNIMPLEMENTED:
266 return clib_error_return (0,
267 "Device driver doesn't support redirection");
271 return clib_error_return (0, "nsim_enable_disable returned %d", rv);
277 * Enable or disable network simulation cross-connect on two interfaces
278 * The network simulator must have already been configured, see
279 * the "nsim_configure" command.
281 * Place the interfaces into a bridge group, to ensure that
282 * interfaces are in promiscuous mode.
285 * To enable or disable network simulation cross-connect
287 * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
288 * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
290 * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
293 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
295 .path = "nsim enable-disable",
297 "nsim enable-disable <interface-name-1> <interface-name-2> [disable]",
298 .function = nsim_enable_disable_command_fn,
302 /* API message handler */
303 static void vl_api_nsim_enable_disable_t_handler
304 (vl_api_nsim_enable_disable_t * mp)
306 vl_api_nsim_enable_disable_reply_t *rmp;
307 nsim_main_t *nsm = &nsim_main;
310 rv = nsim_enable_disable (nsm, ntohl (mp->sw_if_index0),
311 ntohl (mp->sw_if_index1),
312 (int) (mp->enable_disable));
314 REPLY_MACRO (VL_API_NSIM_ENABLE_DISABLE_REPLY);
317 /* API message handler */
319 vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
321 vl_api_nsim_configure_reply_t *rmp;
322 nsim_main_t *nsm = &nsim_main;
323 f64 delay, bandwidth, packet_size, drop_fraction;
324 u32 packets_per_drop;
327 delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
328 bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
329 packet_size = (f64) (ntohl (mp->average_packet_size));
331 packets_per_drop = ntohl (mp->packets_per_drop);
332 if (packets_per_drop > 0)
333 drop_fraction = 1.0 / (f64) (packets_per_drop);
337 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
339 REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
343 /* Set up the API message handling tables */
344 static clib_error_t *
345 nsim_plugin_api_hookup (vlib_main_t * vm)
347 nsim_main_t *nsm = &nsim_main;
349 vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base), \
351 vl_api_##n##_t_handler, \
353 vl_api_##n##_t_endian, \
354 vl_api_##n##_t_print, \
355 sizeof(vl_api_##n##_t), 1);
356 foreach_nsim_plugin_api_msg;
362 #define vl_msg_name_crc_list
363 #include <nsim/nsim_all_api_h.h>
364 #undef vl_msg_name_crc_list
367 setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
369 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nsm->msg_id_base);
370 foreach_vl_msg_name_crc_nsim;
374 static clib_error_t *
375 nsim_init (vlib_main_t * vm)
377 nsim_main_t *nsm = &nsim_main;
378 clib_error_t *error = 0;
382 nsm->vnet_main = vnet_get_main ();
384 name = format (0, "nsim_%08x%c", api_version, 0);
386 /* Ask for a correctly-sized block of API message decode slots */
387 nsm->msg_id_base = vl_msg_api_get_msg_ids
388 ((char *) name, VL_MSG_FIRST_AVAILABLE);
390 error = nsim_plugin_api_hookup (vm);
392 /* Add our API messages to the global name_crc hash table */
393 setup_message_id_table (nsm, &api_main);
400 VLIB_INIT_FUNCTION (nsim_init);
403 VNET_FEATURE_INIT (nsim, static) =
405 .arc_name = "device-input",
407 .runs_before = VNET_FEATURES ("ethernet-input"),
412 VLIB_PLUGIN_REGISTER () =
414 .version = VPP_BUILD_VER,
415 .description = "network delay simulator plugin",
420 unformat_delay (unformat_input_t * input, va_list * args)
422 f64 *result = va_arg (*args, f64 *);
425 if (unformat (input, "%f us", &tmp))
426 *result = tmp * 1e-6;
427 else if (unformat (input, "%f ms", &tmp))
428 *result = tmp * 1e-3;
429 else if (unformat (input, "%f sec", &tmp))
438 unformat_bandwidth (unformat_input_t * input, va_list * args)
440 f64 *result = va_arg (*args, f64 *);
443 if (unformat (input, "%f gbit", &tmp))
445 else if (unformat (input, "%f gbyte", &tmp))
452 static clib_error_t *
453 set_nsim_command_fn (vlib_main_t * vm,
454 unformat_input_t * input, vlib_cli_command_t * cmd)
456 nsim_main_t *nsm = &nsim_main;
457 f64 delay, bandwidth;
458 f64 packet_size = 1500.0;
459 f64 drop_fraction = 0.0;
460 u32 packets_per_drop;
461 u32 num_workers = vlib_num_workers ();
464 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
466 if (unformat (input, "delay %U", unformat_delay, &delay))
468 else if (unformat (input, "bandwidth %U", unformat_bandwidth,
471 else if (unformat (input, "packet-size %f", &packet_size))
473 else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
475 if (packets_per_drop > 0)
476 drop_fraction = 1.0 / ((f64) packets_per_drop);
478 else if (unformat (input, "drop-fraction %f", &drop_fraction))
480 if (drop_fraction < 0.0 || drop_fraction > 1.0)
481 return clib_error_return
482 (0, "drop fraction must be between zero and 1");
488 rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction);
492 case VNET_API_ERROR_INVALID_VALUE:
493 return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
495 case VNET_API_ERROR_INVALID_VALUE_2:
496 return clib_error_return (0, "invalid delay %.2f", delay);
498 case VNET_API_ERROR_INVALID_VALUE_3:
499 return clib_error_return (0, "invalid packet size %.2f", packet_size);
502 return clib_error_return (0, "error %d", rv);
508 vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
509 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
510 if (nsm->drop_fraction > 0.0)
511 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
516 vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
517 nsm->mmap_size, nsm->mmap_size * num_workers);
519 vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
525 * Configure the network simulation cross-connect
526 * Once the simulator is configured, use the "nsim enable-disable" command
527 * to set up a cross-connect with the supplied delay characteristics.
529 * The cross connect configuration may be changed without restarting vpp
530 * but it is good practice to shut down the interfaces.
533 * To configure the network delay simulator:
535 * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
538 * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
541 VLIB_CLI_COMMAND (set_nsim_command, static) =
544 .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>",
545 .function = set_nsim_command_fn,
550 static clib_error_t *
551 show_nsim_command_fn (vlib_main_t * vm,
552 unformat_input_t * input, vlib_cli_command_t * cmd)
554 nsim_main_t *nsm = &nsim_main;
555 u32 num_workers = vlib_num_workers ();
558 if (nsm->is_configured == 0)
559 return clib_error_return (0, "Network simulator not configured");
561 if (nsm->sw_if_index0 == 0)
562 return clib_error_return (0, "Network simulator not enabled");
564 if (unformat (input, "verbose"))
567 vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
568 format_vnet_sw_if_index_name,
569 nsm->vnet_main, nsm->sw_if_index0,
570 format_vnet_sw_if_index_name,
571 nsm->vnet_main, nsm->sw_if_index1);
574 "...inserting link delay of %.2f ms, %.2f ms round-trip",
575 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
577 if (nsm->drop_fraction > 0.0)
578 vlib_cli_output (vm, "... simulating a network drop fraction of %.5f",
584 vlib_cli_output (vm, " Configured bandwidth: %.2f gbit/sec",
585 nsm->bandwidth / 1e9);
586 vlib_cli_output (vm, " Configured packet size: %f", nsm->packet_size);
589 (vm, " Sim uses %llu bytes per thread, %llu bytes total",
590 nsm->mmap_size, nsm->mmap_size * num_workers);
592 vlib_cli_output (vm, " Sim uses %llu bytes total", nsm->mmap_size);
599 * Display state info for the network delay simulator.
602 * To display the state of the network simulator
605 * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
606 * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
607 * Configured bandwidth: 10.10 gbit/sec
608 * Configured packet size: 128
609 * Sim uses 157814784 bytes total
611 * @cliexcmd{show nsim}
615 VLIB_CLI_COMMAND (show_nsim_command, static) =
618 .short_help = "Display network delay simulator configuration",
619 .function = show_nsim_command_fn,
624 * fd.io coding-style-patch-verification: ON
627 * eval: (c-set-style "gnu")