blob: caad6e54828211983c57ad89e92bbec300d42a62 [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>
33
Dave Barach80f54e22017-03-08 19:08:56 -050034/* *INDENT-OFF* */
Dave Barach39d69112019-11-27 11:42:13 -050035api_main_t api_global_main =
Dave Barach80f54e22017-03-08 19:08:56 -050036 {
37 .region_name = "/unset",
38 .api_uid = -1,
39 .api_gid = -1,
40 };
41/* *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 Barach371e4e12016-07-08 09:38:52 -040082 trace_cfg_t *cfgp;
Dave Barach9683c1e2019-07-01 09:42:41 -040083 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
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
Dave Barach371e4e12016-07-08 09:38:52 -040086 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barach371e4e12016-07-08 09:38:52 -040088 if (!cfgp || !cfgp->trace_enable)
89 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach371e4e12016-07-08 09:38:52 -040091 msg_copy = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Dave Barach371e4e12016-07-08 09:38:52 -040093 if (tp->nitems == 0)
94 {
95 clib_warning ("tp->nitems is 0");
96 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 }
98
Dave Barach371e4e12016-07-08 09:38:52 -040099 if (vec_len (tp->traces) < tp->nitems)
100 {
101 vec_add1 (tp->traces, 0);
102 this_trace = tp->traces + vec_len (tp->traces) - 1;
103 }
104 else
105 {
106 tp->wrapped = 1;
107 old_trace = tp->traces + tp->curindex++;
108 if (tp->curindex == tp->nitems)
109 tp->curindex = 0;
Dave Barachf35a0722019-06-12 16:50:38 -0400110 /* Reuse the trace record, may save some memory allocator traffic */
111 msg_copy = *old_trace;
112 vec_reset_length (msg_copy);
Dave Barach371e4e12016-07-08 09:38:52 -0400113 this_trace = old_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 }
115
Dave Barach072f8de2016-12-02 13:31:25 -0500116 length = clib_net_to_host_u32 (header->data_len);
117
118 vec_validate (msg_copy, length - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500119 clib_memcpy_fast (msg_copy, msg, length);
Dave Barach371e4e12016-07-08 09:38:52 -0400120 *this_trace = msg_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121}
122
Dave Barach371e4e12016-07-08 09:38:52 -0400123int
124vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
125 int onoff)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126{
Dave Barach371e4e12016-07-08 09:38:52 -0400127 vl_api_trace_t *tp;
128 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barach371e4e12016-07-08 09:38:52 -0400130 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 {
132 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400133 tp = am->tx_trace;
134 if (tp == 0)
135 {
136 vl_msg_api_trace_configure (am, which, 1024);
137 tp = am->tx_trace;
138 }
139 break;
140
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400142 tp = am->rx_trace;
143 if (tp == 0)
144 {
145 vl_msg_api_trace_configure (am, which, 1024);
146 tp = am->rx_trace;
147 }
148 break;
149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400151 /* duh? */
152 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 }
154
Dave Barach371e4e12016-07-08 09:38:52 -0400155 /* Configured? */
156 if (tp == 0 || tp->nitems == 0)
157 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Dave Barach371e4e12016-07-08 09:38:52 -0400159 rv = tp->enabled;
160 tp->enabled = onoff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach371e4e12016-07-08 09:38:52 -0400162 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163}
164
Dave Barach371e4e12016-07-08 09:38:52 -0400165int
166vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167{
Dave Barach371e4e12016-07-08 09:38:52 -0400168 vl_api_trace_t *tp;
169 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Dave Barach371e4e12016-07-08 09:38:52 -0400171 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 {
173 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400174 tp = am->tx_trace;
175 break;
176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400178 tp = am->rx_trace;
179 break;
180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400182 /* duh? */
183 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 }
185
Dave Barach371e4e12016-07-08 09:38:52 -0400186 /* Configured? */
187 if (!tp || tp->nitems == 0)
188 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Dave Barach371e4e12016-07-08 09:38:52 -0400190 tp->curindex = 0;
191 tp->wrapped = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach371e4e12016-07-08 09:38:52 -0400193 for (i = 0; i < vec_len (tp->traces); i++)
194 {
195 vec_free (tp->traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 }
Dave Barach371e4e12016-07-08 09:38:52 -0400197 vec_free (tp->traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Dave Barach371e4e12016-07-08 09:38:52 -0400199 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200}
201
Ole Troanedfe2c02019-07-30 15:38:13 +0200202u8 *
203vl_api_serialize_message_table (api_main_t * am, u8 * vector)
204{
205 serialize_main_t _sm, *sm = &_sm;
206 hash_pair_t *hp;
207 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
208
209 serialize_open_vector (sm, vector);
210
211 /* serialize the count */
212 serialize_integer (sm, nmsg, sizeof (u32));
213
214 /* *INDENT-OFF* */
215 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
216 ({
217 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
218 serialize_cstring (sm, (char *) hp->key);
219 }));
220 /* *INDENT-ON* */
221
222 return serialize_close_vector (sm);
223}
224
Dave Barach371e4e12016-07-08 09:38:52 -0400225int
226vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227{
Dave Barach371e4e12016-07-08 09:38:52 -0400228 vl_api_trace_t *tp;
229 vl_api_trace_file_header_t fh;
230 int i;
231 u8 *msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
Dave Barach371e4e12016-07-08 09:38:52 -0400233 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 {
235 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400236 tp = am->tx_trace;
237 break;
238
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400240 tp = am->rx_trace;
241 break;
242
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400244 /* duh? */
245 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 }
247
Dave Barach371e4e12016-07-08 09:38:52 -0400248 /* Configured, data present? */
249 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
250 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Dave Barach371e4e12016-07-08 09:38:52 -0400252 /* "Dare to be stupid" check */
253 if (fp == 0)
254 {
255 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 }
257
Dave Barach371e4e12016-07-08 09:38:52 -0400258 /* Write the file header */
Dave Barach371e4e12016-07-08 09:38:52 -0400259 fh.wrapped = tp->wrapped;
Ole Troanedfe2c02019-07-30 15:38:13 +0200260 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
261 u8 *m = vl_api_serialize_message_table (am, 0);
Ole Troanedfe2c02019-07-30 15:38:13 +0200262 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Dave Barach371e4e12016-07-08 09:38:52 -0400264 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
265 {
266 return (-10);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 }
268
Ole Troanedfe2c02019-07-30 15:38:13 +0200269 /* Write the message table */
270 if (fwrite (m, vec_len (m), 1, fp) != 1)
271 {
272 return (-14);
273 }
274 vec_free (m);
275
Dave Barach371e4e12016-07-08 09:38:52 -0400276 /* No-wrap case */
277 if (tp->wrapped == 0)
278 {
279 /*
280 * Note: vec_len return 0 when fed a NULL pointer.
281 * Unfortunately, the static analysis tool doesn't
282 * figure it out, hence the suppressed warnings.
283 * What a great use of my time.
284 */
285 for (i = 0; i < vec_len (tp->traces); i++)
286 {
Dave Barach072f8de2016-12-02 13:31:25 -0500287 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400288 /*sa_ignore NO_NULL_CHK */
289 msg = tp->traces[i];
290 /*
291 * This retarded check required to pass
292 * [sic] SA-checking.
293 */
294 if (!msg)
295 continue;
Dave Barach072f8de2016-12-02 13:31:25 -0500296
297 msg_length = clib_host_to_net_u32 (vec_len (msg));
298 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
299 != sizeof (msg_length))
300 {
301 return (-14);
302 }
Dave Barach371e4e12016-07-08 09:38:52 -0400303 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
304 {
305 return (-11);
306 }
307 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 }
Dave Barach371e4e12016-07-08 09:38:52 -0400309 else
310 {
311 /* Wrap case: write oldest -> end of buffer */
312 for (i = tp->curindex; i < vec_len (tp->traces); i++)
313 {
Dave Barach072f8de2016-12-02 13:31:25 -0500314 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400315 msg = tp->traces[i];
316 /*
317 * This retarded check required to pass
318 * [sic] SA-checking
319 */
320 if (!msg)
321 continue;
322
Dave Barach072f8de2016-12-02 13:31:25 -0500323 msg_length = clib_host_to_net_u32 (vec_len (msg));
324 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
325 != sizeof (msg_length))
326 {
327 return (-14);
328 }
329
Dave Barach371e4e12016-07-08 09:38:52 -0400330 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
331 {
332 return (-12);
333 }
334 }
335 /* write beginning of buffer -> oldest-1 */
336 for (i = 0; i < tp->curindex; i++)
337 {
Dave Barach072f8de2016-12-02 13:31:25 -0500338 u32 msg_length;
Dave Barach371e4e12016-07-08 09:38:52 -0400339 /*sa_ignore NO_NULL_CHK */
340 msg = tp->traces[i];
341 /*
342 * This retarded check required to pass
343 * [sic] SA-checking
344 */
345 if (!msg)
346 continue;
347
Dave Barach072f8de2016-12-02 13:31:25 -0500348 msg_length = clib_host_to_net_u32 (vec_len (msg));
349 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
350 != sizeof (msg_length))
351 {
352 return (-14);
353 }
354
Dave Barach371e4e12016-07-08 09:38:52 -0400355 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
356 {
357 return (-13);
358 }
359 }
360 }
361 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362}
363
Dave Barach371e4e12016-07-08 09:38:52 -0400364int
365vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
366 u32 nitems)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367{
Dave Barach371e4e12016-07-08 09:38:52 -0400368 vl_api_trace_t *tp;
369 int was_on = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach371e4e12016-07-08 09:38:52 -0400371 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 {
373 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400374 tp = am->tx_trace;
375 if (tp == 0)
376 {
377 vec_validate (am->tx_trace, 0);
378 tp = am->tx_trace;
379 }
380 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Dave Barach371e4e12016-07-08 09:38:52 -0400382 case VL_API_TRACE_RX:
383 tp = am->rx_trace;
384 if (tp == 0)
385 {
386 vec_validate (am->rx_trace, 0);
387 tp = am->rx_trace;
388 }
389
390 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400393 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395 }
396
Dave Barach371e4e12016-07-08 09:38:52 -0400397 if (tp->enabled)
398 {
399 was_on = vl_msg_api_trace_onoff (am, which, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 }
Dave Barach371e4e12016-07-08 09:38:52 -0400401 if (tp->traces)
402 {
403 vl_msg_api_trace_free (am, which);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 }
405
Dave Barachb7b92992018-10-17 10:38:51 -0400406 clib_memset (tp, 0, sizeof (*tp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Dave Barach371e4e12016-07-08 09:38:52 -0400408 if (clib_arch_is_big_endian)
409 {
410 tp->endian = VL_API_BIG_ENDIAN;
411 }
412 else
413 {
414 tp->endian = VL_API_LITTLE_ENDIAN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 }
416
Dave Barach371e4e12016-07-08 09:38:52 -0400417 tp->nitems = nitems;
418 if (was_on)
419 {
420 (void) vl_msg_api_trace_onoff (am, which, was_on);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 }
Dave Barach371e4e12016-07-08 09:38:52 -0400422 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423}
424
Dave Barach80f54e22017-03-08 19:08:56 -0500425void
426vl_msg_api_barrier_sync (void)
427{
428}
429
430void
431vl_msg_api_barrier_release (void)
432{
433}
434
Dave Barach371e4e12016-07-08 09:38:52 -0400435always_inline void
436msg_handler_internal (api_main_t * am,
437 void *the_msg, int trace_it, int do_it, int free_it)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438{
Dave Barach9683c1e2019-07-01 09:42:41 -0400439 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Dave Barach371e4e12016-07-08 09:38:52 -0400440 u8 *(*print_fp) (void *, void *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barachb09f4d02019-07-15 16:00:03 -0400442 if (PREDICT_FALSE (am->elog_trace_api_messages))
443 {
444 /* *INDENT-OFF* */
445 ELOG_TYPE_DECLARE (e) =
446 {
447 .format = "api-msg: %s",
448 .format_args = "T4",
449 };
450 /* *INDENT-ON* */
451 struct
452 {
453 u32 c;
454 } *ed;
455 ed = ELOG_DATA (am->elog_main, e);
456 if (id < vec_len (am->msg_names))
457 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
458 else
459 ed->c = elog_string (am->elog_main, "BOGUS");
460 }
461
Dave Barach371e4e12016-07-08 09:38:52 -0400462 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
463 {
464 if (trace_it)
465 vl_msg_api_trace (am, am->rx_trace, the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
Dave Barach371e4e12016-07-08 09:38:52 -0400467 if (am->msg_print_flag)
468 {
469 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
470 print_fp = (void *) am->msg_print_handlers[id];
471 if (print_fp == 0)
472 {
473 fformat (stdout, " [no registered print fn]\n");
474 }
475 else
476 {
477 (*print_fp) (the_msg, stdout);
478 }
479 }
480
481 if (do_it)
482 {
483 if (!am->is_mp_safe[id])
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100484 {
485 vl_msg_api_barrier_trace_context (am->msg_names[id]);
486 vl_msg_api_barrier_sync ();
487 }
Ole Troane796a182020-05-18 11:14:05 +0200488
489 if (am->is_autoendian[id])
490 {
491 void (*endian_fp) (void *);
492 endian_fp = am->msg_endian_handlers[id];
493 (*endian_fp) (the_msg);
494 }
495
Dave Barach371e4e12016-07-08 09:38:52 -0400496 (*am->msg_handlers[id]) (the_msg);
497 if (!am->is_mp_safe[id])
498 vl_msg_api_barrier_release ();
499 }
500 }
501 else
502 {
503 clib_warning ("no handler for msg id %d", id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 }
505
Dave Barach371e4e12016-07-08 09:38:52 -0400506 if (free_it)
507 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barachb09f4d02019-07-15 16:00:03 -0400509 if (PREDICT_FALSE (am->elog_trace_api_messages))
510 {
511 /* *INDENT-OFF* */
512 ELOG_TYPE_DECLARE (e) =
513 {
514 .format = "api-msg-done(%s): %s",
515 .format_args = "t4T4",
516 .n_enum_strings = 2,
517 .enum_strings =
518 {
519 "barrier",
520 "mp-safe",
521 }
522 };
523 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barachb09f4d02019-07-15 16:00:03 -0400525 struct
526 {
527 u32 barrier;
528 u32 c;
529 } *ed;
530 ed = ELOG_DATA (am->elog_main, e);
531 if (id < vec_len (am->msg_names))
Dave Barach67947872019-07-19 09:31:29 -0400532 {
533 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
534 ed->barrier = !am->is_mp_safe[id];
535 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400536 else
Dave Barach67947872019-07-19 09:31:29 -0400537 {
538 ed->c = elog_string (am->elog_main, "BOGUS");
539 ed->barrier = 0;
540 }
Dave Barachb09f4d02019-07-15 16:00:03 -0400541 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Dave Barach77841402020-04-29 17:04:10 -0400544void (*vl_msg_api_fuzz_hook) (u16, void *);
545
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546/* This is only to be called from a vlib/vnet app */
Dave Barach371e4e12016-07-08 09:38:52 -0400547void
Florin Coras8d820852019-11-27 09:15:25 -0800548vl_msg_api_handler_with_vm_node (api_main_t * am, svm_region_t * vlib_rp,
Dave Barach371e4e12016-07-08 09:38:52 -0400549 void *the_msg, vlib_main_t * vm,
Florin Coras8d820852019-11-27 09:15:25 -0800550 vlib_node_runtime_t * node, u8 is_private)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551{
Dave Barach9683c1e2019-07-01 09:42:41 -0400552 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Dave Barach371e4e12016-07-08 09:38:52 -0400553 u8 *(*handler) (void *, void *, void *);
ezkexma75f9fb62019-03-21 07:38:19 -0400554 u8 *(*print_fp) (void *, void *);
Florin Coras8d820852019-11-27 09:15:25 -0800555 svm_region_t *old_vlib_rp;
556 void *save_shmem_hdr;
Dave Barachc898a4f2019-06-14 17:29:55 -0400557 int is_mp_safe = 1;
Dave Barach371e4e12016-07-08 09:38:52 -0400558
Dave Barachb09f4d02019-07-15 16:00:03 -0400559 if (PREDICT_FALSE (am->elog_trace_api_messages))
Dave Barach371e4e12016-07-08 09:38:52 -0400560 {
Dave Barachc3a06552018-10-01 09:25:32 -0400561 /* *INDENT-OFF* */
562 ELOG_TYPE_DECLARE (e) =
563 {
564 .format = "api-msg: %s",
565 .format_args = "T4",
566 };
567 /* *INDENT-ON* */
568 struct
569 {
570 u32 c;
571 } *ed;
Dave Barachb09f4d02019-07-15 16:00:03 -0400572 ed = ELOG_DATA (am->elog_main, e);
Dave Barachc3a06552018-10-01 09:25:32 -0400573 if (id < vec_len (am->msg_names))
Dave Barachb09f4d02019-07-15 16:00:03 -0400574 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
Dave Barachc3a06552018-10-01 09:25:32 -0400575 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400576 ed->c = elog_string (am->elog_main, "BOGUS");
Dave Barachc3a06552018-10-01 09:25:32 -0400577 }
Dave Barach371e4e12016-07-08 09:38:52 -0400578
579 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
580 {
581 handler = (void *) am->msg_handlers[id];
582
ezkexma75f9fb62019-03-21 07:38:19 -0400583 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
Dave Barach371e4e12016-07-08 09:38:52 -0400584 vl_msg_api_trace (am, am->rx_trace, the_msg);
585
ezkexma75f9fb62019-03-21 07:38:19 -0400586 if (PREDICT_FALSE (am->msg_print_flag))
587 {
588 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
589 print_fp = (void *) am->msg_print_handlers[id];
590 if (print_fp == 0)
591 {
592 fformat (stdout, " [no registered print fn for msg %d]\n", id);
593 }
594 else
595 {
596 (*print_fp) (the_msg, vm);
597 }
598 }
Dave Barachc898a4f2019-06-14 17:29:55 -0400599 is_mp_safe = am->is_mp_safe[id];
ezkexma75f9fb62019-03-21 07:38:19 -0400600
Dave Barachc898a4f2019-06-14 17:29:55 -0400601 if (!is_mp_safe)
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +0100602 {
603 vl_msg_api_barrier_trace_context (am->msg_names[id]);
604 vl_msg_api_barrier_sync ();
605 }
Florin Coras8d820852019-11-27 09:15:25 -0800606 if (is_private)
607 {
608 old_vlib_rp = am->vlib_rp;
609 save_shmem_hdr = am->shmem_hdr;
610 am->vlib_rp = vlib_rp;
611 am->shmem_hdr = (void *) vlib_rp->user_ctx;
612 }
Dave Barach77841402020-04-29 17:04:10 -0400613
614 if (PREDICT_FALSE (vl_msg_api_fuzz_hook != 0))
615 (*vl_msg_api_fuzz_hook) (id, the_msg);
616
Ole Troane796a182020-05-18 11:14:05 +0200617 if (am->is_autoendian[id])
618 {
619 void (*endian_fp) (void *);
620 endian_fp = am->msg_endian_handlers[id];
621 (*endian_fp) (the_msg);
622 }
623
Dave Barach371e4e12016-07-08 09:38:52 -0400624 (*handler) (the_msg, vm, node);
Florin Coras8d820852019-11-27 09:15:25 -0800625 if (is_private)
626 {
627 am->vlib_rp = old_vlib_rp;
628 am->shmem_hdr = save_shmem_hdr;
629 }
Dave Barachc898a4f2019-06-14 17:29:55 -0400630 if (!is_mp_safe)
Dave Barach371e4e12016-07-08 09:38:52 -0400631 vl_msg_api_barrier_release ();
632 }
633 else
634 {
Jon Loeligerf95b37e2017-02-13 15:21:12 -0600635 clib_warning ("no handler for msg id %d", id);
Dave Barach371e4e12016-07-08 09:38:52 -0400636 }
637
638 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 * Special-case, so we can e.g. bounce messages off the vnet
640 * main thread without copying them...
641 */
Benoît Ganneff13e462020-01-21 18:33:14 +0100642 if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id]))
Dave Barach371e4e12016-07-08 09:38:52 -0400643 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644
Dave Barachb09f4d02019-07-15 16:00:03 -0400645 if (PREDICT_FALSE (am->elog_trace_api_messages))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646 {
Dave Barachc3a06552018-10-01 09:25:32 -0400647 /* *INDENT-OFF* */
Dave Barachc898a4f2019-06-14 17:29:55 -0400648 ELOG_TYPE_DECLARE (e) =
649 {
650 .format = "api-msg-done(%s): %s",
651 .format_args = "t4T4",
652 .n_enum_strings = 2,
653 .enum_strings =
654 {
655 "barrier",
656 "mp-safe",
657 }
658 };
Dave Barachc3a06552018-10-01 09:25:32 -0400659 /* *INDENT-ON* */
660
661 struct
662 {
Dave Barachc898a4f2019-06-14 17:29:55 -0400663 u32 barrier;
Dave Barachc3a06552018-10-01 09:25:32 -0400664 u32 c;
665 } *ed;
Dave Barachb09f4d02019-07-15 16:00:03 -0400666 ed = ELOG_DATA (am->elog_main, e);
Dave Barachc3a06552018-10-01 09:25:32 -0400667 if (id < vec_len (am->msg_names))
Dave Barachb09f4d02019-07-15 16:00:03 -0400668 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
Dave Barachc3a06552018-10-01 09:25:32 -0400669 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400670 ed->c = elog_string (am->elog_main, "BOGUS");
Dave Barachc898a4f2019-06-14 17:29:55 -0400671 ed->barrier = is_mp_safe;
Dave Barachc3a06552018-10-01 09:25:32 -0400672 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673}
674
Dave Barach371e4e12016-07-08 09:38:52 -0400675void
676vl_msg_api_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 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
Dave Barach371e4e12016-07-08 09:38:52 -0400680 msg_handler_internal (am, the_msg,
681 (am->rx_trace
682 && am->rx_trace->enabled) /* trace_it */ ,
683 1 /* do_it */ , 1 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684}
685
Dave Barach371e4e12016-07-08 09:38:52 -0400686void
687vl_msg_api_handler_no_free (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688{
Dave Barach39d69112019-11-27 11:42:13 -0500689 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400690 msg_handler_internal (am, the_msg,
691 (am->rx_trace
692 && am->rx_trace->enabled) /* trace_it */ ,
693 1 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694}
695
Dave Barach371e4e12016-07-08 09:38:52 -0400696void
697vl_msg_api_handler_no_trace_no_free (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698{
Dave Barach39d69112019-11-27 11:42:13 -0500699 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400700 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
701 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702}
703
704/*
705 * Add a trace record to the API message trace buffer, if
706 * API message tracing is enabled. Handy for adding sufficient
707 * data to the trace to reproduce autonomous state, as opposed to
Dave Barach371e4e12016-07-08 09:38:52 -0400708 * state downloaded via control-plane API messages. Example: the NAT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 * application creates database entries based on packet traffic, not
710 * control-plane messages.
711 *
712 */
Dave Barach371e4e12016-07-08 09:38:52 -0400713void
714vl_msg_api_trace_only (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715{
Dave Barach39d69112019-11-27 11:42:13 -0500716 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
Dave Barach371e4e12016-07-08 09:38:52 -0400718 msg_handler_internal (am, the_msg,
719 (am->rx_trace
720 && am->rx_trace->enabled) /* trace_it */ ,
721 0 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722}
723
Dave Barach371e4e12016-07-08 09:38:52 -0400724void
725vl_msg_api_cleanup_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726{
Dave Barach39d69112019-11-27 11:42:13 -0500727 api_main_t *am = vlibapi_get_main ();
Dave Barach9683c1e2019-07-01 09:42:41 -0400728 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729
Dave Barach371e4e12016-07-08 09:38:52 -0400730 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
731 {
732 clib_warning ("_vl_msg_id too large: %d\n", id);
733 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 }
Dave Barach371e4e12016-07-08 09:38:52 -0400735 if (am->msg_cleanup_handlers[id])
736 (*am->msg_cleanup_handlers[id]) (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Dave Barach371e4e12016-07-08 09:38:52 -0400738 vl_msg_api_free (the_msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739}
740
741/*
742 * vl_msg_api_replay_handler
743 */
Dave Barach371e4e12016-07-08 09:38:52 -0400744void
745vl_msg_api_replay_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746{
Dave Barach39d69112019-11-27 11:42:13 -0500747 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Dave Barach9683c1e2019-07-01 09:42:41 -0400749 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach371e4e12016-07-08 09:38:52 -0400751 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
752 {
753 clib_warning ("_vl_msg_id too large: %d\n", id);
754 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 }
Dave Barach371e4e12016-07-08 09:38:52 -0400756 /* do NOT trace the message... */
757 if (am->msg_handlers[id])
758 (*am->msg_handlers[id]) (the_msg);
759 /* do NOT free the message buffer... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760}
Dave Barach371e4e12016-07-08 09:38:52 -0400761
Dave Barachbfd92272017-05-12 11:59:25 -0400762u32
763vl_msg_api_get_msg_length (void *msg_arg)
764{
765 return vl_msg_api_get_msg_length_inline (msg_arg);
766}
767
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768/*
769 * vl_msg_api_socket_handler
770 */
Dave Barach371e4e12016-07-08 09:38:52 -0400771void
772vl_msg_api_socket_handler (void *the_msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773{
Dave Barach39d69112019-11-27 11:42:13 -0500774 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775
Dave Barach371e4e12016-07-08 09:38:52 -0400776 msg_handler_internal (am, the_msg,
777 (am->rx_trace
778 && am->rx_trace->enabled) /* trace_it */ ,
779 1 /* do_it */ , 0 /* free_it */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780}
781
782#define foreach_msg_api_vector \
783_(msg_names) \
784_(msg_handlers) \
785_(msg_cleanup_handlers) \
786_(msg_endian_handlers) \
787_(msg_print_handlers) \
788_(api_trace_cfg) \
789_(message_bounce) \
Ole Troane796a182020-05-18 11:14:05 +0200790_(is_mp_safe) \
791_(is_autoendian)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
Dave Barach371e4e12016-07-08 09:38:52 -0400793void
794vl_msg_api_config (vl_msg_api_msg_config_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795{
Dave Barach39d69112019-11-27 11:42:13 -0500796 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Dave Barach65457162017-10-10 17:53:14 -0400798 /*
799 * This happens during the java core tests if the message
800 * dictionary is missing newly added xxx_reply_t messages.
801 * Should never happen, but since I shot myself in the foot once
802 * this way, I thought I'd make it easy to debug if I ever do
803 * it again... (;-)...
804 */
805 if (c->id == 0)
806 {
807 if (c->name)
808 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
809 else
810 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
811 clib_warning ("Did you forget to call setup_message_id_table?");
812 return;
813 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814
815#define _(a) vec_validate (am->a, c->id);
Dave Barach371e4e12016-07-08 09:38:52 -0400816 foreach_msg_api_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817#undef _
818
Dave Barach392206e2017-09-20 08:40:16 -0400819 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
820 clib_warning
821 ("BUG: re-registering 'vl_api_%s_t_handler'."
822 "Handler was %llx, replaced by %llx",
823 c->name, am->msg_handlers[c->id], c->handler);
Dave Baracha1a093d2017-03-02 13:13:23 -0500824
Dave Barach371e4e12016-07-08 09:38:52 -0400825 am->msg_names[c->id] = c->name;
826 am->msg_handlers[c->id] = c->handler;
827 am->msg_cleanup_handlers[c->id] = c->cleanup;
828 am->msg_endian_handlers[c->id] = c->endian;
829 am->msg_print_handlers[c->id] = c->print;
830 am->message_bounce[c->id] = c->message_bounce;
831 am->is_mp_safe[c->id] = c->is_mp_safe;
Ole Troane796a182020-05-18 11:14:05 +0200832 am->is_autoendian[c->id] = c->is_autoendian;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
Dave Barach371e4e12016-07-08 09:38:52 -0400834 am->api_trace_cfg[c->id].size = c->size;
835 am->api_trace_cfg[c->id].trace_enable = c->traced;
836 am->api_trace_cfg[c->id].replay_enable = c->replay;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837}
838
Dave Barach371e4e12016-07-08 09:38:52 -0400839/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840 * vl_msg_api_set_handlers
841 * preserve the old API for a while
842 */
Dave Barach371e4e12016-07-08 09:38:52 -0400843void
844vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
845 void *endian, void *print, int size, int traced)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846{
Dave Barach371e4e12016-07-08 09:38:52 -0400847 vl_msg_api_msg_config_t cfg;
848 vl_msg_api_msg_config_t *c = &cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Dave Barachb7b92992018-10-17 10:38:51 -0400850 clib_memset (c, 0, sizeof (*c));
Dave Barach0691d6e2017-01-05 10:08:52 -0500851
Dave Barach371e4e12016-07-08 09:38:52 -0400852 c->id = id;
853 c->name = name;
854 c->handler = handler;
855 c->cleanup = cleanup;
856 c->endian = endian;
857 c->print = print;
Dave Barach371e4e12016-07-08 09:38:52 -0400858 c->traced = traced;
859 c->replay = 1;
860 c->message_bounce = 0;
861 c->is_mp_safe = 0;
Ole Troane796a182020-05-18 11:14:05 +0200862 c->is_autoendian = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400863 vl_msg_api_config (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864}
865
Dave Barach371e4e12016-07-08 09:38:52 -0400866void
Matej Perina75a17ec2017-09-21 17:03:27 +0200867vl_msg_api_clean_handlers (int msg_id)
868{
869 vl_msg_api_msg_config_t cfg;
870 vl_msg_api_msg_config_t *c = &cfg;
871
Dave Barachb7b92992018-10-17 10:38:51 -0400872 clib_memset (c, 0, sizeof (*c));
Matej Perina75a17ec2017-09-21 17:03:27 +0200873
874 c->id = msg_id;
875 vl_msg_api_config (c);
876}
877
878void
Dave Barach371e4e12016-07-08 09:38:52 -0400879vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880{
Dave Barach39d69112019-11-27 11:42:13 -0500881 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400882 ASSERT (msg_id > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Dave Barach371e4e12016-07-08 09:38:52 -0400884 vec_validate (am->msg_cleanup_handlers, msg_id);
885 am->msg_cleanup_handlers[msg_id] = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886}
887
Dave Barach371e4e12016-07-08 09:38:52 -0400888void
Florin Corase86a8ed2018-01-05 03:20:25 -0800889vl_msg_api_queue_handler (svm_queue_t * q)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890{
Dave Barach371e4e12016-07-08 09:38:52 -0400891 uword msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100893 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
Dave Barach371e4e12016-07-08 09:38:52 -0400894 vl_msg_api_handler ((void *) msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895}
896
Dave Barach77841402020-04-29 17:04:10 -0400897u32
898vl_msg_api_max_length (void *mp)
899{
900 msgbuf_t *mb;
901 u32 data_len = ~0;
902
903 /* Work out the maximum sane message length, and return it */
904 if (PREDICT_TRUE (mp != 0))
905 {
906 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
907 data_len = clib_net_to_host_u32 (mb->data_len);
908 }
909 return data_len;
910}
911
Dave Barach371e4e12016-07-08 09:38:52 -0400912vl_api_trace_t *
913vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914{
Dave Barach371e4e12016-07-08 09:38:52 -0400915 switch (which)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 {
917 case VL_API_TRACE_RX:
Dave Barach371e4e12016-07-08 09:38:52 -0400918 return am->rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 case VL_API_TRACE_TX:
Dave Barach371e4e12016-07-08 09:38:52 -0400920 return am->tx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921 default:
Dave Barach371e4e12016-07-08 09:38:52 -0400922 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 }
924}
925
Dave Barach371e4e12016-07-08 09:38:52 -0400926void
927vl_noop_handler (void *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929}
930
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931
932static u8 post_mortem_dump_enabled;
933
Dave Barach80f54e22017-03-08 19:08:56 -0500934void
935vl_msg_api_post_mortem_dump_enable_disable (int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936{
Dave Barach80f54e22017-03-08 19:08:56 -0500937 post_mortem_dump_enabled = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938}
939
Dave Barach371e4e12016-07-08 09:38:52 -0400940void
941vl_msg_api_post_mortem_dump (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942{
Dave Barach39d69112019-11-27 11:42:13 -0500943 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400944 FILE *fp;
945 char filename[64];
946 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Dave Barach371e4e12016-07-08 09:38:52 -0400948 if (post_mortem_dump_enabled == 0)
949 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
Dave Barach371e4e12016-07-08 09:38:52 -0400951 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
952 getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
Dave Barach371e4e12016-07-08 09:38:52 -0400954 fp = fopen (filename, "w");
955 if (fp == NULL)
956 {
957 rv = write (2, "Couldn't create ", 16);
958 rv = write (2, filename, strlen (filename));
959 rv = write (2, "\n", 1);
960 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 }
Dave Barach371e4e12016-07-08 09:38:52 -0400962 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
963 fclose (fp);
964 if (rv < 0)
965 {
966 rv = write (2, "Failed to save post-mortem API trace to ", 40);
967 rv = write (2, filename, strlen (filename));
968 rv = write (2, "\n", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969 }
Dave Barach371e4e12016-07-08 09:38:52 -0400970
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971}
972
973/* Layered message handling support */
974
Dave Barach371e4e12016-07-08 09:38:52 -0400975void
976vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977{
Dave Barach39d69112019-11-27 11:42:13 -0500978 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Dave Barach371e4e12016-07-08 09:38:52 -0400980 /* Mild idiot proofing */
981 if (msg_id_host_byte_order > 10000)
982 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
983 msg_id_host_byte_order,
984 clib_net_to_host_u16 (msg_id_host_byte_order));
985 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
986 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987}
988
Dave Barach371e4e12016-07-08 09:38:52 -0400989int
990vl_msg_api_pd_handler (void *mp, int rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991{
Dave Barach39d69112019-11-27 11:42:13 -0500992 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400993 int (*fp) (void *, int);
994 u16 msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995
Dave Barach371e4e12016-07-08 09:38:52 -0400996 if (clib_arch_is_little_endian)
997 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
998 else
999 msg_id = *((u16 *) mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Dave Barach371e4e12016-07-08 09:38:52 -04001001 if (msg_id >= vec_len (am->pd_msg_handlers)
1002 || am->pd_msg_handlers[msg_id] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 return rv;
Dave Barach371e4e12016-07-08 09:38:52 -04001004
1005 fp = am->pd_msg_handlers[msg_id];
1006 rv = (*fp) (mp, rv);
1007 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008}
1009
Dave Barach371e4e12016-07-08 09:38:52 -04001010void
1011vl_msg_api_set_first_available_msg_id (u16 first_avail)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012{
Dave Barach39d69112019-11-27 11:42:13 -05001013 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001014
1015 am->first_available_msg_id = first_avail;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016}
1017
Dave Barach371e4e12016-07-08 09:38:52 -04001018u16
Neale Rannse72be392017-04-26 13:59:20 -07001019vl_msg_api_get_msg_ids (const char *name, int n)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020{
Dave Barach39d69112019-11-27 11:42:13 -05001021 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -04001022 u8 *name_copy;
1023 vl_api_msg_range_t *rp;
1024 uword *p;
1025 u16 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026
Dave Barach371e4e12016-07-08 09:38:52 -04001027 if (am->msg_range_by_name == 0)
1028 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
Dave Barach371e4e12016-07-08 09:38:52 -04001030 name_copy = format (0, "%s%c", name, 0);
1031
1032 p = hash_get_mem (am->msg_range_by_name, name_copy);
1033 if (p)
1034 {
1035 clib_warning ("WARNING: duplicate message range registration for '%s'",
1036 name_copy);
1037 vec_free (name_copy);
1038 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039 }
1040
Dave Barach371e4e12016-07-08 09:38:52 -04001041 if (n < 0 || n > 1024)
1042 {
1043 clib_warning
1044 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
1045 n, name_copy);
1046 vec_free (name_copy);
1047 return ((u16) ~ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048 }
1049
Dave Barach371e4e12016-07-08 09:38:52 -04001050 vec_add2 (am->msg_ranges, rp, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
Dave Barach371e4e12016-07-08 09:38:52 -04001052 rv = rp->first_msg_id = am->first_available_msg_id;
1053 am->first_available_msg_id += n;
1054 rp->last_msg_id = am->first_available_msg_id - 1;
1055 rp->name = name_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
Dave Barach371e4e12016-07-08 09:38:52 -04001057 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Barach371e4e12016-07-08 09:38:52 -04001059 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060}
Dave Barach371e4e12016-07-08 09:38:52 -04001061
Dave Barach557d1282016-11-10 14:22:49 -05001062void
Neale Rannse72be392017-04-26 13:59:20 -07001063vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
Dave Barach557d1282016-11-10 14:22:49 -05001064{
1065 uword *p;
1066
1067 if (am->msg_index_by_name_and_crc == 0)
1068 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1069
1070 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1071 if (p)
1072 {
1073 clib_warning ("attempt to redefine '%s' ignored...", string);
1074 return;
1075 }
1076
1077 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1078}
1079
Dave Barach0d056e52017-09-28 15:11:16 -04001080void
1081vl_msg_api_add_version (api_main_t * am, const char *string,
1082 u32 major, u32 minor, u32 patch)
1083{
1084 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1085 ASSERT (strlen (string) < 64);
Ole Troan7504e992017-10-10 08:43:35 +02001086 strncpy (version.name, string, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -04001087 vec_add1 (am->api_version_list, version);
1088}
Dave Barach557d1282016-11-10 14:22:49 -05001089
Florin Corase86a8ed2018-01-05 03:20:25 -08001090u32
1091vl_msg_api_get_msg_index (u8 * name_and_crc)
1092{
Dave Barach39d69112019-11-27 11:42:13 -05001093 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -08001094 uword *p;
1095
1096 if (am->msg_index_by_name_and_crc)
1097 {
1098 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1099 if (p)
1100 return p[0];
1101 }
1102 return ~0;
1103}
1104
Ole Troan73710c72018-06-04 22:27:49 +02001105void *
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001106vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1107{
1108 pthread_mutex_lock (&vlib_rp->mutex);
1109 return svm_push_data_heap (vlib_rp);
1110}
1111
1112void *
Ole Troan73710c72018-06-04 22:27:49 +02001113vl_msg_push_heap (void)
1114{
Dave Barach39d69112019-11-27 11:42:13 -05001115 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001116 return vl_msg_push_heap_w_region (am->vlib_rp);
1117}
1118
1119void
1120vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1121{
1122 svm_pop_heap (oldheap);
1123 pthread_mutex_unlock (&vlib_rp->mutex);
Ole Troan73710c72018-06-04 22:27:49 +02001124}
1125
1126void
1127vl_msg_pop_heap (void *oldheap)
1128{
Dave Barach39d69112019-11-27 11:42:13 -05001129 api_main_t *am = vlibapi_get_main ();
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001130 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
Ole Troan73710c72018-06-04 22:27:49 +02001131}
1132
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001133/* Must be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001134int
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001135vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
Neale Ranns377860a2019-06-21 07:57:18 -07001136{
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001137 /* copy without nul terminator */
1138 u32 len = strlen (buf);
1139 if (len > 0)
Ole Troan33a58172019-09-04 09:12:29 +02001140 clib_memcpy_fast (str->buf, buf, len);
Neale Ranns377860a2019-06-21 07:57:18 -07001141 str->length = htonl (len);
1142 return len + sizeof (u32);
1143}
1144
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001145/* Must NOT be nul terminated */
Neale Ranns377860a2019-06-21 07:57:18 -07001146int
1147vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1148{
1149 u32 len = vec_len (vec);
1150 clib_memcpy (str->buf, vec, len);
1151 str->length = htonl (len);
1152 return len + sizeof (u32);
1153}
1154
Neale Ranns377860a2019-06-21 07:57:18 -07001155u32
1156vl_api_string_len (vl_api_string_t * astr)
1157{
Dave Barach9683c1e2019-07-01 09:42:41 -04001158 return clib_net_to_host_u32 (astr->length);
Neale Ranns377860a2019-06-21 07:57:18 -07001159}
1160
Ole Troan33a58172019-09-04 09:12:29 +02001161u8 *
1162vl_api_format_string (u8 * s, va_list * args)
1163{
1164 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1165 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1166 return s;
1167}
1168
Neale Ranns377860a2019-06-21 07:57:18 -07001169/*
1170 * Returns a new vector. Remember to free it after use.
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001171 * NOT nul terminated.
Neale Ranns377860a2019-06-21 07:57:18 -07001172 */
1173u8 *
Dave Barach77841402020-04-29 17:04:10 -04001174vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
Neale Ranns377860a2019-06-21 07:57:18 -07001175{
1176 u8 *v = 0;
Dave Barach77841402020-04-29 17:04:10 -04001177
1178 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1179 return format (0, "insane astr->length %u%c",
1180 clib_net_to_host_u32 (astr->length), 0);
Dave Barach9683c1e2019-07-01 09:42:41 -04001181 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
Neale Ranns377860a2019-06-21 07:57:18 -07001182 return v;
1183}
Ole Troan73710c72018-06-04 22:27:49 +02001184
Jakub Grajciar2dbee932020-02-07 11:30:26 +01001185/*
1186 * Returns a new vector. Remember to free it after use.
1187 * Nul terminated.
1188 */
1189char *
1190vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1191{
1192 char *v = 0;
1193 if (clib_net_to_host_u32 (astr->length) > 0)
1194 {
1195 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1196 vec_add1 (v, 0);
1197 }
1198 return v;
1199}
1200
Dave Barachb09f4d02019-07-15 16:00:03 -04001201void
1202vl_api_set_elog_main (elog_main_t * m)
1203{
Dave Barach39d69112019-11-27 11:42:13 -05001204 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001205 am->elog_main = m;
1206}
1207
1208int
1209vl_api_set_elog_trace_api_messages (int enable)
1210{
1211 int rv;
Dave Barach39d69112019-11-27 11:42:13 -05001212 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001213
1214 rv = am->elog_trace_api_messages;
1215 am->elog_trace_api_messages = enable;
1216 return rv;
1217}
1218
1219int
1220vl_api_get_elog_trace_api_messages (void)
1221{
Dave Barach39d69112019-11-27 11:42:13 -05001222 api_main_t *am = vlibapi_get_main ();
Dave Barachb09f4d02019-07-15 16:00:03 -04001223
1224 return am->elog_trace_api_messages;
1225}
1226
Dave Barach371e4e12016-07-08 09:38:52 -04001227/*
1228 * fd.io coding-style-patch-verification: ON
1229 *
1230 * Local Variables:
1231 * eval: (c-set-style "gnu")
1232 * End:
1233 */