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