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>
43 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
48 unix_shared_memory_queue_t *q;
50 vl_shmem_hdr_t *shmem_hdr;
51 api_main_t *am = &api_main;
53 shmem_hdr = am->shmem_hdr;
57 clib_warning ("shared memory header NULL");
61 /* account for the msgbuf_t header */
62 nbytes += sizeof (msgbuf_t);
64 if (shmem_hdr->vl_rings == 0)
66 clib_warning ("vl_rings NULL");
71 if (shmem_hdr->client_rings == 0)
73 clib_warning ("client_rings NULL");
78 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
79 for (i = 0; i < vec_len (ap); i++)
82 if (nbytes > ap[i].size)
90 pthread_mutex_lock (&q->mutex);
92 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
94 * Is this item still in use?
98 u32 now = (u32) time (0);
100 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
101 rv->gc_mark_timestamp = now;
104 if (now - rv->gc_mark_timestamp > 10)
108 u16 *msg_idp, msg_id;
110 ("garbage collect pool %d ring %d index %d", pool, i,
112 msg_idp = (u16 *) (rv->data);
113 msg_id = clib_net_to_host_u16 (*msg_idp);
114 if (msg_id < vec_len (api_main.msg_names))
115 clib_warning ("msg id %d name %s", (u32) msg_id,
116 api_main.msg_names[msg_id]);
118 shmem_hdr->garbage_collects++;
124 /* yes, loser; try next larger pool */
127 pthread_mutex_unlock (&q->mutex);
132 /* OK, we have a winner */
135 * Remember the source queue, although we
136 * don't need to know the queue to free the item.
139 rv->gc_mark_timestamp = 0;
141 if (q->head == q->maxsize)
145 pthread_mutex_unlock (&q->mutex);
150 * Request too big, or head element of all size-compatible rings
151 * still in use. Fall back to shared-memory malloc.
155 pthread_mutex_lock (&am->vlib_rp->mutex);
156 oldheap = svm_push_data_heap (am->vlib_rp);
159 rv = clib_mem_alloc_or_null (nbytes);
160 if (PREDICT_FALSE (rv == 0))
162 svm_pop_heap (oldheap);
163 pthread_mutex_unlock (&am->vlib_rp->mutex);
168 rv = clib_mem_alloc (nbytes);
171 svm_pop_heap (oldheap);
172 pthread_mutex_unlock (&am->vlib_rp->mutex);
175 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
180 vl_msg_api_alloc (int nbytes)
183 api_main_t *am = &api_main;
184 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
187 * Clients use pool-0, vlib proc uses pool 1
189 pool = (am->our_pid == shmem_hdr->vl_pid);
190 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
194 vl_msg_api_alloc_or_null (int nbytes)
197 api_main_t *am = &api_main;
198 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
200 pool = (am->our_pid == shmem_hdr->vl_pid);
201 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
205 vl_msg_api_alloc_as_if_client (int nbytes)
207 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
211 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
213 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
217 vl_msg_api_free (void *a)
221 api_main_t *am = &api_main;
223 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
226 * Here's the beauty of the scheme. Only one proc/thread has
227 * control of a given message buffer. To free a buffer, we just clear the
228 * queue field, and leave. No locks, no hits, no errors...
233 rv->gc_mark_timestamp = 0;
237 pthread_mutex_lock (&am->vlib_rp->mutex);
238 oldheap = svm_push_data_heap (am->vlib_rp);
240 svm_pop_heap (oldheap);
241 pthread_mutex_unlock (&am->vlib_rp->mutex);
245 vl_msg_api_free_nolock (void *a)
249 api_main_t *am = &api_main;
251 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
253 * Here's the beauty of the scheme. Only one proc/thread has
254 * control of a given message buffer. To free a buffer, we just clear the
255 * queue field, and leave. No locks, no hits, no errors...
263 oldheap = svm_push_data_heap (am->vlib_rp);
265 svm_pop_heap (oldheap);
269 vl_set_memory_root_path (const char *name)
271 api_main_t *am = &api_main;
273 am->root_path = name;
277 vl_set_memory_uid (int uid)
279 api_main_t *am = &api_main;
285 vl_set_memory_gid (int gid)
287 api_main_t *am = &api_main;
293 vl_set_global_memory_baseva (u64 baseva)
295 api_main_t *am = &api_main;
297 am->global_baseva = baseva;
301 vl_set_global_memory_size (u64 size)
303 api_main_t *am = &api_main;
305 am->global_size = size;
309 vl_set_api_memory_size (u64 size)
311 api_main_t *am = &api_main;
317 vl_set_global_pvt_heap_size (u64 size)
319 api_main_t *am = &api_main;
321 am->global_pvt_heap_size = size;
325 vl_set_api_pvt_heap_size (u64 size)
327 api_main_t *am = &api_main;
329 am->api_pvt_heap_size = size;
333 vl_map_shmem (const char *region_name, int is_vlib)
335 svm_map_region_args_t _a, *a = &_a;
336 svm_region_t *vlib_rp, *root_rp;
338 vl_shmem_hdr_t *shmem_hdr = 0;
339 api_main_t *am = &api_main;
341 struct timespec ts, tsrem;
342 u32 vlib_input_queue_length;
344 memset (a, 0, sizeof (*a));
346 if (strstr (region_name, "-vpe-api"))
348 char root_path[strlen (region_name)];
349 strncpy (root_path, region_name, strlen (region_name) - 8);
350 a->root_path = root_path;
351 am->root_path = root_path;
355 svm_region_init_chroot (am->root_path);
357 if (a->root_path != NULL)
359 a->name = "/vpe-api";
362 a->name = region_name;
363 a->size = am->api_size ? am->api_size : (16 << 20);
364 a->flags = SVM_FLAGS_MHEAP;
365 a->uid = am->api_uid;
366 a->gid = am->api_gid;
367 a->pvt_heap_size = am->api_pvt_heap_size;
369 vlib_rp = svm_region_find_or_create (a);
374 pthread_mutex_lock (&vlib_rp->mutex);
375 /* Has someone else set up the shared-memory variable table? */
376 if (vlib_rp->user_ctx)
378 am->shmem_hdr = (void *) vlib_rp->user_ctx;
379 am->our_pid = getpid ();
382 unix_shared_memory_queue_t *q;
385 * application restart. Reset cached pids, API message
386 * rings, list of clients; otherwise, various things
387 * fail. (e.g. queue non-empty notification)
390 /* ghosts keep the region from disappearing properly */
391 svm_client_scan_this_region_nolock (vlib_rp);
392 am->shmem_hdr->application_restarts++;
393 q = am->shmem_hdr->vl_input_queue;
394 am->shmem_hdr->vl_pid = getpid ();
395 q->consumer_pid = am->shmem_hdr->vl_pid;
396 /* Drain the input queue, freeing msgs */
397 for (i = 0; i < 10; i++)
399 if (pthread_mutex_trylock (&q->mutex) == 0)
401 pthread_mutex_unlock (&q->mutex);
405 ts.tv_nsec = 10000 * 1000; /* 10 ms */
406 while (nanosleep (&ts, &tsrem) < 0)
409 /* Mutex buggered, "fix" it */
410 memset (&q->mutex, 0, sizeof (q->mutex));
411 clib_warning ("forcibly release main input queue mutex");
414 am->vlib_rp = vlib_rp;
415 while (unix_shared_memory_queue_sub (q,
418 != -2 /* queue underflow */ )
420 vl_msg_api_free_nolock ((void *) old_msg);
421 am->shmem_hdr->restart_reclaims++;
423 pthread_mutex_unlock (&vlib_rp->mutex);
424 root_rp = svm_get_root_rp ();
426 /* Clean up the root region client list */
427 pthread_mutex_lock (&root_rp->mutex);
428 svm_client_scan_this_region_nolock (root_rp);
429 pthread_mutex_unlock (&root_rp->mutex);
433 pthread_mutex_unlock (&vlib_rp->mutex);
435 am->vlib_rp = vlib_rp;
436 vec_add1 (am->mapped_shmem_regions, vlib_rp);
439 /* Clients simply have to wait... */
442 pthread_mutex_unlock (&vlib_rp->mutex);
444 /* Wait up to 100 seconds... */
445 for (i = 0; i < 10000; i++)
448 ts.tv_nsec = 10000 * 1000; /* 10 ms */
449 while (nanosleep (&ts, &tsrem) < 0)
451 if (vlib_rp->user_ctx)
454 /* Clean up and leave... */
455 svm_region_unmap (vlib_rp);
456 clib_warning ("region init fail");
460 am->shmem_hdr = (void *) vlib_rp->user_ctx;
461 am->our_pid = getpid ();
462 am->vlib_rp = vlib_rp;
463 vec_add1 (am->mapped_shmem_regions, vlib_rp);
467 /* Nope, it's our problem... */
469 oldheap = svm_push_data_heap (vlib_rp);
471 vec_validate (shmem_hdr, 0);
472 shmem_hdr->version = VL_SHM_VERSION;
474 /* vlib main input queue */
475 vlib_input_queue_length = 1024;
476 if (am->vlib_input_queue_length)
477 vlib_input_queue_length = am->vlib_input_queue_length;
479 shmem_hdr->vl_input_queue =
480 unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
481 getpid (), am->vlib_signal);
483 /* Set up the msg ring allocator */
487 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
492 vec_add1(shmem_hdr->vl_rings, _rp); \
495 foreach_vl_aring_size;
501 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
506 vec_add1(shmem_hdr->client_rings, _rp); \
509 foreach_clnt_aring_size;
512 am->shmem_hdr = shmem_hdr;
513 am->vlib_rp = vlib_rp;
514 am->our_pid = getpid ();
516 am->shmem_hdr->vl_pid = am->our_pid;
518 svm_pop_heap (oldheap);
521 * After absolutely everything that a client might see is set up,
522 * declare the shmem region valid
524 vlib_rp->user_ctx = shmem_hdr;
526 pthread_mutex_unlock (&vlib_rp->mutex);
527 vec_add1 (am->mapped_shmem_regions, vlib_rp);
532 vl_register_mapped_shmem_region (svm_region_t * rp)
534 api_main_t *am = &api_main;
536 vec_add1 (am->mapped_shmem_regions, rp);
540 vl_unmap_shmem (void)
544 api_main_t *am = &api_main;
546 if (!svm_get_root_rp ())
549 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
551 rp = am->mapped_shmem_regions[i];
552 svm_region_unmap (rp);
555 vec_free (am->mapped_shmem_regions);
559 /* $$$ more careful cleanup, valgrind run... */
560 vec_free (am->msg_handlers);
561 vec_free (am->msg_endian_handlers);
562 vec_free (am->msg_print_handlers);
566 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
568 api_main_t *am = &api_main;
569 uword *trace = (uword *) elem;
571 if (am->tx_trace && am->tx_trace->enabled)
572 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
574 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
578 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
580 api_main_t *am = &api_main;
581 uword *trace = (uword *) elem;
583 if (am->tx_trace && am->tx_trace->enabled)
584 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
586 (void) unix_shared_memory_queue_add_nolock (q, elem);
590 vl_api_get_msg_index (u8 * name_and_crc)
592 api_main_t *am = &api_main;
595 if (am->msg_index_by_name_and_crc)
597 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
604 static inline vl_api_registration_t *
605 vl_api_client_index_to_registration_internal (u32 handle)
607 vl_api_registration_t **regpp;
608 vl_api_registration_t *regp;
609 api_main_t *am = &api_main;
612 index = vl_msg_api_handle_get_index (handle);
613 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
614 != vl_msg_api_handle_get_epoch (handle))
616 vl_msg_api_increment_missing_client_counter ();
620 regpp = am->vl_clients + index;
622 if (pool_is_free (am->vl_clients, regpp))
624 vl_msg_api_increment_missing_client_counter ();
631 vl_api_registration_t *
632 vl_api_client_index_to_registration (u32 index)
634 return (vl_api_client_index_to_registration_internal (index));
637 unix_shared_memory_queue_t *
638 vl_api_client_index_to_input_queue (u32 index)
640 vl_api_registration_t *regp;
641 api_main_t *am = &api_main;
643 /* Special case: vlib trying to send itself a message */
644 if (index == (u32) ~ 0)
645 return (am->shmem_hdr->vl_input_queue);
647 regp = vl_api_client_index_to_registration_internal (index);
650 return (regp->vl_input_queue);
654 * fd.io coding-style-patch-verification: ON
657 * eval: (c-set-style "gnu")