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>
42 #include <vppinfra/serialize.h>
46 static void local_set_variable_nolock (svmdb_client_t * client,
47 svmdb_namespace_t namespace,
48 u8 * var, u8 * val, u32 elsize);
51 region_lock (svm_region_t * rp, int tag)
53 pthread_mutex_lock (&rp->mutex);
55 rp->mutex_owner_pid = getpid ();
56 rp->mutex_owner_tag = tag;
61 region_unlock (svm_region_t * rp)
64 rp->mutex_owner_pid = 0;
65 rp->mutex_owner_tag = 0;
67 pthread_mutex_unlock (&rp->mutex);
71 svmdb_map (svmdb_map_args_t * dba)
73 svmdb_client_t *client = 0;
74 svm_map_region_args_t *a = 0;
77 svmdb_shm_hdr_t *hp = 0;
79 vec_validate (client, 0);
82 svm_region_init_chroot_uid_gid (dba->root_path, dba->uid, dba->gid);
84 a->root_path = dba->root_path;
86 a->size = dba->size ? dba->size : SVMDB_DEFAULT_SIZE;
87 a->flags = SVM_FLAGS_MHEAP;
91 db_rp = client->db_rp = svm_region_find_or_create (a);
97 region_lock (client->db_rp, 10);
98 /* Has someone else set up the shared-memory variable table? */
101 client->shm = (void *) db_rp->user_ctx;
102 client->pid = getpid ();
103 region_unlock (client->db_rp);
104 ASSERT (client->shm->version == SVMDB_SHM_VERSION);
107 /* Nope, it's our problem... */
111 /* Add a bogus client (pid=0) so the svm won't be deallocated */
113 ("[%d] adding fake client (pid=0) so '%s' won't be unlinked",
114 getpid (), db_rp->region_name);
115 oldheap = svm_push_pvt_heap (db_rp);
116 vec_add1 (client->db_rp->client_pids, 0);
117 svm_pop_heap (oldheap);
119 oldheap = svm_push_data_heap (db_rp);
121 vec_validate (hp, 0);
122 hp->version = SVMDB_SHM_VERSION;
123 hp->namespaces[SVMDB_NAMESPACE_STRING]
124 = hash_create_string (0, sizeof (uword));
125 hp->namespaces[SVMDB_NAMESPACE_VEC]
126 = hash_create_string (0, sizeof (uword));
128 db_rp->user_ctx = hp;
131 svm_pop_heap (oldheap);
132 region_unlock (client->db_rp);
133 client->pid = getpid ();
139 svmdb_unmap (svmdb_client_t * client)
143 if (!svm_get_root_rp ())
146 svm_region_unmap ((void *) client->db_rp);
152 notify_value (svmdb_value_t * v, svmdb_action_t a)
158 u32 *dead_registrations = 0;
162 for (i = 0; i < vec_len (v->notifications); i++)
164 np = vec_elt_at_index (v->notifications, i);
167 value = (np->action << 28) | (np->opaque);
168 sv.sival_ptr = (void *) (uword) value;
172 if (sigqueue (np->pid, np->signum, sv) == 0)
176 while (rv == EAGAIN);
179 vec_add1 (dead_registrations, i);
183 for (i = 0; i < vec_len (dead_registrations); i++)
185 np = vec_elt_at_index (v->notifications, dead_registrations[i]);
186 clib_warning ("dead reg pid %d sig %d action %d opaque %x",
187 np->pid, np->signum, np->action, np->opaque);
188 vec_delete (v->notifications, 1, dead_registrations[i]);
190 vec_free (dead_registrations);
194 svmdb_local_add_del_notification (svmdb_client_t * client,
195 svmdb_notification_args_t * a)
200 svmdb_shm_hdr_t *shm;
201 u8 *placeholder_value = 0;
202 svmdb_value_t *value;
209 region_lock (client->db_rp, 18);
211 oldheap = svm_push_data_heap (client->db_rp);
213 h = shm->namespaces[a->nspace];
215 hp = hash_get_pair_mem (h, a->var);
218 local_set_variable_nolock (client, a->nspace, (u8 *) a->var,
219 placeholder_value, a->elsize);
220 /* might have moved */
221 h = shm->namespaces[a->nspace];
222 hp = hash_get_pair_mem (h, a->var);
226 value = pool_elt_at_index (shm->values, hp->value[0]);
228 for (i = 0; i < vec_len (value->notifications); i++)
230 np = vec_elt_at_index (value->notifications, i);
231 if ((np->pid == client->pid)
232 && (np->signum == a->signum)
233 && (np->action == a->action) && (np->opaque == a->opaque))
235 if (a->add_del == 0 /* delete */ )
237 vec_delete (value->notifications, 1, i);
243 ("%s: ignore dup reg pid %d signum %d action %d opaque %x",
244 a->var, client->pid, a->signum, a->action, a->opaque);
256 vec_add2 (value->notifications, np, 1);
257 np->pid = client->pid;
258 np->signum = a->signum;
259 np->action = a->action;
260 np->opaque = a->opaque;
263 svm_pop_heap (oldheap);
264 region_unlock (client->db_rp);
270 local_unset_variable_nolock (svmdb_client_t * client,
271 svmdb_namespace_t namespace, char *var)
274 svmdb_value_t *oldvalue;
277 h = client->shm->namespaces[namespace];
278 hp = hash_get_pair_mem (h, var);
281 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
282 if (vec_len (oldvalue->notifications))
283 notify_value (oldvalue, SVMDB_ACTION_UNSET);
284 /* zero length value means unset */
285 _vec_len (oldvalue->value) = 0;
287 client->shm->namespaces[namespace] = h;
291 svmdb_local_unset_string_variable (svmdb_client_t * client, char *var)
295 region_lock (client->db_rp, 11);
296 oldheap = svm_push_data_heap (client->db_rp);
297 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
298 svm_pop_heap (oldheap);
299 region_unlock (client->db_rp);
303 local_set_variable_nolock (svmdb_client_t * client,
304 svmdb_namespace_t namespace,
305 u8 * var, u8 * val, u32 elsize)
310 svmdb_shm_hdr_t *shm;
313 h = shm->namespaces[namespace];
314 hp = hash_get_pair_mem (h, var);
317 svmdb_value_t *oldvalue;
318 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
319 vec_alloc (oldvalue->value, vec_len (val) * elsize);
320 clib_memcpy (oldvalue->value, val, vec_len (val) * elsize);
321 _vec_len (oldvalue->value) = vec_len (val);
322 notify_value (oldvalue, SVMDB_ACTION_SET);
326 svmdb_value_t *newvalue;
327 pool_get (shm->values, newvalue);
328 clib_memset (newvalue, 0, sizeof (*newvalue));
329 newvalue->elsize = elsize;
330 vec_alloc (newvalue->value, vec_len (val) * elsize);
331 clib_memcpy (newvalue->value, val, vec_len (val) * elsize);
332 _vec_len (newvalue->value) = vec_len (val);
333 name = format (0, "%s%c", var, 0);
334 hash_set_mem (h, name, newvalue - shm->values);
336 shm->namespaces[namespace] = h;
340 svmdb_local_set_string_variable (svmdb_client_t * client,
341 char *var, char *val)
345 region_lock (client->db_rp, 12);
346 oldheap = svm_push_data_heap (client->db_rp);
348 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
350 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
351 (u8 *) var, (u8 *) val, 1 /* elsize */ );
352 svm_pop_heap (oldheap);
353 region_unlock (client->db_rp);
357 local_get_variable_nolock (svmdb_client_t * client,
358 svmdb_namespace_t namespace, u8 * var)
362 svmdb_shm_hdr_t *shm;
363 svmdb_value_t *oldvalue;
366 h = shm->namespaces[namespace];
367 p = hash_get_mem (h, var);
370 oldvalue = pool_elt_at_index (shm->values, p[0]);
371 notify_value (oldvalue, SVMDB_ACTION_GET);
372 return (oldvalue->value);
378 svmdb_local_get_variable_reference (svmdb_client_t * client,
379 svmdb_namespace_t namespace, char *var)
383 region_lock (client->db_rp, 19);
384 rv = local_get_variable_nolock (client, namespace, (u8 *) var);
385 region_unlock (client->db_rp);
390 svmdb_local_get_string_variable (svmdb_client_t * client, char *var)
394 region_lock (client->db_rp, 13);
395 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_STRING, (u8 *) var);
397 if (rv && vec_len (rv))
399 rv = format (0, "%s", rv);
402 region_unlock (client->db_rp);
403 return ((char *) rv);
407 svmdb_local_dump_strings (svmdb_client_t * client)
412 svmdb_shm_hdr_t *shm = client->shm;
414 region_lock (client->db_rp, 14);
416 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
419 hash_foreach_mem(key, value, h,
421 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
423 fformat(stdout, "%s: %s\n", key,
424 vec_len(v->value) ? v->value : (u8 *)"(nil)");
427 region_unlock (client->db_rp);
431 svmdb_local_serialize_strings (svmdb_client_t * client, char *filename)
436 svmdb_shm_hdr_t *shm = client->shm;
437 serialize_main_t _sm, *sm = &_sm;
438 clib_error_t *error = 0;
439 u8 *sanitized_name = 0;
442 if (strstr (filename, "..") || index (filename, '/'))
444 error = clib_error_return (0, "Illegal characters in filename '%s'",
449 sanitized_name = format (0, "/tmp/%s%c", filename, 0);
451 fd = creat ((char *) sanitized_name, 0644);
455 error = clib_error_return_unix (0, "Create '%s'", sanitized_name);
459 serialize_open_clib_file_descriptor (sm, fd);
461 region_lock (client->db_rp, 20);
463 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
465 serialize_likely_small_unsigned_integer (sm, hash_elts (h));
468 hash_foreach_mem(key, value, h,
470 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
472 /* Omit names with nil values */
473 if (vec_len(v->value))
475 serialize_cstring (sm, (char *)key);
476 serialize_cstring (sm, (char *)v->value);
480 region_unlock (client->db_rp);
482 serialize_close (sm);
485 if (fd > 0 && close (fd) < 0)
486 error = clib_error_return_unix (0, "close fd %d", fd);
490 clib_error_report (error);
497 svmdb_local_unserialize_strings (svmdb_client_t * client, char *filename)
499 serialize_main_t _sm, *sm = &_sm;
501 clib_error_t *error = 0;
507 fd = open (filename, O_RDONLY);
511 error = clib_error_return_unix (0, "Failed to open '%s'", filename);
515 unserialize_open_clib_file_descriptor (sm, fd);
517 region_lock (client->db_rp, 21);
518 oldheap = svm_push_data_heap (client->db_rp);
520 nelts = unserialize_likely_small_unsigned_integer (sm);
522 for (i = 0; i < nelts; i++)
524 unserialize_cstring (sm, (char **) &key);
525 unserialize_cstring (sm, (char **) &value);
526 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
527 key, value, 1 /* elsize */ );
531 svm_pop_heap (oldheap);
532 region_unlock (client->db_rp);
534 serialize_close (sm);
537 if (fd > 0 && close (fd) < 0)
538 error = clib_error_return_unix (0, "close fd %d", fd);
542 clib_error_report (error);
549 svmdb_local_unset_vec_variable (svmdb_client_t * client, char *var)
553 region_lock (client->db_rp, 15);
554 oldheap = svm_push_data_heap (client->db_rp);
555 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
556 svm_pop_heap (oldheap);
557 region_unlock (client->db_rp);
561 svmdb_local_set_vec_variable (svmdb_client_t * client,
562 char *var, void *val_arg, u32 elsize)
564 u8 *val = (u8 *) val_arg;
567 region_lock (client->db_rp, 16);
568 oldheap = svm_push_data_heap (client->db_rp);
570 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
571 local_set_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var,
574 svm_pop_heap (oldheap);
575 region_unlock (client->db_rp);
579 svmdb_local_get_vec_variable (svmdb_client_t * client, char *var, u32 elsize)
584 region_lock (client->db_rp, 17);
586 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
588 if (rv && vec_len (rv))
590 /* Make a copy in process-local memory */
591 vec_alloc (copy, vec_len (rv) * elsize);
592 clib_memcpy (copy, rv, vec_len (rv) * elsize);
593 _vec_len (copy) = vec_len (rv);
594 region_unlock (client->db_rp);
597 region_unlock (client->db_rp);
602 svmdb_local_dump_vecs (svmdb_client_t * client)
607 svmdb_shm_hdr_t *shm;
609 region_lock (client->db_rp, 17);
612 h = client->shm->namespaces[SVMDB_NAMESPACE_VEC];
615 hash_foreach_mem(key, value, h,
617 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
618 (void) fformat(stdout, "%s:\n %U (%.2f)\n", key,
619 format_hex_bytes, v->value,
620 vec_len(v->value)*v->elsize, ((f64 *)(v->value))[0]);
624 region_unlock (client->db_rp);
628 svmdb_local_find_or_add_vec_variable (svmdb_client_t * client,
629 char *var, u32 nbytes)
634 region_lock (client->db_rp, 18);
635 oldheap = svm_push_data_heap (client->db_rp);
637 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
647 svmdb_shm_hdr_t *shm;
648 svmdb_value_t *newvalue;
651 h = shm->namespaces[SVMDB_NAMESPACE_VEC];
653 pool_get (shm->values, newvalue);
654 clib_memset (newvalue, 0, sizeof (*newvalue));
655 newvalue->elsize = 1;
656 vec_alloc (newvalue->value, nbytes);
657 _vec_len (newvalue->value) = nbytes;
658 name = format (0, "%s%c", var, 0);
659 hash_set_mem (h, name, newvalue - shm->values);
660 shm->namespaces[SVMDB_NAMESPACE_VEC] = h;
661 rv = newvalue->value;
665 svm_pop_heap (oldheap);
666 region_unlock (client->db_rp);
671 * fd.io coding-style-patch-verification: ON
674 * eval: (c-set-style "gnu")