2 *------------------------------------------------------------------
3 * memclnt_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
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:
12 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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>
36 #include <vlibmemory/vl_memory_msg_enum.h>
39 #include <vlibmemory/vl_memory_api_h.h>
42 static inline void *vl_msg_api_alloc_internal(int nbytes, int pool)
47 unix_shared_memory_queue_t *q;
49 vl_shmem_hdr_t *shmem_hdr;
50 api_main_t *am = &api_main;
52 shmem_hdr = am->shmem_hdr;
55 clib_warning ("shared memory header NULL");
59 /* account for the msgbuf_t header*/
60 nbytes += sizeof(msgbuf_t);
62 if (shmem_hdr->vl_rings == 0) {
63 clib_warning ("vl_rings NULL");
67 if (shmem_hdr->client_rings == 0) {
68 clib_warning ("client_rings NULL");
72 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
73 for (i = 0; i < vec_len (ap); i++) {
75 if (nbytes > ap[i].size) {
81 pthread_mutex_lock(&q->mutex);
83 rv = (msgbuf_t *) (&q->data[0] + q->head*q->elsize);
85 * Is this item still in use?
88 /* yes, loser; try next larger pool */
91 pthread_mutex_unlock(&q->mutex);
94 /* OK, we have a winner */
97 * Remember the source queue, although we
98 * don't need to know the queue to free the item.
102 if (q->head == q->maxsize)
106 pthread_mutex_unlock(&q->mutex);
111 * Request too big, or head element of all size-compatible rings
112 * still in use. Fall back to shared-memory malloc.
116 pthread_mutex_lock (&am->vlib_rp->mutex);
117 oldheap = svm_push_data_heap (am->vlib_rp);
118 rv = clib_mem_alloc(nbytes);
120 svm_pop_heap (oldheap);
121 pthread_mutex_unlock (&am->vlib_rp->mutex);
124 rv->data_len = htonl(nbytes - sizeof(msgbuf_t));
128 void *vl_msg_api_alloc (int nbytes)
131 api_main_t *am = &api_main;
132 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
135 * Clients use pool-0, vlib proc uses pool 1
137 pool = (am->our_pid == shmem_hdr->vl_pid);
138 return vl_msg_api_alloc_internal (nbytes, pool);
141 void *vl_msg_api_alloc_as_if_client (int nbytes)
143 return vl_msg_api_alloc_internal (nbytes, 0);
146 void vl_msg_api_free(void *a)
150 api_main_t *am = &api_main;
152 rv = (msgbuf_t *)(((u8 *)a) - offsetof(msgbuf_t, data));
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...
164 pthread_mutex_lock (&am->vlib_rp->mutex);
165 oldheap = svm_push_data_heap (am->vlib_rp);
167 svm_pop_heap (oldheap);
168 pthread_mutex_unlock (&am->vlib_rp->mutex);
171 static void vl_msg_api_free_nolock (void *a)
175 api_main_t *am = &api_main;
177 rv = (msgbuf_t *)(((u8 *)a) - offsetof(msgbuf_t, data));
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...
188 oldheap = svm_push_data_heap (am->vlib_rp);
190 svm_pop_heap (oldheap);
193 void vl_set_memory_root_path (char *name)
195 api_main_t *am = &api_main;
197 am->root_path = name;
200 int vl_map_shmem (char *region_name, int is_vlib)
202 svm_map_region_args_t *a = 0;
203 svm_region_t *vlib_rp, *root_rp;
205 vl_shmem_hdr_t *shmem_hdr=0;
206 api_main_t *am = &api_main;
208 struct timespec ts, tsrem;
211 svm_region_init_chroot(am->root_path);
215 a->name = region_name;
217 a->flags = SVM_FLAGS_MHEAP;
219 vlib_rp = svm_region_find_or_create (a);
226 pthread_mutex_lock (&vlib_rp->mutex);
227 /* Has someone else set up the shared-memory variable table? */
228 if (vlib_rp->user_ctx) {
229 am->shmem_hdr = (void *) vlib_rp->user_ctx;
230 am->our_pid = getpid();
232 unix_shared_memory_queue_t *q;
235 * application restart. Reset cached pids, API message
236 * rings, list of clients; otherwise, various things
237 * fail. (e.g. queue non-empty notification)
240 /* ghosts keep the region from disappearing properly */
241 svm_client_scan_this_region_nolock(vlib_rp);
242 am->shmem_hdr->application_restarts++;
243 q = am->shmem_hdr->vl_input_queue;
244 am->shmem_hdr->vl_pid = getpid();
245 q->consumer_pid = am->shmem_hdr->vl_pid;
246 /* Drain the input queue, freeing msgs */
247 for (i = 0; i < 10; i++) {
248 if (pthread_mutex_trylock (&q->mutex) == 0) {
249 pthread_mutex_unlock (&q->mutex);
253 ts.tv_nsec = 10000*1000; /* 10 ms */
254 while (nanosleep(&ts, &tsrem) < 0)
257 /* Mutex buggered, "fix" it */
258 memset (&q->mutex, 0, sizeof (q->mutex));
259 clib_warning ("forcibly release main input queue mutex");
262 am->vlib_rp = vlib_rp;
263 while (unix_shared_memory_queue_sub (q,
266 != -2 /* queue underflow */) {
267 vl_msg_api_free_nolock ((void *)old_msg);
268 am->shmem_hdr->restart_reclaims++;
270 pthread_mutex_unlock (&vlib_rp->mutex);
271 root_rp = svm_get_root_rp();
273 /* Clean up the root region client list */
274 pthread_mutex_lock (&root_rp->mutex);
275 svm_client_scan_this_region_nolock (root_rp);
276 pthread_mutex_unlock (&root_rp->mutex);
278 pthread_mutex_unlock (&vlib_rp->mutex);
280 * Make sure the vlib app is really there...
281 * Wait up to 100 seconds...
283 for (i = 0; i < 10000; i++) {
284 /* Yup, it's there, off we go... */
285 if (kill (am->shmem_hdr->vl_pid, 0) >= 0)
289 ts.tv_nsec = 10000*1000; /* 10 ms */
290 while (nanosleep(&ts, &tsrem) < 0)
295 am->vlib_rp = vlib_rp;
296 vec_add1(am->mapped_shmem_regions, vlib_rp);
299 /* Clients simply have to wait... */
301 pthread_mutex_unlock (&vlib_rp->mutex);
303 /* Wait up to 100 seconds... */
304 for (i = 0; i < 10000; i++) {
306 ts.tv_nsec = 10000*1000; /* 10 ms */
307 while (nanosleep(&ts, &tsrem) < 0)
309 if (vlib_rp->user_ctx)
312 /* Clean up and leave... */
313 svm_region_unmap (vlib_rp);
314 clib_warning ("region init fail");
318 am->shmem_hdr = (void *)vlib_rp->user_ctx;
319 am->our_pid = getpid();
320 am->vlib_rp = vlib_rp;
321 vec_add1(am->mapped_shmem_regions, vlib_rp);
325 /* Nope, it's our problem... */
327 oldheap = svm_push_data_heap (vlib_rp);
329 vec_validate(shmem_hdr, 0);
330 shmem_hdr->version = VL_SHM_VERSION;
332 /* vlib main input queue */
333 shmem_hdr->vl_input_queue =
334 unix_shared_memory_queue_init (1024, sizeof (uword), getpid(),
337 /* Set up the msg ring allocator */
341 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
346 vec_add1(shmem_hdr->vl_rings, _rp); \
349 foreach_vl_aring_size;
355 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
360 vec_add1(shmem_hdr->client_rings, _rp); \
363 foreach_clnt_aring_size;
366 am->shmem_hdr = shmem_hdr;
367 am->vlib_rp = vlib_rp;
368 am->our_pid = getpid();
370 am->shmem_hdr->vl_pid = am->our_pid;
372 svm_pop_heap (oldheap);
375 * After absolutely everything that a client might see is set up,
376 * declare the shmem region valid
378 vlib_rp->user_ctx = shmem_hdr;
380 pthread_mutex_unlock (&vlib_rp->mutex);
381 vec_add1(am->mapped_shmem_regions, vlib_rp);
385 void vl_register_mapped_shmem_region(svm_region_t *rp)
387 api_main_t *am = &api_main;
389 vec_add1(am->mapped_shmem_regions, rp);
392 void vl_unmap_shmem (void)
396 api_main_t *am = &api_main;
398 if (! svm_get_root_rp())
401 for (i = 0; i < vec_len(am->mapped_shmem_regions); i++) {
402 rp = am->mapped_shmem_regions[i];
403 svm_region_unmap (rp);
406 vec_free(am->mapped_shmem_regions);
410 /* $$$ more careful cleanup, valgrind run... */
411 vec_free (am->msg_handlers);
412 vec_free (am->msg_endian_handlers);
413 vec_free (am->msg_print_handlers);
416 void vl_msg_api_send_shmem (unix_shared_memory_queue_t *q, u8 *elem)
418 api_main_t *am = &api_main;
419 uword *trace = (uword *)elem;
421 if (am->tx_trace && am->tx_trace->enabled)
422 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
424 (void)unix_shared_memory_queue_add(q, elem, 0 /* nowait */);
427 void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t *q, u8 *elem)
429 api_main_t *am = &api_main;
430 uword *trace = (uword *)elem;
432 if (am->tx_trace && am->tx_trace->enabled)
433 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
435 (void)unix_shared_memory_queue_add_nolock (q, elem);
438 static void vl_api_memclnt_create_reply_t_handler (
439 vl_api_memclnt_create_reply_t *mp)
441 api_main_t *am = &api_main;
444 am->my_client_index = mp->index;
445 am->my_registration = (vl_api_registration_t *)(uword)
448 rv = ntohl(mp->response);
451 clib_warning ("WARNING: API mismatch detected");
454 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
455 __attribute__((weak));
457 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
461 for (i = 0; i < ARRAY_LEN(mp->api_versions); i++)
462 mp->api_versions[i] = 0;
465 int vl_client_connect (char *name, int ctx_quota, int input_queue_size)
468 vl_api_memclnt_create_t *mp;
469 vl_api_memclnt_create_reply_t *rp;
470 unix_shared_memory_queue_t *vl_input_queue;
471 vl_shmem_hdr_t *shmem_hdr;
474 api_main_t *am = &api_main;
476 if (am->my_registration) {
477 clib_warning ("client %s already connected...", name);
481 if (am->vlib_rp == 0) {
482 clib_warning ("am->vlib_rp NULL");
487 shmem_hdr = am->shmem_hdr;
489 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) {
490 clib_warning ("shmem_hdr / input queue NULL");
494 pthread_mutex_lock (&svm->mutex);
495 oldheap = svm_push_data_heap(svm);
497 unix_shared_memory_queue_init (input_queue_size, sizeof(uword),
499 pthread_mutex_unlock(&svm->mutex);
500 svm_pop_heap (oldheap);
502 am->my_client_index = ~0;
503 am->my_registration = 0;
504 am->vl_input_queue = vl_input_queue;
506 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_create_t));
507 memset(mp, 0, sizeof (*mp));
508 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_CREATE);
509 mp->ctx_quota = ctx_quota;
510 mp->input_queue = (uword)vl_input_queue;
511 strncpy ((char *) mp->name, name, sizeof(mp->name)-1);
513 vl_client_add_api_signatures(mp);
515 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
519 struct timespec ts, tsrem;
522 /* Wait up to 10 seconds */
523 for (i = 0; i < 1000; i++) {
524 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp,
529 ts.tv_nsec = 10000*1000; /* 10 ms */
530 while (nanosleep(&ts, &tsrem) < 0)
534 clib_warning ("memclnt_create_reply timeout");
538 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) {
539 clib_warning ("unexpected reply: id %d", ntohs(rp->_vl_msg_id));
542 rv = clib_net_to_host_u32(rp->response);
544 vl_msg_api_handler((void *)rp);
550 static void vl_api_memclnt_delete_reply_t_handler (
551 vl_api_memclnt_delete_reply_t *mp)
554 api_main_t *am = &api_main;
556 pthread_mutex_lock (&am->vlib_rp->mutex);
557 oldheap = svm_push_data_heap(am->vlib_rp);
558 unix_shared_memory_queue_free (am->vl_input_queue);
559 pthread_mutex_unlock (&am->vlib_rp->mutex);
560 svm_pop_heap (oldheap);
562 am->my_client_index = ~0;
563 am->my_registration = 0;
564 am->vl_input_queue = 0;
567 void vl_client_disconnect (void)
569 vl_api_memclnt_delete_t *mp;
570 vl_api_memclnt_delete_reply_t *rp;
571 unix_shared_memory_queue_t *vl_input_queue;
572 vl_shmem_hdr_t *shmem_hdr;
574 api_main_t *am = &api_main;
577 shmem_hdr = am->shmem_hdr;
578 ASSERT(shmem_hdr && shmem_hdr->vl_input_queue);
580 vl_input_queue = am->vl_input_queue;
582 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_delete_t));
583 memset(mp, 0, sizeof (*mp));
584 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_DELETE);
585 mp->index = am->my_client_index;
586 mp->handle = (uword) am->my_registration;
588 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
591 * Have to be careful here, in case the client is disconnecting
592 * because e.g. the vlib process died, or is unresponsive.
601 if (now >= (begin + 2)) {
602 clib_warning ("peer unresponsive, give up");
603 am->my_client_index = ~0;
604 am->my_registration = 0;
608 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp, 1) < 0)
611 /* drain the queue */
612 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) {
613 vl_msg_api_handler ((void *)rp);
616 vl_msg_api_handler((void *)rp);
621 static inline vl_api_registration_t
622 *vl_api_client_index_to_registration_internal (u32 handle)
624 vl_api_registration_t **regpp;
625 vl_api_registration_t *regp;
626 api_main_t *am = &api_main;
629 index = vl_msg_api_handle_get_index (handle);
630 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
631 != vl_msg_api_handle_get_epoch (handle)) {
632 vl_msg_api_increment_missing_client_counter();
636 regpp = am->vl_clients + index;
638 if (pool_is_free(am->vl_clients, regpp)) {
639 vl_msg_api_increment_missing_client_counter();
646 vl_api_registration_t *vl_api_client_index_to_registration (u32 index)
648 return (vl_api_client_index_to_registration_internal (index));
651 unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index)
653 vl_api_registration_t *regp;
655 regp = vl_api_client_index_to_registration_internal (index);
658 return (regp->vl_input_queue);
661 #define foreach_api_client_msg \
662 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
663 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
665 int vl_client_api_map (char *region_name)
669 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */)) < 0) {
674 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
675 vl_api_##n##_t_handler, \
676 0/* cleanup */, 0/* endian */, 0/* print */, \
677 sizeof(vl_api_##n##_t), 1);
678 foreach_api_client_msg;
683 void vl_client_api_unmap (void)