blob: 73ef3ce54d81d396419eb239bd243b7df6cbefd9 [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
Andrew Yourtchenkocbc10392022-08-17 13:26:54 +0000235 if (m && m->endian_handler)
236 {
237 m->endian_handler (tmpmem);
238 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000239
Damjan Marioncada9eb2022-05-18 22:16:11 +0200240 if (m && m->tojson_handler)
Filip Tehlar36217e32021-07-23 08:51:10 +0000241 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200242 cJSON *o = m->tojson_handler (tmpmem);
Filip Tehlar36217e32021-07-23 08:51:10 +0000243 char *s = cJSON_Print (o);
244 slen = strlen (s);
245 tlen = fwrite (s, 1, slen, fp);
246 cJSON_free (s);
247 cJSON_Delete (o);
248 vec_free (tmpmem);
249 if (tlen != slen)
250 {
251 fformat (stderr, "writing to file error\n");
252 return -11;
253 }
254 }
255 else
256 fformat (stderr, " [no registered tojson fn]\n");
257
258 return 0;
259}
260
261#define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
262
263typedef struct
264{
265 FILE *fp;
266 u32 n_traces;
267 u32 i;
268} vl_msg_write_json_args_t;
269
270static int
271vl_msg_write_json_fn (u8 *msg, void *ctx)
272{
273 vl_msg_write_json_args_t *arg = ctx;
274 FILE *fp = arg->fp;
275 api_main_t *am = vlibapi_get_main ();
276 int rc = vl_msg_api_trace_write_one (am, msg, fp);
277 if (rc < 0)
278 return rc;
279
280 if (arg->i < arg->n_traces - 1)
281 vl_msg_fwrite (",\n", fp);
282 arg->i++;
283 return 0;
284}
285
286static int
287vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
288{
289 vl_msg_write_json_args_t args;
290 clib_memset (&args, 0, sizeof (args));
291 args.fp = fp;
292 args.n_traces = vec_len (tp->traces);
293 vl_msg_fwrite ("[\n", fp);
294
295 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
296 if (rv < 0)
297 return rv;
298
299 vl_msg_fwrite ("]", fp);
300 return 0;
301}
302
Dave Barach371e4e12016-07-08 09:38:52 -0400303int
Filip Tehlar36217e32021-07-23 08:51:10 +0000304vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
305 void *ctx)
306{
307 int i;
308 u8 *msg;
309 int rv = 0;
310
311 /* No-wrap case */
312 if (tp->wrapped == 0)
313 {
314 for (i = 0; i < vec_len (tp->traces); i++)
315 {
316 /*sa_ignore NO_NULL_CHK */
317 msg = tp->traces[i];
318 if (!msg)
319 continue;
320
321 rv = fn (msg, ctx);
322 if (rv < 0)
323 return rv;
324 }
325 }
326 else
327 {
328 /* Wrap case: write oldest -> end of buffer */
329 for (i = tp->curindex; i < vec_len (tp->traces); i++)
330 {
331 msg = tp->traces[i];
332 if (!msg)
333 continue;
334
335 rv = fn (msg, ctx);
336 if (rv < 0)
337 return rv;
338 }
339 /* write beginning of buffer -> oldest-1 */
340 for (i = 0; i < tp->curindex; i++)
341 {
342 /*sa_ignore NO_NULL_CHK */
343 msg = tp->traces[i];
344 if (!msg)
345 continue;
346
347 rv = fn (msg, ctx);
348 if (rv < 0)
349 return rv;
350 }
351 }
352 return 0;
353}
354
355static int
356vl_api_msg_write_fn (u8 *msg, void *ctx)
357{
358 FILE *fp = ctx;
359 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
360 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
361 {
362 return (-14);
363 }
364 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
365 {
366 return (-14);
367 }
368 return 0;
369}
370
371int
372vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
373 u8 is_json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374{
Dave Barach371e4e12016-07-08 09:38:52 -0400375 vl_api_trace_t *tp;
376 vl_api_trace_file_header_t fh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
Dave Barach371e4e12016-07-08 09:38:52 -0400378 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 {
380 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400381 tp = am->tx_trace;
382 break;
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400385 tp = am->rx_trace;
386 break;
387
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400389 /* duh? */
390 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 }
392
Dave Barach371e4e12016-07-08 09:38:52 -0400393 /* Configured, data present? */
394 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
395 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barach371e4e12016-07-08 09:38:52 -0400397 /* "Dare to be stupid" check */
398 if (fp == 0)
399 {
400 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 }
402
Filip Tehlar36217e32021-07-23 08:51:10 +0000403 if (is_json)
404 return vl_msg_api_trace_write_json (am, tp, fp);
405
Dave Barach371e4e12016-07-08 09:38:52 -0400406 /* Write the file header */
Dave Barach371e4e12016-07-08 09:38:52 -0400407 fh.wrapped = tp->wrapped;
Ole Troanedfe2c02019-07-30 15:38:13 +0200408 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
Filip Tehlar36217e32021-07-23 08:51:10 +0000409
Ole Troanedfe2c02019-07-30 15:38:13 +0200410 u8 *m = vl_api_serialize_message_table (am, 0);
Ole Troanedfe2c02019-07-30 15:38:13 +0200411 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
Dave Barach371e4e12016-07-08 09:38:52 -0400413 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
414 {
415 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 }
417
Ole Troanedfe2c02019-07-30 15:38:13 +0200418 /* Write the message table */
419 if (fwrite (m, vec_len (m), 1, fp) != 1)
420 {
421 return (-14);
422 }
423 vec_free (m);
424
Filip Tehlar36217e32021-07-23 08:51:10 +0000425 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426}
427
Dave Barach371e4e12016-07-08 09:38:52 -0400428int
429vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
430 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431{
Dave Barach371e4e12016-07-08 09:38:52 -0400432 vl_api_trace_t *tp;
433 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Dave Barach371e4e12016-07-08 09:38:52 -0400435 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 {
437 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400438 tp = am->tx_trace;
439 if (tp == 0)
440 {
441 vec_validate (am->tx_trace, 0);
442 tp = am->tx_trace;
443 }
444 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Dave Barach371e4e12016-07-08 09:38:52 -0400446 case VL_API_TRACE_RX:
447 tp = am->rx_trace;
448 if (tp == 0)
449 {
450 vec_validate (am->rx_trace, 0);
451 tp = am->rx_trace;
452 }
453
454 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400457 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
459 }
460
Dave Barach371e4e12016-07-08 09:38:52 -0400461 if (tp->enabled)
462 {
463 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 }
Dave Barach371e4e12016-07-08 09:38:52 -0400465 if (tp->traces)
466 {
467 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 }
469
Dave Barachb7b92992018-10-17 10:38:51 -0400470 clib_memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barach371e4e12016-07-08 09:38:52 -0400472 if (clib_arch_is_big_endian)
473 {
474 tp->endian = VL_API_BIG_ENDIAN;
475 }
476 else
477 {
478 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 }
480
Dave Barach371e4e12016-07-08 09:38:52 -0400481 tp->nitems = nitems;
482 if (was_on)
483 {
484 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 }
Dave Barach371e4e12016-07-08 09:38:52 -0400486 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487}
488
Dave Barach80f54e22017-03-08 19:08:56 -0500489void
490vl_msg_api_barrier_sync (void)
491{
492}
493
494void
495vl_msg_api_barrier_release (void)
496{
497}
498
Dave Barach371e4e12016-07-08 09:38:52 -0400499always_inline void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100500msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
501 int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502{
Dave Barach9683c1e2019-07-01 09:42:41 -0400503 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200504 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Dave Barachb09f4d02019-07-15 16:00:03 -0400506 if (PREDICT_FALSE (am->elog_trace_api_messages))
507 {
508 /* *INDENT-OFF* */
509 ELOG_TYPE_DECLARE (e) =
510 {
511 .format = "api-msg: %s",
512 .format_args = "T4",
513 };
514 /* *INDENT-ON* */
515 struct
516 {
517 u32 c;
518 } *ed;
519 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200520 if (m && m->name)
521 ed->c = elog_string (am->elog_main, (char *) m->name);
Dave Barachb09f4d02019-07-15 16:00:03 -0400522 else
523 ed->c = elog_string (am->elog_main, "BOGUS");
524 }
525
Damjan Marioncada9eb2022-05-18 22:16:11 +0200526 if (m && m->handler)
Dave Barach371e4e12016-07-08 09:38:52 -0400527 {
528 if (trace_it)
529 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Dave Barach371e4e12016-07-08 09:38:52 -0400531 if (am->msg_print_flag)
532 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200533 fformat (stdout, "[%d]: %s\n", id, m->name);
534 if (m->print_handler)
535 m->print_handler (the_msg, stdout);
Dave Barach371e4e12016-07-08 09:38:52 -0400536 else
Damjan Marioncada9eb2022-05-18 22:16:11 +0200537 fformat (stdout, " [no registered print fn]\n");
Dave Barach371e4e12016-07-08 09:38:52 -0400538 }
539
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100540 uword calc_size = 0;
Damjan Marioncada9eb2022-05-18 22:16:11 +0200541 ASSERT (NULL != m->calc_size_func);
542 if (m->calc_size_func)
Dave Barach371e4e12016-07-08 09:38:52 -0400543 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200544 calc_size = m->calc_size_func (the_msg);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100545 if (calc_size > msg_len)
546 {
547 clib_warning (
548 "Truncated message '%s' (id %u) received, calculated size "
549 "%lu is bigger than actual size %llu, message dropped.",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200550 m->name, id, calc_size, msg_len);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100551 }
552 }
553 else
554 {
555 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
556 "verify message size is correct",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200557 m->name, id);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100558 }
559
560 /* don't process message if it's truncated, otherwise byte swaps
561 * and stuff could corrupt memory even beyond message if it's malicious
562 * e.g. VLA length field set to 1M elements, but VLA empty */
563 if (do_it && calc_size <= msg_len)
564 {
565
Damjan Marioncada9eb2022-05-18 22:16:11 +0200566 if (!m->is_mp_safe)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100567 {
568 vl_msg_api_barrier_trace_context (am->msg_names[id]);
569 vl_msg_api_barrier_sync ();
570 }
Ole Troane796a182020-05-18 11:14:05 +0200571
Damjan Marioncada9eb2022-05-18 22:16:11 +0200572 if (m->is_autoendian)
573 m->endian_handler (the_msg);
Ole Troane796a182020-05-18 11:14:05 +0200574
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000575 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
576 clib_call_callbacks (am->perf_counter_cbs, am, id,
577 0 /* before */ );
578
Damjan Marioncada9eb2022-05-18 22:16:11 +0200579 m->handler (the_msg);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000580
581 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
582 clib_call_callbacks (am->perf_counter_cbs, am, id,
583 1 /* after */ );
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100584
Damjan Marioncada9eb2022-05-18 22:16:11 +0200585 if (!m->is_mp_safe)
Dave Barach371e4e12016-07-08 09:38:52 -0400586 vl_msg_api_barrier_release ();
587 }
588 }
589 else
590 {
591 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 }
593
Dave Barach371e4e12016-07-08 09:38:52 -0400594 if (free_it)
595 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Dave Barachb09f4d02019-07-15 16:00:03 -0400597 if (PREDICT_FALSE (am->elog_trace_api_messages))
598 {
599 /* *INDENT-OFF* */
600 ELOG_TYPE_DECLARE (e) =
601 {
602 .format = "api-msg-done(%s): %s",
603 .format_args = "t4T4",
604 .n_enum_strings = 2,
605 .enum_strings =
606 {
607 "barrier",
608 "mp-safe",
609 }
610 };
611 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
Dave Barachb09f4d02019-07-15 16:00:03 -0400613 struct
614 {
615 u32 barrier;
616 u32 c;
617 } *ed;
618 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200619 if (m && m->name)
Dave Barach67947872019-07-19 09:31:29 -0400620 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200621 ed->c = elog_string (am->elog_main, (char *) m->name);
622 ed->barrier = !m->is_mp_safe;
Dave Barach67947872019-07-19 09:31:29 -0400623 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400624 else
Dave Barach67947872019-07-19 09:31:29 -0400625 {
626 ed->c = elog_string (am->elog_main, "BOGUS");
627 ed->barrier = 0;
628 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400629 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Dave Barach371e4e12016-07-08 09:38:52 -0400632void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100633vl_msg_api_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634{
Dave Barach39d69112019-11-27 11:42:13 -0500635 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100637 msg_handler_internal (am, the_msg, msg_len,
638 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
639 1 /* do_it */, 1 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640}
641
Dave Barach371e4e12016-07-08 09:38:52 -0400642void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100643vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Dave Barach39d69112019-11-27 11:42:13 -0500645 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100646 msg_handler_internal (am, the_msg, msg_len,
647 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
648 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649}
650
Dave Barach371e4e12016-07-08 09:38:52 -0400651void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100652vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653{
Dave Barach39d69112019-11-27 11:42:13 -0500654 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100655 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
656 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657}
658
659/*
660 * Add a trace record to the API message trace buffer, if
661 * API message tracing is enabled. Handy for adding sufficient
662 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400663 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 * application creates database entries based on packet traffic, not
665 * control-plane messages.
666 *
667 */
Dave Barach371e4e12016-07-08 09:38:52 -0400668void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100669vl_msg_api_trace_only (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670{
Dave Barach39d69112019-11-27 11:42:13 -0500671 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100673 msg_handler_internal (am, the_msg, msg_len,
674 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
675 0 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676}
677
Dave Barach371e4e12016-07-08 09:38:52 -0400678void
679vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680{
Dave Barach39d69112019-11-27 11:42:13 -0500681 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400682 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200683 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684
Damjan Marioncada9eb2022-05-18 22:16:11 +0200685 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400686 {
687 clib_warning ("_vl_msg_id too large: %d\n", id);
688 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689 }
Damjan Marioncada9eb2022-05-18 22:16:11 +0200690
691 if (m->cleanup_handler)
692 m->cleanup_handler (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Dave Barach371e4e12016-07-08 09:38:52 -0400694 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695}
696
697/*
698 * vl_msg_api_replay_handler
699 */
Dave Barach371e4e12016-07-08 09:38:52 -0400700void
701vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702{
Dave Barach39d69112019-11-27 11:42:13 -0500703 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400704 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200705 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Damjan Marioncada9eb2022-05-18 22:16:11 +0200707 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400708 {
709 clib_warning ("_vl_msg_id too large: %d\n", id);
710 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 }
Dave Barach371e4e12016-07-08 09:38:52 -0400712 /* do NOT trace the message... */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200713 if (m->handler)
714 m->handler (the_msg);
Dave Barach371e4e12016-07-08 09:38:52 -0400715 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716}
Dave Barach371e4e12016-07-08 09:38:52 -0400717
Dave Barachbfd92272017-05-12 11:59:25 -0400718u32
719vl_msg_api_get_msg_length (void *msg_arg)
720{
721 return vl_msg_api_get_msg_length_inline (msg_arg);
722}
723
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724/*
725 * vl_msg_api_socket_handler
726 */
Dave Barach371e4e12016-07-08 09:38:52 -0400727void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100728vl_msg_api_socket_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729{
Dave Barach39d69112019-11-27 11:42:13 -0500730 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100732 msg_handler_internal (am, the_msg, msg_len,
733 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
734 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735}
736
Dave Barach371e4e12016-07-08 09:38:52 -0400737void
738vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739{
Dave Barach39d69112019-11-27 11:42:13 -0500740 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200741 vl_api_msg_data_t *m;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Dave Barach65457162017-10-10 17:53:14 -0400743 /*
744 * This happens during the java core tests if the message
745 * dictionary is missing newly added xxx_reply_t messages.
746 * Should never happen, but since I shot myself in the foot once
747 * this way, I thought I'd make it easy to debug if I ever do
748 * it again... (;-)...
749 */
750 if (c->id == 0)
751 {
752 if (c->name)
753 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
754 else
755 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
756 clib_warning ("Did you forget to call setup_message_id_table?");
757 return;
758 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Damjan Marioncada9eb2022-05-18 22:16:11 +0200760 vec_validate (am->msg_data, c->id);
761 m = vl_api_get_msg_data (am, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Damjan Marioncada9eb2022-05-18 22:16:11 +0200763 if (m->handler && m->handler != c->handler)
764 clib_warning ("BUG: re-registering 'vl_api_%s_t_handler'."
765 "Handler was %llx, replaced by %llx",
766 c->name, m->handler, c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500767
Damjan Marioncada9eb2022-05-18 22:16:11 +0200768 m->name = c->name;
769 m->handler = c->handler;
770 m->cleanup_handler = c->cleanup;
771 m->endian_handler = c->endian;
772 m->print_handler = c->print;
773 m->print_json_handler = c->print_json;
774 m->tojson_handler = c->tojson;
775 m->fromjson_handler = c->fromjson;
776 m->calc_size_func = c->calc_size;
777 m->bounce = c->message_bounce;
778 m->is_mp_safe = c->is_mp_safe;
779 m->is_autoendian = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Damjan Marioncada9eb2022-05-18 22:16:11 +0200781 m->trace_size = c->size;
782 m->trace_enable = c->traced;
783 m->replay_allowed = c->replay;
Filip Tehlar36217e32021-07-23 08:51:10 +0000784
785 if (!am->msg_id_by_name)
786 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
787
788 hash_set_mem (am->msg_id_by_name, c->name, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789}
790
Dave Barach371e4e12016-07-08 09:38:52 -0400791/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 * vl_msg_api_set_handlers
793 * preserve the old API for a while
794 */
Dave Barach371e4e12016-07-08 09:38:52 -0400795void
796vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
Filip Tehlar36217e32021-07-23 08:51:10 +0000797 void *endian, void *print, int size, int traced,
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100798 void *print_json, void *tojson, void *fromjson,
799 void *calc_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800{
Dave Barach371e4e12016-07-08 09:38:52 -0400801 vl_msg_api_msg_config_t cfg;
802 vl_msg_api_msg_config_t *c = &cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Dave Barachb7b92992018-10-17 10:38:51 -0400804 clib_memset (c, 0, sizeof (*c));
Dave Barach0691d6e2017-01-05 10:08:52 -0500805
Dave Barach371e4e12016-07-08 09:38:52 -0400806 c->id = id;
807 c->name = name;
808 c->handler = handler;
809 c->cleanup = cleanup;
810 c->endian = endian;
811 c->print = print;
Dave Barach371e4e12016-07-08 09:38:52 -0400812 c->traced = traced;
813 c->replay = 1;
814 c->message_bounce = 0;
815 c->is_mp_safe = 0;
Ole Troane796a182020-05-18 11:14:05 +0200816 c->is_autoendian = 0;
Filip Tehlar36217e32021-07-23 08:51:10 +0000817 c->tojson = tojson;
818 c->fromjson = fromjson;
819 c->print_json = print_json;
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100820 c->calc_size = calc_size;
Dave Barach371e4e12016-07-08 09:38:52 -0400821 vl_msg_api_config (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822}
823
Dave Barach371e4e12016-07-08 09:38:52 -0400824void
Matej Perina75a17ec2017-09-21 17:03:27 +0200825vl_msg_api_clean_handlers (int msg_id)
826{
827 vl_msg_api_msg_config_t cfg;
828 vl_msg_api_msg_config_t *c = &cfg;
829
Dave Barachb7b92992018-10-17 10:38:51 -0400830 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +0200831
832 c->id = msg_id;
833 vl_msg_api_config (c);
834}
835
836void
Dave Barach371e4e12016-07-08 09:38:52 -0400837vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838{
Dave Barach39d69112019-11-27 11:42:13 -0500839 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200840 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach371e4e12016-07-08 09:38:52 -0400841 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
Damjan Marioncada9eb2022-05-18 22:16:11 +0200843 m->cleanup_handler = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844}
845
Dave Barach371e4e12016-07-08 09:38:52 -0400846void
Florin Corase86a8ed2018-01-05 03:20:25 -0800847vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848{
Dave Barach371e4e12016-07-08 09:38:52 -0400849 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100851 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
852 {
853 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
854 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
855 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856}
857
Dave Barach77841402020-04-29 17:04:10 -0400858u32
859vl_msg_api_max_length (void *mp)
860{
861 msgbuf_t *mb;
862 u32 data_len = ~0;
863
864 /* Work out the maximum sane message length, and return it */
865 if (PREDICT_TRUE (mp != 0))
866 {
867 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
868 data_len = clib_net_to_host_u32 (mb->data_len);
869 }
870 return data_len;
871}
872
Dave Barach371e4e12016-07-08 09:38:52 -0400873vl_api_trace_t *
874vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875{
Dave Barach371e4e12016-07-08 09:38:52 -0400876 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877 {
878 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400879 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400881 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400883 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 }
885}
886
Dave Barach371e4e12016-07-08 09:38:52 -0400887void
888vl_noop_handler (void *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890}
891
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
893static u8 post_mortem_dump_enabled;
894
Dave Barach80f54e22017-03-08 19:08:56 -0500895void
896vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897{
Dave Barach80f54e22017-03-08 19:08:56 -0500898 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899}
900
Dave Barach371e4e12016-07-08 09:38:52 -0400901void
902vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903{
Dave Barach39d69112019-11-27 11:42:13 -0500904 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400905 FILE *fp;
906 char filename[64];
907 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908
Dave Barach371e4e12016-07-08 09:38:52 -0400909 if (post_mortem_dump_enabled == 0)
910 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911
Dave Barach371e4e12016-07-08 09:38:52 -0400912 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
913 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Dave Barach371e4e12016-07-08 09:38:52 -0400915 fp = fopen (filename, "w");
916 if (fp == NULL)
917 {
918 rv = write (2, "Couldn't create ", 16);
919 rv = write (2, filename, strlen (filename));
920 rv = write (2, "\n", 1);
921 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000923 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
Dave Barach371e4e12016-07-08 09:38:52 -0400924 fclose (fp);
925 if (rv < 0)
926 {
927 rv = write (2, "Failed to save post-mortem API trace to ", 40);
928 rv = write (2, filename, strlen (filename));
929 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 }
Dave Barach371e4e12016-07-08 09:38:52 -0400931
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932}
933
934/* Layered message handling support */
935
Dave Barach371e4e12016-07-08 09:38:52 -0400936void
Dave Barach371e4e12016-07-08 09:38:52 -0400937vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938{
Dave Barach39d69112019-11-27 11:42:13 -0500939 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400940
941 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942}
943
Dave Barach371e4e12016-07-08 09:38:52 -0400944u16
Neale Rannse72be392017-04-26 13:59:20 -0700945vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946{
Dave Barach39d69112019-11-27 11:42:13 -0500947 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400948 u8 *name_copy;
949 vl_api_msg_range_t *rp;
950 uword *p;
951 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952
Dave Barach371e4e12016-07-08 09:38:52 -0400953 if (am->msg_range_by_name == 0)
954 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
Dave Barach371e4e12016-07-08 09:38:52 -0400956 name_copy = format (0, "%s%c", name, 0);
957
958 p = hash_get_mem (am->msg_range_by_name, name_copy);
959 if (p)
960 {
961 clib_warning ("WARNING: duplicate message range registration for '%s'",
962 name_copy);
963 vec_free (name_copy);
964 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965 }
966
Dave Barach371e4e12016-07-08 09:38:52 -0400967 if (n < 0 || n > 1024)
968 {
969 clib_warning
970 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
971 n, name_copy);
972 vec_free (name_copy);
973 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 }
975
Dave Barach371e4e12016-07-08 09:38:52 -0400976 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977
Dave Barach371e4e12016-07-08 09:38:52 -0400978 rv = rp->first_msg_id = am->first_available_msg_id;
979 am->first_available_msg_id += n;
980 rp->last_msg_id = am->first_available_msg_id - 1;
981 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Dave Barach371e4e12016-07-08 09:38:52 -0400983 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
Dave Barach371e4e12016-07-08 09:38:52 -0400985 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986}
Dave Barach371e4e12016-07-08 09:38:52 -0400987
Dave Barach557d1282016-11-10 14:22:49 -0500988void
Neale Rannse72be392017-04-26 13:59:20 -0700989vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -0500990{
991 uword *p;
992
993 if (am->msg_index_by_name_and_crc == 0)
994 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
995
996 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
997 if (p)
998 {
999 clib_warning ("attempt to redefine '%s' ignored...", string);
1000 return;
1001 }
1002
1003 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1004}
1005
Dave Barach0d056e52017-09-28 15:11:16 -04001006void
1007vl_msg_api_add_version (api_main_t * am, const char *string,
1008 u32 major, u32 minor, u32 patch)
1009{
1010 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1011 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +02001012 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -04001013 vec_add1 (am->api_version_list, version);
1014}
Dave Barach557d1282016-11-10 14:22:49 -05001015
Florin Corase86a8ed2018-01-05 03:20:25 -08001016u32
1017vl_msg_api_get_msg_index (u8 * name_and_crc)
1018{
Dave Barach39d69112019-11-27 11:42:13 -05001019 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -08001020 uword *p;
1021
1022 if (am->msg_index_by_name_and_crc)
1023 {
1024 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1025 if (p)
1026 return p[0];
1027 }
1028 return ~0;
1029}
1030
Ole Troan73710c72018-06-04 22:27:49 +02001031void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001032vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1033{
1034 pthread_mutex_lock (&vlib_rp->mutex);
1035 return svm_push_data_heap (vlib_rp);
1036}
1037
1038void *
Ole Troan73710c72018-06-04 22:27:49 +02001039vl_msg_push_heap (void)
1040{
Dave Barach39d69112019-11-27 11:42:13 -05001041 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001042 return vl_msg_push_heap_w_region (am->vlib_rp);
1043}
1044
1045void
1046vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1047{
1048 svm_pop_heap (oldheap);
1049 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001050}
1051
1052void
1053vl_msg_pop_heap (void *oldheap)
1054{
Dave Barach39d69112019-11-27 11:42:13 -05001055 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001056 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001057}
1058
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001059/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001060int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001061vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001062{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001063 /* copy without nul terminator */
1064 u32 len = strlen (buf);
1065 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001066 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001067 str->length = htonl (len);
1068 return len + sizeof (u32);
1069}
1070
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001071/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001072int
1073vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1074{
1075 u32 len = vec_len (vec);
1076 clib_memcpy (str->buf, vec, len);
1077 str->length = htonl (len);
1078 return len + sizeof (u32);
1079}
1080
Neale Ranns377860a2019-06-21 07:57:18 -07001081u32
1082vl_api_string_len (vl_api_string_t * astr)
1083{
Dave Barach9683c1e2019-07-01 09:42:41 -04001084 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001085}
1086
Ole Troan33a58172019-09-04 09:12:29 +02001087u8 *
1088vl_api_format_string (u8 * s, va_list * args)
1089{
1090 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1091 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1092 return s;
1093}
1094
Neale Ranns377860a2019-06-21 07:57:18 -07001095/*
1096 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001097 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001098 */
1099u8 *
Dave Barach77841402020-04-29 17:04:10 -04001100vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001101{
1102 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001103
1104 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1105 return format (0, "insane astr->length %u%c",
1106 clib_net_to_host_u32 (astr->length), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001107 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001108 return v;
1109}
Ole Troan73710c72018-06-04 22:27:49 +02001110
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001111/*
1112 * Returns a new vector. Remember to free it after use.
1113 * Nul terminated.
1114 */
1115char *
1116vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1117{
1118 char *v = 0;
1119 if (clib_net_to_host_u32 (astr->length) > 0)
1120 {
1121 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1122 vec_add1 (v, 0);
1123 }
1124 return v;
1125}
1126
Dave Barachb09f4d02019-07-15 16:00:03 -04001127void
1128vl_api_set_elog_main (elog_main_t * m)
1129{
Dave Barach39d69112019-11-27 11:42:13 -05001130 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001131 am->elog_main = m;
1132}
1133
1134int
1135vl_api_set_elog_trace_api_messages (int enable)
1136{
1137 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001138 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001139
1140 rv = am->elog_trace_api_messages;
1141 am->elog_trace_api_messages = enable;
1142 return rv;
1143}
1144
1145int
1146vl_api_get_elog_trace_api_messages (void)
1147{
Dave Barach39d69112019-11-27 11:42:13 -05001148 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001149
1150 return am->elog_trace_api_messages;
1151}
1152
Dave Barach371e4e12016-07-08 09:38:52 -04001153/*
1154 * fd.io coding-style-patch-verification: ON
1155 *
1156 * Local Variables:
1157 * eval: (c-set-style "gnu")
1158 * End:
1159 */