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