f4f1d4d54e2e8830ec5290e13d7c3ab70897bcb9
[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 #include <vlibapi/vat_helper_macros.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 \n",mp->trace_tsp);                                        \
73      errmsg("                           Node Id : 0x%x (%d)\n",htonl(mp->node_id), htonl(mp->node_id));         \
74      errmsg("                          App Data : 0x%x (%d)\n",htonl(mp->app_data), htonl(mp->app_data));       \
75   }                                                                                                             \
76     else errmsg("No valid trace profile configuration found\n");)
77 #define _(n)                                            \
78     static void vl_api_##n##_t_handler                  \
79     (vl_api_##n##_t * mp)                               \
80     {                                                   \
81         vat_main_t * vam = trace_test_main.vat_main;   \
82         i32 retval = ntohl(mp->retval);                 \
83         if (vam->async_mode) {                          \
84             vam->async_errors += (retval < 0);          \
85         } else {                                        \
86             vam->retval = retval;                       \
87             vam->result_ready = 1;                      \
88         }                                               \
89     }
90 foreach_standard_reply_retval_handler;
91 #undef _
92
93 #define _(n,body)                                       \
94     static void vl_api_##n##_t_handler                  \
95     (vl_api_##n##_t * mp)                               \
96     {                                                   \
97         vat_main_t * vam = trace_test_main.vat_main;    \
98         i32 retval = ntohl(mp->retval);                 \
99         if (vam->async_mode) {                          \
100             vam->async_errors += (retval < 0);          \
101         } else {                                        \
102             vam->retval = retval;                       \
103             vam->result_ready = 1;                      \
104         }                                               \
105         if(retval>=0)do{body;} while(0);                 \
106         else errmsg("Error, retval: %d",retval);        \
107     }
108 foreach_custom_reply_handler;
109 #undef _
110 /*
111  * Table of message reply handlers, must include boilerplate handlers
112  * we just generated
113  */
114 #define foreach_vpe_api_reply_msg                                       \
115 _(TRACE_PROFILE_ADD_REPLY, trace_profile_add_reply)                         \
116 _(TRACE_PROFILE_DEL_REPLY, trace_profile_del_reply)                         \
117 _(TRACE_PROFILE_SHOW_CONFIG_REPLY, trace_profile_show_config_reply)
118
119 static int
120 api_trace_profile_add (vat_main_t * vam)
121 {
122   unformat_input_t *input = vam->input;
123   vl_api_trace_profile_add_t *mp;
124   u8 trace_type = 0;
125   u8 num_elts = 0;
126   int rv = 0;
127   u32 node_id = 0;
128   u32 app_data = 0;
129   u8 trace_tsp = 0;
130   f64 timeout;
131
132   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
133     {
134       if (unformat (input, "trace-type 0x%x", &trace_type))
135         ;
136       else if (unformat (input, "trace-elts %d", &num_elts))
137         ;
138       else if (unformat (input, "trace-tsp %d", &trace_tsp))
139         ;
140       else if (unformat (input, "node-id 0x%x", &node_id))
141         ;
142       else if (unformat (input, "app-data 0x%x", &app_data))
143         ;
144
145       else
146         break;
147     }
148
149
150   M (TRACE_PROFILE_ADD, trace_profile_add);
151
152   mp->trace_type = trace_type;
153   mp->trace_tsp = trace_tsp;
154   mp->node_id = htonl (node_id);
155   mp->app_data = htonl (app_data);
156   mp->num_elts = num_elts;
157
158   S;
159   W;
160
161   return (rv);
162 }
163
164
165
166 static int
167 api_trace_profile_del (vat_main_t * vam)
168 {
169   vl_api_trace_profile_del_t *mp;
170   f64 timeout;
171
172   M (TRACE_PROFILE_DEL, trace_profile_del);
173   S;
174   W;
175   return 0;
176 }
177
178 static int
179 api_trace_profile_show_config (vat_main_t * vam)
180 {
181   vl_api_trace_profile_show_config_t *mp;
182   f64 timeout;
183   M (TRACE_PROFILE_SHOW_CONFIG, trace_profile_show_config);
184   S;
185   W;
186   return 0;
187 }
188
189 /*
190  * List of messages that the api test plugin sends,
191  * and that the data plane plugin processes
192  */
193 #define foreach_vpe_api_msg \
194 _(trace_profile_add, ""\
195   "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>")  \
196 _(trace_profile_del, "[id <nn>]")                    \
197 _(trace_profile_show_config, "[id <nn>]")
198
199
200 static void
201 ioam_trace_vat_api_hookup (vat_main_t * vam)
202 {
203   trace_test_main_t *sm = &trace_test_main;
204   /* Hook up handlers for replies from the data plane plug-in */
205 #define _(N,n)                                                  \
206     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
207                            #n,                                  \
208                            vl_api_##n##_t_handler,              \
209                            vl_noop_handler,                     \
210                            vl_api_##n##_t_endian,               \
211                            vl_api_##n##_t_print,                \
212                            sizeof(vl_api_##n##_t), 1);
213   foreach_vpe_api_reply_msg;
214 #undef _
215
216   /* API messages we can send */
217 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
218   foreach_vpe_api_msg;
219 #undef _
220
221   /* Help strings */
222 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
223   foreach_vpe_api_msg;
224 #undef _
225 }
226
227 clib_error_t *
228 vat_plugin_register (vat_main_t * vam)
229 {
230   trace_test_main_t *sm = &trace_test_main;
231   u8 *name;
232
233   sm->vat_main = vam;
234
235   name = format (0, "ioam_trace_%08x%c", api_version, 0);
236   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
237
238   if (sm->msg_id_base != (u16) ~ 0)
239     ioam_trace_vat_api_hookup (vam);
240
241   vec_free (name);
242
243   return 0;
244 }
245
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */