ioam:trace plugin - trace profile read API
[vpp.git] / plugins / ioam-plugin / ioam / lib-trace / trace_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  *------------------------------------------------------------------
17  * trace_test.c - test harness for trace plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26 #include <vnet/ip/ip6_hop_by_hop.h>
27
28 /* Declare message IDs */
29 #include <ioam/lib-trace/trace_msg_enum.h>
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <ioam/lib-trace/trace_all_api_h.h>
34 #undef vl_typedefs
35
36 /* declare message handlers for each api */
37
38 #define vl_endianfun            /* define message structures */
39 #include <ioam/lib-trace/trace_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...)
44 #define vl_printfun
45 #include <ioam/lib-trace/trace_all_api_h.h>
46 #undef vl_printfun
47
48 /* Get the API version number. */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <ioam/lib-trace/trace_all_api_h.h>
51 #undef vl_api_version
52
53
54 typedef struct
55 {
56   /* API message ID base */
57   u16 msg_id_base;
58   vat_main_t *vat_main;
59 } trace_test_main_t;
60
61 trace_test_main_t trace_test_main;
62
63 #define foreach_standard_reply_retval_handler     \
64 _(trace_profile_add_reply)                          \
65 _(trace_profile_del_reply)
66
67 #define foreach_custom_reply_handler     \
68 _(trace_profile_show_config_reply,                                                                              \
69   if(mp->trace_type)                                                                                            \
70   {                                                                                                             \
71      errmsg("                        Trace Type : 0x%x (%d)\n",mp->trace_type, mp->trace_type);                 \
72      errmsg("         Trace timestamp precision : %d (%s)\n",mp->trace_tsp,                                     \
73        (mp->trace_tsp == TSP_SECONDS) ? "Seconds" : ((mp->trace_tsp ==TSP_MILLISECONDS) ?                       \
74          "Milliseconds": (((mp->trace_tsp == TSP_MICROSECONDS) ? "Microseconds" : "Nanoseconds"))));            \
75      errmsg("                           Node Id : 0x%x (%d)\n",htonl(mp->node_id), htonl(mp->node_id));         \
76      errmsg("                          App Data : 0x%x (%d)\n",htonl(mp->app_data), htonl(mp->app_data));       \
77   }                                                                                                             \
78     else errmsg("No valid trace profile configuration found\n");)
79 #define _(n)                                            \
80     static void vl_api_##n##_t_handler                  \
81     (vl_api_##n##_t * mp)                               \
82     {                                                   \
83         vat_main_t * vam = trace_test_main.vat_main;   \
84         i32 retval = ntohl(mp->retval);                 \
85         if (vam->async_mode) {                          \
86             vam->async_errors += (retval < 0);          \
87         } else {                                        \
88             vam->retval = retval;                       \
89             vam->result_ready = 1;                      \
90         }                                               \
91     }
92 foreach_standard_reply_retval_handler;
93 #undef _
94
95 #define _(n,body)                                       \
96     static void vl_api_##n##_t_handler                  \
97     (vl_api_##n##_t * mp)                               \
98     {                                                   \
99         vat_main_t * vam = trace_test_main.vat_main;    \
100         i32 retval = ntohl(mp->retval);                 \
101         if (vam->async_mode) {                          \
102             vam->async_errors += (retval < 0);          \
103         } else {                                        \
104             vam->retval = retval;                       \
105             vam->result_ready = 1;                      \
106         }                                               \
107         if(retval>=0)do{body;} while(0);                 \
108         else errmsg("Error, retval: %d",retval);        \
109     }
110 foreach_custom_reply_handler;
111 #undef _
112 /*
113  * Table of message reply handlers, must include boilerplate handlers
114  * we just generated
115  */
116 #define foreach_vpe_api_reply_msg                                       \
117 _(TRACE_PROFILE_ADD_REPLY, trace_profile_add_reply)                         \
118 _(TRACE_PROFILE_DEL_REPLY, trace_profile_del_reply)                         \
119 _(TRACE_PROFILE_SHOW_CONFIG_REPLY, trace_profile_show_config_reply)
120
121
122 /* M: construct, but don't yet send a message */
123
124 #define M(T,t)                                                  \
125 do {                                                            \
126     vam->result_ready = 0;                                      \
127     mp = vl_msg_api_alloc(sizeof(*mp));                         \
128     memset (mp, 0, sizeof (*mp));                               \
129     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
130     mp->client_index = vam->my_client_index;                    \
131 } while(0);
132
133 #define M2(T,t,n)                                               \
134 do {                                                            \
135     vam->result_ready = 0;                                      \
136     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
137     memset (mp, 0, sizeof (*mp));                               \
138     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
139     mp->client_index = vam->my_client_index;                    \
140 } while(0);
141
142 /* S: send a message */
143 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
144
145 /* W: wait for results, with timeout */
146 #define W                                       \
147 do {                                            \
148     timeout = vat_time_now (vam) + 1.0;         \
149                                                 \
150     while (vat_time_now (vam) < timeout) {      \
151         if (vam->result_ready == 1) {           \
152             return (vam->retval);               \
153         }                                       \
154     }                                           \
155     return -99;                                 \
156 } while(0);
157
158
159 static int
160 api_trace_profile_add (vat_main_t * vam)
161 {
162   trace_test_main_t *sm = &trace_test_main;
163   unformat_input_t *input = vam->input;
164   vl_api_trace_profile_add_t *mp;
165   u8 trace_type = 0;
166   u8 num_elts = 0;
167   int rv = 0;
168   u32 node_id = 0;
169   u32 app_data = 0;
170   u8 trace_tsp = 0;
171   f64 timeout;
172
173   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
174     {
175       if (unformat (input, "trace-type 0x%x", &trace_type))
176         ;
177       else if (unformat (input, "trace-elts %d", &num_elts))
178         ;
179       else if (unformat (input, "trace-tsp %d", &trace_tsp))
180         ;
181       else if (unformat (input, "node-id 0x%x", &node_id))
182         ;
183       else if (unformat (input, "app-data 0x%x", &app_data))
184         ;
185
186       else
187         break;
188     }
189
190
191   M (TRACE_PROFILE_ADD, trace_profile_add);
192
193   mp->trace_type = trace_type;
194   mp->trace_tsp = trace_tsp;
195   mp->node_id = htonl (node_id);
196   mp->app_data = htonl (app_data);
197   mp->num_elts = num_elts;
198
199   S;
200   W;
201
202   return (rv);
203 }
204
205
206
207 static int
208 api_trace_profile_del (vat_main_t * vam)
209 {
210   trace_test_main_t *sm = &trace_test_main;
211   vl_api_trace_profile_del_t *mp;
212   f64 timeout;
213
214   M (TRACE_PROFILE_DEL, trace_profile_del);
215   S;
216   W;
217   return 0;
218 }
219
220 static int
221 api_trace_profile_show_config (vat_main_t * vam)
222 {
223   trace_test_main_t *sm = &trace_test_main;
224   vl_api_trace_profile_show_config_t *mp;
225   f64 timeout;
226   M (TRACE_PROFILE_SHOW_CONFIG, trace_profile_show_config);
227   S;
228   W;
229   return 0;
230 }
231
232 /*
233  * List of messages that the api test plugin sends,
234  * and that the data plane plugin processes
235  */
236 #define foreach_vpe_api_msg \
237 _(trace_profile_add, ""\
238   "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>")  \
239 _(trace_profile_del, "[id <nn>]")                    \
240 _(trace_profile_show_config, "[id <nn>]")
241
242
243 void
244 vat_api_hookup (vat_main_t * vam)
245 {
246   trace_test_main_t *sm = &trace_test_main;
247   /* Hook up handlers for replies from the data plane plug-in */
248 #define _(N,n)                                                  \
249     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
250                            #n,                                  \
251                            vl_api_##n##_t_handler,              \
252                            vl_noop_handler,                     \
253                            vl_api_##n##_t_endian,               \
254                            vl_api_##n##_t_print,                \
255                            sizeof(vl_api_##n##_t), 1);
256   foreach_vpe_api_reply_msg;
257 #undef _
258
259   /* API messages we can send */
260 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
261   foreach_vpe_api_msg;
262 #undef _
263
264   /* Help strings */
265 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
266   foreach_vpe_api_msg;
267 #undef _
268 }
269
270 clib_error_t *
271 vat_plugin_register (vat_main_t * vam)
272 {
273   trace_test_main_t *sm = &trace_test_main;
274   u8 *name;
275
276   sm->vat_main = vam;
277
278   name = format (0, "ioam_trace_%08x%c", api_version, 0);
279   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
280
281   if (sm->msg_id_base != (u16) ~ 0)
282     vat_api_hookup (vam);
283
284   vec_free (name);
285
286   return 0;
287 }
288
289 /*
290  * fd.io coding-style-patch-verification: ON
291  *
292  * Local Variables:
293  * eval: (c-set-style "gnu")
294  * End:
295  */