Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * api_shared.c - API message handling, common code for both clients |
| 4 | * and the vlib process itself. |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 5 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 6 | * |
| 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 Barach | 072f8de | 2016-12-02 13:31:25 -0500 | [diff] [blame] | 24 | #include <stddef.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #include <string.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | #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 Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 33 | #include <vppinfra/callback.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 35 | /* *INDENT-OFF* */ |
BenoƮt Ganne | 7f9256e | 2022-03-04 17:17:04 +0100 | [diff] [blame] | 36 | static api_main_t api_global_main = { |
| 37 | .region_name = "/unset", |
| 38 | .api_uid = -1, |
| 39 | .api_gid = -1, |
| 40 | }; |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 41 | /* *INDENT-ON* */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 42 | |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 43 | /* Please use vlibapi_get_main() to access my_api_main */ |
| 44 | __thread api_main_t *my_api_main = &api_global_main; |
| 45 | |
| 46 | void |
| 47 | vl_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 Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 53 | void |
| 54 | vl_msg_api_increment_missing_client_counter (void) |
| 55 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 56 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 57 | am->missing_clients++; |
| 58 | } |
| 59 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 60 | int |
| 61 | vl_msg_api_rx_trace_enabled (api_main_t * am) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 63 | return (am->rx_trace && am->rx_trace->enabled); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 66 | int |
| 67 | vl_msg_api_tx_trace_enabled (api_main_t * am) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 69 | return (am->tx_trace && am->tx_trace->enabled); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * vl_msg_api_trace |
| 74 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 75 | void |
| 76 | vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 78 | u8 **this_trace; |
| 79 | u8 **old_trace; |
| 80 | u8 *msg_copy; |
Dave Barach | 072f8de | 2016-12-02 13:31:25 -0500 | [diff] [blame] | 81 | u32 length; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 82 | trace_cfg_t *cfgp; |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 83 | u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg)); |
Dave Barach | 072f8de | 2016-12-02 13:31:25 -0500 | [diff] [blame] | 84 | msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 86 | cfgp = am->api_trace_cfg + msg_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 88 | if (!cfgp || !cfgp->trace_enable) |
| 89 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 91 | msg_copy = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 93 | if (tp->nitems == 0) |
| 94 | { |
| 95 | clib_warning ("tp->nitems is 0"); |
| 96 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 99 | 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 Barach | f35a072 | 2019-06-12 16:50:38 -0400 | [diff] [blame] | 110 | /* Reuse the trace record, may save some memory allocator traffic */ |
| 111 | msg_copy = *old_trace; |
| 112 | vec_reset_length (msg_copy); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 113 | this_trace = old_trace; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Dave Barach | 072f8de | 2016-12-02 13:31:25 -0500 | [diff] [blame] | 116 | length = clib_net_to_host_u32 (header->data_len); |
| 117 | |
| 118 | vec_validate (msg_copy, length - 1); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 119 | clib_memcpy_fast (msg_copy, msg, length); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 120 | *this_trace = msg_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 123 | int |
| 124 | vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which, |
| 125 | int onoff) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 127 | vl_api_trace_t *tp; |
| 128 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 130 | switch (which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | { |
| 132 | case VL_API_TRACE_TX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 133 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | case VL_API_TRACE_RX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 142 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | default: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 151 | /* duh? */ |
| 152 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 155 | /* Configured? */ |
| 156 | if (tp == 0 || tp->nitems == 0) |
| 157 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 159 | rv = tp->enabled; |
| 160 | tp->enabled = onoff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 162 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 165 | int |
| 166 | vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 168 | vl_api_trace_t *tp; |
| 169 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 171 | switch (which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | { |
| 173 | case VL_API_TRACE_TX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 174 | tp = am->tx_trace; |
| 175 | break; |
| 176 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | case VL_API_TRACE_RX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 178 | tp = am->rx_trace; |
| 179 | break; |
| 180 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | default: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 182 | /* duh? */ |
| 183 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 186 | /* Configured? */ |
| 187 | if (!tp || tp->nitems == 0) |
| 188 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 190 | tp->curindex = 0; |
| 191 | tp->wrapped = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 193 | for (i = 0; i < vec_len (tp->traces); i++) |
| 194 | { |
| 195 | vec_free (tp->traces[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 197 | vec_free (tp->traces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 199 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Ole Troan | edfe2c0 | 2019-07-30 15:38:13 +0200 | [diff] [blame] | 202 | u8 * |
| 203 | vl_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 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 225 | static int |
| 226 | vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp) |
| 227 | { |
| 228 | u8 *tmpmem = 0; |
| 229 | int tlen, slen; |
| 230 | cJSON *(*tojson_fn) (void *); |
| 231 | |
| 232 | u32 msg_length = vec_len (msg); |
| 233 | vec_validate (tmpmem, msg_length - 1); |
| 234 | clib_memcpy_fast (tmpmem, msg, msg_length); |
| 235 | u16 id = clib_net_to_host_u16 (*((u16 *) msg)); |
| 236 | |
| 237 | void (*endian_fp) (void *); |
| 238 | endian_fp = am->msg_endian_handlers[id]; |
| 239 | (*endian_fp) (tmpmem); |
| 240 | |
| 241 | if (id < vec_len (am->msg_tojson_handlers) && am->msg_tojson_handlers[id]) |
| 242 | { |
| 243 | tojson_fn = am->msg_tojson_handlers[id]; |
| 244 | cJSON *o = tojson_fn (tmpmem); |
| 245 | char *s = cJSON_Print (o); |
| 246 | slen = strlen (s); |
| 247 | tlen = fwrite (s, 1, slen, fp); |
| 248 | cJSON_free (s); |
| 249 | cJSON_Delete (o); |
| 250 | vec_free (tmpmem); |
| 251 | if (tlen != slen) |
| 252 | { |
| 253 | fformat (stderr, "writing to file error\n"); |
| 254 | return -11; |
| 255 | } |
| 256 | } |
| 257 | else |
| 258 | fformat (stderr, " [no registered tojson fn]\n"); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f) |
| 264 | |
| 265 | typedef struct |
| 266 | { |
| 267 | FILE *fp; |
| 268 | u32 n_traces; |
| 269 | u32 i; |
| 270 | } vl_msg_write_json_args_t; |
| 271 | |
| 272 | static int |
| 273 | vl_msg_write_json_fn (u8 *msg, void *ctx) |
| 274 | { |
| 275 | vl_msg_write_json_args_t *arg = ctx; |
| 276 | FILE *fp = arg->fp; |
| 277 | api_main_t *am = vlibapi_get_main (); |
| 278 | int rc = vl_msg_api_trace_write_one (am, msg, fp); |
| 279 | if (rc < 0) |
| 280 | return rc; |
| 281 | |
| 282 | if (arg->i < arg->n_traces - 1) |
| 283 | vl_msg_fwrite (",\n", fp); |
| 284 | arg->i++; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp) |
| 290 | { |
| 291 | vl_msg_write_json_args_t args; |
| 292 | clib_memset (&args, 0, sizeof (args)); |
| 293 | args.fp = fp; |
| 294 | args.n_traces = vec_len (tp->traces); |
| 295 | vl_msg_fwrite ("[\n", fp); |
| 296 | |
| 297 | int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args); |
| 298 | if (rv < 0) |
| 299 | return rv; |
| 300 | |
| 301 | vl_msg_fwrite ("]", fp); |
| 302 | return 0; |
| 303 | } |
| 304 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 305 | int |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 306 | vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn, |
| 307 | void *ctx) |
| 308 | { |
| 309 | int i; |
| 310 | u8 *msg; |
| 311 | int rv = 0; |
| 312 | |
| 313 | /* No-wrap case */ |
| 314 | if (tp->wrapped == 0) |
| 315 | { |
| 316 | for (i = 0; i < vec_len (tp->traces); i++) |
| 317 | { |
| 318 | /*sa_ignore NO_NULL_CHK */ |
| 319 | msg = tp->traces[i]; |
| 320 | if (!msg) |
| 321 | continue; |
| 322 | |
| 323 | rv = fn (msg, ctx); |
| 324 | if (rv < 0) |
| 325 | return rv; |
| 326 | } |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | /* Wrap case: write oldest -> end of buffer */ |
| 331 | for (i = tp->curindex; i < vec_len (tp->traces); i++) |
| 332 | { |
| 333 | msg = tp->traces[i]; |
| 334 | if (!msg) |
| 335 | continue; |
| 336 | |
| 337 | rv = fn (msg, ctx); |
| 338 | if (rv < 0) |
| 339 | return rv; |
| 340 | } |
| 341 | /* write beginning of buffer -> oldest-1 */ |
| 342 | for (i = 0; i < tp->curindex; i++) |
| 343 | { |
| 344 | /*sa_ignore NO_NULL_CHK */ |
| 345 | msg = tp->traces[i]; |
| 346 | if (!msg) |
| 347 | continue; |
| 348 | |
| 349 | rv = fn (msg, ctx); |
| 350 | if (rv < 0) |
| 351 | return rv; |
| 352 | } |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int |
| 358 | vl_api_msg_write_fn (u8 *msg, void *ctx) |
| 359 | { |
| 360 | FILE *fp = ctx; |
| 361 | u32 msg_length = clib_host_to_net_u32 (vec_len (msg)); |
| 362 | if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) |
| 363 | { |
| 364 | return (-14); |
| 365 | } |
| 366 | if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) |
| 367 | { |
| 368 | return (-14); |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | int |
| 374 | vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp, |
| 375 | u8 is_json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 377 | vl_api_trace_t *tp; |
| 378 | vl_api_trace_file_header_t fh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 380 | switch (which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | { |
| 382 | case VL_API_TRACE_TX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 383 | tp = am->tx_trace; |
| 384 | break; |
| 385 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | case VL_API_TRACE_RX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 387 | tp = am->rx_trace; |
| 388 | break; |
| 389 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | default: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 391 | /* duh? */ |
| 392 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 395 | /* Configured, data present? */ |
| 396 | if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0) |
| 397 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 399 | /* "Dare to be stupid" check */ |
| 400 | if (fp == 0) |
| 401 | { |
| 402 | return -2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 405 | if (is_json) |
| 406 | return vl_msg_api_trace_write_json (am, tp, fp); |
| 407 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 408 | /* Write the file header */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 409 | fh.wrapped = tp->wrapped; |
Ole Troan | edfe2c0 | 2019-07-30 15:38:13 +0200 | [diff] [blame] | 410 | fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces)); |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 411 | |
Ole Troan | edfe2c0 | 2019-07-30 15:38:13 +0200 | [diff] [blame] | 412 | u8 *m = vl_api_serialize_message_table (am, 0); |
Ole Troan | edfe2c0 | 2019-07-30 15:38:13 +0200 | [diff] [blame] | 413 | fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 415 | if (fwrite (&fh, sizeof (fh), 1, fp) != 1) |
| 416 | { |
| 417 | return (-10); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Ole Troan | edfe2c0 | 2019-07-30 15:38:13 +0200 | [diff] [blame] | 420 | /* Write the message table */ |
| 421 | if (fwrite (m, vec_len (m), 1, fp) != 1) |
| 422 | { |
| 423 | return (-14); |
| 424 | } |
| 425 | vec_free (m); |
| 426 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 427 | return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 430 | int |
| 431 | vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which, |
| 432 | u32 nitems) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 434 | vl_api_trace_t *tp; |
| 435 | int was_on = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 437 | switch (which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | { |
| 439 | case VL_API_TRACE_TX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 440 | tp = am->tx_trace; |
| 441 | if (tp == 0) |
| 442 | { |
| 443 | vec_validate (am->tx_trace, 0); |
| 444 | tp = am->tx_trace; |
| 445 | } |
| 446 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 448 | case VL_API_TRACE_RX: |
| 449 | tp = am->rx_trace; |
| 450 | if (tp == 0) |
| 451 | { |
| 452 | vec_validate (am->rx_trace, 0); |
| 453 | tp = am->rx_trace; |
| 454 | } |
| 455 | |
| 456 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
| 458 | default: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 459 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | |
| 461 | } |
| 462 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 463 | if (tp->enabled) |
| 464 | { |
| 465 | was_on = vl_msg_api_trace_onoff (am, which, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 467 | if (tp->traces) |
| 468 | { |
| 469 | vl_msg_api_trace_free (am, which); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 472 | clib_memset (tp, 0, sizeof (*tp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 474 | if (clib_arch_is_big_endian) |
| 475 | { |
| 476 | tp->endian = VL_API_BIG_ENDIAN; |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | tp->endian = VL_API_LITTLE_ENDIAN; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 483 | tp->nitems = nitems; |
| 484 | if (was_on) |
| 485 | { |
| 486 | (void) vl_msg_api_trace_onoff (am, which, was_on); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 488 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 491 | void |
| 492 | vl_msg_api_barrier_sync (void) |
| 493 | { |
| 494 | } |
| 495 | |
| 496 | void |
| 497 | vl_msg_api_barrier_release (void) |
| 498 | { |
| 499 | } |
| 500 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 501 | always_inline void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 502 | msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len, |
| 503 | int trace_it, int do_it, int free_it) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | { |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 505 | u16 id = clib_net_to_host_u16 (*((u16 *) the_msg)); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 506 | u8 *(*print_fp) (void *, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 508 | if (PREDICT_FALSE (am->elog_trace_api_messages)) |
| 509 | { |
| 510 | /* *INDENT-OFF* */ |
| 511 | ELOG_TYPE_DECLARE (e) = |
| 512 | { |
| 513 | .format = "api-msg: %s", |
| 514 | .format_args = "T4", |
| 515 | }; |
| 516 | /* *INDENT-ON* */ |
| 517 | struct |
| 518 | { |
| 519 | u32 c; |
| 520 | } *ed; |
| 521 | ed = ELOG_DATA (am->elog_main, e); |
Andrew Yourtchenko | f48de88 | 2021-02-06 12:28:52 +0000 | [diff] [blame] | 522 | if (id < vec_len (am->msg_names) && am->msg_names[id]) |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 523 | ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]); |
| 524 | else |
| 525 | ed->c = elog_string (am->elog_main, "BOGUS"); |
| 526 | } |
| 527 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 528 | if (id < vec_len (am->msg_handlers) && am->msg_handlers[id]) |
| 529 | { |
| 530 | if (trace_it) |
| 531 | vl_msg_api_trace (am, am->rx_trace, the_msg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 533 | if (am->msg_print_flag) |
| 534 | { |
| 535 | fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]); |
| 536 | print_fp = (void *) am->msg_print_handlers[id]; |
| 537 | if (print_fp == 0) |
| 538 | { |
| 539 | fformat (stdout, " [no registered print fn]\n"); |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | (*print_fp) (the_msg, stdout); |
| 544 | } |
| 545 | } |
| 546 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 547 | uword calc_size = 0; |
| 548 | uword (*calc_size_fp) (void *); |
| 549 | calc_size_fp = am->msg_calc_size_funcs[id]; |
| 550 | ASSERT (NULL != calc_size_fp); |
| 551 | if (calc_size_fp) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 552 | { |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 553 | calc_size = (*calc_size_fp) (the_msg); |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 554 | if (calc_size > msg_len) |
| 555 | { |
| 556 | clib_warning ( |
| 557 | "Truncated message '%s' (id %u) received, calculated size " |
| 558 | "%lu is bigger than actual size %llu, message dropped.", |
| 559 | am->msg_names[id], id, calc_size, msg_len); |
| 560 | } |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot " |
| 565 | "verify message size is correct", |
| 566 | am->msg_names[id], id); |
| 567 | } |
| 568 | |
| 569 | /* don't process message if it's truncated, otherwise byte swaps |
| 570 | * and stuff could corrupt memory even beyond message if it's malicious |
| 571 | * e.g. VLA length field set to 1M elements, but VLA empty */ |
| 572 | if (do_it && calc_size <= msg_len) |
| 573 | { |
| 574 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 575 | if (!am->is_mp_safe[id]) |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 576 | { |
| 577 | vl_msg_api_barrier_trace_context (am->msg_names[id]); |
| 578 | vl_msg_api_barrier_sync (); |
| 579 | } |
Ole Troan | e796a18 | 2020-05-18 11:14:05 +0200 | [diff] [blame] | 580 | |
| 581 | if (am->is_autoendian[id]) |
| 582 | { |
| 583 | void (*endian_fp) (void *); |
| 584 | endian_fp = am->msg_endian_handlers[id]; |
| 585 | (*endian_fp) (the_msg); |
| 586 | } |
| 587 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 588 | if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0)) |
| 589 | clib_call_callbacks (am->perf_counter_cbs, am, id, |
| 590 | 0 /* before */ ); |
| 591 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 592 | (*am->msg_handlers[id]) (the_msg); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 593 | |
| 594 | if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0)) |
| 595 | clib_call_callbacks (am->perf_counter_cbs, am, id, |
| 596 | 1 /* after */ ); |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 597 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 598 | if (!am->is_mp_safe[id]) |
| 599 | vl_msg_api_barrier_release (); |
| 600 | } |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | clib_warning ("no handler for msg id %d", id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 607 | if (free_it) |
| 608 | vl_msg_api_free (the_msg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 610 | if (PREDICT_FALSE (am->elog_trace_api_messages)) |
| 611 | { |
| 612 | /* *INDENT-OFF* */ |
| 613 | ELOG_TYPE_DECLARE (e) = |
| 614 | { |
| 615 | .format = "api-msg-done(%s): %s", |
| 616 | .format_args = "t4T4", |
| 617 | .n_enum_strings = 2, |
| 618 | .enum_strings = |
| 619 | { |
| 620 | "barrier", |
| 621 | "mp-safe", |
| 622 | } |
| 623 | }; |
| 624 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 626 | struct |
| 627 | { |
| 628 | u32 barrier; |
| 629 | u32 c; |
| 630 | } *ed; |
| 631 | ed = ELOG_DATA (am->elog_main, e); |
Andrew Yourtchenko | f48de88 | 2021-02-06 12:28:52 +0000 | [diff] [blame] | 632 | if (id < vec_len (am->msg_names) && am->msg_names[id]) |
Dave Barach | 6794787 | 2019-07-19 09:31:29 -0400 | [diff] [blame] | 633 | { |
| 634 | ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]); |
| 635 | ed->barrier = !am->is_mp_safe[id]; |
| 636 | } |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 637 | else |
Dave Barach | 6794787 | 2019-07-19 09:31:29 -0400 | [diff] [blame] | 638 | { |
| 639 | ed->c = elog_string (am->elog_main, "BOGUS"); |
| 640 | ed->barrier = 0; |
| 641 | } |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 642 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 645 | void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 646 | vl_msg_api_handler (void *the_msg, uword msg_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 647 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 648 | api_main_t *am = vlibapi_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 650 | msg_handler_internal (am, the_msg, msg_len, |
| 651 | (am->rx_trace && am->rx_trace->enabled) /* trace_it */, |
| 652 | 1 /* do_it */, 1 /* free_it */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 655 | void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 656 | vl_msg_api_handler_no_free (void *the_msg, uword msg_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 658 | api_main_t *am = vlibapi_get_main (); |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 659 | msg_handler_internal (am, the_msg, msg_len, |
| 660 | (am->rx_trace && am->rx_trace->enabled) /* trace_it */, |
| 661 | 1 /* do_it */, 0 /* free_it */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 664 | void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 665 | vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 667 | api_main_t *am = vlibapi_get_main (); |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 668 | msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */, |
| 669 | 0 /* free_it */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Add a trace record to the API message trace buffer, if |
| 674 | * API message tracing is enabled. Handy for adding sufficient |
| 675 | * data to the trace to reproduce autonomous state, as opposed to |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 676 | * state downloaded via control-plane API messages. Example: the NAT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | * application creates database entries based on packet traffic, not |
| 678 | * control-plane messages. |
| 679 | * |
| 680 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 681 | void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 682 | vl_msg_api_trace_only (void *the_msg, uword msg_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 684 | api_main_t *am = vlibapi_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 686 | msg_handler_internal (am, the_msg, msg_len, |
| 687 | (am->rx_trace && am->rx_trace->enabled) /* trace_it */, |
| 688 | 0 /* do_it */, 0 /* free_it */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 691 | void |
| 692 | vl_msg_api_cleanup_handler (void *the_msg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 694 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 695 | u16 id = clib_net_to_host_u16 (*((u16 *) the_msg)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 697 | if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers))) |
| 698 | { |
| 699 | clib_warning ("_vl_msg_id too large: %d\n", id); |
| 700 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 702 | if (am->msg_cleanup_handlers[id]) |
| 703 | (*am->msg_cleanup_handlers[id]) (the_msg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 705 | vl_msg_api_free (the_msg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
| 709 | * vl_msg_api_replay_handler |
| 710 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 711 | void |
| 712 | vl_msg_api_replay_handler (void *the_msg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 713 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 714 | api_main_t *am = vlibapi_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 716 | u16 id = clib_net_to_host_u16 (*((u16 *) the_msg)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 718 | if (PREDICT_FALSE (id >= vec_len (am->msg_handlers))) |
| 719 | { |
| 720 | clib_warning ("_vl_msg_id too large: %d\n", id); |
| 721 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 722 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 723 | /* do NOT trace the message... */ |
| 724 | if (am->msg_handlers[id]) |
| 725 | (*am->msg_handlers[id]) (the_msg); |
| 726 | /* do NOT free the message buffer... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 728 | |
Dave Barach | bfd9227 | 2017-05-12 11:59:25 -0400 | [diff] [blame] | 729 | u32 |
| 730 | vl_msg_api_get_msg_length (void *msg_arg) |
| 731 | { |
| 732 | return vl_msg_api_get_msg_length_inline (msg_arg); |
| 733 | } |
| 734 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 735 | /* |
| 736 | * vl_msg_api_socket_handler |
| 737 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 738 | void |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 739 | vl_msg_api_socket_handler (void *the_msg, uword msg_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 741 | api_main_t *am = vlibapi_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 743 | msg_handler_internal (am, the_msg, msg_len, |
| 744 | (am->rx_trace && am->rx_trace->enabled) /* trace_it */, |
| 745 | 1 /* do_it */, 0 /* free_it */); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 748 | #define foreach_msg_api_vector \ |
| 749 | _ (msg_names) \ |
| 750 | _ (msg_handlers) \ |
| 751 | _ (msg_cleanup_handlers) \ |
| 752 | _ (msg_endian_handlers) \ |
| 753 | _ (msg_print_handlers) \ |
| 754 | _ (msg_print_json_handlers) \ |
| 755 | _ (msg_tojson_handlers) \ |
| 756 | _ (msg_fromjson_handlers) \ |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 757 | _ (msg_calc_size_funcs) \ |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 758 | _ (api_trace_cfg) \ |
| 759 | _ (message_bounce) \ |
| 760 | _ (is_mp_safe) \ |
| 761 | _ (is_autoendian) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 763 | void |
| 764 | vl_msg_api_config (vl_msg_api_msg_config_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 765 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 766 | api_main_t *am = vlibapi_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 768 | /* |
| 769 | * This happens during the java core tests if the message |
| 770 | * dictionary is missing newly added xxx_reply_t messages. |
| 771 | * Should never happen, but since I shot myself in the foot once |
| 772 | * this way, I thought I'd make it easy to debug if I ever do |
| 773 | * it again... (;-)... |
| 774 | */ |
| 775 | if (c->id == 0) |
| 776 | { |
| 777 | if (c->name) |
| 778 | clib_warning ("Trying to register %s with a NULL msg id!", c->name); |
| 779 | else |
| 780 | clib_warning ("Trying to register a NULL msg with a NULL msg id!"); |
| 781 | clib_warning ("Did you forget to call setup_message_id_table?"); |
| 782 | return; |
| 783 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | |
| 785 | #define _(a) vec_validate (am->a, c->id); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 786 | foreach_msg_api_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | #undef _ |
| 788 | |
Dave Barach | 392206e | 2017-09-20 08:40:16 -0400 | [diff] [blame] | 789 | if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler) |
| 790 | clib_warning |
| 791 | ("BUG: re-registering 'vl_api_%s_t_handler'." |
| 792 | "Handler was %llx, replaced by %llx", |
| 793 | c->name, am->msg_handlers[c->id], c->handler); |
Dave Barach | a1a093d | 2017-03-02 13:13:23 -0500 | [diff] [blame] | 794 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 795 | am->msg_names[c->id] = c->name; |
| 796 | am->msg_handlers[c->id] = c->handler; |
| 797 | am->msg_cleanup_handlers[c->id] = c->cleanup; |
| 798 | am->msg_endian_handlers[c->id] = c->endian; |
| 799 | am->msg_print_handlers[c->id] = c->print; |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 800 | am->msg_print_json_handlers[c->id] = c->print_json; |
| 801 | am->msg_tojson_handlers[c->id] = c->tojson; |
| 802 | am->msg_fromjson_handlers[c->id] = c->fromjson; |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 803 | am->msg_calc_size_funcs[c->id] = c->calc_size; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 804 | am->message_bounce[c->id] = c->message_bounce; |
| 805 | am->is_mp_safe[c->id] = c->is_mp_safe; |
Ole Troan | e796a18 | 2020-05-18 11:14:05 +0200 | [diff] [blame] | 806 | am->is_autoendian[c->id] = c->is_autoendian; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 808 | am->api_trace_cfg[c->id].size = c->size; |
| 809 | am->api_trace_cfg[c->id].trace_enable = c->traced; |
| 810 | am->api_trace_cfg[c->id].replay_enable = c->replay; |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 811 | |
| 812 | if (!am->msg_id_by_name) |
| 813 | am->msg_id_by_name = hash_create_string (0, sizeof (uword)); |
| 814 | |
| 815 | hash_set_mem (am->msg_id_by_name, c->name, c->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 818 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 819 | * vl_msg_api_set_handlers |
| 820 | * preserve the old API for a while |
| 821 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 822 | void |
| 823 | vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup, |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 824 | void *endian, void *print, int size, int traced, |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 825 | void *print_json, void *tojson, void *fromjson, |
| 826 | void *calc_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 828 | vl_msg_api_msg_config_t cfg; |
| 829 | vl_msg_api_msg_config_t *c = &cfg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 831 | clib_memset (c, 0, sizeof (*c)); |
Dave Barach | 0691d6e | 2017-01-05 10:08:52 -0500 | [diff] [blame] | 832 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 833 | c->id = id; |
| 834 | c->name = name; |
| 835 | c->handler = handler; |
| 836 | c->cleanup = cleanup; |
| 837 | c->endian = endian; |
| 838 | c->print = print; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 839 | c->traced = traced; |
| 840 | c->replay = 1; |
| 841 | c->message_bounce = 0; |
| 842 | c->is_mp_safe = 0; |
Ole Troan | e796a18 | 2020-05-18 11:14:05 +0200 | [diff] [blame] | 843 | c->is_autoendian = 0; |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 844 | c->tojson = tojson; |
| 845 | c->fromjson = fromjson; |
| 846 | c->print_json = print_json; |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 847 | c->calc_size = calc_size; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 848 | vl_msg_api_config (c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 851 | void |
Matej Perina | 75a17ec | 2017-09-21 17:03:27 +0200 | [diff] [blame] | 852 | vl_msg_api_clean_handlers (int msg_id) |
| 853 | { |
| 854 | vl_msg_api_msg_config_t cfg; |
| 855 | vl_msg_api_msg_config_t *c = &cfg; |
| 856 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 857 | clib_memset (c, 0, sizeof (*c)); |
Matej Perina | 75a17ec | 2017-09-21 17:03:27 +0200 | [diff] [blame] | 858 | |
| 859 | c->id = msg_id; |
| 860 | vl_msg_api_config (c); |
| 861 | } |
| 862 | |
| 863 | void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 864 | vl_msg_api_set_cleanup_handler (int msg_id, void *fp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 865 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 866 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 867 | ASSERT (msg_id > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 869 | vec_validate (am->msg_cleanup_handlers, msg_id); |
| 870 | am->msg_cleanup_handlers[msg_id] = fp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 873 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 874 | vl_msg_api_queue_handler (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 875 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 876 | uword msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 877 | |
Klement Sekera | 9b7e8ac | 2021-11-22 21:26:20 +0100 | [diff] [blame] | 878 | while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0)) |
| 879 | { |
| 880 | msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data)); |
| 881 | vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len)); |
| 882 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 885 | u32 |
| 886 | vl_msg_api_max_length (void *mp) |
| 887 | { |
| 888 | msgbuf_t *mb; |
| 889 | u32 data_len = ~0; |
| 890 | |
| 891 | /* Work out the maximum sane message length, and return it */ |
| 892 | if (PREDICT_TRUE (mp != 0)) |
| 893 | { |
| 894 | mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data)); |
| 895 | data_len = clib_net_to_host_u32 (mb->data_len); |
| 896 | } |
| 897 | return data_len; |
| 898 | } |
| 899 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 900 | vl_api_trace_t * |
| 901 | vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 902 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 903 | switch (which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | { |
| 905 | case VL_API_TRACE_RX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 906 | return am->rx_trace; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 907 | case VL_API_TRACE_TX: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 908 | return am->tx_trace; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 909 | default: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 910 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 914 | void |
| 915 | vl_noop_handler (void *mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 919 | |
| 920 | static u8 post_mortem_dump_enabled; |
| 921 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 922 | void |
| 923 | vl_msg_api_post_mortem_dump_enable_disable (int enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | { |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 925 | post_mortem_dump_enabled = enable; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 928 | void |
| 929 | vl_msg_api_post_mortem_dump (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 930 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 931 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 932 | FILE *fp; |
| 933 | char filename[64]; |
| 934 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 935 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 936 | if (post_mortem_dump_enabled == 0) |
| 937 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 938 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 939 | snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d", |
| 940 | getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 941 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 942 | fp = fopen (filename, "w"); |
| 943 | if (fp == NULL) |
| 944 | { |
| 945 | rv = write (2, "Couldn't create ", 16); |
| 946 | rv = write (2, filename, strlen (filename)); |
| 947 | rv = write (2, "\n", 1); |
| 948 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 949 | } |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 950 | rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 951 | fclose (fp); |
| 952 | if (rv < 0) |
| 953 | { |
| 954 | rv = write (2, "Failed to save post-mortem API trace to ", 40); |
| 955 | rv = write (2, filename, strlen (filename)); |
| 956 | rv = write (2, "\n", 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 958 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* Layered message handling support */ |
| 962 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 963 | void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 964 | vl_msg_api_set_first_available_msg_id (u16 first_avail) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 965 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 966 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 967 | |
| 968 | am->first_available_msg_id = first_avail; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 971 | u16 |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 972 | vl_msg_api_get_msg_ids (const char *name, int n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 974 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 975 | u8 *name_copy; |
| 976 | vl_api_msg_range_t *rp; |
| 977 | uword *p; |
| 978 | u16 rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 979 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 980 | if (am->msg_range_by_name == 0) |
| 981 | am->msg_range_by_name = hash_create_string (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 982 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 983 | name_copy = format (0, "%s%c", name, 0); |
| 984 | |
| 985 | p = hash_get_mem (am->msg_range_by_name, name_copy); |
| 986 | if (p) |
| 987 | { |
| 988 | clib_warning ("WARNING: duplicate message range registration for '%s'", |
| 989 | name_copy); |
| 990 | vec_free (name_copy); |
| 991 | return ((u16) ~ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 994 | if (n < 0 || n > 1024) |
| 995 | { |
| 996 | clib_warning |
| 997 | ("WARNING: bad number of message-IDs (%d) requested by '%s'", |
| 998 | n, name_copy); |
| 999 | vec_free (name_copy); |
| 1000 | return ((u16) ~ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1003 | vec_add2 (am->msg_ranges, rp, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1004 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1005 | rv = rp->first_msg_id = am->first_available_msg_id; |
| 1006 | am->first_available_msg_id += n; |
| 1007 | rp->last_msg_id = am->first_available_msg_id - 1; |
| 1008 | rp->name = name_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1009 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1010 | hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1012 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1014 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 1015 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 1016 | vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id) |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 1017 | { |
| 1018 | uword *p; |
| 1019 | |
| 1020 | if (am->msg_index_by_name_and_crc == 0) |
| 1021 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 1022 | |
| 1023 | p = hash_get_mem (am->msg_index_by_name_and_crc, string); |
| 1024 | if (p) |
| 1025 | { |
| 1026 | clib_warning ("attempt to redefine '%s' ignored...", string); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | hash_set_mem (am->msg_index_by_name_and_crc, string, id); |
| 1031 | } |
| 1032 | |
Dave Barach | 0d056e5 | 2017-09-28 15:11:16 -0400 | [diff] [blame] | 1033 | void |
| 1034 | vl_msg_api_add_version (api_main_t * am, const char *string, |
| 1035 | u32 major, u32 minor, u32 patch) |
| 1036 | { |
| 1037 | api_version_t version = {.major = major,.minor = minor,.patch = patch }; |
| 1038 | ASSERT (strlen (string) < 64); |
Ole Troan | 7504e99 | 2017-10-10 08:43:35 +0200 | [diff] [blame] | 1039 | strncpy (version.name, string, 64 - 1); |
Dave Barach | 0d056e5 | 2017-09-28 15:11:16 -0400 | [diff] [blame] | 1040 | vec_add1 (am->api_version_list, version); |
| 1041 | } |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 1042 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1043 | u32 |
| 1044 | vl_msg_api_get_msg_index (u8 * name_and_crc) |
| 1045 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1046 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1047 | uword *p; |
| 1048 | |
| 1049 | if (am->msg_index_by_name_and_crc) |
| 1050 | { |
| 1051 | p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc); |
| 1052 | if (p) |
| 1053 | return p[0]; |
| 1054 | } |
| 1055 | return ~0; |
| 1056 | } |
| 1057 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 1058 | void * |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 1059 | vl_msg_push_heap_w_region (svm_region_t * vlib_rp) |
| 1060 | { |
| 1061 | pthread_mutex_lock (&vlib_rp->mutex); |
| 1062 | return svm_push_data_heap (vlib_rp); |
| 1063 | } |
| 1064 | |
| 1065 | void * |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 1066 | vl_msg_push_heap (void) |
| 1067 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1068 | api_main_t *am = vlibapi_get_main (); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 1069 | return vl_msg_push_heap_w_region (am->vlib_rp); |
| 1070 | } |
| 1071 | |
| 1072 | void |
| 1073 | vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap) |
| 1074 | { |
| 1075 | svm_pop_heap (oldheap); |
| 1076 | pthread_mutex_unlock (&vlib_rp->mutex); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | void |
| 1080 | vl_msg_pop_heap (void *oldheap) |
| 1081 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1082 | api_main_t *am = vlibapi_get_main (); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 1083 | vl_msg_pop_heap_w_region (am->vlib_rp, oldheap); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1086 | /* Must be nul terminated */ |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1087 | int |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1088 | vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str) |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1089 | { |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1090 | /* copy without nul terminator */ |
| 1091 | u32 len = strlen (buf); |
| 1092 | if (len > 0) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 1093 | clib_memcpy_fast (str->buf, buf, len); |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1094 | str->length = htonl (len); |
| 1095 | return len + sizeof (u32); |
| 1096 | } |
| 1097 | |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1098 | /* Must NOT be nul terminated */ |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1099 | int |
| 1100 | vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str) |
| 1101 | { |
| 1102 | u32 len = vec_len (vec); |
| 1103 | clib_memcpy (str->buf, vec, len); |
| 1104 | str->length = htonl (len); |
| 1105 | return len + sizeof (u32); |
| 1106 | } |
| 1107 | |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1108 | u32 |
| 1109 | vl_api_string_len (vl_api_string_t * astr) |
| 1110 | { |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 1111 | return clib_net_to_host_u32 (astr->length); |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 1114 | u8 * |
| 1115 | vl_api_format_string (u8 * s, va_list * args) |
| 1116 | { |
| 1117 | vl_api_string_t *a = va_arg (*args, vl_api_string_t *); |
| 1118 | vec_add (s, a->buf, clib_net_to_host_u32 (a->length)); |
| 1119 | return s; |
| 1120 | } |
| 1121 | |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Returns a new vector. Remember to free it after use. |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1124 | * NOT nul terminated. |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1125 | */ |
| 1126 | u8 * |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 1127 | vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr) |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1128 | { |
| 1129 | u8 *v = 0; |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 1130 | |
| 1131 | if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length)) |
| 1132 | return format (0, "insane astr->length %u%c", |
| 1133 | clib_net_to_host_u32 (astr->length), 0); |
Dave Barach | 9683c1e | 2019-07-01 09:42:41 -0400 | [diff] [blame] | 1134 | vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length)); |
Neale Ranns | 377860a | 2019-06-21 07:57:18 -0700 | [diff] [blame] | 1135 | return v; |
| 1136 | } |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 1137 | |
Jakub Grajciar | 2dbee93 | 2020-02-07 11:30:26 +0100 | [diff] [blame] | 1138 | /* |
| 1139 | * Returns a new vector. Remember to free it after use. |
| 1140 | * Nul terminated. |
| 1141 | */ |
| 1142 | char * |
| 1143 | vl_api_from_api_to_new_c_string (vl_api_string_t * astr) |
| 1144 | { |
| 1145 | char *v = 0; |
| 1146 | if (clib_net_to_host_u32 (astr->length) > 0) |
| 1147 | { |
| 1148 | vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length)); |
| 1149 | vec_add1 (v, 0); |
| 1150 | } |
| 1151 | return v; |
| 1152 | } |
| 1153 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1154 | void |
| 1155 | vl_api_set_elog_main (elog_main_t * m) |
| 1156 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1157 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1158 | am->elog_main = m; |
| 1159 | } |
| 1160 | |
| 1161 | int |
| 1162 | vl_api_set_elog_trace_api_messages (int enable) |
| 1163 | { |
| 1164 | int rv; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1165 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1166 | |
| 1167 | rv = am->elog_trace_api_messages; |
| 1168 | am->elog_trace_api_messages = enable; |
| 1169 | return rv; |
| 1170 | } |
| 1171 | |
| 1172 | int |
| 1173 | vl_api_get_elog_trace_api_messages (void) |
| 1174 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1175 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1176 | |
| 1177 | return am->elog_trace_api_messages; |
| 1178 | } |
| 1179 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1180 | /* |
| 1181 | * fd.io coding-style-patch-verification: ON |
| 1182 | * |
| 1183 | * Local Variables: |
| 1184 | * eval: (c-set-style "gnu") |
| 1185 | * End: |
| 1186 | */ |