blob: 9db27ebd5745323c2b51cd0467d6a344dcdade71 [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;
Dave Barach39d69112019-11-27 11:42:13 -050067 api_main_t *am = vlibapi_get_main ();
Xiaoming Jiang405debc2021-07-13 03:55:59 +000068 int have_pending_rpcs;
Florin Corase86a8ed2018-01-05 03:20:25 -080069
70 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
71 1 + vec_len (am->vlib_private_rps)))
72 {
73 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
74 svm_queue_t *q;
75
76 if (shmem_hdr == 0)
77 return;
78
79 q = shmem_hdr->vl_input_queue;
80 if (q == 0)
81 return;
82
83 vec_add1 (vl_api_queue_cursizes, &q->cursize);
84
85 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
86 {
87 svm_region_t *vlib_rp = am->vlib_private_rps[i];
88
89 shmem_hdr = (void *) vlib_rp->user_ctx;
90 q = shmem_hdr->vl_input_queue;
91 vec_add1 (vl_api_queue_cursizes, &q->cursize);
92 }
93 }
94
95 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
96 {
97 if (*vl_api_queue_cursizes[i])
98 {
99 vm->queue_signal_pending = 1;
100 vm->api_queue_nonempty = 1;
101 vlib_process_signal_event (vm, vl_api_clnt_node.index,
102 /* event_type */ QUEUE_SIGNAL_EVENT,
103 /* event_data */ 0);
104 break;
105 }
106 }
Xiaoming Jiang405debc2021-07-13 03:55:59 +0000107
108 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
109 have_pending_rpcs = vec_len (vm->pending_rpc_requests) > 0;
110 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
111
112 if (have_pending_rpcs)
Dave Barachf6c68d72018-11-01 08:12:52 -0400113 {
114 vm->queue_signal_pending = 1;
115 vm->api_queue_nonempty = 1;
116 vlib_process_signal_event (vm, vl_api_clnt_node.index,
117 /* event_type */ QUEUE_SIGNAL_EVENT,
118 /* event_data */ 0);
119 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800120}
121
122/*
123 * vl_api_memclnt_create_internal
124 */
125u32
126vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
127{
128 vl_api_registration_t **regpp;
129 vl_api_registration_t *regp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800130 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500131 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800132
133 ASSERT (vlib_get_thread_index () == 0);
134 pool_get (am->vl_clients, regpp);
135
Florin Corase86a8ed2018-01-05 03:20:25 -0800136
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100137 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800138 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
139
140 regp = *regpp;
Dave Barachb7b92992018-10-17 10:38:51 -0400141 clib_memset (regp, 0, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800142 regp->registration_type = REGISTRATION_TYPE_SHMEM;
143 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100144 regp->vlib_rp = am->vlib_rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800145 regp->shmem_hdr = am->shmem_hdr;
146
147 regp->vl_input_queue = q;
148 regp->name = format (0, "%s%c", name, 0);
149
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100150 vl_msg_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800151 return vl_msg_api_handle_from_index_and_epoch
152 (regp->vl_api_registration_pool_index,
153 am->shmem_hdr->application_restarts);
154}
155
156/*
157 * vl_api_memclnt_create_t_handler
158 */
159void
160vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
161{
162 vl_api_registration_t **regpp;
163 vl_api_registration_t *regp;
164 vl_api_memclnt_create_reply_t *rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800165 svm_queue_t *q;
166 int rv = 0;
167 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500168 api_main_t *am = vlibapi_get_main ();
Florin Corase25c9bf2018-08-06 12:19:29 -0700169 u8 *msg_table;
Florin Corase86a8ed2018-01-05 03:20:25 -0800170
171 /*
172 * This is tortured. Maintain a vlib-address-space private
173 * pool of client registrations. We use the shared-memory virtual
174 * address of client structure as a handle, to allow direct
175 * manipulation of context quota vbls from the client library.
176 *
177 * This scheme causes trouble w/ API message trace replay, since
178 * some random VA from clib_mem_alloc() certainly won't
179 * occur in the Linux sim. The (very) few places
180 * that care need to use the pool index.
181 *
182 * Putting the registration object(s) into a pool in shared memory and
183 * using the pool index as a handle seems like a great idea.
184 * Unfortunately, each and every reference to that pool would need
185 * to be protected by a mutex:
186 *
187 * Client VLIB
188 * ------ ----
189 * convert pool index to
190 * pointer.
191 * <deschedule>
192 * expand pool
193 * <deschedule>
194 * kaboom!
195 */
196
197 pool_get (am->vl_clients, regpp);
198
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100199 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800200 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
201
202 regp = *regpp;
Dave Barachb7b92992018-10-17 10:38:51 -0400203 clib_memset (regp, 0, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800204 regp->registration_type = REGISTRATION_TYPE_SHMEM;
205 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100206 regp->vlib_rp = am->vlib_rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800207 regp->shmem_hdr = am->shmem_hdr;
Florin Corasb384b542018-01-15 01:08:33 -0800208 regp->clib_file_index = am->shmem_hdr->clib_file_index;
Florin Corase86a8ed2018-01-05 03:20:25 -0800209
210 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200211 VL_MSG_API_SVM_QUEUE_UNPOISON (q);
Florin Corase86a8ed2018-01-05 03:20:25 -0800212
Ole Troan7adaa222019-08-27 15:05:27 +0200213 regp->name = format (0, "%s", mp->name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800214 vec_add1 (regp->name, 0);
215
216 if (am->serialized_message_table_in_shmem == 0)
217 am->serialized_message_table_in_shmem =
218 vl_api_serialize_message_table (am, 0);
219
Florin Corase25c9bf2018-08-06 12:19:29 -0700220 if (am->vlib_rp != am->vlib_primary_rp)
221 msg_table = vl_api_serialize_message_table (am, 0);
222 else
223 msg_table = am->serialized_message_table_in_shmem;
224
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100225 vl_msg_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800226
227 rp = vl_msg_api_alloc (sizeof (*rp));
228 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
229 rp->handle = (uword) regp;
230 rp->index = vl_msg_api_handle_from_index_and_epoch
231 (regp->vl_api_registration_pool_index,
232 am->shmem_hdr->application_restarts);
233 rp->context = mp->context;
234 rp->response = ntohl (rv);
Florin Corase25c9bf2018-08-06 12:19:29 -0700235 rp->message_table = pointer_to_uword (msg_table);
Florin Corase86a8ed2018-01-05 03:20:25 -0800236
237 vl_msg_api_send_shmem (q, (u8 *) & rp);
238}
239
Dave Barach38ca6e62020-07-17 17:16:34 -0400240void
Florin Corase86a8ed2018-01-05 03:20:25 -0800241vl_api_call_reaper_functions (u32 client_index)
242{
243 clib_error_t *error = 0;
244 _vl_msg_api_function_list_elt_t *i;
245
Dave Barach39d69112019-11-27 11:42:13 -0500246 i = vlibapi_get_main ()->reaper_function_registrations;
Florin Corase86a8ed2018-01-05 03:20:25 -0800247 while (i)
248 {
249 error = i->f (client_index);
250 if (error)
251 clib_error_report (error);
252 i = i->next_init_function;
253 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800254}
255
256/*
257 * vl_api_memclnt_delete_t_handler
258 */
259void
260vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
261{
262 vl_api_registration_t **regpp;
263 vl_api_registration_t *regp;
264 vl_api_memclnt_delete_reply_t *rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800265 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500266 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800267 u32 handle, client_index, epoch;
268
269 handle = mp->index;
270
Dave Barach38ca6e62020-07-17 17:16:34 -0400271 vl_api_call_reaper_functions (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800272
273 epoch = vl_msg_api_handle_get_epoch (handle);
274 client_index = vl_msg_api_handle_get_index (handle);
275
276 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
277 {
278 clib_warning
279 ("Stale clnt delete index %d old epoch %d cur epoch %d",
280 client_index, epoch,
281 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
282 return;
283 }
284
Florin Corasb384b542018-01-15 01:08:33 -0800285 regpp = pool_elt_at_index (am->vl_clients, client_index);
Florin Corase86a8ed2018-01-05 03:20:25 -0800286
287 if (!pool_is_free (am->vl_clients, regpp))
288 {
289 int i;
290 regp = *regpp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800291 int private_registration = 0;
292
Florin Coraseaec2a62018-12-04 16:34:05 -0800293 /* Send reply unless client asked us to do the cleanup */
294 if (!mp->do_cleanup)
Florin Corase86a8ed2018-01-05 03:20:25 -0800295 {
Florin Coraseaec2a62018-12-04 16:34:05 -0800296 /*
297 * Note: the API message handling path will set am->vlib_rp
298 * as appropriate for pairwise / private memory segments
299 */
300 rp = vl_msg_api_alloc (sizeof (*rp));
301 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
302 rp->handle = mp->handle;
303 rp->response = 1;
304
305 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
306 if (client_index != regp->vl_api_registration_pool_index)
307 {
308 clib_warning ("mismatch client_index %d pool_index %d",
309 client_index,
310 regp->vl_api_registration_pool_index);
311 vl_msg_api_free (rp);
312 return;
313 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800314 }
315
Benoît Gannef26b2512019-09-11 16:43:44 +0200316 /* No dangling references, please */
317 *regpp = 0;
318
Florin Corase86a8ed2018-01-05 03:20:25 -0800319 /* For horizontal scaling, add a hash table... */
320 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
321 {
322 /* Is this a pairwise / private API segment? */
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100323 if (am->vlib_private_rps[i] == am->vlib_rp)
Florin Corase86a8ed2018-01-05 03:20:25 -0800324 {
325 /* Note: account for the memfd header page */
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100326 uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE;
327 uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE;
Florin Corase86a8ed2018-01-05 03:20:25 -0800328
329 /*
330 * Kill the registration pool element before we make
331 * the index vanish forever
332 */
333 pool_put_index (am->vl_clients,
334 regp->vl_api_registration_pool_index);
335
336 vec_delete (am->vlib_private_rps, 1, i);
337 /* Kill it, accounting for the memfd header page */
338 if (munmap ((void *) virtual_base, virtual_size) < 0)
339 clib_unix_warning ("munmap");
340 /* Reset the queue-length-address cache */
341 vec_reset_length (vl_api_queue_cursizes);
342 private_registration = 1;
343 break;
344 }
345 }
346
Florin Corase86a8ed2018-01-05 03:20:25 -0800347 if (private_registration == 0)
348 {
349 pool_put_index (am->vl_clients,
350 regp->vl_api_registration_pool_index);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100351 oldheap = vl_msg_push_heap ();
Florin Coraseaec2a62018-12-04 16:34:05 -0800352 if (mp->do_cleanup)
353 svm_queue_free (regp->vl_input_queue);
Ole Troan73710c72018-06-04 22:27:49 +0200354 vec_free (regp->name);
Florin Corase86a8ed2018-01-05 03:20:25 -0800355 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400356 clib_memset (regp, 0xF1, sizeof (*regp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800357 clib_mem_free (regp);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100358 vl_msg_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800359 /*
360 * These messages must be freed manually, since they're set up
361 * as "bounce" messages. In the private_registration == 1 case,
362 * we kill the shared-memory segment which contains the message
363 * with munmap.
364 */
365 vl_msg_api_free (mp);
366 }
367 }
368 else
369 {
370 clib_warning ("unknown client ID %d", mp->index);
371 }
372}
373
374/**
375 * client answered a ping, stave off the grim reaper...
376 */
377void
378 vl_api_memclnt_keepalive_reply_t_handler
379 (vl_api_memclnt_keepalive_reply_t * mp)
380{
381 vl_api_registration_t *regp;
382 vlib_main_t *vm = vlib_get_main ();
383
384 regp = vl_api_client_index_to_registration (mp->context);
385 if (regp)
386 {
387 regp->last_heard = vlib_time_now (vm);
388 regp->unanswered_pings = 0;
389 }
390 else
391 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
392}
393
394/**
395 * We can send ourselves these messages if someone uses the
396 * builtin binary api test tool...
397 */
398static void
399vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
400{
401 vl_api_memclnt_keepalive_reply_t *rmp;
402 api_main_t *am;
403 vl_shmem_hdr_t *shmem_hdr;
404
Dave Barach39d69112019-11-27 11:42:13 -0500405 am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800406 shmem_hdr = am->shmem_hdr;
407
408 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400409 clib_memset (rmp, 0, sizeof (*rmp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800410 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
411 rmp->context = mp->context;
412 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
413}
414
Dave Barachc89c7672019-07-19 17:40:18 -0400415/*
416 * To avoid filling the API trace buffer with boring messages,
417 * don't trace memclnt_keepalive[_reply] msgs
418 */
419
Florin Corase86a8ed2018-01-05 03:20:25 -0800420#define foreach_vlib_api_msg \
Dave Barachc89c7672019-07-19 17:40:18 -0400421_(MEMCLNT_CREATE, memclnt_create, 1) \
422_(MEMCLNT_DELETE, memclnt_delete, 1) \
423_(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
424_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
Florin Corase86a8ed2018-01-05 03:20:25 -0800425
426/*
427 * memory_api_init
428 */
429int
430vl_mem_api_init (const char *region_name)
431{
432 int rv;
Dave Barach39d69112019-11-27 11:42:13 -0500433 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800434 vl_msg_api_msg_config_t cfg;
435 vl_msg_api_msg_config_t *c = &cfg;
436 vl_shmem_hdr_t *shm;
437 vlib_main_t *vm = vlib_get_main ();
438
Dave Barachb7b92992018-10-17 10:38:51 -0400439 clib_memset (c, 0, sizeof (*c));
Florin Corase86a8ed2018-01-05 03:20:25 -0800440
441 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
442 return rv;
443
Dave Barachc89c7672019-07-19 17:40:18 -0400444#define _(N,n,t) do { \
Florin Corase86a8ed2018-01-05 03:20:25 -0800445 c->id = VL_API_##N; \
446 c->name = #n; \
447 c->handler = vl_api_##n##_t_handler; \
448 c->cleanup = vl_noop_handler; \
449 c->endian = vl_api_##n##_t_endian; \
450 c->print = vl_api_##n##_t_print; \
451 c->size = sizeof(vl_api_##n##_t); \
Dave Barachc89c7672019-07-19 17:40:18 -0400452 c->traced = t; /* trace, so these msgs print */ \
Florin Corase86a8ed2018-01-05 03:20:25 -0800453 c->replay = 0; /* don't replay client create/delete msgs */ \
454 c->message_bounce = 0; /* don't bounce this message */ \
455 vl_msg_api_config(c);} while (0);
456
457 foreach_vlib_api_msg;
458#undef _
459
Ole Troan3459ece2021-09-27 17:11:34 +0200460#define vl_msg_name_crc_list
461#include <vlibmemory/memclnt.api.h>
462#undef vl_msg_name_crc_list
463
464#define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
465 foreach_vl_msg_name_crc_memclnt;
466#undef _
467
Florin Corase86a8ed2018-01-05 03:20:25 -0800468 /*
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{
Dave Barach39d69112019-11-27 11:42:13 -0500490 api_main_t *am = vlibapi_get_main ();
Dave Barach048a4e52018-06-01 18:52:25 -0400491 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;
Florin Coras8d820852019-11-27 09:15:25 -0800505 svm_queue_t *q;
Dave Barach39d69112019-11-27 11:42:13 -0500506 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800507
508 q = regp->vl_input_queue;
509
510 /*
511 * If the queue head is moving, assume that the client is processing
512 * messages and skip the ping. This heuristic may fail if the queue
513 * is in the same position as last time, net of wrapping; in which
514 * case, the client will receive a keepalive.
515 */
516 if (regp->last_queue_head != q->head)
517 {
518 regp->last_heard = now;
519 regp->unanswered_pings = 0;
520 regp->last_queue_head = q->head;
521 return;
522 }
523
524 /*
525 * push/pop shared memory segment, so this routine
526 * will work with "normal" as well as "private segment"
527 * memory clients..
528 */
529
Florin Coras8d820852019-11-27 09:15:25 -0800530 mp = vl_mem_api_alloc_as_if_client_w_reg (regp, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400531 clib_memset (mp, 0, sizeof (*mp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800532 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
533 mp->context = mp->client_index =
534 vl_msg_api_handle_from_index_and_epoch
535 (regp->vl_api_registration_pool_index,
536 am->shmem_hdr->application_restarts);
537
538 regp->unanswered_pings++;
539
540 /* Failure-to-send due to a stuffed queue is absolutely expected */
541 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
Florin Coras8d820852019-11-27 09:15:25 -0800542 vl_msg_api_free_w_region (regp->vlib_rp, mp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800543}
544
Florin Corasb384b542018-01-15 01:08:33 -0800545static void
546vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
547 vl_api_registration_t ** regpp,
548 u32 ** dead_indices,
549 u32 ** confused_indices)
550{
551 vl_api_registration_t *regp = *regpp;
552 if (regp)
553 {
554 /* If we haven't heard from this client recently... */
555 if (regp->last_heard < (now - 10.0))
556 {
557 if (regp->unanswered_pings == 2)
558 {
559 svm_queue_t *q;
560 q = regp->vl_input_queue;
561 if (kill (q->consumer_pid, 0) >= 0)
562 {
563 clib_warning ("REAPER: lazy binary API client '%s'",
564 regp->name);
565 regp->unanswered_pings = 0;
566 regp->last_heard = now;
567 }
568 else
569 {
570 clib_warning ("REAPER: binary API client '%s' died",
571 regp->name);
572 vec_add1 (*dead_indices, regpp - am->vl_clients);
573 }
574 }
575 else
576 send_memclnt_keepalive (regp, now);
577 }
578 else
579 regp->unanswered_pings = 0;
580 }
581 else
582 {
583 clib_warning ("NULL client registration index %d",
584 regpp - am->vl_clients);
585 vec_add1 (*confused_indices, regpp - am->vl_clients);
586 }
587}
588
Florin Corase86a8ed2018-01-05 03:20:25 -0800589void
590vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
591{
592 vl_api_registration_t **regpp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800593 static u32 *dead_indices;
594 static u32 *confused_indices;
595
596 vec_reset_length (dead_indices);
597 vec_reset_length (confused_indices);
598
599 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100600 pool_foreach (regpp, am->vl_clients) {
Florin Corasb384b542018-01-15 01:08:33 -0800601 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
602 &confused_indices);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100603 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800604 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800605
Florin Corase86a8ed2018-01-05 03:20:25 -0800606 /* This should "never happen," but if it does, fix it... */
607 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
608 {
609 int i;
610 for (i = 0; i < vec_len (confused_indices); i++)
611 {
612 pool_put_index (am->vl_clients, confused_indices[i]);
613 }
614 }
615
616 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
617 {
618 int i;
Florin Corase86a8ed2018-01-05 03:20:25 -0800619 void *oldheap;
620
621 /* Allow the application to clean up its registrations */
622 for (i = 0; i < vec_len (dead_indices); i++)
623 {
624 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
625 if (regpp)
626 {
627 u32 handle;
628
629 handle = vl_msg_api_handle_from_index_and_epoch
630 (dead_indices[i], shm->application_restarts);
Dave Barach38ca6e62020-07-17 17:16:34 -0400631 vl_api_call_reaper_functions (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800632 }
633 }
634
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100635 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800636
637 for (i = 0; i < vec_len (dead_indices); i++)
638 {
639 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
640 if (regpp)
641 {
642 /* Is this a pairwise SVM segment? */
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100643 if ((*regpp)->vlib_rp != am->vlib_rp)
Florin Corase86a8ed2018-01-05 03:20:25 -0800644 {
645 int i;
646 svm_region_t *dead_rp = (*regpp)->vlib_rp;
647 /* Note: account for the memfd header page */
David Johnsond9818dd2018-12-14 14:53:41 -0500648 uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
649 uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
Florin Corase86a8ed2018-01-05 03:20:25 -0800650
651 /* For horizontal scaling, add a hash table... */
652 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
653 if (am->vlib_private_rps[i] == dead_rp)
654 {
655 vec_delete (am->vlib_private_rps, 1, i);
656 goto found;
657 }
Nathan Skrzypczak5ed3fe32019-11-07 16:00:57 +0100658 svm_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800659 clib_warning ("private rp %llx AWOL", dead_rp);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100660 oldheap = svm_push_data_heap (am->vlib_rp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800661
662 found:
663 /* Kill it, accounting for the memfd header page */
Nathan Skrzypczak5ed3fe32019-11-07 16:00:57 +0100664 svm_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800665 if (munmap ((void *) virtual_base, virtual_size) < 0)
666 clib_unix_warning ("munmap");
667 /* Reset the queue-length-address cache */
668 vec_reset_length (vl_api_queue_cursizes);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100669 oldheap = svm_push_data_heap (am->vlib_rp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800670 }
671 else
672 {
673 /* Poison the old registration */
Dave Barachb7b92992018-10-17 10:38:51 -0400674 clib_memset (*regpp, 0xF3, sizeof (**regpp));
Florin Corase86a8ed2018-01-05 03:20:25 -0800675 clib_mem_free (*regpp);
676 }
677 /* no dangling references, please */
678 *regpp = 0;
679 }
680 else
681 {
682 svm_pop_heap (oldheap);
683 clib_warning ("Duplicate free, client index %d",
684 regpp - am->vl_clients);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100685 oldheap = svm_push_data_heap (am->vlib_rp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800686 }
687 }
688
689 svm_client_scan_this_region_nolock (am->vlib_rp);
690
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100691 vl_msg_pop_heap (oldheap);
Florin Corase86a8ed2018-01-05 03:20:25 -0800692 for (i = 0; i < vec_len (dead_indices); i++)
693 pool_put_index (am->vl_clients, dead_indices[i]);
694 }
695}
696
697static inline int
Florin Coras8d820852019-11-27 09:15:25 -0800698void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp,
699 vlib_main_t * vm, vlib_node_runtime_t * node,
700 u8 is_private)
Florin Corase86a8ed2018-01-05 03:20:25 -0800701{
Florin Coras8d820852019-11-27 09:15:25 -0800702 svm_queue_t *q;
Florin Corase86a8ed2018-01-05 03:20:25 -0800703 uword mp;
Florin Coras8d820852019-11-27 09:15:25 -0800704
705 q = ((vl_shmem_hdr_t *) (void *) vlib_rp->user_ctx)->vl_input_queue;
706
Florin Corase86a8ed2018-01-05 03:20:25 -0800707 if (!svm_queue_sub2 (q, (u8 *) & mp))
708 {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200709 VL_MSG_API_UNPOISON ((void *) mp);
Florin Coras8d820852019-11-27 09:15:25 -0800710 vl_msg_api_handler_with_vm_node (am, vlib_rp, (void *) mp, vm, node,
711 is_private);
Florin Corase86a8ed2018-01-05 03:20:25 -0800712 return 0;
713 }
714 return -1;
715}
716
717int
718vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
719{
Dave Barach39d69112019-11-27 11:42:13 -0500720 api_main_t *am = vlibapi_get_main ();
Florin Coras8d820852019-11-27 09:15:25 -0800721 return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node,
722 0 /* is_private */ );
Florin Corase86a8ed2018-01-05 03:20:25 -0800723}
724
725int
Dave Barachf6c68d72018-11-01 08:12:52 -0400726vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
727{
Dave Barach39d69112019-11-27 11:42:13 -0500728 api_main_t *am = vlibapi_get_main ();
Dave Barachf6c68d72018-11-01 08:12:52 -0400729 int i;
730 uword *tmp, mp;
731
732 /*
733 * Swap pending and processing vectors, then process the RPCs
734 * Avoid deadlock conditions by construction.
735 */
736 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
737 tmp = vm->processing_rpc_requests;
738 vec_reset_length (tmp);
739 vm->processing_rpc_requests = vm->pending_rpc_requests;
740 vm->pending_rpc_requests = tmp;
741 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
742
Dave Barach1bb981d2019-02-26 17:04:40 -0500743 /*
744 * RPCs are used to reflect function calls to thread 0
745 * when the underlying code is not thread-safe.
746 *
747 * Grabbing the thread barrier across a set of RPCs
748 * greatly increases efficiency, and avoids
749 * running afoul of the barrier sync holddown timer.
750 * The barrier sync code supports recursive locking.
751 *
752 * We really need to rewrite RPC-based code...
753 */
754 if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
Dave Barachf6c68d72018-11-01 08:12:52 -0400755 {
Dave Barach1bb981d2019-02-26 17:04:40 -0500756 vl_msg_api_barrier_sync ();
757 for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
758 {
759 mp = vm->processing_rpc_requests[i];
Florin Coras8d820852019-11-27 09:15:25 -0800760 vl_msg_api_handler_with_vm_node (am, am->vlib_rp, (void *) mp, vm,
761 node, 0 /* is_private */ );
Dave Barach1bb981d2019-02-26 17:04:40 -0500762 }
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{
Dave Barach39d69112019-11-27 11:42:13 -0500773 api_main_t *am = vlibapi_get_main ();
Florin Coras8d820852019-11-27 09:15:25 -0800774 return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm,
775 node, 1 /* is_private */ );
Florin Corase86a8ed2018-01-05 03:20:25 -0800776}
777
778vl_api_registration_t *
779vl_mem_api_client_index_to_registration (u32 handle)
780{
781 vl_api_registration_t **regpp;
782 vl_api_registration_t *regp;
Dave Barach39d69112019-11-27 11:42:13 -0500783 api_main_t *am = vlibapi_get_main ();
Florin Corasb384b542018-01-15 01:08:33 -0800784 vl_shmem_hdr_t *shmem_hdr;
Florin Corase86a8ed2018-01-05 03:20:25 -0800785 u32 index;
786
787 index = vl_msg_api_handle_get_index (handle);
Florin Corase86a8ed2018-01-05 03:20:25 -0800788 regpp = am->vl_clients + index;
789
790 if (pool_is_free (am->vl_clients, regpp))
791 {
792 vl_msg_api_increment_missing_client_counter ();
793 return 0;
794 }
795 regp = *regpp;
Florin Corasb384b542018-01-15 01:08:33 -0800796
797 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
798 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
799 {
800 vl_msg_api_increment_missing_client_counter ();
801 return 0;
802 }
803
Florin Corase86a8ed2018-01-05 03:20:25 -0800804 return (regp);
805}
806
807svm_queue_t *
808vl_api_client_index_to_input_queue (u32 index)
809{
810 vl_api_registration_t *regp;
Dave Barach39d69112019-11-27 11:42:13 -0500811 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800812
813 /* Special case: vlib trying to send itself a message */
814 if (index == (u32) ~ 0)
815 return (am->shmem_hdr->vl_input_queue);
816
817 regp = vl_mem_api_client_index_to_registration (index);
818 if (!regp)
819 return 0;
820 return (regp->vl_input_queue);
821}
822
823static clib_error_t *
824setup_memclnt_exit (vlib_main_t * vm)
825{
826 atexit (vl_unmap_shmem);
827 return 0;
828}
829
830VLIB_INIT_FUNCTION (setup_memclnt_exit);
831
832u8 *
833format_api_message_rings (u8 * s, va_list * args)
834{
835 api_main_t *am = va_arg (*args, api_main_t *);
836 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
837 int main_segment = va_arg (*args, int);
838 ring_alloc_t *ap;
839 int i;
840
841 if (shmem_hdr == 0)
842 return format (s, "%8s %8s %8s %8s %8s\n",
843 "Owner", "Size", "Nitems", "Hits", "Misses");
844
845 ap = shmem_hdr->vl_rings;
846
847 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
848 {
849 s = format (s, "%8s %8d %8d %8d %8d\n",
850 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
851 ap++;
852 }
853
854 ap = shmem_hdr->client_rings;
855
856 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
857 {
858 s = format (s, "%8s %8d %8d %8d %8d\n",
859 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
860 ap++;
861 }
862
863 if (main_segment)
864 {
865 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
866 s = format
867 (s,
868 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
869 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
870 shmem_hdr->garbage_collects);
871 }
872 return s;
873}
874
875static clib_error_t *
876vl_api_ring_command (vlib_main_t * vm,
877 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
878{
879 int i;
880 vl_shmem_hdr_t *shmem_hdr;
Dave Barach39d69112019-11-27 11:42:13 -0500881 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800882
883 /* First, dump the primary region rings.. */
884
885 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
886 {
887 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
888 return 0;
889 }
890
891 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
892
893 vlib_cli_output (vm, "Main API segment rings:");
894
895 vlib_cli_output (vm, "%U", format_api_message_rings, am,
896 0 /* print header */ , 0 /* notused */ );
897
898 vlib_cli_output (vm, "%U", format_api_message_rings, am,
899 shmem_hdr, 1 /* main segment */ );
900
901 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
902 {
903 svm_region_t *vlib_rp = am->vlib_private_rps[i];
904 shmem_hdr = (void *) vlib_rp->user_ctx;
905 vl_api_registration_t **regpp;
906 vl_api_registration_t *regp = 0;
907
908 /* For horizontal scaling, add a hash table... */
909 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100910 pool_foreach (regpp, am->vl_clients)
911 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800912 regp = *regpp;
913 if (regp && regp->vlib_rp == vlib_rp)
914 {
915 vlib_cli_output (vm, "%s segment rings:", regp->name);
916 goto found;
917 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100918 }
Florin Corase86a8ed2018-01-05 03:20:25 -0800919 vlib_cli_output (vm, "regp %llx not found?", regp);
920 continue;
921 /* *INDENT-ON* */
922 found:
923 vlib_cli_output (vm, "%U", format_api_message_rings, am,
924 0 /* print header */ , 0 /* notused */ );
925 vlib_cli_output (vm, "%U", format_api_message_rings, am,
926 shmem_hdr, 0 /* main segment */ );
927 }
928
929 return 0;
930}
931
932/*?
933 * Display binary api message allocation ring statistics
934?*/
935/* *INDENT-OFF* */
936VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
937{
938 .path = "show api ring-stats",
939 .short_help = "Message ring statistics",
940 .function = vl_api_ring_command,
941};
942/* *INDENT-ON* */
943
944clib_error_t *
945vlibmemory_init (vlib_main_t * vm)
946{
Dave Barach39d69112019-11-27 11:42:13 -0500947 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800948 svm_map_region_args_t _a, *a = &_a;
Dave Barachb2204672018-11-30 16:46:29 -0500949 u8 *remove_path1, *remove_path2;
Dave Barachf8d50682019-05-14 18:01:44 -0400950 void vlibsocket_reference (void);
951
952 vlibsocket_reference ();
Dave Barachb2204672018-11-30 16:46:29 -0500953
954 /*
955 * By popular request / to avoid support fires, remove any old api segment
956 * files Right Here.
957 */
958 if (am->root_path == 0)
959 {
960 remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
961 remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
962 }
963 else
964 {
965 remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
966 remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
967 }
968
969 (void) unlink ((char *) remove_path1);
970 (void) unlink ((char *) remove_path2);
971
972 vec_free (remove_path1);
973 vec_free (remove_path2);
Florin Corase86a8ed2018-01-05 03:20:25 -0800974
Dave Barachb7b92992018-10-17 10:38:51 -0400975 clib_memset (a, 0, sizeof (*a));
Florin Corase86a8ed2018-01-05 03:20:25 -0800976 a->root_path = am->root_path;
977 a->name = SVM_GLOBAL_REGION_NAME;
978 a->baseva = (am->global_baseva != 0) ?
Damjan Marionaec8f892018-01-08 16:35:35 +0100979 am->global_baseva : +svm_get_global_region_base_va ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800980 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
981 a->flags = SVM_FLAGS_NODATA;
982 a->uid = am->api_uid;
983 a->gid = am->api_gid;
984 a->pvt_heap_size =
985 (am->global_pvt_heap_size !=
986 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
987
988 svm_region_init_args (a);
989
Dave Barachf8d50682019-05-14 18:01:44 -0400990 return 0;
Florin Corase86a8ed2018-01-05 03:20:25 -0800991}
992
Florin Corase86a8ed2018-01-05 03:20:25 -0800993void
994vl_set_memory_region_name (const char *name)
995{
Dave Barach39d69112019-11-27 11:42:13 -0500996 api_main_t *am = vlibapi_get_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800997 am->region_name = name;
998}
999
1000/*
1001 * fd.io coding-style-patch-verification: ON
1002 *
1003 * Local Variables:
1004 * eval: (c-set-style "gnu")
1005 * End:
1006 */