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