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/heap.h>
39 #include <vppinfra/pool.h>
40 #include <vppinfra/format.h>
41 #include <vppinfra/serialize.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... */
110 /* Add a bogus client (pid=0) so the svm won't be deallocated */
112 ("[%d] adding fake client (pid=0) so '%s' won't be unlinked",
113 getpid (), db_rp->region_name);
114 oldheap = svm_push_pvt_heap (db_rp);
115 vec_add1 (client->db_rp->client_pids, 0);
116 svm_pop_heap (oldheap);
118 oldheap = svm_push_data_heap (db_rp);
120 vec_validate (hp, 0);
121 hp->version = SVMDB_SHM_VERSION;
122 hp->namespaces[SVMDB_NAMESPACE_STRING]
123 = hash_create_string (0, sizeof (uword));
124 hp->namespaces[SVMDB_NAMESPACE_VEC]
125 = hash_create_string (0, sizeof (uword));
127 db_rp->user_ctx = hp;
130 svm_pop_heap (oldheap);
131 region_unlock (client->db_rp);
132 client->pid = getpid ();
138 svmdb_unmap (svmdb_client_t * client)
142 if (!svm_get_root_rp ())
145 svm_region_unmap ((void *) client->db_rp);
151 notify_value (svmdb_value_t * v, svmdb_action_t a)
157 u32 *dead_registrations = 0;
161 for (i = 0; i < vec_len (v->notifications); i++)
163 np = vec_elt_at_index (v->notifications, i);
166 value = (np->action << 28) | (np->opaque);
167 sv.sival_ptr = (void *) (uword) value;
171 if (sigqueue (np->pid, np->signum, sv) == 0)
175 while (rv == EAGAIN);
178 vec_add1 (dead_registrations, i);
182 for (i = 0; i < vec_len (dead_registrations); i++)
184 np = vec_elt_at_index (v->notifications, dead_registrations[i]);
185 clib_warning ("dead reg pid %d sig %d action %d opaque %x",
186 np->pid, np->signum, np->action, np->opaque);
187 vec_delete (v->notifications, 1, dead_registrations[i]);
189 vec_free (dead_registrations);
193 svmdb_local_add_del_notification (svmdb_client_t * client,
194 svmdb_notification_args_t * a)
199 svmdb_shm_hdr_t *shm;
200 u8 *placeholder_value = 0;
201 svmdb_value_t *value;
208 region_lock (client->db_rp, 18);
210 oldheap = svm_push_data_heap (client->db_rp);
212 h = shm->namespaces[a->nspace];
214 hp = hash_get_pair_mem (h, a->var);
217 local_set_variable_nolock (client, a->nspace, (u8 *) a->var,
218 placeholder_value, a->elsize);
219 /* might have moved */
220 h = shm->namespaces[a->nspace];
221 hp = hash_get_pair_mem (h, a->var);
225 value = pool_elt_at_index (shm->values, hp->value[0]);
227 for (i = 0; i < vec_len (value->notifications); i++)
229 np = vec_elt_at_index (value->notifications, i);
230 if ((np->pid == client->pid)
231 && (np->signum == a->signum)
232 && (np->action == a->action) && (np->opaque == a->opaque))
234 if (a->add_del == 0 /* delete */ )
236 vec_delete (value->notifications, 1, i);
242 ("%s: ignore dup reg pid %d signum %d action %d opaque %x",
243 a->var, client->pid, a->signum, a->action, a->opaque);
255 vec_add2 (value->notifications, np, 1);
256 np->pid = client->pid;
257 np->signum = a->signum;
258 np->action = a->action;
259 np->opaque = a->opaque;
262 svm_pop_heap (oldheap);
263 region_unlock (client->db_rp);
269 local_unset_variable_nolock (svmdb_client_t * client,
270 svmdb_namespace_t namespace, char *var)
273 svmdb_value_t *oldvalue;
276 h = client->shm->namespaces[namespace];
277 hp = hash_get_pair_mem (h, var);
280 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
281 if (vec_len (oldvalue->notifications))
282 notify_value (oldvalue, SVMDB_ACTION_UNSET);
283 /* zero length value means unset */
284 vec_set_len (oldvalue->value, 0);
286 client->shm->namespaces[namespace] = h;
290 svmdb_local_unset_string_variable (svmdb_client_t * client, char *var)
294 region_lock (client->db_rp, 11);
295 oldheap = svm_push_data_heap (client->db_rp);
296 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
297 svm_pop_heap (oldheap);
298 region_unlock (client->db_rp);
302 local_set_variable_nolock (svmdb_client_t * client,
303 svmdb_namespace_t namespace,
304 u8 * var, u8 * val, u32 elsize)
309 svmdb_shm_hdr_t *shm;
312 h = shm->namespaces[namespace];
313 hp = hash_get_pair_mem (h, var);
316 svmdb_value_t *oldvalue;
317 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
318 vec_alloc (oldvalue->value, vec_len (val) * elsize);
319 clib_memcpy (oldvalue->value, val, vec_len (val) * elsize);
320 vec_set_len (oldvalue->value, vec_len (val));
321 notify_value (oldvalue, SVMDB_ACTION_SET);
325 svmdb_value_t *newvalue;
326 pool_get (shm->values, newvalue);
327 clib_memset (newvalue, 0, sizeof (*newvalue));
328 newvalue->elsize = elsize;
329 vec_alloc (newvalue->value, vec_len (val) * elsize);
330 clib_memcpy (newvalue->value, val, vec_len (val) * elsize);
331 vec_set_len (newvalue->value, vec_len (val));
332 name = format (0, "%s%c", var, 0);
333 hash_set_mem (h, name, newvalue - shm->values);
335 shm->namespaces[namespace] = h;
339 svmdb_local_set_string_variable (svmdb_client_t * client,
340 char *var, char *val)
344 region_lock (client->db_rp, 12);
345 oldheap = svm_push_data_heap (client->db_rp);
347 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
349 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
350 (u8 *) var, (u8 *) val, 1 /* elsize */ );
351 svm_pop_heap (oldheap);
352 region_unlock (client->db_rp);
356 local_get_variable_nolock (svmdb_client_t * client,
357 svmdb_namespace_t namespace, u8 * var)
361 svmdb_shm_hdr_t *shm;
362 svmdb_value_t *oldvalue;
365 h = shm->namespaces[namespace];
366 p = hash_get_mem (h, var);
369 oldvalue = pool_elt_at_index (shm->values, p[0]);
370 notify_value (oldvalue, SVMDB_ACTION_GET);
371 return (oldvalue->value);
377 svmdb_local_get_variable_reference (svmdb_client_t * client,
378 svmdb_namespace_t namespace, char *var)
382 region_lock (client->db_rp, 19);
383 rv = local_get_variable_nolock (client, namespace, (u8 *) var);
384 region_unlock (client->db_rp);
389 svmdb_local_get_string_variable (svmdb_client_t * client, char *var)
393 region_lock (client->db_rp, 13);
394 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_STRING, (u8 *) var);
396 if (rv && vec_len (rv))
398 rv = format (0, "%s", rv);
401 region_unlock (client->db_rp);
402 return ((char *) rv);
406 svmdb_local_dump_strings (svmdb_client_t * client)
411 svmdb_shm_hdr_t *shm = client->shm;
413 region_lock (client->db_rp, 14);
415 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
418 hash_foreach_mem(key, value, h,
420 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
422 fformat(stdout, "%s: %s\n", key,
423 vec_len(v->value) ? v->value : (u8 *)"(nil)");
426 region_unlock (client->db_rp);
430 svmdb_local_serialize_strings (svmdb_client_t * client, char *filename)
435 svmdb_shm_hdr_t *shm = client->shm;
436 serialize_main_t _sm = { 0 }, *sm = &_sm;
437 clib_error_t *error = 0;
438 u8 *sanitized_name = 0;
441 if (strstr (filename, "..") || index (filename, '/'))
443 error = clib_error_return (0, "Illegal characters in filename '%s'",
448 sanitized_name = format (0, "/tmp/%s%c", filename, 0);
450 fd = creat ((char *) sanitized_name, 0644);
454 error = clib_error_return_unix (0, "Create '%s'", sanitized_name);
458 serialize_open_clib_file_descriptor (sm, fd);
460 region_lock (client->db_rp, 20);
462 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
464 serialize_likely_small_unsigned_integer (sm, hash_elts (h));
467 hash_foreach_mem(key, value, h,
469 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
471 /* Omit names with nil values */
472 if (vec_len(v->value))
474 serialize_cstring (sm, (char *)key);
475 serialize_cstring (sm, (char *)v->value);
479 region_unlock (client->db_rp);
481 serialize_close (sm);
484 if (fd > 0 && close (fd) < 0)
485 error = clib_error_return_unix (0, "close fd %d", fd);
489 clib_error_report (error);
496 svmdb_local_unserialize_strings (svmdb_client_t * client, char *filename)
498 serialize_main_t _sm = { 0 }, *sm = &_sm;
500 clib_error_t *error = 0;
506 fd = open (filename, O_RDONLY);
510 error = clib_error_return_unix (0, "Failed to open '%s'", filename);
514 unserialize_open_clib_file_descriptor (sm, fd);
516 region_lock (client->db_rp, 21);
517 oldheap = svm_push_data_heap (client->db_rp);
519 nelts = unserialize_likely_small_unsigned_integer (sm);
521 for (i = 0; i < nelts; i++)
523 unserialize_cstring (sm, (char **) &key);
524 unserialize_cstring (sm, (char **) &value);
525 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
526 key, value, 1 /* elsize */ );
530 svm_pop_heap (oldheap);
531 region_unlock (client->db_rp);
533 serialize_close (sm);
536 if (fd > 0 && close (fd) < 0)
537 error = clib_error_return_unix (0, "close fd %d", fd);
541 clib_error_report (error);
548 svmdb_local_unset_vec_variable (svmdb_client_t * client, char *var)
552 region_lock (client->db_rp, 15);
553 oldheap = svm_push_data_heap (client->db_rp);
554 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
555 svm_pop_heap (oldheap);
556 region_unlock (client->db_rp);
560 svmdb_local_set_vec_variable (svmdb_client_t * client,
561 char *var, void *val_arg, u32 elsize)
563 u8 *val = (u8 *) val_arg;
566 region_lock (client->db_rp, 16);
567 oldheap = svm_push_data_heap (client->db_rp);
569 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
570 local_set_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var,
573 svm_pop_heap (oldheap);
574 region_unlock (client->db_rp);
578 svmdb_local_get_vec_variable (svmdb_client_t * client, char *var, u32 elsize)
583 region_lock (client->db_rp, 17);
585 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
587 if (rv && vec_len (rv))
589 /* Make a copy in process-local memory */
590 vec_alloc (copy, vec_len (rv) * elsize);
591 clib_memcpy (copy, rv, vec_len (rv) * elsize);
592 vec_set_len (copy, vec_len (rv));
593 region_unlock (client->db_rp);
596 region_unlock (client->db_rp);
601 svmdb_local_dump_vecs (svmdb_client_t * client)
606 svmdb_shm_hdr_t *shm;
608 region_lock (client->db_rp, 17);
611 h = client->shm->namespaces[SVMDB_NAMESPACE_VEC];
614 hash_foreach_mem(key, value, h,
616 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
617 (void) fformat(stdout, "%s:\n %U (%.2f)\n", key,
618 format_hex_bytes, v->value,
619 vec_len(v->value)*v->elsize, ((f64 *)(v->value))[0]);
623 region_unlock (client->db_rp);
627 svmdb_local_find_or_add_vec_variable (svmdb_client_t * client,
628 char *var, u32 nbytes)
633 region_lock (client->db_rp, 18);
634 oldheap = svm_push_data_heap (client->db_rp);
636 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
646 svmdb_shm_hdr_t *shm;
647 svmdb_value_t *newvalue;
650 h = shm->namespaces[SVMDB_NAMESPACE_VEC];
652 pool_get (shm->values, newvalue);
653 clib_memset (newvalue, 0, sizeof (*newvalue));
654 newvalue->elsize = 1;
655 vec_alloc (newvalue->value, nbytes);
656 vec_set_len (newvalue->value, nbytes);
657 name = format (0, "%s%c", var, 0);
658 hash_set_mem (h, name, newvalue - shm->values);
659 shm->namespaces[SVMDB_NAMESPACE_VEC] = h;
660 rv = newvalue->value;
664 svm_pop_heap (oldheap);
665 region_unlock (client->db_rp);
670 * fd.io coding-style-patch-verification: ON
673 * eval: (c-set-style "gnu")