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