blob: 0ea52802bb749d3268ee2ee13288c11ea23a63f8 [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);
Damjan Marionfe45f8f2022-05-20 16:01:22 +0200534 fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
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;
Damjan Marionfe45f8f2022-05-20 16:01:22 +0200769 m->format_fn = c->format_fn;
Damjan Marioncada9eb2022-05-18 22:16:11 +0200770 m->tojson_handler = c->tojson;
771 m->fromjson_handler = c->fromjson;
772 m->calc_size_func = c->calc_size;
773 m->bounce = c->message_bounce;
774 m->is_mp_safe = c->is_mp_safe;
775 m->is_autoendian = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
Damjan Marioncada9eb2022-05-18 22:16:11 +0200777 m->trace_size = c->size;
778 m->trace_enable = c->traced;
779 m->replay_allowed = c->replay;
Filip Tehlar36217e32021-07-23 08:51:10 +0000780
781 if (!am->msg_id_by_name)
782 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
783
784 hash_set_mem (am->msg_id_by_name, c->name, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785}
786
Dave Barach371e4e12016-07-08 09:38:52 -0400787void
Matej Perina75a17ec2017-09-21 17:03:27 +0200788vl_msg_api_clean_handlers (int msg_id)
789{
790 vl_msg_api_msg_config_t cfg;
791 vl_msg_api_msg_config_t *c = &cfg;
792
Dave Barachb7b92992018-10-17 10:38:51 -0400793 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +0200794
795 c->id = msg_id;
796 vl_msg_api_config (c);
797}
798
799void
Dave Barach371e4e12016-07-08 09:38:52 -0400800vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801{
Dave Barach39d69112019-11-27 11:42:13 -0500802 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200803 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach371e4e12016-07-08 09:38:52 -0400804 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Damjan Marioncada9eb2022-05-18 22:16:11 +0200806 m->cleanup_handler = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807}
808
Dave Barach371e4e12016-07-08 09:38:52 -0400809void
Florin Corase86a8ed2018-01-05 03:20:25 -0800810vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811{
Dave Barach371e4e12016-07-08 09:38:52 -0400812 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100814 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
815 {
BenoƮt Ganne49bbf9e2023-01-26 19:27:20 +0100816 VL_MSG_API_UNPOISON ((u8 *) msg);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100817 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
818 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
819 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820}
821
Dave Barach77841402020-04-29 17:04:10 -0400822u32
823vl_msg_api_max_length (void *mp)
824{
825 msgbuf_t *mb;
826 u32 data_len = ~0;
827
828 /* Work out the maximum sane message length, and return it */
829 if (PREDICT_TRUE (mp != 0))
830 {
831 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
832 data_len = clib_net_to_host_u32 (mb->data_len);
833 }
834 return data_len;
835}
836
Dave Barach371e4e12016-07-08 09:38:52 -0400837vl_api_trace_t *
838vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839{
Dave Barach371e4e12016-07-08 09:38:52 -0400840 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 {
842 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400843 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400845 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400847 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 }
849}
850
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851static u8 post_mortem_dump_enabled;
852
Dave Barach80f54e22017-03-08 19:08:56 -0500853void
854vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855{
Dave Barach80f54e22017-03-08 19:08:56 -0500856 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857}
858
Dave Barach371e4e12016-07-08 09:38:52 -0400859void
860vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861{
Dave Barach39d69112019-11-27 11:42:13 -0500862 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400863 FILE *fp;
864 char filename[64];
865 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Dave Barach371e4e12016-07-08 09:38:52 -0400867 if (post_mortem_dump_enabled == 0)
868 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869
Dave Barach371e4e12016-07-08 09:38:52 -0400870 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
871 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872
Dave Barach371e4e12016-07-08 09:38:52 -0400873 fp = fopen (filename, "w");
874 if (fp == NULL)
875 {
876 rv = write (2, "Couldn't create ", 16);
877 rv = write (2, filename, strlen (filename));
878 rv = write (2, "\n", 1);
879 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000881 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
Dave Barach371e4e12016-07-08 09:38:52 -0400882 fclose (fp);
883 if (rv < 0)
884 {
885 rv = write (2, "Failed to save post-mortem API trace to ", 40);
886 rv = write (2, filename, strlen (filename));
887 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 }
Dave Barach371e4e12016-07-08 09:38:52 -0400889
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890}
891
892/* Layered message handling support */
893
Dave Barach371e4e12016-07-08 09:38:52 -0400894void
Dave Barach371e4e12016-07-08 09:38:52 -0400895vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896{
Dave Barach39d69112019-11-27 11:42:13 -0500897 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400898
899 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900}
901
Dave Barach371e4e12016-07-08 09:38:52 -0400902u16
Neale Rannse72be392017-04-26 13:59:20 -0700903vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904{
Dave Barach39d69112019-11-27 11:42:13 -0500905 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400906 u8 *name_copy;
907 vl_api_msg_range_t *rp;
908 uword *p;
909 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
Dave Barach371e4e12016-07-08 09:38:52 -0400911 if (am->msg_range_by_name == 0)
912 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913
Dave Barach371e4e12016-07-08 09:38:52 -0400914 name_copy = format (0, "%s%c", name, 0);
915
916 p = hash_get_mem (am->msg_range_by_name, name_copy);
917 if (p)
918 {
919 clib_warning ("WARNING: duplicate message range registration for '%s'",
920 name_copy);
921 vec_free (name_copy);
922 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 }
924
Dave Barach371e4e12016-07-08 09:38:52 -0400925 if (n < 0 || n > 1024)
926 {
927 clib_warning
928 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
929 n, name_copy);
930 vec_free (name_copy);
931 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 }
933
Dave Barach371e4e12016-07-08 09:38:52 -0400934 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935
Dave Barach371e4e12016-07-08 09:38:52 -0400936 rv = rp->first_msg_id = am->first_available_msg_id;
937 am->first_available_msg_id += n;
938 rp->last_msg_id = am->first_available_msg_id - 1;
939 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Dave Barach371e4e12016-07-08 09:38:52 -0400941 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942
Dave Barach371e4e12016-07-08 09:38:52 -0400943 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944}
Dave Barach371e4e12016-07-08 09:38:52 -0400945
Dave Barach557d1282016-11-10 14:22:49 -0500946void
Neale Rannse72be392017-04-26 13:59:20 -0700947vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -0500948{
949 uword *p;
950
951 if (am->msg_index_by_name_and_crc == 0)
952 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
953
954 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
955 if (p)
956 {
957 clib_warning ("attempt to redefine '%s' ignored...", string);
958 return;
959 }
960
961 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
962}
963
Dave Barach0d056e52017-09-28 15:11:16 -0400964void
965vl_msg_api_add_version (api_main_t * am, const char *string,
966 u32 major, u32 minor, u32 patch)
967{
968 api_version_t version = {.major = major,.minor = minor,.patch = patch };
969 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +0200970 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -0400971 vec_add1 (am->api_version_list, version);
972}
Dave Barach557d1282016-11-10 14:22:49 -0500973
Florin Corase86a8ed2018-01-05 03:20:25 -0800974u32
975vl_msg_api_get_msg_index (u8 * name_and_crc)
976{
Dave Barach39d69112019-11-27 11:42:13 -0500977 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800978 uword *p;
979
980 if (am->msg_index_by_name_and_crc)
981 {
982 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
983 if (p)
984 return p[0];
985 }
986 return ~0;
987}
988
Ole Troan73710c72018-06-04 22:27:49 +0200989void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100990vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
991{
992 pthread_mutex_lock (&vlib_rp->mutex);
993 return svm_push_data_heap (vlib_rp);
994}
995
996void *
Ole Troan73710c72018-06-04 22:27:49 +0200997vl_msg_push_heap (void)
998{
Dave Barach39d69112019-11-27 11:42:13 -0500999 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001000 return vl_msg_push_heap_w_region (am->vlib_rp);
1001}
1002
1003void
1004vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1005{
1006 svm_pop_heap (oldheap);
1007 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001008}
1009
1010void
1011vl_msg_pop_heap (void *oldheap)
1012{
Dave Barach39d69112019-11-27 11:42:13 -05001013 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001014 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001015}
1016
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001017/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001018int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001019vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001020{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001021 /* copy without nul terminator */
1022 u32 len = strlen (buf);
1023 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001024 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001025 str->length = htonl (len);
1026 return len + sizeof (u32);
1027}
1028
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001029/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001030int
1031vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1032{
1033 u32 len = vec_len (vec);
1034 clib_memcpy (str->buf, vec, len);
1035 str->length = htonl (len);
1036 return len + sizeof (u32);
1037}
1038
Neale Ranns377860a2019-06-21 07:57:18 -07001039u32
1040vl_api_string_len (vl_api_string_t * astr)
1041{
Dave Barach9683c1e2019-07-01 09:42:41 -04001042 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001043}
1044
Ole Troan33a58172019-09-04 09:12:29 +02001045u8 *
1046vl_api_format_string (u8 * s, va_list * args)
1047{
1048 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1049 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1050 return s;
1051}
1052
Neale Ranns377860a2019-06-21 07:57:18 -07001053/*
1054 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001055 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001056 */
1057u8 *
Dave Barach77841402020-04-29 17:04:10 -04001058vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001059{
1060 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001061
1062 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1063 return format (0, "insane astr->length %u%c",
1064 clib_net_to_host_u32 (astr->length), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001065 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001066 return v;
1067}
Ole Troan73710c72018-06-04 22:27:49 +02001068
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001069/*
1070 * Returns a new vector. Remember to free it after use.
1071 * Nul terminated.
1072 */
1073char *
1074vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1075{
1076 char *v = 0;
1077 if (clib_net_to_host_u32 (astr->length) > 0)
1078 {
1079 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1080 vec_add1 (v, 0);
1081 }
1082 return v;
1083}
1084
Dave Barachb09f4d02019-07-15 16:00:03 -04001085void
1086vl_api_set_elog_main (elog_main_t * m)
1087{
Dave Barach39d69112019-11-27 11:42:13 -05001088 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001089 am->elog_main = m;
1090}
1091
1092int
1093vl_api_set_elog_trace_api_messages (int enable)
1094{
1095 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001096 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001097
1098 rv = am->elog_trace_api_messages;
1099 am->elog_trace_api_messages = enable;
1100 return rv;
1101}
1102
1103int
1104vl_api_get_elog_trace_api_messages (void)
1105{
Dave Barach39d69112019-11-27 11:42:13 -05001106 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001107
1108 return am->elog_trace_api_messages;
1109}
1110
Dave Barach371e4e12016-07-08 09:38:52 -04001111/*
1112 * fd.io coding-style-patch-verification: ON
1113 *
1114 * Local Variables:
1115 * eval: (c-set-style "gnu")
1116 * End:
1117 */