2 *------------------------------------------------------------------
3 * svmdb.c -- simple shared memory database
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
22 #include <sys/types.h>
25 #include <netinet/in.h>
32 #include <vppinfra/clib.h>
33 #include <vppinfra/vec.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/bitmap.h>
36 #include <vppinfra/fifo.h>
37 #include <vppinfra/time.h>
38 #include <vppinfra/mheap.h>
39 #include <vppinfra/heap.h>
40 #include <vppinfra/pool.h>
41 #include <vppinfra/format.h>
45 static void local_set_variable_nolock (svmdb_client_t * client,
46 svmdb_namespace_t namespace,
47 u8 * var, u8 * val, u32 elsize);
50 region_lock (svm_region_t * rp, int tag)
52 pthread_mutex_lock (&rp->mutex);
54 rp->mutex_owner_pid = getpid ();
55 rp->mutex_owner_tag = tag;
60 region_unlock (svm_region_t * rp)
63 rp->mutex_owner_pid = 0;
64 rp->mutex_owner_tag = 0;
66 pthread_mutex_unlock (&rp->mutex);
70 svmdb_map (svmdb_map_args_t * dba)
72 svmdb_client_t *client = 0;
73 svm_map_region_args_t *a = 0;
76 svmdb_shm_hdr_t *hp = 0;
78 vec_validate (client, 0);
81 svm_region_init_chroot_uid_gid (dba->root_path, dba->uid, dba->gid);
83 a->root_path = dba->root_path;
85 a->size = dba->size ? dba->size : SVMDB_DEFAULT_SIZE;
86 a->flags = SVM_FLAGS_MHEAP;
90 db_rp = client->db_rp = svm_region_find_or_create (a);
96 region_lock (client->db_rp, 10);
97 /* Has someone else set up the shared-memory variable table? */
100 client->shm = (void *) db_rp->user_ctx;
101 client->pid = getpid ();
102 region_unlock (client->db_rp);
103 ASSERT (client->shm->version == SVMDB_SHM_VERSION);
106 /* Nope, it's our problem... */
108 /* Add a bogus client (pid=0) so the svm won't be deallocated */
109 oldheap = svm_push_pvt_heap (db_rp);
110 vec_add1 (client->db_rp->client_pids, 0);
111 svm_pop_heap (oldheap);
113 oldheap = svm_push_data_heap (db_rp);
115 vec_validate (hp, 0);
116 hp->version = SVMDB_SHM_VERSION;
117 hp->namespaces[SVMDB_NAMESPACE_STRING]
118 = hash_create_string (0, sizeof (uword));
119 hp->namespaces[SVMDB_NAMESPACE_VEC]
120 = hash_create_string (0, sizeof (uword));
122 db_rp->user_ctx = hp;
125 svm_pop_heap (oldheap);
126 region_unlock (client->db_rp);
127 client->pid = getpid ();
133 svmdb_unmap (svmdb_client_t * client)
137 if (!svm_get_root_rp ())
140 svm_region_unmap ((void *) client->db_rp);
146 notify_value (svmdb_value_t * v, svmdb_action_t a)
152 u32 *dead_registrations = 0;
156 for (i = 0; i < vec_len (v->notifications); i++)
158 np = vec_elt_at_index (v->notifications, i);
161 value = (np->action << 28) | (np->opaque);
162 sv.sival_ptr = (void *) (uword) value;
166 if (sigqueue (np->pid, np->signum, sv) == 0)
170 while (rv == EAGAIN);
173 vec_add1 (dead_registrations, i);
177 for (i = 0; i < vec_len (dead_registrations); i++)
179 np = vec_elt_at_index (v->notifications, dead_registrations[i]);
180 clib_warning ("dead reg pid %d sig %d action %d opaque %x",
181 np->pid, np->signum, np->action, np->opaque);
182 vec_delete (v->notifications, 1, dead_registrations[i]);
184 vec_free (dead_registrations);
188 svmdb_local_add_del_notification (svmdb_client_t * client,
189 svmdb_notification_args_t * a)
194 svmdb_shm_hdr_t *shm;
196 svmdb_value_t *value;
203 region_lock (client->db_rp, 18);
205 oldheap = svm_push_data_heap (client->db_rp);
207 h = shm->namespaces[a->nspace];
209 hp = hash_get_pair_mem (h, a->var);
212 local_set_variable_nolock (client, a->nspace, (u8 *) a->var,
213 dummy_value, a->elsize);
214 /* might have moved */
215 h = shm->namespaces[a->nspace];
216 hp = hash_get_pair_mem (h, a->var);
220 value = pool_elt_at_index (shm->values, hp->value[0]);
222 for (i = 0; i < vec_len (value->notifications); i++)
224 np = vec_elt_at_index (value->notifications, i);
225 if ((np->pid == client->pid)
226 && (np->signum == a->signum)
227 && (np->action == a->action) && (np->opaque == a->opaque))
229 if (a->add_del == 0 /* delete */ )
231 vec_delete (value->notifications, 1, i);
237 ("%s: ignore dup reg pid %d signum %d action %d opaque %x",
238 a->var, client->pid, a->signum, a->action, a->opaque);
250 vec_add2 (value->notifications, np, 1);
251 np->pid = client->pid;
252 np->signum = a->signum;
253 np->action = a->action;
254 np->opaque = a->opaque;
257 svm_pop_heap (oldheap);
258 region_unlock (client->db_rp);
264 local_unset_variable_nolock (svmdb_client_t * client,
265 svmdb_namespace_t namespace, char *var)
268 svmdb_value_t *oldvalue;
271 h = client->shm->namespaces[namespace];
272 hp = hash_get_pair_mem (h, var);
275 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
276 if (vec_len (oldvalue->notifications))
277 notify_value (oldvalue, SVMDB_ACTION_UNSET);
278 /* zero length value means unset */
279 _vec_len (oldvalue->value) = 0;
281 client->shm->namespaces[namespace] = h;
285 svmdb_local_unset_string_variable (svmdb_client_t * client, char *var)
289 region_lock (client->db_rp, 11);
290 oldheap = svm_push_data_heap (client->db_rp);
291 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
292 svm_pop_heap (oldheap);
293 region_unlock (client->db_rp);
297 local_set_variable_nolock (svmdb_client_t * client,
298 svmdb_namespace_t namespace,
299 u8 * var, u8 * val, u32 elsize)
304 svmdb_shm_hdr_t *shm;
307 h = shm->namespaces[namespace];
308 hp = hash_get_pair_mem (h, var);
311 svmdb_value_t *oldvalue;
312 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
313 vec_alloc (oldvalue->value, vec_len (val) * elsize);
314 clib_memcpy (oldvalue->value, val, vec_len (val) * elsize);
315 _vec_len (oldvalue->value) = vec_len (val);
316 notify_value (oldvalue, SVMDB_ACTION_SET);
320 svmdb_value_t *newvalue;
321 pool_get (shm->values, newvalue);
322 memset (newvalue, 0, sizeof (*newvalue));
323 newvalue->elsize = elsize;
324 vec_alloc (newvalue->value, vec_len (val) * elsize);
325 clib_memcpy (newvalue->value, val, vec_len (val) * elsize);
326 _vec_len (newvalue->value) = vec_len (val);
327 name = format (0, "%s%c", var, 0);
328 hash_set_mem (h, name, newvalue - shm->values);
330 shm->namespaces[namespace] = h;
334 svmdb_local_set_string_variable (svmdb_client_t * client,
335 char *var, char *val)
339 region_lock (client->db_rp, 12);
340 oldheap = svm_push_data_heap (client->db_rp);
342 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
344 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
345 (u8 *) var, (u8 *) val, 1 /* elsize */ );
346 svm_pop_heap (oldheap);
347 region_unlock (client->db_rp);
351 local_get_variable_nolock (svmdb_client_t * client,
352 svmdb_namespace_t namespace, u8 * var)
356 svmdb_shm_hdr_t *shm;
357 svmdb_value_t *oldvalue;
360 h = shm->namespaces[namespace];
361 p = hash_get_mem (h, var);
364 oldvalue = pool_elt_at_index (shm->values, p[0]);
365 notify_value (oldvalue, SVMDB_ACTION_GET);
366 return (oldvalue->value);
372 svmdb_local_get_variable_reference (svmdb_client_t * client,
373 svmdb_namespace_t namespace, char *var)
377 region_lock (client->db_rp, 19);
378 rv = local_get_variable_nolock (client, namespace, (u8 *) var);
379 region_unlock (client->db_rp);
384 svmdb_local_get_string_variable (svmdb_client_t * client, char *var)
388 region_lock (client->db_rp, 13);
389 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_STRING, (u8 *) var);
391 if (rv && vec_len (rv))
393 rv = format (0, "%s", rv);
396 region_unlock (client->db_rp);
397 return ((char *) rv);
401 svmdb_local_dump_strings (svmdb_client_t * client)
406 svmdb_shm_hdr_t *shm = client->shm;
408 region_lock (client->db_rp, 14);
410 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
413 hash_foreach_mem(key, value, h,
415 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
417 fformat(stdout, "%s: %s\n", key,
418 vec_len(v->value) ? v->value : (u8 *)"(nil)");
421 region_unlock (client->db_rp);
425 svmdb_local_unset_vec_variable (svmdb_client_t * client, char *var)
429 region_lock (client->db_rp, 15);
430 oldheap = svm_push_data_heap (client->db_rp);
431 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
432 svm_pop_heap (oldheap);
433 region_unlock (client->db_rp);
437 svmdb_local_set_vec_variable (svmdb_client_t * client,
438 char *var, void *val_arg, u32 elsize)
440 u8 *val = (u8 *) val_arg;
443 region_lock (client->db_rp, 16);
444 oldheap = svm_push_data_heap (client->db_rp);
446 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
447 local_set_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var,
450 svm_pop_heap (oldheap);
451 region_unlock (client->db_rp);
455 svmdb_local_get_vec_variable (svmdb_client_t * client, char *var, u32 elsize)
460 region_lock (client->db_rp, 17);
462 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
464 if (rv && vec_len (rv))
466 /* Make a copy in process-local memory */
467 vec_alloc (copy, vec_len (rv) * elsize);
468 clib_memcpy (copy, rv, vec_len (rv) * elsize);
469 _vec_len (copy) = vec_len (rv);
470 region_unlock (client->db_rp);
473 region_unlock (client->db_rp);
478 svmdb_local_dump_vecs (svmdb_client_t * client)
483 svmdb_shm_hdr_t *shm;
485 region_lock (client->db_rp, 17);
488 h = client->shm->namespaces[SVMDB_NAMESPACE_VEC];
491 hash_foreach_mem(key, value, h,
493 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
494 (void) fformat(stdout, "%s:\n %U (%.2f)\n", key,
495 format_hex_bytes, v->value,
496 vec_len(v->value)*v->elsize, ((f64 *)(v->value))[0]);
500 region_unlock (client->db_rp);
504 svmdb_local_find_or_add_vec_variable (svmdb_client_t * client,
505 char *var, u32 nbytes)
510 region_lock (client->db_rp, 18);
511 oldheap = svm_push_data_heap (client->db_rp);
513 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
523 svmdb_shm_hdr_t *shm;
524 svmdb_value_t *newvalue;
527 h = shm->namespaces[SVMDB_NAMESPACE_VEC];
529 pool_get (shm->values, newvalue);
530 memset (newvalue, 0, sizeof (*newvalue));
531 newvalue->elsize = 1;
532 vec_alloc (newvalue->value, nbytes);
533 _vec_len (newvalue->value) = nbytes;
534 name = format (0, "%s%c", var, 0);
535 hash_set_mem (h, name, newvalue - shm->values);
536 shm->namespaces[SVMDB_NAMESPACE_VEC] = h;
537 rv = newvalue->value;
541 svm_pop_heap (oldheap);
542 region_unlock (client->db_rp);
547 * fd.io coding-style-patch-verification: ON
550 * eval: (c-set-style "gnu")