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