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)
123 u64 total_buffer_size_in_bytes, per_worker_buffer_size;
124 u64 wheel_slots_per_worker;
126 int num_workers = vlib_num_workers ();
127 u32 pagesize = getpagesize ();
128 vlib_main_t *vm = nsm->vlib_main;
130 if (bandwidth == 0.0)
131 return VNET_API_ERROR_INVALID_VALUE;
134 return VNET_API_ERROR_INVALID_VALUE_2;
136 if (packet_size < 64.0 || packet_size > (f64) WHEEL_ENTRY_DATA_SIZE)
137 return VNET_API_ERROR_INVALID_VALUE_3;
139 /* Toss the old wheel(s)... */
140 if (nsm->is_configured)
142 for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
144 nsim_wheel_t *wp = nsm->wheel_by_thread[i];
145 munmap (wp, nsm->mmap_size);
146 nsm->wheel_by_thread[i] = 0;
152 /* delay in seconds, bandwidth in bits/sec */
153 total_buffer_size_in_bytes = (u32) ((delay * bandwidth) / 8.0) + 0.5;
156 * Work out how much buffering each worker needs, assuming decent
160 per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
162 per_worker_buffer_size = total_buffer_size_in_bytes;
164 wheel_slots_per_worker = per_worker_buffer_size / packet_size;
165 wheel_slots_per_worker++;
167 /* Save these for the show command */
168 nsm->bandwidth = bandwidth;
169 nsm->packet_size = packet_size;
171 vec_validate (nsm->wheel_by_thread, num_workers);
172 vec_validate (nsm->buffer_indices_by_thread, num_workers);
174 /* Initialize the output scheduler wheels */
175 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
179 nsm->mmap_size = sizeof (nsim_wheel_t)
180 + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
182 nsm->mmap_size += pagesize - 1;
183 nsm->mmap_size &= ~(pagesize - 1);
185 wp = clib_mem_vm_alloc (nsm->mmap_size);
187 wp->wheel_size = wheel_slots_per_worker;
191 wp->entries = (void *) (wp + 1);
192 nsm->wheel_by_thread[i] = wp;
193 vec_validate (nsm->buffer_indices_by_thread[i], VLIB_FRAME_SIZE - 1);
194 _vec_len (nsm->buffer_indices_by_thread[i]) = 0;
197 vlib_worker_thread_barrier_sync (vm);
199 /* turn on the ring scrapers */
200 for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
202 vlib_main_t *this_vm = vlib_mains[i];
204 vlib_node_set_state (this_vm, nsim_input_node.index,
205 VLIB_NODE_STATE_POLLING);
208 vlib_worker_thread_barrier_release (vm);
210 nsm->is_configured = 1;
215 * enable or disable the cross-connect
217 static clib_error_t *
218 nsim_enable_disable_command_fn (vlib_main_t * vm,
219 unformat_input_t * input,
220 vlib_cli_command_t * cmd)
222 nsim_main_t *nsm = &nsim_main;
223 u32 sw_if_index0 = ~0;
224 u32 sw_if_index1 = ~0;
225 int enable_disable = 1;
229 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
231 if (unformat (input, "disable"))
233 else if (unformat (input, "%U", unformat_vnet_sw_interface,
234 nsm->vnet_main, &tmp))
236 if (sw_if_index0 == ~0)
245 if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
246 return clib_error_return (0, "Please specify two interfaces...");
248 rv = nsim_enable_disable (nsm, sw_if_index0, sw_if_index1, enable_disable);
255 case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
256 return clib_error_return (0, "Not configured, please 'set nsim' first");
258 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
259 return clib_error_return
260 (0, "Invalid interface, only works on physical ports");
263 case VNET_API_ERROR_UNIMPLEMENTED:
264 return clib_error_return (0,
265 "Device driver doesn't support redirection");
269 return clib_error_return (0, "nsim_enable_disable returned %d", rv);
275 * Enable or disable network simulation cross-connect on two interfaces
276 * The network simulator must have already been configured, see
277 * the "nsim_configure" command.
279 * Place the interfaces into a bridge group, to ensure that
280 * interfaces are in promiscuous mode.
283 * To enable or disable network simulation cross-connect
285 * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
286 * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
288 * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
291 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
293 .path = "nsim enable-disable",
295 "nsim enable-disable <interface-name-1> <interface-name-2> [disable]",
296 .function = nsim_enable_disable_command_fn,
300 /* API message handler */
301 static void vl_api_nsim_enable_disable_t_handler
302 (vl_api_nsim_enable_disable_t * mp)
304 vl_api_nsim_enable_disable_reply_t *rmp;
305 nsim_main_t *nsm = &nsim_main;
308 rv = nsim_enable_disable (nsm, ntohl (mp->sw_if_index0),
309 ntohl (mp->sw_if_index1),
310 (int) (mp->enable_disable));
312 REPLY_MACRO (VL_API_NSIM_ENABLE_DISABLE_REPLY);
315 /* API message handler */
317 vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
319 vl_api_nsim_configure_reply_t *rmp;
320 nsim_main_t *nsm = &nsim_main;
321 f64 delay, bandwidth, packet_size;
324 delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
325 bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
326 packet_size = (f64) (ntohl (mp->average_packet_size));
328 rv = nsim_configure (nsm, bandwidth, delay, packet_size);
330 REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
334 /* Set up the API message handling tables */
335 static clib_error_t *
336 nsim_plugin_api_hookup (vlib_main_t * vm)
338 nsim_main_t *nsm = &nsim_main;
340 vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base), \
342 vl_api_##n##_t_handler, \
344 vl_api_##n##_t_endian, \
345 vl_api_##n##_t_print, \
346 sizeof(vl_api_##n##_t), 1);
347 foreach_nsim_plugin_api_msg;
353 #define vl_msg_name_crc_list
354 #include <nsim/nsim_all_api_h.h>
355 #undef vl_msg_name_crc_list
358 setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
360 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nsm->msg_id_base);
361 foreach_vl_msg_name_crc_nsim;
365 static clib_error_t *
366 nsim_init (vlib_main_t * vm)
368 nsim_main_t *nsm = &nsim_main;
369 clib_error_t *error = 0;
373 nsm->vnet_main = vnet_get_main ();
375 name = format (0, "nsim_%08x%c", api_version, 0);
377 /* Ask for a correctly-sized block of API message decode slots */
378 nsm->msg_id_base = vl_msg_api_get_msg_ids
379 ((char *) name, VL_MSG_FIRST_AVAILABLE);
381 error = nsim_plugin_api_hookup (vm);
383 /* Add our API messages to the global name_crc hash table */
384 setup_message_id_table (nsm, &api_main);
391 VLIB_INIT_FUNCTION (nsim_init);
394 VNET_FEATURE_INIT (nsim, static) =
396 .arc_name = "device-input",
398 .runs_before = VNET_FEATURES ("ethernet-input"),
403 VLIB_PLUGIN_REGISTER () =
405 .version = VPP_BUILD_VER,
406 .description = "network delay simulator plugin",
411 unformat_delay (unformat_input_t * input, va_list * args)
413 f64 *result = va_arg (*args, f64 *);
416 if (unformat (input, "%f us", &tmp))
417 *result = tmp * 1e-6;
418 else if (unformat (input, "%f ms", &tmp))
419 *result = tmp * 1e-3;
420 else if (unformat (input, "%f sec", &tmp))
429 unformat_bandwidth (unformat_input_t * input, va_list * args)
431 f64 *result = va_arg (*args, f64 *);
434 if (unformat (input, "%f gbit", &tmp))
436 else if (unformat (input, "%f gbyte", &tmp))
443 static clib_error_t *
444 set_nsim_command_fn (vlib_main_t * vm,
445 unformat_input_t * input, vlib_cli_command_t * cmd)
447 nsim_main_t *nsm = &nsim_main;
448 f64 delay, bandwidth;
449 f64 packet_size = 1500.0;
450 u32 num_workers = vlib_num_workers ();
453 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
455 if (unformat (input, "delay %U", unformat_delay, &delay))
457 else if (unformat (input, "bandwidth %U", unformat_bandwidth,
460 else if (unformat (input, "packet-size %f", &packet_size))
466 rv = nsim_configure (nsm, bandwidth, delay, packet_size);
470 case VNET_API_ERROR_INVALID_VALUE:
471 return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
473 case VNET_API_ERROR_INVALID_VALUE_2:
474 return clib_error_return (0, "invalid delay %.2f", delay);
476 case VNET_API_ERROR_INVALID_VALUE_3:
477 return clib_error_return (0, "invalid packet size %.2f", packet_size);
480 return clib_error_return (0, "error %d", rv);
486 vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
487 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
490 vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
491 nsm->mmap_size, nsm->mmap_size * num_workers);
493 vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
499 * Configure the network simulation cross-connect
500 * Once the simulator is configured, use the "nsim enable-disable" command
501 * to set up a cross-connect with the supplied delay characteristics.
503 * The cross connect configuration may be changed without restarting vpp
504 * but it is good practice to shut down the interfaces.
507 * To configure the network delay simulator:
509 * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
512 * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
515 VLIB_CLI_COMMAND (set_nsim_command, static) =
518 .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>",
519 .function = set_nsim_command_fn,
524 static clib_error_t *
525 show_nsim_command_fn (vlib_main_t * vm,
526 unformat_input_t * input, vlib_cli_command_t * cmd)
528 nsim_main_t *nsm = &nsim_main;
529 u32 num_workers = vlib_num_workers ();
532 if (nsm->is_configured == 0)
533 return clib_error_return (0, "Network simulator not configured");
535 if (nsm->sw_if_index0 == 0)
536 return clib_error_return (0, "Network simulator not enabled");
538 if (unformat (input, "verbose"))
541 vlib_cli_output (vm, "Network simulator cross-connects %U and %U",
542 format_vnet_sw_if_index_name,
543 nsm->vnet_main, nsm->sw_if_index0,
544 format_vnet_sw_if_index_name,
545 nsm->vnet_main, nsm->sw_if_index1);
548 "...inserting link delay of %.2f ms, %.2f ms round-trip",
549 nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
554 vlib_cli_output (vm, " Configured bandwidth: %.2f gbit/sec",
555 nsm->bandwidth / 1e9);
556 vlib_cli_output (vm, " Configured packet size: %f", nsm->packet_size);
559 (vm, " Sim uses %llu bytes per thread, %llu bytes total",
560 nsm->mmap_size, nsm->mmap_size * num_workers);
562 vlib_cli_output (vm, " Sim uses %llu bytes total", nsm->mmap_size);
569 * Display state info for the network delay simulator.
572 * To display the state of the network simulator
575 * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
576 * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
577 * Configured bandwidth: 10.10 gbit/sec
578 * Configured packet size: 128
579 * Sim uses 157814784 bytes total
581 * @cliexcmd{show nsim}
585 VLIB_CLI_COMMAND (show_nsim_command, static) =
588 .short_help = "Display network delay simulator configuration",
589 .function = show_nsim_command_fn,
594 * fd.io coding-style-patch-verification: ON
597 * eval: (c-set-style "gnu")