Network delay simulator plugin
[vpp.git] / src / plugins / nsim / nsim.c
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
57 #include <vlibapi/api_helper_macros.h>
58
59 nsim_main_t nsim_main;
60
61 /* List of message types that this plugin understands */
62
63 #define foreach_nsim_plugin_api_msg             \
64 _(NSIM_ENABLE_DISABLE, nsim_enable_disable)     \
65 _(NSIM_CONFIGURE, nsim_configure)
66
67 /* Action function shared between message handler and debug CLI */
68
69 int
70 nsim_enable_disable (nsim_main_t * nsm, u32 sw_if_index0,
71                      u32 sw_if_index1, int enable_disable)
72 {
73   vnet_sw_interface_t *sw;
74   vnet_hw_interface_t *hw;
75   int rv = 0;
76
77   if (nsm->is_configured == 0)
78     return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
79
80   /* Utterly wrong? */
81   if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
82                           sw_if_index0))
83     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
84
85   if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
86                           sw_if_index1))
87     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
88
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;
93
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;
97
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);
103
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);
108
109   nsm->sw_if_index0 = sw_if_index0;
110   nsm->sw_if_index1 = sw_if_index1;
111
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);
116
117   return rv;
118 }
119
120 static int
121 nsim_configure (nsim_main_t * nsm, f64 bandwidth, f64 delay, f64 packet_size)
122 {
123   u64 total_buffer_size_in_bytes, per_worker_buffer_size;
124   u64 wheel_slots_per_worker;
125   int i;
126   int num_workers = vlib_num_workers ();
127   u32 pagesize = getpagesize ();
128   vlib_main_t *vm = nsm->vlib_main;
129
130   if (bandwidth == 0.0)
131     return VNET_API_ERROR_INVALID_VALUE;
132
133   if (delay == 0.0)
134     return VNET_API_ERROR_INVALID_VALUE_2;
135
136   if (packet_size < 64.0 || packet_size > (f64) WHEEL_ENTRY_DATA_SIZE)
137     return VNET_API_ERROR_INVALID_VALUE_3;
138
139   /* Toss the old wheel(s)... */
140   if (nsm->is_configured)
141     {
142       for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
143         {
144           nsim_wheel_t *wp = nsm->wheel_by_thread[i];
145           munmap (wp, nsm->mmap_size);
146           nsm->wheel_by_thread[i] = 0;
147         }
148     }
149
150   nsm->delay = delay;
151
152   /* delay in seconds, bandwidth in bits/sec */
153   total_buffer_size_in_bytes = (u32) ((delay * bandwidth) / 8.0) + 0.5;
154
155   /*
156    * Work out how much buffering each worker needs, assuming decent
157    * RSS behavior.
158    */
159   if (num_workers)
160     per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
161   else
162     per_worker_buffer_size = total_buffer_size_in_bytes;
163
164   wheel_slots_per_worker = per_worker_buffer_size / packet_size;
165   wheel_slots_per_worker++;
166
167   /* Save these for the show command */
168   nsm->bandwidth = bandwidth;
169   nsm->packet_size = packet_size;
170
171   vec_validate (nsm->wheel_by_thread, num_workers);
172   vec_validate (nsm->buffer_indices_by_thread, num_workers);
173
174   /* Initialize the output scheduler wheels */
175   for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
176     {
177       nsim_wheel_t *wp;
178
179       nsm->mmap_size = sizeof (nsim_wheel_t)
180         + wheel_slots_per_worker * sizeof (nsim_wheel_entry_t);
181
182       nsm->mmap_size += pagesize - 1;
183       nsm->mmap_size &= ~(pagesize - 1);
184
185       wp = clib_mem_vm_alloc (nsm->mmap_size);
186       ASSERT (wp != 0);
187       wp->wheel_size = wheel_slots_per_worker;
188       wp->cursize = 0;
189       wp->head = 0;
190       wp->tail = 0;
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;
195     }
196
197   vlib_worker_thread_barrier_sync (vm);
198
199   /* turn on the ring scrapers */
200   for (i = num_workers ? 1 : 0; i < num_workers + 1; i++)
201     {
202       vlib_main_t *this_vm = vlib_mains[i];
203
204       vlib_node_set_state (this_vm, nsim_input_node.index,
205                            VLIB_NODE_STATE_POLLING);
206     }
207
208   vlib_worker_thread_barrier_release (vm);
209
210   nsm->is_configured = 1;
211   return 0;
212 }
213
214 /*
215  * enable or disable the cross-connect
216  */
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)
221 {
222   nsim_main_t *nsm = &nsim_main;
223   u32 sw_if_index0 = ~0;
224   u32 sw_if_index1 = ~0;
225   int enable_disable = 1;
226   u32 tmp;
227   int rv;
228
229   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
230     {
231       if (unformat (input, "disable"))
232         enable_disable = 0;
233       else if (unformat (input, "%U", unformat_vnet_sw_interface,
234                          nsm->vnet_main, &tmp))
235         {
236           if (sw_if_index0 == ~0)
237             sw_if_index0 = tmp;
238           else
239             sw_if_index1 = tmp;
240         }
241       else
242         break;
243     }
244
245   if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
246     return clib_error_return (0, "Please specify two interfaces...");
247
248   rv = nsim_enable_disable (nsm, sw_if_index0, sw_if_index1, enable_disable);
249
250   switch (rv)
251     {
252     case 0:
253       break;
254
255     case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
256       return clib_error_return (0, "Not configured, please 'set nsim' first");
257
258     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
259       return clib_error_return
260         (0, "Invalid interface, only works on physical ports");
261       break;
262
263     case VNET_API_ERROR_UNIMPLEMENTED:
264       return clib_error_return (0,
265                                 "Device driver doesn't support redirection");
266       break;
267
268     default:
269       return clib_error_return (0, "nsim_enable_disable returned %d", rv);
270     }
271   return 0;
272 }
273
274 /*?
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.
278  *
279  * Place the interfaces into a bridge group, to ensure that
280  * interfaces are in promiscuous mode.
281  *
282  * @cliexpar
283  * To enable or disable network simulation cross-connect
284  * @clistart
285  * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
286  * nsim enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
287  * @cliend
288  * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
289 ?*/
290 /* *INDENT-OFF* */
291 VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
292 {
293   .path = "nsim enable-disable",
294   .short_help =
295   "nsim enable-disable <interface-name-1> <interface-name-2> [disable]",
296   .function = nsim_enable_disable_command_fn,
297 };
298 /* *INDENT-ON* */
299
300 /* API message handler */
301 static void vl_api_nsim_enable_disable_t_handler
302   (vl_api_nsim_enable_disable_t * mp)
303 {
304   vl_api_nsim_enable_disable_reply_t *rmp;
305   nsim_main_t *nsm = &nsim_main;
306   int rv;
307
308   rv = nsim_enable_disable (nsm, ntohl (mp->sw_if_index0),
309                             ntohl (mp->sw_if_index1),
310                             (int) (mp->enable_disable));
311
312   REPLY_MACRO (VL_API_NSIM_ENABLE_DISABLE_REPLY);
313 }
314
315 /* API message handler */
316 static void
317 vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
318 {
319   vl_api_nsim_configure_reply_t *rmp;
320   nsim_main_t *nsm = &nsim_main;
321   f64 delay, bandwidth, packet_size;
322   int rv;
323
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));
327
328   rv = nsim_configure (nsm, bandwidth, delay, packet_size);
329
330   REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
331 }
332
333
334 /* Set up the API message handling tables */
335 static clib_error_t *
336 nsim_plugin_api_hookup (vlib_main_t * vm)
337 {
338   nsim_main_t *nsm = &nsim_main;
339 #define _(N,n)                                                  \
340     vl_msg_api_set_handlers((VL_API_##N + nsm->msg_id_base),     \
341                            #n,                                  \
342                            vl_api_##n##_t_handler,              \
343                            vl_noop_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;
348 #undef _
349
350   return 0;
351 }
352
353 #define vl_msg_name_crc_list
354 #include <nsim/nsim_all_api_h.h>
355 #undef vl_msg_name_crc_list
356
357 static void
358 setup_message_id_table (nsim_main_t * nsm, api_main_t * am)
359 {
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;
362 #undef _
363 }
364
365 static clib_error_t *
366 nsim_init (vlib_main_t * vm)
367 {
368   nsim_main_t *nsm = &nsim_main;
369   clib_error_t *error = 0;
370   u8 *name;
371
372   nsm->vlib_main = vm;
373   nsm->vnet_main = vnet_get_main ();
374
375   name = format (0, "nsim_%08x%c", api_version, 0);
376
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);
380
381   error = nsim_plugin_api_hookup (vm);
382
383   /* Add our API messages to the global name_crc hash table */
384   setup_message_id_table (nsm, &api_main);
385
386   vec_free (name);
387
388   return error;
389 }
390
391 VLIB_INIT_FUNCTION (nsim_init);
392
393 /* *INDENT-OFF* */
394 VNET_FEATURE_INIT (nsim, static) =
395 {
396   .arc_name = "device-input",
397   .node_name = "nsim",
398   .runs_before = VNET_FEATURES ("ethernet-input"),
399 };
400 /* *INDENT-ON */
401
402 /* *INDENT-OFF* */
403 VLIB_PLUGIN_REGISTER () =
404 {
405   .version = VPP_BUILD_VER,
406   .description = "network delay simulator plugin",
407 };
408 /* *INDENT-ON* */
409
410 static uword
411 unformat_delay (unformat_input_t * input, va_list * args)
412 {
413   f64 *result = va_arg (*args, f64 *);
414   f64 tmp;
415
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))
421     *result = tmp;
422   else
423     return 0;
424
425   return 1;
426 }
427
428 static uword
429 unformat_bandwidth (unformat_input_t * input, va_list * args)
430 {
431   f64 *result = va_arg (*args, f64 *);
432   f64 tmp;
433
434   if (unformat (input, "%f gbit", &tmp))
435     *result = tmp * 1e9;
436   else if (unformat (input, "%f gbyte", &tmp))
437     *result = tmp * 8e9;
438   else
439     return 0;
440   return 1;
441 }
442
443 static clib_error_t *
444 set_nsim_command_fn (vlib_main_t * vm,
445                      unformat_input_t * input, vlib_cli_command_t * cmd)
446 {
447   nsim_main_t *nsm = &nsim_main;
448   f64 delay, bandwidth;
449   f64 packet_size = 1500.0;
450   u32 num_workers = vlib_num_workers ();
451   int rv;
452
453   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
454     {
455       if (unformat (input, "delay %U", unformat_delay, &delay))
456         ;
457       else if (unformat (input, "bandwidth %U", unformat_bandwidth,
458                          &bandwidth))
459         ;
460       else if (unformat (input, "packet-size %f", &packet_size))
461         ;
462       else
463         break;
464     }
465
466   rv = nsim_configure (nsm, bandwidth, delay, packet_size);
467
468   switch (rv)
469     {
470     case VNET_API_ERROR_INVALID_VALUE:
471       return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
472
473     case VNET_API_ERROR_INVALID_VALUE_2:
474       return clib_error_return (0, "invalid delay %.2f", delay);
475
476     case VNET_API_ERROR_INVALID_VALUE_3:
477       return clib_error_return (0, "invalid packet size %.2f", packet_size);
478
479     default:
480       return clib_error_return (0, "error %d", rv);
481
482     case 0:
483       break;
484     }
485
486   vlib_cli_output (vm, "Configured link delay %.2f ms, %.2f ms round-trip",
487                    nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
488
489   if (num_workers)
490     vlib_cli_output (vm, "Sim uses %llu bytes per thread, %llu bytes total",
491                      nsm->mmap_size, nsm->mmap_size * num_workers);
492   else
493     vlib_cli_output (vm, "Sim uses %llu bytes total", nsm->mmap_size);
494
495   return 0;
496 }
497
498 /*?
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.
502  *
503  * The cross connect configuration may be changed without restarting vpp
504  * but it is good practice to shut down the interfaces.
505  *
506  * @cliexpar
507  * To configure the network delay simulator:
508  * @clistart
509  * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
510  *
511  * @cliend
512  * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
513 ?*/
514 /* *INDENT-OFF* */
515 VLIB_CLI_COMMAND (set_nsim_command, static) =
516 {
517   .path = "set nsim",
518   .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>",
519   .function = set_nsim_command_fn,
520 };
521 /* *INDENT-ON*/
522
523
524 static clib_error_t *
525 show_nsim_command_fn (vlib_main_t * vm,
526                       unformat_input_t * input, vlib_cli_command_t * cmd)
527 {
528   nsim_main_t *nsm = &nsim_main;
529   u32 num_workers = vlib_num_workers ();
530   int verbose = 0;
531
532   if (nsm->is_configured == 0)
533     return clib_error_return (0, "Network simulator not configured");
534
535   if (nsm->sw_if_index0 == 0)
536     return clib_error_return (0, "Network simulator not enabled");
537
538   if (unformat (input, "verbose"))
539     verbose = 1;
540
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);
546
547   vlib_cli_output (vm,
548                    "...inserting link delay of %.2f ms, %.2f ms round-trip",
549                    nsm->delay * 1e3, 2.0 * nsm->delay * 1e3);
550
551   if (verbose)
552     {
553
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);
557       if (num_workers)
558         vlib_cli_output
559           (vm, "  Sim uses %llu bytes per thread, %llu bytes total",
560            nsm->mmap_size, nsm->mmap_size * num_workers);
561       else
562         vlib_cli_output (vm, "  Sim uses %llu bytes total", nsm->mmap_size);
563     }
564
565   return 0;
566 }
567
568 /*?
569  * Display state info for the network delay simulator.
570  *
571  * @cliexpar
572  * To display the state of the network simulator
573  * @clistart
574  * show nsim verbose
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
580  * @cliend
581  * @cliexcmd{show nsim}
582 ?*/
583
584 /* *INDENT-OFF* */
585 VLIB_CLI_COMMAND (show_nsim_command, static) =
586 {
587   .path = "show nsim",
588   .short_help = "Display network delay simulator configuration",
589   .function = show_nsim_command_fn,
590 };
591 /* *INDENT-ON* */
592
593 /*
594  * fd.io coding-style-patch-verification: ON
595  *
596  * Local Variables:
597  * eval: (c-set-style "gnu")
598  * End:
599  */