blob: 0ee1384f22b9fbad314ae71248a4087a1b63d277 [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;
Florin Corasb384b542018-01-15 01:08:33 -0800201 regp->clib_file_index = am->shmem_hdr->clib_file_index;
Florin Corase86a8ed2018-01-05 03:20:25 -0800202
203 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
204
205 regp->name = format (0, "%s", mp->name);
206 vec_add1 (regp->name, 0);
207
208 if (am->serialized_message_table_in_shmem == 0)
209 am->serialized_message_table_in_shmem =
210 vl_api_serialize_message_table (am, 0);
211
212 pthread_mutex_unlock (&svm->mutex);
213 svm_pop_heap (oldheap);
214
215 rp = vl_msg_api_alloc (sizeof (*rp));
216 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
217 rp->handle = (uword) regp;
218 rp->index = vl_msg_api_handle_from_index_and_epoch
219 (regp->vl_api_registration_pool_index,
220 am->shmem_hdr->application_restarts);
221 rp->context = mp->context;
222 rp->response = ntohl (rv);
223 rp->message_table =
224 pointer_to_uword (am->serialized_message_table_in_shmem);
225
226 vl_msg_api_send_shmem (q, (u8 *) & rp);
227}
228
229int
230vl_api_call_reaper_functions (u32 client_index)
231{
232 clib_error_t *error = 0;
233 _vl_msg_api_function_list_elt_t *i;
234
235 i = api_main.reaper_function_registrations;
236 while (i)
237 {
238 error = i->f (client_index);
239 if (error)
240 clib_error_report (error);
241 i = i->next_init_function;
242 }
243 return 0;
244}
245
246/*
247 * vl_api_memclnt_delete_t_handler
248 */
249void
250vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
251{
252 vl_api_registration_t **regpp;
253 vl_api_registration_t *regp;
254 vl_api_memclnt_delete_reply_t *rp;
255 svm_region_t *svm;
256 void *oldheap;
257 api_main_t *am = &api_main;
258 u32 handle, client_index, epoch;
259
260 handle = mp->index;
261
262 if (vl_api_call_reaper_functions (handle))
263 return;
264
265 epoch = vl_msg_api_handle_get_epoch (handle);
266 client_index = vl_msg_api_handle_get_index (handle);
267
268 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
269 {
270 clib_warning
271 ("Stale clnt delete index %d old epoch %d cur epoch %d",
272 client_index, epoch,
273 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
274 return;
275 }
276
Florin Corasb384b542018-01-15 01:08:33 -0800277 regpp = pool_elt_at_index (am->vl_clients, client_index);
Florin Corase86a8ed2018-01-05 03:20:25 -0800278
279 if (!pool_is_free (am->vl_clients, regpp))
280 {
281 int i;
282 regp = *regpp;
283 svm = am->vlib_rp;
284 int private_registration = 0;
285
286 /*
287 * Note: the API message handling path will set am->vlib_rp
288 * as appropriate for pairwise / private memory segments
289 */
290 rp = vl_msg_api_alloc (sizeof (*rp));
291 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
292 rp->handle = mp->handle;
293 rp->response = 1;
294
295 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
296
297 if (client_index != regp->vl_api_registration_pool_index)
298 {
299 clib_warning ("mismatch client_index %d pool_index %d",
300 client_index, regp->vl_api_registration_pool_index);
301 vl_msg_api_free (rp);
302 return;
303 }
304
305 /* For horizontal scaling, add a hash table... */
306 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
307 {
308 /* Is this a pairwise / private API segment? */
309 if (am->vlib_private_rps[i] == svm)
310 {
311 /* Note: account for the memfd header page */
312 u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
313 u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
314
315 /*
316 * Kill the registration pool element before we make
317 * the index vanish forever
318 */
319 pool_put_index (am->vl_clients,
320 regp->vl_api_registration_pool_index);
321
322 vec_delete (am->vlib_private_rps, 1, i);
323 /* Kill it, accounting for the memfd header page */
324 if (munmap ((void *) virtual_base, virtual_size) < 0)
325 clib_unix_warning ("munmap");
326 /* Reset the queue-length-address cache */
327 vec_reset_length (vl_api_queue_cursizes);
328 private_registration = 1;
329 break;
330 }
331 }
332
333 /* No dangling references, please */
334 *regpp = 0;
335
336 if (private_registration == 0)
337 {
338 pool_put_index (am->vl_clients,
339 regp->vl_api_registration_pool_index);
340 pthread_mutex_lock (&svm->mutex);
341 oldheap = svm_push_data_heap (svm);
342 /* Poison the old registration */
343 memset (regp, 0xF1, sizeof (*regp));
344 clib_mem_free (regp);
345 pthread_mutex_unlock (&svm->mutex);
346 svm_pop_heap (oldheap);
347 /*
348 * These messages must be freed manually, since they're set up
349 * as "bounce" messages. In the private_registration == 1 case,
350 * we kill the shared-memory segment which contains the message
351 * with munmap.
352 */
353 vl_msg_api_free (mp);
354 }
355 }
356 else
357 {
358 clib_warning ("unknown client ID %d", mp->index);
359 }
360}
361
362/**
363 * client answered a ping, stave off the grim reaper...
364 */
365void
366 vl_api_memclnt_keepalive_reply_t_handler
367 (vl_api_memclnt_keepalive_reply_t * mp)
368{
369 vl_api_registration_t *regp;
370 vlib_main_t *vm = vlib_get_main ();
371
372 regp = vl_api_client_index_to_registration (mp->context);
373 if (regp)
374 {
375 regp->last_heard = vlib_time_now (vm);
376 regp->unanswered_pings = 0;
377 }
378 else
379 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
380}
381
382/**
383 * We can send ourselves these messages if someone uses the
384 * builtin binary api test tool...
385 */
386static void
387vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
388{
389 vl_api_memclnt_keepalive_reply_t *rmp;
390 api_main_t *am;
391 vl_shmem_hdr_t *shmem_hdr;
392
393 am = &api_main;
394 shmem_hdr = am->shmem_hdr;
395
396 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
397 memset (rmp, 0, sizeof (*rmp));
398 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
399 rmp->context = mp->context;
400 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
401}
402
403#define foreach_vlib_api_msg \
404_(MEMCLNT_CREATE, memclnt_create) \
405_(MEMCLNT_DELETE, memclnt_delete) \
406_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
407_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply) \
408
409/*
410 * memory_api_init
411 */
412int
413vl_mem_api_init (const char *region_name)
414{
415 int rv;
416 api_main_t *am = &api_main;
417 vl_msg_api_msg_config_t cfg;
418 vl_msg_api_msg_config_t *c = &cfg;
419 vl_shmem_hdr_t *shm;
420 vlib_main_t *vm = vlib_get_main ();
421
422 memset (c, 0, sizeof (*c));
423
424 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
425 return rv;
426
427#define _(N,n) do { \
428 c->id = VL_API_##N; \
429 c->name = #n; \
430 c->handler = vl_api_##n##_t_handler; \
431 c->cleanup = vl_noop_handler; \
432 c->endian = vl_api_##n##_t_endian; \
433 c->print = vl_api_##n##_t_print; \
434 c->size = sizeof(vl_api_##n##_t); \
435 c->traced = 1; /* trace, so these msgs print */ \
436 c->replay = 0; /* don't replay client create/delete msgs */ \
437 c->message_bounce = 0; /* don't bounce this message */ \
438 vl_msg_api_config(c);} while (0);
439
440 foreach_vlib_api_msg;
441#undef _
442
443 /*
444 * special-case freeing of memclnt_delete messages, so we can
445 * simply munmap pairwise / private API segments...
446 */
447 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
448 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
449
450 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
451
452 shm = am->shmem_hdr;
453 ASSERT (shm && shm->vl_input_queue);
454
455 /* Make a note so we can always find the primary region easily */
456 am->vlib_primary_rp = am->vlib_rp;
457
458 return 0;
459}
460
461static void
462send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
463{
464 vl_api_memclnt_keepalive_t *mp;
465 svm_queue_t *q;
466 api_main_t *am = &api_main;
467 svm_region_t *save_vlib_rp = am->vlib_rp;
468 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
469
470 q = regp->vl_input_queue;
471
472 /*
473 * If the queue head is moving, assume that the client is processing
474 * messages and skip the ping. This heuristic may fail if the queue
475 * is in the same position as last time, net of wrapping; in which
476 * case, the client will receive a keepalive.
477 */
478 if (regp->last_queue_head != q->head)
479 {
480 regp->last_heard = now;
481 regp->unanswered_pings = 0;
482 regp->last_queue_head = q->head;
483 return;
484 }
485
486 /*
487 * push/pop shared memory segment, so this routine
488 * will work with "normal" as well as "private segment"
489 * memory clients..
490 */
491
492 am->vlib_rp = regp->vlib_rp;
493 am->shmem_hdr = regp->shmem_hdr;
494
495 mp = vl_msg_api_alloc (sizeof (*mp));
496 memset (mp, 0, sizeof (*mp));
497 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
498 mp->context = mp->client_index =
499 vl_msg_api_handle_from_index_and_epoch
500 (regp->vl_api_registration_pool_index,
501 am->shmem_hdr->application_restarts);
502
503 regp->unanswered_pings++;
504
505 /* Failure-to-send due to a stuffed queue is absolutely expected */
506 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
507 vl_msg_api_free (mp);
508
509 am->vlib_rp = save_vlib_rp;
510 am->shmem_hdr = save_shmem_hdr;
511}
512
Florin Corasb384b542018-01-15 01:08:33 -0800513static void
514vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
515 vl_api_registration_t ** regpp,
516 u32 ** dead_indices,
517 u32 ** confused_indices)
518{
519 vl_api_registration_t *regp = *regpp;
520 if (regp)
521 {
522 /* If we haven't heard from this client recently... */
523 if (regp->last_heard < (now - 10.0))
524 {
525 if (regp->unanswered_pings == 2)
526 {
527 svm_queue_t *q;
528 q = regp->vl_input_queue;
529 if (kill (q->consumer_pid, 0) >= 0)
530 {
531 clib_warning ("REAPER: lazy binary API client '%s'",
532 regp->name);
533 regp->unanswered_pings = 0;
534 regp->last_heard = now;
535 }
536 else
537 {
538 clib_warning ("REAPER: binary API client '%s' died",
539 regp->name);
540 vec_add1 (*dead_indices, regpp - am->vl_clients);
541 }
542 }
543 else
544 send_memclnt_keepalive (regp, now);
545 }
546 else
547 regp->unanswered_pings = 0;
548 }
549 else
550 {
551 clib_warning ("NULL client registration index %d",
552 regpp - am->vl_clients);
553 vec_add1 (*confused_indices, regpp - am->vl_clients);
554 }
555}
556
Florin Corase86a8ed2018-01-05 03:20:25 -0800557void
558vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
559{
560 vl_api_registration_t **regpp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800561 static u32 *dead_indices;
562 static u32 *confused_indices;
563
564 vec_reset_length (dead_indices);
565 vec_reset_length (confused_indices);
566
567 /* *INDENT-OFF* */
Florin Corasb384b542018-01-15 01:08:33 -0800568 pool_foreach (regpp, am->vl_clients, ({
569 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
570 &confused_indices);
Florin Corase86a8ed2018-01-05 03:20:25 -0800571 }));
572 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800573
Florin Corase86a8ed2018-01-05 03:20:25 -0800574 /* This should "never happen," but if it does, fix it... */
575 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
576 {
577 int i;
578 for (i = 0; i < vec_len (confused_indices); i++)
579 {
580 pool_put_index (am->vl_clients, confused_indices[i]);
581 }
582 }
583
584 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
585 {
586 int i;
587 svm_region_t *svm;
588 void *oldheap;
589
590 /* Allow the application to clean up its registrations */
591 for (i = 0; i < vec_len (dead_indices); i++)
592 {
593 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
594 if (regpp)
595 {
596 u32 handle;
597
598 handle = vl_msg_api_handle_from_index_and_epoch
599 (dead_indices[i], shm->application_restarts);
600 (void) vl_api_call_reaper_functions (handle);
601 }
602 }
603
604 svm = am->vlib_rp;
605 pthread_mutex_lock (&svm->mutex);
606 oldheap = svm_push_data_heap (svm);
607
608 for (i = 0; i < vec_len (dead_indices); i++)
609 {
610 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
611 if (regpp)
612 {
613 /* Is this a pairwise SVM segment? */
614 if ((*regpp)->vlib_rp != svm)
615 {
616 int i;
617 svm_region_t *dead_rp = (*regpp)->vlib_rp;
618 /* Note: account for the memfd header page */
619 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
620 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
621
622 /* For horizontal scaling, add a hash table... */
623 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
624 if (am->vlib_private_rps[i] == dead_rp)
625 {
626 vec_delete (am->vlib_private_rps, 1, i);
627 goto found;
628 }
629 clib_warning ("private rp %llx AWOL", dead_rp);
630
631 found:
632 /* Kill it, accounting for the memfd header page */
633 if (munmap ((void *) virtual_base, virtual_size) < 0)
634 clib_unix_warning ("munmap");
635 /* Reset the queue-length-address cache */
636 vec_reset_length (vl_api_queue_cursizes);
637 }
638 else
639 {
640 /* Poison the old registration */
641 memset (*regpp, 0xF3, sizeof (**regpp));
642 clib_mem_free (*regpp);
643 }
644 /* no dangling references, please */
645 *regpp = 0;
646 }
647 else
648 {
649 svm_pop_heap (oldheap);
650 clib_warning ("Duplicate free, client index %d",
651 regpp - am->vl_clients);
652 oldheap = svm_push_data_heap (svm);
653 }
654 }
655
656 svm_client_scan_this_region_nolock (am->vlib_rp);
657
658 pthread_mutex_unlock (&svm->mutex);
659 svm_pop_heap (oldheap);
660 for (i = 0; i < vec_len (dead_indices); i++)
661 pool_put_index (am->vl_clients, dead_indices[i]);
662 }
663}
664
665static inline int
666void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
667 vlib_node_runtime_t * node, svm_queue_t * q)
668{
669 uword mp;
670 if (!svm_queue_sub2 (q, (u8 *) & mp))
671 {
672 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
673 return 0;
674 }
675 return -1;
676}
677
678int
679vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
680{
681 api_main_t *am = &api_main;
682 return void_mem_api_handle_msg_i (am, vm, node,
683 am->shmem_hdr->vl_input_queue);
684}
685
686int
687vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
688 u32 reg_index)
689{
690 api_main_t *am = &api_main;
691 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
692 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
693 svm_queue_t *q;
694 int rv;
695
696 vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
697
698 am->shmem_hdr = (void *) vlib_rp->user_ctx;
699 q = am->shmem_hdr->vl_input_queue;
700
701 rv = void_mem_api_handle_msg_i (am, vm, node, q);
702
703 am->shmem_hdr = save_shmem_hdr;
704 am->vlib_rp = save_vlib_rp;
705
706 return rv;
707}
708
709vl_api_registration_t *
710vl_mem_api_client_index_to_registration (u32 handle)
711{
712 vl_api_registration_t **regpp;
713 vl_api_registration_t *regp;
714 api_main_t *am = &api_main;
Florin Corasb384b542018-01-15 01:08:33 -0800715 vl_shmem_hdr_t *shmem_hdr;
Florin Corase86a8ed2018-01-05 03:20:25 -0800716 u32 index;
717
718 index = vl_msg_api_handle_get_index (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800719 regpp = am->vl_clients + index;
720
721 if (pool_is_free (am->vl_clients, regpp))
722 {
723 vl_msg_api_increment_missing_client_counter ();
724 return 0;
725 }
726 regp = *regpp;
Florin Corasb384b542018-01-15 01:08:33 -0800727
728 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
729 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
730 {
731 vl_msg_api_increment_missing_client_counter ();
732 return 0;
733 }
734
Florin Corase86a8ed2018-01-05 03:20:25 -0800735 return (regp);
736}
737
738svm_queue_t *
739vl_api_client_index_to_input_queue (u32 index)
740{
741 vl_api_registration_t *regp;
742 api_main_t *am = &api_main;
743
744 /* Special case: vlib trying to send itself a message */
745 if (index == (u32) ~ 0)
746 return (am->shmem_hdr->vl_input_queue);
747
748 regp = vl_mem_api_client_index_to_registration (index);
749 if (!regp)
750 return 0;
751 return (regp->vl_input_queue);
752}
753
754static clib_error_t *
755setup_memclnt_exit (vlib_main_t * vm)
756{
757 atexit (vl_unmap_shmem);
758 return 0;
759}
760
761VLIB_INIT_FUNCTION (setup_memclnt_exit);
762
763u8 *
764format_api_message_rings (u8 * s, va_list * args)
765{
766 api_main_t *am = va_arg (*args, api_main_t *);
767 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
768 int main_segment = va_arg (*args, int);
769 ring_alloc_t *ap;
770 int i;
771
772 if (shmem_hdr == 0)
773 return format (s, "%8s %8s %8s %8s %8s\n",
774 "Owner", "Size", "Nitems", "Hits", "Misses");
775
776 ap = shmem_hdr->vl_rings;
777
778 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
779 {
780 s = format (s, "%8s %8d %8d %8d %8d\n",
781 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
782 ap++;
783 }
784
785 ap = shmem_hdr->client_rings;
786
787 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
788 {
789 s = format (s, "%8s %8d %8d %8d %8d\n",
790 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
791 ap++;
792 }
793
794 if (main_segment)
795 {
796 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
797 s = format
798 (s,
799 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
800 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
801 shmem_hdr->garbage_collects);
802 }
803 return s;
804}
805
806static clib_error_t *
807vl_api_ring_command (vlib_main_t * vm,
808 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
809{
810 int i;
811 vl_shmem_hdr_t *shmem_hdr;
812 api_main_t *am = &api_main;
813
814 /* First, dump the primary region rings.. */
815
816 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
817 {
818 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
819 return 0;
820 }
821
822 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
823
824 vlib_cli_output (vm, "Main API segment rings:");
825
826 vlib_cli_output (vm, "%U", format_api_message_rings, am,
827 0 /* print header */ , 0 /* notused */ );
828
829 vlib_cli_output (vm, "%U", format_api_message_rings, am,
830 shmem_hdr, 1 /* main segment */ );
831
832 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
833 {
834 svm_region_t *vlib_rp = am->vlib_private_rps[i];
835 shmem_hdr = (void *) vlib_rp->user_ctx;
836 vl_api_registration_t **regpp;
837 vl_api_registration_t *regp = 0;
838
839 /* For horizontal scaling, add a hash table... */
840 /* *INDENT-OFF* */
841 pool_foreach (regpp, am->vl_clients,
842 ({
843 regp = *regpp;
844 if (regp && regp->vlib_rp == vlib_rp)
845 {
846 vlib_cli_output (vm, "%s segment rings:", regp->name);
847 goto found;
848 }
849 }));
850 vlib_cli_output (vm, "regp %llx not found?", regp);
851 continue;
852 /* *INDENT-ON* */
853 found:
854 vlib_cli_output (vm, "%U", format_api_message_rings, am,
855 0 /* print header */ , 0 /* notused */ );
856 vlib_cli_output (vm, "%U", format_api_message_rings, am,
857 shmem_hdr, 0 /* main segment */ );
858 }
859
860 return 0;
861}
862
863/*?
864 * Display binary api message allocation ring statistics
865?*/
866/* *INDENT-OFF* */
867VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
868{
869 .path = "show api ring-stats",
870 .short_help = "Message ring statistics",
871 .function = vl_api_ring_command,
872};
873/* *INDENT-ON* */
874
875clib_error_t *
876vlibmemory_init (vlib_main_t * vm)
877{
878 api_main_t *am = &api_main;
879 svm_map_region_args_t _a, *a = &_a;
880 clib_error_t *error;
881
882 memset (a, 0, sizeof (*a));
883 a->root_path = am->root_path;
884 a->name = SVM_GLOBAL_REGION_NAME;
885 a->baseva = (am->global_baseva != 0) ?
Damjan Marionaec8f892018-01-08 16:35:35 +0100886 am->global_baseva : +svm_get_global_region_base_va ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800887 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
888 a->flags = SVM_FLAGS_NODATA;
889 a->uid = am->api_uid;
890 a->gid = am->api_gid;
891 a->pvt_heap_size =
892 (am->global_pvt_heap_size !=
893 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
894
895 svm_region_init_args (a);
896
897 error = vlib_call_init_function (vm, vlibsocket_init);
898
899 return error;
900}
901
902VLIB_INIT_FUNCTION (vlibmemory_init);
903
904void
905vl_set_memory_region_name (const char *name)
906{
907 api_main_t *am = &api_main;
908 am->region_name = name;
909}
910
911/*
912 * fd.io coding-style-patch-verification: ON
913 *
914 * Local Variables:
915 * eval: (c-set-style "gnu")
916 * End:
917 */