LCOV - code coverage report
Current view: top level - plugins/nsim - nsim.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 19 347 5.5 %
Date: 2023-10-26 01:39:38 Functions: 18 35 51.4 %

          Line data    Source code
       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.api_enum.h>
      34             : #include <nsim/nsim.api_types.h>
      35             : 
      36             : #define REPLY_MSG_ID_BASE nsm->msg_id_base
      37             : #include <vlibapi/api_helper_macros.h>
      38             : 
      39             : nsim_main_t nsim_main;
      40             : 
      41             : /* Action functions shared between message handlers and debug CLI */
      42             : 
      43             : int
      44           0 : nsim_cross_connect_enable_disable (nsim_main_t * nsm, u32 sw_if_index0,
      45             :                                    u32 sw_if_index1, int enable_disable)
      46             : {
      47             :   vnet_sw_interface_t *sw;
      48             :   vnet_hw_interface_t *hw;
      49           0 :   int rv = 0;
      50             : 
      51           0 :   if (nsm->is_configured == 0)
      52           0 :     return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
      53             : 
      54             :   /* Utterly wrong? */
      55           0 :   if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
      56             :                           sw_if_index0))
      57           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
      58             : 
      59           0 :   if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
      60             :                           sw_if_index1))
      61           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
      62             : 
      63             :   /* Not a physical port? */
      64           0 :   sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index0);
      65           0 :   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
      66           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
      67             : 
      68           0 :   sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index1);
      69           0 :   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
      70           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
      71             : 
      72             :   /* Add graph arcs for the input / wheel scraper node */
      73           0 :   hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index0);
      74           0 :   nsm->output_next_index0 =
      75           0 :     vlib_node_add_next (nsm->vlib_main,
      76           0 :                         nsim_input_node.index, hw->output_node_index);
      77             : 
      78           0 :   hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index1);
      79           0 :   nsm->output_next_index1 =
      80           0 :     vlib_node_add_next (nsm->vlib_main,
      81           0 :                         nsim_input_node.index, hw->output_node_index);
      82             : 
      83           0 :   nsm->sw_if_index0 = sw_if_index0;
      84           0 :   nsm->sw_if_index1 = sw_if_index1;
      85             : 
      86           0 :   vnet_feature_enable_disable ("device-input", "nsim",
      87             :                                sw_if_index0, enable_disable, 0, 0);
      88           0 :   vnet_feature_enable_disable ("device-input", "nsim",
      89             :                                sw_if_index1, enable_disable, 0, 0);
      90             : 
      91           0 :   return rv;
      92             : }
      93             : 
      94             : int
      95           0 : nsim_output_feature_enable_disable (nsim_main_t * nsm, u32 sw_if_index,
      96             :                                     int enable_disable)
      97             : {
      98             :   vnet_sw_interface_t *sw;
      99             :   vnet_hw_interface_t *hw;
     100           0 :   int rv = 0;
     101             : 
     102           0 :   if (nsm->is_configured == 0)
     103           0 :     return VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
     104             : 
     105             :   /* Utterly wrong? */
     106           0 :   if (pool_is_free_index (nsm->vnet_main->interface_main.sw_interfaces,
     107             :                           sw_if_index))
     108           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
     109             : 
     110             :   /* Not a physical port? */
     111           0 :   sw = vnet_get_sw_interface (nsm->vnet_main, sw_if_index);
     112           0 :   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
     113           0 :     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
     114             : 
     115             :   /* Add a graph arc for the input / wheel scraper node */
     116           0 :   hw = vnet_get_hw_interface (nsm->vnet_main, sw_if_index);
     117           0 :   vec_validate_init_empty (nsm->output_next_index_by_sw_if_index, sw_if_index,
     118             :                            ~0);
     119             :   /* Note: use the tx node, this pkt has already visited the output node... */
     120           0 :   nsm->output_next_index_by_sw_if_index[sw_if_index] =
     121           0 :     vlib_node_add_next (nsm->vlib_main, nsim_input_node.index,
     122           0 :                         hw->tx_node_index);
     123             : 
     124           0 :   vnet_feature_enable_disable ("interface-output", "nsim-output-feature",
     125             :                                sw_if_index, enable_disable, 0, 0);
     126           0 :   return rv;
     127             : }
     128             : 
     129             : static nsim_wheel_t *
     130           0 : nsim_wheel_alloc (nsim_main_t *nsm)
     131             : {
     132           0 :   u32 pagesize = getpagesize ();
     133             :   nsim_wheel_t *wp;
     134             : 
     135           0 :   nsm->mmap_size = sizeof (nsim_wheel_t) +
     136           0 :                    nsm->wheel_slots_per_wrk * sizeof (nsim_wheel_entry_t);
     137             : 
     138           0 :   nsm->mmap_size += pagesize - 1;
     139           0 :   nsm->mmap_size &= ~(pagesize - 1);
     140             : 
     141           0 :   wp = clib_mem_vm_alloc (nsm->mmap_size);
     142           0 :   ASSERT (wp != 0);
     143           0 :   wp->wheel_size = nsm->wheel_slots_per_wrk;
     144           0 :   wp->cursize = 0;
     145           0 :   wp->head = 0;
     146           0 :   wp->tail = 0;
     147           0 :   wp->entries = (void *) (wp + 1);
     148             : 
     149           0 :   return wp;
     150             : }
     151             : 
     152             : static int
     153           0 : nsim_configure (nsim_main_t *nsm, f64 bandwidth, f64 delay, u32 packet_size,
     154             :                 f64 drop_fraction, f64 reorder_fraction)
     155             : {
     156             :   u64 total_buffer_size_in_bytes, per_worker_buffer_size, wheel_slots_per_wrk;
     157           0 :   int i, num_workers = vlib_num_workers ();
     158           0 :   vlib_main_t *vm = nsm->vlib_main;
     159             : 
     160           0 :   if (bandwidth == 0.0)
     161           0 :     return VNET_API_ERROR_INVALID_VALUE;
     162             : 
     163           0 :   if (delay == 0.0)
     164           0 :     return VNET_API_ERROR_INVALID_VALUE_2;
     165             : 
     166           0 :   if (packet_size < 64 || packet_size > 9000)
     167           0 :     return VNET_API_ERROR_INVALID_VALUE_3;
     168             : 
     169           0 :   if (reorder_fraction > 0.0 && delay == 0.0)
     170           0 :     return VNET_API_ERROR_INVALID_VALUE_4;
     171             : 
     172             :   /* Toss the old wheel(s)... */
     173           0 :   if (nsm->is_configured)
     174             :     {
     175           0 :       for (i = 0; i < vec_len (nsm->wheel_by_thread); i++)
     176             :         {
     177           0 :           clib_mem_vm_free (nsm->wheel_by_thread[i], nsm->mmap_size);
     178           0 :           nsm->wheel_by_thread[i] = 0;
     179             :         }
     180             :     }
     181             : 
     182           0 :   nsm->delay = delay;
     183           0 :   nsm->drop_fraction = drop_fraction;
     184           0 :   nsm->reorder_fraction = reorder_fraction;
     185             : 
     186             :   /* delay in seconds, bandwidth in bits/sec */
     187           0 :   total_buffer_size_in_bytes = ((delay * bandwidth) / 8.0) + 0.5;
     188             : 
     189             :   /*
     190             :    * Work out how much buffering each worker needs, assuming decent
     191             :    * RSS behavior.
     192             :    */
     193           0 :   if (num_workers)
     194           0 :     per_worker_buffer_size = total_buffer_size_in_bytes / num_workers;
     195             :   else
     196           0 :     per_worker_buffer_size = total_buffer_size_in_bytes;
     197             : 
     198           0 :   wheel_slots_per_wrk = per_worker_buffer_size / packet_size;
     199           0 :   wheel_slots_per_wrk++;
     200             : 
     201             :   /* Save these for the show command */
     202           0 :   nsm->bandwidth = bandwidth;
     203           0 :   nsm->packet_size = packet_size;
     204           0 :   nsm->wheel_slots_per_wrk = wheel_slots_per_wrk;
     205             : 
     206           0 :   vec_validate (nsm->wheel_by_thread, num_workers);
     207             : 
     208             :   /* Initialize the output scheduler wheels */
     209           0 :   i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
     210           0 :   for (; i < num_workers + 1; i++)
     211           0 :     nsm->wheel_by_thread[i] = nsim_wheel_alloc (nsm);
     212             : 
     213           0 :   vlib_worker_thread_barrier_sync (vm);
     214             : 
     215             :   /* turn on the ring scrapers */
     216           0 :   i = (!nsm->poll_main_thread && num_workers) ? 1 : 0;
     217           0 :   for (; i < num_workers + 1; i++)
     218             :     {
     219           0 :       vlib_main_t *this_vm = vlib_get_main_by_index (i);
     220             : 
     221           0 :       vlib_node_set_state (this_vm, nsim_input_node.index,
     222             :                            VLIB_NODE_STATE_POLLING);
     223             :     }
     224             : 
     225           0 :   vlib_worker_thread_barrier_release (vm);
     226             : 
     227           0 :   nsm->is_configured = 1;
     228           0 :   return 0;
     229             : }
     230             : 
     231             : /*
     232             :  * enable or disable the cross-connect
     233             :  */
     234             : static clib_error_t *
     235           0 : nsim_cross_connect_enable_disable_command_fn (vlib_main_t * vm,
     236             :                                               unformat_input_t * input,
     237             :                                               vlib_cli_command_t * cmd)
     238             : {
     239           0 :   nsim_main_t *nsm = &nsim_main;
     240           0 :   unformat_input_t _line_input, *line_input = &_line_input;
     241           0 :   u32 sw_if_index0 = ~0;
     242           0 :   u32 sw_if_index1 = ~0;
     243           0 :   int enable_disable = 1;
     244             :   u32 tmp;
     245             :   int rv;
     246             : 
     247             :   /* Get a line of input. */
     248           0 :   if (!unformat_user (input, unformat_line_input, line_input))
     249           0 :     return 0;
     250             : 
     251           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     252             :     {
     253           0 :       if (unformat (line_input, "disable"))
     254           0 :         enable_disable = 0;
     255           0 :       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
     256             :                          nsm->vnet_main, &tmp))
     257             :         {
     258           0 :           if (sw_if_index0 == ~0)
     259           0 :             sw_if_index0 = tmp;
     260             :           else
     261           0 :             sw_if_index1 = tmp;
     262             :         }
     263             :       else
     264           0 :         break;
     265             :     }
     266             : 
     267           0 :   unformat_free (line_input);
     268             : 
     269           0 :   if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
     270           0 :     return clib_error_return (0, "Please specify two interfaces...");
     271             : 
     272           0 :   rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0,
     273             :                                           sw_if_index1, enable_disable);
     274             : 
     275           0 :   switch (rv)
     276             :     {
     277           0 :     case 0:
     278           0 :       break;
     279             : 
     280           0 :     case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
     281           0 :       return clib_error_return (0, "Not configured, please 'set nsim' first");
     282             : 
     283           0 :     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
     284           0 :       return clib_error_return
     285             :         (0, "Invalid interface, only works on physical ports");
     286             :       break;
     287             : 
     288           0 :     case VNET_API_ERROR_UNIMPLEMENTED:
     289           0 :       return clib_error_return (0,
     290             :                                 "Device driver doesn't support redirection");
     291             :       break;
     292             : 
     293           0 :     default:
     294           0 :       return clib_error_return (0, "nsim_enable_disable returned %d", rv);
     295             :     }
     296           0 :   return 0;
     297             : }
     298             : 
     299             : static clib_error_t *
     300         575 : nsim_config (vlib_main_t * vm, unformat_input_t * input)
     301             : {
     302         575 :   nsim_main_t *nsm = &nsim_main;
     303             : 
     304         575 :   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     305             :     {
     306           0 :       if (unformat (input, "poll-main-thread"))
     307             :         {
     308           0 :           nsm->poll_main_thread = 1;
     309             :         }
     310             :       else
     311             :         {
     312           0 :           return clib_error_return (0, "unknown input '%U'",
     313             :                                     format_unformat_error, input);
     314             :         }
     315             :     }
     316         575 :   return 0;
     317             : }
     318             : 
     319        1752 : VLIB_CONFIG_FUNCTION (nsim_config, "nsim");
     320             : 
     321             : /*?
     322             :  * Enable or disable network simulation cross-connect on two interfaces
     323             :  * The network simulator must have already been configured, see
     324             :  * the "nsim_configure" command.
     325             :  *
     326             :  * Place the interfaces into a bridge group, to ensure that
     327             :  * interfaces are in promiscuous mode.
     328             :  *
     329             :  * @cliexpar
     330             :  * To enable or disable network simulation cross-connect
     331             :  * @clistart
     332             :  * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0
     333             :  * nsim cross-connect enable-disable TenGigabitEthernet2/0/0 TenGigabitEthernet2/0 disable
     334             :  * @cliend
     335             :  * @cliexcmd{nsim enable-disable <intfc> <intfc> [disable]}
     336             : ?*/
     337             : /* *INDENT-OFF* */
     338       56597 : VLIB_CLI_COMMAND (nsim_enable_disable_command, static) =
     339             : {
     340             :   .path = "nsim cross-connect enable-disable",
     341             :   .short_help =
     342             :   "nsim cross-connect enable-disable <interface-name-1> "
     343             :   "<interface-name-2> [disable]",
     344             :   .function = nsim_cross_connect_enable_disable_command_fn,
     345             : };
     346             : /* *INDENT-ON* */
     347             : 
     348             : /* API message handler */
     349           0 : static void vl_api_nsim_cross_connect_enable_disable_t_handler
     350             :   (vl_api_nsim_cross_connect_enable_disable_t * mp)
     351             : {
     352             :   vl_api_nsim_cross_connect_enable_disable_reply_t *rmp;
     353           0 :   nsim_main_t *nsm = &nsim_main;
     354             :   int rv;
     355             :   u32 sw_if_index0, sw_if_index1;
     356             : 
     357           0 :   sw_if_index0 = clib_net_to_host_u32 (mp->sw_if_index0);
     358           0 :   sw_if_index1 = clib_net_to_host_u32 (mp->sw_if_index1);
     359             : 
     360           0 :   if (!vnet_sw_if_index_is_api_valid (sw_if_index0))
     361             :     {
     362           0 :       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
     363           0 :       goto bad_sw_if_index;
     364             :     }
     365           0 :   if (!vnet_sw_if_index_is_api_valid (sw_if_index1))
     366             :     {
     367           0 :       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
     368           0 :       goto bad_sw_if_index;
     369             :     }
     370             : 
     371           0 :   rv = nsim_cross_connect_enable_disable (nsm, sw_if_index0, sw_if_index1,
     372           0 :                                           (int) (mp->enable_disable));
     373             : 
     374           0 :   BAD_SW_IF_INDEX_LABEL;
     375           0 :   REPLY_MACRO (VL_API_NSIM_CROSS_CONNECT_ENABLE_DISABLE_REPLY);
     376             : }
     377             : 
     378             : /* API message handler */
     379           0 : static void vl_api_nsim_output_feature_enable_disable_t_handler
     380             :   (vl_api_nsim_output_feature_enable_disable_t * mp)
     381             : {
     382             :   vl_api_nsim_output_feature_enable_disable_reply_t *rmp;
     383           0 :   nsim_main_t *nsm = &nsim_main;
     384             :   int rv;
     385           0 :   VALIDATE_SW_IF_INDEX (mp);
     386             : 
     387           0 :   rv = nsim_output_feature_enable_disable (nsm, ntohl (mp->sw_if_index),
     388           0 :                                            (int) (mp->enable_disable));
     389             : 
     390           0 :   BAD_SW_IF_INDEX_LABEL;
     391           0 :   REPLY_MACRO (VL_API_NSIM_OUTPUT_FEATURE_ENABLE_DISABLE_REPLY);
     392             : }
     393             : 
     394             : /* API message handler */
     395             : static void
     396           0 : vl_api_nsim_configure_t_handler (vl_api_nsim_configure_t * mp)
     397             : {
     398             :   vl_api_nsim_configure_reply_t *rmp;
     399           0 :   nsim_main_t *nsm = &nsim_main;
     400           0 :   f64 delay, bandwidth, packet_size, drop_fraction = 0.0, reorder_rate = 0.0;
     401             :   u32 packets_per_drop;
     402             :   int rv;
     403             : 
     404           0 :   delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
     405           0 :   bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
     406           0 :   packet_size = (f64) (ntohl (mp->average_packet_size));
     407             : 
     408           0 :   packets_per_drop = ntohl (mp->packets_per_drop);
     409           0 :   if (packets_per_drop > 0)
     410           0 :     drop_fraction = 1.0 / (f64) (packets_per_drop);
     411             : 
     412           0 :   rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
     413             :                        reorder_rate);
     414             : 
     415           0 :   REPLY_MACRO (VL_API_NSIM_CONFIGURE_REPLY);
     416             : }
     417             : 
     418             : static void
     419           0 : vl_api_nsim_configure2_t_handler (vl_api_nsim_configure2_t * mp)
     420             : {
     421             :   vl_api_nsim_configure_reply_t *rmp;
     422           0 :   nsim_main_t *nsm = &nsim_main;
     423           0 :   f64 delay, bandwidth, packet_size, drop_fraction = 0.0, reorder_rate = 0.0;
     424             :   u32 packets_per_drop, packets_per_reorder;
     425             :   int rv;
     426             : 
     427           0 :   delay = ((f64) (ntohl (mp->delay_in_usec))) * 1e-6;
     428           0 :   bandwidth = (f64) (clib_net_to_host_u64 (mp->bandwidth_in_bits_per_second));
     429           0 :   packet_size = (f64) (ntohl (mp->average_packet_size));
     430             : 
     431           0 :   packets_per_drop = ntohl (mp->packets_per_drop);
     432           0 :   if (packets_per_drop > 0)
     433           0 :     drop_fraction = 1.0 / (f64) (packets_per_drop);
     434             : 
     435           0 :   packets_per_reorder = ntohl (mp->packets_per_reorder);
     436           0 :   if (packets_per_reorder > 0)
     437           0 :     reorder_rate = 1.0 / (f64) packets_per_reorder;
     438             : 
     439           0 :   rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
     440             :                        reorder_rate);
     441             : 
     442           0 :   REPLY_MACRO (VL_API_NSIM_CONFIGURE2_REPLY);
     443             : }
     444             : 
     445             : 
     446             : /*
     447             :  * enable or disable the output_feature
     448             :  */
     449             : static clib_error_t *
     450           0 : nsim_output_feature_enable_disable_command_fn (vlib_main_t * vm,
     451             :                                                unformat_input_t * input,
     452             :                                                vlib_cli_command_t * cmd)
     453             : {
     454           0 :   nsim_main_t *nsm = &nsim_main;
     455           0 :   unformat_input_t _line_input, *line_input = &_line_input;
     456           0 :   u32 sw_if_index = ~0;
     457           0 :   int enable_disable = 1;
     458             :   int rv;
     459             : 
     460             :   /* Get a line of input. */
     461           0 :   if (!unformat_user (input, unformat_line_input, line_input))
     462           0 :     return 0;
     463             : 
     464           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     465             :     {
     466           0 :       if (unformat (line_input, "disable"))
     467           0 :         enable_disable = 0;
     468           0 :       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
     469             :                          nsm->vnet_main, &sw_if_index))
     470             :         ;
     471             :       else
     472             :         {
     473           0 :           clib_error_t *error = clib_error_return (0, "unknown input `%U'",
     474             :                                                    format_unformat_error,
     475             :                                                    line_input);
     476           0 :           unformat_free (line_input);
     477           0 :           return error;
     478             :         }
     479             :     }
     480             : 
     481           0 :   unformat_free (line_input);
     482             : 
     483           0 :   if (sw_if_index == ~0)
     484           0 :     return clib_error_return (0, "Please specify one interface...");
     485             : 
     486           0 :   rv = nsim_output_feature_enable_disable (nsm, sw_if_index, enable_disable);
     487             : 
     488           0 :   switch (rv)
     489             :     {
     490           0 :     case 0:
     491           0 :       break;
     492             : 
     493           0 :     case VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE:
     494           0 :       return clib_error_return (0, "Not configured, please 'set nsim' first");
     495             : 
     496           0 :     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
     497           0 :       return clib_error_return
     498             :         (0, "Invalid interface, only works on physical ports");
     499             :       break;
     500             : 
     501           0 :     case VNET_API_ERROR_UNIMPLEMENTED:
     502           0 :       return clib_error_return (0,
     503             :                                 "Device driver doesn't support redirection");
     504             :       break;
     505             : 
     506           0 :     default:
     507           0 :       return clib_error_return
     508             :         (0, "nsim_output_feature_enable_disable returned %d", rv);
     509             :     }
     510           0 :   return 0;
     511             : }
     512             : 
     513             : /*?
     514             :  * Enable or disable network simulation output feature on an interface
     515             :  * The network simulator must have already been configured, see
     516             :  * the "nsim_configure" command.
     517             :  *
     518             :  * @cliexpar
     519             :  * To enable or disable network simulation output feature
     520             :  * @clistart
     521             :  * nsim output-feature enable-disable TenGigabitEthernet2/0/0
     522             :  * nsim output-feature enable-disable TenGigabitEthernet2/0/0 disable
     523             :  * @cliend
     524             :  * @cliexcmd{nsim output-feature enable-disable <intfc> [disable]}
     525             : ?*/
     526             : /* *INDENT-OFF* */
     527       56597 : VLIB_CLI_COMMAND (nsim_output_feature_enable_disable_command, static) =
     528             : {
     529             :   .path = "nsim output-feature enable-disable",
     530             :   .short_help =
     531             :   "nsim output-feature enable-disable <interface-name> [disable]",
     532             :   .function = nsim_output_feature_enable_disable_command_fn,
     533             : };
     534             : /* *INDENT-ON* */
     535             : 
     536             : #include <nsim/nsim.api.c>
     537             : static clib_error_t *
     538         575 : nsim_init (vlib_main_t * vm)
     539             : {
     540         575 :   nsim_main_t *nsm = &nsim_main;
     541             : 
     542         575 :   nsm->vlib_main = vm;
     543         575 :   nsm->vnet_main = vnet_get_main ();
     544             : 
     545             :   /* Ask for a correctly-sized block of API message decode slots */
     546         575 :   nsm->msg_id_base = setup_message_id_table ();
     547         575 :   nsm->arc_index = nsm->vnet_main->interface_main.output_feature_arc_index;
     548         575 :   return 0;
     549             : }
     550             : 
     551        1151 : VLIB_INIT_FUNCTION (nsim_init);
     552             : 
     553             : /* *INDENT-OFF* */
     554       26495 : VNET_FEATURE_INIT (nsim, static) =
     555             : {
     556             :   .arc_name = "device-input",
     557             :   .node_name = "nsim",
     558             :   .runs_before = VNET_FEATURES ("ethernet-input"),
     559             : };
     560             : /* *INDENT-ON */
     561             : 
     562             : /* *INDENT-OFF* */
     563       26495 : VNET_FEATURE_INIT (nsim_feature, static) = {
     564             :   .arc_name = "interface-output",
     565             :   .node_name = "nsim-output-feature",
     566             :   .runs_before = VNET_FEATURES ("interface-output-arc-end"),
     567             : };
     568             : /* *INDENT-ON */
     569             : 
     570             : /* *INDENT-OFF* */
     571             : VLIB_PLUGIN_REGISTER () =
     572             : {
     573             :   .version = VPP_BUILD_VER,
     574             :   .description = "Network Delay Simulator",
     575             : };
     576             : /* *INDENT-ON* */
     577             : 
     578             : static uword
     579           0 : unformat_delay (unformat_input_t * input, va_list * args)
     580             : {
     581           0 :   f64 *result = va_arg (*args, f64 *);
     582             :   f64 tmp;
     583             : 
     584           0 :   if (unformat (input, "%f us", &tmp))
     585           0 :     *result = tmp * 1e-6;
     586           0 :   else if (unformat (input, "%f ms", &tmp))
     587           0 :     *result = tmp * 1e-3;
     588           0 :   else if (unformat (input, "%f sec", &tmp))
     589           0 :     *result = tmp;
     590             :   else
     591           0 :     return 0;
     592             : 
     593           0 :   return 1;
     594             : }
     595             : 
     596             : static uword
     597           0 : unformat_bandwidth (unformat_input_t * input, va_list * args)
     598             : {
     599           0 :   f64 *result = va_arg (*args, f64 *);
     600             :   f64 tmp;
     601             : 
     602           0 :   if (unformat (input, "%f gbit", &tmp))
     603           0 :     *result = tmp * 1e9;
     604           0 :   else if (unformat (input, "%f gbyte", &tmp))
     605           0 :     *result = tmp * 8e9;
     606           0 :   else if (unformat (input, "%f gbps", &tmp))
     607           0 :     *result = tmp * 1e9;
     608           0 :   else if (unformat (input, "%f mbps", &tmp))
     609           0 :     *result = tmp * 1e6;
     610           0 :   else if (unformat (input, "%f kbps", &tmp))
     611           0 :     *result = tmp * 1e3;
     612           0 :   else if (unformat (input, "%f bps", &tmp))
     613           0 :     *result = tmp;
     614             :   else
     615           0 :     return 0;
     616           0 :   return 1;
     617             : }
     618             : 
     619             : static u8 *
     620           0 : format_delay (u8 *s, va_list *args)
     621             : {
     622           0 :   f64 delay = va_arg (*args, f64);
     623             : 
     624           0 :   if (delay < 1e-3)
     625           0 :     s = format (s, "%.1f us", delay * 1e6);
     626           0 :   else if (delay < 1)
     627           0 :     s = format (s, "%.1f ms", delay * 1e3);
     628             :   else
     629           0 :     s = format (s, "%f sec", delay);
     630             : 
     631           0 :   return s;
     632             : }
     633             : 
     634             : static u8 *
     635           0 : format_bandwidth (u8 *s, va_list *args)
     636             : {
     637           0 :   f64 bandwidth = va_arg (*args, f64);
     638             : 
     639           0 :   if (bandwidth >= 1e9)
     640           0 :     s = format (s, "%.1f gbps", bandwidth / 1e9);
     641           0 :   else if (bandwidth >= 1e6)
     642           0 :     s = format (s, "%.1f mbps", bandwidth / 1e6);
     643           0 :   else if (bandwidth >= 1e3)
     644           0 :     s = format (s, "%.1f kbps", bandwidth / 1e3);
     645             :   else
     646           0 :     s = format (s, "%f bps", bandwidth);
     647             : 
     648           0 :   return s;
     649             : }
     650             : 
     651             : static u8 *
     652           0 : format_nsim_config (u8 * s, va_list * args)
     653             : {
     654           0 :   int verbose = va_arg (*args, int);
     655           0 :   nsim_main_t *nsm = &nsim_main;
     656             : 
     657           0 :   s = format (s, "configuration\n");
     658           0 :   s = format (s, " delay: %U\n", format_delay, nsm->delay);
     659           0 :   if (nsm->drop_fraction)
     660           0 :     s = format (s, " drop fraction: %.5f\n", nsm->drop_fraction);
     661             :   else
     662           0 :     s = format (s, " drop fraction: 0\n");
     663           0 :   if (nsm->reorder_fraction)
     664           0 :     s = format (s, " reorder fraction: %.5f\n", nsm->reorder_fraction);
     665             :   else
     666           0 :     s = format (s, " reorder fraction: 0\n");
     667           0 :   s = format (s, " packet size: %u\n", nsm->packet_size);
     668           0 :   s = format (s, " worker wheel size: %u\n", nsm->wheel_slots_per_wrk);
     669           0 :   s = format (s, " throughput: %U\n", format_bandwidth, nsm->bandwidth);
     670             : 
     671           0 :   if (verbose)
     672             :     {
     673           0 :       s = format (s, " poll main thread: %u\n", nsm->poll_main_thread);
     674           0 :       s = format (s, " memory: %U bytes per thread %U bytes total\n",
     675             :                   format_memory_size, nsm->mmap_size, format_memory_size,
     676           0 :                   nsm->mmap_size * vlib_num_workers ());
     677             :     }
     678             : 
     679           0 :   s = format (s, "\n");
     680             : 
     681           0 :   if (nsm->sw_if_index0 != 0)
     682             :     {
     683           0 :       s = format (s, "cross-connect\n %U and %U\n",
     684             :                   format_vnet_sw_if_index_name, nsm->vnet_main,
     685             :                   nsm->sw_if_index0, format_vnet_sw_if_index_name,
     686             :                   nsm->vnet_main, nsm->sw_if_index1);
     687             :     }
     688           0 :   else if (vec_len (nsm->output_next_index_by_sw_if_index))
     689           0 :     {
     690             :       int i;
     691           0 :       s = format (s, "output feature arcs to:\n");
     692           0 :       for (i = 0; i < vec_len (nsm->output_next_index_by_sw_if_index); i++)
     693             :         {
     694           0 :           if (nsm->output_next_index_by_sw_if_index[i] != ~0)
     695           0 :             s = format (s, " %U %u\n", format_vnet_sw_if_index_name,
     696             :                         nsm->vnet_main, i, i);
     697             :         }
     698             :     }
     699             :   else
     700             :     {
     701           0 :       s = format (s, " nsim not enabled\n");
     702             :     }
     703             : 
     704           0 :   return s;
     705             : }
     706             : 
     707             : static clib_error_t *
     708           0 : set_nsim_command_fn (vlib_main_t * vm,
     709             :                      unformat_input_t * input, vlib_cli_command_t * cmd)
     710             : {
     711           0 :   f64 drop_fraction = 0.0, reorder_fraction = 0.0, delay, bandwidth;
     712           0 :   u32 packets_per_drop, packets_per_reorder, packet_size = 1500;
     713           0 :   nsim_main_t *nsm = &nsim_main;
     714             :   int rv;
     715             : 
     716           0 :   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     717             :     {
     718           0 :       if (unformat (input, "delay %U", unformat_delay, &delay))
     719             :         ;
     720           0 :       else if (unformat (input, "bandwidth %U", unformat_bandwidth,
     721             :                          &bandwidth))
     722             :         ;
     723           0 :       else if (unformat (input, "packet-size %u", &packet_size))
     724             :         ;
     725           0 :       else if (unformat (input, "packets-per-drop %d", &packets_per_drop))
     726             :         {
     727           0 :           if (packets_per_drop > 0)
     728           0 :             drop_fraction = 1.0 / ((f64) packets_per_drop);
     729             :         }
     730           0 :       else if (unformat (input, "packets-per-reorder %d",
     731             :                          &packets_per_reorder))
     732             :         {
     733           0 :           if (packets_per_reorder > 0)
     734           0 :             reorder_fraction = 1.0 / ((f64) packets_per_reorder);
     735             :         }
     736           0 :       else if (unformat (input, "drop-fraction %f", &drop_fraction))
     737             :         {
     738           0 :           if (drop_fraction < 0.0 || drop_fraction > 1.0)
     739           0 :             return clib_error_return
     740             :               (0, "drop fraction must be between zero and 1");
     741             :         }
     742           0 :       else if (unformat (input, "reorder-fraction %f", &reorder_fraction))
     743             :         {
     744           0 :           if (reorder_fraction < 0.0 || reorder_fraction > 1.0)
     745           0 :             return clib_error_return
     746             :               (0, "reorder fraction must be between zero and 1");
     747             :         }
     748           0 :       else if (unformat (input, "poll-main-thread"))
     749           0 :         nsm->poll_main_thread = 1;
     750             :       else
     751           0 :         break;
     752             :     }
     753             : 
     754           0 :   rv = nsim_configure (nsm, bandwidth, delay, packet_size, drop_fraction,
     755             :                        reorder_fraction);
     756             : 
     757           0 :   switch (rv)
     758             :     {
     759           0 :     case VNET_API_ERROR_INVALID_VALUE:
     760           0 :       return clib_error_return (0, "invalid bandwidth %.2f", bandwidth);
     761             : 
     762           0 :     case VNET_API_ERROR_INVALID_VALUE_2:
     763           0 :       return clib_error_return (0, "invalid delay %.2f", delay);
     764             : 
     765           0 :     case VNET_API_ERROR_INVALID_VALUE_3:
     766           0 :       return clib_error_return (0, "invalid packet size %.2f", packet_size);
     767             : 
     768           0 :     case VNET_API_ERROR_INVALID_VALUE_4:
     769           0 :       return clib_error_return (0, "invalid reorder fraction %.3f for "
     770             :                                 "delay %.2f", reorder_fraction, delay);
     771             : 
     772           0 :     default:
     773           0 :       return clib_error_return (0, "error %d", rv);
     774             : 
     775           0 :     case 0:
     776           0 :       break;
     777             :     }
     778             : 
     779           0 :   vlib_cli_output (vm, "%U", format_nsim_config, 1);
     780             : 
     781           0 :   return 0;
     782             : }
     783             : 
     784             : /*?
     785             :  * Configure the network simulation cross-connect
     786             :  * Once the simulator is configured, use the "nsim enable-disable" command
     787             :  * to set up a cross-connect with the supplied delay characteristics.
     788             :  *
     789             :  * The cross connect configuration may be changed without restarting vpp
     790             :  * but it is good practice to shut down the interfaces.
     791             :  *
     792             :  * @cliexpar
     793             :  * To configure the network delay simulator:
     794             :  * @clistart
     795             :  * set nsim delay 10.0 ms bandwidth 5.5 gbit packet-size 128
     796             :  *
     797             :  * @cliend
     798             :  * @cliexcmd{set nsim delay <nn> bandwidth <bb> packet-size <nn>}
     799             : ?*/
     800             : /* *INDENT-OFF* */
     801       56597 : VLIB_CLI_COMMAND (set_nsim_command, static) =
     802             : {
     803             :   .path = "set nsim",
     804             :   .short_help = "set nsim delay <time> bandwidth <bps> packet-size <nbytes>\n"
     805             :   "    [packets-per-drop <nn>][drop-fraction <f64: 0.0 - 1.0>]",
     806             :   .function = set_nsim_command_fn,
     807             : };
     808             : /* *INDENT-ON*/
     809             : 
     810             : 
     811             : static clib_error_t *
     812           0 : show_nsim_command_fn (vlib_main_t * vm,
     813             :                       unformat_input_t * input, vlib_cli_command_t * cmd)
     814             : {
     815           0 :   nsim_main_t *nsm = &nsim_main;
     816           0 :   int verbose = 0;
     817             : 
     818           0 :   if (nsm->is_configured == 0)
     819           0 :     return clib_error_return (0, "Network simulator not configured");
     820             : 
     821           0 :   if (unformat (input, "verbose"))
     822           0 :     verbose = 1;
     823             : 
     824           0 :   vlib_cli_output (vm, "%U", format_nsim_config, verbose);
     825             : 
     826           0 :   return 0;
     827             : }
     828             : 
     829             : /*?
     830             :  * Display state info for the network delay simulator.
     831             :  *
     832             :  * @cliexpar
     833             :  * To display the state of the network simulator
     834             :  * @clistart
     835             :  * show nsim verbose
     836             :  * Network simulator cross-connects TenGigabitEthernet2/0/0 and TenGigabitEthernet2/0/1
     837             :  * ...inserting link delay of 10.00 ms, 20.00 ms round-trip
     838             :  *  Configured bandwidth: 10.10 gbit/sec
     839             :  *  Configured packet size: 128
     840             :  *  Sim uses 157814784 bytes total
     841             :  * @cliend
     842             :  * @cliexcmd{show nsim}
     843             : ?*/
     844             : 
     845             : /* *INDENT-OFF* */
     846       56597 : VLIB_CLI_COMMAND (show_nsim_command, static) =
     847             : {
     848             :   .path = "show nsim",
     849             :   .short_help = "Display network delay simulator configuration",
     850             :   .function = show_nsim_command_fn,
     851             : };
     852             : /* *INDENT-ON* */
     853             : 
     854             : /*
     855             :  * fd.io coding-style-patch-verification: ON
     856             :  *
     857             :  * Local Variables:
     858             :  * eval: (c-set-style "gnu")
     859             :  * End:
     860             :  */

Generated by: LCOV version 1.14