blob: 1ccd563639f716dd87fb2bad64dc3edb9924714d [file] [log] [blame]
Dave Barach371e4e12016-07-08 09:38:52 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * memclnt_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
Dave Barach371e4e12016-07-08 09:38:52 -04005 *
Ed Warnickecb9cada2015-12-08 15:45:58 -07006 *
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>
Florin Corase86a8ed2018-01-05 03:20:25 -080028
Ed Warnickecb9cada2015-12-08 15:45:58 -070029#include <vppinfra/format.h>
30#include <vppinfra/byte_order.h>
31#include <vppinfra/error.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080032#include <svm/queue.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070033#include <vlib/vlib.h>
34#include <vlib/unix/unix.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080035#include <vlibmemory/memory_api.h>
Dave Barach371e4e12016-07-08 09:38:52 -040036#include <vlibmemory/vl_memory_msg_enum.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Dave Barach371e4e12016-07-08 09:38:52 -040038#define vl_typedefs
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#include <vlibmemory/vl_memory_api_h.h>
40#undef vl_typedefs
41
Dave Barach59b25652017-09-10 15:04:27 -040042#define DEBUG_MESSAGE_BUFFER_OVERRUN 0
43
Dave Barach371e4e12016-07-08 09:38:52 -040044static inline void *
Dave Barach77378332016-10-13 17:35:09 -040045vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
Dave Barach371e4e12016-07-08 09:38:52 -040047 int i;
48 msgbuf_t *rv;
49 ring_alloc_t *ap;
Florin Corase86a8ed2018-01-05 03:20:25 -080050 svm_queue_t *q;
Dave Barach371e4e12016-07-08 09:38:52 -040051 void *oldheap;
52 vl_shmem_hdr_t *shmem_hdr;
53 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Dave Barach371e4e12016-07-08 09:38:52 -040055 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Dave Barach59b25652017-09-10 15:04:27 -040057#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
58 nbytes += 4;
59#endif
60
Florin Coras7a2e1bd2017-11-30 16:07:39 -050061 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
62
Dave Barach371e4e12016-07-08 09:38:52 -040063 if (shmem_hdr == 0)
64 {
65 clib_warning ("shared memory header NULL");
66 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 }
68
Dave Barach371e4e12016-07-08 09:38:52 -040069 /* account for the msgbuf_t header */
70 nbytes += sizeof (msgbuf_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Dave Barach371e4e12016-07-08 09:38:52 -040072 if (shmem_hdr->vl_rings == 0)
73 {
74 clib_warning ("vl_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040075 ASSERT (0);
76 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 }
78
Dave Barach371e4e12016-07-08 09:38:52 -040079 if (shmem_hdr->client_rings == 0)
80 {
81 clib_warning ("client_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040082 ASSERT (0);
83 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 }
85
Dave Barach371e4e12016-07-08 09:38:52 -040086 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
87 for (i = 0; i < vec_len (ap); i++)
88 {
89 /* Too big? */
90 if (nbytes > ap[i].size)
91 {
92 continue;
93 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Dave Barach371e4e12016-07-08 09:38:52 -040095 q = ap[i].rp;
96 if (pool == 0)
97 {
98 pthread_mutex_lock (&q->mutex);
99 }
100 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
101 /*
102 * Is this item still in use?
103 */
104 if (rv->q)
105 {
Dave Barach842b9c52017-01-09 15:54:00 -0500106 u32 now = (u32) time (0);
107
108 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
109 rv->gc_mark_timestamp = now;
110 else
111 {
112 if (now - rv->gc_mark_timestamp > 10)
113 {
114 if (CLIB_DEBUG > 0)
Dave Barach10d8cc62017-05-30 09:30:07 -0400115 {
116 u16 *msg_idp, msg_id;
117 clib_warning
118 ("garbage collect pool %d ring %d index %d", pool, i,
119 q->head);
120 msg_idp = (u16 *) (rv->data);
121 msg_id = clib_net_to_host_u16 (*msg_idp);
122 if (msg_id < vec_len (api_main.msg_names))
123 clib_warning ("msg id %d name %s", (u32) msg_id,
124 api_main.msg_names[msg_id]);
125 }
Dave Barach842b9c52017-01-09 15:54:00 -0500126 shmem_hdr->garbage_collects++;
127 goto collected;
128 }
129 }
130
131
Dave Barach371e4e12016-07-08 09:38:52 -0400132 /* yes, loser; try next larger pool */
133 ap[i].misses++;
134 if (pool == 0)
135 pthread_mutex_unlock (&q->mutex);
136 continue;
137 }
Dave Barach842b9c52017-01-09 15:54:00 -0500138 collected:
139
Dave Barach371e4e12016-07-08 09:38:52 -0400140 /* OK, we have a winner */
141 ap[i].hits++;
142 /*
143 * Remember the source queue, although we
144 * don't need to know the queue to free the item.
145 */
146 rv->q = q;
Dave Barach842b9c52017-01-09 15:54:00 -0500147 rv->gc_mark_timestamp = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400148 q->head++;
149 if (q->head == q->maxsize)
150 q->head = 0;
151
152 if (pool == 0)
153 pthread_mutex_unlock (&q->mutex);
154 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 }
156
Dave Barach371e4e12016-07-08 09:38:52 -0400157 /*
158 * Request too big, or head element of all size-compatible rings
159 * still in use. Fall back to shared-memory malloc.
160 */
161 am->ring_misses++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Dave Barach371e4e12016-07-08 09:38:52 -0400163 pthread_mutex_lock (&am->vlib_rp->mutex);
164 oldheap = svm_push_data_heap (am->vlib_rp);
Dave Barach77378332016-10-13 17:35:09 -0400165 if (may_return_null)
166 {
167 rv = clib_mem_alloc_or_null (nbytes);
168 if (PREDICT_FALSE (rv == 0))
169 {
170 svm_pop_heap (oldheap);
171 pthread_mutex_unlock (&am->vlib_rp->mutex);
172 return 0;
173 }
174 }
175 else
176 rv = clib_mem_alloc (nbytes);
177
Dave Barach371e4e12016-07-08 09:38:52 -0400178 rv->q = 0;
179 svm_pop_heap (oldheap);
180 pthread_mutex_unlock (&am->vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barach371e4e12016-07-08 09:38:52 -0400182out:
Dave Barach59b25652017-09-10 15:04:27 -0400183#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
184 {
185 nbytes -= 4;
186 u32 *overrun;
187 overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
188 *overrun = 0x1badbabe;
189 }
190#endif
Dave Barach371e4e12016-07-08 09:38:52 -0400191 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
Dave Barach59b25652017-09-10 15:04:27 -0400192
Dave Barach371e4e12016-07-08 09:38:52 -0400193 return (rv->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194}
195
Dave Barach371e4e12016-07-08 09:38:52 -0400196void *
197vl_msg_api_alloc (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
Dave Barach371e4e12016-07-08 09:38:52 -0400199 int pool;
200 api_main_t *am = &api_main;
201 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Dave Barach371e4e12016-07-08 09:38:52 -0400203 /*
204 * Clients use pool-0, vlib proc uses pool 1
205 */
206 pool = (am->our_pid == shmem_hdr->vl_pid);
Dave Barach77378332016-10-13 17:35:09 -0400207 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
208}
209
210void *
211vl_msg_api_alloc_or_null (int nbytes)
212{
213 int pool;
214 api_main_t *am = &api_main;
215 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
216
217 pool = (am->our_pid == shmem_hdr->vl_pid);
218 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219}
220
Dave Barach371e4e12016-07-08 09:38:52 -0400221void *
222vl_msg_api_alloc_as_if_client (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
Dave Barach77378332016-10-13 17:35:09 -0400224 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
225}
226
227void *
228vl_msg_api_alloc_as_if_client_or_null (int nbytes)
229{
230 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231}
232
Florin Corasb384b542018-01-15 01:08:33 -0800233void *
234vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
235{
236 api_main_t *am = &api_main;
237 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
238 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
239 void *msg;
240
241 vlib_rp = am->vlib_rp = reg->vlib_rp;
242 am->shmem_hdr = (void *) vlib_rp->user_ctx;
243
244 msg = vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
245
246 am->shmem_hdr = save_shmem_hdr;
247 am->vlib_rp = save_vlib_rp;
248
249 return msg;
250}
251
Dave Barach371e4e12016-07-08 09:38:52 -0400252void
253vl_msg_api_free (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
Dave Barach371e4e12016-07-08 09:38:52 -0400255 msgbuf_t *rv;
256 void *oldheap;
257 api_main_t *am = &api_main;
Ole Troan6855f6c2016-04-09 03:16:30 +0200258
Dave Barach371e4e12016-07-08 09:38:52 -0400259 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
260
261 /*
262 * Here's the beauty of the scheme. Only one proc/thread has
263 * control of a given message buffer. To free a buffer, we just clear the
264 * queue field, and leave. No locks, no hits, no errors...
265 */
266 if (rv->q)
267 {
268 rv->q = 0;
Dave Barach842b9c52017-01-09 15:54:00 -0500269 rv->gc_mark_timestamp = 0;
Dave Barach59b25652017-09-10 15:04:27 -0400270#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
271 {
272 u32 *overrun;
273 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
274 ASSERT (*overrun == 0x1badbabe);
275 }
276#endif
Dave Barach371e4e12016-07-08 09:38:52 -0400277 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 }
279
Dave Barach371e4e12016-07-08 09:38:52 -0400280 pthread_mutex_lock (&am->vlib_rp->mutex);
281 oldheap = svm_push_data_heap (am->vlib_rp);
Dave Barach59b25652017-09-10 15:04:27 -0400282
283#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
284 {
285 u32 *overrun;
286 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
287 ASSERT (*overrun == 0x1badbabe);
288 }
289#endif
290
Dave Barach371e4e12016-07-08 09:38:52 -0400291 clib_mem_free (rv);
292 svm_pop_heap (oldheap);
293 pthread_mutex_unlock (&am->vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294}
295
Dave Barach371e4e12016-07-08 09:38:52 -0400296static void
297vl_msg_api_free_nolock (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298{
Dave Barach371e4e12016-07-08 09:38:52 -0400299 msgbuf_t *rv;
300 void *oldheap;
301 api_main_t *am = &api_main;
302
303 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
304 /*
305 * Here's the beauty of the scheme. Only one proc/thread has
306 * control of a given message buffer. To free a buffer, we just clear the
307 * queue field, and leave. No locks, no hits, no errors...
308 */
309 if (rv->q)
310 {
311 rv->q = 0;
312 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 }
314
Dave Barach371e4e12016-07-08 09:38:52 -0400315 oldheap = svm_push_data_heap (am->vlib_rp);
316 clib_mem_free (rv);
317 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318}
319
Dave Barach371e4e12016-07-08 09:38:52 -0400320void
Neale Rannse72be392017-04-26 13:59:20 -0700321vl_set_memory_root_path (const char *name)
Dave Barach309bef22016-01-22 16:09:52 -0500322{
Dave Barach371e4e12016-07-08 09:38:52 -0400323 api_main_t *am = &api_main;
Dave Barach309bef22016-01-22 16:09:52 -0500324
Dave Barach371e4e12016-07-08 09:38:52 -0400325 am->root_path = name;
Dave Barach309bef22016-01-22 16:09:52 -0500326}
327
Dave Barach371e4e12016-07-08 09:38:52 -0400328void
329vl_set_memory_uid (int uid)
Dave Barach16c75df2016-05-31 14:05:46 -0400330{
Dave Barach371e4e12016-07-08 09:38:52 -0400331 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400332
Dave Barach371e4e12016-07-08 09:38:52 -0400333 am->api_uid = uid;
Dave Barach16c75df2016-05-31 14:05:46 -0400334}
335
Dave Barach371e4e12016-07-08 09:38:52 -0400336void
337vl_set_memory_gid (int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400338{
Dave Barach371e4e12016-07-08 09:38:52 -0400339 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400340
Dave Barach371e4e12016-07-08 09:38:52 -0400341 am->api_gid = gid;
Dave Barach16c75df2016-05-31 14:05:46 -0400342}
343
Dave Barachb3d93da2016-08-03 14:34:38 -0400344void
345vl_set_global_memory_baseva (u64 baseva)
346{
347 api_main_t *am = &api_main;
348
349 am->global_baseva = baseva;
350}
351
352void
353vl_set_global_memory_size (u64 size)
354{
355 api_main_t *am = &api_main;
356
357 am->global_size = size;
358}
359
360void
361vl_set_api_memory_size (u64 size)
362{
363 api_main_t *am = &api_main;
364
365 am->api_size = size;
366}
367
368void
369vl_set_global_pvt_heap_size (u64 size)
370{
371 api_main_t *am = &api_main;
372
373 am->global_pvt_heap_size = size;
374}
375
376void
377vl_set_api_pvt_heap_size (u64 size)
378{
379 api_main_t *am = &api_main;
380
381 am->api_pvt_heap_size = size;
382}
383
Florin Coras90a63982017-12-19 04:50:01 -0800384static void
385vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
Dave Barach59b25652017-09-10 15:04:27 -0400386{
387 api_main_t *am = &api_main;
Dave Barach59b25652017-09-10 15:04:27 -0400388 u32 vlib_input_queue_length;
Dave Barach59b25652017-09-10 15:04:27 -0400389
390 /* vlib main input queue */
391 vlib_input_queue_length = 1024;
392 if (am->vlib_input_queue_length)
393 vlib_input_queue_length = am->vlib_input_queue_length;
394
395 shmem_hdr->vl_input_queue =
Florin Corasc470e222018-08-01 07:53:18 -0700396 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
397 getpid ());
Dave Barach59b25652017-09-10 15:04:27 -0400398
Dave Barach59b25652017-09-10 15:04:27 -0400399#define _(sz,n) \
400 do { \
401 ring_alloc_t _rp; \
Florin Corasc470e222018-08-01 07:53:18 -0700402 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
Dave Barach59b25652017-09-10 15:04:27 -0400403 _rp.size = (sz); \
404 _rp.nitems = n; \
405 _rp.hits = 0; \
406 _rp.misses = 0; \
407 vec_add1(shmem_hdr->vl_rings, _rp); \
408 } while (0);
409
410 foreach_vl_aring_size;
411#undef _
412
413#define _(sz,n) \
414 do { \
415 ring_alloc_t _rp; \
Florin Corasc470e222018-08-01 07:53:18 -0700416 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
Dave Barach59b25652017-09-10 15:04:27 -0400417 _rp.size = (sz); \
418 _rp.nitems = n; \
419 _rp.hits = 0; \
420 _rp.misses = 0; \
421 vec_add1(shmem_hdr->client_rings, _rp); \
422 } while (0);
423
424 foreach_clnt_aring_size;
425#undef _
Florin Coras90a63982017-12-19 04:50:01 -0800426}
427
428void
429vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
430{
Florin Coras90a63982017-12-19 04:50:01 -0800431 vl_api_shm_elem_config_t *c;
432 ring_alloc_t *rp;
433 u32 size;
434
435 if (!config)
436 {
437 vl_api_default_mem_config (hdr);
438 return;
439 }
440
441 vec_foreach (c, config)
442 {
443 switch (c->type)
444 {
445 case VL_API_QUEUE:
Florin Corasc470e222018-08-01 07:53:18 -0700446 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
447 getpid ());
Florin Coras90a63982017-12-19 04:50:01 -0800448 continue;
449 case VL_API_VLIB_RING:
450 vec_add2 (hdr->vl_rings, rp, 1);
451 break;
452 case VL_API_CLIENT_RING:
453 vec_add2 (hdr->client_rings, rp, 1);
454 break;
455 default:
456 clib_warning ("unknown config type: %d", c->type);
457 continue;
458 }
459
460 size = sizeof (ring_alloc_t) + c->size;
Florin Corasc470e222018-08-01 07:53:18 -0700461 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
Florin Coras90a63982017-12-19 04:50:01 -0800462 rp->size = size;
463 rp->nitems = c->count;
464 rp->hits = 0;
465 rp->misses = 0;
466 }
467}
468
469void
470vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
471 int is_vlib, int is_private_region)
472{
473 api_main_t *am = &api_main;
474 vl_shmem_hdr_t *shmem_hdr = 0;
475 void *oldheap;
476 ASSERT (vlib_rp);
477
478 /* $$$$ need private region config parameters */
479
480 oldheap = svm_push_data_heap (vlib_rp);
481
482 vec_validate (shmem_hdr, 0);
483 shmem_hdr->version = VL_SHM_VERSION;
Florin Corasb384b542018-01-15 01:08:33 -0800484 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
Florin Coras90a63982017-12-19 04:50:01 -0800485
486 /* Set up the queue and msg ring allocator */
487 vl_api_mem_config (shmem_hdr, config);
Dave Barach59b25652017-09-10 15:04:27 -0400488
489 if (is_private_region == 0)
490 {
491 am->shmem_hdr = shmem_hdr;
492 am->vlib_rp = vlib_rp;
493 am->our_pid = getpid ();
494 if (is_vlib)
495 am->shmem_hdr->vl_pid = am->our_pid;
496 }
497 else
498 shmem_hdr->vl_pid = am->our_pid;
499
500 svm_pop_heap (oldheap);
501
502 /*
503 * After absolutely everything that a client might see is set up,
504 * declare the shmem region valid
505 */
506 vlib_rp->user_ctx = shmem_hdr;
507
508 pthread_mutex_unlock (&vlib_rp->mutex);
509}
510
Dave Barach371e4e12016-07-08 09:38:52 -0400511int
Neale Rannse72be392017-04-26 13:59:20 -0700512vl_map_shmem (const char *region_name, int is_vlib)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513{
Dave Barach371e4e12016-07-08 09:38:52 -0400514 svm_map_region_args_t _a, *a = &_a;
515 svm_region_t *vlib_rp, *root_rp;
Dave Barach371e4e12016-07-08 09:38:52 -0400516 api_main_t *am = &api_main;
Ole Troan3cdc25f2017-08-17 11:07:33 +0200517 int i, rv;
Dave Barach371e4e12016-07-08 09:38:52 -0400518 struct timespec ts, tsrem;
Dave Wallacecfc997e2017-08-22 18:32:34 -0400519 char *vpe_api_region_suffix = "-vpe-api";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Dave Barachb7b92992018-10-17 10:38:51 -0400521 clib_memset (a, 0, sizeof (*a));
Jan Srnicek5beec812017-03-24 10:18:11 +0100522
Dave Wallacecfc997e2017-08-22 18:32:34 -0400523 if (strstr (region_name, vpe_api_region_suffix))
Jan Srnicek5beec812017-03-24 10:18:11 +0100524 {
Dave Wallacecfc997e2017-08-22 18:32:34 -0400525 u8 *root_path = format (0, "%s", region_name);
526 _vec_len (root_path) = (vec_len (root_path) -
527 strlen (vpe_api_region_suffix));
528 vec_terminate_c_string (root_path);
529 a->root_path = (const char *) root_path;
530 am->root_path = (const char *) root_path;
Jan Srnicek5beec812017-03-24 10:18:11 +0100531 }
532
Dave Barach371e4e12016-07-08 09:38:52 -0400533 if (is_vlib == 0)
Ole Troan3cdc25f2017-08-17 11:07:33 +0200534 {
Dave Barach22af4472018-12-27 13:57:41 -0500535 int tfd;
536 u8 *api_name;
537 /*
538 * Clients wait for vpp to set up the root / API regioins
539 */
540 if (am->root_path)
541 api_name = format (0, "/dev/shm/%s-%s%c", am->root_path,
542 region_name + 1, 0);
543 else
544 api_name = format (0, "/dev/shm%s%c", region_name, 0);
545
546 /* Wait up to 100 seconds... */
547 for (i = 0; i < 10000; i++)
548 {
549 ts.tv_sec = 0;
550 ts.tv_nsec = 10000 * 1000; /* 10 ms */
551 while (nanosleep (&ts, &tsrem) < 0)
552 ts = tsrem;
553 tfd = open ((char *) api_name, O_RDWR);
554 if (tfd > 0)
555 break;
556 }
557 vec_free (api_name);
558 if (tfd < 0)
559 {
560 clib_warning ("region init fail");
561 return -2;
562 }
563 close (tfd);
Ole Troan3cdc25f2017-08-17 11:07:33 +0200564 rv = svm_region_init_chroot (am->root_path);
565 if (rv)
566 return rv;
567 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Jan Srnicek5beec812017-03-24 10:18:11 +0100569 if (a->root_path != NULL)
570 {
571 a->name = "/vpe-api";
572 }
573 else
574 a->name = region_name;
Dave Barachc3799992016-08-15 11:12:27 -0400575 a->size = am->api_size ? am->api_size : (16 << 20);
Dave Barach371e4e12016-07-08 09:38:52 -0400576 a->flags = SVM_FLAGS_MHEAP;
577 a->uid = am->api_uid;
578 a->gid = am->api_gid;
Dave Barachb3d93da2016-08-03 14:34:38 -0400579 a->pvt_heap_size = am->api_pvt_heap_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barach371e4e12016-07-08 09:38:52 -0400581 vlib_rp = svm_region_find_or_create (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach371e4e12016-07-08 09:38:52 -0400583 if (vlib_rp == 0)
584 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach371e4e12016-07-08 09:38:52 -0400586 pthread_mutex_lock (&vlib_rp->mutex);
587 /* Has someone else set up the shared-memory variable table? */
588 if (vlib_rp->user_ctx)
589 {
590 am->shmem_hdr = (void *) vlib_rp->user_ctx;
591 am->our_pid = getpid ();
592 if (is_vlib)
593 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800594 svm_queue_t *q;
Dave Barach371e4e12016-07-08 09:38:52 -0400595 uword old_msg;
596 /*
597 * application restart. Reset cached pids, API message
598 * rings, list of clients; otherwise, various things
599 * fail. (e.g. queue non-empty notification)
600 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Dave Barach371e4e12016-07-08 09:38:52 -0400602 /* ghosts keep the region from disappearing properly */
603 svm_client_scan_this_region_nolock (vlib_rp);
604 am->shmem_hdr->application_restarts++;
605 q = am->shmem_hdr->vl_input_queue;
606 am->shmem_hdr->vl_pid = getpid ();
607 q->consumer_pid = am->shmem_hdr->vl_pid;
608 /* Drain the input queue, freeing msgs */
609 for (i = 0; i < 10; i++)
610 {
611 if (pthread_mutex_trylock (&q->mutex) == 0)
612 {
613 pthread_mutex_unlock (&q->mutex);
614 goto mutex_ok;
615 }
616 ts.tv_sec = 0;
617 ts.tv_nsec = 10000 * 1000; /* 10 ms */
618 while (nanosleep (&ts, &tsrem) < 0)
619 ts = tsrem;
620 }
621 /* Mutex buggered, "fix" it */
Dave Barachb7b92992018-10-17 10:38:51 -0400622 clib_memset (&q->mutex, 0, sizeof (q->mutex));
Dave Barach371e4e12016-07-08 09:38:52 -0400623 clib_warning ("forcibly release main input queue mutex");
624
625 mutex_ok:
626 am->vlib_rp = vlib_rp;
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100627 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400628 != -2 /* queue underflow */ )
629 {
630 vl_msg_api_free_nolock ((void *) old_msg);
631 am->shmem_hdr->restart_reclaims++;
632 }
633 pthread_mutex_unlock (&vlib_rp->mutex);
634 root_rp = svm_get_root_rp ();
635 ASSERT (root_rp);
636 /* Clean up the root region client list */
637 pthread_mutex_lock (&root_rp->mutex);
638 svm_client_scan_this_region_nolock (root_rp);
639 pthread_mutex_unlock (&root_rp->mutex);
640 }
641 else
642 {
643 pthread_mutex_unlock (&vlib_rp->mutex);
644 }
645 am->vlib_rp = vlib_rp;
646 vec_add1 (am->mapped_shmem_regions, vlib_rp);
647 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 }
Dave Barach371e4e12016-07-08 09:38:52 -0400649 /* Clients simply have to wait... */
650 if (!is_vlib)
651 {
652 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Dave Barach371e4e12016-07-08 09:38:52 -0400654 /* Wait up to 100 seconds... */
655 for (i = 0; i < 10000; i++)
656 {
657 ts.tv_sec = 0;
658 ts.tv_nsec = 10000 * 1000; /* 10 ms */
659 while (nanosleep (&ts, &tsrem) < 0)
660 ts = tsrem;
661 if (vlib_rp->user_ctx)
662 goto ready;
663 }
664 /* Clean up and leave... */
665 svm_region_unmap (vlib_rp);
666 clib_warning ("region init fail");
667 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
669 ready:
Dave Barach371e4e12016-07-08 09:38:52 -0400670 am->shmem_hdr = (void *) vlib_rp->user_ctx;
671 am->our_pid = getpid ();
672 am->vlib_rp = vlib_rp;
673 vec_add1 (am->mapped_shmem_regions, vlib_rp);
674 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 }
676
Dave Barach371e4e12016-07-08 09:38:52 -0400677 /* Nope, it's our problem... */
Florin Coras90a63982017-12-19 04:50:01 -0800678 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
679 0 /* is_private_region */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Dave Barach371e4e12016-07-08 09:38:52 -0400681 vec_add1 (am->mapped_shmem_regions, vlib_rp);
682 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683}
684
Dave Barach371e4e12016-07-08 09:38:52 -0400685void
686vl_register_mapped_shmem_region (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687{
Dave Barach371e4e12016-07-08 09:38:52 -0400688 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Dave Barach371e4e12016-07-08 09:38:52 -0400690 vec_add1 (am->mapped_shmem_regions, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691}
692
Florin Corasd6c30d92018-01-29 05:11:24 -0800693static void
694vl_unmap_shmem_internal (u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695{
Dave Barach371e4e12016-07-08 09:38:52 -0400696 svm_region_t *rp;
697 int i;
698 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
Dave Barach371e4e12016-07-08 09:38:52 -0400700 if (!svm_get_root_rp ())
701 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
Dave Barach371e4e12016-07-08 09:38:52 -0400703 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
704 {
705 rp = am->mapped_shmem_regions[i];
Florin Corasd6c30d92018-01-29 05:11:24 -0800706 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707 }
708
Dave Barach371e4e12016-07-08 09:38:52 -0400709 vec_free (am->mapped_shmem_regions);
710 am->shmem_hdr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Florin Corasd6c30d92018-01-29 05:11:24 -0800712 is_client ? svm_region_exit_client () : svm_region_exit ();
713
Dave Barach371e4e12016-07-08 09:38:52 -0400714 /* $$$ more careful cleanup, valgrind run... */
715 vec_free (am->msg_handlers);
716 vec_free (am->msg_endian_handlers);
717 vec_free (am->msg_print_handlers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718}
719
Dave Barach371e4e12016-07-08 09:38:52 -0400720void
Florin Corasd6c30d92018-01-29 05:11:24 -0800721vl_unmap_shmem (void)
722{
723 vl_unmap_shmem_internal (0);
724}
725
726void
727vl_unmap_shmem_client (void)
728{
729 vl_unmap_shmem_internal (1);
730}
731
732void
Florin Corase86a8ed2018-01-05 03:20:25 -0800733vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734{
Dave Barach371e4e12016-07-08 09:38:52 -0400735 api_main_t *am = &api_main;
736 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Dave Barach371e4e12016-07-08 09:38:52 -0400738 if (am->tx_trace && am->tx_trace->enabled)
739 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
Dave Barach0c4fec02018-11-18 12:03:48 -0500741 /*
742 * Announce a probable binary API client bug:
743 * some client's input queue is stuffed.
744 * The situation may be recoverable, or not.
745 */
746 if (PREDICT_FALSE
747 (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize)))
748 clib_warning ("WARNING: client input queue at %llx is stuffed...", q);
Florin Corase86a8ed2018-01-05 03:20:25 -0800749 (void) svm_queue_add (q, elem, 0 /* nowait */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750}
751
Florin Corasaf0ff5a2018-01-10 08:17:03 -0800752int
753vl_mem_api_can_send (svm_queue_t * q)
754{
755 return (q->cursize < q->maxsize);
756}
757
Dave Barach371e4e12016-07-08 09:38:52 -0400758void
Florin Corase86a8ed2018-01-05 03:20:25 -0800759vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
Dave Barach371e4e12016-07-08 09:38:52 -0400761 api_main_t *am = &api_main;
762 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763
Dave Barach371e4e12016-07-08 09:38:52 -0400764 if (am->tx_trace && am->tx_trace->enabled)
765 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Florin Corase86a8ed2018-01-05 03:20:25 -0800767 (void) svm_queue_add_nolock (q, elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768}
769
Dave Barach371e4e12016-07-08 09:38:52 -0400770/*
771 * fd.io coding-style-patch-verification: ON
772 *
773 * Local Variables:
774 * eval: (c-set-style "gnu")
775 * End:
776 */