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