blob: 752f50717b0c20b144c8b01ab4d448e95c35b11c [file] [log] [blame]
Florin Corase86a8ed2018-01-05 03:20:25 -08001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2018 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17#include <signal.h>
18
19#include <vlib/vlib.h>
20#include <vlibapi/api.h>
21#include <vlibmemory/api.h>
22#include <vlibmemory/memory_api.h>
23
24#include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
25
26#define vl_typedefs /* define message structures */
27#include <vlibmemory/vl_memory_api_h.h>
28#undef vl_typedefs
29
30/* instantiate all the print functions we know about */
31#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
32#define vl_printfun
33#include <vlibmemory/vl_memory_api_h.h>
34#undef vl_printfun
35
36/* instantiate all the endian swap functions we know about */
37#define vl_endianfun
38#include <vlibmemory/vl_memory_api_h.h>
39#undef vl_endianfun
40
41static inline void *
42vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
43{
44 vl_print (handle, "vl_api_memclnt_create_t:\n");
45 vl_print (handle, "name: %s\n", a->name);
46 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
47 vl_print (handle, "context: %u\n", (unsigned) a->context);
48 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
49 return handle;
50}
51
52static inline void *
53vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
54{
55 vl_print (handle, "vl_api_memclnt_delete_t:\n");
56 vl_print (handle, "index: %u\n", (unsigned) a->index);
57 vl_print (handle, "handle: 0x%wx\n", a->handle);
58 return handle;
59}
60
61volatile int **vl_api_queue_cursizes;
62
63static void
64memclnt_queue_callback (vlib_main_t * vm)
65{
66 int i;
67 api_main_t *am = &api_main;
68
69 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
70 1 + vec_len (am->vlib_private_rps)))
71 {
72 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
73 svm_queue_t *q;
74
75 if (shmem_hdr == 0)
76 return;
77
78 q = shmem_hdr->vl_input_queue;
79 if (q == 0)
80 return;
81
82 vec_add1 (vl_api_queue_cursizes, &q->cursize);
83
84 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
85 {
86 svm_region_t *vlib_rp = am->vlib_private_rps[i];
87
88 shmem_hdr = (void *) vlib_rp->user_ctx;
89 q = shmem_hdr->vl_input_queue;
90 vec_add1 (vl_api_queue_cursizes, &q->cursize);
91 }
92 }
93
94 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
95 {
96 if (*vl_api_queue_cursizes[i])
97 {
98 vm->queue_signal_pending = 1;
99 vm->api_queue_nonempty = 1;
100 vlib_process_signal_event (vm, vl_api_clnt_node.index,
101 /* event_type */ QUEUE_SIGNAL_EVENT,
102 /* event_data */ 0);
103 break;
104 }
105 }
106}
107
108/*
109 * vl_api_memclnt_create_internal
110 */
111u32
112vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
113{
114 vl_api_registration_t **regpp;
115 vl_api_registration_t *regp;
116 svm_region_t *svm;
117 void *oldheap;
118 api_main_t *am = &api_main;
119
120 ASSERT (vlib_get_thread_index () == 0);
121 pool_get (am->vl_clients, regpp);
122
123 svm = am->vlib_rp;
124
125 pthread_mutex_lock (&svm->mutex);
126 oldheap = svm_push_data_heap (svm);
127 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
128
129 regp = *regpp;
130 memset (regp, 0, sizeof (*regp));
131 regp->registration_type = REGISTRATION_TYPE_SHMEM;
132 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
133 regp->vlib_rp = svm;
134 regp->shmem_hdr = am->shmem_hdr;
135
136 regp->vl_input_queue = q;
137 regp->name = format (0, "%s%c", name, 0);
138
139 pthread_mutex_unlock (&svm->mutex);
140 svm_pop_heap (oldheap);
141 return vl_msg_api_handle_from_index_and_epoch
142 (regp->vl_api_registration_pool_index,
143 am->shmem_hdr->application_restarts);
144}
145
146/*
147 * vl_api_memclnt_create_t_handler
148 */
149void
150vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
151{
152 vl_api_registration_t **regpp;
153 vl_api_registration_t *regp;
154 vl_api_memclnt_create_reply_t *rp;
155 svm_region_t *svm;
156 svm_queue_t *q;
157 int rv = 0;
158 void *oldheap;
159 api_main_t *am = &api_main;
160
161 /*
162 * This is tortured. Maintain a vlib-address-space private
163 * pool of client registrations. We use the shared-memory virtual
164 * address of client structure as a handle, to allow direct
165 * manipulation of context quota vbls from the client library.
166 *
167 * This scheme causes trouble w/ API message trace replay, since
168 * some random VA from clib_mem_alloc() certainly won't
169 * occur in the Linux sim. The (very) few places
170 * that care need to use the pool index.
171 *
172 * Putting the registration object(s) into a pool in shared memory and
173 * using the pool index as a handle seems like a great idea.
174 * Unfortunately, each and every reference to that pool would need
175 * to be protected by a mutex:
176 *
177 * Client VLIB
178 * ------ ----
179 * convert pool index to
180 * pointer.
181 * <deschedule>
182 * expand pool
183 * <deschedule>
184 * kaboom!
185 */
186
187 pool_get (am->vl_clients, regpp);
188
189 svm = am->vlib_rp;
190
191 pthread_mutex_lock (&svm->mutex);
192 oldheap = svm_push_data_heap (svm);
193 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
194
195 regp = *regpp;
196 memset (regp, 0, sizeof (*regp));
197 regp->registration_type = REGISTRATION_TYPE_SHMEM;
198 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
199 regp->vlib_rp = svm;
200 regp->shmem_hdr = am->shmem_hdr;
201
202 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
203
204 regp->name = format (0, "%s", mp->name);
205 vec_add1 (regp->name, 0);
206
207 if (am->serialized_message_table_in_shmem == 0)
208 am->serialized_message_table_in_shmem =
209 vl_api_serialize_message_table (am, 0);
210
211 pthread_mutex_unlock (&svm->mutex);
212 svm_pop_heap (oldheap);
213
214 rp = vl_msg_api_alloc (sizeof (*rp));
215 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
216 rp->handle = (uword) regp;
217 rp->index = vl_msg_api_handle_from_index_and_epoch
218 (regp->vl_api_registration_pool_index,
219 am->shmem_hdr->application_restarts);
220 rp->context = mp->context;
221 rp->response = ntohl (rv);
222 rp->message_table =
223 pointer_to_uword (am->serialized_message_table_in_shmem);
224
225 vl_msg_api_send_shmem (q, (u8 *) & rp);
226}
227
228int
229vl_api_call_reaper_functions (u32 client_index)
230{
231 clib_error_t *error = 0;
232 _vl_msg_api_function_list_elt_t *i;
233
234 i = api_main.reaper_function_registrations;
235 while (i)
236 {
237 error = i->f (client_index);
238 if (error)
239 clib_error_report (error);
240 i = i->next_init_function;
241 }
242 return 0;
243}
244
245/*
246 * vl_api_memclnt_delete_t_handler
247 */
248void
249vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
250{
251 vl_api_registration_t **regpp;
252 vl_api_registration_t *regp;
253 vl_api_memclnt_delete_reply_t *rp;
254 svm_region_t *svm;
255 void *oldheap;
256 api_main_t *am = &api_main;
257 u32 handle, client_index, epoch;
258
259 handle = mp->index;
260
261 if (vl_api_call_reaper_functions (handle))
262 return;
263
264 epoch = vl_msg_api_handle_get_epoch (handle);
265 client_index = vl_msg_api_handle_get_index (handle);
266
267 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
268 {
269 clib_warning
270 ("Stale clnt delete index %d old epoch %d cur epoch %d",
271 client_index, epoch,
272 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
273 return;
274 }
275
276 regpp = am->vl_clients + client_index;
277
278 if (!pool_is_free (am->vl_clients, regpp))
279 {
280 int i;
281 regp = *regpp;
282 svm = am->vlib_rp;
283 int private_registration = 0;
284
285 /*
286 * Note: the API message handling path will set am->vlib_rp
287 * as appropriate for pairwise / private memory segments
288 */
289 rp = vl_msg_api_alloc (sizeof (*rp));
290 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
291 rp->handle = mp->handle;
292 rp->response = 1;
293
294 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
295
296 if (client_index != regp->vl_api_registration_pool_index)
297 {
298 clib_warning ("mismatch client_index %d pool_index %d",
299 client_index, regp->vl_api_registration_pool_index);
300 vl_msg_api_free (rp);
301 return;
302 }
303
304 /* For horizontal scaling, add a hash table... */
305 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
306 {
307 /* Is this a pairwise / private API segment? */
308 if (am->vlib_private_rps[i] == svm)
309 {
310 /* Note: account for the memfd header page */
311 u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
312 u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
313
314 /*
315 * Kill the registration pool element before we make
316 * the index vanish forever
317 */
318 pool_put_index (am->vl_clients,
319 regp->vl_api_registration_pool_index);
320
321 vec_delete (am->vlib_private_rps, 1, i);
322 /* Kill it, accounting for the memfd header page */
323 if (munmap ((void *) virtual_base, virtual_size) < 0)
324 clib_unix_warning ("munmap");
325 /* Reset the queue-length-address cache */
326 vec_reset_length (vl_api_queue_cursizes);
327 private_registration = 1;
328 break;
329 }
330 }
331
332 /* No dangling references, please */
333 *regpp = 0;
334
335 if (private_registration == 0)
336 {
337 pool_put_index (am->vl_clients,
338 regp->vl_api_registration_pool_index);
339 pthread_mutex_lock (&svm->mutex);
340 oldheap = svm_push_data_heap (svm);
341 /* Poison the old registration */
342 memset (regp, 0xF1, sizeof (*regp));
343 clib_mem_free (regp);
344 pthread_mutex_unlock (&svm->mutex);
345 svm_pop_heap (oldheap);
346 /*
347 * These messages must be freed manually, since they're set up
348 * as "bounce" messages. In the private_registration == 1 case,
349 * we kill the shared-memory segment which contains the message
350 * with munmap.
351 */
352 vl_msg_api_free (mp);
353 }
354 }
355 else
356 {
357 clib_warning ("unknown client ID %d", mp->index);
358 }
359}
360
361/**
362 * client answered a ping, stave off the grim reaper...
363 */
364void
365 vl_api_memclnt_keepalive_reply_t_handler
366 (vl_api_memclnt_keepalive_reply_t * mp)
367{
368 vl_api_registration_t *regp;
369 vlib_main_t *vm = vlib_get_main ();
370
371 regp = vl_api_client_index_to_registration (mp->context);
372 if (regp)
373 {
374 regp->last_heard = vlib_time_now (vm);
375 regp->unanswered_pings = 0;
376 }
377 else
378 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
379}
380
381/**
382 * We can send ourselves these messages if someone uses the
383 * builtin binary api test tool...
384 */
385static void
386vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
387{
388 vl_api_memclnt_keepalive_reply_t *rmp;
389 api_main_t *am;
390 vl_shmem_hdr_t *shmem_hdr;
391
392 am = &api_main;
393 shmem_hdr = am->shmem_hdr;
394
395 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
396 memset (rmp, 0, sizeof (*rmp));
397 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
398 rmp->context = mp->context;
399 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
400}
401
402#define foreach_vlib_api_msg \
403_(MEMCLNT_CREATE, memclnt_create) \
404_(MEMCLNT_DELETE, memclnt_delete) \
405_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
406_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply) \
407
408/*
409 * memory_api_init
410 */
411int
412vl_mem_api_init (const char *region_name)
413{
414 int rv;
415 api_main_t *am = &api_main;
416 vl_msg_api_msg_config_t cfg;
417 vl_msg_api_msg_config_t *c = &cfg;
418 vl_shmem_hdr_t *shm;
419 vlib_main_t *vm = vlib_get_main ();
420
421 memset (c, 0, sizeof (*c));
422
423 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
424 return rv;
425
426#define _(N,n) do { \
427 c->id = VL_API_##N; \
428 c->name = #n; \
429 c->handler = vl_api_##n##_t_handler; \
430 c->cleanup = vl_noop_handler; \
431 c->endian = vl_api_##n##_t_endian; \
432 c->print = vl_api_##n##_t_print; \
433 c->size = sizeof(vl_api_##n##_t); \
434 c->traced = 1; /* trace, so these msgs print */ \
435 c->replay = 0; /* don't replay client create/delete msgs */ \
436 c->message_bounce = 0; /* don't bounce this message */ \
437 vl_msg_api_config(c);} while (0);
438
439 foreach_vlib_api_msg;
440#undef _
441
442 /*
443 * special-case freeing of memclnt_delete messages, so we can
444 * simply munmap pairwise / private API segments...
445 */
446 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
447 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
448
449 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
450
451 shm = am->shmem_hdr;
452 ASSERT (shm && shm->vl_input_queue);
453
454 /* Make a note so we can always find the primary region easily */
455 am->vlib_primary_rp = am->vlib_rp;
456
457 return 0;
458}
459
460static void
461send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
462{
463 vl_api_memclnt_keepalive_t *mp;
464 svm_queue_t *q;
465 api_main_t *am = &api_main;
466 svm_region_t *save_vlib_rp = am->vlib_rp;
467 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
468
469 q = regp->vl_input_queue;
470
471 /*
472 * If the queue head is moving, assume that the client is processing
473 * messages and skip the ping. This heuristic may fail if the queue
474 * is in the same position as last time, net of wrapping; in which
475 * case, the client will receive a keepalive.
476 */
477 if (regp->last_queue_head != q->head)
478 {
479 regp->last_heard = now;
480 regp->unanswered_pings = 0;
481 regp->last_queue_head = q->head;
482 return;
483 }
484
485 /*
486 * push/pop shared memory segment, so this routine
487 * will work with "normal" as well as "private segment"
488 * memory clients..
489 */
490
491 am->vlib_rp = regp->vlib_rp;
492 am->shmem_hdr = regp->shmem_hdr;
493
494 mp = vl_msg_api_alloc (sizeof (*mp));
495 memset (mp, 0, sizeof (*mp));
496 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
497 mp->context = mp->client_index =
498 vl_msg_api_handle_from_index_and_epoch
499 (regp->vl_api_registration_pool_index,
500 am->shmem_hdr->application_restarts);
501
502 regp->unanswered_pings++;
503
504 /* Failure-to-send due to a stuffed queue is absolutely expected */
505 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
506 vl_msg_api_free (mp);
507
508 am->vlib_rp = save_vlib_rp;
509 am->shmem_hdr = save_shmem_hdr;
510}
511
512void
513vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
514{
515 vl_api_registration_t **regpp;
516 vl_api_registration_t *regp;
517 static u32 *dead_indices;
518 static u32 *confused_indices;
519
520 vec_reset_length (dead_indices);
521 vec_reset_length (confused_indices);
522
523 /* *INDENT-OFF* */
524 pool_foreach (regpp, am->vl_clients,
525 ({
526 regp = *regpp;
527 if (regp)
528 {
529 /* If we haven't heard from this client recently... */
530 if (regp->last_heard < (now - 10.0))
531 {
532 if (regp->unanswered_pings == 2)
533 {
534 svm_queue_t *q;
535 q = regp->vl_input_queue;
536 if (kill (q->consumer_pid, 0) >=0)
537 {
538 clib_warning ("REAPER: lazy binary API client '%s'",
539 regp->name);
540 regp->unanswered_pings = 0;
541 regp->last_heard = now;
542 }
543 else
544 {
545 clib_warning ("REAPER: binary API client '%s' died",
546 regp->name);
547 vec_add1(dead_indices, regpp - am->vl_clients);
548 }
549 }
550 else
551 send_memclnt_keepalive (regp, now);
552 }
553 else
554 regp->unanswered_pings = 0;
555 }
556 else
557 {
558 clib_warning ("NULL client registration index %d",
559 regpp - am->vl_clients);
560 vec_add1 (confused_indices, regpp - am->vl_clients);
561 }
562 }));
563 /* *INDENT-ON* */
564 /* This should "never happen," but if it does, fix it... */
565 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
566 {
567 int i;
568 for (i = 0; i < vec_len (confused_indices); i++)
569 {
570 pool_put_index (am->vl_clients, confused_indices[i]);
571 }
572 }
573
574 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
575 {
576 int i;
577 svm_region_t *svm;
578 void *oldheap;
579
580 /* Allow the application to clean up its registrations */
581 for (i = 0; i < vec_len (dead_indices); i++)
582 {
583 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
584 if (regpp)
585 {
586 u32 handle;
587
588 handle = vl_msg_api_handle_from_index_and_epoch
589 (dead_indices[i], shm->application_restarts);
590 (void) vl_api_call_reaper_functions (handle);
591 }
592 }
593
594 svm = am->vlib_rp;
595 pthread_mutex_lock (&svm->mutex);
596 oldheap = svm_push_data_heap (svm);
597
598 for (i = 0; i < vec_len (dead_indices); i++)
599 {
600 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
601 if (regpp)
602 {
603 /* Is this a pairwise SVM segment? */
604 if ((*regpp)->vlib_rp != svm)
605 {
606 int i;
607 svm_region_t *dead_rp = (*regpp)->vlib_rp;
608 /* Note: account for the memfd header page */
609 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
610 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
611
612 /* For horizontal scaling, add a hash table... */
613 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
614 if (am->vlib_private_rps[i] == dead_rp)
615 {
616 vec_delete (am->vlib_private_rps, 1, i);
617 goto found;
618 }
619 clib_warning ("private rp %llx AWOL", dead_rp);
620
621 found:
622 /* Kill it, accounting for the memfd header page */
623 if (munmap ((void *) virtual_base, virtual_size) < 0)
624 clib_unix_warning ("munmap");
625 /* Reset the queue-length-address cache */
626 vec_reset_length (vl_api_queue_cursizes);
627 }
628 else
629 {
630 /* Poison the old registration */
631 memset (*regpp, 0xF3, sizeof (**regpp));
632 clib_mem_free (*regpp);
633 }
634 /* no dangling references, please */
635 *regpp = 0;
636 }
637 else
638 {
639 svm_pop_heap (oldheap);
640 clib_warning ("Duplicate free, client index %d",
641 regpp - am->vl_clients);
642 oldheap = svm_push_data_heap (svm);
643 }
644 }
645
646 svm_client_scan_this_region_nolock (am->vlib_rp);
647
648 pthread_mutex_unlock (&svm->mutex);
649 svm_pop_heap (oldheap);
650 for (i = 0; i < vec_len (dead_indices); i++)
651 pool_put_index (am->vl_clients, dead_indices[i]);
652 }
653}
654
655static inline int
656void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
657 vlib_node_runtime_t * node, svm_queue_t * q)
658{
659 uword mp;
660 if (!svm_queue_sub2 (q, (u8 *) & mp))
661 {
662 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
663 return 0;
664 }
665 return -1;
666}
667
668int
669vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
670{
671 api_main_t *am = &api_main;
672 return void_mem_api_handle_msg_i (am, vm, node,
673 am->shmem_hdr->vl_input_queue);
674}
675
676int
677vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
678 u32 reg_index)
679{
680 api_main_t *am = &api_main;
681 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
682 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
683 svm_queue_t *q;
684 int rv;
685
686 vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
687
688 am->shmem_hdr = (void *) vlib_rp->user_ctx;
689 q = am->shmem_hdr->vl_input_queue;
690
691 rv = void_mem_api_handle_msg_i (am, vm, node, q);
692
693 am->shmem_hdr = save_shmem_hdr;
694 am->vlib_rp = save_vlib_rp;
695
696 return rv;
697}
698
699vl_api_registration_t *
700vl_mem_api_client_index_to_registration (u32 handle)
701{
702 vl_api_registration_t **regpp;
703 vl_api_registration_t *regp;
704 api_main_t *am = &api_main;
705 u32 index;
706
707 index = vl_msg_api_handle_get_index (handle);
708 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
709 != vl_msg_api_handle_get_epoch (handle))
710 {
711 vl_msg_api_increment_missing_client_counter ();
712 return 0;
713 }
714
715 regpp = am->vl_clients + index;
716
717 if (pool_is_free (am->vl_clients, regpp))
718 {
719 vl_msg_api_increment_missing_client_counter ();
720 return 0;
721 }
722 regp = *regpp;
723 return (regp);
724}
725
726svm_queue_t *
727vl_api_client_index_to_input_queue (u32 index)
728{
729 vl_api_registration_t *regp;
730 api_main_t *am = &api_main;
731
732 /* Special case: vlib trying to send itself a message */
733 if (index == (u32) ~ 0)
734 return (am->shmem_hdr->vl_input_queue);
735
736 regp = vl_mem_api_client_index_to_registration (index);
737 if (!regp)
738 return 0;
739 return (regp->vl_input_queue);
740}
741
742static clib_error_t *
743setup_memclnt_exit (vlib_main_t * vm)
744{
745 atexit (vl_unmap_shmem);
746 return 0;
747}
748
749VLIB_INIT_FUNCTION (setup_memclnt_exit);
750
751u8 *
752format_api_message_rings (u8 * s, va_list * args)
753{
754 api_main_t *am = va_arg (*args, api_main_t *);
755 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
756 int main_segment = va_arg (*args, int);
757 ring_alloc_t *ap;
758 int i;
759
760 if (shmem_hdr == 0)
761 return format (s, "%8s %8s %8s %8s %8s\n",
762 "Owner", "Size", "Nitems", "Hits", "Misses");
763
764 ap = shmem_hdr->vl_rings;
765
766 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
767 {
768 s = format (s, "%8s %8d %8d %8d %8d\n",
769 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
770 ap++;
771 }
772
773 ap = shmem_hdr->client_rings;
774
775 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
776 {
777 s = format (s, "%8s %8d %8d %8d %8d\n",
778 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
779 ap++;
780 }
781
782 if (main_segment)
783 {
784 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
785 s = format
786 (s,
787 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
788 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
789 shmem_hdr->garbage_collects);
790 }
791 return s;
792}
793
794static clib_error_t *
795vl_api_ring_command (vlib_main_t * vm,
796 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
797{
798 int i;
799 vl_shmem_hdr_t *shmem_hdr;
800 api_main_t *am = &api_main;
801
802 /* First, dump the primary region rings.. */
803
804 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
805 {
806 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
807 return 0;
808 }
809
810 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
811
812 vlib_cli_output (vm, "Main API segment rings:");
813
814 vlib_cli_output (vm, "%U", format_api_message_rings, am,
815 0 /* print header */ , 0 /* notused */ );
816
817 vlib_cli_output (vm, "%U", format_api_message_rings, am,
818 shmem_hdr, 1 /* main segment */ );
819
820 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
821 {
822 svm_region_t *vlib_rp = am->vlib_private_rps[i];
823 shmem_hdr = (void *) vlib_rp->user_ctx;
824 vl_api_registration_t **regpp;
825 vl_api_registration_t *regp = 0;
826
827 /* For horizontal scaling, add a hash table... */
828 /* *INDENT-OFF* */
829 pool_foreach (regpp, am->vl_clients,
830 ({
831 regp = *regpp;
832 if (regp && regp->vlib_rp == vlib_rp)
833 {
834 vlib_cli_output (vm, "%s segment rings:", regp->name);
835 goto found;
836 }
837 }));
838 vlib_cli_output (vm, "regp %llx not found?", regp);
839 continue;
840 /* *INDENT-ON* */
841 found:
842 vlib_cli_output (vm, "%U", format_api_message_rings, am,
843 0 /* print header */ , 0 /* notused */ );
844 vlib_cli_output (vm, "%U", format_api_message_rings, am,
845 shmem_hdr, 0 /* main segment */ );
846 }
847
848 return 0;
849}
850
851/*?
852 * Display binary api message allocation ring statistics
853?*/
854/* *INDENT-OFF* */
855VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
856{
857 .path = "show api ring-stats",
858 .short_help = "Message ring statistics",
859 .function = vl_api_ring_command,
860};
861/* *INDENT-ON* */
862
863clib_error_t *
864vlibmemory_init (vlib_main_t * vm)
865{
866 api_main_t *am = &api_main;
867 svm_map_region_args_t _a, *a = &_a;
868 clib_error_t *error;
869
870 memset (a, 0, sizeof (*a));
871 a->root_path = am->root_path;
872 a->name = SVM_GLOBAL_REGION_NAME;
873 a->baseva = (am->global_baseva != 0) ?
Damjan Marionaec8f892018-01-08 16:35:35 +0100874 am->global_baseva : +svm_get_global_region_base_va ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800875 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
876 a->flags = SVM_FLAGS_NODATA;
877 a->uid = am->api_uid;
878 a->gid = am->api_gid;
879 a->pvt_heap_size =
880 (am->global_pvt_heap_size !=
881 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
882
883 svm_region_init_args (a);
884
885 error = vlib_call_init_function (vm, vlibsocket_init);
886
887 return error;
888}
889
890VLIB_INIT_FUNCTION (vlibmemory_init);
891
892void
893vl_set_memory_region_name (const char *name)
894{
895 api_main_t *am = &api_main;
896 am->region_name = name;
897}
898
899/*
900 * fd.io coding-style-patch-verification: ON
901 *
902 * Local Variables:
903 * eval: (c-set-style "gnu")
904 * End:
905 */