2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 *------------------------------------------------------------------
17 * trace_test.c - test harness for trace plugin
18 *------------------------------------------------------------------
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
27 /* Declare message IDs */
28 #include <ioam/lib-trace/trace_msg_enum.h>
30 /* define message structures */
32 #include <ioam/lib-trace/trace_all_api_h.h>
35 /* declare message handlers for each api */
37 #define vl_endianfun /* define message structures */
38 #include <ioam/lib-trace/trace_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
44 #include <ioam/lib-trace/trace_all_api_h.h>
47 /* Get the API version number. */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <ioam/lib-trace/trace_all_api_h.h>
55 /* API message ID base */
60 trace_test_main_t trace_test_main;
62 #define foreach_standard_reply_retval_handler \
63 _(trace_profile_add_reply) \
64 _(trace_profile_del_reply)
66 #define foreach_custom_reply_handler \
67 _(trace_profile_show_config_reply, \
70 errmsg(" Trace Type : 0x%x (%d)\n",mp->trace_type, mp->trace_type); \
71 errmsg(" Trace timestamp precision : %d \n",mp->trace_tsp); \
72 errmsg(" Node Id : 0x%x (%d)\n",htonl(mp->node_id), htonl(mp->node_id)); \
73 errmsg(" App Data : 0x%x (%d)\n",htonl(mp->app_data), htonl(mp->app_data)); \
75 else errmsg("No valid trace profile configuration found\n");)
77 static void vl_api_##n##_t_handler \
78 (vl_api_##n##_t * mp) \
80 vat_main_t * vam = trace_test_main.vat_main; \
81 i32 retval = ntohl(mp->retval); \
82 if (vam->async_mode) { \
83 vam->async_errors += (retval < 0); \
85 vam->retval = retval; \
86 vam->result_ready = 1; \
89 foreach_standard_reply_retval_handler;
93 static void vl_api_##n##_t_handler \
94 (vl_api_##n##_t * mp) \
96 vat_main_t * vam = trace_test_main.vat_main; \
97 i32 retval = ntohl(mp->retval); \
98 if (vam->async_mode) { \
99 vam->async_errors += (retval < 0); \
101 vam->retval = retval; \
102 vam->result_ready = 1; \
104 if(retval>=0)do{body;} while(0); \
105 else errmsg("Error, retval: %d",retval); \
107 foreach_custom_reply_handler;
110 * Table of message reply handlers, must include boilerplate handlers
113 #define foreach_vpe_api_reply_msg \
114 _(TRACE_PROFILE_ADD_REPLY, trace_profile_add_reply) \
115 _(TRACE_PROFILE_DEL_REPLY, trace_profile_del_reply) \
116 _(TRACE_PROFILE_SHOW_CONFIG_REPLY, trace_profile_show_config_reply)
119 /* M: construct, but don't yet send a message */
123 vam->result_ready = 0; \
124 mp = vl_msg_api_alloc(sizeof(*mp)); \
125 memset (mp, 0, sizeof (*mp)); \
126 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
127 mp->client_index = vam->my_client_index; \
132 vam->result_ready = 0; \
133 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
134 memset (mp, 0, sizeof (*mp)); \
135 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
136 mp->client_index = vam->my_client_index; \
139 /* S: send a message */
140 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
142 /* W: wait for results, with timeout */
145 timeout = vat_time_now (vam) + 1.0; \
147 while (vat_time_now (vam) < timeout) { \
148 if (vam->result_ready == 1) { \
149 return (vam->retval); \
157 api_trace_profile_add (vat_main_t * vam)
159 trace_test_main_t *sm = &trace_test_main;
160 unformat_input_t *input = vam->input;
161 vl_api_trace_profile_add_t *mp;
170 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
172 if (unformat (input, "trace-type 0x%x", &trace_type))
174 else if (unformat (input, "trace-elts %d", &num_elts))
176 else if (unformat (input, "trace-tsp %d", &trace_tsp))
178 else if (unformat (input, "node-id 0x%x", &node_id))
180 else if (unformat (input, "app-data 0x%x", &app_data))
188 M (TRACE_PROFILE_ADD, trace_profile_add);
190 mp->trace_type = trace_type;
191 mp->trace_tsp = trace_tsp;
192 mp->node_id = htonl (node_id);
193 mp->app_data = htonl (app_data);
194 mp->num_elts = num_elts;
205 api_trace_profile_del (vat_main_t * vam)
207 trace_test_main_t *sm = &trace_test_main;
208 vl_api_trace_profile_del_t *mp;
211 M (TRACE_PROFILE_DEL, trace_profile_del);
218 api_trace_profile_show_config (vat_main_t * vam)
220 trace_test_main_t *sm = &trace_test_main;
221 vl_api_trace_profile_show_config_t *mp;
223 M (TRACE_PROFILE_SHOW_CONFIG, trace_profile_show_config);
230 * List of messages that the api test plugin sends,
231 * and that the data plane plugin processes
233 #define foreach_vpe_api_msg \
234 _(trace_profile_add, ""\
235 "trace-type <0x1f|0x3|0x9|0x11|0x19> trace-elts <nn> trace-tsp <0|1|2|3> node-id <node id in hex> app-data <app_data in hex>") \
236 _(trace_profile_del, "[id <nn>]") \
237 _(trace_profile_show_config, "[id <nn>]")
241 vat_api_hookup (vat_main_t * vam)
243 trace_test_main_t *sm = &trace_test_main;
244 /* Hook up handlers for replies from the data plane plug-in */
246 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
248 vl_api_##n##_t_handler, \
250 vl_api_##n##_t_endian, \
251 vl_api_##n##_t_print, \
252 sizeof(vl_api_##n##_t), 1);
253 foreach_vpe_api_reply_msg;
256 /* API messages we can send */
257 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
262 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
268 vat_plugin_register (vat_main_t * vam)
270 trace_test_main_t *sm = &trace_test_main;
275 name = format (0, "ioam_trace_%08x%c", api_version, 0);
276 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
278 if (sm->msg_id_base != (u16) ~ 0)
279 vat_api_hookup (vam);
287 * fd.io coding-style-patch-verification: ON
290 * eval: (c-set-style "gnu")