blob: 98da604a4568702ec670b46894677b8dafe8a5a9 [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* */
BenoƮt Ganne7f9256e2022-03-04 17:17:04 +010036static api_main_t api_global_main = {
37 .region_name = "/unset",
38 .api_uid = -1,
39 .api_gid = -1,
40};
Dave Barach80f54e22017-03-08 19:08:56 -050041/* *INDENT-ON* */
Dave Barach371e4e12016-07-08 09:38:52 -040042
Dave Barach39d69112019-11-27 11:42:13 -050043/* Please use vlibapi_get_main() to access my_api_main */
44__thread api_main_t *my_api_main = &api_global_main;
45
46void
47vl_msg_api_set_global_main (void *am_arg)
48{
49 ASSERT (am_arg);
50 my_api_main = (api_main_t *) am_arg;
51}
52
Dave Barach371e4e12016-07-08 09:38:52 -040053void
54vl_msg_api_increment_missing_client_counter (void)
55{
Dave Barach39d69112019-11-27 11:42:13 -050056 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -040057 am->missing_clients++;
58}
59
Dave Barach371e4e12016-07-08 09:38:52 -040060int
61vl_msg_api_rx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Dave Barach371e4e12016-07-08 09:38:52 -040063 return (am->rx_trace && am->rx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070064}
65
Dave Barach371e4e12016-07-08 09:38:52 -040066int
67vl_msg_api_tx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach371e4e12016-07-08 09:38:52 -040069 return (am->tx_trace && am->tx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070}
71
72/*
73 * vl_msg_api_trace
74 */
Dave Barach371e4e12016-07-08 09:38:52 -040075void
76vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barach371e4e12016-07-08 09:38:52 -040078 u8 **this_trace;
79 u8 **old_trace;
80 u8 *msg_copy;
Dave Barach072f8de2016-12-02 13:31:25 -050081 u32 length;
Dave Barach9683c1e2019-07-01 09:42:41 -040082 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +020083 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach072f8de2016-12-02 13:31:25 -050084 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
Damjan Marioncada9eb2022-05-18 22:16:11 +020086 if (!m || !m->trace_enable)
Dave Barach371e4e12016-07-08 09:38:52 -040087 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Dave Barach371e4e12016-07-08 09:38:52 -040089 msg_copy = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach371e4e12016-07-08 09:38:52 -040091 if (tp->nitems == 0)
92 {
93 clib_warning ("tp->nitems is 0");
94 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 }
96
Dave Barach371e4e12016-07-08 09:38:52 -040097 if (vec_len (tp->traces) < tp->nitems)
98 {
99 vec_add1 (tp->traces, 0);
100 this_trace = tp->traces + vec_len (tp->traces) - 1;
101 }
102 else
103 {
104 tp->wrapped = 1;
105 old_trace = tp->traces + tp->curindex++;
106 if (tp->curindex == tp->nitems)
107 tp->curindex = 0;
Dave Barachf35a0722019-06-12 16:50:38 -0400108 /* Reuse the trace record, may save some memory allocator traffic */
109 msg_copy = *old_trace;
110 vec_reset_length (msg_copy);
Dave Barach371e4e12016-07-08 09:38:52 -0400111 this_trace = old_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 }
113
Dave Barach072f8de2016-12-02 13:31:25 -0500114 length = clib_net_to_host_u32 (header->data_len);
115
116 vec_validate (msg_copy, length - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500117 clib_memcpy_fast (msg_copy, msg, length);
Dave Barach371e4e12016-07-08 09:38:52 -0400118 *this_trace = msg_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
Dave Barach371e4e12016-07-08 09:38:52 -0400121int
122vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
123 int onoff)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barach371e4e12016-07-08 09:38:52 -0400125 vl_api_trace_t *tp;
126 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Dave Barach371e4e12016-07-08 09:38:52 -0400128 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 {
130 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400131 tp = am->tx_trace;
132 if (tp == 0)
133 {
134 vl_msg_api_trace_configure (am, which, 1024);
135 tp = am->tx_trace;
136 }
137 break;
138
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400140 tp = am->rx_trace;
141 if (tp == 0)
142 {
143 vl_msg_api_trace_configure (am, which, 1024);
144 tp = am->rx_trace;
145 }
146 break;
147
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400149 /* duh? */
150 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 }
152
Dave Barach371e4e12016-07-08 09:38:52 -0400153 /* Configured? */
154 if (tp == 0 || tp->nitems == 0)
155 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Dave Barach371e4e12016-07-08 09:38:52 -0400157 rv = tp->enabled;
158 tp->enabled = onoff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach371e4e12016-07-08 09:38:52 -0400160 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161}
162
Dave Barach371e4e12016-07-08 09:38:52 -0400163int
164vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165{
Dave Barach371e4e12016-07-08 09:38:52 -0400166 vl_api_trace_t *tp;
167 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach371e4e12016-07-08 09:38:52 -0400169 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 {
171 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400172 tp = am->tx_trace;
173 break;
174
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400176 tp = am->rx_trace;
177 break;
178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400180 /* duh? */
181 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 }
183
Dave Barach371e4e12016-07-08 09:38:52 -0400184 /* Configured? */
185 if (!tp || tp->nitems == 0)
186 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Dave Barach371e4e12016-07-08 09:38:52 -0400188 tp->curindex = 0;
189 tp->wrapped = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barach371e4e12016-07-08 09:38:52 -0400191 for (i = 0; i < vec_len (tp->traces); i++)
192 {
193 vec_free (tp->traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 }
Dave Barach371e4e12016-07-08 09:38:52 -0400195 vec_free (tp->traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barach371e4e12016-07-08 09:38:52 -0400197 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198}
199
Ole Troanedfe2c02019-07-30 15:38:13 +0200200u8 *
201vl_api_serialize_message_table (api_main_t * am, u8 * vector)
202{
203 serialize_main_t _sm, *sm = &_sm;
204 hash_pair_t *hp;
205 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
206
207 serialize_open_vector (sm, vector);
208
209 /* serialize the count */
210 serialize_integer (sm, nmsg, sizeof (u32));
211
212 /* *INDENT-OFF* */
213 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
214 ({
215 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
216 serialize_cstring (sm, (char *) hp->key);
217 }));
218 /* *INDENT-ON* */
219
220 return serialize_close_vector (sm);
221}
222
Filip Tehlar36217e32021-07-23 08:51:10 +0000223static int
224vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
225{
226 u8 *tmpmem = 0;
227 int tlen, slen;
Filip Tehlar36217e32021-07-23 08:51:10 +0000228
229 u32 msg_length = vec_len (msg);
230 vec_validate (tmpmem, msg_length - 1);
231 clib_memcpy_fast (tmpmem, msg, msg_length);
232 u16 id = clib_net_to_host_u16 (*((u16 *) msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200233 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Filip Tehlar36217e32021-07-23 08:51:10 +0000234
Damjan Marioncada9eb2022-05-18 22:16:11 +0200235 m->endian_handler (tmpmem);
Filip Tehlar36217e32021-07-23 08:51:10 +0000236
Damjan Marioncada9eb2022-05-18 22:16:11 +0200237 if (m && m->tojson_handler)
Filip Tehlar36217e32021-07-23 08:51:10 +0000238 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200239 cJSON *o = m->tojson_handler (tmpmem);
Filip Tehlar36217e32021-07-23 08:51:10 +0000240 char *s = cJSON_Print (o);
241 slen = strlen (s);
242 tlen = fwrite (s, 1, slen, fp);
243 cJSON_free (s);
244 cJSON_Delete (o);
245 vec_free (tmpmem);
246 if (tlen != slen)
247 {
248 fformat (stderr, "writing to file error\n");
249 return -11;
250 }
251 }
252 else
253 fformat (stderr, " [no registered tojson fn]\n");
254
255 return 0;
256}
257
258#define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
259
260typedef struct
261{
262 FILE *fp;
263 u32 n_traces;
264 u32 i;
265} vl_msg_write_json_args_t;
266
267static int
268vl_msg_write_json_fn (u8 *msg, void *ctx)
269{
270 vl_msg_write_json_args_t *arg = ctx;
271 FILE *fp = arg->fp;
272 api_main_t *am = vlibapi_get_main ();
273 int rc = vl_msg_api_trace_write_one (am, msg, fp);
274 if (rc < 0)
275 return rc;
276
277 if (arg->i < arg->n_traces - 1)
278 vl_msg_fwrite (",\n", fp);
279 arg->i++;
280 return 0;
281}
282
283static int
284vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
285{
286 vl_msg_write_json_args_t args;
287 clib_memset (&args, 0, sizeof (args));
288 args.fp = fp;
289 args.n_traces = vec_len (tp->traces);
290 vl_msg_fwrite ("[\n", fp);
291
292 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
293 if (rv < 0)
294 return rv;
295
296 vl_msg_fwrite ("]", fp);
297 return 0;
298}
299
Dave Barach371e4e12016-07-08 09:38:52 -0400300int
Filip Tehlar36217e32021-07-23 08:51:10 +0000301vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
302 void *ctx)
303{
304 int i;
305 u8 *msg;
306 int rv = 0;
307
308 /* No-wrap case */
309 if (tp->wrapped == 0)
310 {
311 for (i = 0; i < vec_len (tp->traces); i++)
312 {
313 /*sa_ignore NO_NULL_CHK */
314 msg = tp->traces[i];
315 if (!msg)
316 continue;
317
318 rv = fn (msg, ctx);
319 if (rv < 0)
320 return rv;
321 }
322 }
323 else
324 {
325 /* Wrap case: write oldest -> end of buffer */
326 for (i = tp->curindex; i < vec_len (tp->traces); i++)
327 {
328 msg = tp->traces[i];
329 if (!msg)
330 continue;
331
332 rv = fn (msg, ctx);
333 if (rv < 0)
334 return rv;
335 }
336 /* write beginning of buffer -> oldest-1 */
337 for (i = 0; i < tp->curindex; i++)
338 {
339 /*sa_ignore NO_NULL_CHK */
340 msg = tp->traces[i];
341 if (!msg)
342 continue;
343
344 rv = fn (msg, ctx);
345 if (rv < 0)
346 return rv;
347 }
348 }
349 return 0;
350}
351
352static int
353vl_api_msg_write_fn (u8 *msg, void *ctx)
354{
355 FILE *fp = ctx;
356 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
357 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
358 {
359 return (-14);
360 }
361 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
362 {
363 return (-14);
364 }
365 return 0;
366}
367
368int
369vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
370 u8 is_json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371{
Dave Barach371e4e12016-07-08 09:38:52 -0400372 vl_api_trace_t *tp;
373 vl_api_trace_file_header_t fh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
Dave Barach371e4e12016-07-08 09:38:52 -0400375 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 {
377 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400378 tp = am->tx_trace;
379 break;
380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400382 tp = am->rx_trace;
383 break;
384
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400386 /* duh? */
387 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 }
389
Dave Barach371e4e12016-07-08 09:38:52 -0400390 /* Configured, data present? */
391 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
392 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Dave Barach371e4e12016-07-08 09:38:52 -0400394 /* "Dare to be stupid" check */
395 if (fp == 0)
396 {
397 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 }
399
Filip Tehlar36217e32021-07-23 08:51:10 +0000400 if (is_json)
401 return vl_msg_api_trace_write_json (am, tp, fp);
402
Dave Barach371e4e12016-07-08 09:38:52 -0400403 /* Write the file header */
Dave Barach371e4e12016-07-08 09:38:52 -0400404 fh.wrapped = tp->wrapped;
Ole Troanedfe2c02019-07-30 15:38:13 +0200405 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
Filip Tehlar36217e32021-07-23 08:51:10 +0000406
Ole Troanedfe2c02019-07-30 15:38:13 +0200407 u8 *m = vl_api_serialize_message_table (am, 0);
Ole Troanedfe2c02019-07-30 15:38:13 +0200408 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Dave Barach371e4e12016-07-08 09:38:52 -0400410 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
411 {
412 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 }
414
Ole Troanedfe2c02019-07-30 15:38:13 +0200415 /* Write the message table */
416 if (fwrite (m, vec_len (m), 1, fp) != 1)
417 {
418 return (-14);
419 }
420 vec_free (m);
421
Filip Tehlar36217e32021-07-23 08:51:10 +0000422 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423}
424
Dave Barach371e4e12016-07-08 09:38:52 -0400425int
426vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
427 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428{
Dave Barach371e4e12016-07-08 09:38:52 -0400429 vl_api_trace_t *tp;
430 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Dave Barach371e4e12016-07-08 09:38:52 -0400432 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 {
434 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400435 tp = am->tx_trace;
436 if (tp == 0)
437 {
438 vec_validate (am->tx_trace, 0);
439 tp = am->tx_trace;
440 }
441 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Dave Barach371e4e12016-07-08 09:38:52 -0400443 case VL_API_TRACE_RX:
444 tp = am->rx_trace;
445 if (tp == 0)
446 {
447 vec_validate (am->rx_trace, 0);
448 tp = am->rx_trace;
449 }
450
451 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
453 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400454 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456 }
457
Dave Barach371e4e12016-07-08 09:38:52 -0400458 if (tp->enabled)
459 {
460 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 }
Dave Barach371e4e12016-07-08 09:38:52 -0400462 if (tp->traces)
463 {
464 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 }
466
Dave Barachb7b92992018-10-17 10:38:51 -0400467 clib_memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Dave Barach371e4e12016-07-08 09:38:52 -0400469 if (clib_arch_is_big_endian)
470 {
471 tp->endian = VL_API_BIG_ENDIAN;
472 }
473 else
474 {
475 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 }
477
Dave Barach371e4e12016-07-08 09:38:52 -0400478 tp->nitems = nitems;
479 if (was_on)
480 {
481 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 }
Dave Barach371e4e12016-07-08 09:38:52 -0400483 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484}
485
Dave Barach80f54e22017-03-08 19:08:56 -0500486void
487vl_msg_api_barrier_sync (void)
488{
489}
490
491void
492vl_msg_api_barrier_release (void)
493{
494}
495
Dave Barach371e4e12016-07-08 09:38:52 -0400496always_inline void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100497msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
498 int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499{
Dave Barach9683c1e2019-07-01 09:42:41 -0400500 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200501 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502
Dave Barachb09f4d02019-07-15 16:00:03 -0400503 if (PREDICT_FALSE (am->elog_trace_api_messages))
504 {
505 /* *INDENT-OFF* */
506 ELOG_TYPE_DECLARE (e) =
507 {
508 .format = "api-msg: %s",
509 .format_args = "T4",
510 };
511 /* *INDENT-ON* */
512 struct
513 {
514 u32 c;
515 } *ed;
516 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200517 if (m && m->name)
518 ed->c = elog_string (am->elog_main, (char *) m->name);
Dave Barachb09f4d02019-07-15 16:00:03 -0400519 else
520 ed->c = elog_string (am->elog_main, "BOGUS");
521 }
522
Damjan Marioncada9eb2022-05-18 22:16:11 +0200523 if (m && m->handler)
Dave Barach371e4e12016-07-08 09:38:52 -0400524 {
525 if (trace_it)
526 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Dave Barach371e4e12016-07-08 09:38:52 -0400528 if (am->msg_print_flag)
529 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200530 fformat (stdout, "[%d]: %s\n", id, m->name);
531 if (m->print_handler)
532 m->print_handler (the_msg, stdout);
Dave Barach371e4e12016-07-08 09:38:52 -0400533 else
Damjan Marioncada9eb2022-05-18 22:16:11 +0200534 fformat (stdout, " [no registered print fn]\n");
Dave Barach371e4e12016-07-08 09:38:52 -0400535 }
536
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100537 uword calc_size = 0;
Damjan Marioncada9eb2022-05-18 22:16:11 +0200538 ASSERT (NULL != m->calc_size_func);
539 if (m->calc_size_func)
Dave Barach371e4e12016-07-08 09:38:52 -0400540 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200541 calc_size = m->calc_size_func (the_msg);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100542 if (calc_size > msg_len)
543 {
544 clib_warning (
545 "Truncated message '%s' (id %u) received, calculated size "
546 "%lu is bigger than actual size %llu, message dropped.",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200547 m->name, id, calc_size, msg_len);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100548 }
549 }
550 else
551 {
552 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
553 "verify message size is correct",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200554 m->name, id);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100555 }
556
557 /* don't process message if it's truncated, otherwise byte swaps
558 * and stuff could corrupt memory even beyond message if it's malicious
559 * e.g. VLA length field set to 1M elements, but VLA empty */
560 if (do_it && calc_size <= msg_len)
561 {
562
Damjan Marioncada9eb2022-05-18 22:16:11 +0200563 if (!m->is_mp_safe)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100564 {
565 vl_msg_api_barrier_trace_context (am->msg_names[id]);
566 vl_msg_api_barrier_sync ();
567 }
Ole Troane796a182020-05-18 11:14:05 +0200568
Damjan Marioncada9eb2022-05-18 22:16:11 +0200569 if (m->is_autoendian)
570 m->endian_handler (the_msg);
Ole Troane796a182020-05-18 11:14:05 +0200571
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000572 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
573 clib_call_callbacks (am->perf_counter_cbs, am, id,
574 0 /* before */ );
575
Damjan Marioncada9eb2022-05-18 22:16:11 +0200576 m->handler (the_msg);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000577
578 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
579 clib_call_callbacks (am->perf_counter_cbs, am, id,
580 1 /* after */ );
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100581
Damjan Marioncada9eb2022-05-18 22:16:11 +0200582 if (!m->is_mp_safe)
Dave Barach371e4e12016-07-08 09:38:52 -0400583 vl_msg_api_barrier_release ();
584 }
585 }
586 else
587 {
588 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 }
590
Dave Barach371e4e12016-07-08 09:38:52 -0400591 if (free_it)
592 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barachb09f4d02019-07-15 16:00:03 -0400594 if (PREDICT_FALSE (am->elog_trace_api_messages))
595 {
596 /* *INDENT-OFF* */
597 ELOG_TYPE_DECLARE (e) =
598 {
599 .format = "api-msg-done(%s): %s",
600 .format_args = "t4T4",
601 .n_enum_strings = 2,
602 .enum_strings =
603 {
604 "barrier",
605 "mp-safe",
606 }
607 };
608 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
Dave Barachb09f4d02019-07-15 16:00:03 -0400610 struct
611 {
612 u32 barrier;
613 u32 c;
614 } *ed;
615 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200616 if (m && m->name)
Dave Barach67947872019-07-19 09:31:29 -0400617 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200618 ed->c = elog_string (am->elog_main, (char *) m->name);
619 ed->barrier = !m->is_mp_safe;
Dave Barach67947872019-07-19 09:31:29 -0400620 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400621 else
Dave Barach67947872019-07-19 09:31:29 -0400622 {
623 ed->c = elog_string (am->elog_main, "BOGUS");
624 ed->barrier = 0;
625 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400626 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Dave Barach371e4e12016-07-08 09:38:52 -0400629void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100630vl_msg_api_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631{
Dave Barach39d69112019-11-27 11:42:13 -0500632 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100634 msg_handler_internal (am, the_msg, msg_len,
635 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
636 1 /* do_it */, 1 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637}
638
Dave Barach371e4e12016-07-08 09:38:52 -0400639void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100640vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641{
Dave Barach39d69112019-11-27 11:42:13 -0500642 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100643 msg_handler_internal (am, the_msg, msg_len,
644 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
645 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646}
647
Dave Barach371e4e12016-07-08 09:38:52 -0400648void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100649vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650{
Dave Barach39d69112019-11-27 11:42:13 -0500651 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100652 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
653 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654}
655
656/*
657 * Add a trace record to the API message trace buffer, if
658 * API message tracing is enabled. Handy for adding sufficient
659 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400660 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 * application creates database entries based on packet traffic, not
662 * control-plane messages.
663 *
664 */
Dave Barach371e4e12016-07-08 09:38:52 -0400665void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100666vl_msg_api_trace_only (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667{
Dave Barach39d69112019-11-27 11:42:13 -0500668 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100670 msg_handler_internal (am, the_msg, msg_len,
671 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
672 0 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673}
674
Dave Barach371e4e12016-07-08 09:38:52 -0400675void
676vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
Dave Barach39d69112019-11-27 11:42:13 -0500678 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400679 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200680 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
Damjan Marioncada9eb2022-05-18 22:16:11 +0200682 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400683 {
684 clib_warning ("_vl_msg_id too large: %d\n", id);
685 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 }
Damjan Marioncada9eb2022-05-18 22:16:11 +0200687
688 if (m->cleanup_handler)
689 m->cleanup_handler (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Dave Barach371e4e12016-07-08 09:38:52 -0400691 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692}
693
694/*
695 * vl_msg_api_replay_handler
696 */
Dave Barach371e4e12016-07-08 09:38:52 -0400697void
698vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699{
Dave Barach39d69112019-11-27 11:42:13 -0500700 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400701 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200702 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703
Damjan Marioncada9eb2022-05-18 22:16:11 +0200704 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400705 {
706 clib_warning ("_vl_msg_id too large: %d\n", id);
707 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 }
Dave Barach371e4e12016-07-08 09:38:52 -0400709 /* do NOT trace the message... */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200710 if (m->handler)
711 m->handler (the_msg);
Dave Barach371e4e12016-07-08 09:38:52 -0400712 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713}
Dave Barach371e4e12016-07-08 09:38:52 -0400714
Dave Barachbfd92272017-05-12 11:59:25 -0400715u32
716vl_msg_api_get_msg_length (void *msg_arg)
717{
718 return vl_msg_api_get_msg_length_inline (msg_arg);
719}
720
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721/*
722 * vl_msg_api_socket_handler
723 */
Dave Barach371e4e12016-07-08 09:38:52 -0400724void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100725vl_msg_api_socket_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726{
Dave Barach39d69112019-11-27 11:42:13 -0500727 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100729 msg_handler_internal (am, the_msg, msg_len,
730 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
731 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732}
733
Dave Barach371e4e12016-07-08 09:38:52 -0400734void
735vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736{
Dave Barach39d69112019-11-27 11:42:13 -0500737 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200738 vl_api_msg_data_t *m;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
Dave Barach65457162017-10-10 17:53:14 -0400740 /*
741 * This happens during the java core tests if the message
742 * dictionary is missing newly added xxx_reply_t messages.
743 * Should never happen, but since I shot myself in the foot once
744 * this way, I thought I'd make it easy to debug if I ever do
745 * it again... (;-)...
746 */
747 if (c->id == 0)
748 {
749 if (c->name)
750 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
751 else
752 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
753 clib_warning ("Did you forget to call setup_message_id_table?");
754 return;
755 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
Damjan Marioncada9eb2022-05-18 22:16:11 +0200757 vec_validate (am->msg_data, c->id);
758 m = vl_api_get_msg_data (am, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Damjan Marioncada9eb2022-05-18 22:16:11 +0200760 if (m->handler && m->handler != c->handler)
761 clib_warning ("BUG: re-registering 'vl_api_%s_t_handler'."
762 "Handler was %llx, replaced by %llx",
763 c->name, m->handler, c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500764
Damjan Marioncada9eb2022-05-18 22:16:11 +0200765 m->name = c->name;
766 m->handler = c->handler;
767 m->cleanup_handler = c->cleanup;
768 m->endian_handler = c->endian;
769 m->print_handler = c->print;
770 m->print_json_handler = c->print_json;
771 m->tojson_handler = c->tojson;
772 m->fromjson_handler = c->fromjson;
773 m->calc_size_func = c->calc_size;
774 m->bounce = c->message_bounce;
775 m->is_mp_safe = c->is_mp_safe;
776 m->is_autoendian = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Damjan Marioncada9eb2022-05-18 22:16:11 +0200778 m->trace_size = c->size;
779 m->trace_enable = c->traced;
780 m->replay_allowed = c->replay;
Filip Tehlar36217e32021-07-23 08:51:10 +0000781
782 if (!am->msg_id_by_name)
783 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
784
785 hash_set_mem (am->msg_id_by_name, c->name, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786}
787
Dave Barach371e4e12016-07-08 09:38:52 -0400788/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 * vl_msg_api_set_handlers
790 * preserve the old API for a while
791 */
Dave Barach371e4e12016-07-08 09:38:52 -0400792void
793vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
Filip Tehlar36217e32021-07-23 08:51:10 +0000794 void *endian, void *print, int size, int traced,
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100795 void *print_json, void *tojson, void *fromjson,
796 void *calc_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797{
Dave Barach371e4e12016-07-08 09:38:52 -0400798 vl_msg_api_msg_config_t cfg;
799 vl_msg_api_msg_config_t *c = &cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
Dave Barachb7b92992018-10-17 10:38:51 -0400801 clib_memset (c, 0, sizeof (*c));
Dave Barach0691d6e2017-01-05 10:08:52 -0500802
Dave Barach371e4e12016-07-08 09:38:52 -0400803 c->id = id;
804 c->name = name;
805 c->handler = handler;
806 c->cleanup = cleanup;
807 c->endian = endian;
808 c->print = print;
Dave Barach371e4e12016-07-08 09:38:52 -0400809 c->traced = traced;
810 c->replay = 1;
811 c->message_bounce = 0;
812 c->is_mp_safe = 0;
Ole Troane796a182020-05-18 11:14:05 +0200813 c->is_autoendian = 0;
Filip Tehlar36217e32021-07-23 08:51:10 +0000814 c->tojson = tojson;
815 c->fromjson = fromjson;
816 c->print_json = print_json;
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100817 c->calc_size = calc_size;
Dave Barach371e4e12016-07-08 09:38:52 -0400818 vl_msg_api_config (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819}
820
Dave Barach371e4e12016-07-08 09:38:52 -0400821void
Matej Perina75a17ec2017-09-21 17:03:27 +0200822vl_msg_api_clean_handlers (int msg_id)
823{
824 vl_msg_api_msg_config_t cfg;
825 vl_msg_api_msg_config_t *c = &cfg;
826
Dave Barachb7b92992018-10-17 10:38:51 -0400827 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +0200828
829 c->id = msg_id;
830 vl_msg_api_config (c);
831}
832
833void
Dave Barach371e4e12016-07-08 09:38:52 -0400834vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835{
Dave Barach39d69112019-11-27 11:42:13 -0500836 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200837 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach371e4e12016-07-08 09:38:52 -0400838 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
Damjan Marioncada9eb2022-05-18 22:16:11 +0200840 m->cleanup_handler = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841}
842
Dave Barach371e4e12016-07-08 09:38:52 -0400843void
Florin Corase86a8ed2018-01-05 03:20:25 -0800844vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845{
Dave Barach371e4e12016-07-08 09:38:52 -0400846 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100848 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
849 {
850 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
851 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
852 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853}
854
Dave Barach77841402020-04-29 17:04:10 -0400855u32
856vl_msg_api_max_length (void *mp)
857{
858 msgbuf_t *mb;
859 u32 data_len = ~0;
860
861 /* Work out the maximum sane message length, and return it */
862 if (PREDICT_TRUE (mp != 0))
863 {
864 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
865 data_len = clib_net_to_host_u32 (mb->data_len);
866 }
867 return data_len;
868}
869
Dave Barach371e4e12016-07-08 09:38:52 -0400870vl_api_trace_t *
871vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872{
Dave Barach371e4e12016-07-08 09:38:52 -0400873 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 {
875 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400876 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400878 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400880 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881 }
882}
883
Dave Barach371e4e12016-07-08 09:38:52 -0400884void
885vl_noop_handler (void *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887}
888
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
890static u8 post_mortem_dump_enabled;
891
Dave Barach80f54e22017-03-08 19:08:56 -0500892void
893vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894{
Dave Barach80f54e22017-03-08 19:08:56 -0500895 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896}
897
Dave Barach371e4e12016-07-08 09:38:52 -0400898void
899vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900{
Dave Barach39d69112019-11-27 11:42:13 -0500901 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400902 FILE *fp;
903 char filename[64];
904 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905
Dave Barach371e4e12016-07-08 09:38:52 -0400906 if (post_mortem_dump_enabled == 0)
907 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908
Dave Barach371e4e12016-07-08 09:38:52 -0400909 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
910 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911
Dave Barach371e4e12016-07-08 09:38:52 -0400912 fp = fopen (filename, "w");
913 if (fp == NULL)
914 {
915 rv = write (2, "Couldn't create ", 16);
916 rv = write (2, filename, strlen (filename));
917 rv = write (2, "\n", 1);
918 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000920 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
Dave Barach371e4e12016-07-08 09:38:52 -0400921 fclose (fp);
922 if (rv < 0)
923 {
924 rv = write (2, "Failed to save post-mortem API trace to ", 40);
925 rv = write (2, filename, strlen (filename));
926 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927 }
Dave Barach371e4e12016-07-08 09:38:52 -0400928
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929}
930
931/* Layered message handling support */
932
Dave Barach371e4e12016-07-08 09:38:52 -0400933void
Dave Barach371e4e12016-07-08 09:38:52 -0400934vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935{
Dave Barach39d69112019-11-27 11:42:13 -0500936 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400937
938 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939}
940
Dave Barach371e4e12016-07-08 09:38:52 -0400941u16
Neale Rannse72be392017-04-26 13:59:20 -0700942vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943{
Dave Barach39d69112019-11-27 11:42:13 -0500944 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400945 u8 *name_copy;
946 vl_api_msg_range_t *rp;
947 uword *p;
948 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949
Dave Barach371e4e12016-07-08 09:38:52 -0400950 if (am->msg_range_by_name == 0)
951 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952
Dave Barach371e4e12016-07-08 09:38:52 -0400953 name_copy = format (0, "%s%c", name, 0);
954
955 p = hash_get_mem (am->msg_range_by_name, name_copy);
956 if (p)
957 {
958 clib_warning ("WARNING: duplicate message range registration for '%s'",
959 name_copy);
960 vec_free (name_copy);
961 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 }
963
Dave Barach371e4e12016-07-08 09:38:52 -0400964 if (n < 0 || n > 1024)
965 {
966 clib_warning
967 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
968 n, name_copy);
969 vec_free (name_copy);
970 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 }
972
Dave Barach371e4e12016-07-08 09:38:52 -0400973 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Dave Barach371e4e12016-07-08 09:38:52 -0400975 rv = rp->first_msg_id = am->first_available_msg_id;
976 am->first_available_msg_id += n;
977 rp->last_msg_id = am->first_available_msg_id - 1;
978 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Dave Barach371e4e12016-07-08 09:38:52 -0400980 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981
Dave Barach371e4e12016-07-08 09:38:52 -0400982 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983}
Dave Barach371e4e12016-07-08 09:38:52 -0400984
Dave Barach557d1282016-11-10 14:22:49 -0500985void
Neale Rannse72be392017-04-26 13:59:20 -0700986vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -0500987{
988 uword *p;
989
990 if (am->msg_index_by_name_and_crc == 0)
991 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
992
993 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
994 if (p)
995 {
996 clib_warning ("attempt to redefine '%s' ignored...", string);
997 return;
998 }
999
1000 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1001}
1002
Dave Barach0d056e52017-09-28 15:11:16 -04001003void
1004vl_msg_api_add_version (api_main_t * am, const char *string,
1005 u32 major, u32 minor, u32 patch)
1006{
1007 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1008 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +02001009 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -04001010 vec_add1 (am->api_version_list, version);
1011}
Dave Barach557d1282016-11-10 14:22:49 -05001012
Florin Corase86a8ed2018-01-05 03:20:25 -08001013u32
1014vl_msg_api_get_msg_index (u8 * name_and_crc)
1015{
Dave Barach39d69112019-11-27 11:42:13 -05001016 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -08001017 uword *p;
1018
1019 if (am->msg_index_by_name_and_crc)
1020 {
1021 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1022 if (p)
1023 return p[0];
1024 }
1025 return ~0;
1026}
1027
Ole Troan73710c72018-06-04 22:27:49 +02001028void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001029vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1030{
1031 pthread_mutex_lock (&vlib_rp->mutex);
1032 return svm_push_data_heap (vlib_rp);
1033}
1034
1035void *
Ole Troan73710c72018-06-04 22:27:49 +02001036vl_msg_push_heap (void)
1037{
Dave Barach39d69112019-11-27 11:42:13 -05001038 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001039 return vl_msg_push_heap_w_region (am->vlib_rp);
1040}
1041
1042void
1043vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1044{
1045 svm_pop_heap (oldheap);
1046 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001047}
1048
1049void
1050vl_msg_pop_heap (void *oldheap)
1051{
Dave Barach39d69112019-11-27 11:42:13 -05001052 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001053 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001054}
1055
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001056/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001057int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001058vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001059{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001060 /* copy without nul terminator */
1061 u32 len = strlen (buf);
1062 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001063 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001064 str->length = htonl (len);
1065 return len + sizeof (u32);
1066}
1067
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001068/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001069int
1070vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1071{
1072 u32 len = vec_len (vec);
1073 clib_memcpy (str->buf, vec, len);
1074 str->length = htonl (len);
1075 return len + sizeof (u32);
1076}
1077
Neale Ranns377860a2019-06-21 07:57:18 -07001078u32
1079vl_api_string_len (vl_api_string_t * astr)
1080{
Dave Barach9683c1e2019-07-01 09:42:41 -04001081 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001082}
1083
Ole Troan33a58172019-09-04 09:12:29 +02001084u8 *
1085vl_api_format_string (u8 * s, va_list * args)
1086{
1087 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1088 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1089 return s;
1090}
1091
Neale Ranns377860a2019-06-21 07:57:18 -07001092/*
1093 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001094 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001095 */
1096u8 *
Dave Barach77841402020-04-29 17:04:10 -04001097vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001098{
1099 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001100
1101 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1102 return format (0, "insane astr->length %u%c",
1103 clib_net_to_host_u32 (astr->length), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001104 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001105 return v;
1106}
Ole Troan73710c72018-06-04 22:27:49 +02001107
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001108/*
1109 * Returns a new vector. Remember to free it after use.
1110 * Nul terminated.
1111 */
1112char *
1113vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1114{
1115 char *v = 0;
1116 if (clib_net_to_host_u32 (astr->length) > 0)
1117 {
1118 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1119 vec_add1 (v, 0);
1120 }
1121 return v;
1122}
1123
Dave Barachb09f4d02019-07-15 16:00:03 -04001124void
1125vl_api_set_elog_main (elog_main_t * m)
1126{
Dave Barach39d69112019-11-27 11:42:13 -05001127 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001128 am->elog_main = m;
1129}
1130
1131int
1132vl_api_set_elog_trace_api_messages (int enable)
1133{
1134 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001135 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001136
1137 rv = am->elog_trace_api_messages;
1138 am->elog_trace_api_messages = enable;
1139 return rv;
1140}
1141
1142int
1143vl_api_get_elog_trace_api_messages (void)
1144{
Dave Barach39d69112019-11-27 11:42:13 -05001145 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001146
1147 return am->elog_trace_api_messages;
1148}
1149
Dave Barach371e4e12016-07-08 09:38:52 -04001150/*
1151 * fd.io coding-style-patch-verification: ON
1152 *
1153 * Local Variables:
1154 * eval: (c-set-style "gnu")
1155 * End:
1156 */