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_cross_connect_enable_disable_reply) \
64 _(nsim_output_feature_enable_disable_reply) \
65 _(nsim_configure_reply)
68 static void vl_api_##n##_t_handler \
69 (vl_api_##n##_t * mp) \
71 vat_main_t * vam = nsim_test_main.vat_main; \
72 i32 retval = ntohl(mp->retval); \
73 if (vam->async_mode) { \
74 vam->async_errors += (retval < 0); \
76 vam->retval = retval; \
77 vam->result_ready = 1; \
80 foreach_standard_reply_retval_handler;
84 * Table of message reply handlers, must include boilerplate handlers
87 #define foreach_vpe_api_reply_msg \
88 _(NSIM_CROSS_CONNECT_ENABLE_DISABLE_REPLY, \
89 nsim_cross_connect_enable_disable_reply) \
90 _(NSIM_OUTPUT_FEATURE_ENABLE_DISABLE_REPLY, \
91 nsim_output_feature_enable_disable_reply) \
92 _(NSIM_CONFIGURE_REPLY, nsim_configure_reply)
95 api_nsim_cross_connect_enable_disable (vat_main_t * vam)
97 unformat_input_t *i = vam->input;
98 int enable_disable = 1;
99 u32 sw_if_index0 = ~0;
100 u32 sw_if_index1 = ~0;
102 vl_api_nsim_cross_connect_enable_disable_t *mp;
105 /* Parse args required to build the message */
106 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
108 if (unformat (i, "%U", unformat_sw_if_index, vam, &tmp))
110 if (sw_if_index0 == ~0)
115 else if (unformat (i, "sw_if_index %d", &tmp))
117 if (sw_if_index0 == ~0)
122 else if (unformat (i, "disable"))
128 if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
130 errmsg ("missing interface name / explicit sw_if_index number \n");
134 /* Construct the API message */
135 M (NSIM_CROSS_CONNECT_ENABLE_DISABLE, mp);
136 mp->sw_if_index0 = ntohl (sw_if_index0);
137 mp->sw_if_index1 = ntohl (sw_if_index1);
138 mp->enable_disable = enable_disable;
143 /* Wait for a reply... */
149 api_nsim_output_feature_enable_disable (vat_main_t * vam)
151 unformat_input_t *i = vam->input;
152 int enable_disable = 1;
153 u32 sw_if_index = ~0;
154 vl_api_nsim_output_feature_enable_disable_t *mp;
157 /* Parse args required to build the message */
158 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
160 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
162 else if (unformat (i, "sw_if_index %d", &sw_if_index))
164 else if (unformat (i, "disable"))
170 if (sw_if_index == ~0)
172 errmsg ("missing interface name / explicit sw_if_index number \n");
176 /* Construct the API message */
177 M (NSIM_OUTPUT_FEATURE_ENABLE_DISABLE, mp);
178 mp->sw_if_index = ntohl (sw_if_index);
179 mp->enable_disable = enable_disable;
184 /* Wait for a reply... */
190 unformat_delay (unformat_input_t * input, va_list * args)
192 f64 *result = va_arg (*args, f64 *);
195 if (unformat (input, "%f us", &tmp))
196 *result = tmp * 1e-6;
197 else if (unformat (input, "%f ms", &tmp))
198 *result = tmp * 1e-3;
199 else if (unformat (input, "%f sec", &tmp))
208 unformat_bandwidth (unformat_input_t * input, va_list * args)
210 f64 *result = va_arg (*args, f64 *);
213 if (unformat (input, "%f gbit", &tmp))
215 else if (unformat (input, "%f gbyte", &tmp))
223 api_nsim_configure (vat_main_t * vam)
225 vl_api_nsim_configure_t *mp;
226 unformat_input_t *i = vam->input;
227 f64 delay = 0.0, bandwidth = 0.0;
228 f64 packet_size = 1500.0;
229 u32 num_workers = vlib_num_workers ();
230 u32 packets_per_drop = 0;
233 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
235 if (unformat (i, "delay %U", unformat_delay, &delay))
237 else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
239 else if (unformat (i, "packet-size %f", &packet_size))
241 else if (unformat (i, "packets-per-drop %u", &packets_per_drop))
247 if (delay == 0.0 || bandwidth == 0.0)
249 errmsg ("must specify delay and bandwidth");
253 /* Construct the API message */
254 M (NSIM_CONFIGURE, mp);
255 mp->delay_in_usec = (u32) (delay * 1e6);
256 mp->delay_in_usec = ntohl (mp->delay_in_usec);
257 mp->average_packet_size = (u32) (packet_size);
258 mp->average_packet_size = ntohl (mp->average_packet_size);
259 mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
260 mp->bandwidth_in_bits_per_second =
261 clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
262 mp->packets_per_drop = ntohl (packets_per_drop);
267 /* Wait for a reply... */
273 * List of messages that the api test plugin sends,
274 * and that the data plane plugin processes
276 #define foreach_vpe_api_msg \
277 _(nsim_cross_connect_enable_disable, \
278 "[<intfc0> | sw_if_index <swif0>] [<intfc1> | sw_if_index <swif1>] [disable]") \
279 _(nsim_output_feature_enable_disable,"[<intfc> | sw_if_index <nnn> [disable]") \
280 _(nsim_configure, "delay <time> bandwidth <bw> [packet-size <nn>]" \
281 "[packets-per-drop <nnnn>]")
284 nsim_api_hookup (vat_main_t * vam)
286 nsim_test_main_t *sm = &nsim_test_main;
287 /* Hook up handlers for replies from the data plane plug-in */
289 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
291 vl_api_##n##_t_handler, \
293 vl_api_##n##_t_endian, \
294 vl_api_##n##_t_print, \
295 sizeof(vl_api_##n##_t), 1);
296 foreach_vpe_api_reply_msg;
299 /* API messages we can send */
300 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
305 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
311 vat_plugin_register (vat_main_t * vam)
313 nsim_test_main_t *sm = &nsim_test_main;
318 /* Ask the vpp engine for the first assigned message-id */
319 name = format (0, "nsim_%08x%c", api_version, 0);
320 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
322 if (sm->msg_id_base != (u16) ~ 0)
323 nsim_api_hookup (vam);
331 * fd.io coding-style-patch-verification: ON
334 * eval: (c-set-style "gnu")