blob: b87aa76b2d07f9f70479491265153db1ea893037 [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
Florin Coraseaec2a62018-12-04 16:34:05 -0800299 /* Send reply unless client asked us to do the cleanup */
300 if (!mp->do_cleanup)
Florin Corase86a8ed2018-01-05 03:20:25 -0800301 {
Florin Coraseaec2a62018-12-04 16:34:05 -0800302 /*
303 * Note: the API message handling path will set am->vlib_rp
304 * as appropriate for pairwise / private memory segments
305 */
306 rp = vl_msg_api_alloc (sizeof (*rp));
307 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
308 rp->handle = mp->handle;
309 rp->response = 1;
310
311 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
312 if (client_index != regp->vl_api_registration_pool_index)
313 {
314 clib_warning ("mismatch client_index %d pool_index %d",
315 client_index,
316 regp->vl_api_registration_pool_index);
317 vl_msg_api_free (rp);
318 return;
319 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800320 }
321
322 /* For horizontal scaling, add a hash table... */
323 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
324 {
325 /* Is this a pairwise / private API segment? */
326 if (am->vlib_private_rps[i] == svm)
327 {
328 /* Note: account for the memfd header page */
David Johnsond9818dd2018-12-14 14:53:41 -0500329 uword virtual_base = svm->virtual_base - MMAP_PAGESIZE;
330 uword virtual_size = svm->virtual_size + MMAP_PAGESIZE;
Florin Corase86a8ed2018-01-05 03:20:25 -0800331
332 /*
333 * Kill the registration pool element before we make
334 * the index vanish forever
335 */
336 pool_put_index (am->vl_clients,
337 regp->vl_api_registration_pool_index);
338
339 vec_delete (am->vlib_private_rps, 1, i);
340 /* Kill it, accounting for the memfd header page */
341 if (munmap ((void *) virtual_base, virtual_size) < 0)
342 clib_unix_warning ("munmap");
343 /* Reset the queue-length-address cache */
344 vec_reset_length (vl_api_queue_cursizes);
345 private_registration = 1;
346 break;
347 }
348 }
349
350 /* No dangling references, please */
351 *regpp = 0;
352
353 if (private_registration == 0)
354 {
355 pool_put_index (am->vl_clients,
356 regp->vl_api_registration_pool_index);
357 pthread_mutex_lock (&svm->mutex);
358 oldheap = svm_push_data_heap (svm);
Florin Coraseaec2a62018-12-04 16:34:05 -0800359 if (mp->do_cleanup)
360 svm_queue_free (regp->vl_input_queue);
Ole Troan73710c72018-06-04 22:27:49 +0200361 vec_free (regp->name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800362 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400363 clib_memset (regp, 0xF1, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800364 clib_mem_free (regp);
365 pthread_mutex_unlock (&svm->mutex);
366 svm_pop_heap (oldheap);
367 /*
368 * These messages must be freed manually, since they're set up
369 * as "bounce" messages. In the private_registration == 1 case,
370 * we kill the shared-memory segment which contains the message
371 * with munmap.
372 */
373 vl_msg_api_free (mp);
374 }
375 }
376 else
377 {
378 clib_warning ("unknown client ID %d", mp->index);
379 }
380}
381
382/**
383 * client answered a ping, stave off the grim reaper...
384 */
385void
386 vl_api_memclnt_keepalive_reply_t_handler
387 (vl_api_memclnt_keepalive_reply_t * mp)
388{
389 vl_api_registration_t *regp;
390 vlib_main_t *vm = vlib_get_main ();
391
392 regp = vl_api_client_index_to_registration (mp->context);
393 if (regp)
394 {
395 regp->last_heard = vlib_time_now (vm);
396 regp->unanswered_pings = 0;
397 }
398 else
399 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
400}
401
402/**
403 * We can send ourselves these messages if someone uses the
404 * builtin binary api test tool...
405 */
406static void
407vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
408{
409 vl_api_memclnt_keepalive_reply_t *rmp;
410 api_main_t *am;
411 vl_shmem_hdr_t *shmem_hdr;
412
413 am = &api_main;
414 shmem_hdr = am->shmem_hdr;
415
416 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400417 clib_memset (rmp, 0, sizeof (*rmp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800418 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
419 rmp->context = mp->context;
420 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
421}
422
Dave Barachc89c7672019-07-19 17:40:18 -0400423/*
424 * To avoid filling the API trace buffer with boring messages,
425 * don't trace memclnt_keepalive[_reply] msgs
426 */
427
Florin Corase86a8ed2018-01-05 03:20:25 -0800428#define foreach_vlib_api_msg \
Dave Barachc89c7672019-07-19 17:40:18 -0400429_(MEMCLNT_CREATE, memclnt_create, 1) \
430_(MEMCLNT_DELETE, memclnt_delete, 1) \
431_(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
432_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
Florin Corase86a8ed2018-01-05 03:20:25 -0800433
434/*
435 * memory_api_init
436 */
437int
438vl_mem_api_init (const char *region_name)
439{
440 int rv;
441 api_main_t *am = &api_main;
442 vl_msg_api_msg_config_t cfg;
443 vl_msg_api_msg_config_t *c = &cfg;
444 vl_shmem_hdr_t *shm;
445 vlib_main_t *vm = vlib_get_main ();
446
Dave Barachb7b92992018-10-17 10:38:51 -0400447 clib_memset (c, 0, sizeof (*c));
Florin Corase86a8ed2018-01-05 03:20:25 -0800448
449 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
450 return rv;
451
Dave Barachc89c7672019-07-19 17:40:18 -0400452#define _(N,n,t) do { \
Florin Corase86a8ed2018-01-05 03:20:25 -0800453 c->id = VL_API_##N; \
454 c->name = #n; \
455 c->handler = vl_api_##n##_t_handler; \
456 c->cleanup = vl_noop_handler; \
457 c->endian = vl_api_##n##_t_endian; \
458 c->print = vl_api_##n##_t_print; \
459 c->size = sizeof(vl_api_##n##_t); \
Dave Barachc89c7672019-07-19 17:40:18 -0400460 c->traced = t; /* trace, so these msgs print */ \
Florin Corase86a8ed2018-01-05 03:20:25 -0800461 c->replay = 0; /* don't replay client create/delete msgs */ \
462 c->message_bounce = 0; /* don't bounce this message */ \
463 vl_msg_api_config(c);} while (0);
464
465 foreach_vlib_api_msg;
466#undef _
467
468 /*
469 * special-case freeing of memclnt_delete messages, so we can
470 * simply munmap pairwise / private API segments...
471 */
472 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
473 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
Dave Barachc898a4f2019-06-14 17:29:55 -0400474 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
Florin Corase86a8ed2018-01-05 03:20:25 -0800475
476 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
477
478 shm = am->shmem_hdr;
479 ASSERT (shm && shm->vl_input_queue);
480
481 /* Make a note so we can always find the primary region easily */
482 am->vlib_primary_rp = am->vlib_rp;
483
484 return 0;
485}
486
Dave Barach1f806582018-06-14 09:18:21 -0400487clib_error_t *
Dave Barach048a4e52018-06-01 18:52:25 -0400488map_api_segment_init (vlib_main_t * vm)
489{
490 api_main_t *am = &api_main;
491 int rv;
492
493 if ((rv = vl_mem_api_init (am->region_name)) < 0)
494 {
495 return clib_error_return (0, "vl_mem_api_init (%s) failed",
496 am->region_name);
497 }
498 return 0;
499}
500
Florin Corase86a8ed2018-01-05 03:20:25 -0800501static void
502send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
503{
504 vl_api_memclnt_keepalive_t *mp;
505 svm_queue_t *q;
506 api_main_t *am = &api_main;
507 svm_region_t *save_vlib_rp = am->vlib_rp;
508 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
509
510 q = regp->vl_input_queue;
511
512 /*
513 * If the queue head is moving, assume that the client is processing
514 * messages and skip the ping. This heuristic may fail if the queue
515 * is in the same position as last time, net of wrapping; in which
516 * case, the client will receive a keepalive.
517 */
518 if (regp->last_queue_head != q->head)
519 {
520 regp->last_heard = now;
521 regp->unanswered_pings = 0;
522 regp->last_queue_head = q->head;
523 return;
524 }
525
526 /*
527 * push/pop shared memory segment, so this routine
528 * will work with "normal" as well as "private segment"
529 * memory clients..
530 */
531
532 am->vlib_rp = regp->vlib_rp;
533 am->shmem_hdr = regp->shmem_hdr;
534
535 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400536 clib_memset (mp, 0, sizeof (*mp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800537 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
538 mp->context = mp->client_index =
539 vl_msg_api_handle_from_index_and_epoch
540 (regp->vl_api_registration_pool_index,
541 am->shmem_hdr->application_restarts);
542
543 regp->unanswered_pings++;
544
545 /* Failure-to-send due to a stuffed queue is absolutely expected */
546 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
547 vl_msg_api_free (mp);
548
549 am->vlib_rp = save_vlib_rp;
550 am->shmem_hdr = save_shmem_hdr;
551}
552
Florin Corasb384b542018-01-15 01:08:33 -0800553static void
554vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
555 vl_api_registration_t ** regpp,
556 u32 ** dead_indices,
557 u32 ** confused_indices)
558{
559 vl_api_registration_t *regp = *regpp;
560 if (regp)
561 {
562 /* If we haven't heard from this client recently... */
563 if (regp->last_heard < (now - 10.0))
564 {
565 if (regp->unanswered_pings == 2)
566 {
567 svm_queue_t *q;
568 q = regp->vl_input_queue;
569 if (kill (q->consumer_pid, 0) >= 0)
570 {
571 clib_warning ("REAPER: lazy binary API client '%s'",
572 regp->name);
573 regp->unanswered_pings = 0;
574 regp->last_heard = now;
575 }
576 else
577 {
578 clib_warning ("REAPER: binary API client '%s' died",
579 regp->name);
580 vec_add1 (*dead_indices, regpp - am->vl_clients);
581 }
582 }
583 else
584 send_memclnt_keepalive (regp, now);
585 }
586 else
587 regp->unanswered_pings = 0;
588 }
589 else
590 {
591 clib_warning ("NULL client registration index %d",
592 regpp - am->vl_clients);
593 vec_add1 (*confused_indices, regpp - am->vl_clients);
594 }
595}
596
Florin Corase86a8ed2018-01-05 03:20:25 -0800597void
598vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
599{
600 vl_api_registration_t **regpp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800601 static u32 *dead_indices;
602 static u32 *confused_indices;
603
604 vec_reset_length (dead_indices);
605 vec_reset_length (confused_indices);
606
607 /* *INDENT-OFF* */
Florin Corasb384b542018-01-15 01:08:33 -0800608 pool_foreach (regpp, am->vl_clients, ({
609 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
610 &confused_indices);
Florin Corase86a8ed2018-01-05 03:20:25 -0800611 }));
612 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800613
Florin Corase86a8ed2018-01-05 03:20:25 -0800614 /* This should "never happen," but if it does, fix it... */
615 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
616 {
617 int i;
618 for (i = 0; i < vec_len (confused_indices); i++)
619 {
620 pool_put_index (am->vl_clients, confused_indices[i]);
621 }
622 }
623
624 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
625 {
626 int i;
627 svm_region_t *svm;
628 void *oldheap;
629
630 /* Allow the application to clean up its registrations */
631 for (i = 0; i < vec_len (dead_indices); i++)
632 {
633 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
634 if (regpp)
635 {
636 u32 handle;
637
638 handle = vl_msg_api_handle_from_index_and_epoch
639 (dead_indices[i], shm->application_restarts);
640 (void) vl_api_call_reaper_functions (handle);
641 }
642 }
643
644 svm = am->vlib_rp;
645 pthread_mutex_lock (&svm->mutex);
646 oldheap = svm_push_data_heap (svm);
647
648 for (i = 0; i < vec_len (dead_indices); i++)
649 {
650 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
651 if (regpp)
652 {
653 /* Is this a pairwise SVM segment? */
654 if ((*regpp)->vlib_rp != svm)
655 {
656 int i;
657 svm_region_t *dead_rp = (*regpp)->vlib_rp;
658 /* Note: account for the memfd header page */
David Johnsond9818dd2018-12-14 14:53:41 -0500659 uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
660 uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
Florin Corase86a8ed2018-01-05 03:20:25 -0800661
662 /* For horizontal scaling, add a hash table... */
663 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
664 if (am->vlib_private_rps[i] == dead_rp)
665 {
666 vec_delete (am->vlib_private_rps, 1, i);
667 goto found;
668 }
669 clib_warning ("private rp %llx AWOL", dead_rp);
670
671 found:
672 /* Kill it, accounting for the memfd header page */
673 if (munmap ((void *) virtual_base, virtual_size) < 0)
674 clib_unix_warning ("munmap");
675 /* Reset the queue-length-address cache */
676 vec_reset_length (vl_api_queue_cursizes);
677 }
678 else
679 {
680 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400681 clib_memset (*regpp, 0xF3, sizeof (**regpp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800682 clib_mem_free (*regpp);
683 }
684 /* no dangling references, please */
685 *regpp = 0;
686 }
687 else
688 {
689 svm_pop_heap (oldheap);
690 clib_warning ("Duplicate free, client index %d",
691 regpp - am->vl_clients);
692 oldheap = svm_push_data_heap (svm);
693 }
694 }
695
696 svm_client_scan_this_region_nolock (am->vlib_rp);
697
698 pthread_mutex_unlock (&svm->mutex);
699 svm_pop_heap (oldheap);
700 for (i = 0; i < vec_len (dead_indices); i++)
701 pool_put_index (am->vl_clients, dead_indices[i]);
702 }
703}
704
705static inline int
706void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
707 vlib_node_runtime_t * node, svm_queue_t * q)
708{
709 uword mp;
710 if (!svm_queue_sub2 (q, (u8 *) & mp))
711 {
712 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
713 return 0;
714 }
715 return -1;
716}
717
718int
719vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
720{
721 api_main_t *am = &api_main;
722 return void_mem_api_handle_msg_i (am, vm, node,
723 am->shmem_hdr->vl_input_queue);
724}
725
726int
Dave Barachf6c68d72018-11-01 08:12:52 -0400727vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
728{
729 api_main_t *am = &api_main;
730 int i;
731 uword *tmp, mp;
732
733 /*
734 * Swap pending and processing vectors, then process the RPCs
735 * Avoid deadlock conditions by construction.
736 */
737 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
738 tmp = vm->processing_rpc_requests;
739 vec_reset_length (tmp);
740 vm->processing_rpc_requests = vm->pending_rpc_requests;
741 vm->pending_rpc_requests = tmp;
742 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
743
Dave Barach1bb981d2019-02-26 17:04:40 -0500744 /*
745 * RPCs are used to reflect function calls to thread 0
746 * when the underlying code is not thread-safe.
747 *
748 * Grabbing the thread barrier across a set of RPCs
749 * greatly increases efficiency, and avoids
750 * running afoul of the barrier sync holddown timer.
751 * The barrier sync code supports recursive locking.
752 *
753 * We really need to rewrite RPC-based code...
754 */
755 if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
Dave Barachf6c68d72018-11-01 08:12:52 -0400756 {
Dave Barach1bb981d2019-02-26 17:04:40 -0500757 vl_msg_api_barrier_sync ();
758 for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
759 {
760 mp = vm->processing_rpc_requests[i];
761 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
762 }
763 vl_msg_api_barrier_release ();
Dave Barachf6c68d72018-11-01 08:12:52 -0400764 }
Dave Barach1bb981d2019-02-26 17:04:40 -0500765
Dave Barachf6c68d72018-11-01 08:12:52 -0400766 return 0;
767}
768
769int
Florin Corase86a8ed2018-01-05 03:20:25 -0800770vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
771 u32 reg_index)
772{
773 api_main_t *am = &api_main;
774 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
775 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
776 svm_queue_t *q;
777 int rv;
778
779 vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
780
781 am->shmem_hdr = (void *) vlib_rp->user_ctx;
782 q = am->shmem_hdr->vl_input_queue;
783
784 rv = void_mem_api_handle_msg_i (am, vm, node, q);
785
786 am->shmem_hdr = save_shmem_hdr;
787 am->vlib_rp = save_vlib_rp;
788
789 return rv;
790}
791
792vl_api_registration_t *
793vl_mem_api_client_index_to_registration (u32 handle)
794{
795 vl_api_registration_t **regpp;
796 vl_api_registration_t *regp;
797 api_main_t *am = &api_main;
Florin Corasb384b542018-01-15 01:08:33 -0800798 vl_shmem_hdr_t *shmem_hdr;
Florin Corase86a8ed2018-01-05 03:20:25 -0800799 u32 index;
800
801 index = vl_msg_api_handle_get_index (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800802 regpp = am->vl_clients + index;
803
804 if (pool_is_free (am->vl_clients, regpp))
805 {
806 vl_msg_api_increment_missing_client_counter ();
807 return 0;
808 }
809 regp = *regpp;
Florin Corasb384b542018-01-15 01:08:33 -0800810
811 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
812 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
813 {
814 vl_msg_api_increment_missing_client_counter ();
815 return 0;
816 }
817
Florin Corase86a8ed2018-01-05 03:20:25 -0800818 return (regp);
819}
820
821svm_queue_t *
822vl_api_client_index_to_input_queue (u32 index)
823{
824 vl_api_registration_t *regp;
825 api_main_t *am = &api_main;
826
827 /* Special case: vlib trying to send itself a message */
828 if (index == (u32) ~ 0)
829 return (am->shmem_hdr->vl_input_queue);
830
831 regp = vl_mem_api_client_index_to_registration (index);
832 if (!regp)
833 return 0;
834 return (regp->vl_input_queue);
835}
836
837static clib_error_t *
838setup_memclnt_exit (vlib_main_t * vm)
839{
840 atexit (vl_unmap_shmem);
841 return 0;
842}
843
844VLIB_INIT_FUNCTION (setup_memclnt_exit);
845
846u8 *
847format_api_message_rings (u8 * s, va_list * args)
848{
849 api_main_t *am = va_arg (*args, api_main_t *);
850 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
851 int main_segment = va_arg (*args, int);
852 ring_alloc_t *ap;
853 int i;
854
855 if (shmem_hdr == 0)
856 return format (s, "%8s %8s %8s %8s %8s\n",
857 "Owner", "Size", "Nitems", "Hits", "Misses");
858
859 ap = shmem_hdr->vl_rings;
860
861 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
862 {
863 s = format (s, "%8s %8d %8d %8d %8d\n",
864 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
865 ap++;
866 }
867
868 ap = shmem_hdr->client_rings;
869
870 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
871 {
872 s = format (s, "%8s %8d %8d %8d %8d\n",
873 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
874 ap++;
875 }
876
877 if (main_segment)
878 {
879 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
880 s = format
881 (s,
882 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
883 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
884 shmem_hdr->garbage_collects);
885 }
886 return s;
887}
888
889static clib_error_t *
890vl_api_ring_command (vlib_main_t * vm,
891 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
892{
893 int i;
894 vl_shmem_hdr_t *shmem_hdr;
895 api_main_t *am = &api_main;
896
897 /* First, dump the primary region rings.. */
898
899 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
900 {
901 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
902 return 0;
903 }
904
905 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
906
907 vlib_cli_output (vm, "Main API segment rings:");
908
909 vlib_cli_output (vm, "%U", format_api_message_rings, am,
910 0 /* print header */ , 0 /* notused */ );
911
912 vlib_cli_output (vm, "%U", format_api_message_rings, am,
913 shmem_hdr, 1 /* main segment */ );
914
915 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
916 {
917 svm_region_t *vlib_rp = am->vlib_private_rps[i];
918 shmem_hdr = (void *) vlib_rp->user_ctx;
919 vl_api_registration_t **regpp;
920 vl_api_registration_t *regp = 0;
921
922 /* For horizontal scaling, add a hash table... */
923 /* *INDENT-OFF* */
924 pool_foreach (regpp, am->vl_clients,
925 ({
926 regp = *regpp;
927 if (regp && regp->vlib_rp == vlib_rp)
928 {
929 vlib_cli_output (vm, "%s segment rings:", regp->name);
930 goto found;
931 }
932 }));
933 vlib_cli_output (vm, "regp %llx not found?", regp);
934 continue;
935 /* *INDENT-ON* */
936 found:
937 vlib_cli_output (vm, "%U", format_api_message_rings, am,
938 0 /* print header */ , 0 /* notused */ );
939 vlib_cli_output (vm, "%U", format_api_message_rings, am,
940 shmem_hdr, 0 /* main segment */ );
941 }
942
943 return 0;
944}
945
946/*?
947 * Display binary api message allocation ring statistics
948?*/
949/* *INDENT-OFF* */
950VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
951{
952 .path = "show api ring-stats",
953 .short_help = "Message ring statistics",
954 .function = vl_api_ring_command,
955};
956/* *INDENT-ON* */
957
958clib_error_t *
959vlibmemory_init (vlib_main_t * vm)
960{
961 api_main_t *am = &api_main;
962 svm_map_region_args_t _a, *a = &_a;
Dave Barachb2204672018-11-30 16:46:29 -0500963 u8 *remove_path1, *remove_path2;
Dave Barachf8d50682019-05-14 18:01:44 -0400964 void vlibsocket_reference (void);
965
966 vlibsocket_reference ();
Dave Barachb2204672018-11-30 16:46:29 -0500967
968 /*
969 * By popular request / to avoid support fires, remove any old api segment
970 * files Right Here.
971 */
972 if (am->root_path == 0)
973 {
974 remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
975 remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
976 }
977 else
978 {
979 remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
980 remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
981 }
982
983 (void) unlink ((char *) remove_path1);
984 (void) unlink ((char *) remove_path2);
985
986 vec_free (remove_path1);
987 vec_free (remove_path2);
Florin Corase86a8ed2018-01-05 03:20:25 -0800988
Dave Barachb7b92992018-10-17 10:38:51 -0400989 clib_memset (a, 0, sizeof (*a));
Florin Corase86a8ed2018-01-05 03:20:25 -0800990 a->root_path = am->root_path;
991 a->name = SVM_GLOBAL_REGION_NAME;
992 a->baseva = (am->global_baseva != 0) ?
Damjan Marionaec8f892018-01-08 16:35:35 +0100993 am->global_baseva : +svm_get_global_region_base_va ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800994 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
995 a->flags = SVM_FLAGS_NODATA;
996 a->uid = am->api_uid;
997 a->gid = am->api_gid;
998 a->pvt_heap_size =
999 (am->global_pvt_heap_size !=
1000 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
1001
1002 svm_region_init_args (a);
1003
Dave Barachf8d50682019-05-14 18:01:44 -04001004 return 0;
Florin Corase86a8ed2018-01-05 03:20:25 -08001005}
1006
Florin Corase86a8ed2018-01-05 03:20:25 -08001007void
1008vl_set_memory_region_name (const char *name)
1009{
1010 api_main_t *am = &api_main;
1011 am->region_name = name;
1012}
1013
1014/*
1015 * fd.io coding-style-patch-verification: ON
1016 *
1017 * Local Variables:
1018 * eval: (c-set-style "gnu")
1019 * End:
1020 */