blob: 1684acfd622a1c964820d604f2f950ff0efa8e3e [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 *------------------------------------------------------------------
3 * memclnt_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
5 *
6 *
7 * Copyright (c) 2009 Cisco and/or its affiliates.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at:
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *------------------------------------------------------------------
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
Ole Troan6855f6c2016-04-09 03:16:30 +020024#include <stddef.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <string.h>
26#include <unistd.h>
27#include <signal.h>
28#include <vppinfra/format.h>
29#include <vppinfra/byte_order.h>
30#include <vppinfra/error.h>
31#include <vlib/vlib.h>
32#include <vlib/unix/unix.h>
33#include <vlibmemory/api.h>
34#include <vlibmemory/unix_shared_memory_queue.h>
35
36#include <vlibmemory/vl_memory_msg_enum.h>
37
38#define vl_typedefs
39#include <vlibmemory/vl_memory_api_h.h>
40#undef vl_typedefs
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042static inline void *vl_msg_api_alloc_internal(int nbytes, int pool)
43{
44 int i;
45 msgbuf_t *rv;
46 ring_alloc_t *ap;
47 unix_shared_memory_queue_t *q;
48 void *oldheap;
49 vl_shmem_hdr_t *shmem_hdr;
50 api_main_t *am = &api_main;
51
52 shmem_hdr = am->shmem_hdr;
53
54 if (shmem_hdr == 0) {
55 clib_warning ("shared memory header NULL");
56 return 0;
57 }
58
59 /* account for the msgbuf_t header*/
60 nbytes += sizeof(msgbuf_t);
61
62 if (shmem_hdr->vl_rings == 0) {
63 clib_warning ("vl_rings NULL");
64 return 0;
65 }
66
67 if (shmem_hdr->client_rings == 0) {
68 clib_warning ("client_rings NULL");
69 return 0;
70 }
71
72 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
73 for (i = 0; i < vec_len (ap); i++) {
74 /* Too big? */
75 if (nbytes > ap[i].size) {
76 continue;
77 }
78
79 q = ap[i].rp;
80 if (pool == 0) {
81 pthread_mutex_lock(&q->mutex);
82 }
83 rv = (msgbuf_t *) (&q->data[0] + q->head*q->elsize);
84 /*
85 * Is this item still in use?
86 */
87 if (rv->q) {
88 /* yes, loser; try next larger pool */
89 ap[i].misses++;
90 if (pool == 0)
91 pthread_mutex_unlock(&q->mutex);
92 continue;
93 }
94 /* OK, we have a winner */
95 ap[i].hits++;
96 /*
97 * Remember the source queue, although we
98 * don't need to know the queue to free the item.
99 */
100 rv->q = q;
101 q->head++;
102 if (q->head == q->maxsize)
103 q->head = 0;
104
105 if (pool == 0)
106 pthread_mutex_unlock(&q->mutex);
107 goto out;
108 }
109
110 /*
111 * Request too big, or head element of all size-compatible rings
112 * still in use. Fall back to shared-memory malloc.
113 */
114 am->ring_misses++;
115
116 pthread_mutex_lock (&am->vlib_rp->mutex);
117 oldheap = svm_push_data_heap (am->vlib_rp);
Ole Troan6855f6c2016-04-09 03:16:30 +0200118 rv = clib_mem_alloc(nbytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 rv->q = 0;
120 svm_pop_heap (oldheap);
121 pthread_mutex_unlock (&am->vlib_rp->mutex);
122
123 out:
Ole Troan6855f6c2016-04-09 03:16:30 +0200124 rv->data_len = htonl(nbytes - sizeof(msgbuf_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 return(rv->data);
126}
127
128void *vl_msg_api_alloc (int nbytes)
129{
130 int pool;
131 api_main_t *am = &api_main;
132 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
133
134 /*
135 * Clients use pool-0, vlib proc uses pool 1
136 */
137 pool = (am->our_pid == shmem_hdr->vl_pid);
138 return vl_msg_api_alloc_internal (nbytes, pool);
139}
140
141void *vl_msg_api_alloc_as_if_client (int nbytes)
142{
143 return vl_msg_api_alloc_internal (nbytes, 0);
144}
145
146void vl_msg_api_free(void *a)
147{
148 msgbuf_t *rv;
149 void *oldheap;
150 api_main_t *am = &api_main;
151
Ole Troan6855f6c2016-04-09 03:16:30 +0200152 rv = (msgbuf_t *)(((u8 *)a) - offsetof(msgbuf_t, data));
153
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 /*
155 * Here's the beauty of the scheme. Only one proc/thread has
156 * control of a given message buffer. To free a buffer, we just clear the
157 * queue field, and leave. No locks, no hits, no errors...
158 */
159 if (rv->q) {
160 rv->q = 0;
161 return;
162 }
163
164 pthread_mutex_lock (&am->vlib_rp->mutex);
165 oldheap = svm_push_data_heap (am->vlib_rp);
166 clib_mem_free (rv);
167 svm_pop_heap (oldheap);
168 pthread_mutex_unlock (&am->vlib_rp->mutex);
169}
170
171static void vl_msg_api_free_nolock (void *a)
172{
173 msgbuf_t *rv;
174 void *oldheap;
175 api_main_t *am = &api_main;
176
Ole Troan6855f6c2016-04-09 03:16:30 +0200177 rv = (msgbuf_t *)(((u8 *)a) - offsetof(msgbuf_t, data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 /*
179 * Here's the beauty of the scheme. Only one proc/thread has
180 * control of a given message buffer. To free a buffer, we just clear the
181 * queue field, and leave. No locks, no hits, no errors...
182 */
183 if (rv->q) {
184 rv->q = 0;
185 return;
186 }
187
188 oldheap = svm_push_data_heap (am->vlib_rp);
189 clib_mem_free (rv);
190 svm_pop_heap (oldheap);
191}
192
Dave Barach309bef22016-01-22 16:09:52 -0500193void vl_set_memory_root_path (char *name)
194{
195 api_main_t *am = &api_main;
196
197 am->root_path = name;
198}
199
Dave Barach16c75df2016-05-31 14:05:46 -0400200void vl_set_memory_uid (int uid)
201{
202 api_main_t *am = &api_main;
203
204 am->api_uid = uid;
205}
206
207void vl_set_memory_gid (int gid)
208{
209 api_main_t *am = &api_main;
210
211 am->api_gid = gid;
212}
213
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214int vl_map_shmem (char *region_name, int is_vlib)
215{
Dave Barach16c75df2016-05-31 14:05:46 -0400216 svm_map_region_args_t _a, *a = &_a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 svm_region_t *vlib_rp, *root_rp;
218 void *oldheap;
219 vl_shmem_hdr_t *shmem_hdr=0;
220 api_main_t *am = &api_main;
221 int i;
222 struct timespec ts, tsrem;
223
224 if (is_vlib == 0)
Dave Barach309bef22016-01-22 16:09:52 -0500225 svm_region_init_chroot(am->root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Dave Barach16c75df2016-05-31 14:05:46 -0400227 memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
229 a->name = region_name;
230 a->size = 16<<20;
231 a->flags = SVM_FLAGS_MHEAP;
Dave Barach16c75df2016-05-31 14:05:46 -0400232 a->uid = am->api_uid;
233 a->gid = am->api_gid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
235 vlib_rp = svm_region_find_or_create (a);
236
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 if (vlib_rp == 0)
238 return (-2);
239
240 pthread_mutex_lock (&vlib_rp->mutex);
241 /* Has someone else set up the shared-memory variable table? */
242 if (vlib_rp->user_ctx) {
243 am->shmem_hdr = (void *) vlib_rp->user_ctx;
244 am->our_pid = getpid();
245 if (is_vlib) {
246 unix_shared_memory_queue_t *q;
247 uword old_msg;
248 /*
249 * application restart. Reset cached pids, API message
250 * rings, list of clients; otherwise, various things
251 * fail. (e.g. queue non-empty notification)
252 */
253
254 /* ghosts keep the region from disappearing properly */
255 svm_client_scan_this_region_nolock(vlib_rp);
256 am->shmem_hdr->application_restarts++;
257 q = am->shmem_hdr->vl_input_queue;
258 am->shmem_hdr->vl_pid = getpid();
259 q->consumer_pid = am->shmem_hdr->vl_pid;
260 /* Drain the input queue, freeing msgs */
261 for (i = 0; i < 10; i++) {
262 if (pthread_mutex_trylock (&q->mutex) == 0) {
263 pthread_mutex_unlock (&q->mutex);
264 goto mutex_ok;
265 }
266 ts.tv_sec = 0;
267 ts.tv_nsec = 10000*1000; /* 10 ms */
268 while (nanosleep(&ts, &tsrem) < 0)
269 ts = tsrem;
270 }
271 /* Mutex buggered, "fix" it */
272 memset (&q->mutex, 0, sizeof (q->mutex));
273 clib_warning ("forcibly release main input queue mutex");
274
275 mutex_ok:
276 am->vlib_rp = vlib_rp;
277 while (unix_shared_memory_queue_sub (q,
278 (u8 *)&old_msg,
279 1 /* nowait */)
280 != -2 /* queue underflow */) {
281 vl_msg_api_free_nolock ((void *)old_msg);
282 am->shmem_hdr->restart_reclaims++;
283 }
284 pthread_mutex_unlock (&vlib_rp->mutex);
285 root_rp = svm_get_root_rp();
286 ASSERT(root_rp);
287 /* Clean up the root region client list */
288 pthread_mutex_lock (&root_rp->mutex);
289 svm_client_scan_this_region_nolock (root_rp);
Dave Barach599839d2016-06-07 17:35:38 -0400290 pthread_mutex_unlock (&root_rp->mutex);
Dave Barach16c75df2016-05-31 14:05:46 -0400291 }
292 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 am->vlib_rp = vlib_rp;
294 vec_add1(am->mapped_shmem_regions, vlib_rp);
295 return 0;
296 }
297 /* Clients simply have to wait... */
298 if (!is_vlib) {
299 pthread_mutex_unlock (&vlib_rp->mutex);
300
301 /* Wait up to 100 seconds... */
302 for (i = 0; i < 10000; i++) {
303 ts.tv_sec = 0;
304 ts.tv_nsec = 10000*1000; /* 10 ms */
305 while (nanosleep(&ts, &tsrem) < 0)
306 ts = tsrem;
307 if (vlib_rp->user_ctx)
308 goto ready;
309 }
310 /* Clean up and leave... */
311 svm_region_unmap (vlib_rp);
312 clib_warning ("region init fail");
313 return (-2);
314
315 ready:
316 am->shmem_hdr = (void *)vlib_rp->user_ctx;
317 am->our_pid = getpid();
318 am->vlib_rp = vlib_rp;
319 vec_add1(am->mapped_shmem_regions, vlib_rp);
320 return 0;
321 }
322
323 /* Nope, it's our problem... */
324
325 oldheap = svm_push_data_heap (vlib_rp);
326
327 vec_validate(shmem_hdr, 0);
328 shmem_hdr->version = VL_SHM_VERSION;
329
330 /* vlib main input queue */
331 shmem_hdr->vl_input_queue =
332 unix_shared_memory_queue_init (1024, sizeof (uword), getpid(),
333 am->vlib_signal);
334
335 /* Set up the msg ring allocator */
336#define _(sz,n) \
337 do { \
338 ring_alloc_t _rp; \
339 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
340 _rp.size = (sz); \
341 _rp.nitems = n; \
342 _rp.hits = 0; \
343 _rp.misses = 0; \
344 vec_add1(shmem_hdr->vl_rings, _rp); \
345 } while (0);
346
347 foreach_vl_aring_size;
348#undef _
349
350#define _(sz,n) \
351 do { \
352 ring_alloc_t _rp; \
353 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
354 _rp.size = (sz); \
355 _rp.nitems = n; \
356 _rp.hits = 0; \
357 _rp.misses = 0; \
358 vec_add1(shmem_hdr->client_rings, _rp); \
359 } while (0);
360
361 foreach_clnt_aring_size;
362#undef _
363
364 am->shmem_hdr = shmem_hdr;
365 am->vlib_rp = vlib_rp;
366 am->our_pid = getpid();
367 if (is_vlib)
368 am->shmem_hdr->vl_pid = am->our_pid;
369
370 svm_pop_heap (oldheap);
371
372 /*
373 * After absolutely everything that a client might see is set up,
374 * declare the shmem region valid
375 */
376 vlib_rp->user_ctx = shmem_hdr;
377
378 pthread_mutex_unlock (&vlib_rp->mutex);
379 vec_add1(am->mapped_shmem_regions, vlib_rp);
380 return 0;
381}
382
383void vl_register_mapped_shmem_region(svm_region_t *rp)
384{
385 api_main_t *am = &api_main;
386
387 vec_add1(am->mapped_shmem_regions, rp);
388}
389
390void vl_unmap_shmem (void)
391{
392 svm_region_t *rp;
393 int i;
394 api_main_t *am = &api_main;
395
396 if (! svm_get_root_rp())
397 return;
398
399 for (i = 0; i < vec_len(am->mapped_shmem_regions); i++) {
400 rp = am->mapped_shmem_regions[i];
401 svm_region_unmap (rp);
402 }
403
404 vec_free(am->mapped_shmem_regions);
405 am->shmem_hdr = 0;
406
407 svm_region_exit ();
408 /* $$$ more careful cleanup, valgrind run... */
409 vec_free (am->msg_handlers);
410 vec_free (am->msg_endian_handlers);
411 vec_free (am->msg_print_handlers);
412}
413
414void vl_msg_api_send_shmem (unix_shared_memory_queue_t *q, u8 *elem)
415{
416 api_main_t *am = &api_main;
417 uword *trace = (uword *)elem;
418
419 if (am->tx_trace && am->tx_trace->enabled)
420 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
421
422 (void)unix_shared_memory_queue_add(q, elem, 0 /* nowait */);
423}
424
425void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t *q, u8 *elem)
426{
427 api_main_t *am = &api_main;
428 uword *trace = (uword *)elem;
429
430 if (am->tx_trace && am->tx_trace->enabled)
431 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
432
433 (void)unix_shared_memory_queue_add_nolock (q, elem);
434}
435
436static void vl_api_memclnt_create_reply_t_handler (
437 vl_api_memclnt_create_reply_t *mp)
438{
439 api_main_t *am = &api_main;
440 int rv;
441
442 am->my_client_index = mp->index;
443 am->my_registration = (vl_api_registration_t *)(uword)
444 mp->handle;
445
446 rv = ntohl(mp->response);
447
448 if (rv < 0)
449 clib_warning ("WARNING: API mismatch detected");
450}
451
452void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
453 __attribute__((weak));
454
455void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
456{
457 int i;
458
459 for (i = 0; i < ARRAY_LEN(mp->api_versions); i++)
460 mp->api_versions[i] = 0;
461}
462
463int vl_client_connect (char *name, int ctx_quota, int input_queue_size)
464{
465 svm_region_t *svm;
466 vl_api_memclnt_create_t *mp;
467 vl_api_memclnt_create_reply_t *rp;
468 unix_shared_memory_queue_t *vl_input_queue;
469 vl_shmem_hdr_t *shmem_hdr;
470 int rv=0;
471 void *oldheap;
472 api_main_t *am = &api_main;
473
474 if (am->my_registration) {
475 clib_warning ("client %s already connected...", name);
476 return -1;
477 }
478
479 if (am->vlib_rp == 0) {
480 clib_warning ("am->vlib_rp NULL");
481 return -1;
482 }
483
484 svm = am->vlib_rp;
485 shmem_hdr = am->shmem_hdr;
486
487 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) {
488 clib_warning ("shmem_hdr / input queue NULL");
489 return -1;
490 }
491
492 pthread_mutex_lock (&svm->mutex);
493 oldheap = svm_push_data_heap(svm);
494 vl_input_queue =
495 unix_shared_memory_queue_init (input_queue_size, sizeof(uword),
496 getpid(), 0);
497 pthread_mutex_unlock(&svm->mutex);
498 svm_pop_heap (oldheap);
499
500 am->my_client_index = ~0;
501 am->my_registration = 0;
502 am->vl_input_queue = vl_input_queue;
503
504 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_create_t));
505 memset(mp, 0, sizeof (*mp));
506 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_CREATE);
507 mp->ctx_quota = ctx_quota;
508 mp->input_queue = (uword)vl_input_queue;
509 strncpy ((char *) mp->name, name, sizeof(mp->name)-1);
510
511 vl_client_add_api_signatures(mp);
512
513 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
514
515 while (1) {
516 int qstatus;
517 struct timespec ts, tsrem;
518 int i;
519
520 /* Wait up to 10 seconds */
521 for (i = 0; i < 1000; i++) {
522 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp,
523 1 /* nowait */);
524 if (qstatus == 0)
525 goto read_one_msg;
526 ts.tv_sec = 0;
527 ts.tv_nsec = 10000*1000; /* 10 ms */
528 while (nanosleep(&ts, &tsrem) < 0)
529 ts = tsrem;
530 }
531 /* Timeout... */
532 clib_warning ("memclnt_create_reply timeout");
533 return -1;
534
535 read_one_msg:
536 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) {
537 clib_warning ("unexpected reply: id %d", ntohs(rp->_vl_msg_id));
538 continue;
539 }
540 rv = clib_net_to_host_u32(rp->response);
541
542 vl_msg_api_handler((void *)rp);
543 break;
544 }
545 return (rv);
546}
547
548static void vl_api_memclnt_delete_reply_t_handler (
549 vl_api_memclnt_delete_reply_t *mp)
550{
551 void *oldheap;
552 api_main_t *am = &api_main;
553
554 pthread_mutex_lock (&am->vlib_rp->mutex);
555 oldheap = svm_push_data_heap(am->vlib_rp);
556 unix_shared_memory_queue_free (am->vl_input_queue);
557 pthread_mutex_unlock (&am->vlib_rp->mutex);
558 svm_pop_heap (oldheap);
559
560 am->my_client_index = ~0;
561 am->my_registration = 0;
562 am->vl_input_queue = 0;
563}
564
565void vl_client_disconnect (void)
566{
567 vl_api_memclnt_delete_t *mp;
568 vl_api_memclnt_delete_reply_t *rp;
569 unix_shared_memory_queue_t *vl_input_queue;
570 vl_shmem_hdr_t *shmem_hdr;
571 time_t begin;
572 api_main_t *am = &api_main;
573
574 ASSERT(am->vlib_rp);
575 shmem_hdr = am->shmem_hdr;
576 ASSERT(shmem_hdr && shmem_hdr->vl_input_queue);
577
578 vl_input_queue = am->vl_input_queue;
579
580 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_delete_t));
581 memset(mp, 0, sizeof (*mp));
582 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_DELETE);
583 mp->index = am->my_client_index;
584 mp->handle = (uword) am->my_registration;
585
586 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
587
588 /*
589 * Have to be careful here, in case the client is disconnecting
590 * because e.g. the vlib process died, or is unresponsive.
591 */
592
593 begin = time (0);
594 while (1) {
595 time_t now;
596
597 now = time (0);
598
599 if (now >= (begin + 2)) {
600 clib_warning ("peer unresponsive, give up");
601 am->my_client_index = ~0;
602 am->my_registration = 0;
603 am->shmem_hdr = 0;
604 break;
605 }
606 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp, 1) < 0)
607 continue;
608
609 /* drain the queue */
610 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) {
611 vl_msg_api_handler ((void *)rp);
612 continue;
613 }
614 vl_msg_api_handler((void *)rp);
615 break;
616 }
617}
618
619static inline vl_api_registration_t
620*vl_api_client_index_to_registration_internal (u32 handle)
621{
622 vl_api_registration_t **regpp;
623 vl_api_registration_t *regp;
624 api_main_t *am = &api_main;
625 u32 index;
626
627 index = vl_msg_api_handle_get_index (handle);
628 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
629 != vl_msg_api_handle_get_epoch (handle)) {
630 vl_msg_api_increment_missing_client_counter();
631 return 0;
632 }
633
634 regpp = am->vl_clients + index;
635
636 if (pool_is_free(am->vl_clients, regpp)) {
637 vl_msg_api_increment_missing_client_counter();
638 return 0;
639 }
640 regp = *regpp;
641 return (regp);
642}
643
644vl_api_registration_t *vl_api_client_index_to_registration (u32 index)
645{
646 return (vl_api_client_index_to_registration_internal (index));
647}
648
649unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index)
650{
651 vl_api_registration_t *regp;
652
653 regp = vl_api_client_index_to_registration_internal (index);
654 if (!regp)
655 return 0;
656 return (regp->vl_input_queue);
657}
658
659#define foreach_api_client_msg \
660_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
661_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
662
663int vl_client_api_map (char *region_name)
664{
665 int rv;
666
667 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */)) < 0) {
668 return rv;
669 }
670
671#define _(N,n) \
672 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
673 vl_api_##n##_t_handler, \
674 0/* cleanup */, 0/* endian */, 0/* print */, \
675 sizeof(vl_api_##n##_t), 1);
676 foreach_api_client_msg;
677#undef _
678 return 0;
679}
680
681void vl_client_api_unmap (void)
682{
683 vl_unmap_shmem();
684}