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