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