Move java,lua api and remaining plugins to src/
[vpp.git] / src / plugins / 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
27 /* Declare message IDs */
28 #include <ioam/lib-trace/trace_msg_enum.h>
29
30 /* define message structures */
31 #define vl_typedefs
32 #include <ioam/lib-trace/trace_all_api_h.h>
33 #undef vl_typedefs
34
35 /* declare message handlers for each api */
36
37 #define vl_endianfun            /* define message structures */
38 #include <ioam/lib-trace/trace_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
43 #define vl_printfun
44 #include <ioam/lib-trace/trace_all_api_h.h>
45 #undef vl_printfun
46
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>
50 #undef vl_api_version
51
52
53 typedef struct
54 {
55   /* API message ID base */
56   u16 msg_id_base;
57   vat_main_t *vat_main;
58 } trace_test_main_t;
59
60 trace_test_main_t trace_test_main;
61
62 #define foreach_standard_reply_retval_handler     \
63 _(trace_profile_add_reply)                          \
64 _(trace_profile_del_reply)
65
66 #define foreach_custom_reply_handler     \
67 _(trace_profile_show_config_reply,                                                                              \
68   if(mp->trace_type)                                                                                            \
69   {                                                                                                             \
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));       \
74   }                                                                                                             \
75     else errmsg("No valid trace profile configuration found\n");)
76 #define _(n)                                            \
77     static void vl_api_##n##_t_handler                  \
78     (vl_api_##n##_t * mp)                               \
79     {                                                   \
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);          \
84         } else {                                        \
85             vam->retval = retval;                       \
86             vam->result_ready = 1;                      \
87         }                                               \
88     }
89 foreach_standard_reply_retval_handler;
90 #undef _
91
92 #define _(n,body)                                       \
93     static void vl_api_##n##_t_handler                  \
94     (vl_api_##n##_t * mp)                               \
95     {                                                   \
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);          \
100         } else {                                        \
101             vam->retval = retval;                       \
102             vam->result_ready = 1;                      \
103         }                                               \
104         if(retval>=0)do{body;} while(0);                 \
105         else errmsg("Error, retval: %d",retval);        \
106     }
107 foreach_custom_reply_handler;
108 #undef _
109 /*
110  * Table of message reply handlers, must include boilerplate handlers
111  * we just generated
112  */
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)
117
118
119 /* M: construct, but don't yet send a message */
120
121 #define M(T,t)                                                  \
122 do {                                                            \
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;                    \
128 } while(0);
129
130 #define M2(T,t,n)                                               \
131 do {                                                            \
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;                    \
137 } while(0);
138
139 /* S: send a message */
140 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
141
142 /* W: wait for results, with timeout */
143 #define W                                       \
144 do {                                            \
145     timeout = vat_time_now (vam) + 1.0;         \
146                                                 \
147     while (vat_time_now (vam) < timeout) {      \
148         if (vam->result_ready == 1) {           \
149             return (vam->retval);               \
150         }                                       \
151     }                                           \
152     return -99;                                 \
153 } while(0);
154
155
156 static int
157 api_trace_profile_add (vat_main_t * vam)
158 {
159   trace_test_main_t *sm = &trace_test_main;
160   unformat_input_t *input = vam->input;
161   vl_api_trace_profile_add_t *mp;
162   u8 trace_type = 0;
163   u8 num_elts = 0;
164   int rv = 0;
165   u32 node_id = 0;
166   u32 app_data = 0;
167   u8 trace_tsp = 0;
168   f64 timeout;
169
170   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
171     {
172       if (unformat (input, "trace-type 0x%x", &trace_type))
173         ;
174       else if (unformat (input, "trace-elts %d", &num_elts))
175         ;
176       else if (unformat (input, "trace-tsp %d", &trace_tsp))
177         ;
178       else if (unformat (input, "node-id 0x%x", &node_id))
179         ;
180       else if (unformat (input, "app-data 0x%x", &app_data))
181         ;
182
183       else
184         break;
185     }
186
187
188   M (TRACE_PROFILE_ADD, trace_profile_add);
189
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;
195
196   S;
197   W;
198
199   return (rv);
200 }
201
202
203
204 static int
205 api_trace_profile_del (vat_main_t * vam)
206 {
207   trace_test_main_t *sm = &trace_test_main;
208   vl_api_trace_profile_del_t *mp;
209   f64 timeout;
210
211   M (TRACE_PROFILE_DEL, trace_profile_del);
212   S;
213   W;
214   return 0;
215 }
216
217 static int
218 api_trace_profile_show_config (vat_main_t * vam)
219 {
220   trace_test_main_t *sm = &trace_test_main;
221   vl_api_trace_profile_show_config_t *mp;
222   f64 timeout;
223   M (TRACE_PROFILE_SHOW_CONFIG, trace_profile_show_config);
224   S;
225   W;
226   return 0;
227 }
228
229 /*
230  * List of messages that the api test plugin sends,
231  * and that the data plane plugin processes
232  */
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>]")
238
239
240 void
241 vat_api_hookup (vat_main_t * vam)
242 {
243   trace_test_main_t *sm = &trace_test_main;
244   /* Hook up handlers for replies from the data plane plug-in */
245 #define _(N,n)                                                  \
246     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
247                            #n,                                  \
248                            vl_api_##n##_t_handler,              \
249                            vl_noop_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;
254 #undef _
255
256   /* API messages we can send */
257 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
258   foreach_vpe_api_msg;
259 #undef _
260
261   /* Help strings */
262 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
263   foreach_vpe_api_msg;
264 #undef _
265 }
266
267 clib_error_t *
268 vat_plugin_register (vat_main_t * vam)
269 {
270   trace_test_main_t *sm = &trace_test_main;
271   u8 *name;
272
273   sm->vat_main = vam;
274
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);
277
278   if (sm->msg_id_base != (u16) ~ 0)
279     vat_api_hookup (vam);
280
281   vec_free (name);
282
283   return 0;
284 }
285
286 /*
287  * fd.io coding-style-patch-verification: ON
288  *
289  * Local Variables:
290  * eval: (c-set-style "gnu")
291  * End:
292  */