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