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