blob: f11344e6e66a6027e36afd1c0ef8facff6209bec [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>
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +000033#include <vppinfra/callback.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
Dave Barach80f54e22017-03-08 19:08:56 -050035/* *INDENT-OFF* */
Dave Barach39d69112019-11-27 11:42:13 -050036api_main_t api_global_main =
Dave Barach80f54e22017-03-08 19:08:56 -050037 {
38 .region_name = "/unset",
39 .api_uid = -1,
40 .api_gid = -1,
41 };
42/* *INDENT-ON* */
Dave Barach371e4e12016-07-08 09:38:52 -040043
Dave Barach39d69112019-11-27 11:42:13 -050044/* Please use vlibapi_get_main() to access my_api_main */
45__thread api_main_t *my_api_main = &api_global_main;
46
47void
48vl_msg_api_set_global_main (void *am_arg)
49{
50 ASSERT (am_arg);
51 my_api_main = (api_main_t *) am_arg;
52}
53
Dave Barach371e4e12016-07-08 09:38:52 -040054void
55vl_msg_api_increment_missing_client_counter (void)
56{
Dave Barach39d69112019-11-27 11:42:13 -050057 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -040058 am->missing_clients++;
59}
60
Dave Barach371e4e12016-07-08 09:38:52 -040061int
62vl_msg_api_rx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Dave Barach371e4e12016-07-08 09:38:52 -040064 return (am->rx_trace && am->rx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065}
66
Dave Barach371e4e12016-07-08 09:38:52 -040067int
68vl_msg_api_tx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barach371e4e12016-07-08 09:38:52 -040070 return (am->tx_trace && am->tx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071}
72
73/*
74 * vl_msg_api_trace
75 */
Dave Barach371e4e12016-07-08 09:38:52 -040076void
77vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
Dave Barach371e4e12016-07-08 09:38:52 -040079 u8 **this_trace;
80 u8 **old_trace;
81 u8 *msg_copy;
Dave Barach072f8de2016-12-02 13:31:25 -050082 u32 length;
Dave Barach371e4e12016-07-08 09:38:52 -040083 trace_cfg_t *cfgp;
Dave Barach9683c1e2019-07-01 09:42:41 -040084 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
Dave Barach072f8de2016-12-02 13:31:25 -050085 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barach371e4e12016-07-08 09:38:52 -040087 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Dave Barach371e4e12016-07-08 09:38:52 -040089 if (!cfgp || !cfgp->trace_enable)
90 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Dave Barach371e4e12016-07-08 09:38:52 -040092 msg_copy = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach371e4e12016-07-08 09:38:52 -040094 if (tp->nitems == 0)
95 {
96 clib_warning ("tp->nitems is 0");
97 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 }
99
Dave Barach371e4e12016-07-08 09:38:52 -0400100 if (vec_len (tp->traces) < tp->nitems)
101 {
102 vec_add1 (tp->traces, 0);
103 this_trace = tp->traces + vec_len (tp->traces) - 1;
104 }
105 else
106 {
107 tp->wrapped = 1;
108 old_trace = tp->traces + tp->curindex++;
109 if (tp->curindex == tp->nitems)
110 tp->curindex = 0;
Dave Barachf35a0722019-06-12 16:50:38 -0400111 /* Reuse the trace record, may save some memory allocator traffic */
112 msg_copy = *old_trace;
113 vec_reset_length (msg_copy);
Dave Barach371e4e12016-07-08 09:38:52 -0400114 this_trace = old_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 }
116
Dave Barach072f8de2016-12-02 13:31:25 -0500117 length = clib_net_to_host_u32 (header->data_len);
118
119 vec_validate (msg_copy, length - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500120 clib_memcpy_fast (msg_copy, msg, length);
Dave Barach371e4e12016-07-08 09:38:52 -0400121 *this_trace = msg_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122}
123
Dave Barach371e4e12016-07-08 09:38:52 -0400124int
125vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
126 int onoff)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
Dave Barach371e4e12016-07-08 09:38:52 -0400128 vl_api_trace_t *tp;
129 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Dave Barach371e4e12016-07-08 09:38:52 -0400131 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 {
133 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400134 tp = am->tx_trace;
135 if (tp == 0)
136 {
137 vl_msg_api_trace_configure (am, which, 1024);
138 tp = am->tx_trace;
139 }
140 break;
141
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400143 tp = am->rx_trace;
144 if (tp == 0)
145 {
146 vl_msg_api_trace_configure (am, which, 1024);
147 tp = am->rx_trace;
148 }
149 break;
150
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400152 /* duh? */
153 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 }
155
Dave Barach371e4e12016-07-08 09:38:52 -0400156 /* Configured? */
157 if (tp == 0 || tp->nitems == 0)
158 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach371e4e12016-07-08 09:38:52 -0400160 rv = tp->enabled;
161 tp->enabled = onoff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Dave Barach371e4e12016-07-08 09:38:52 -0400163 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164}
165
Dave Barach371e4e12016-07-08 09:38:52 -0400166int
167vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168{
Dave Barach371e4e12016-07-08 09:38:52 -0400169 vl_api_trace_t *tp;
170 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Dave Barach371e4e12016-07-08 09:38:52 -0400172 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 {
174 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400175 tp = am->tx_trace;
176 break;
177
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400179 tp = am->rx_trace;
180 break;
181
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400183 /* duh? */
184 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 }
186
Dave Barach371e4e12016-07-08 09:38:52 -0400187 /* Configured? */
188 if (!tp || tp->nitems == 0)
189 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barach371e4e12016-07-08 09:38:52 -0400191 tp->curindex = 0;
192 tp->wrapped = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Dave Barach371e4e12016-07-08 09:38:52 -0400194 for (i = 0; i < vec_len (tp->traces); i++)
195 {
196 vec_free (tp->traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 }
Dave Barach371e4e12016-07-08 09:38:52 -0400198 vec_free (tp->traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Dave Barach371e4e12016-07-08 09:38:52 -0400200 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201}
202
Ole Troanedfe2c02019-07-30 15:38:13 +0200203u8 *
204vl_api_serialize_message_table (api_main_t * am, u8 * vector)
205{
206 serialize_main_t _sm, *sm = &_sm;
207 hash_pair_t *hp;
208 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
209
210 serialize_open_vector (sm, vector);
211
212 /* serialize the count */
213 serialize_integer (sm, nmsg, sizeof (u32));
214
215 /* *INDENT-OFF* */
216 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
217 ({
218 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
219 serialize_cstring (sm, (char *) hp->key);
220 }));
221 /* *INDENT-ON* */
222
223 return serialize_close_vector (sm);
224}
225
Filip Tehlar36217e32021-07-23 08:51:10 +0000226static int
227vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
228{
229 u8 *tmpmem = 0;
230 int tlen, slen;
231 cJSON *(*tojson_fn) (void *);
232
233 u32 msg_length = vec_len (msg);
234 vec_validate (tmpmem, msg_length - 1);
235 clib_memcpy_fast (tmpmem, msg, msg_length);
236 u16 id = clib_net_to_host_u16 (*((u16 *) msg));
237
238 void (*endian_fp) (void *);
239 endian_fp = am->msg_endian_handlers[id];
240 (*endian_fp) (tmpmem);
241
242 if (id < vec_len (am->msg_tojson_handlers) && am->msg_tojson_handlers[id])
243 {
244 tojson_fn = am->msg_tojson_handlers[id];
245 cJSON *o = tojson_fn (tmpmem);
246 char *s = cJSON_Print (o);
247 slen = strlen (s);
248 tlen = fwrite (s, 1, slen, fp);
249 cJSON_free (s);
250 cJSON_Delete (o);
251 vec_free (tmpmem);
252 if (tlen != slen)
253 {
254 fformat (stderr, "writing to file error\n");
255 return -11;
256 }
257 }
258 else
259 fformat (stderr, " [no registered tojson fn]\n");
260
261 return 0;
262}
263
264#define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
265
266typedef struct
267{
268 FILE *fp;
269 u32 n_traces;
270 u32 i;
271} vl_msg_write_json_args_t;
272
273static int
274vl_msg_write_json_fn (u8 *msg, void *ctx)
275{
276 vl_msg_write_json_args_t *arg = ctx;
277 FILE *fp = arg->fp;
278 api_main_t *am = vlibapi_get_main ();
279 int rc = vl_msg_api_trace_write_one (am, msg, fp);
280 if (rc < 0)
281 return rc;
282
283 if (arg->i < arg->n_traces - 1)
284 vl_msg_fwrite (",\n", fp);
285 arg->i++;
286 return 0;
287}
288
289static int
290vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
291{
292 vl_msg_write_json_args_t args;
293 clib_memset (&args, 0, sizeof (args));
294 args.fp = fp;
295 args.n_traces = vec_len (tp->traces);
296 vl_msg_fwrite ("[\n", fp);
297
298 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
299 if (rv < 0)
300 return rv;
301
302 vl_msg_fwrite ("]", fp);
303 return 0;
304}
305
Dave Barach371e4e12016-07-08 09:38:52 -0400306int
Filip Tehlar36217e32021-07-23 08:51:10 +0000307vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
308 void *ctx)
309{
310 int i;
311 u8 *msg;
312 int rv = 0;
313
314 /* No-wrap case */
315 if (tp->wrapped == 0)
316 {
317 for (i = 0; i < vec_len (tp->traces); i++)
318 {
319 /*sa_ignore NO_NULL_CHK */
320 msg = tp->traces[i];
321 if (!msg)
322 continue;
323
324 rv = fn (msg, ctx);
325 if (rv < 0)
326 return rv;
327 }
328 }
329 else
330 {
331 /* Wrap case: write oldest -> end of buffer */
332 for (i = tp->curindex; i < vec_len (tp->traces); i++)
333 {
334 msg = tp->traces[i];
335 if (!msg)
336 continue;
337
338 rv = fn (msg, ctx);
339 if (rv < 0)
340 return rv;
341 }
342 /* write beginning of buffer -> oldest-1 */
343 for (i = 0; i < tp->curindex; i++)
344 {
345 /*sa_ignore NO_NULL_CHK */
346 msg = tp->traces[i];
347 if (!msg)
348 continue;
349
350 rv = fn (msg, ctx);
351 if (rv < 0)
352 return rv;
353 }
354 }
355 return 0;
356}
357
358static int
359vl_api_msg_write_fn (u8 *msg, void *ctx)
360{
361 FILE *fp = ctx;
362 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
363 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
364 {
365 return (-14);
366 }
367 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
368 {
369 return (-14);
370 }
371 return 0;
372}
373
374int
375vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
376 u8 is_json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377{
Dave Barach371e4e12016-07-08 09:38:52 -0400378 vl_api_trace_t *tp;
379 vl_api_trace_file_header_t fh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Dave Barach371e4e12016-07-08 09:38:52 -0400381 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 {
383 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400384 tp = am->tx_trace;
385 break;
386
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400388 tp = am->rx_trace;
389 break;
390
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400392 /* duh? */
393 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 }
395
Dave Barach371e4e12016-07-08 09:38:52 -0400396 /* Configured, data present? */
397 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
398 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Dave Barach371e4e12016-07-08 09:38:52 -0400400 /* "Dare to be stupid" check */
401 if (fp == 0)
402 {
403 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 }
405
Filip Tehlar36217e32021-07-23 08:51:10 +0000406 if (is_json)
407 return vl_msg_api_trace_write_json (am, tp, fp);
408
Dave Barach371e4e12016-07-08 09:38:52 -0400409 /* Write the file header */
Dave Barach371e4e12016-07-08 09:38:52 -0400410 fh.wrapped = tp->wrapped;
Ole Troanedfe2c02019-07-30 15:38:13 +0200411 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
Filip Tehlar36217e32021-07-23 08:51:10 +0000412
Ole Troanedfe2c02019-07-30 15:38:13 +0200413 u8 *m = vl_api_serialize_message_table (am, 0);
Ole Troanedfe2c02019-07-30 15:38:13 +0200414 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
Dave Barach371e4e12016-07-08 09:38:52 -0400416 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
417 {
418 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 }
420
Ole Troanedfe2c02019-07-30 15:38:13 +0200421 /* Write the message table */
422 if (fwrite (m, vec_len (m), 1, fp) != 1)
423 {
424 return (-14);
425 }
426 vec_free (m);
427
Filip Tehlar36217e32021-07-23 08:51:10 +0000428 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
Dave Barach371e4e12016-07-08 09:38:52 -0400431int
432vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
433 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434{
Dave Barach371e4e12016-07-08 09:38:52 -0400435 vl_api_trace_t *tp;
436 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Dave Barach371e4e12016-07-08 09:38:52 -0400438 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 {
440 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400441 tp = am->tx_trace;
442 if (tp == 0)
443 {
444 vec_validate (am->tx_trace, 0);
445 tp = am->tx_trace;
446 }
447 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
Dave Barach371e4e12016-07-08 09:38:52 -0400449 case VL_API_TRACE_RX:
450 tp = am->rx_trace;
451 if (tp == 0)
452 {
453 vec_validate (am->rx_trace, 0);
454 tp = am->rx_trace;
455 }
456
457 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
459 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400460 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
462 }
463
Dave Barach371e4e12016-07-08 09:38:52 -0400464 if (tp->enabled)
465 {
466 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 }
Dave Barach371e4e12016-07-08 09:38:52 -0400468 if (tp->traces)
469 {
470 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 }
472
Dave Barachb7b92992018-10-17 10:38:51 -0400473 clib_memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
Dave Barach371e4e12016-07-08 09:38:52 -0400475 if (clib_arch_is_big_endian)
476 {
477 tp->endian = VL_API_BIG_ENDIAN;
478 }
479 else
480 {
481 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 }
483
Dave Barach371e4e12016-07-08 09:38:52 -0400484 tp->nitems = nitems;
485 if (was_on)
486 {
487 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 }
Dave Barach371e4e12016-07-08 09:38:52 -0400489 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490}
491
Dave Barach80f54e22017-03-08 19:08:56 -0500492void
493vl_msg_api_barrier_sync (void)
494{
495}
496
497void
498vl_msg_api_barrier_release (void)
499{
500}
501
Dave Barach371e4e12016-07-08 09:38:52 -0400502always_inline void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100503msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
504 int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505{
Dave Barach9683c1e2019-07-01 09:42:41 -0400506 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Dave Barach371e4e12016-07-08 09:38:52 -0400507 u8 *(*print_fp) (void *, void *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barachb09f4d02019-07-15 16:00:03 -0400509 if (PREDICT_FALSE (am->elog_trace_api_messages))
510 {
511 /* *INDENT-OFF* */
512 ELOG_TYPE_DECLARE (e) =
513 {
514 .format = "api-msg: %s",
515 .format_args = "T4",
516 };
517 /* *INDENT-ON* */
518 struct
519 {
520 u32 c;
521 } *ed;
522 ed = ELOG_DATA (am->elog_main, e);
Andrew Yourtchenkof48de882021-02-06 12:28:52 +0000523 if (id < vec_len (am->msg_names) && am->msg_names[id])
Dave Barachb09f4d02019-07-15 16:00:03 -0400524 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
525 else
526 ed->c = elog_string (am->elog_main, "BOGUS");
527 }
528
Dave Barach371e4e12016-07-08 09:38:52 -0400529 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
530 {
531 if (trace_it)
532 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Dave Barach371e4e12016-07-08 09:38:52 -0400534 if (am->msg_print_flag)
535 {
536 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
537 print_fp = (void *) am->msg_print_handlers[id];
538 if (print_fp == 0)
539 {
540 fformat (stdout, " [no registered print fn]\n");
541 }
542 else
543 {
544 (*print_fp) (the_msg, stdout);
545 }
546 }
547
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100548 uword calc_size = 0;
549 uword (*calc_size_fp) (void *);
550 calc_size_fp = am->msg_calc_size_funcs[id];
551 ASSERT (NULL != calc_size_fp);
552 if (calc_size_fp)
Dave Barach371e4e12016-07-08 09:38:52 -0400553 {
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100554 calc_size = (*calc_size_fp) (the_msg);
555 ASSERT (calc_size <= msg_len);
556 if (calc_size > msg_len)
557 {
558 clib_warning (
559 "Truncated message '%s' (id %u) received, calculated size "
560 "%lu is bigger than actual size %llu, message dropped.",
561 am->msg_names[id], id, calc_size, msg_len);
562 }
563 }
564 else
565 {
566 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
567 "verify message size is correct",
568 am->msg_names[id], id);
569 }
570
571 /* don't process message if it's truncated, otherwise byte swaps
572 * and stuff could corrupt memory even beyond message if it's malicious
573 * e.g. VLA length field set to 1M elements, but VLA empty */
574 if (do_it && calc_size <= msg_len)
575 {
576
Dave Barach371e4e12016-07-08 09:38:52 -0400577 if (!am->is_mp_safe[id])
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100578 {
579 vl_msg_api_barrier_trace_context (am->msg_names[id]);
580 vl_msg_api_barrier_sync ();
581 }
Ole Troane796a182020-05-18 11:14:05 +0200582
583 if (am->is_autoendian[id])
584 {
585 void (*endian_fp) (void *);
586 endian_fp = am->msg_endian_handlers[id];
587 (*endian_fp) (the_msg);
588 }
589
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000590 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
591 clib_call_callbacks (am->perf_counter_cbs, am, id,
592 0 /* before */ );
593
Dave Barach371e4e12016-07-08 09:38:52 -0400594 (*am->msg_handlers[id]) (the_msg);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000595
596 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
597 clib_call_callbacks (am->perf_counter_cbs, am, id,
598 1 /* after */ );
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100599
Dave Barach371e4e12016-07-08 09:38:52 -0400600 if (!am->is_mp_safe[id])
601 vl_msg_api_barrier_release ();
602 }
603 }
604 else
605 {
606 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 }
608
Dave Barach371e4e12016-07-08 09:38:52 -0400609 if (free_it)
610 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Dave Barachb09f4d02019-07-15 16:00:03 -0400612 if (PREDICT_FALSE (am->elog_trace_api_messages))
613 {
614 /* *INDENT-OFF* */
615 ELOG_TYPE_DECLARE (e) =
616 {
617 .format = "api-msg-done(%s): %s",
618 .format_args = "t4T4",
619 .n_enum_strings = 2,
620 .enum_strings =
621 {
622 "barrier",
623 "mp-safe",
624 }
625 };
626 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
Dave Barachb09f4d02019-07-15 16:00:03 -0400628 struct
629 {
630 u32 barrier;
631 u32 c;
632 } *ed;
633 ed = ELOG_DATA (am->elog_main, e);
Andrew Yourtchenkof48de882021-02-06 12:28:52 +0000634 if (id < vec_len (am->msg_names) && am->msg_names[id])
Dave Barach67947872019-07-19 09:31:29 -0400635 {
636 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
637 ed->barrier = !am->is_mp_safe[id];
638 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400639 else
Dave Barach67947872019-07-19 09:31:29 -0400640 {
641 ed->c = elog_string (am->elog_main, "BOGUS");
642 ed->barrier = 0;
643 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400644 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Dave Barach77841402020-04-29 17:04:10 -0400647void (*vl_msg_api_fuzz_hook) (u16, void *);
648
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649/* This is only to be called from a vlib/vnet app */
Dave Barach371e4e12016-07-08 09:38:52 -0400650void
Florin Coras8d820852019-11-27 09:15:25 -0800651vl_msg_api_handler_with_vm_node (api_main_t * am, svm_region_t * vlib_rp,
Dave Barach371e4e12016-07-08 09:38:52 -0400652 void *the_msg, vlib_main_t * vm,
Florin Coras8d820852019-11-27 09:15:25 -0800653 vlib_node_runtime_t * node, u8 is_private)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654{
Dave Barach9683c1e2019-07-01 09:42:41 -0400655 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Dave Barach371e4e12016-07-08 09:38:52 -0400656 u8 *(*handler) (void *, void *, void *);
ezkexma75f9fb62019-03-21 07:38:19 -0400657 u8 *(*print_fp) (void *, void *);
Florin Coras8d820852019-11-27 09:15:25 -0800658 svm_region_t *old_vlib_rp;
659 void *save_shmem_hdr;
Dave Barachc898a4f2019-06-14 17:29:55 -0400660 int is_mp_safe = 1;
Dave Barach371e4e12016-07-08 09:38:52 -0400661
Dave Barachb09f4d02019-07-15 16:00:03 -0400662 if (PREDICT_FALSE (am->elog_trace_api_messages))
Dave Barach371e4e12016-07-08 09:38:52 -0400663 {
Dave Barachc3a06552018-10-01 09:25:32 -0400664 /* *INDENT-OFF* */
665 ELOG_TYPE_DECLARE (e) =
666 {
667 .format = "api-msg: %s",
668 .format_args = "T4",
669 };
670 /* *INDENT-ON* */
671 struct
672 {
673 u32 c;
674 } *ed;
Dave Barachb09f4d02019-07-15 16:00:03 -0400675 ed = ELOG_DATA (am->elog_main, e);
Andrew Yourtchenkof48de882021-02-06 12:28:52 +0000676 if (id < vec_len (am->msg_names) && am->msg_names[id])
Dave Barachb09f4d02019-07-15 16:00:03 -0400677 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
Dave Barachc3a06552018-10-01 09:25:32 -0400678 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400679 ed->c = elog_string (am->elog_main, "BOGUS");
Dave Barachc3a06552018-10-01 09:25:32 -0400680 }
Dave Barach371e4e12016-07-08 09:38:52 -0400681
682 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
683 {
684 handler = (void *) am->msg_handlers[id];
685
ezkexma75f9fb62019-03-21 07:38:19 -0400686 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
Dave Barach371e4e12016-07-08 09:38:52 -0400687 vl_msg_api_trace (am, am->rx_trace, the_msg);
688
ezkexma75f9fb62019-03-21 07:38:19 -0400689 if (PREDICT_FALSE (am->msg_print_flag))
690 {
691 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
692 print_fp = (void *) am->msg_print_handlers[id];
693 if (print_fp == 0)
694 {
695 fformat (stdout, " [no registered print fn for msg %d]\n", id);
696 }
697 else
698 {
699 (*print_fp) (the_msg, vm);
700 }
701 }
Dave Barachc898a4f2019-06-14 17:29:55 -0400702 is_mp_safe = am->is_mp_safe[id];
ezkexma75f9fb62019-03-21 07:38:19 -0400703
Dave Barachc898a4f2019-06-14 17:29:55 -0400704 if (!is_mp_safe)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100705 {
706 vl_msg_api_barrier_trace_context (am->msg_names[id]);
707 vl_msg_api_barrier_sync ();
708 }
Florin Coras8d820852019-11-27 09:15:25 -0800709 if (is_private)
710 {
711 old_vlib_rp = am->vlib_rp;
712 save_shmem_hdr = am->shmem_hdr;
713 am->vlib_rp = vlib_rp;
714 am->shmem_hdr = (void *) vlib_rp->user_ctx;
715 }
Dave Barach77841402020-04-29 17:04:10 -0400716
717 if (PREDICT_FALSE (vl_msg_api_fuzz_hook != 0))
718 (*vl_msg_api_fuzz_hook) (id, the_msg);
719
Ole Troane796a182020-05-18 11:14:05 +0200720 if (am->is_autoendian[id])
721 {
722 void (*endian_fp) (void *);
723 endian_fp = am->msg_endian_handlers[id];
724 (*endian_fp) (the_msg);
725 }
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000726 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
727 clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */ );
Ole Troane796a182020-05-18 11:14:05 +0200728
Dave Barach371e4e12016-07-08 09:38:52 -0400729 (*handler) (the_msg, vm, node);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000730
731 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
732 clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */ );
Florin Coras8d820852019-11-27 09:15:25 -0800733 if (is_private)
734 {
735 am->vlib_rp = old_vlib_rp;
736 am->shmem_hdr = save_shmem_hdr;
737 }
Dave Barachc898a4f2019-06-14 17:29:55 -0400738 if (!is_mp_safe)
Dave Barach371e4e12016-07-08 09:38:52 -0400739 vl_msg_api_barrier_release ();
740 }
741 else
742 {
Jon Loeligerf95b37e2017-02-13 15:21:12 -0600743 clib_warning ("no handler for msg id %d", id);
Dave Barach371e4e12016-07-08 09:38:52 -0400744 }
745
746 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 * Special-case, so we can e.g. bounce messages off the vnet
748 * main thread without copying them...
749 */
Benoît Ganneff13e462020-01-21 18:33:14 +0100750 if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id]))
wanghanlin9f42fff2021-05-08 11:39:53 +0800751 {
752 if (is_private)
753 {
754 old_vlib_rp = am->vlib_rp;
755 save_shmem_hdr = am->shmem_hdr;
756 am->vlib_rp = vlib_rp;
757 am->shmem_hdr = (void *) vlib_rp->user_ctx;
758 }
759 vl_msg_api_free (the_msg);
760 if (is_private)
761 {
762 am->vlib_rp = old_vlib_rp;
763 am->shmem_hdr = save_shmem_hdr;
764 }
765 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Dave Barachb09f4d02019-07-15 16:00:03 -0400767 if (PREDICT_FALSE (am->elog_trace_api_messages))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 {
Dave Barachc3a06552018-10-01 09:25:32 -0400769 /* *INDENT-OFF* */
Dave Barachc898a4f2019-06-14 17:29:55 -0400770 ELOG_TYPE_DECLARE (e) =
771 {
772 .format = "api-msg-done(%s): %s",
773 .format_args = "t4T4",
774 .n_enum_strings = 2,
775 .enum_strings =
776 {
777 "barrier",
778 "mp-safe",
779 }
780 };
Dave Barachc3a06552018-10-01 09:25:32 -0400781 /* *INDENT-ON* */
782
783 struct
784 {
Dave Barachc898a4f2019-06-14 17:29:55 -0400785 u32 barrier;
Dave Barachc3a06552018-10-01 09:25:32 -0400786 u32 c;
787 } *ed;
Dave Barachb09f4d02019-07-15 16:00:03 -0400788 ed = ELOG_DATA (am->elog_main, e);
Andrew Yourtchenkof48de882021-02-06 12:28:52 +0000789 if (id < vec_len (am->msg_names) && am->msg_names[id])
Dave Barachb09f4d02019-07-15 16:00:03 -0400790 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
Dave Barachc3a06552018-10-01 09:25:32 -0400791 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400792 ed->c = elog_string (am->elog_main, "BOGUS");
Dave Barachc898a4f2019-06-14 17:29:55 -0400793 ed->barrier = is_mp_safe;
Dave Barachc3a06552018-10-01 09:25:32 -0400794 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795}
796
Dave Barach371e4e12016-07-08 09:38:52 -0400797void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100798vl_msg_api_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799{
Dave Barach39d69112019-11-27 11:42:13 -0500800 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100802 msg_handler_internal (am, the_msg, msg_len,
803 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
804 1 /* do_it */, 1 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805}
806
Dave Barach371e4e12016-07-08 09:38:52 -0400807void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100808vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809{
Dave Barach39d69112019-11-27 11:42:13 -0500810 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100811 msg_handler_internal (am, the_msg, msg_len,
812 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
813 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814}
815
Dave Barach371e4e12016-07-08 09:38:52 -0400816void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100817vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818{
Dave Barach39d69112019-11-27 11:42:13 -0500819 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100820 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
821 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822}
823
824/*
825 * Add a trace record to the API message trace buffer, if
826 * API message tracing is enabled. Handy for adding sufficient
827 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400828 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 * application creates database entries based on packet traffic, not
830 * control-plane messages.
831 *
832 */
Dave Barach371e4e12016-07-08 09:38:52 -0400833void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100834vl_msg_api_trace_only (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835{
Dave Barach39d69112019-11-27 11:42:13 -0500836 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100838 msg_handler_internal (am, the_msg, msg_len,
839 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
840 0 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841}
842
Dave Barach371e4e12016-07-08 09:38:52 -0400843void
844vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845{
Dave Barach39d69112019-11-27 11:42:13 -0500846 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400847 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848
Dave Barach371e4e12016-07-08 09:38:52 -0400849 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
850 {
851 clib_warning ("_vl_msg_id too large: %d\n", id);
852 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 }
Dave Barach371e4e12016-07-08 09:38:52 -0400854 if (am->msg_cleanup_handlers[id])
855 (*am->msg_cleanup_handlers[id]) (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Dave Barach371e4e12016-07-08 09:38:52 -0400857 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858}
859
860/*
861 * vl_msg_api_replay_handler
862 */
Dave Barach371e4e12016-07-08 09:38:52 -0400863void
864vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865{
Dave Barach39d69112019-11-27 11:42:13 -0500866 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Dave Barach9683c1e2019-07-01 09:42:41 -0400868 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869
Dave Barach371e4e12016-07-08 09:38:52 -0400870 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
871 {
872 clib_warning ("_vl_msg_id too large: %d\n", id);
873 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 }
Dave Barach371e4e12016-07-08 09:38:52 -0400875 /* do NOT trace the message... */
876 if (am->msg_handlers[id])
877 (*am->msg_handlers[id]) (the_msg);
878 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879}
Dave Barach371e4e12016-07-08 09:38:52 -0400880
Dave Barachbfd92272017-05-12 11:59:25 -0400881u32
882vl_msg_api_get_msg_length (void *msg_arg)
883{
884 return vl_msg_api_get_msg_length_inline (msg_arg);
885}
886
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887/*
888 * vl_msg_api_socket_handler
889 */
Dave Barach371e4e12016-07-08 09:38:52 -0400890void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100891vl_msg_api_socket_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892{
Dave Barach39d69112019-11-27 11:42:13 -0500893 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100895 msg_handler_internal (am, the_msg, msg_len,
896 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
897 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898}
899
Filip Tehlar36217e32021-07-23 08:51:10 +0000900#define foreach_msg_api_vector \
901 _ (msg_names) \
902 _ (msg_handlers) \
903 _ (msg_cleanup_handlers) \
904 _ (msg_endian_handlers) \
905 _ (msg_print_handlers) \
906 _ (msg_print_json_handlers) \
907 _ (msg_tojson_handlers) \
908 _ (msg_fromjson_handlers) \
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100909 _ (msg_calc_size_funcs) \
Filip Tehlar36217e32021-07-23 08:51:10 +0000910 _ (api_trace_cfg) \
911 _ (message_bounce) \
912 _ (is_mp_safe) \
913 _ (is_autoendian)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Dave Barach371e4e12016-07-08 09:38:52 -0400915void
916vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917{
Dave Barach39d69112019-11-27 11:42:13 -0500918 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919
Dave Barach65457162017-10-10 17:53:14 -0400920 /*
921 * This happens during the java core tests if the message
922 * dictionary is missing newly added xxx_reply_t messages.
923 * Should never happen, but since I shot myself in the foot once
924 * this way, I thought I'd make it easy to debug if I ever do
925 * it again... (;-)...
926 */
927 if (c->id == 0)
928 {
929 if (c->name)
930 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
931 else
932 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
933 clib_warning ("Did you forget to call setup_message_id_table?");
934 return;
935 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
937#define _(a) vec_validate (am->a, c->id);
Dave Barach371e4e12016-07-08 09:38:52 -0400938 foreach_msg_api_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939#undef _
940
Dave Barach392206e2017-09-20 08:40:16 -0400941 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
942 clib_warning
943 ("BUG: re-registering 'vl_api_%s_t_handler'."
944 "Handler was %llx, replaced by %llx",
945 c->name, am->msg_handlers[c->id], c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500946
Dave Barach371e4e12016-07-08 09:38:52 -0400947 am->msg_names[c->id] = c->name;
948 am->msg_handlers[c->id] = c->handler;
949 am->msg_cleanup_handlers[c->id] = c->cleanup;
950 am->msg_endian_handlers[c->id] = c->endian;
951 am->msg_print_handlers[c->id] = c->print;
Filip Tehlar36217e32021-07-23 08:51:10 +0000952 am->msg_print_json_handlers[c->id] = c->print_json;
953 am->msg_tojson_handlers[c->id] = c->tojson;
954 am->msg_fromjson_handlers[c->id] = c->fromjson;
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100955 am->msg_calc_size_funcs[c->id] = c->calc_size;
Dave Barach371e4e12016-07-08 09:38:52 -0400956 am->message_bounce[c->id] = c->message_bounce;
957 am->is_mp_safe[c->id] = c->is_mp_safe;
Ole Troane796a182020-05-18 11:14:05 +0200958 am->is_autoendian[c->id] = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
Dave Barach371e4e12016-07-08 09:38:52 -0400960 am->api_trace_cfg[c->id].size = c->size;
961 am->api_trace_cfg[c->id].trace_enable = c->traced;
962 am->api_trace_cfg[c->id].replay_enable = c->replay;
Filip Tehlar36217e32021-07-23 08:51:10 +0000963
964 if (!am->msg_id_by_name)
965 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
966
967 hash_set_mem (am->msg_id_by_name, c->name, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968}
969
Dave Barach371e4e12016-07-08 09:38:52 -0400970/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 * vl_msg_api_set_handlers
972 * preserve the old API for a while
973 */
Dave Barach371e4e12016-07-08 09:38:52 -0400974void
975vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
Filip Tehlar36217e32021-07-23 08:51:10 +0000976 void *endian, void *print, int size, int traced,
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100977 void *print_json, void *tojson, void *fromjson,
978 void *calc_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979{
Dave Barach371e4e12016-07-08 09:38:52 -0400980 vl_msg_api_msg_config_t cfg;
981 vl_msg_api_msg_config_t *c = &cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Dave Barachb7b92992018-10-17 10:38:51 -0400983 clib_memset (c, 0, sizeof (*c));
Dave Barach0691d6e2017-01-05 10:08:52 -0500984
Dave Barach371e4e12016-07-08 09:38:52 -0400985 c->id = id;
986 c->name = name;
987 c->handler = handler;
988 c->cleanup = cleanup;
989 c->endian = endian;
990 c->print = print;
Dave Barach371e4e12016-07-08 09:38:52 -0400991 c->traced = traced;
992 c->replay = 1;
993 c->message_bounce = 0;
994 c->is_mp_safe = 0;
Ole Troane796a182020-05-18 11:14:05 +0200995 c->is_autoendian = 0;
Filip Tehlar36217e32021-07-23 08:51:10 +0000996 c->tojson = tojson;
997 c->fromjson = fromjson;
998 c->print_json = print_json;
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100999 c->calc_size = calc_size;
Dave Barach371e4e12016-07-08 09:38:52 -04001000 vl_msg_api_config (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001}
1002
Dave Barach371e4e12016-07-08 09:38:52 -04001003void
Matej Perina75a17ec2017-09-21 17:03:27 +02001004vl_msg_api_clean_handlers (int msg_id)
1005{
1006 vl_msg_api_msg_config_t cfg;
1007 vl_msg_api_msg_config_t *c = &cfg;
1008
Dave Barachb7b92992018-10-17 10:38:51 -04001009 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +02001010
1011 c->id = msg_id;
1012 vl_msg_api_config (c);
1013}
1014
1015void
Dave Barach371e4e12016-07-08 09:38:52 -04001016vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017{
Dave Barach39d69112019-11-27 11:42:13 -05001018 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001019 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020
Dave Barach371e4e12016-07-08 09:38:52 -04001021 vec_validate (am->msg_cleanup_handlers, msg_id);
1022 am->msg_cleanup_handlers[msg_id] = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023}
1024
Dave Barach371e4e12016-07-08 09:38:52 -04001025void
Florin Corase86a8ed2018-01-05 03:20:25 -08001026vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027{
Dave Barach371e4e12016-07-08 09:38:52 -04001028 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
Klement Sekera9b7e8ac2021-11-22 21:26:20 +01001030 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
1031 {
1032 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
1033 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
1034 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035}
1036
Dave Barach77841402020-04-29 17:04:10 -04001037u32
1038vl_msg_api_max_length (void *mp)
1039{
1040 msgbuf_t *mb;
1041 u32 data_len = ~0;
1042
1043 /* Work out the maximum sane message length, and return it */
1044 if (PREDICT_TRUE (mp != 0))
1045 {
1046 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
1047 data_len = clib_net_to_host_u32 (mb->data_len);
1048 }
1049 return data_len;
1050}
1051
Dave Barach371e4e12016-07-08 09:38:52 -04001052vl_api_trace_t *
1053vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054{
Dave Barach371e4e12016-07-08 09:38:52 -04001055 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 {
1057 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -04001058 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -04001060 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 default:
Dave Barach371e4e12016-07-08 09:38:52 -04001062 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 }
1064}
1065
Dave Barach371e4e12016-07-08 09:38:52 -04001066void
1067vl_noop_handler (void *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069}
1070
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071
1072static u8 post_mortem_dump_enabled;
1073
Dave Barach80f54e22017-03-08 19:08:56 -05001074void
1075vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076{
Dave Barach80f54e22017-03-08 19:08:56 -05001077 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078}
1079
Dave Barach371e4e12016-07-08 09:38:52 -04001080void
1081vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082{
Dave Barach39d69112019-11-27 11:42:13 -05001083 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001084 FILE *fp;
1085 char filename[64];
1086 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
Dave Barach371e4e12016-07-08 09:38:52 -04001088 if (post_mortem_dump_enabled == 0)
1089 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090
Dave Barach371e4e12016-07-08 09:38:52 -04001091 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
1092 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093
Dave Barach371e4e12016-07-08 09:38:52 -04001094 fp = fopen (filename, "w");
1095 if (fp == NULL)
1096 {
1097 rv = write (2, "Couldn't create ", 16);
1098 rv = write (2, filename, strlen (filename));
1099 rv = write (2, "\n", 1);
1100 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101 }
Filip Tehlar36217e32021-07-23 08:51:10 +00001102 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
Dave Barach371e4e12016-07-08 09:38:52 -04001103 fclose (fp);
1104 if (rv < 0)
1105 {
1106 rv = write (2, "Failed to save post-mortem API trace to ", 40);
1107 rv = write (2, filename, strlen (filename));
1108 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 }
Dave Barach371e4e12016-07-08 09:38:52 -04001110
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111}
1112
1113/* Layered message handling support */
1114
Dave Barach371e4e12016-07-08 09:38:52 -04001115void
Dave Barach371e4e12016-07-08 09:38:52 -04001116vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117{
Dave Barach39d69112019-11-27 11:42:13 -05001118 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001119
1120 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121}
1122
Dave Barach371e4e12016-07-08 09:38:52 -04001123u16
Neale Rannse72be392017-04-26 13:59:20 -07001124vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125{
Dave Barach39d69112019-11-27 11:42:13 -05001126 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001127 u8 *name_copy;
1128 vl_api_msg_range_t *rp;
1129 uword *p;
1130 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131
Dave Barach371e4e12016-07-08 09:38:52 -04001132 if (am->msg_range_by_name == 0)
1133 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134
Dave Barach371e4e12016-07-08 09:38:52 -04001135 name_copy = format (0, "%s%c", name, 0);
1136
1137 p = hash_get_mem (am->msg_range_by_name, name_copy);
1138 if (p)
1139 {
1140 clib_warning ("WARNING: duplicate message range registration for '%s'",
1141 name_copy);
1142 vec_free (name_copy);
1143 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 }
1145
Dave Barach371e4e12016-07-08 09:38:52 -04001146 if (n < 0 || n > 1024)
1147 {
1148 clib_warning
1149 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
1150 n, name_copy);
1151 vec_free (name_copy);
1152 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 }
1154
Dave Barach371e4e12016-07-08 09:38:52 -04001155 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156
Dave Barach371e4e12016-07-08 09:38:52 -04001157 rv = rp->first_msg_id = am->first_available_msg_id;
1158 am->first_available_msg_id += n;
1159 rp->last_msg_id = am->first_available_msg_id - 1;
1160 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161
Dave Barach371e4e12016-07-08 09:38:52 -04001162 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163
Dave Barach371e4e12016-07-08 09:38:52 -04001164 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165}
Dave Barach371e4e12016-07-08 09:38:52 -04001166
Dave Barach557d1282016-11-10 14:22:49 -05001167void
Neale Rannse72be392017-04-26 13:59:20 -07001168vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -05001169{
1170 uword *p;
1171
1172 if (am->msg_index_by_name_and_crc == 0)
1173 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1174
1175 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1176 if (p)
1177 {
1178 clib_warning ("attempt to redefine '%s' ignored...", string);
1179 return;
1180 }
1181
1182 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1183}
1184
Dave Barach0d056e52017-09-28 15:11:16 -04001185void
1186vl_msg_api_add_version (api_main_t * am, const char *string,
1187 u32 major, u32 minor, u32 patch)
1188{
1189 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1190 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +02001191 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -04001192 vec_add1 (am->api_version_list, version);
1193}
Dave Barach557d1282016-11-10 14:22:49 -05001194
Florin Corase86a8ed2018-01-05 03:20:25 -08001195u32
1196vl_msg_api_get_msg_index (u8 * name_and_crc)
1197{
Dave Barach39d69112019-11-27 11:42:13 -05001198 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -08001199 uword *p;
1200
1201 if (am->msg_index_by_name_and_crc)
1202 {
1203 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1204 if (p)
1205 return p[0];
1206 }
1207 return ~0;
1208}
1209
Ole Troan73710c72018-06-04 22:27:49 +02001210void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001211vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1212{
1213 pthread_mutex_lock (&vlib_rp->mutex);
1214 return svm_push_data_heap (vlib_rp);
1215}
1216
1217void *
Ole Troan73710c72018-06-04 22:27:49 +02001218vl_msg_push_heap (void)
1219{
Dave Barach39d69112019-11-27 11:42:13 -05001220 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001221 return vl_msg_push_heap_w_region (am->vlib_rp);
1222}
1223
1224void
1225vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1226{
1227 svm_pop_heap (oldheap);
1228 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001229}
1230
1231void
1232vl_msg_pop_heap (void *oldheap)
1233{
Dave Barach39d69112019-11-27 11:42:13 -05001234 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001235 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001236}
1237
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001238/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001239int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001240vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001241{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001242 /* copy without nul terminator */
1243 u32 len = strlen (buf);
1244 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001245 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001246 str->length = htonl (len);
1247 return len + sizeof (u32);
1248}
1249
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001250/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001251int
1252vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1253{
1254 u32 len = vec_len (vec);
1255 clib_memcpy (str->buf, vec, len);
1256 str->length = htonl (len);
1257 return len + sizeof (u32);
1258}
1259
Neale Ranns377860a2019-06-21 07:57:18 -07001260u32
1261vl_api_string_len (vl_api_string_t * astr)
1262{
Dave Barach9683c1e2019-07-01 09:42:41 -04001263 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001264}
1265
Ole Troan33a58172019-09-04 09:12:29 +02001266u8 *
1267vl_api_format_string (u8 * s, va_list * args)
1268{
1269 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1270 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1271 return s;
1272}
1273
Neale Ranns377860a2019-06-21 07:57:18 -07001274/*
1275 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001276 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001277 */
1278u8 *
Dave Barach77841402020-04-29 17:04:10 -04001279vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001280{
1281 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001282
1283 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1284 return format (0, "insane astr->length %u%c",
1285 clib_net_to_host_u32 (astr->length), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001286 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001287 return v;
1288}
Ole Troan73710c72018-06-04 22:27:49 +02001289
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001290/*
1291 * Returns a new vector. Remember to free it after use.
1292 * Nul terminated.
1293 */
1294char *
1295vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1296{
1297 char *v = 0;
1298 if (clib_net_to_host_u32 (astr->length) > 0)
1299 {
1300 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1301 vec_add1 (v, 0);
1302 }
1303 return v;
1304}
1305
Dave Barachb09f4d02019-07-15 16:00:03 -04001306void
1307vl_api_set_elog_main (elog_main_t * m)
1308{
Dave Barach39d69112019-11-27 11:42:13 -05001309 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001310 am->elog_main = m;
1311}
1312
1313int
1314vl_api_set_elog_trace_api_messages (int enable)
1315{
1316 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001317 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001318
1319 rv = am->elog_trace_api_messages;
1320 am->elog_trace_api_messages = enable;
1321 return rv;
1322}
1323
1324int
1325vl_api_get_elog_trace_api_messages (void)
1326{
Dave Barach39d69112019-11-27 11:42:13 -05001327 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001328
1329 return am->elog_trace_api_messages;
1330}
1331
Dave Barach371e4e12016-07-08 09:38:52 -04001332/*
1333 * fd.io coding-style-patch-verification: ON
1334 *
1335 * Local Variables:
1336 * eval: (c-set-style "gnu")
1337 * End:
1338 */