Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vlib_api.c VLIB API implementation |
| 4 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <pthread.h> |
| 22 | #include <vppinfra/vec.h> |
| 23 | #include <vppinfra/hash.h> |
| 24 | #include <vppinfra/pool.h> |
| 25 | #include <vppinfra/format.h> |
| 26 | #include <vppinfra/byte_order.h> |
| 27 | #include <vppinfra/elog.h> |
| 28 | #include <vlib/vlib.h> |
| 29 | #include <vlib/unix/unix.h> |
| 30 | #include <vlibapi/api.h> |
| 31 | #include <vlibmemory/api.h> |
| 32 | |
| 33 | /** |
| 34 | * @file |
| 35 | * @brief Binary API messaging via shared memory |
| 36 | * Low-level, primary provisioning interface |
| 37 | */ |
| 38 | /*? %%clicmd:group_label Binary API CLI %% ?*/ |
| 39 | /*? %%syscfg:group_label Binary API configuration %% ?*/ |
| 40 | |
| 41 | #define TRACE_VLIB_MEMORY_QUEUE 0 |
| 42 | |
| 43 | #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */ |
| 44 | |
| 45 | #define vl_typedefs /* define message structures */ |
| 46 | #include <vlibmemory/vl_memory_api_h.h> |
| 47 | #undef vl_typedefs |
| 48 | |
| 49 | /* instantiate all the print functions we know about */ |
| 50 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 51 | #define vl_printfun |
| 52 | #include <vlibmemory/vl_memory_api_h.h> |
| 53 | #undef vl_printfun |
| 54 | |
| 55 | static inline void * |
| 56 | vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a, |
| 57 | void *handle) |
| 58 | { |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 59 | vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n", |
| 60 | a->plugin_name, |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 61 | clib_host_to_net_u16 (a->first_msg_id), |
| 62 | clib_host_to_net_u16 (a->last_msg_id)); |
| 63 | return handle; |
| 64 | } |
| 65 | |
| 66 | /* instantiate all the endian swap functions we know about */ |
| 67 | #define vl_endianfun |
| 68 | #include <vlibmemory/vl_memory_api_h.h> |
| 69 | #undef vl_endianfun |
| 70 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 71 | static void |
| 72 | vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp) |
| 73 | { |
| 74 | vl_api_get_first_msg_id_reply_t *rmp; |
| 75 | vl_api_registration_t *regp; |
| 76 | uword *p; |
| 77 | api_main_t *am = &api_main; |
| 78 | vl_api_msg_range_t *rp; |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 79 | u8 name[64]; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 80 | u16 first_msg_id = ~0; |
| 81 | int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */ |
| 82 | |
| 83 | regp = vl_api_client_index_to_registration (mp->client_index); |
| 84 | if (!regp) |
| 85 | return; |
| 86 | |
| 87 | if (am->msg_range_by_name == 0) |
| 88 | goto out; |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 89 | strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name)); |
| 90 | name[ARRAY_LEN (name) - 1] = '\0'; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 91 | p = hash_get_mem (am->msg_range_by_name, name); |
| 92 | if (p == 0) |
| 93 | goto out; |
| 94 | |
| 95 | rp = vec_elt_at_index (am->msg_ranges, p[0]); |
| 96 | first_msg_id = rp->first_msg_id; |
| 97 | rv = 0; |
| 98 | |
| 99 | out: |
| 100 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 101 | rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY); |
| 102 | rmp->context = mp->context; |
| 103 | rmp->retval = ntohl (rv); |
| 104 | rmp->first_msg_id = ntohs (first_msg_id); |
| 105 | vl_api_send_msg (regp, (u8 *) rmp); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | vl_api_api_versions_t_handler (vl_api_api_versions_t * mp) |
| 110 | { |
| 111 | api_main_t *am = &api_main; |
| 112 | vl_api_api_versions_reply_t *rmp; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 113 | vl_api_registration_t *reg; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 114 | u32 nmsg = vec_len (am->api_version_list); |
| 115 | int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg; |
| 116 | int i; |
| 117 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 118 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 119 | if (!reg) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 120 | return; |
| 121 | |
| 122 | rmp = vl_msg_api_alloc (msg_size); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 123 | clib_memset (rmp, 0, msg_size); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 124 | rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY); |
| 125 | |
| 126 | /* fill in the message */ |
| 127 | rmp->context = mp->context; |
| 128 | rmp->count = htonl (nmsg); |
| 129 | |
| 130 | for (i = 0; i < nmsg; ++i) |
| 131 | { |
| 132 | api_version_t *vl = &am->api_version_list[i]; |
| 133 | rmp->api_versions[i].major = htonl (vl->major); |
| 134 | rmp->api_versions[i].minor = htonl (vl->minor); |
| 135 | rmp->api_versions[i].patch = htonl (vl->patch); |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 136 | strncpy ((char *) rmp->api_versions[i].name, vl->name, |
| 137 | ARRAY_LEN (rmp->api_versions[i].name)); |
| 138 | rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] = |
| 139 | '\0'; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 142 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 145 | #define foreach_vlib_api_msg \ |
| 146 | _(GET_FIRST_MSG_ID, get_first_msg_id) \ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 147 | _(API_VERSIONS, api_versions) |
| 148 | |
| 149 | /* |
| 150 | * vl_api_init |
| 151 | */ |
| 152 | static int |
| 153 | vlib_api_init (void) |
| 154 | { |
| 155 | vl_msg_api_msg_config_t cfg; |
| 156 | vl_msg_api_msg_config_t *c = &cfg; |
| 157 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 158 | clib_memset (c, 0, sizeof (*c)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 159 | |
| 160 | #define _(N,n) do { \ |
| 161 | c->id = VL_API_##N; \ |
| 162 | c->name = #n; \ |
| 163 | c->handler = vl_api_##n##_t_handler; \ |
| 164 | c->cleanup = vl_noop_handler; \ |
| 165 | c->endian = vl_api_##n##_t_endian; \ |
| 166 | c->print = vl_api_##n##_t_print; \ |
| 167 | c->size = sizeof(vl_api_##n##_t); \ |
| 168 | c->traced = 1; /* trace, so these msgs print */ \ |
| 169 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 170 | c->message_bounce = 0; /* don't bounce this message */ \ |
| 171 | vl_msg_api_config(c);} while (0); |
| 172 | |
| 173 | foreach_vlib_api_msg; |
| 174 | #undef _ |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | u64 vector_rate_histogram[SLEEP_N_BUCKETS]; |
| 180 | |
| 181 | /* |
| 182 | * Callback to send ourselves a plugin numbering-space trace msg |
| 183 | */ |
| 184 | static void |
| 185 | send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id) |
| 186 | { |
| 187 | vl_api_trace_plugin_msg_ids_t *mp; |
| 188 | api_main_t *am = &api_main; |
| 189 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
| 190 | svm_queue_t *q; |
| 191 | |
| 192 | mp = vl_msg_api_alloc_as_if_client (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 193 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 194 | |
| 195 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS); |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 196 | strncpy ((char *) mp->plugin_name, (char *) name, |
| 197 | sizeof (mp->plugin_name) - 1); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 198 | mp->first_msg_id = clib_host_to_net_u16 (first_msg_id); |
| 199 | mp->last_msg_id = clib_host_to_net_u16 (last_msg_id); |
| 200 | |
| 201 | q = shmem_hdr->vl_input_queue; |
| 202 | |
| 203 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | vl_api_save_msg_table (void) |
| 208 | { |
| 209 | u8 *serialized_message_table; |
| 210 | api_main_t *am = &api_main; |
| 211 | u8 *chroot_file; |
| 212 | int fd, rv; |
| 213 | |
| 214 | /* |
| 215 | * Snapshoot the api message table. |
| 216 | */ |
| 217 | if (strstr ((char *) am->save_msg_table_filename, "..") |
| 218 | || index ((char *) am->save_msg_table_filename, '/')) |
| 219 | { |
| 220 | clib_warning ("illegal save-message-table filename '%s'", |
| 221 | am->save_msg_table_filename); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0); |
| 226 | |
| 227 | fd = creat ((char *) chroot_file, 0644); |
| 228 | |
| 229 | if (fd < 0) |
| 230 | { |
| 231 | clib_unix_warning ("creat"); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | serialized_message_table = vl_api_serialize_message_table (am, 0); |
| 236 | |
| 237 | rv = write (fd, serialized_message_table, |
| 238 | vec_len (serialized_message_table)); |
| 239 | |
| 240 | if (rv != vec_len (serialized_message_table)) |
| 241 | clib_unix_warning ("write"); |
| 242 | |
| 243 | rv = close (fd); |
| 244 | if (rv < 0) |
| 245 | clib_unix_warning ("close"); |
| 246 | |
| 247 | vec_free (chroot_file); |
| 248 | vec_free (serialized_message_table); |
| 249 | } |
| 250 | |
| 251 | static uword |
| 252 | vl_api_clnt_process (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 253 | vlib_frame_t * f) |
| 254 | { |
| 255 | int private_segment_rotor = 0, i, rv; |
| 256 | vl_socket_args_for_process_t *a; |
| 257 | vl_shmem_hdr_t *shm; |
| 258 | svm_queue_t *q; |
| 259 | clib_error_t *e; |
| 260 | api_main_t *am = &api_main; |
| 261 | f64 dead_client_scan_time; |
| 262 | f64 sleep_time, start_time; |
| 263 | f64 vector_rate; |
| 264 | clib_error_t *error; |
| 265 | uword event_type; |
| 266 | uword *event_data = 0; |
| 267 | f64 now; |
| 268 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 269 | if ((error = vl_sock_api_init (vm))) |
| 270 | { |
| 271 | clib_error_report (error); |
| 272 | clib_warning ("socksvr_api_init failed, quitting..."); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if ((rv = vlib_api_init ()) < 0) |
| 277 | { |
| 278 | clib_warning ("vlib_api_init returned %d, quitting...", rv); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | shm = am->shmem_hdr; |
| 283 | q = shm->vl_input_queue; |
| 284 | |
| 285 | e = vlib_call_init_exit_functions |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 286 | (vm, &vm->api_init_function_registrations, 1 /* call_once */ ); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 287 | if (e) |
| 288 | clib_error_report (e); |
| 289 | |
| 290 | sleep_time = 10.0; |
| 291 | dead_client_scan_time = vlib_time_now (vm) + 10.0; |
| 292 | |
| 293 | /* |
| 294 | * Send plugin message range messages for each plugin we loaded |
| 295 | */ |
| 296 | for (i = 0; i < vec_len (am->msg_ranges); i++) |
| 297 | { |
| 298 | vl_api_msg_range_t *rp = am->msg_ranges + i; |
| 299 | send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id, |
| 300 | rp->last_msg_id); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Save the api message table snapshot, if configured |
| 305 | */ |
| 306 | if (am->save_msg_table_filename) |
| 307 | vl_api_save_msg_table (); |
| 308 | |
| 309 | /* $$$ pay attention to frame size, control CPU usage */ |
| 310 | while (1) |
| 311 | { |
| 312 | /* |
| 313 | * There's a reason for checking the queue before |
| 314 | * sleeping. If the vlib application crashes, it's entirely |
| 315 | * possible for a client to enqueue a connect request |
| 316 | * during the process restart interval. |
| 317 | * |
| 318 | * Unless some force of physics causes the new incarnation |
| 319 | * of the application to process the request, the client will |
| 320 | * sit and wait for Godot... |
| 321 | */ |
Dave Barach | a8df85c | 2019-10-01 13:34:23 -0400 | [diff] [blame] | 322 | vector_rate = (f64) vlib_last_vectors_per_main_loop (vm); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 323 | start_time = vlib_time_now (vm); |
| 324 | while (1) |
| 325 | { |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 326 | if (vl_mem_api_handle_rpc (vm, node) |
| 327 | || vl_mem_api_handle_msg_main (vm, node)) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 328 | { |
| 329 | vm->api_queue_nonempty = 0; |
| 330 | VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0); |
| 331 | sleep_time = 20.0; |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | /* Allow no more than 10us without a pause */ |
| 336 | if (vlib_time_now (vm) > start_time + 10e-6) |
| 337 | { |
| 338 | int index = SLEEP_400_US; |
| 339 | if (vector_rate > 40.0) |
| 340 | sleep_time = 400e-6; |
| 341 | else if (vector_rate > 20.0) |
| 342 | { |
| 343 | index = SLEEP_200_US; |
| 344 | sleep_time = 200e-6; |
| 345 | } |
| 346 | else if (vector_rate >= 1.0) |
| 347 | { |
| 348 | index = SLEEP_100_US; |
| 349 | sleep_time = 100e-6; |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | index = SLEEP_10_US; |
| 354 | sleep_time = 10e-6; |
| 355 | } |
| 356 | vector_rate_histogram[index] += 1; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * see if we have any private api shared-memory segments |
| 363 | * If so, push required context variables, and process |
| 364 | * a message. |
| 365 | */ |
| 366 | if (PREDICT_FALSE (vec_len (am->vlib_private_rps))) |
| 367 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 368 | if (private_segment_rotor >= vec_len (am->vlib_private_rps)) |
| 369 | private_segment_rotor = 0; |
Florin Coras | ec54e11 | 2018-08-16 15:45:34 -0700 | [diff] [blame] | 370 | vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | vlib_process_wait_for_event_or_clock (vm, sleep_time); |
| 374 | vec_reset_length (event_data); |
| 375 | event_type = vlib_process_get_events (vm, &event_data); |
| 376 | now = vlib_time_now (vm); |
| 377 | |
| 378 | switch (event_type) |
| 379 | { |
| 380 | case QUEUE_SIGNAL_EVENT: |
| 381 | vm->queue_signal_pending = 0; |
| 382 | VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize); |
| 383 | |
| 384 | break; |
| 385 | case SOCKET_READ_EVENT: |
| 386 | for (i = 0; i < vec_len (event_data); i++) |
| 387 | { |
| 388 | a = pool_elt_at_index (socket_main.process_args, event_data[i]); |
| 389 | vl_socket_process_api_msg (a->clib_file, a->regp, |
| 390 | (i8 *) a->data); |
| 391 | vec_free (a->data); |
| 392 | pool_put (socket_main.process_args, a); |
| 393 | } |
| 394 | break; |
| 395 | |
| 396 | /* Timeout... */ |
| 397 | case -1: |
| 398 | break; |
| 399 | |
| 400 | default: |
| 401 | clib_warning ("unknown event type %d", event_type); |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | if (now > dead_client_scan_time) |
| 406 | { |
| 407 | vl_mem_api_dead_client_scan (am, shm, now); |
| 408 | dead_client_scan_time = vlib_time_now (vm) + 10.0; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | /* *INDENT-OFF* */ |
| 415 | VLIB_REGISTER_NODE (vl_api_clnt_node) = |
| 416 | { |
| 417 | .function = vl_api_clnt_process, |
| 418 | .type = VLIB_NODE_TYPE_PROCESS, |
| 419 | .name = "api-rx-from-ring", |
| 420 | .state = VLIB_NODE_STATE_DISABLED, |
| 421 | }; |
| 422 | /* *INDENT-ON* */ |
| 423 | |
| 424 | void |
| 425 | vl_mem_api_enable_disable (vlib_main_t * vm, int enable) |
| 426 | { |
| 427 | vlib_node_set_state (vm, vl_api_clnt_node.index, |
| 428 | (enable |
| 429 | ? VLIB_NODE_STATE_POLLING |
| 430 | : VLIB_NODE_STATE_DISABLED)); |
| 431 | } |
| 432 | |
| 433 | static uword |
| 434 | api_rx_from_node (vlib_main_t * vm, |
| 435 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 436 | { |
| 437 | uword n_packets = frame->n_vectors; |
| 438 | uword n_left_from; |
| 439 | u32 *from; |
| 440 | static u8 *long_msg; |
| 441 | |
| 442 | vec_validate (long_msg, 4095); |
| 443 | n_left_from = frame->n_vectors; |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 444 | from = vlib_frame_vector_args (frame); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 445 | |
| 446 | while (n_left_from > 0) |
| 447 | { |
| 448 | u32 bi0; |
| 449 | vlib_buffer_t *b0; |
| 450 | void *msg; |
| 451 | uword msg_len; |
| 452 | |
| 453 | bi0 = from[0]; |
| 454 | b0 = vlib_get_buffer (vm, bi0); |
| 455 | from += 1; |
| 456 | n_left_from -= 1; |
| 457 | |
| 458 | msg = b0->data + b0->current_data; |
| 459 | msg_len = b0->current_length; |
| 460 | if (b0->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 461 | { |
| 462 | ASSERT (long_msg != 0); |
| 463 | _vec_len (long_msg) = 0; |
| 464 | vec_add (long_msg, msg, msg_len); |
| 465 | while (b0->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 466 | { |
| 467 | b0 = vlib_get_buffer (vm, b0->next_buffer); |
| 468 | msg = b0->data + b0->current_data; |
| 469 | msg_len = b0->current_length; |
| 470 | vec_add (long_msg, msg, msg_len); |
| 471 | } |
| 472 | msg = long_msg; |
| 473 | } |
| 474 | vl_msg_api_handler_no_trace_no_free (msg); |
| 475 | } |
| 476 | |
| 477 | /* Free what we've been given. */ |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 478 | vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_packets); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 479 | |
| 480 | return n_packets; |
| 481 | } |
| 482 | |
| 483 | /* *INDENT-OFF* */ |
| 484 | VLIB_REGISTER_NODE (api_rx_from_node_node,static) = { |
| 485 | .function = api_rx_from_node, |
| 486 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 487 | .vector_size = 4, |
| 488 | .name = "api-rx-from-node", |
| 489 | }; |
| 490 | /* *INDENT-ON* */ |
| 491 | |
| 492 | static void |
| 493 | vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp) |
| 494 | { |
| 495 | vl_api_rpc_call_reply_t *rmp; |
| 496 | int (*fp) (void *); |
| 497 | i32 rv = 0; |
| 498 | vlib_main_t *vm = vlib_get_main (); |
| 499 | |
| 500 | if (mp->function == 0) |
| 501 | { |
| 502 | rv = -1; |
| 503 | clib_warning ("rpc NULL function pointer"); |
| 504 | } |
| 505 | |
| 506 | else |
| 507 | { |
| 508 | if (mp->need_barrier_sync) |
| 509 | vlib_worker_thread_barrier_sync (vm); |
| 510 | |
| 511 | fp = uword_to_pointer (mp->function, int (*)(void *)); |
| 512 | rv = fp (mp->data); |
| 513 | |
| 514 | if (mp->need_barrier_sync) |
| 515 | vlib_worker_thread_barrier_release (vm); |
| 516 | } |
| 517 | |
| 518 | if (mp->send_reply) |
| 519 | { |
| 520 | svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index); |
| 521 | if (q) |
| 522 | { |
| 523 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
| 524 | rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY); |
| 525 | rmp->context = mp->context; |
| 526 | rmp->retval = rv; |
| 527 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 528 | } |
| 529 | } |
| 530 | if (mp->multicast) |
| 531 | { |
| 532 | clib_warning ("multicast not yet implemented..."); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | static void |
| 537 | vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp) |
| 538 | { |
| 539 | clib_warning ("unimplemented"); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | vl_api_send_pending_rpc_requests (vlib_main_t * vm) |
| 544 | { |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 545 | vlib_main_t *vm_global = &vlib_global_main; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 546 | |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 547 | ASSERT (vm != vm_global); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 548 | |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 549 | clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock); |
| 550 | vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests); |
| 551 | vec_reset_length (vm->pending_rpc_requests); |
| 552 | clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | always_inline void |
| 556 | vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length, |
| 557 | u8 force_rpc) |
| 558 | { |
| 559 | vl_api_rpc_call_t *mp; |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 560 | vlib_main_t *vm_global = &vlib_global_main; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 561 | vlib_main_t *vm = vlib_get_main (); |
| 562 | |
| 563 | /* Main thread and not a forced RPC: call the function directly */ |
| 564 | if ((force_rpc == 0) && (vlib_get_thread_index () == 0)) |
| 565 | { |
| 566 | void (*call_fp) (void *); |
| 567 | |
| 568 | vlib_worker_thread_barrier_sync (vm); |
| 569 | |
| 570 | call_fp = fp; |
| 571 | call_fp (data); |
| 572 | |
| 573 | vlib_worker_thread_barrier_release (vm); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | /* Otherwise, actually do an RPC */ |
| 578 | mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length); |
| 579 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 580 | clib_memset (mp, 0, sizeof (*mp)); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 581 | clib_memcpy_fast (mp->data, data, data_length); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 582 | mp->_vl_msg_id = ntohs (VL_API_RPC_CALL); |
| 583 | mp->function = pointer_to_uword (fp); |
| 584 | mp->need_barrier_sync = 1; |
| 585 | |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 586 | /* Add to the pending vector. Thread 0 requires locking. */ |
| 587 | if (vm == vm_global) |
| 588 | clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 589 | vec_add1 (vm->pending_rpc_requests, (uword) mp); |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 590 | if (vm == vm_global) |
| 591 | clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Check if called from worker threads. |
| 596 | * If so, make rpc call of fp through shmem. |
| 597 | * Otherwise, call fp directly |
| 598 | */ |
| 599 | void |
| 600 | vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length) |
| 601 | { |
| 602 | vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */ |
| 603 | 0); |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Always make rpc call of fp through shmem, useful for calling from threads |
| 608 | * not setup as worker threads, such as DPDK callback thread |
| 609 | */ |
| 610 | void |
| 611 | vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length) |
| 612 | { |
| 613 | vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */ |
| 614 | 1); |
| 615 | } |
| 616 | |
| 617 | static void |
| 618 | vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp) |
| 619 | { |
| 620 | api_main_t *am = &api_main; |
| 621 | vl_api_msg_range_t *rp; |
| 622 | uword *p; |
| 623 | |
| 624 | /* Noop (except for tracing) during normal operation */ |
| 625 | if (am->replay_in_progress == 0) |
| 626 | return; |
| 627 | |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 628 | p = hash_get_mem (am->msg_range_by_name, mp->plugin_name); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 629 | if (p == 0) |
| 630 | { |
| 631 | clib_warning ("WARNING: traced plugin '%s' not in current image", |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 632 | mp->plugin_name); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 633 | return; |
| 634 | } |
| 635 | |
| 636 | rp = vec_elt_at_index (am->msg_ranges, p[0]); |
| 637 | if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id)) |
| 638 | { |
| 639 | clib_warning ("WARNING: traced plugin '%s' first message id %d not %d", |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 640 | mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id), |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 641 | rp->first_msg_id); |
| 642 | } |
| 643 | |
| 644 | if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id)) |
| 645 | { |
| 646 | clib_warning ("WARNING: traced plugin '%s' last message id %d not %d", |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 647 | mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id), |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 648 | rp->last_msg_id); |
| 649 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | #define foreach_rpc_api_msg \ |
| 653 | _(RPC_CALL,rpc_call) \ |
| 654 | _(RPC_CALL_REPLY,rpc_call_reply) |
| 655 | |
| 656 | #define foreach_plugin_trace_msg \ |
| 657 | _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids) |
| 658 | |
| 659 | /* |
| 660 | * Set the rpc callback at our earliest possible convenience. |
| 661 | * This avoids ordering issues between thread_init() -> start_workers and |
| 662 | * an init function which we could define here. If we ever intend to use |
| 663 | * vlib all by itself, we can't create a link-time dependency on |
| 664 | * an init function here and a typical "call foo_init first" |
| 665 | * guitar lick. |
| 666 | */ |
| 667 | |
| 668 | extern void *rpc_call_main_thread_cb_fn; |
| 669 | |
| 670 | static clib_error_t * |
| 671 | rpc_api_hookup (vlib_main_t * vm) |
| 672 | { |
| 673 | api_main_t *am = &api_main; |
| 674 | #define _(N,n) \ |
| 675 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 676 | vl_api_##n##_t_handler, \ |
| 677 | vl_noop_handler, \ |
| 678 | vl_noop_handler, \ |
| 679 | vl_api_##n##_t_print, \ |
| 680 | sizeof(vl_api_##n##_t), 0 /* do not trace */); |
| 681 | foreach_rpc_api_msg; |
| 682 | #undef _ |
| 683 | |
| 684 | #define _(N,n) \ |
| 685 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 686 | vl_api_##n##_t_handler, \ |
| 687 | vl_noop_handler, \ |
| 688 | vl_noop_handler, \ |
| 689 | vl_api_##n##_t_print, \ |
| 690 | sizeof(vl_api_##n##_t), 1 /* do trace */); |
| 691 | foreach_plugin_trace_msg; |
| 692 | #undef _ |
| 693 | |
| 694 | /* No reason to halt the parade to create a trace record... */ |
| 695 | am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1; |
| 696 | rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | VLIB_API_INIT_FUNCTION (rpc_api_hookup); |
| 701 | |
| 702 | /* |
| 703 | * fd.io coding-style-patch-verification: ON |
| 704 | * |
| 705 | * Local Variables: |
| 706 | * eval: (c-set-style "gnu") |
| 707 | * End: |
| 708 | */ |