Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * memory_vlib.c |
| 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 <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <signal.h> |
| 26 | #include <pthread.h> |
| 27 | #include <vppinfra/vec.h> |
| 28 | #include <vppinfra/hash.h> |
| 29 | #include <vppinfra/pool.h> |
| 30 | #include <vppinfra/format.h> |
| 31 | #include <vppinfra/byte_order.h> |
| 32 | #include <vppinfra/elog.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <vlib/vlib.h> |
| 35 | #include <vlib/unix/unix.h> |
| 36 | #include <vlibapi/api.h> |
| 37 | #include <vlibmemory/api.h> |
| 38 | |
| 39 | #define TRACE_VLIB_MEMORY_QUEUE 0 |
| 40 | |
| 41 | #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */ |
| 42 | |
| 43 | #define vl_typedefs /* define message structures */ |
| 44 | #include <vlibmemory/vl_memory_api_h.h> |
| 45 | #undef vl_typedefs |
| 46 | |
| 47 | /* instantiate all the print functions we know about */ |
| 48 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 49 | #define vl_printfun |
| 50 | #include <vlibmemory/vl_memory_api_h.h> |
| 51 | #undef vl_printfun |
| 52 | |
| 53 | static inline void * |
| 54 | vl_api_memclnt_create_t_print (vl_api_memclnt_create_t *a,void *handle) |
| 55 | { |
| 56 | vl_print(handle, "vl_api_memclnt_create_t:\n"); |
| 57 | vl_print(handle, "name: %s\n", a->name); |
| 58 | vl_print(handle, "input_queue: 0x%wx\n", a->input_queue); |
| 59 | vl_print(handle, "context: %u\n", (unsigned) a->context); |
| 60 | vl_print(handle, "ctx_quota: %ld\n", (long) a->ctx_quota); |
| 61 | return handle; |
| 62 | } |
| 63 | |
| 64 | static inline void * |
| 65 | vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t *a,void *handle) |
| 66 | { |
| 67 | vl_print(handle, "vl_api_memclnt_delete_t:\n"); |
| 68 | vl_print(handle, "index: %u\n", (unsigned) a->index); |
| 69 | vl_print(handle, "handle: 0x%wx\n", a->handle); |
| 70 | return handle; |
| 71 | } |
| 72 | |
| 73 | /* instantiate all the endian swap functions we know about */ |
| 74 | #define vl_endianfun |
| 75 | #include <vlibmemory/vl_memory_api_h.h> |
| 76 | #undef vl_endianfun |
| 77 | |
| 78 | void vl_socket_api_send (vl_api_registration_t *rp, u8 *elem) |
| 79 | __attribute__((weak)); |
| 80 | |
| 81 | void |
| 82 | vl_socket_api_send (vl_api_registration_t *rp, u8 *elem) |
| 83 | { |
| 84 | static int count; |
| 85 | |
| 86 | if (count++ < 5) |
| 87 | clib_warning ("need to link against -lvlibsocket, msg not sent!"); |
| 88 | } |
| 89 | |
| 90 | void vl_msg_api_send (vl_api_registration_t *rp, u8 *elem) |
| 91 | { |
| 92 | if (PREDICT_FALSE(rp->registration_type > REGISTRATION_TYPE_SHMEM)) { |
| 93 | vl_socket_api_send (rp, elem); |
| 94 | } else { |
| 95 | vl_msg_api_send_shmem (rp->vl_input_queue, elem); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int vl_msg_api_version_check (vl_api_memclnt_create_t * mp) |
| 100 | __attribute__((weak)); |
| 101 | |
| 102 | int vl_msg_api_version_check (vl_api_memclnt_create_t * mp) { return 0; } |
| 103 | |
| 104 | /* |
| 105 | * vl_api_memclnt_create_t_handler |
| 106 | */ |
| 107 | void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *mp) |
| 108 | { |
| 109 | vl_api_registration_t **regpp; |
| 110 | vl_api_registration_t *regp; |
| 111 | vl_api_memclnt_create_reply_t *rp; |
| 112 | svm_region_t *svm; |
| 113 | unix_shared_memory_queue_t *q; |
| 114 | int rv; |
| 115 | void *oldheap; |
| 116 | api_main_t *am = &api_main; |
| 117 | |
| 118 | /* Indicate API version mismatch if appropriate */ |
| 119 | rv = vl_msg_api_version_check (mp); |
| 120 | |
| 121 | /* |
| 122 | * This is tortured. Maintain a vlib-address-space private |
| 123 | * pool of client registrations. We use the shared-memory virtual |
| 124 | * address of client structure as a handle, to allow direct |
| 125 | * manipulation of context quota vbls from the client library. |
| 126 | * |
| 127 | * This scheme causes trouble w/ API message trace replay, since |
| 128 | * some random VA from clib_mem_alloc() certainly won't |
| 129 | * occur in the Linux sim. The (very) few places |
| 130 | * that care need to use the pool index. |
| 131 | * |
| 132 | * Putting the registration object(s) into a pool in shared memory and |
| 133 | * using the pool index as a handle seems like a great idea. |
| 134 | * Unfortunately, each and every reference to that pool would need |
| 135 | * to be protected by a mutex: |
| 136 | * |
| 137 | * Client VLIB |
| 138 | * ------ ---- |
| 139 | * convert pool index to |
| 140 | * pointer. |
| 141 | * <deschedule> |
| 142 | * expand pool |
| 143 | * <deschedule> |
| 144 | * kaboom! |
| 145 | */ |
| 146 | |
| 147 | pool_get(am->vl_clients, regpp); |
| 148 | |
| 149 | svm = am->vlib_rp; |
| 150 | |
| 151 | pthread_mutex_lock (&svm->mutex); |
| 152 | oldheap = svm_push_data_heap(svm); |
| 153 | *regpp = clib_mem_alloc(sizeof(vl_api_registration_t)); |
| 154 | |
| 155 | regp = *regpp; |
| 156 | memset(regp, 0, sizeof(*regp)); |
| 157 | regp->registration_type = REGISTRATION_TYPE_SHMEM; |
| 158 | regp->vl_api_registration_pool_index = regpp - am->vl_clients; |
| 159 | |
| 160 | q = regp->vl_input_queue = (unix_shared_memory_queue_t *)(uword) |
| 161 | mp->input_queue; |
| 162 | |
| 163 | regp->name = format(0, "%s", mp->name); |
| 164 | vec_add1(regp->name, 0); |
| 165 | |
| 166 | pthread_mutex_unlock(&svm->mutex); |
| 167 | svm_pop_heap (oldheap); |
| 168 | |
| 169 | rp = vl_msg_api_alloc(sizeof(*rp)); |
| 170 | rp->_vl_msg_id = ntohs(VL_API_MEMCLNT_CREATE_REPLY); |
| 171 | rp->handle = (uword)regp; |
| 172 | rp->index = vl_msg_api_handle_from_index_and_epoch |
| 173 | (regp->vl_api_registration_pool_index, |
| 174 | am->shmem_hdr->application_restarts); |
| 175 | rp->context = mp->context; |
| 176 | rp->response = ntohl(rv); |
| 177 | |
| 178 | vl_msg_api_send_shmem(q, (u8 *)&rp); |
| 179 | } |
| 180 | |
| 181 | /* Application callback to clean up leftover registrations from this client */ |
| 182 | int vl_api_memclnt_delete_callback (u32 client_index) |
| 183 | __attribute__((weak)); |
| 184 | |
| 185 | int vl_api_memclnt_delete_callback (u32 client_index) |
| 186 | { return 0; } |
| 187 | |
| 188 | /* |
| 189 | * vl_api_memclnt_delete_t_handler |
| 190 | */ |
| 191 | void vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t *mp) |
| 192 | { |
| 193 | vl_api_registration_t **regpp; |
| 194 | vl_api_registration_t *regp; |
| 195 | vl_api_memclnt_delete_reply_t *rp; |
| 196 | svm_region_t *svm; |
| 197 | void *oldheap; |
| 198 | api_main_t *am = &api_main; |
| 199 | u32 handle, client_index, epoch; |
| 200 | |
| 201 | handle = mp->index; |
| 202 | |
| 203 | if (vl_api_memclnt_delete_callback (handle)) |
| 204 | return; |
| 205 | |
| 206 | epoch = vl_msg_api_handle_get_epoch (handle); |
| 207 | client_index = vl_msg_api_handle_get_index (handle); |
| 208 | |
| 209 | if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)) { |
| 210 | clib_warning |
| 211 | ("Stale clnt delete index %d old epoch %d cur epoch %d", |
| 212 | client_index, epoch, |
| 213 | (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | regpp = am->vl_clients + client_index; |
| 218 | |
| 219 | if (!pool_is_free(am->vl_clients, regpp)) { |
| 220 | regp = *regpp; |
| 221 | svm = am->vlib_rp; |
| 222 | |
| 223 | /* $$$ check the input queue for e.g. punted sf's */ |
| 224 | |
| 225 | rp = vl_msg_api_alloc(sizeof(*rp)); |
| 226 | rp->_vl_msg_id = ntohs(VL_API_MEMCLNT_DELETE_REPLY); |
| 227 | rp->handle = mp->handle; |
| 228 | rp->response = 1; |
| 229 | |
| 230 | vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *)&rp); |
| 231 | |
| 232 | if (client_index != regp->vl_api_registration_pool_index) { |
| 233 | clib_warning ("mismatch client_index %d pool_index %d", |
| 234 | client_index, regp->vl_api_registration_pool_index); |
| 235 | vl_msg_api_free (rp); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | /* No dangling references, please */ |
| 240 | *regpp = 0; |
| 241 | |
| 242 | pool_put_index(am->vl_clients, |
| 243 | regp->vl_api_registration_pool_index); |
| 244 | |
| 245 | pthread_mutex_lock (&svm->mutex); |
| 246 | oldheap = svm_push_data_heap(svm); |
| 247 | /* Poison the old registration */ |
| 248 | memset (regp, 0xF1, sizeof (*regp)); |
| 249 | clib_mem_free (regp); |
| 250 | pthread_mutex_unlock(&svm->mutex); |
| 251 | svm_pop_heap (oldheap); |
| 252 | } else { |
| 253 | clib_warning("unknown client ID %d", mp->index); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp) |
| 258 | { |
| 259 | vl_api_get_first_msg_id_reply_t * rmp; |
| 260 | unix_shared_memory_queue_t * q; |
| 261 | uword * p; |
| 262 | api_main_t *am = &api_main; |
| 263 | vl_api_msg_range_t * rp; |
| 264 | u8 name[64]; |
| 265 | u16 first_msg_id = ~0; |
| 266 | int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */ |
| 267 | |
| 268 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 269 | if (!q) |
| 270 | return; |
| 271 | |
| 272 | if (am->msg_range_by_name == 0) |
| 273 | goto out; |
| 274 | |
| 275 | strncpy ((char *)name, (char *) mp->name, ARRAY_LEN(name)-1); |
| 276 | |
| 277 | p = hash_get_mem (am->msg_range_by_name, name); |
| 278 | if (p == 0) |
| 279 | goto out; |
| 280 | |
| 281 | rp = vec_elt_at_index (am->msg_ranges, p[0]); |
| 282 | |
| 283 | first_msg_id = rp->first_msg_id; |
| 284 | rv = 0; |
| 285 | |
| 286 | out: |
| 287 | |
| 288 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 289 | rmp->_vl_msg_id = ntohs(VL_API_GET_FIRST_MSG_ID_REPLY); |
| 290 | rmp->context = mp->context; |
| 291 | rmp->retval = ntohl(rv); |
| 292 | rmp->first_msg_id = ntohs(first_msg_id); |
| 293 | vl_msg_api_send_shmem (q, (u8 *)&rmp); |
| 294 | } |
| 295 | |
| 296 | #define foreach_vlib_api_msg \ |
| 297 | _(MEMCLNT_CREATE, memclnt_create) \ |
| 298 | _(MEMCLNT_DELETE, memclnt_delete) \ |
| 299 | _(GET_FIRST_MSG_ID, get_first_msg_id) |
| 300 | |
| 301 | /* |
| 302 | * vl_api_init |
| 303 | */ |
| 304 | static int memory_api_init(char *region_name) |
| 305 | { |
| 306 | int rv; |
| 307 | vl_msg_api_msg_config_t cfg; |
| 308 | vl_msg_api_msg_config_t *c = &cfg; |
| 309 | |
| 310 | if ((rv = vl_map_shmem(region_name, 1 /* is_vlib */)) < 0) |
| 311 | return rv; |
| 312 | |
| 313 | #define _(N,n) do { \ |
| 314 | c->id = VL_API_##N; \ |
| 315 | c->name = #n; \ |
| 316 | c->handler = vl_api_##n##_t_handler; \ |
| 317 | c->cleanup = vl_noop_handler; \ |
| 318 | c->endian = vl_api_##n##_t_endian; \ |
| 319 | c->print = vl_api_##n##_t_print; \ |
| 320 | c->size = sizeof(vl_api_##n##_t); \ |
| 321 | c->traced = 1; /* trace, so these msgs print */ \ |
| 322 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 323 | vl_msg_api_config(c);} while (0); |
| 324 | |
| 325 | foreach_vlib_api_msg; |
| 326 | #undef _ |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | #define foreach_histogram_bucket \ |
| 332 | _(400) \ |
| 333 | _(200) \ |
| 334 | _(100) \ |
| 335 | _(10) |
| 336 | |
| 337 | typedef enum { |
| 338 | #define _(n) SLEEP_##n##_US, |
| 339 | foreach_histogram_bucket |
| 340 | #undef _ |
| 341 | SLEEP_N_BUCKETS, |
| 342 | } histogram_index_t; |
| 343 | |
| 344 | static u64 vector_rate_histogram[SLEEP_N_BUCKETS]; |
| 345 | |
| 346 | static void memclnt_queue_signal (int signum); |
| 347 | static void memclnt_queue_callback (vlib_main_t *vm); |
| 348 | |
| 349 | static uword |
| 350 | memclnt_process (vlib_main_t * vm, |
| 351 | vlib_node_runtime_t * node, |
| 352 | vlib_frame_t * f) |
| 353 | { |
| 354 | uword mp; |
| 355 | vl_shmem_hdr_t *shm; |
| 356 | unix_shared_memory_queue_t *q; |
| 357 | clib_error_t *e; |
| 358 | int rv; |
| 359 | api_main_t *am = &api_main; |
| 360 | f64 dead_client_scan_time; |
| 361 | f64 sleep_time, start_time; |
| 362 | f64 vector_rate; |
| 363 | |
| 364 | vlib_set_queue_signal_callback (vm, memclnt_queue_callback); |
| 365 | am->vlib_signal = SIGUSR1; |
| 366 | signal (am->vlib_signal, memclnt_queue_signal); |
| 367 | |
| 368 | if ((rv = memory_api_init(am->region_name)) < 0) { |
| 369 | clib_warning("memory_api_init returned %d, wait for godot...", rv); |
| 370 | vlib_process_suspend (vm, 1e70); |
| 371 | } |
| 372 | |
| 373 | shm = am->shmem_hdr; |
| 374 | ASSERT(shm); |
| 375 | q = shm->vl_input_queue; |
| 376 | ASSERT(q); |
| 377 | |
| 378 | e = vlib_call_init_exit_functions |
| 379 | (vm, vm->api_init_function_registrations, 1 /* call_once */); |
| 380 | if (e) |
| 381 | clib_error_report (e); |
| 382 | |
| 383 | sleep_time = 20.0; |
| 384 | dead_client_scan_time = vlib_time_now(vm) + 20.0; |
| 385 | |
| 386 | /* $$$ pay attention to frame size, control CPU usage */ |
| 387 | while (1) { |
| 388 | uword event_type __attribute__((unused)); |
| 389 | i8 *headp; |
| 390 | int need_broadcast; |
| 391 | |
| 392 | /* |
| 393 | * There's a reason for checking the queue before |
| 394 | * sleeping. If the vlib application crashes, it's entirely |
| 395 | * possible for a client to enqueue a connect request |
| 396 | * during the process restart interval. |
| 397 | * |
| 398 | * Unless some force of physics causes the new incarnation |
| 399 | * of the application to process the request, the client will |
| 400 | * sit and wait for Godot... |
| 401 | */ |
| 402 | vector_rate = vlib_last_vector_length_per_node(vm); |
| 403 | start_time = vlib_time_now (vm); |
| 404 | while (1) { |
| 405 | pthread_mutex_lock (&q->mutex); |
| 406 | if (q->cursize == 0) { |
Dave Barach | dae88b9 | 2016-04-19 09:38:35 -0400 | [diff] [blame^] | 407 | vm->api_queue_nonempty = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | pthread_mutex_unlock (&q->mutex); |
| 409 | |
| 410 | if (TRACE_VLIB_MEMORY_QUEUE) |
| 411 | { |
| 412 | ELOG_TYPE_DECLARE (e) = { |
| 413 | .format = "q-underflow: len %d", |
| 414 | .format_args = "i4", |
| 415 | }; |
| 416 | struct { u32 len; } * ed; |
| 417 | ed = ELOG_DATA (&vm->elog_main, e); |
| 418 | ed->len = 0; |
| 419 | } |
| 420 | sleep_time = 20.0; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | headp = (i8 *) (q->data + sizeof(uword)*q->head); |
| 425 | memcpy (&mp, headp, sizeof(uword)); |
| 426 | |
| 427 | q->head++; |
| 428 | need_broadcast = (q->cursize == q->maxsize/2); |
| 429 | q->cursize--; |
| 430 | |
| 431 | if (PREDICT_FALSE(q->head == q->maxsize)) |
| 432 | q->head = 0; |
| 433 | pthread_mutex_unlock(&q->mutex); |
| 434 | if (need_broadcast) |
| 435 | (void) pthread_cond_broadcast(&q->condvar); |
| 436 | |
| 437 | vl_msg_api_handler_with_vm_node (am, (void *)mp, vm, node); |
| 438 | |
| 439 | /* Allow no more than 10us without a pause */ |
| 440 | if (vlib_time_now(vm) > start_time + 10e-6) { |
| 441 | int index = SLEEP_400_US; |
| 442 | if (vector_rate > 40.0) |
| 443 | sleep_time = 400e-6; |
| 444 | else if (vector_rate > 20.0) { |
| 445 | index = SLEEP_200_US; |
| 446 | sleep_time = 200e-6; |
| 447 | } else if (vector_rate >= 1.0) { |
| 448 | index = SLEEP_100_US; |
| 449 | sleep_time = 100e-6; |
| 450 | } |
| 451 | else { |
| 452 | index = SLEEP_10_US; |
| 453 | sleep_time = 10e-6; |
| 454 | } |
| 455 | vector_rate_histogram[index] += 1; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time); |
| 461 | vlib_process_get_events (vm, 0 /* event_data */); |
| 462 | |
| 463 | if (vlib_time_now (vm) > dead_client_scan_time) { |
| 464 | vl_api_registration_t **regpp; |
| 465 | vl_api_registration_t *regp; |
| 466 | unix_shared_memory_queue_t *q; |
| 467 | static u32 * dead_indices; |
| 468 | static u32 * confused_indices; |
| 469 | |
| 470 | vec_reset_length (dead_indices); |
| 471 | vec_reset_length (confused_indices); |
| 472 | |
| 473 | pool_foreach (regpp, am->vl_clients, |
| 474 | ({ |
| 475 | regp = *regpp; |
| 476 | |
| 477 | if (regp) { |
| 478 | q = regp->vl_input_queue; |
| 479 | if (kill (q->consumer_pid, 0) < 0) { |
| 480 | vec_add1(dead_indices, regpp - am->vl_clients); |
| 481 | } |
| 482 | } else { |
| 483 | clib_warning ("NULL client registration index %d", |
| 484 | regpp - am->vl_clients); |
| 485 | vec_add1 (confused_indices, regpp - am->vl_clients); |
| 486 | } |
| 487 | })); |
| 488 | /* This should "never happen," but if it does, fix it... */ |
| 489 | if (PREDICT_FALSE (vec_len(confused_indices) > 0)) { |
| 490 | int i; |
| 491 | for (i = 0; i < vec_len (confused_indices); i++) { |
| 492 | pool_put_index (am->vl_clients, confused_indices[i]); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (PREDICT_FALSE (vec_len(dead_indices) > 0)) { |
| 497 | int i; |
| 498 | svm_region_t *svm; |
| 499 | void * oldheap; |
| 500 | |
| 501 | /* Allow the application to clean up its registrations */ |
| 502 | for (i = 0; i < vec_len(dead_indices); i++) { |
| 503 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 504 | if (regpp) { |
| 505 | u32 handle; |
| 506 | |
| 507 | handle = vl_msg_api_handle_from_index_and_epoch |
| 508 | (dead_indices[i], shm->application_restarts); |
| 509 | (void) vl_api_memclnt_delete_callback (handle); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | svm = am->vlib_rp; |
| 514 | pthread_mutex_lock (&svm->mutex); |
| 515 | oldheap = svm_push_data_heap(svm); |
| 516 | |
| 517 | for (i = 0; i < vec_len(dead_indices); i++) { |
| 518 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 519 | if (regpp) { |
| 520 | /* Poison the old registration */ |
| 521 | memset (*regpp, 0xF3, sizeof (**regpp)); |
| 522 | clib_mem_free (*regpp); |
| 523 | /* no dangling references, please */ |
| 524 | *regpp = 0; |
| 525 | } else { |
| 526 | svm_pop_heap (oldheap); |
| 527 | clib_warning ("Duplicate free, client index %d", |
| 528 | regpp - am->vl_clients); |
| 529 | oldheap = svm_push_data_heap(svm); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | svm_client_scan_this_region_nolock (am->vlib_rp); |
| 534 | |
| 535 | pthread_mutex_unlock(&svm->mutex); |
| 536 | svm_pop_heap (oldheap); |
| 537 | for (i = 0; i < vec_len (dead_indices); i++) |
| 538 | pool_put_index (am->vl_clients, dead_indices[i]); |
| 539 | } |
| 540 | |
| 541 | dead_client_scan_time = vlib_time_now (vm) + 20.0; |
| 542 | } |
| 543 | |
| 544 | if (TRACE_VLIB_MEMORY_QUEUE) |
| 545 | { |
| 546 | ELOG_TYPE_DECLARE (e) = { |
| 547 | .format = "q-awake: len %d", |
| 548 | .format_args = "i4", |
| 549 | }; |
| 550 | struct { u32 len; } * ed; |
| 551 | ed = ELOG_DATA (&vm->elog_main, e); |
| 552 | ed->len = q->cursize; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static clib_error_t * |
| 560 | vl_api_show_histogram_command(vlib_main_t * vm, |
| 561 | unformat_input_t * input, |
| 562 | vlib_cli_command_t * cli_cmd) |
| 563 | { |
| 564 | u64 total_counts = 0; |
| 565 | int i; |
| 566 | |
| 567 | for (i = 0; i < SLEEP_N_BUCKETS; i++) { |
| 568 | total_counts += vector_rate_histogram [i]; |
| 569 | } |
| 570 | |
| 571 | if (total_counts == 0) { |
| 572 | vlib_cli_output (vm, "No control-plane activity."); |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | #define _(n) \ |
| 577 | do { \ |
| 578 | f64 percent; \ |
| 579 | percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \ |
| 580 | / (f64) total_counts; \ |
| 581 | percent *= 100.0; \ |
| 582 | vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \ |
| 583 | vector_rate_histogram[SLEEP_##n##_US], \ |
| 584 | percent); \ |
| 585 | } while (0); |
| 586 | foreach_histogram_bucket; |
| 587 | #undef _ |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = { |
| 593 | .path = "show api histogram", |
| 594 | .short_help = "show api histogram", |
| 595 | .function = vl_api_show_histogram_command, |
| 596 | }; |
| 597 | |
| 598 | static clib_error_t * |
| 599 | vl_api_clear_histogram_command(vlib_main_t * vm, |
| 600 | unformat_input_t * input, |
| 601 | vlib_cli_command_t * cli_cmd) |
| 602 | { |
| 603 | int i; |
| 604 | |
| 605 | for (i = 0; i < SLEEP_N_BUCKETS; i++) |
| 606 | vector_rate_histogram[i] = 0; |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) = { |
| 611 | .path = "clear api histogram", |
| 612 | .short_help = "clear api histogram", |
| 613 | .function = vl_api_clear_histogram_command, |
| 614 | }; |
| 615 | |
| 616 | |
| 617 | VLIB_REGISTER_NODE (memclnt_node,static) = { |
| 618 | .function = memclnt_process, |
| 619 | .type = VLIB_NODE_TYPE_PROCESS, |
| 620 | .name = "api-rx-from-ring", |
| 621 | .state = VLIB_NODE_STATE_DISABLED, |
| 622 | }; |
| 623 | |
| 624 | static void |
| 625 | memclnt_queue_signal (int signum) |
| 626 | { |
| 627 | vlib_main_t * vm = vlib_get_main(); |
| 628 | |
| 629 | vm->queue_signal_pending = 1; |
Dave Barach | dae88b9 | 2016-04-19 09:38:35 -0400 | [diff] [blame^] | 630 | vm->api_queue_nonempty = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static void |
| 634 | memclnt_queue_callback (vlib_main_t *vm) |
| 635 | { |
| 636 | #if 0 |
| 637 | /* If we need to manually suspend / resume the memclnt process */ |
| 638 | vlib_node_t * n = vlib_get_node (vm, memclnt_node.index); |
| 639 | vlib_process_t * p = vlib_get_process_from_node (vm, n); |
| 640 | #endif |
| 641 | |
| 642 | vm->queue_signal_pending = 0; |
| 643 | vlib_process_signal_event |
| 644 | (vm, memclnt_node.index, /* event_type */ 0, /* event_data */ 0); |
| 645 | } |
| 646 | |
| 647 | void vl_enable_disable_memory_api (vlib_main_t *vm, int enable) |
| 648 | { |
| 649 | vlib_node_set_state (vm, memclnt_node.index, |
| 650 | (enable |
| 651 | ? VLIB_NODE_STATE_POLLING |
| 652 | : VLIB_NODE_STATE_DISABLED)); |
| 653 | } |
| 654 | |
| 655 | static uword |
| 656 | api_rx_from_node (vlib_main_t * vm, |
| 657 | vlib_node_runtime_t * node, |
| 658 | vlib_frame_t * frame) |
| 659 | { |
| 660 | uword n_packets = frame->n_vectors; |
| 661 | uword n_left_from; |
| 662 | u32 * from; |
| 663 | static u8 * long_msg; |
| 664 | |
| 665 | vec_validate (long_msg, 4095); |
| 666 | n_left_from = frame->n_vectors; |
| 667 | from = vlib_frame_args (frame); |
| 668 | |
| 669 | while (n_left_from > 0) { |
| 670 | u32 bi0; |
| 671 | vlib_buffer_t * b0; |
| 672 | void * msg; |
| 673 | uword msg_len; |
| 674 | |
| 675 | bi0 = from[0]; |
| 676 | b0 = vlib_get_buffer (vm, bi0); |
| 677 | from += 1; |
| 678 | n_left_from -= 1; |
| 679 | |
| 680 | msg = b0->data + b0->current_data; |
| 681 | msg_len = b0->current_length; |
| 682 | if (b0->flags & VLIB_BUFFER_NEXT_PRESENT) { |
| 683 | ASSERT (long_msg != 0); |
| 684 | _vec_len (long_msg) = 0; |
| 685 | vec_add (long_msg, msg, msg_len); |
| 686 | while (b0->flags & VLIB_BUFFER_NEXT_PRESENT) { |
| 687 | b0 = vlib_get_buffer (vm, b0->next_buffer); |
| 688 | msg = b0->data + b0->current_data; |
| 689 | msg_len = b0->current_length; |
| 690 | vec_add (long_msg, msg, msg_len); |
| 691 | } |
| 692 | msg = long_msg; |
| 693 | } |
| 694 | vl_msg_api_handler_no_trace_no_free (msg); |
| 695 | } |
| 696 | |
| 697 | /* Free what we've been given. */ |
| 698 | vlib_buffer_free (vm, vlib_frame_args (frame), n_packets); |
| 699 | |
| 700 | return n_packets; |
| 701 | } |
| 702 | |
| 703 | VLIB_REGISTER_NODE (api_rx_from_node_node,static) = { |
| 704 | .function = api_rx_from_node, |
| 705 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 706 | .vector_size = 4, |
| 707 | .name = "api-rx-from-node", |
| 708 | }; |
| 709 | |
| 710 | static clib_error_t * |
| 711 | setup_memclnt_exit (vlib_main_t * vm) |
| 712 | { |
| 713 | atexit (vl_unmap_shmem); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | VLIB_INIT_FUNCTION (setup_memclnt_exit); |
| 718 | |
| 719 | |
| 720 | static clib_error_t * |
| 721 | vl_api_ring_command(vlib_main_t * vm, |
| 722 | unformat_input_t * input, |
| 723 | vlib_cli_command_t * cli_cmd) |
| 724 | { |
| 725 | int i; |
| 726 | ring_alloc_t *ap; |
| 727 | vl_shmem_hdr_t *shmem_hdr; |
| 728 | api_main_t *am = &api_main; |
| 729 | |
| 730 | shmem_hdr = am->shmem_hdr; |
| 731 | |
| 732 | if (shmem_hdr == 0) { |
| 733 | vlib_cli_output (vm, "Shared memory segment not initialized...\n"); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n", |
| 738 | "Owner", "Size", "Nitems", "Hits", "Misses"); |
| 739 | |
| 740 | ap = shmem_hdr->vl_rings; |
| 741 | |
| 742 | for (i = 0; i < vec_len(shmem_hdr->vl_rings); i++) { |
| 743 | vlib_cli_output(vm, "%8s %8d %8d %8d %8d\n", |
| 744 | "vlib", ap->size, ap->nitems, ap->hits, ap->misses); |
| 745 | ap++; |
| 746 | } |
| 747 | |
| 748 | ap = shmem_hdr->client_rings; |
| 749 | |
| 750 | for (i = 0; i < vec_len(shmem_hdr->client_rings); i++) { |
| 751 | vlib_cli_output(vm, "%8s %8d %8d %8d %8d\n", |
| 752 | "clnt", ap->size, ap->nitems, ap->hits, ap->misses); |
| 753 | ap++; |
| 754 | } |
| 755 | |
| 756 | vlib_cli_output (vm, "%d ring miss fallback allocations\n", |
| 757 | am->ring_misses); |
| 758 | |
| 759 | vlib_cli_output (vm, "%d application restarts, %d reclaimed msgs\n", |
| 760 | shmem_hdr->application_restarts, |
| 761 | shmem_hdr->restart_reclaims); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | void dump_socket_clients (vlib_main_t *vm, api_main_t *am) |
| 766 | __attribute__((weak)); |
| 767 | |
| 768 | void dump_socket_clients (vlib_main_t *vm, api_main_t *am) |
| 769 | { |
| 770 | } |
| 771 | |
| 772 | static clib_error_t * |
| 773 | vl_api_client_command(vlib_main_t * vm, |
| 774 | unformat_input_t * input, |
| 775 | vlib_cli_command_t * cli_cmd) |
| 776 | { |
| 777 | vl_api_registration_t **regpp, *regp; |
| 778 | unix_shared_memory_queue_t *q; |
| 779 | char *health; |
| 780 | api_main_t *am = &api_main; |
| 781 | u32 * confused_indices = 0; |
| 782 | |
| 783 | if (!pool_elts (am->vl_clients)) |
| 784 | goto socket_clients; |
| 785 | vlib_cli_output (vm, "Shared memory clients"); |
| 786 | vlib_cli_output (vm, "%16s %8s %14s %18s %s", |
| 787 | "Name", "PID", "Queue Length", "Queue VA", "Health"); |
| 788 | |
| 789 | pool_foreach (regpp, am->vl_clients, |
| 790 | ({ |
| 791 | regp = *regpp; |
| 792 | |
| 793 | if (regp) { |
| 794 | q = regp->vl_input_queue; |
| 795 | if (kill (q->consumer_pid, 0) < 0) { |
| 796 | health = "DEAD"; |
| 797 | } else { |
| 798 | health = "alive"; |
| 799 | } |
| 800 | vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n", |
| 801 | regp->name, q->consumer_pid, q->cursize, |
| 802 | q, health); |
| 803 | } else { |
| 804 | clib_warning ("NULL client registration index %d", |
| 805 | regpp - am->vl_clients); |
| 806 | vec_add1 (confused_indices, regpp - am->vl_clients); |
| 807 | } |
| 808 | })); |
| 809 | |
| 810 | /* This should "never happen," but if it does, fix it... */ |
| 811 | if (PREDICT_FALSE (vec_len(confused_indices) > 0)) { |
| 812 | int i; |
| 813 | for (i = 0; i < vec_len (confused_indices); i++) { |
| 814 | pool_put_index (am->vl_clients, confused_indices[i]); |
| 815 | } |
| 816 | } |
| 817 | vec_free (confused_indices); |
| 818 | |
| 819 | if (am->missing_clients) |
| 820 | vlib_cli_output (vm, "%u messages with missing clients", |
| 821 | am->missing_clients); |
| 822 | socket_clients: |
| 823 | dump_socket_clients (vm, am); |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | VLIB_CLI_COMMAND (cli_show_api_command, static) = { |
| 829 | .path = "show api", |
| 830 | .short_help = "Show API information", |
| 831 | }; |
| 832 | |
| 833 | VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = { |
| 834 | .path = "show api ring-stats", |
| 835 | .short_help = "Message ring statistics", |
| 836 | .function = vl_api_ring_command, |
| 837 | }; |
| 838 | |
| 839 | VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = { |
| 840 | .path = "show api clients", |
| 841 | .short_help = "Client information", |
| 842 | .function = vl_api_client_command, |
| 843 | }; |
| 844 | |
| 845 | static clib_error_t * |
| 846 | vl_api_message_table_command(vlib_main_t * vm, |
| 847 | unformat_input_t * input, |
| 848 | vlib_cli_command_t * cli_cmd) |
| 849 | { |
| 850 | api_main_t *am = &api_main; |
| 851 | int i; |
| 852 | int verbose = 0; |
| 853 | |
| 854 | if (unformat (input, "verbose")) |
| 855 | verbose = 1; |
| 856 | |
| 857 | |
| 858 | if (verbose == 0) |
| 859 | vlib_cli_output (vm, "%-4s %s", "ID", "Name"); |
| 860 | else |
| 861 | vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce", |
| 862 | "MP-safe"); |
| 863 | |
| 864 | for (i = 1; i < vec_len (am->msg_names); i++) { |
| 865 | if (verbose == 0) { |
| 866 | vlib_cli_output (vm, "%-4d %s", i, |
| 867 | am->msg_names[i] ? am->msg_names[i] : |
| 868 | " [no handler]"); |
| 869 | } else { |
| 870 | vlib_cli_output (vm, "%-4d %-40s %6d %7d", i, |
| 871 | am->msg_names[i] ? am->msg_names[i] : |
| 872 | " [no handler]", am->message_bounce[i], |
| 873 | am->is_mp_safe[i]); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = { |
| 881 | .path = "show api message-table", |
| 882 | .short_help = "Message Table", |
| 883 | .function = vl_api_message_table_command, |
| 884 | }; |
| 885 | |
| 886 | void vl_api_trace_print_file_cmd(vlib_main_t *vm, u32 first, u32 last, |
| 887 | u8 *filename) |
| 888 | { |
| 889 | FILE *fp; |
| 890 | static vl_api_trace_t *tp = 0; |
| 891 | int endian_swap = 0; |
| 892 | u32 i; |
| 893 | u16 msg_id; |
| 894 | static u8 *msg_buf=0; |
| 895 | void (*endian_fp)(void *); |
| 896 | u8 *(*print_fp)(void *, void *); |
| 897 | int size; |
| 898 | api_main_t *am = &api_main; |
| 899 | |
| 900 | /* |
| 901 | * On-demand: allocate enough space for the largest message |
| 902 | */ |
| 903 | if (msg_buf == 0) { |
| 904 | vec_validate(tp, 0); |
| 905 | int max_size = 0; |
| 906 | for (i = 0; i < vec_len(am->api_trace_cfg); i++) { |
| 907 | if (am->api_trace_cfg[i].size > max_size) |
| 908 | max_size = am->api_trace_cfg[i].size; |
| 909 | } |
| 910 | /* round size to a multiple of the cache-line size */ |
| 911 | max_size = (max_size + (CLIB_CACHE_LINE_BYTES-1)) & |
| 912 | (~(CLIB_CACHE_LINE_BYTES-1)); |
| 913 | vec_validate (msg_buf, max_size-1); |
| 914 | } |
| 915 | |
| 916 | fp = fopen ((char *)filename, "r"); |
| 917 | |
| 918 | if (fp == NULL) { |
| 919 | vlib_cli_output (vm, "Couldn't open %s\n", filename); |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | /* first, fish the header record from the file */ |
| 924 | |
| 925 | if (fread(tp, sizeof(*tp), 1, fp) != 1) { |
| 926 | fclose(fp); |
| 927 | vlib_cli_output (vm, "Header read error\n"); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | /* Endian swap required? */ |
| 932 | if (clib_arch_is_big_endian != tp->endian) { |
| 933 | endian_swap = 1; |
| 934 | } |
| 935 | |
| 936 | for (i = 0; i <= last; i++) { |
| 937 | /* First 2 bytes are the message type */ |
| 938 | if (fread(&msg_id, sizeof(u16), 1, fp) != 1) { |
| 939 | break; |
| 940 | } |
| 941 | msg_id = ntohs(msg_id); |
| 942 | |
| 943 | fseek(fp, -2, SEEK_CUR); |
| 944 | |
| 945 | /* Mild sanity check */ |
| 946 | if (msg_id >= vec_len(am->msg_handlers)) { |
| 947 | fclose(fp); |
| 948 | vlib_cli_output (vm, "msg_id %d out of bounds\n", |
| 949 | msg_id); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | size = am->api_trace_cfg[msg_id].size; |
| 954 | |
| 955 | if (fread(msg_buf, size, 1, fp) != 1) { |
| 956 | fclose(fp); |
| 957 | vlib_cli_output (vm, "read error on %s\n", filename); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | if (i < first) |
| 962 | continue; |
| 963 | |
| 964 | if (endian_swap) { |
| 965 | endian_fp = am->msg_endian_handlers[msg_id]; |
| 966 | (*endian_fp)(msg_buf); |
| 967 | } |
| 968 | |
| 969 | vlib_cli_output (vm, "[%d]: %s\n", i, |
| 970 | am->msg_names[msg_id]); |
| 971 | |
| 972 | print_fp = (void *)am->msg_print_handlers[msg_id]; |
| 973 | (*print_fp)(msg_buf, vm); |
| 974 | vlib_cli_output (vm, "-------------\n"); |
| 975 | } |
| 976 | fclose(fp); |
| 977 | } |
| 978 | |
| 979 | static clib_error_t * |
| 980 | vl_api_trace_command(vlib_main_t * vm, |
| 981 | unformat_input_t * input, |
| 982 | vlib_cli_command_t * cli_cmd) |
| 983 | { |
| 984 | u32 nitems=1024; |
| 985 | vl_api_trace_which_t which=VL_API_TRACE_RX; |
| 986 | u8 *filename; |
| 987 | u32 first = 0; |
| 988 | u32 last = ~0; |
| 989 | api_main_t *am = &api_main; |
| 990 | |
| 991 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { |
| 992 | if (unformat (input, "rx nitems %u", &nitems) |
| 993 | || unformat (input, "rx")) |
| 994 | goto configure; |
| 995 | else if (unformat (input, "tx nitems %u", &nitems) |
| 996 | || unformat (input, "tx")) { |
| 997 | which = VL_API_TRACE_RX; |
| 998 | goto configure; |
| 999 | } else if (unformat (input, "on rx")) { |
| 1000 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); |
| 1001 | } else if (unformat (input, "on tx")) { |
| 1002 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1); |
| 1003 | } else if (unformat (input, "on")) { |
| 1004 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); |
| 1005 | } else if (unformat (input, "off")) { |
| 1006 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); |
| 1007 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); |
| 1008 | } else if (unformat (input, "free")) { |
| 1009 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); |
| 1010 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); |
| 1011 | vl_msg_api_trace_free (am, VL_API_TRACE_RX); |
| 1012 | vl_msg_api_trace_free (am, VL_API_TRACE_TX); |
| 1013 | } else if (unformat (input, "print %s from %d to %d", &filename, |
| 1014 | &first, &last) |
| 1015 | || unformat (input, "print %s", &filename)) { |
| 1016 | goto print; |
| 1017 | } else if (unformat (input, "debug on")) { |
| 1018 | am->msg_print_flag = 1; |
| 1019 | } else if (unformat (input, "debug off")) { |
| 1020 | am->msg_print_flag = 0; |
| 1021 | } |
| 1022 | else |
| 1023 | return clib_error_return (0, "unknown input `%U'", |
| 1024 | format_unformat_error, input); |
| 1025 | } |
| 1026 | return 0; |
| 1027 | |
| 1028 | print: |
| 1029 | vl_api_trace_print_file_cmd (vm, first, last, filename); |
| 1030 | goto out; |
| 1031 | |
| 1032 | configure: |
| 1033 | if (vl_msg_api_trace_configure (am, which, nitems)) { |
| 1034 | vlib_cli_output (vm, "warning: trace configure error (%d, %d)", |
| 1035 | which, nitems); |
| 1036 | } |
| 1037 | |
| 1038 | out: |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | VLIB_CLI_COMMAND (trace, static) = { |
| 1043 | .path = "set api-trace", |
| 1044 | .short_help = "API trace", |
| 1045 | .function = vl_api_trace_command, |
| 1046 | }; |
| 1047 | |
| 1048 | clib_error_t * |
| 1049 | vlibmemory_init (vlib_main_t * vm) |
| 1050 | { |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 1051 | api_main_t *am = &api_main; |
| 1052 | /* Normally NULL, can be set by cmd line "chroot {prefix foo}" */ |
| 1053 | svm_region_init_chroot (am->root_path); |
| 1054 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | VLIB_INIT_FUNCTION (vlibmemory_init); |
| 1058 | |
| 1059 | void vl_set_memory_region_name (char *name) |
| 1060 | { |
| 1061 | api_main_t *am = &api_main; |
| 1062 | |
| 1063 | am->region_name = name; |
| 1064 | } |
| 1065 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1066 | static int range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1) |
| 1067 | { |
| 1068 | int len0, len1, clen; |
| 1069 | |
| 1070 | len0 = vec_len (a0->name); |
| 1071 | len1 = vec_len (a1->name); |
| 1072 | clen = len0 < len1 ? len0 : len1; |
| 1073 | return (strncmp ((char *) a0->name, (char *)a1->name, clen)); |
| 1074 | } |
| 1075 | |
| 1076 | static u8 * format_api_msg_range (u8 * s, va_list * args) |
| 1077 | { |
| 1078 | vl_api_msg_range_t * rp = va_arg (*args, vl_api_msg_range_t *); |
| 1079 | |
| 1080 | if (rp == 0) |
| 1081 | s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID"); |
| 1082 | else |
| 1083 | s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id, |
| 1084 | rp->last_msg_id); |
| 1085 | |
| 1086 | return s; |
| 1087 | } |
| 1088 | |
| 1089 | static clib_error_t * |
| 1090 | vl_api_show_plugin_command(vlib_main_t * vm, |
| 1091 | unformat_input_t * input, |
| 1092 | vlib_cli_command_t * cli_cmd) |
| 1093 | { |
| 1094 | api_main_t *am = &api_main; |
| 1095 | vl_api_msg_range_t * rp = 0; |
| 1096 | int i; |
| 1097 | |
| 1098 | if (vec_len (am->msg_ranges) == 0) { |
| 1099 | vlib_cli_output (vm, "No plugin API message ranges configured..."); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | rp = vec_dup (am->msg_ranges); |
| 1104 | |
| 1105 | vec_sort_with_function (rp, range_compare); |
| 1106 | |
| 1107 | vlib_cli_output (vm, "Plugin API message ID ranges...\n"); |
| 1108 | vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */); |
| 1109 | |
| 1110 | for (i = 0; i < vec_len (rp); i++) |
| 1111 | vlib_cli_output (vm, "%U", format_api_msg_range, rp+i); |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = { |
| 1117 | .path = "show api plugin", |
| 1118 | .short_help = "show api plugin", |
| 1119 | .function = vl_api_show_plugin_command, |
| 1120 | }; |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1121 | |
| 1122 | static void vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp) |
| 1123 | { |
| 1124 | vl_api_rpc_reply_t * rmp; |
| 1125 | int (*fp)(void *); |
| 1126 | i32 rv = 0; |
| 1127 | vlib_main_t * vm = vlib_get_main(); |
| 1128 | |
| 1129 | if (mp->function == 0) |
| 1130 | { |
| 1131 | rv = -1; |
| 1132 | clib_warning ("rpc NULL function pointer"); |
| 1133 | } |
| 1134 | |
| 1135 | else |
| 1136 | { |
| 1137 | if (mp->need_barrier_sync) |
| 1138 | vlib_worker_thread_barrier_sync (vm); |
| 1139 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 1140 | fp = uword_to_pointer(mp->function, int(*)(void *)); |
| 1141 | rv = fp(mp->data); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1142 | |
| 1143 | if (mp->need_barrier_sync) |
| 1144 | vlib_worker_thread_barrier_release (vm); |
| 1145 | } |
| 1146 | |
| 1147 | if (mp->send_reply) |
| 1148 | { |
| 1149 | unix_shared_memory_queue_t * q = |
| 1150 | vl_api_client_index_to_input_queue (mp->client_index); |
| 1151 | if (q) |
| 1152 | { |
| 1153 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
| 1154 | rmp->_vl_msg_id = ntohs (VL_API_RPC_REPLY); |
| 1155 | rmp->context = mp->context; |
| 1156 | rmp->retval = rv; |
| 1157 | vl_msg_api_send_shmem (q, (u8 *)&rmp); |
| 1158 | } |
| 1159 | } |
| 1160 | if (mp->multicast) |
| 1161 | { |
| 1162 | clib_warning ("multicast not yet implemented..."); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static void vl_api_rpc_reply_t_handler (vl_api_rpc_reply_t * mp) |
| 1167 | { clib_warning ("unimplemented"); } |
| 1168 | |
| 1169 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length) |
| 1170 | { |
| 1171 | vl_api_rpc_call_t * mp; |
| 1172 | api_main_t *am = &api_main; |
| 1173 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
| 1174 | |
| 1175 | mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length); |
| 1176 | memset (mp, 0, sizeof (*mp)); |
| 1177 | memcpy (mp->data, data, data_length); |
| 1178 | mp->_vl_msg_id = ntohs (VL_API_RPC_CALL); |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 1179 | mp->function = pointer_to_uword(fp); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1180 | mp->need_barrier_sync = 1; |
| 1181 | |
| 1182 | /* Use the "normal" control-plane mechanism for the main thread */ |
| 1183 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp); |
| 1184 | } |
| 1185 | |
| 1186 | #define foreach_rpc_api_msg \ |
| 1187 | _(RPC_CALL,rpc_call) \ |
| 1188 | _(RPC_REPLY,rpc_reply) |
| 1189 | |
| 1190 | static clib_error_t * |
| 1191 | rpc_api_hookup (vlib_main_t *vm) |
| 1192 | { |
| 1193 | #define _(N,n) \ |
| 1194 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1195 | vl_api_##n##_t_handler, \ |
| 1196 | vl_noop_handler, \ |
| 1197 | vl_noop_handler, \ |
| 1198 | vl_api_##n##_t_print, \ |
| 1199 | sizeof(vl_api_##n##_t), 0 /* do not trace */); |
| 1200 | foreach_rpc_api_msg; |
| 1201 | #undef _ |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | VLIB_API_INIT_FUNCTION(rpc_api_hookup); |