blob: 7de1906f17a0e7ed6f8798079ba96f5f1ec29f11 [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
BenoƮt Ganne7f9256e2022-03-04 17:17:04 +010035static api_main_t api_global_main = {
36 .region_name = "/unset",
37 .api_uid = -1,
38 .api_gid = -1,
39};
Dave Barach371e4e12016-07-08 09:38:52 -040040
Dave Barach39d69112019-11-27 11:42:13 -050041/* Please use vlibapi_get_main() to access my_api_main */
42__thread api_main_t *my_api_main = &api_global_main;
43
44void
45vl_msg_api_set_global_main (void *am_arg)
46{
47 ASSERT (am_arg);
48 my_api_main = (api_main_t *) am_arg;
49}
50
Dave Barach371e4e12016-07-08 09:38:52 -040051void
52vl_msg_api_increment_missing_client_counter (void)
53{
Dave Barach39d69112019-11-27 11:42:13 -050054 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -040055 am->missing_clients++;
56}
57
Dave Barach371e4e12016-07-08 09:38:52 -040058int
59vl_msg_api_rx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
Dave Barach371e4e12016-07-08 09:38:52 -040061 return (am->rx_trace && am->rx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062}
63
Dave Barach371e4e12016-07-08 09:38:52 -040064int
65vl_msg_api_tx_trace_enabled (api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
Dave Barach371e4e12016-07-08 09:38:52 -040067 return (am->tx_trace && am->tx_trace->enabled);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068}
69
70/*
71 * vl_msg_api_trace
72 */
Dave Barach371e4e12016-07-08 09:38:52 -040073void
74vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
Dave Barach371e4e12016-07-08 09:38:52 -040076 u8 **this_trace;
77 u8 **old_trace;
78 u8 *msg_copy;
Dave Barach072f8de2016-12-02 13:31:25 -050079 u32 length;
Dave Barach9683c1e2019-07-01 09:42:41 -040080 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +020081 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach072f8de2016-12-02 13:31:25 -050082 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Damjan Marioncada9eb2022-05-18 22:16:11 +020084 if (!m || !m->trace_enable)
Dave Barach371e4e12016-07-08 09:38:52 -040085 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barach371e4e12016-07-08 09:38:52 -040087 msg_copy = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Dave Barach371e4e12016-07-08 09:38:52 -040089 if (tp->nitems == 0)
90 {
91 clib_warning ("tp->nitems is 0");
92 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 }
94
Dave Barach371e4e12016-07-08 09:38:52 -040095 if (vec_len (tp->traces) < tp->nitems)
96 {
97 vec_add1 (tp->traces, 0);
98 this_trace = tp->traces + vec_len (tp->traces) - 1;
99 }
100 else
101 {
102 tp->wrapped = 1;
103 old_trace = tp->traces + tp->curindex++;
104 if (tp->curindex == tp->nitems)
105 tp->curindex = 0;
Dave Barachf35a0722019-06-12 16:50:38 -0400106 /* Reuse the trace record, may save some memory allocator traffic */
107 msg_copy = *old_trace;
108 vec_reset_length (msg_copy);
Dave Barach371e4e12016-07-08 09:38:52 -0400109 this_trace = old_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 }
111
Dave Barach072f8de2016-12-02 13:31:25 -0500112 length = clib_net_to_host_u32 (header->data_len);
113
114 vec_validate (msg_copy, length - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500115 clib_memcpy_fast (msg_copy, msg, length);
Dave Barach371e4e12016-07-08 09:38:52 -0400116 *this_trace = msg_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117}
118
Dave Barach371e4e12016-07-08 09:38:52 -0400119int
120vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
121 int onoff)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Dave Barach371e4e12016-07-08 09:38:52 -0400123 vl_api_trace_t *tp;
124 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barach371e4e12016-07-08 09:38:52 -0400126 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 {
128 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400129 tp = am->tx_trace;
130 if (tp == 0)
131 {
132 vl_msg_api_trace_configure (am, which, 1024);
133 tp = am->tx_trace;
134 }
135 break;
136
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400138 tp = am->rx_trace;
139 if (tp == 0)
140 {
141 vl_msg_api_trace_configure (am, which, 1024);
142 tp = am->rx_trace;
143 }
144 break;
145
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400147 /* duh? */
148 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150
Dave Barach371e4e12016-07-08 09:38:52 -0400151 /* Configured? */
152 if (tp == 0 || tp->nitems == 0)
153 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Dave Barach371e4e12016-07-08 09:38:52 -0400155 rv = tp->enabled;
156 tp->enabled = onoff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Dave Barach371e4e12016-07-08 09:38:52 -0400158 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159}
160
Dave Barach371e4e12016-07-08 09:38:52 -0400161int
162vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
Dave Barach371e4e12016-07-08 09:38:52 -0400164 vl_api_trace_t *tp;
165 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Dave Barach371e4e12016-07-08 09:38:52 -0400167 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 {
169 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400170 tp = am->tx_trace;
171 break;
172
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400174 tp = am->rx_trace;
175 break;
176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400178 /* duh? */
179 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 }
181
Dave Barach371e4e12016-07-08 09:38:52 -0400182 /* Configured? */
183 if (!tp || tp->nitems == 0)
184 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach371e4e12016-07-08 09:38:52 -0400186 tp->curindex = 0;
187 tp->wrapped = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barach371e4e12016-07-08 09:38:52 -0400189 for (i = 0; i < vec_len (tp->traces); i++)
190 {
191 vec_free (tp->traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 }
Dave Barach371e4e12016-07-08 09:38:52 -0400193 vec_free (tp->traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Dave Barach371e4e12016-07-08 09:38:52 -0400195 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196}
197
Ole Troanedfe2c02019-07-30 15:38:13 +0200198u8 *
199vl_api_serialize_message_table (api_main_t * am, u8 * vector)
200{
201 serialize_main_t _sm, *sm = &_sm;
202 hash_pair_t *hp;
203 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
204
205 serialize_open_vector (sm, vector);
206
207 /* serialize the count */
208 serialize_integer (sm, nmsg, sizeof (u32));
209
Ole Troanedfe2c02019-07-30 15:38:13 +0200210 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
211 ({
212 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
213 serialize_cstring (sm, (char *) hp->key);
214 }));
Ole Troanedfe2c02019-07-30 15:38:13 +0200215
216 return serialize_close_vector (sm);
217}
218
Filip Tehlar36217e32021-07-23 08:51:10 +0000219static int
220vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
221{
222 u8 *tmpmem = 0;
223 int tlen, slen;
Filip Tehlar36217e32021-07-23 08:51:10 +0000224
225 u32 msg_length = vec_len (msg);
226 vec_validate (tmpmem, msg_length - 1);
227 clib_memcpy_fast (tmpmem, msg, msg_length);
228 u16 id = clib_net_to_host_u16 (*((u16 *) msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200229 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Filip Tehlar36217e32021-07-23 08:51:10 +0000230
Andrew Yourtchenkocbc10392022-08-17 13:26:54 +0000231 if (m && m->endian_handler)
232 {
233 m->endian_handler (tmpmem);
234 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000235
Damjan Marioncada9eb2022-05-18 22:16:11 +0200236 if (m && m->tojson_handler)
Filip Tehlar36217e32021-07-23 08:51:10 +0000237 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200238 cJSON *o = m->tojson_handler (tmpmem);
Filip Tehlar36217e32021-07-23 08:51:10 +0000239 char *s = cJSON_Print (o);
240 slen = strlen (s);
241 tlen = fwrite (s, 1, slen, fp);
242 cJSON_free (s);
243 cJSON_Delete (o);
244 vec_free (tmpmem);
245 if (tlen != slen)
246 {
247 fformat (stderr, "writing to file error\n");
248 return -11;
249 }
250 }
251 else
252 fformat (stderr, " [no registered tojson fn]\n");
253
254 return 0;
255}
256
257#define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
258
259typedef struct
260{
261 FILE *fp;
262 u32 n_traces;
263 u32 i;
264} vl_msg_write_json_args_t;
265
266static int
267vl_msg_write_json_fn (u8 *msg, void *ctx)
268{
269 vl_msg_write_json_args_t *arg = ctx;
270 FILE *fp = arg->fp;
271 api_main_t *am = vlibapi_get_main ();
272 int rc = vl_msg_api_trace_write_one (am, msg, fp);
273 if (rc < 0)
274 return rc;
275
276 if (arg->i < arg->n_traces - 1)
277 vl_msg_fwrite (",\n", fp);
278 arg->i++;
279 return 0;
280}
281
282static int
283vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
284{
285 vl_msg_write_json_args_t args;
286 clib_memset (&args, 0, sizeof (args));
287 args.fp = fp;
288 args.n_traces = vec_len (tp->traces);
289 vl_msg_fwrite ("[\n", fp);
290
291 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
292 if (rv < 0)
293 return rv;
294
295 vl_msg_fwrite ("]", fp);
296 return 0;
297}
298
Dave Barach371e4e12016-07-08 09:38:52 -0400299int
Filip Tehlar36217e32021-07-23 08:51:10 +0000300vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
301 void *ctx)
302{
303 int i;
304 u8 *msg;
305 int rv = 0;
306
307 /* No-wrap case */
308 if (tp->wrapped == 0)
309 {
310 for (i = 0; i < vec_len (tp->traces); i++)
311 {
312 /*sa_ignore NO_NULL_CHK */
313 msg = tp->traces[i];
314 if (!msg)
315 continue;
316
317 rv = fn (msg, ctx);
318 if (rv < 0)
319 return rv;
320 }
321 }
322 else
323 {
324 /* Wrap case: write oldest -> end of buffer */
325 for (i = tp->curindex; i < vec_len (tp->traces); i++)
326 {
327 msg = tp->traces[i];
328 if (!msg)
329 continue;
330
331 rv = fn (msg, ctx);
332 if (rv < 0)
333 return rv;
334 }
335 /* write beginning of buffer -> oldest-1 */
336 for (i = 0; i < tp->curindex; i++)
337 {
338 /*sa_ignore NO_NULL_CHK */
339 msg = tp->traces[i];
340 if (!msg)
341 continue;
342
343 rv = fn (msg, ctx);
344 if (rv < 0)
345 return rv;
346 }
347 }
348 return 0;
349}
350
351static int
352vl_api_msg_write_fn (u8 *msg, void *ctx)
353{
354 FILE *fp = ctx;
355 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
356 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
357 {
358 return (-14);
359 }
360 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
361 {
362 return (-14);
363 }
364 return 0;
365}
366
367int
368vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
369 u8 is_json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370{
Dave Barach371e4e12016-07-08 09:38:52 -0400371 vl_api_trace_t *tp;
372 vl_api_trace_file_header_t fh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach371e4e12016-07-08 09:38:52 -0400374 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 {
376 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400377 tp = am->tx_trace;
378 break;
379
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400381 tp = am->rx_trace;
382 break;
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400385 /* duh? */
386 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 }
388
Dave Barach371e4e12016-07-08 09:38:52 -0400389 /* Configured, data present? */
390 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
391 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Dave Barach371e4e12016-07-08 09:38:52 -0400393 /* "Dare to be stupid" check */
394 if (fp == 0)
395 {
396 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 }
398
Filip Tehlar36217e32021-07-23 08:51:10 +0000399 if (is_json)
400 return vl_msg_api_trace_write_json (am, tp, fp);
401
Dave Barach371e4e12016-07-08 09:38:52 -0400402 /* Write the file header */
Dave Barach371e4e12016-07-08 09:38:52 -0400403 fh.wrapped = tp->wrapped;
Ole Troanedfe2c02019-07-30 15:38:13 +0200404 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
Filip Tehlar36217e32021-07-23 08:51:10 +0000405
Ole Troanedfe2c02019-07-30 15:38:13 +0200406 u8 *m = vl_api_serialize_message_table (am, 0);
Ole Troanedfe2c02019-07-30 15:38:13 +0200407 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Dave Barach371e4e12016-07-08 09:38:52 -0400409 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
410 {
411 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 }
413
Ole Troanedfe2c02019-07-30 15:38:13 +0200414 /* Write the message table */
415 if (fwrite (m, vec_len (m), 1, fp) != 1)
416 {
417 return (-14);
418 }
419 vec_free (m);
420
Filip Tehlar36217e32021-07-23 08:51:10 +0000421 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422}
423
Dave Barach371e4e12016-07-08 09:38:52 -0400424int
425vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
426 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427{
Dave Barach371e4e12016-07-08 09:38:52 -0400428 vl_api_trace_t *tp;
429 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Dave Barach371e4e12016-07-08 09:38:52 -0400431 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 {
433 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400434 tp = am->tx_trace;
435 if (tp == 0)
436 {
437 vec_validate (am->tx_trace, 0);
438 tp = am->tx_trace;
439 }
440 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barach371e4e12016-07-08 09:38:52 -0400442 case VL_API_TRACE_RX:
443 tp = am->rx_trace;
444 if (tp == 0)
445 {
446 vec_validate (am->rx_trace, 0);
447 tp = am->rx_trace;
448 }
449
450 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
452 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400453 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
455 }
456
Dave Barach371e4e12016-07-08 09:38:52 -0400457 if (tp->enabled)
458 {
459 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 }
Dave Barach371e4e12016-07-08 09:38:52 -0400461 if (tp->traces)
462 {
463 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 }
465
Dave Barachb7b92992018-10-17 10:38:51 -0400466 clib_memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Dave Barach371e4e12016-07-08 09:38:52 -0400468 if (clib_arch_is_big_endian)
469 {
470 tp->endian = VL_API_BIG_ENDIAN;
471 }
472 else
473 {
474 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 }
476
Dave Barach371e4e12016-07-08 09:38:52 -0400477 tp->nitems = nitems;
478 if (was_on)
479 {
480 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 }
Dave Barach371e4e12016-07-08 09:38:52 -0400482 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483}
484
Dave Barach80f54e22017-03-08 19:08:56 -0500485void
486vl_msg_api_barrier_sync (void)
487{
488}
489
490void
491vl_msg_api_barrier_release (void)
492{
493}
494
Dave Barach371e4e12016-07-08 09:38:52 -0400495always_inline void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100496msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
497 int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498{
Dave Barach9683c1e2019-07-01 09:42:41 -0400499 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200500 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
Dave Barachb09f4d02019-07-15 16:00:03 -0400502 if (PREDICT_FALSE (am->elog_trace_api_messages))
503 {
Dave Barachb09f4d02019-07-15 16:00:03 -0400504 ELOG_TYPE_DECLARE (e) =
505 {
506 .format = "api-msg: %s",
507 .format_args = "T4",
508 };
Dave Barachb09f4d02019-07-15 16:00:03 -0400509 struct
510 {
511 u32 c;
512 } *ed;
513 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200514 if (m && m->name)
515 ed->c = elog_string (am->elog_main, (char *) m->name);
Dave Barachb09f4d02019-07-15 16:00:03 -0400516 else
517 ed->c = elog_string (am->elog_main, "BOGUS");
518 }
519
Damjan Marioncada9eb2022-05-18 22:16:11 +0200520 if (m && m->handler)
Dave Barach371e4e12016-07-08 09:38:52 -0400521 {
522 if (trace_it)
523 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barach371e4e12016-07-08 09:38:52 -0400525 if (am->msg_print_flag)
526 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200527 fformat (stdout, "[%d]: %s\n", id, m->name);
Damjan Marionfe45f8f2022-05-20 16:01:22 +0200528 fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
Dave Barach371e4e12016-07-08 09:38:52 -0400529 }
530
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100531 uword calc_size = 0;
Damjan Marioncada9eb2022-05-18 22:16:11 +0200532 ASSERT (NULL != m->calc_size_func);
533 if (m->calc_size_func)
Dave Barach371e4e12016-07-08 09:38:52 -0400534 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200535 calc_size = m->calc_size_func (the_msg);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100536 if (calc_size > msg_len)
537 {
538 clib_warning (
539 "Truncated message '%s' (id %u) received, calculated size "
540 "%lu is bigger than actual size %llu, message dropped.",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200541 m->name, id, calc_size, msg_len);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100542 }
543 }
544 else
545 {
546 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
547 "verify message size is correct",
Damjan Marioncada9eb2022-05-18 22:16:11 +0200548 m->name, id);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100549 }
550
551 /* don't process message if it's truncated, otherwise byte swaps
552 * and stuff could corrupt memory even beyond message if it's malicious
553 * e.g. VLA length field set to 1M elements, but VLA empty */
554 if (do_it && calc_size <= msg_len)
555 {
556
Damjan Marioncada9eb2022-05-18 22:16:11 +0200557 if (!m->is_mp_safe)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100558 {
559 vl_msg_api_barrier_trace_context (am->msg_names[id]);
560 vl_msg_api_barrier_sync ();
561 }
Ole Troane796a182020-05-18 11:14:05 +0200562
Damjan Marioncada9eb2022-05-18 22:16:11 +0200563 if (m->is_autoendian)
564 m->endian_handler (the_msg);
Ole Troane796a182020-05-18 11:14:05 +0200565
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000566 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
567 clib_call_callbacks (am->perf_counter_cbs, am, id,
568 0 /* before */ );
569
Damjan Marioncada9eb2022-05-18 22:16:11 +0200570 m->handler (the_msg);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000571
572 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
573 clib_call_callbacks (am->perf_counter_cbs, am, id,
574 1 /* after */ );
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100575
Damjan Marioncada9eb2022-05-18 22:16:11 +0200576 if (!m->is_mp_safe)
Dave Barach371e4e12016-07-08 09:38:52 -0400577 vl_msg_api_barrier_release ();
578 }
579 }
580 else
581 {
582 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 }
584
Dave Barach371e4e12016-07-08 09:38:52 -0400585 if (free_it)
586 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Dave Barachb09f4d02019-07-15 16:00:03 -0400588 if (PREDICT_FALSE (am->elog_trace_api_messages))
589 {
Dave Barachb09f4d02019-07-15 16:00:03 -0400590 ELOG_TYPE_DECLARE (e) =
591 {
592 .format = "api-msg-done(%s): %s",
593 .format_args = "t4T4",
594 .n_enum_strings = 2,
595 .enum_strings =
596 {
597 "barrier",
598 "mp-safe",
599 }
600 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Dave Barachb09f4d02019-07-15 16:00:03 -0400602 struct
603 {
604 u32 barrier;
605 u32 c;
606 } *ed;
607 ed = ELOG_DATA (am->elog_main, e);
Damjan Marioncada9eb2022-05-18 22:16:11 +0200608 if (m && m->name)
Dave Barach67947872019-07-19 09:31:29 -0400609 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200610 ed->c = elog_string (am->elog_main, (char *) m->name);
611 ed->barrier = !m->is_mp_safe;
Dave Barach67947872019-07-19 09:31:29 -0400612 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400613 else
Dave Barach67947872019-07-19 09:31:29 -0400614 {
615 ed->c = elog_string (am->elog_main, "BOGUS");
616 ed->barrier = 0;
617 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400618 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620
Dave Barach371e4e12016-07-08 09:38:52 -0400621void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100622vl_msg_api_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623{
Dave Barach39d69112019-11-27 11:42:13 -0500624 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100626 msg_handler_internal (am, the_msg, msg_len,
627 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
628 1 /* do_it */, 1 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629}
630
Dave Barach371e4e12016-07-08 09:38:52 -0400631void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100632vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633{
Dave Barach39d69112019-11-27 11:42:13 -0500634 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100635 msg_handler_internal (am, the_msg, msg_len,
636 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
637 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638}
639
Dave Barach371e4e12016-07-08 09:38:52 -0400640void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100641vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642{
Dave Barach39d69112019-11-27 11:42:13 -0500643 api_main_t *am = vlibapi_get_main ();
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100644 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
645 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646}
647
648/*
649 * Add a trace record to the API message trace buffer, if
650 * API message tracing is enabled. Handy for adding sufficient
651 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400652 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 * application creates database entries based on packet traffic, not
654 * control-plane messages.
655 *
656 */
Dave Barach371e4e12016-07-08 09:38:52 -0400657void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100658vl_msg_api_trace_only (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659{
Dave Barach39d69112019-11-27 11:42:13 -0500660 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100662 msg_handler_internal (am, the_msg, msg_len,
663 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
664 0 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665}
666
Dave Barach371e4e12016-07-08 09:38:52 -0400667void
668vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669{
Dave Barach39d69112019-11-27 11:42:13 -0500670 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400671 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200672 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Damjan Marioncada9eb2022-05-18 22:16:11 +0200674 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400675 {
676 clib_warning ("_vl_msg_id too large: %d\n", id);
677 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 }
Damjan Marioncada9eb2022-05-18 22:16:11 +0200679
680 if (m->cleanup_handler)
681 m->cleanup_handler (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Dave Barach371e4e12016-07-08 09:38:52 -0400683 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684}
685
686/*
687 * vl_msg_api_replay_handler
688 */
Dave Barach371e4e12016-07-08 09:38:52 -0400689void
690vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691{
Dave Barach39d69112019-11-27 11:42:13 -0500692 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400693 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Damjan Marioncada9eb2022-05-18 22:16:11 +0200694 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Damjan Marioncada9eb2022-05-18 22:16:11 +0200696 if (PREDICT_FALSE (!m))
Dave Barach371e4e12016-07-08 09:38:52 -0400697 {
698 clib_warning ("_vl_msg_id too large: %d\n", id);
699 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 }
Dave Barach371e4e12016-07-08 09:38:52 -0400701 /* do NOT trace the message... */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200702 if (m->handler)
703 m->handler (the_msg);
Dave Barach371e4e12016-07-08 09:38:52 -0400704 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705}
Dave Barach371e4e12016-07-08 09:38:52 -0400706
Dave Barachbfd92272017-05-12 11:59:25 -0400707u32
708vl_msg_api_get_msg_length (void *msg_arg)
709{
710 return vl_msg_api_get_msg_length_inline (msg_arg);
711}
712
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713/*
714 * vl_msg_api_socket_handler
715 */
Dave Barach371e4e12016-07-08 09:38:52 -0400716void
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100717vl_msg_api_socket_handler (void *the_msg, uword msg_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718{
Dave Barach39d69112019-11-27 11:42:13 -0500719 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100721 msg_handler_internal (am, the_msg, msg_len,
722 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
723 1 /* do_it */, 0 /* free_it */);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724}
725
Dave Barach371e4e12016-07-08 09:38:52 -0400726void
727vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
Dave Barach39d69112019-11-27 11:42:13 -0500729 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200730 vl_api_msg_data_t *m;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
Dave Barach65457162017-10-10 17:53:14 -0400732 /*
733 * This happens during the java core tests if the message
734 * dictionary is missing newly added xxx_reply_t messages.
735 * Should never happen, but since I shot myself in the foot once
736 * this way, I thought I'd make it easy to debug if I ever do
737 * it again... (;-)...
738 */
739 if (c->id == 0)
740 {
741 if (c->name)
742 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
743 else
744 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
745 clib_warning ("Did you forget to call setup_message_id_table?");
746 return;
747 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Damjan Marioncada9eb2022-05-18 22:16:11 +0200749 vec_validate (am->msg_data, c->id);
750 m = vl_api_get_msg_data (am, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Damjan Marioncada9eb2022-05-18 22:16:11 +0200752 if (m->handler && m->handler != c->handler)
753 clib_warning ("BUG: re-registering 'vl_api_%s_t_handler'."
754 "Handler was %llx, replaced by %llx",
755 c->name, m->handler, c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500756
Damjan Marioncada9eb2022-05-18 22:16:11 +0200757 m->name = c->name;
758 m->handler = c->handler;
759 m->cleanup_handler = c->cleanup;
760 m->endian_handler = c->endian;
Damjan Marionfe45f8f2022-05-20 16:01:22 +0200761 m->format_fn = c->format_fn;
Damjan Marioncada9eb2022-05-18 22:16:11 +0200762 m->tojson_handler = c->tojson;
763 m->fromjson_handler = c->fromjson;
764 m->calc_size_func = c->calc_size;
765 m->bounce = c->message_bounce;
766 m->is_mp_safe = c->is_mp_safe;
767 m->is_autoendian = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Damjan Marioncada9eb2022-05-18 22:16:11 +0200769 m->trace_size = c->size;
770 m->trace_enable = c->traced;
771 m->replay_allowed = c->replay;
Filip Tehlar36217e32021-07-23 08:51:10 +0000772
773 if (!am->msg_id_by_name)
774 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
775
776 hash_set_mem (am->msg_id_by_name, c->name, c->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777}
778
Dave Barach371e4e12016-07-08 09:38:52 -0400779void
Matej Perina75a17ec2017-09-21 17:03:27 +0200780vl_msg_api_clean_handlers (int msg_id)
781{
782 vl_msg_api_msg_config_t cfg;
783 vl_msg_api_msg_config_t *c = &cfg;
784
Dave Barachb7b92992018-10-17 10:38:51 -0400785 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +0200786
787 c->id = msg_id;
788 vl_msg_api_config (c);
789}
790
791void
Dave Barach371e4e12016-07-08 09:38:52 -0400792vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793{
Dave Barach39d69112019-11-27 11:42:13 -0500794 api_main_t *am = vlibapi_get_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200795 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
Dave Barach371e4e12016-07-08 09:38:52 -0400796 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Damjan Marioncada9eb2022-05-18 22:16:11 +0200798 m->cleanup_handler = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799}
800
Dave Barach371e4e12016-07-08 09:38:52 -0400801void
Florin Corase86a8ed2018-01-05 03:20:25 -0800802vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803{
Dave Barach371e4e12016-07-08 09:38:52 -0400804 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100806 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
807 {
BenoƮt Ganne49bbf9e2023-01-26 19:27:20 +0100808 VL_MSG_API_UNPOISON ((u8 *) msg);
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100809 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
810 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
811 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812}
813
Dave Barach77841402020-04-29 17:04:10 -0400814u32
815vl_msg_api_max_length (void *mp)
816{
817 msgbuf_t *mb;
818 u32 data_len = ~0;
819
820 /* Work out the maximum sane message length, and return it */
821 if (PREDICT_TRUE (mp != 0))
822 {
823 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
824 data_len = clib_net_to_host_u32 (mb->data_len);
825 }
826 return data_len;
827}
828
Dave Barach371e4e12016-07-08 09:38:52 -0400829vl_api_trace_t *
830vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831{
Dave Barach371e4e12016-07-08 09:38:52 -0400832 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833 {
834 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400835 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400837 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400839 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840 }
841}
842
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843static u8 post_mortem_dump_enabled;
844
Dave Barach80f54e22017-03-08 19:08:56 -0500845void
846vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847{
Dave Barach80f54e22017-03-08 19:08:56 -0500848 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849}
850
Dave Barach371e4e12016-07-08 09:38:52 -0400851void
852vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853{
Dave Barach39d69112019-11-27 11:42:13 -0500854 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400855 FILE *fp;
856 char filename[64];
857 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858
Dave Barach371e4e12016-07-08 09:38:52 -0400859 if (post_mortem_dump_enabled == 0)
860 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
Dave Barach371e4e12016-07-08 09:38:52 -0400862 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
863 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Dave Barach371e4e12016-07-08 09:38:52 -0400865 fp = fopen (filename, "w");
866 if (fp == NULL)
867 {
868 rv = write (2, "Couldn't create ", 16);
869 rv = write (2, filename, strlen (filename));
870 rv = write (2, "\n", 1);
871 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000873 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
Dave Barach371e4e12016-07-08 09:38:52 -0400874 fclose (fp);
875 if (rv < 0)
876 {
877 rv = write (2, "Failed to save post-mortem API trace to ", 40);
878 rv = write (2, filename, strlen (filename));
879 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 }
Dave Barach371e4e12016-07-08 09:38:52 -0400881
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882}
883
884/* Layered message handling support */
885
Dave Barach371e4e12016-07-08 09:38:52 -0400886void
Dave Barach371e4e12016-07-08 09:38:52 -0400887vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888{
Dave Barach39d69112019-11-27 11:42:13 -0500889 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400890
891 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892}
893
Dave Barach371e4e12016-07-08 09:38:52 -0400894u16
Neale Rannse72be392017-04-26 13:59:20 -0700895vl_msg_api_get_msg_ids (const char *name, int n)
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 u8 *name_copy;
899 vl_api_msg_range_t *rp;
900 uword *p;
901 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
Dave Barach371e4e12016-07-08 09:38:52 -0400903 if (am->msg_range_by_name == 0)
904 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905
Dave Barach371e4e12016-07-08 09:38:52 -0400906 name_copy = format (0, "%s%c", name, 0);
907
908 p = hash_get_mem (am->msg_range_by_name, name_copy);
909 if (p)
910 {
911 clib_warning ("WARNING: duplicate message range registration for '%s'",
912 name_copy);
913 vec_free (name_copy);
914 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915 }
916
Dave Barach371e4e12016-07-08 09:38:52 -0400917 if (n < 0 || n > 1024)
918 {
919 clib_warning
920 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
921 n, name_copy);
922 vec_free (name_copy);
923 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 }
925
Dave Barach371e4e12016-07-08 09:38:52 -0400926 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
Dave Barach371e4e12016-07-08 09:38:52 -0400928 rv = rp->first_msg_id = am->first_available_msg_id;
929 am->first_available_msg_id += n;
930 rp->last_msg_id = am->first_available_msg_id - 1;
931 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932
Dave Barach371e4e12016-07-08 09:38:52 -0400933 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
Dave Barach371e4e12016-07-08 09:38:52 -0400935 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936}
Dave Barach371e4e12016-07-08 09:38:52 -0400937
Dave Barach557d1282016-11-10 14:22:49 -0500938void
Neale Rannse72be392017-04-26 13:59:20 -0700939vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -0500940{
941 uword *p;
942
943 if (am->msg_index_by_name_and_crc == 0)
944 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
945
946 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
947 if (p)
948 {
949 clib_warning ("attempt to redefine '%s' ignored...", string);
950 return;
951 }
952
953 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
954}
955
Dave Barach0d056e52017-09-28 15:11:16 -0400956void
957vl_msg_api_add_version (api_main_t * am, const char *string,
958 u32 major, u32 minor, u32 patch)
959{
960 api_version_t version = {.major = major,.minor = minor,.patch = patch };
961 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +0200962 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -0400963 vec_add1 (am->api_version_list, version);
964}
Dave Barach557d1282016-11-10 14:22:49 -0500965
Florin Corase86a8ed2018-01-05 03:20:25 -0800966u32
967vl_msg_api_get_msg_index (u8 * name_and_crc)
968{
Dave Barach39d69112019-11-27 11:42:13 -0500969 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800970 uword *p;
971
972 if (am->msg_index_by_name_and_crc)
973 {
974 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
975 if (p)
976 return p[0];
977 }
978 return ~0;
979}
980
Ole Troan73710c72018-06-04 22:27:49 +0200981void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100982vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
983{
984 pthread_mutex_lock (&vlib_rp->mutex);
985 return svm_push_data_heap (vlib_rp);
986}
987
988void *
Ole Troan73710c72018-06-04 22:27:49 +0200989vl_msg_push_heap (void)
990{
Dave Barach39d69112019-11-27 11:42:13 -0500991 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100992 return vl_msg_push_heap_w_region (am->vlib_rp);
993}
994
995void
996vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
997{
998 svm_pop_heap (oldheap);
999 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001000}
1001
1002void
1003vl_msg_pop_heap (void *oldheap)
1004{
Dave Barach39d69112019-11-27 11:42:13 -05001005 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001006 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001007}
1008
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001009/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001010int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001011vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001012{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001013 /* copy without nul terminator */
1014 u32 len = strlen (buf);
1015 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001016 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001017 str->length = htonl (len);
1018 return len + sizeof (u32);
1019}
1020
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001021/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001022int
1023vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1024{
1025 u32 len = vec_len (vec);
1026 clib_memcpy (str->buf, vec, len);
1027 str->length = htonl (len);
1028 return len + sizeof (u32);
1029}
1030
Neale Ranns377860a2019-06-21 07:57:18 -07001031u32
1032vl_api_string_len (vl_api_string_t * astr)
1033{
Dave Barach9683c1e2019-07-01 09:42:41 -04001034 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001035}
1036
Ole Troan33a58172019-09-04 09:12:29 +02001037u8 *
1038vl_api_format_string (u8 * s, va_list * args)
1039{
1040 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1041 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1042 return s;
1043}
1044
Neale Ranns377860a2019-06-21 07:57:18 -07001045/*
1046 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001047 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001048 */
1049u8 *
Dave Barach77841402020-04-29 17:04:10 -04001050vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001051{
1052 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001053
1054 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
Dave Wallace0b7200f2023-05-10 12:05:55 -04001055 return format (0, "Invalid astr->length %u > max (%u)%c",
1056 clib_net_to_host_u32 (astr->length),
1057 vl_msg_api_max_length (mp), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001058 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001059 return v;
1060}
Ole Troan73710c72018-06-04 22:27:49 +02001061
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001062/*
1063 * Returns a new vector. Remember to free it after use.
1064 * Nul terminated.
1065 */
1066char *
1067vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1068{
1069 char *v = 0;
1070 if (clib_net_to_host_u32 (astr->length) > 0)
1071 {
1072 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1073 vec_add1 (v, 0);
1074 }
1075 return v;
1076}
1077
Dave Barachb09f4d02019-07-15 16:00:03 -04001078void
1079vl_api_set_elog_main (elog_main_t * m)
1080{
Dave Barach39d69112019-11-27 11:42:13 -05001081 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001082 am->elog_main = m;
1083}
1084
1085int
1086vl_api_set_elog_trace_api_messages (int enable)
1087{
1088 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001089 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001090
1091 rv = am->elog_trace_api_messages;
1092 am->elog_trace_api_messages = enable;
1093 return rv;
1094}
1095
1096int
1097vl_api_get_elog_trace_api_messages (void)
1098{
Dave Barach39d69112019-11-27 11:42:13 -05001099 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001100
1101 return am->elog_trace_api_messages;
1102}
1103
Dave Barach371e4e12016-07-08 09:38:52 -04001104/*
1105 * fd.io coding-style-patch-verification: ON
1106 *
1107 * Local Variables:
1108 * eval: (c-set-style "gnu")
1109 * End:
1110 */