2 * nsim.c - skeleton vpp-api-test 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.
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
22 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
24 /* Declare message IDs */
25 #include <nsim/nsim_msg_enum.h>
27 /* define message structures */
29 #include <nsim/nsim_all_api_h.h>
32 /* declare message handlers for each api */
34 #define vl_endianfun /* define message structures */
35 #include <nsim/nsim_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
41 #include <nsim/nsim_all_api_h.h>
44 /* Get the API version number. */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <nsim/nsim_all_api_h.h>
52 /* API message ID base */
57 nsim_test_main_t nsim_test_main;
59 #define __plugin_msg_base nsim_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
62 #define foreach_standard_reply_retval_handler \
63 _(nsim_enable_disable_reply) \
64 _(nsim_configure_reply)
67 static void vl_api_##n##_t_handler \
68 (vl_api_##n##_t * mp) \
70 vat_main_t * vam = nsim_test_main.vat_main; \
71 i32 retval = ntohl(mp->retval); \
72 if (vam->async_mode) { \
73 vam->async_errors += (retval < 0); \
75 vam->retval = retval; \
76 vam->result_ready = 1; \
79 foreach_standard_reply_retval_handler;
83 * Table of message reply handlers, must include boilerplate handlers
86 #define foreach_vpe_api_reply_msg \
87 _(NSIM_ENABLE_DISABLE_REPLY, nsim_enable_disable_reply) \
88 _(NSIM_CONFIGURE_REPLY, nsim_configure_reply)
91 api_nsim_enable_disable (vat_main_t * vam)
93 unformat_input_t *i = vam->input;
94 int enable_disable = 1;
95 u32 sw_if_index0 = ~0;
96 u32 sw_if_index1 = ~0;
98 vl_api_nsim_enable_disable_t *mp;
101 /* Parse args required to build the message */
102 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
104 if (unformat (i, "%U", unformat_sw_if_index, vam, &tmp))
106 if (sw_if_index0 == ~0)
111 else if (unformat (i, "sw_if_index %d", &tmp))
113 if (sw_if_index0 == ~0)
118 else if (unformat (i, "disable"))
124 if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
126 errmsg ("missing interface name / explicit sw_if_index number \n");
130 /* Construct the API message */
131 M (NSIM_ENABLE_DISABLE, mp);
132 mp->sw_if_index0 = ntohl (sw_if_index0);
133 mp->sw_if_index1 = ntohl (sw_if_index1);
134 mp->enable_disable = enable_disable;
139 /* Wait for a reply... */
145 unformat_delay (unformat_input_t * input, va_list * args)
147 f64 *result = va_arg (*args, f64 *);
150 if (unformat (input, "%f us", &tmp))
151 *result = tmp * 1e-6;
152 else if (unformat (input, "%f ms", &tmp))
153 *result = tmp * 1e-3;
154 else if (unformat (input, "%f sec", &tmp))
163 unformat_bandwidth (unformat_input_t * input, va_list * args)
165 f64 *result = va_arg (*args, f64 *);
168 if (unformat (input, "%f gbit", &tmp))
170 else if (unformat (input, "%f gbyte", &tmp))
178 api_nsim_configure (vat_main_t * vam)
180 vl_api_nsim_configure_t *mp;
181 unformat_input_t *i = vam->input;
182 f64 delay = 0.0, bandwidth = 0.0;
183 f64 packet_size = 1500.0;
184 u32 num_workers = vlib_num_workers ();
187 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
189 if (unformat (i, "delay %U", unformat_delay, &delay))
191 else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
193 else if (unformat (i, "packet-size %f", &packet_size))
199 if (delay == 0.0 || bandwidth == 0.0)
201 errmsg ("must specify delay and bandwidth");
205 /* Construct the API message */
206 M (NSIM_CONFIGURE, mp);
207 mp->delay_in_usec = (u32) (delay * 1e6);
208 mp->delay_in_usec = ntohl (mp->delay_in_usec);
209 mp->average_packet_size = (u32) (packet_size);
210 mp->average_packet_size = ntohl (mp->average_packet_size);
211 mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
212 mp->bandwidth_in_bits_per_second =
213 clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
218 /* Wait for a reply... */
224 * List of messages that the api test plugin sends,
225 * and that the data plane plugin processes
227 #define foreach_vpe_api_msg \
228 _(nsim_enable_disable, \
229 "[<intfc0> | sw_if_index <swif0>] [<intfc1> | sw_if_index <swif1>] [disable]") \
230 _(nsim_configure, "delay <time> bandwidth <bw> [packet-size <nn>]")
233 nsim_api_hookup (vat_main_t * vam)
235 nsim_test_main_t *sm = &nsim_test_main;
236 /* Hook up handlers for replies from the data plane plug-in */
238 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
240 vl_api_##n##_t_handler, \
242 vl_api_##n##_t_endian, \
243 vl_api_##n##_t_print, \
244 sizeof(vl_api_##n##_t), 1);
245 foreach_vpe_api_reply_msg;
248 /* API messages we can send */
249 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
254 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
260 vat_plugin_register (vat_main_t * vam)
262 nsim_test_main_t *sm = &nsim_test_main;
267 /* Ask the vpp engine for the first assigned message-id */
268 name = format (0, "nsim_%08x%c", api_version, 0);
269 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
271 if (sm->msg_id_base != (u16) ~ 0)
272 nsim_api_hookup (vam);
280 * fd.io coding-style-patch-verification: ON
283 * eval: (c-set-style "gnu")