cd00c591fda91a8008daebaaa43da2008323850e
[vpp.git] / src / plugins / ioam / lib-trace / trace_api.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_api.c - iOAM Trace related APIs to create
18  *               and maintain profiles
19  *------------------------------------------------------------------
20  */
21
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <ioam/lib-trace/trace_util.h>
25 #include <ioam/lib-trace/trace_config.h>
26
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29
30
31 /* define message IDs */
32 #include <ioam/lib-trace/trace_msg_enum.h>
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <ioam/lib-trace/trace_all_api_h.h>
37 #undef vl_typedefs
38
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <ioam/lib-trace/trace_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #define vl_printfun
47 #include <ioam/lib-trace/trace_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/lib-trace/trace_all_api_h.h>
53 #undef vl_api_version
54
55 /*
56  * A handy macro to set up a message reply.
57  * Assumes that the following variables are available:
58  * mp - pointer to request message
59  * rmp - pointer to reply message type
60  * rv - return value
61  */
62
63 #define TRACE_REPLY_MACRO(t)                                          \
64 do {                                                            \
65     unix_shared_memory_queue_t * q =                            \
66     vl_api_client_index_to_input_queue (mp->client_index);      \
67     if (!q)                                                     \
68         return;                                                 \
69                                                                 \
70     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
71     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
72     rmp->context = mp->context;                                 \
73     rmp->retval = ntohl(rv);                                    \
74                                                                 \
75     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
76 } while(0);
77
78 /* *INDENT-OFF* */
79 #define TRACE_REPLY_MACRO2(t, body)                                   \
80 do {                                                            \
81     unix_shared_memory_queue_t * q;                             \
82     rv = vl_msg_api_pd_handler (mp, rv);                        \
83     q = vl_api_client_index_to_input_queue (mp->client_index);  \
84     if (!q)                                                     \
85         return;                                                 \
86                                                                 \
87     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
88     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
89     rmp->context = mp->context;                                 \
90     rmp->retval = ntohl(rv);                                    \
91     do {body;} while (0);                                       \
92     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
93 } while(0);
94 /* *INDENT-ON* */
95
96 /* List of message types that this plugin understands */
97
98 #define foreach_trace_plugin_api_msg                                      \
99 _(TRACE_PROFILE_ADD, trace_profile_add)                                     \
100 _(TRACE_PROFILE_DEL, trace_profile_del)                                     \
101 _(TRACE_PROFILE_SHOW_CONFIG, trace_profile_show_config)
102
103 static void vl_api_trace_profile_add_t_handler
104   (vl_api_trace_profile_add_t * mp)
105 {
106   trace_main_t *sm = &trace_main;
107   int rv = 0;
108   vl_api_trace_profile_add_reply_t *rmp;
109   trace_profile *profile = NULL;
110
111   profile = trace_profile_find ();
112   if (profile)
113     {
114       rv =
115         trace_profile_create (profile, mp->trace_type, mp->num_elts,
116                               mp->trace_tsp, ntohl (mp->node_id),
117                               ntohl (mp->app_data));
118       if (rv != 0)
119         goto ERROROUT;
120     }
121   else
122     {
123       rv = -3;
124     }
125 ERROROUT:
126   TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_ADD_REPLY);
127 }
128
129
130 static void vl_api_trace_profile_del_t_handler
131   (vl_api_trace_profile_del_t * mp)
132 {
133   trace_main_t *sm = &trace_main;
134   int rv = 0;
135   vl_api_trace_profile_del_reply_t *rmp;
136
137   clear_trace_profiles ();
138
139   TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_DEL_REPLY);
140 }
141
142 static void vl_api_trace_profile_show_config_t_handler
143   (vl_api_trace_profile_show_config_t * mp)
144 {
145   trace_main_t *sm = &trace_main;
146   vl_api_trace_profile_show_config_reply_t *rmp;
147   int rv = 0;
148   trace_profile *profile = trace_profile_find ();
149   if (profile->valid)
150     {
151       TRACE_REPLY_MACRO2 (VL_API_TRACE_PROFILE_SHOW_CONFIG_REPLY,
152                           rmp->trace_type = profile->trace_type;
153                           rmp->num_elts = profile->num_elts;
154                           rmp->trace_tsp = profile->trace_tsp;
155                           rmp->node_id = htonl (profile->node_id);
156                           rmp->app_data = htonl (profile->app_data);
157         );
158     }
159   else
160     {
161       TRACE_REPLY_MACRO2 (VL_API_TRACE_PROFILE_SHOW_CONFIG_REPLY,
162                           rmp->trace_type = 0;
163                           rmp->num_elts = 0; rmp->trace_tsp = 0;
164                           rmp->node_id = 0; rmp->app_data = 0;
165         );
166     }
167 }
168
169 /* Set up the API message handling tables */
170 static clib_error_t *
171 trace_plugin_api_hookup (vlib_main_t * vm)
172 {
173   trace_main_t *sm = &trace_main;
174 #define _(N,n)                                                  \
175     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
176                            #n,                                  \
177                            vl_api_##n##_t_handler,              \
178                            vl_noop_handler,                     \
179                            vl_api_##n##_t_endian,               \
180                            vl_api_##n##_t_print,                \
181                            sizeof(vl_api_##n##_t), 1);
182   foreach_trace_plugin_api_msg;
183 #undef _
184
185   return 0;
186 }
187
188 #define vl_msg_name_crc_list
189 #include <ioam/lib-trace/trace_all_api_h.h>
190 #undef vl_msg_name_crc_list
191
192 static void
193 setup_message_id_table (trace_main_t * sm, api_main_t * am)
194 {
195 #define _(id,n,crc) \
196   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
197   foreach_vl_msg_name_crc_trace;
198 #undef _
199 }
200
201 static clib_error_t *
202 trace_init (vlib_main_t * vm)
203 {
204   trace_main_t *sm = &trace_main;
205   clib_error_t *error = 0;
206   u8 *name;
207
208   bzero (sm, sizeof (trace_main));
209   (void) trace_util_init ();
210
211   sm->vlib_main = vm;
212   sm->vnet_main = vnet_get_main ();
213
214   name = format (0, "ioam_trace_%08x%c", api_version, 0);
215
216   /* Ask for a correctly-sized block of API message decode slots */
217   sm->msg_id_base = vl_msg_api_get_msg_ids
218     ((char *) name, VL_MSG_FIRST_AVAILABLE);
219
220   error = trace_plugin_api_hookup (vm);
221
222   /* Add our API messages to the global name_crc hash table */
223   setup_message_id_table (sm, &api_main);
224
225   vec_free (name);
226
227   return error;
228 }
229
230 VLIB_INIT_FUNCTION (trace_init);
231
232 /*
233  * fd.io coding-style-patch-verification: ON
234  *
235  * Local Variables:
236  * eval: (c-set-style "gnu")
237  * End:
238  */