blob: 4d11b519cf2b87407de7e6114f9472d9c1a9b694 [file] [log] [blame]
Dave Barach371e4e12016-07-08 09:38:52 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * api_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
Dave Barach371e4e12016-07-08 09:38:52 -04005 *
Ed Warnickecb9cada2015-12-08 15:45:58 -07006 *
7 * Copyright (c) 2009 Cisco and/or its affiliates.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at:
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *------------------------------------------------------------------
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
Dave Barach072f8de2016-12-02 13:31:25 -050024#include <stddef.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <string.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026#include <vppinfra/format.h>
27#include <vppinfra/byte_order.h>
28#include <vppinfra/error.h>
29#include <vlib/vlib.h>
30#include <vlib/unix/unix.h>
31#include <vlibapi/api.h>
32#include <vppinfra/elog.h>
33
Dave Barach80f54e22017-03-08 19:08:56 -050034/* *INDENT-OFF* */
35api_main_t api_main =
36 {
37 .region_name = "/unset",
38 .api_uid = -1,
39 .api_gid = -1,
40 };
41/* *INDENT-ON* */
Dave Barach371e4e12016-07-08 09:38:52 -040042
43void
44vl_msg_api_increment_missing_client_counter (void)
45{
46 api_main_t *am = &api_main;
47 am->missing_clients++;
48}
49
Dave Barach371e4e12016-07-08 09:38:52 -040050int
51vl_msg_api_rx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
Dave Barach371e4e12016-07-08 09:38:52 -040053 return (am->rx_trace && am->rx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070054}
55
Dave Barach371e4e12016-07-08 09:38:52 -040056int
57vl_msg_api_tx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070058{
Dave Barach371e4e12016-07-08 09:38:52 -040059 return (am->tx_trace && am->tx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070060}
61
62/*
63 * vl_msg_api_trace
64 */
Dave Barach371e4e12016-07-08 09:38:52 -040065void
66vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070067{
Dave Barach371e4e12016-07-08 09:38:52 -040068 u8 **this_trace;
69 u8 **old_trace;
70 u8 *msg_copy;
Dave Barach072f8de2016-12-02 13:31:25 -050071 u32 length;
Dave Barach371e4e12016-07-08 09:38:52 -040072 trace_cfg_t *cfgp;
73 u16 msg_id = ntohs (*((u16 *) msg));
Dave Barach072f8de2016-12-02 13:31:25 -050074 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Dave Barach371e4e12016-07-08 09:38:52 -040076 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
Dave Barach371e4e12016-07-08 09:38:52 -040078 if (!cfgp || !cfgp->trace_enable)
79 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Dave Barach371e4e12016-07-08 09:38:52 -040081 msg_copy = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Dave Barach371e4e12016-07-08 09:38:52 -040083 if (tp->nitems == 0)
84 {
85 clib_warning ("tp->nitems is 0");
86 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 }
88
Dave Barach371e4e12016-07-08 09:38:52 -040089 if (vec_len (tp->traces) < tp->nitems)
90 {
91 vec_add1 (tp->traces, 0);
92 this_trace = tp->traces + vec_len (tp->traces) - 1;
93 }
94 else
95 {
96 tp->wrapped = 1;
97 old_trace = tp->traces + tp->curindex++;
98 if (tp->curindex == tp->nitems)
99 tp->curindex = 0;
100 vec_free (*old_trace);
101 this_trace = old_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 }
103
Dave Barach072f8de2016-12-02 13:31:25 -0500104 length = clib_net_to_host_u32 (header->data_len);
105
106 vec_validate (msg_copy, length - 1);
107 clib_memcpy (msg_copy, msg, length);
Dave Barach371e4e12016-07-08 09:38:52 -0400108 *this_trace = msg_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109}
110
Dave Barach371e4e12016-07-08 09:38:52 -0400111int
112vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
113 int onoff)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Dave Barach371e4e12016-07-08 09:38:52 -0400115 vl_api_trace_t *tp;
116 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Dave Barach371e4e12016-07-08 09:38:52 -0400118 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 {
120 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400121 tp = am->tx_trace;
122 if (tp == 0)
123 {
124 vl_msg_api_trace_configure (am, which, 1024);
125 tp = am->tx_trace;
126 }
127 break;
128
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400130 tp = am->rx_trace;
131 if (tp == 0)
132 {
133 vl_msg_api_trace_configure (am, which, 1024);
134 tp = am->rx_trace;
135 }
136 break;
137
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400139 /* duh? */
140 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 }
142
Dave Barach371e4e12016-07-08 09:38:52 -0400143 /* Configured? */
144 if (tp == 0 || tp->nitems == 0)
145 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Dave Barach371e4e12016-07-08 09:38:52 -0400147 rv = tp->enabled;
148 tp->enabled = onoff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach371e4e12016-07-08 09:38:52 -0400150 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151}
152
Dave Barach371e4e12016-07-08 09:38:52 -0400153int
154vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barach371e4e12016-07-08 09:38:52 -0400156 vl_api_trace_t *tp;
157 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Dave Barach371e4e12016-07-08 09:38:52 -0400159 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 {
161 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400162 tp = am->tx_trace;
163 break;
164
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400166 tp = am->rx_trace;
167 break;
168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400170 /* duh? */
171 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 }
173
Dave Barach371e4e12016-07-08 09:38:52 -0400174 /* Configured? */
175 if (!tp || tp->nitems == 0)
176 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Dave Barach371e4e12016-07-08 09:38:52 -0400178 tp->curindex = 0;
179 tp->wrapped = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Dave Barach371e4e12016-07-08 09:38:52 -0400181 for (i = 0; i < vec_len (tp->traces); i++)
182 {
183 vec_free (tp->traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 }
Dave Barach371e4e12016-07-08 09:38:52 -0400185 vec_free (tp->traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
Dave Barach371e4e12016-07-08 09:38:52 -0400187 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188}
189
Dave Barach371e4e12016-07-08 09:38:52 -0400190int
191vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192{
Dave Barach371e4e12016-07-08 09:38:52 -0400193 vl_api_trace_t *tp;
194 vl_api_trace_file_header_t fh;
195 int i;
196 u8 *msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Dave Barach371e4e12016-07-08 09:38:52 -0400198 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 {
200 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400201 tp = am->tx_trace;
202 break;
203
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400205 tp = am->rx_trace;
206 break;
207
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400209 /* duh? */
210 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 }
212
Dave Barach371e4e12016-07-08 09:38:52 -0400213 /* Configured, data present? */
214 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
215 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Dave Barach371e4e12016-07-08 09:38:52 -0400217 /* "Dare to be stupid" check */
218 if (fp == 0)
219 {
220 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 }
222
Dave Barach371e4e12016-07-08 09:38:52 -0400223 /* Write the file header */
224 fh.nitems = vec_len (tp->traces);
225 fh.endian = tp->endian;
226 fh.wrapped = tp->wrapped;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barach371e4e12016-07-08 09:38:52 -0400228 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
229 {
230 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 }
232
Dave Barach371e4e12016-07-08 09:38:52 -0400233 /* No-wrap case */
234 if (tp->wrapped == 0)
235 {
236 /*
237 * Note: vec_len return 0 when fed a NULL pointer.
238 * Unfortunately, the static analysis tool doesn't
239 * figure it out, hence the suppressed warnings.
240 * What a great use of my time.
241 */
242 for (i = 0; i < vec_len (tp->traces); i++)
243 {
Dave Barach072f8de2016-12-02 13:31:25 -0500244 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400245 /*sa_ignore NO_NULL_CHK */
246 msg = tp->traces[i];
247 /*
248 * This retarded check required to pass
249 * [sic] SA-checking.
250 */
251 if (!msg)
252 continue;
Dave Barach072f8de2016-12-02 13:31:25 -0500253
254 msg_length = clib_host_to_net_u32 (vec_len (msg));
255 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
256 != sizeof (msg_length))
257 {
258 return (-14);
259 }
Dave Barach371e4e12016-07-08 09:38:52 -0400260 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
261 {
262 return (-11);
263 }
264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 }
Dave Barach371e4e12016-07-08 09:38:52 -0400266 else
267 {
268 /* Wrap case: write oldest -> end of buffer */
269 for (i = tp->curindex; i < vec_len (tp->traces); i++)
270 {
Dave Barach072f8de2016-12-02 13:31:25 -0500271 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400272 msg = tp->traces[i];
273 /*
274 * This retarded check required to pass
275 * [sic] SA-checking
276 */
277 if (!msg)
278 continue;
279
Dave Barach072f8de2016-12-02 13:31:25 -0500280 msg_length = clib_host_to_net_u32 (vec_len (msg));
281 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
282 != sizeof (msg_length))
283 {
284 return (-14);
285 }
286
Dave Barach371e4e12016-07-08 09:38:52 -0400287 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
288 {
289 return (-12);
290 }
291 }
292 /* write beginning of buffer -> oldest-1 */
293 for (i = 0; i < tp->curindex; i++)
294 {
Dave Barach072f8de2016-12-02 13:31:25 -0500295 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400296 /*sa_ignore NO_NULL_CHK */
297 msg = tp->traces[i];
298 /*
299 * This retarded check required to pass
300 * [sic] SA-checking
301 */
302 if (!msg)
303 continue;
304
Dave Barach072f8de2016-12-02 13:31:25 -0500305 msg_length = clib_host_to_net_u32 (vec_len (msg));
306 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
307 != sizeof (msg_length))
308 {
309 return (-14);
310 }
311
Dave Barach371e4e12016-07-08 09:38:52 -0400312 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
313 {
314 return (-13);
315 }
316 }
317 }
318 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319}
320
Dave Barach371e4e12016-07-08 09:38:52 -0400321int
322vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
323 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324{
Dave Barach371e4e12016-07-08 09:38:52 -0400325 vl_api_trace_t *tp;
326 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Dave Barach371e4e12016-07-08 09:38:52 -0400328 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 {
330 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400331 tp = am->tx_trace;
332 if (tp == 0)
333 {
334 vec_validate (am->tx_trace, 0);
335 tp = am->tx_trace;
336 }
337 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barach371e4e12016-07-08 09:38:52 -0400339 case VL_API_TRACE_RX:
340 tp = am->rx_trace;
341 if (tp == 0)
342 {
343 vec_validate (am->rx_trace, 0);
344 tp = am->rx_trace;
345 }
346
347 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
349 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400350 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
352 }
353
Dave Barach371e4e12016-07-08 09:38:52 -0400354 if (tp->enabled)
355 {
356 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 }
Dave Barach371e4e12016-07-08 09:38:52 -0400358 if (tp->traces)
359 {
360 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 }
362
Dave Barach371e4e12016-07-08 09:38:52 -0400363 memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
Dave Barach371e4e12016-07-08 09:38:52 -0400365 if (clib_arch_is_big_endian)
366 {
367 tp->endian = VL_API_BIG_ENDIAN;
368 }
369 else
370 {
371 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 }
373
Dave Barach371e4e12016-07-08 09:38:52 -0400374 tp->nitems = nitems;
375 if (was_on)
376 {
377 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 }
Dave Barach371e4e12016-07-08 09:38:52 -0400379 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380}
381
Dave Barach80f54e22017-03-08 19:08:56 -0500382void
383vl_msg_api_barrier_sync (void)
384{
385}
386
387void
388vl_msg_api_barrier_release (void)
389{
390}
391
Dave Barach371e4e12016-07-08 09:38:52 -0400392always_inline void
393msg_handler_internal (api_main_t * am,
394 void *the_msg, int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395{
Dave Barach371e4e12016-07-08 09:38:52 -0400396 u16 id = ntohs (*((u16 *) the_msg));
397 u8 *(*print_fp) (void *, void *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Dave Barach371e4e12016-07-08 09:38:52 -0400399 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
400 {
401 if (trace_it)
402 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Dave Barach371e4e12016-07-08 09:38:52 -0400404 if (am->msg_print_flag)
405 {
406 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
407 print_fp = (void *) am->msg_print_handlers[id];
408 if (print_fp == 0)
409 {
410 fformat (stdout, " [no registered print fn]\n");
411 }
412 else
413 {
414 (*print_fp) (the_msg, stdout);
415 }
416 }
417
418 if (do_it)
419 {
420 if (!am->is_mp_safe[id])
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100421 {
422 vl_msg_api_barrier_trace_context (am->msg_names[id]);
423 vl_msg_api_barrier_sync ();
424 }
Dave Barach371e4e12016-07-08 09:38:52 -0400425 (*am->msg_handlers[id]) (the_msg);
426 if (!am->is_mp_safe[id])
427 vl_msg_api_barrier_release ();
428 }
429 }
430 else
431 {
432 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 }
434
Dave Barach371e4e12016-07-08 09:38:52 -0400435 if (free_it)
436 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437}
438
439/* set to 1 if you want before/after message handler event logging */
440#define ELOG_API_MESSAGE_HANDLERS 0
441
442#if ELOG_API_MESSAGE_HANDLERS > 0
Dave Barach371e4e12016-07-08 09:38:52 -0400443static u32
444elog_id_for_msg_name (vlib_main_t * vm, char *msg_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445{
Dave Barach371e4e12016-07-08 09:38:52 -0400446 uword *p, r;
447 static uword *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 u8 *name_copy;
449
Dave Barach371e4e12016-07-08 09:38:52 -0400450 if (!h)
451 h = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
453 p = hash_get_mem (h, msg_name);
454 if (p)
455 return p[0];
456 r = elog_string (&vm->elog_main, "%s", msg_name);
457
458 name_copy = format (0, "%s%c", msg_name, 0);
459
460 hash_set_mem (h, name_copy, r);
461
462 return r;
463}
464#endif
465
466/* This is only to be called from a vlib/vnet app */
Dave Barach371e4e12016-07-08 09:38:52 -0400467void
468vl_msg_api_handler_with_vm_node (api_main_t * am,
469 void *the_msg, vlib_main_t * vm,
470 vlib_node_runtime_t * node)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471{
Dave Barach371e4e12016-07-08 09:38:52 -0400472 u16 id = ntohs (*((u16 *) the_msg));
473 u8 *(*handler) (void *, void *, void *);
474
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475#if ELOG_API_MESSAGE_HANDLERS > 0
Dave Barach371e4e12016-07-08 09:38:52 -0400476 {
477 /* *INDENT-OFF* */
478 ELOG_TYPE_DECLARE (e) =
479 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 .format = "api-msg: %s",
481 .format_args = "T4",
482 };
Dave Barach371e4e12016-07-08 09:38:52 -0400483 /* *INDENT-ON* */
484 struct
485 {
486 u32 c;
487 } *ed;
488 ed = ELOG_DATA (&vm->elog_main, e);
489 if (id < vec_len (am->msg_names))
490 ed->c = elog_id_for_msg_name (vm, am->msg_names[id]);
491 else
492 ed->c = elog_id_for_msg_name (vm, "BOGUS");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 }
Dave Barach371e4e12016-07-08 09:38:52 -0400494#endif
495
496 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
497 {
498 handler = (void *) am->msg_handlers[id];
499
500 if (am->rx_trace && am->rx_trace->enabled)
501 vl_msg_api_trace (am, am->rx_trace, the_msg);
502
503 if (!am->is_mp_safe[id])
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100504 {
505 vl_msg_api_barrier_trace_context (am->msg_names[id]);
506 vl_msg_api_barrier_sync ();
507 }
Dave Barach371e4e12016-07-08 09:38:52 -0400508 (*handler) (the_msg, vm, node);
509 if (!am->is_mp_safe[id])
510 vl_msg_api_barrier_release ();
511 }
512 else
513 {
Jon Loeligerf95b37e2017-02-13 15:21:12 -0600514 clib_warning ("no handler for msg id %d", id);
Dave Barach371e4e12016-07-08 09:38:52 -0400515 }
516
517 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 * Special-case, so we can e.g. bounce messages off the vnet
519 * main thread without copying them...
520 */
521 if (!(am->message_bounce[id]))
Dave Barach371e4e12016-07-08 09:38:52 -0400522 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
524#if ELOG_API_MESSAGE_HANDLERS > 0
Dave Barach371e4e12016-07-08 09:38:52 -0400525 {
526 /* *INDENT-OFF* */
527 ELOG_TYPE_DECLARE (e) = {
528 .format = "api-msg-done: %s",
529 .format_args = "T4",
530 };
531 /* *INDENT-ON* */
532
533 struct
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 {
Dave Barach371e4e12016-07-08 09:38:52 -0400535 u32 c;
536 } *ed;
537 ed = ELOG_DATA (&vm->elog_main, e);
538 if (id < vec_len (am->msg_names))
539 ed->c = elog_id_for_msg_name (vm, am->msg_names[id]);
540 else
541 ed->c = elog_id_for_msg_name (vm, "BOGUS");
542 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543#endif
544}
545
Dave Barach371e4e12016-07-08 09:38:52 -0400546void
547vl_msg_api_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
Dave Barach371e4e12016-07-08 09:38:52 -0400549 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
Dave Barach371e4e12016-07-08 09:38:52 -0400551 msg_handler_internal (am, the_msg,
552 (am->rx_trace
553 && am->rx_trace->enabled) /* trace_it */ ,
554 1 /* do_it */ , 1 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555}
556
Dave Barach371e4e12016-07-08 09:38:52 -0400557void
558vl_msg_api_handler_no_free (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559{
Dave Barach371e4e12016-07-08 09:38:52 -0400560 api_main_t *am = &api_main;
561 msg_handler_internal (am, the_msg,
562 (am->rx_trace
563 && am->rx_trace->enabled) /* trace_it */ ,
564 1 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565}
566
Dave Barach371e4e12016-07-08 09:38:52 -0400567void
568vl_msg_api_handler_no_trace_no_free (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569{
Dave Barach371e4e12016-07-08 09:38:52 -0400570 api_main_t *am = &api_main;
571 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
572 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573}
574
575/*
576 * Add a trace record to the API message trace buffer, if
577 * API message tracing is enabled. Handy for adding sufficient
578 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400579 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 * application creates database entries based on packet traffic, not
581 * control-plane messages.
582 *
583 */
Dave Barach371e4e12016-07-08 09:38:52 -0400584void
585vl_msg_api_trace_only (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586{
Dave Barach371e4e12016-07-08 09:38:52 -0400587 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
Dave Barach371e4e12016-07-08 09:38:52 -0400589 msg_handler_internal (am, the_msg,
590 (am->rx_trace
591 && am->rx_trace->enabled) /* trace_it */ ,
592 0 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593}
594
Dave Barach371e4e12016-07-08 09:38:52 -0400595void
596vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597{
Dave Barach371e4e12016-07-08 09:38:52 -0400598 api_main_t *am = &api_main;
599 u16 id = ntohs (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach371e4e12016-07-08 09:38:52 -0400601 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
602 {
603 clib_warning ("_vl_msg_id too large: %d\n", id);
604 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 }
Dave Barach371e4e12016-07-08 09:38:52 -0400606 if (am->msg_cleanup_handlers[id])
607 (*am->msg_cleanup_handlers[id]) (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
Dave Barach371e4e12016-07-08 09:38:52 -0400609 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610}
611
612/*
613 * vl_msg_api_replay_handler
614 */
Dave Barach371e4e12016-07-08 09:38:52 -0400615void
616vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617{
Dave Barach371e4e12016-07-08 09:38:52 -0400618 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Dave Barach371e4e12016-07-08 09:38:52 -0400620 u16 id = ntohs (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
Dave Barach371e4e12016-07-08 09:38:52 -0400622 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
623 {
624 clib_warning ("_vl_msg_id too large: %d\n", id);
625 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 }
Dave Barach371e4e12016-07-08 09:38:52 -0400627 /* do NOT trace the message... */
628 if (am->msg_handlers[id])
629 (*am->msg_handlers[id]) (the_msg);
630 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631}
Dave Barach371e4e12016-07-08 09:38:52 -0400632
Dave Barachbfd92272017-05-12 11:59:25 -0400633u32
634vl_msg_api_get_msg_length (void *msg_arg)
635{
636 return vl_msg_api_get_msg_length_inline (msg_arg);
637}
638
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639/*
640 * vl_msg_api_socket_handler
641 */
Dave Barach371e4e12016-07-08 09:38:52 -0400642void
643vl_msg_api_socket_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Dave Barach371e4e12016-07-08 09:38:52 -0400645 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Dave Barach371e4e12016-07-08 09:38:52 -0400647 msg_handler_internal (am, the_msg,
648 (am->rx_trace
649 && am->rx_trace->enabled) /* trace_it */ ,
650 1 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651}
652
653#define foreach_msg_api_vector \
654_(msg_names) \
655_(msg_handlers) \
656_(msg_cleanup_handlers) \
657_(msg_endian_handlers) \
658_(msg_print_handlers) \
659_(api_trace_cfg) \
660_(message_bounce) \
661_(is_mp_safe)
662
Dave Barach371e4e12016-07-08 09:38:52 -0400663void
664vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
Dave Barach371e4e12016-07-08 09:38:52 -0400666 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barach65457162017-10-10 17:53:14 -0400668 /*
669 * This happens during the java core tests if the message
670 * dictionary is missing newly added xxx_reply_t messages.
671 * Should never happen, but since I shot myself in the foot once
672 * this way, I thought I'd make it easy to debug if I ever do
673 * it again... (;-)...
674 */
675 if (c->id == 0)
676 {
677 if (c->name)
678 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
679 else
680 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
681 clib_warning ("Did you forget to call setup_message_id_table?");
682 return;
683 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684
685#define _(a) vec_validate (am->a, c->id);
Dave Barach371e4e12016-07-08 09:38:52 -0400686 foreach_msg_api_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687#undef _
688
Dave Barach392206e2017-09-20 08:40:16 -0400689 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
690 clib_warning
691 ("BUG: re-registering 'vl_api_%s_t_handler'."
692 "Handler was %llx, replaced by %llx",
693 c->name, am->msg_handlers[c->id], c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500694
Dave Barach371e4e12016-07-08 09:38:52 -0400695 am->msg_names[c->id] = c->name;
696 am->msg_handlers[c->id] = c->handler;
697 am->msg_cleanup_handlers[c->id] = c->cleanup;
698 am->msg_endian_handlers[c->id] = c->endian;
699 am->msg_print_handlers[c->id] = c->print;
700 am->message_bounce[c->id] = c->message_bounce;
701 am->is_mp_safe[c->id] = c->is_mp_safe;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
Dave Barach371e4e12016-07-08 09:38:52 -0400703 am->api_trace_cfg[c->id].size = c->size;
704 am->api_trace_cfg[c->id].trace_enable = c->traced;
705 am->api_trace_cfg[c->id].replay_enable = c->replay;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706}
707
Dave Barach371e4e12016-07-08 09:38:52 -0400708/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 * vl_msg_api_set_handlers
710 * preserve the old API for a while
711 */
Dave Barach371e4e12016-07-08 09:38:52 -0400712void
713vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
714 void *endian, void *print, int size, int traced)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715{
Dave Barach371e4e12016-07-08 09:38:52 -0400716 vl_msg_api_msg_config_t cfg;
717 vl_msg_api_msg_config_t *c = &cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Dave Barach0691d6e2017-01-05 10:08:52 -0500719 memset (c, 0, sizeof (*c));
720
Dave Barach371e4e12016-07-08 09:38:52 -0400721 c->id = id;
722 c->name = name;
723 c->handler = handler;
724 c->cleanup = cleanup;
725 c->endian = endian;
726 c->print = print;
Dave Barach371e4e12016-07-08 09:38:52 -0400727 c->traced = traced;
728 c->replay = 1;
729 c->message_bounce = 0;
730 c->is_mp_safe = 0;
731 vl_msg_api_config (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732}
733
Dave Barach371e4e12016-07-08 09:38:52 -0400734void
Matej Perina75a17ec2017-09-21 17:03:27 +0200735vl_msg_api_clean_handlers (int msg_id)
736{
737 vl_msg_api_msg_config_t cfg;
738 vl_msg_api_msg_config_t *c = &cfg;
739
740 memset (c, 0, sizeof (*c));
741
742 c->id = msg_id;
743 vl_msg_api_config (c);
744}
745
746void
Dave Barach371e4e12016-07-08 09:38:52 -0400747vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748{
Dave Barach371e4e12016-07-08 09:38:52 -0400749 api_main_t *am = &api_main;
750 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Dave Barach371e4e12016-07-08 09:38:52 -0400752 vec_validate (am->msg_cleanup_handlers, msg_id);
753 am->msg_cleanup_handlers[msg_id] = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754}
755
Dave Barach371e4e12016-07-08 09:38:52 -0400756void
757vl_msg_api_queue_handler (unix_shared_memory_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758{
Dave Barach371e4e12016-07-08 09:38:52 -0400759 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Dave Barach371e4e12016-07-08 09:38:52 -0400761 while (!unix_shared_memory_queue_sub (q, (u8 *) & msg, 0))
762 vl_msg_api_handler ((void *) msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763}
764
Dave Barach371e4e12016-07-08 09:38:52 -0400765vl_api_trace_t *
766vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767{
Dave Barach371e4e12016-07-08 09:38:52 -0400768 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 {
770 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400771 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400773 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400775 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776 }
777}
778
Dave Barach371e4e12016-07-08 09:38:52 -0400779void
780vl_noop_handler (void *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782}
783
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
785static u8 post_mortem_dump_enabled;
786
Dave Barach80f54e22017-03-08 19:08:56 -0500787void
788vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789{
Dave Barach80f54e22017-03-08 19:08:56 -0500790 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791}
792
Dave Barach371e4e12016-07-08 09:38:52 -0400793void
794vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795{
Dave Barach371e4e12016-07-08 09:38:52 -0400796 api_main_t *am = &api_main;
797 FILE *fp;
798 char filename[64];
799 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
Dave Barach371e4e12016-07-08 09:38:52 -0400801 if (post_mortem_dump_enabled == 0)
802 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Dave Barach371e4e12016-07-08 09:38:52 -0400804 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
805 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Dave Barach371e4e12016-07-08 09:38:52 -0400807 fp = fopen (filename, "w");
808 if (fp == NULL)
809 {
810 rv = write (2, "Couldn't create ", 16);
811 rv = write (2, filename, strlen (filename));
812 rv = write (2, "\n", 1);
813 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 }
Dave Barach371e4e12016-07-08 09:38:52 -0400815 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
816 fclose (fp);
817 if (rv < 0)
818 {
819 rv = write (2, "Failed to save post-mortem API trace to ", 40);
820 rv = write (2, filename, strlen (filename));
821 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822 }
Dave Barach371e4e12016-07-08 09:38:52 -0400823
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824}
825
826/* Layered message handling support */
827
Dave Barach371e4e12016-07-08 09:38:52 -0400828void
829vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830{
Dave Barach371e4e12016-07-08 09:38:52 -0400831 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
Dave Barach371e4e12016-07-08 09:38:52 -0400833 /* Mild idiot proofing */
834 if (msg_id_host_byte_order > 10000)
835 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
836 msg_id_host_byte_order,
837 clib_net_to_host_u16 (msg_id_host_byte_order));
838 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
839 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840}
841
Dave Barach371e4e12016-07-08 09:38:52 -0400842int
843vl_msg_api_pd_handler (void *mp, int rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844{
Dave Barach371e4e12016-07-08 09:38:52 -0400845 api_main_t *am = &api_main;
846 int (*fp) (void *, int);
847 u16 msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848
Dave Barach371e4e12016-07-08 09:38:52 -0400849 if (clib_arch_is_little_endian)
850 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
851 else
852 msg_id = *((u16 *) mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
Dave Barach371e4e12016-07-08 09:38:52 -0400854 if (msg_id >= vec_len (am->pd_msg_handlers)
855 || am->pd_msg_handlers[msg_id] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856 return rv;
Dave Barach371e4e12016-07-08 09:38:52 -0400857
858 fp = am->pd_msg_handlers[msg_id];
859 rv = (*fp) (mp, rv);
860 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861}
862
Dave Barach371e4e12016-07-08 09:38:52 -0400863void
864vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865{
Dave Barach371e4e12016-07-08 09:38:52 -0400866 api_main_t *am = &api_main;
867
868 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869}
870
Dave Barach371e4e12016-07-08 09:38:52 -0400871u16
Neale Rannse72be392017-04-26 13:59:20 -0700872vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873{
Dave Barach371e4e12016-07-08 09:38:52 -0400874 api_main_t *am = &api_main;
875 u8 *name_copy;
876 vl_api_msg_range_t *rp;
877 uword *p;
878 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
Dave Barach371e4e12016-07-08 09:38:52 -0400880 if (am->msg_range_by_name == 0)
881 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Dave Barach371e4e12016-07-08 09:38:52 -0400883 name_copy = format (0, "%s%c", name, 0);
884
885 p = hash_get_mem (am->msg_range_by_name, name_copy);
886 if (p)
887 {
888 clib_warning ("WARNING: duplicate message range registration for '%s'",
889 name_copy);
890 vec_free (name_copy);
891 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892 }
893
Dave Barach371e4e12016-07-08 09:38:52 -0400894 if (n < 0 || n > 1024)
895 {
896 clib_warning
897 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
898 n, name_copy);
899 vec_free (name_copy);
900 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901 }
902
Dave Barach371e4e12016-07-08 09:38:52 -0400903 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904
Dave Barach371e4e12016-07-08 09:38:52 -0400905 rv = rp->first_msg_id = am->first_available_msg_id;
906 am->first_available_msg_id += n;
907 rp->last_msg_id = am->first_available_msg_id - 1;
908 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
Dave Barach371e4e12016-07-08 09:38:52 -0400910 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911
Dave Barach371e4e12016-07-08 09:38:52 -0400912 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913}
Dave Barach371e4e12016-07-08 09:38:52 -0400914
Dave Barach557d1282016-11-10 14:22:49 -0500915void
Neale Rannse72be392017-04-26 13:59:20 -0700916vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -0500917{
918 uword *p;
919
920 if (am->msg_index_by_name_and_crc == 0)
921 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
922
923 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
924 if (p)
925 {
926 clib_warning ("attempt to redefine '%s' ignored...", string);
927 return;
928 }
929
930 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
931}
932
Dave Barach0d056e52017-09-28 15:11:16 -0400933void
934vl_msg_api_add_version (api_main_t * am, const char *string,
935 u32 major, u32 minor, u32 patch)
936{
937 api_version_t version = {.major = major,.minor = minor,.patch = patch };
938 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +0200939 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -0400940 vec_add1 (am->api_version_list, version);
941}
Dave Barach557d1282016-11-10 14:22:49 -0500942
Dave Barach371e4e12016-07-08 09:38:52 -0400943/*
944 * fd.io coding-style-patch-verification: ON
945 *
946 * Local Variables:
947 * eval: (c-set-style "gnu")
948 * End:
949 */