blob: 7a7644a09984330062c1d1b91100485c78888358 [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 }
Dave Barachf6c68d72018-11-01 08:12:52 -0400106 if (vec_len (vm->pending_rpc_requests))
107 {
108 vm->queue_signal_pending = 1;
109 vm->api_queue_nonempty = 1;
110 vlib_process_signal_event (vm, vl_api_clnt_node.index,
111 /* event_type */ QUEUE_SIGNAL_EVENT,
112 /* event_data */ 0);
113 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800114}
115
116/*
117 * vl_api_memclnt_create_internal
118 */
119u32
120vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
121{
122 vl_api_registration_t **regpp;
123 vl_api_registration_t *regp;
124 svm_region_t *svm;
125 void *oldheap;
126 api_main_t *am = &api_main;
127
128 ASSERT (vlib_get_thread_index () == 0);
129 pool_get (am->vl_clients, regpp);
130
131 svm = am->vlib_rp;
132
133 pthread_mutex_lock (&svm->mutex);
134 oldheap = svm_push_data_heap (svm);
135 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
136
137 regp = *regpp;
Dave Barachb7b92992018-10-17 10:38:51 -0400138 clib_memset (regp, 0, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800139 regp->registration_type = REGISTRATION_TYPE_SHMEM;
140 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
141 regp->vlib_rp = svm;
142 regp->shmem_hdr = am->shmem_hdr;
143
144 regp->vl_input_queue = q;
145 regp->name = format (0, "%s%c", name, 0);
146
147 pthread_mutex_unlock (&svm->mutex);
148 svm_pop_heap (oldheap);
149 return vl_msg_api_handle_from_index_and_epoch
150 (regp->vl_api_registration_pool_index,
151 am->shmem_hdr->application_restarts);
152}
153
154/*
155 * vl_api_memclnt_create_t_handler
156 */
157void
158vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
159{
160 vl_api_registration_t **regpp;
161 vl_api_registration_t *regp;
162 vl_api_memclnt_create_reply_t *rp;
163 svm_region_t *svm;
164 svm_queue_t *q;
165 int rv = 0;
166 void *oldheap;
167 api_main_t *am = &api_main;
Florin Corase25c9bf2018-08-06 12:19:29 -0700168 u8 *msg_table;
Florin Corase86a8ed2018-01-05 03:20:25 -0800169
170 /*
171 * This is tortured. Maintain a vlib-address-space private
172 * pool of client registrations. We use the shared-memory virtual
173 * address of client structure as a handle, to allow direct
174 * manipulation of context quota vbls from the client library.
175 *
176 * This scheme causes trouble w/ API message trace replay, since
177 * some random VA from clib_mem_alloc() certainly won't
178 * occur in the Linux sim. The (very) few places
179 * that care need to use the pool index.
180 *
181 * Putting the registration object(s) into a pool in shared memory and
182 * using the pool index as a handle seems like a great idea.
183 * Unfortunately, each and every reference to that pool would need
184 * to be protected by a mutex:
185 *
186 * Client VLIB
187 * ------ ----
188 * convert pool index to
189 * pointer.
190 * <deschedule>
191 * expand pool
192 * <deschedule>
193 * kaboom!
194 */
195
196 pool_get (am->vl_clients, regpp);
197
198 svm = am->vlib_rp;
199
200 pthread_mutex_lock (&svm->mutex);
201 oldheap = svm_push_data_heap (svm);
202 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
203
204 regp = *regpp;
Dave Barachb7b92992018-10-17 10:38:51 -0400205 clib_memset (regp, 0, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800206 regp->registration_type = REGISTRATION_TYPE_SHMEM;
207 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
208 regp->vlib_rp = svm;
209 regp->shmem_hdr = am->shmem_hdr;
Florin Corasb384b542018-01-15 01:08:33 -0800210 regp->clib_file_index = am->shmem_hdr->clib_file_index;
Florin Corase86a8ed2018-01-05 03:20:25 -0800211
212 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
213
214 regp->name = format (0, "%s", mp->name);
215 vec_add1 (regp->name, 0);
216
217 if (am->serialized_message_table_in_shmem == 0)
218 am->serialized_message_table_in_shmem =
219 vl_api_serialize_message_table (am, 0);
220
Florin Corase25c9bf2018-08-06 12:19:29 -0700221 if (am->vlib_rp != am->vlib_primary_rp)
222 msg_table = vl_api_serialize_message_table (am, 0);
223 else
224 msg_table = am->serialized_message_table_in_shmem;
225
Florin Corase86a8ed2018-01-05 03:20:25 -0800226 pthread_mutex_unlock (&svm->mutex);
227 svm_pop_heap (oldheap);
228
229 rp = vl_msg_api_alloc (sizeof (*rp));
230 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
231 rp->handle = (uword) regp;
232 rp->index = vl_msg_api_handle_from_index_and_epoch
233 (regp->vl_api_registration_pool_index,
234 am->shmem_hdr->application_restarts);
235 rp->context = mp->context;
236 rp->response = ntohl (rv);
Florin Corase25c9bf2018-08-06 12:19:29 -0700237 rp->message_table = pointer_to_uword (msg_table);
Florin Corase86a8ed2018-01-05 03:20:25 -0800238
239 vl_msg_api_send_shmem (q, (u8 *) & rp);
240}
241
242int
243vl_api_call_reaper_functions (u32 client_index)
244{
245 clib_error_t *error = 0;
246 _vl_msg_api_function_list_elt_t *i;
247
248 i = api_main.reaper_function_registrations;
249 while (i)
250 {
251 error = i->f (client_index);
252 if (error)
253 clib_error_report (error);
254 i = i->next_init_function;
255 }
256 return 0;
257}
258
259/*
260 * vl_api_memclnt_delete_t_handler
261 */
262void
263vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
264{
265 vl_api_registration_t **regpp;
266 vl_api_registration_t *regp;
267 vl_api_memclnt_delete_reply_t *rp;
268 svm_region_t *svm;
269 void *oldheap;
270 api_main_t *am = &api_main;
271 u32 handle, client_index, epoch;
272
273 handle = mp->index;
274
275 if (vl_api_call_reaper_functions (handle))
276 return;
277
278 epoch = vl_msg_api_handle_get_epoch (handle);
279 client_index = vl_msg_api_handle_get_index (handle);
280
281 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
282 {
283 clib_warning
284 ("Stale clnt delete index %d old epoch %d cur epoch %d",
285 client_index, epoch,
286 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
287 return;
288 }
289
Florin Corasb384b542018-01-15 01:08:33 -0800290 regpp = pool_elt_at_index (am->vl_clients, client_index);
Florin Corase86a8ed2018-01-05 03:20:25 -0800291
292 if (!pool_is_free (am->vl_clients, regpp))
293 {
294 int i;
295 regp = *regpp;
296 svm = am->vlib_rp;
297 int private_registration = 0;
298
299 /*
300 * Note: the API message handling path will set am->vlib_rp
301 * as appropriate for pairwise / private memory segments
302 */
303 rp = vl_msg_api_alloc (sizeof (*rp));
304 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
305 rp->handle = mp->handle;
306 rp->response = 1;
307
308 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
309
310 if (client_index != regp->vl_api_registration_pool_index)
311 {
312 clib_warning ("mismatch client_index %d pool_index %d",
313 client_index, regp->vl_api_registration_pool_index);
314 vl_msg_api_free (rp);
315 return;
316 }
317
318 /* For horizontal scaling, add a hash table... */
319 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
320 {
321 /* Is this a pairwise / private API segment? */
322 if (am->vlib_private_rps[i] == svm)
323 {
324 /* Note: account for the memfd header page */
325 u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
326 u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
327
328 /*
329 * Kill the registration pool element before we make
330 * the index vanish forever
331 */
332 pool_put_index (am->vl_clients,
333 regp->vl_api_registration_pool_index);
334
335 vec_delete (am->vlib_private_rps, 1, i);
336 /* Kill it, accounting for the memfd header page */
337 if (munmap ((void *) virtual_base, virtual_size) < 0)
338 clib_unix_warning ("munmap");
339 /* Reset the queue-length-address cache */
340 vec_reset_length (vl_api_queue_cursizes);
341 private_registration = 1;
342 break;
343 }
344 }
345
346 /* No dangling references, please */
347 *regpp = 0;
348
349 if (private_registration == 0)
350 {
351 pool_put_index (am->vl_clients,
352 regp->vl_api_registration_pool_index);
353 pthread_mutex_lock (&svm->mutex);
354 oldheap = svm_push_data_heap (svm);
Ole Troan73710c72018-06-04 22:27:49 +0200355 vec_free (regp->name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800356 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400357 clib_memset (regp, 0xF1, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800358 clib_mem_free (regp);
359 pthread_mutex_unlock (&svm->mutex);
360 svm_pop_heap (oldheap);
361 /*
362 * These messages must be freed manually, since they're set up
363 * as "bounce" messages. In the private_registration == 1 case,
364 * we kill the shared-memory segment which contains the message
365 * with munmap.
366 */
367 vl_msg_api_free (mp);
368 }
369 }
370 else
371 {
372 clib_warning ("unknown client ID %d", mp->index);
373 }
374}
375
376/**
377 * client answered a ping, stave off the grim reaper...
378 */
379void
380 vl_api_memclnt_keepalive_reply_t_handler
381 (vl_api_memclnt_keepalive_reply_t * mp)
382{
383 vl_api_registration_t *regp;
384 vlib_main_t *vm = vlib_get_main ();
385
386 regp = vl_api_client_index_to_registration (mp->context);
387 if (regp)
388 {
389 regp->last_heard = vlib_time_now (vm);
390 regp->unanswered_pings = 0;
391 }
392 else
393 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
394}
395
396/**
397 * We can send ourselves these messages if someone uses the
398 * builtin binary api test tool...
399 */
400static void
401vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
402{
403 vl_api_memclnt_keepalive_reply_t *rmp;
404 api_main_t *am;
405 vl_shmem_hdr_t *shmem_hdr;
406
407 am = &api_main;
408 shmem_hdr = am->shmem_hdr;
409
410 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400411 clib_memset (rmp, 0, sizeof (*rmp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800412 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
413 rmp->context = mp->context;
414 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
415}
416
417#define foreach_vlib_api_msg \
418_(MEMCLNT_CREATE, memclnt_create) \
419_(MEMCLNT_DELETE, memclnt_delete) \
420_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
421_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply) \
422
423/*
424 * memory_api_init
425 */
426int
427vl_mem_api_init (const char *region_name)
428{
429 int rv;
430 api_main_t *am = &api_main;
431 vl_msg_api_msg_config_t cfg;
432 vl_msg_api_msg_config_t *c = &cfg;
433 vl_shmem_hdr_t *shm;
434 vlib_main_t *vm = vlib_get_main ();
435
Dave Barachb7b92992018-10-17 10:38:51 -0400436 clib_memset (c, 0, sizeof (*c));
Florin Corase86a8ed2018-01-05 03:20:25 -0800437
438 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
439 return rv;
440
441#define _(N,n) do { \
442 c->id = VL_API_##N; \
443 c->name = #n; \
444 c->handler = vl_api_##n##_t_handler; \
445 c->cleanup = vl_noop_handler; \
446 c->endian = vl_api_##n##_t_endian; \
447 c->print = vl_api_##n##_t_print; \
448 c->size = sizeof(vl_api_##n##_t); \
449 c->traced = 1; /* trace, so these msgs print */ \
450 c->replay = 0; /* don't replay client create/delete msgs */ \
451 c->message_bounce = 0; /* don't bounce this message */ \
452 vl_msg_api_config(c);} while (0);
453
454 foreach_vlib_api_msg;
455#undef _
456
457 /*
458 * special-case freeing of memclnt_delete messages, so we can
459 * simply munmap pairwise / private API segments...
460 */
461 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
462 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
463
464 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
465
466 shm = am->shmem_hdr;
467 ASSERT (shm && shm->vl_input_queue);
468
469 /* Make a note so we can always find the primary region easily */
470 am->vlib_primary_rp = am->vlib_rp;
471
472 return 0;
473}
474
Dave Barach1f806582018-06-14 09:18:21 -0400475clib_error_t *
Dave Barach048a4e52018-06-01 18:52:25 -0400476map_api_segment_init (vlib_main_t * vm)
477{
478 api_main_t *am = &api_main;
479 int rv;
480
481 if ((rv = vl_mem_api_init (am->region_name)) < 0)
482 {
483 return clib_error_return (0, "vl_mem_api_init (%s) failed",
484 am->region_name);
485 }
486 return 0;
487}
488
Florin Corase86a8ed2018-01-05 03:20:25 -0800489static void
490send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
491{
492 vl_api_memclnt_keepalive_t *mp;
493 svm_queue_t *q;
494 api_main_t *am = &api_main;
495 svm_region_t *save_vlib_rp = am->vlib_rp;
496 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
497
498 q = regp->vl_input_queue;
499
500 /*
501 * If the queue head is moving, assume that the client is processing
502 * messages and skip the ping. This heuristic may fail if the queue
503 * is in the same position as last time, net of wrapping; in which
504 * case, the client will receive a keepalive.
505 */
506 if (regp->last_queue_head != q->head)
507 {
508 regp->last_heard = now;
509 regp->unanswered_pings = 0;
510 regp->last_queue_head = q->head;
511 return;
512 }
513
514 /*
515 * push/pop shared memory segment, so this routine
516 * will work with "normal" as well as "private segment"
517 * memory clients..
518 */
519
520 am->vlib_rp = regp->vlib_rp;
521 am->shmem_hdr = regp->shmem_hdr;
522
523 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400524 clib_memset (mp, 0, sizeof (*mp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800525 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
526 mp->context = mp->client_index =
527 vl_msg_api_handle_from_index_and_epoch
528 (regp->vl_api_registration_pool_index,
529 am->shmem_hdr->application_restarts);
530
531 regp->unanswered_pings++;
532
533 /* Failure-to-send due to a stuffed queue is absolutely expected */
534 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
535 vl_msg_api_free (mp);
536
537 am->vlib_rp = save_vlib_rp;
538 am->shmem_hdr = save_shmem_hdr;
539}
540
Florin Corasb384b542018-01-15 01:08:33 -0800541static void
542vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
543 vl_api_registration_t ** regpp,
544 u32 ** dead_indices,
545 u32 ** confused_indices)
546{
547 vl_api_registration_t *regp = *regpp;
548 if (regp)
549 {
550 /* If we haven't heard from this client recently... */
551 if (regp->last_heard < (now - 10.0))
552 {
553 if (regp->unanswered_pings == 2)
554 {
555 svm_queue_t *q;
556 q = regp->vl_input_queue;
557 if (kill (q->consumer_pid, 0) >= 0)
558 {
559 clib_warning ("REAPER: lazy binary API client '%s'",
560 regp->name);
561 regp->unanswered_pings = 0;
562 regp->last_heard = now;
563 }
564 else
565 {
566 clib_warning ("REAPER: binary API client '%s' died",
567 regp->name);
568 vec_add1 (*dead_indices, regpp - am->vl_clients);
569 }
570 }
571 else
572 send_memclnt_keepalive (regp, now);
573 }
574 else
575 regp->unanswered_pings = 0;
576 }
577 else
578 {
579 clib_warning ("NULL client registration index %d",
580 regpp - am->vl_clients);
581 vec_add1 (*confused_indices, regpp - am->vl_clients);
582 }
583}
584
Florin Corase86a8ed2018-01-05 03:20:25 -0800585void
586vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
587{
588 vl_api_registration_t **regpp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800589 static u32 *dead_indices;
590 static u32 *confused_indices;
591
592 vec_reset_length (dead_indices);
593 vec_reset_length (confused_indices);
594
595 /* *INDENT-OFF* */
Florin Corasb384b542018-01-15 01:08:33 -0800596 pool_foreach (regpp, am->vl_clients, ({
597 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
598 &confused_indices);
Florin Corase86a8ed2018-01-05 03:20:25 -0800599 }));
600 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800601
Florin Corase86a8ed2018-01-05 03:20:25 -0800602 /* This should "never happen," but if it does, fix it... */
603 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
604 {
605 int i;
606 for (i = 0; i < vec_len (confused_indices); i++)
607 {
608 pool_put_index (am->vl_clients, confused_indices[i]);
609 }
610 }
611
612 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
613 {
614 int i;
615 svm_region_t *svm;
616 void *oldheap;
617
618 /* Allow the application to clean up its registrations */
619 for (i = 0; i < vec_len (dead_indices); i++)
620 {
621 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
622 if (regpp)
623 {
624 u32 handle;
625
626 handle = vl_msg_api_handle_from_index_and_epoch
627 (dead_indices[i], shm->application_restarts);
628 (void) vl_api_call_reaper_functions (handle);
629 }
630 }
631
632 svm = am->vlib_rp;
633 pthread_mutex_lock (&svm->mutex);
634 oldheap = svm_push_data_heap (svm);
635
636 for (i = 0; i < vec_len (dead_indices); i++)
637 {
638 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
639 if (regpp)
640 {
641 /* Is this a pairwise SVM segment? */
642 if ((*regpp)->vlib_rp != svm)
643 {
644 int i;
645 svm_region_t *dead_rp = (*regpp)->vlib_rp;
646 /* Note: account for the memfd header page */
647 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
648 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
649
650 /* For horizontal scaling, add a hash table... */
651 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
652 if (am->vlib_private_rps[i] == dead_rp)
653 {
654 vec_delete (am->vlib_private_rps, 1, i);
655 goto found;
656 }
657 clib_warning ("private rp %llx AWOL", dead_rp);
658
659 found:
660 /* Kill it, accounting for the memfd header page */
661 if (munmap ((void *) virtual_base, virtual_size) < 0)
662 clib_unix_warning ("munmap");
663 /* Reset the queue-length-address cache */
664 vec_reset_length (vl_api_queue_cursizes);
665 }
666 else
667 {
668 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400669 clib_memset (*regpp, 0xF3, sizeof (**regpp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800670 clib_mem_free (*regpp);
671 }
672 /* no dangling references, please */
673 *regpp = 0;
674 }
675 else
676 {
677 svm_pop_heap (oldheap);
678 clib_warning ("Duplicate free, client index %d",
679 regpp - am->vl_clients);
680 oldheap = svm_push_data_heap (svm);
681 }
682 }
683
684 svm_client_scan_this_region_nolock (am->vlib_rp);
685
686 pthread_mutex_unlock (&svm->mutex);
687 svm_pop_heap (oldheap);
688 for (i = 0; i < vec_len (dead_indices); i++)
689 pool_put_index (am->vl_clients, dead_indices[i]);
690 }
691}
692
693static inline int
694void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
695 vlib_node_runtime_t * node, svm_queue_t * q)
696{
697 uword mp;
698 if (!svm_queue_sub2 (q, (u8 *) & mp))
699 {
700 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
701 return 0;
702 }
703 return -1;
704}
705
706int
707vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
708{
709 api_main_t *am = &api_main;
710 return void_mem_api_handle_msg_i (am, vm, node,
711 am->shmem_hdr->vl_input_queue);
712}
713
714int
Dave Barachf6c68d72018-11-01 08:12:52 -0400715vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
716{
717 api_main_t *am = &api_main;
718 int i;
719 uword *tmp, mp;
720
721 /*
722 * Swap pending and processing vectors, then process the RPCs
723 * Avoid deadlock conditions by construction.
724 */
725 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
726 tmp = vm->processing_rpc_requests;
727 vec_reset_length (tmp);
728 vm->processing_rpc_requests = vm->pending_rpc_requests;
729 vm->pending_rpc_requests = tmp;
730 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
731
732 for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
733 {
734 mp = vm->processing_rpc_requests[i];
735 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
736 }
737 return 0;
738}
739
740int
Florin Corase86a8ed2018-01-05 03:20:25 -0800741vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
742 u32 reg_index)
743{
744 api_main_t *am = &api_main;
745 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
746 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
747 svm_queue_t *q;
748 int rv;
749
750 vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
751
752 am->shmem_hdr = (void *) vlib_rp->user_ctx;
753 q = am->shmem_hdr->vl_input_queue;
754
755 rv = void_mem_api_handle_msg_i (am, vm, node, q);
756
757 am->shmem_hdr = save_shmem_hdr;
758 am->vlib_rp = save_vlib_rp;
759
760 return rv;
761}
762
763vl_api_registration_t *
764vl_mem_api_client_index_to_registration (u32 handle)
765{
766 vl_api_registration_t **regpp;
767 vl_api_registration_t *regp;
768 api_main_t *am = &api_main;
Florin Corasb384b542018-01-15 01:08:33 -0800769 vl_shmem_hdr_t *shmem_hdr;
Florin Corase86a8ed2018-01-05 03:20:25 -0800770 u32 index;
771
772 index = vl_msg_api_handle_get_index (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800773 regpp = am->vl_clients + index;
774
775 if (pool_is_free (am->vl_clients, regpp))
776 {
777 vl_msg_api_increment_missing_client_counter ();
778 return 0;
779 }
780 regp = *regpp;
Florin Corasb384b542018-01-15 01:08:33 -0800781
782 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
783 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
784 {
785 vl_msg_api_increment_missing_client_counter ();
786 return 0;
787 }
788
Florin Corase86a8ed2018-01-05 03:20:25 -0800789 return (regp);
790}
791
792svm_queue_t *
793vl_api_client_index_to_input_queue (u32 index)
794{
795 vl_api_registration_t *regp;
796 api_main_t *am = &api_main;
797
798 /* Special case: vlib trying to send itself a message */
799 if (index == (u32) ~ 0)
800 return (am->shmem_hdr->vl_input_queue);
801
802 regp = vl_mem_api_client_index_to_registration (index);
803 if (!regp)
804 return 0;
805 return (regp->vl_input_queue);
806}
807
808static clib_error_t *
809setup_memclnt_exit (vlib_main_t * vm)
810{
811 atexit (vl_unmap_shmem);
812 return 0;
813}
814
815VLIB_INIT_FUNCTION (setup_memclnt_exit);
816
817u8 *
818format_api_message_rings (u8 * s, va_list * args)
819{
820 api_main_t *am = va_arg (*args, api_main_t *);
821 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
822 int main_segment = va_arg (*args, int);
823 ring_alloc_t *ap;
824 int i;
825
826 if (shmem_hdr == 0)
827 return format (s, "%8s %8s %8s %8s %8s\n",
828 "Owner", "Size", "Nitems", "Hits", "Misses");
829
830 ap = shmem_hdr->vl_rings;
831
832 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
833 {
834 s = format (s, "%8s %8d %8d %8d %8d\n",
835 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
836 ap++;
837 }
838
839 ap = shmem_hdr->client_rings;
840
841 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
842 {
843 s = format (s, "%8s %8d %8d %8d %8d\n",
844 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
845 ap++;
846 }
847
848 if (main_segment)
849 {
850 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
851 s = format
852 (s,
853 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
854 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
855 shmem_hdr->garbage_collects);
856 }
857 return s;
858}
859
860static clib_error_t *
861vl_api_ring_command (vlib_main_t * vm,
862 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
863{
864 int i;
865 vl_shmem_hdr_t *shmem_hdr;
866 api_main_t *am = &api_main;
867
868 /* First, dump the primary region rings.. */
869
870 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
871 {
872 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
873 return 0;
874 }
875
876 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
877
878 vlib_cli_output (vm, "Main API segment rings:");
879
880 vlib_cli_output (vm, "%U", format_api_message_rings, am,
881 0 /* print header */ , 0 /* notused */ );
882
883 vlib_cli_output (vm, "%U", format_api_message_rings, am,
884 shmem_hdr, 1 /* main segment */ );
885
886 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
887 {
888 svm_region_t *vlib_rp = am->vlib_private_rps[i];
889 shmem_hdr = (void *) vlib_rp->user_ctx;
890 vl_api_registration_t **regpp;
891 vl_api_registration_t *regp = 0;
892
893 /* For horizontal scaling, add a hash table... */
894 /* *INDENT-OFF* */
895 pool_foreach (regpp, am->vl_clients,
896 ({
897 regp = *regpp;
898 if (regp && regp->vlib_rp == vlib_rp)
899 {
900 vlib_cli_output (vm, "%s segment rings:", regp->name);
901 goto found;
902 }
903 }));
904 vlib_cli_output (vm, "regp %llx not found?", regp);
905 continue;
906 /* *INDENT-ON* */
907 found:
908 vlib_cli_output (vm, "%U", format_api_message_rings, am,
909 0 /* print header */ , 0 /* notused */ );
910 vlib_cli_output (vm, "%U", format_api_message_rings, am,
911 shmem_hdr, 0 /* main segment */ );
912 }
913
914 return 0;
915}
916
917/*?
918 * Display binary api message allocation ring statistics
919?*/
920/* *INDENT-OFF* */
921VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
922{
923 .path = "show api ring-stats",
924 .short_help = "Message ring statistics",
925 .function = vl_api_ring_command,
926};
927/* *INDENT-ON* */
928
929clib_error_t *
930vlibmemory_init (vlib_main_t * vm)
931{
932 api_main_t *am = &api_main;
933 svm_map_region_args_t _a, *a = &_a;
934 clib_error_t *error;
935
Dave Barachb7b92992018-10-17 10:38:51 -0400936 clib_memset (a, 0, sizeof (*a));
Florin Corase86a8ed2018-01-05 03:20:25 -0800937 a->root_path = am->root_path;
938 a->name = SVM_GLOBAL_REGION_NAME;
939 a->baseva = (am->global_baseva != 0) ?
Damjan Marionaec8f892018-01-08 16:35:35 +0100940 am->global_baseva : +svm_get_global_region_base_va ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800941 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
942 a->flags = SVM_FLAGS_NODATA;
943 a->uid = am->api_uid;
944 a->gid = am->api_gid;
945 a->pvt_heap_size =
946 (am->global_pvt_heap_size !=
947 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
948
949 svm_region_init_args (a);
950
951 error = vlib_call_init_function (vm, vlibsocket_init);
952
953 return error;
954}
955
Florin Corase86a8ed2018-01-05 03:20:25 -0800956void
957vl_set_memory_region_name (const char *name)
958{
959 api_main_t *am = &api_main;
960 am->region_name = name;
961}
962
963/*
964 * fd.io coding-style-patch-verification: ON
965 *
966 * Local Variables:
967 * eval: (c-set-style "gnu")
968 * End:
969 */