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... */
109 /* Add a bogus client (pid=0) so the svm won't be deallocated */
110 oldheap = svm_push_pvt_heap (db_rp);
111 vec_add1 (client->db_rp->client_pids, 0);
112 svm_pop_heap (oldheap);
114 oldheap = svm_push_data_heap (db_rp);
116 vec_validate (hp, 0);
117 hp->version = SVMDB_SHM_VERSION;
118 hp->namespaces[SVMDB_NAMESPACE_STRING]
119 = hash_create_string (0, sizeof (uword));
120 hp->namespaces[SVMDB_NAMESPACE_VEC]
121 = hash_create_string (0, sizeof (uword));
123 db_rp->user_ctx = hp;
126 svm_pop_heap (oldheap);
127 region_unlock (client->db_rp);
128 client->pid = getpid ();
134 svmdb_unmap (svmdb_client_t * client)
138 if (!svm_get_root_rp ())
141 svm_region_unmap ((void *) client->db_rp);
147 notify_value (svmdb_value_t * v, svmdb_action_t a)
153 u32 *dead_registrations = 0;
157 for (i = 0; i < vec_len (v->notifications); i++)
159 np = vec_elt_at_index (v->notifications, i);
162 value = (np->action << 28) | (np->opaque);
163 sv.sival_ptr = (void *) (uword) value;
167 if (sigqueue (np->pid, np->signum, sv) == 0)
171 while (rv == EAGAIN);
174 vec_add1 (dead_registrations, i);
178 for (i = 0; i < vec_len (dead_registrations); i++)
180 np = vec_elt_at_index (v->notifications, dead_registrations[i]);
181 clib_warning ("dead reg pid %d sig %d action %d opaque %x",
182 np->pid, np->signum, np->action, np->opaque);
183 vec_delete (v->notifications, 1, dead_registrations[i]);
185 vec_free (dead_registrations);
189 svmdb_local_add_del_notification (svmdb_client_t * client,
190 svmdb_notification_args_t * a)
195 svmdb_shm_hdr_t *shm;
197 svmdb_value_t *value;
204 region_lock (client->db_rp, 18);
206 oldheap = svm_push_data_heap (client->db_rp);
208 h = shm->namespaces[a->nspace];
210 hp = hash_get_pair_mem (h, a->var);
213 local_set_variable_nolock (client, a->nspace, (u8 *) a->var,
214 dummy_value, a->elsize);
215 /* might have moved */
216 h = shm->namespaces[a->nspace];
217 hp = hash_get_pair_mem (h, a->var);
221 value = pool_elt_at_index (shm->values, hp->value[0]);
223 for (i = 0; i < vec_len (value->notifications); i++)
225 np = vec_elt_at_index (value->notifications, i);
226 if ((np->pid == client->pid)
227 && (np->signum == a->signum)
228 && (np->action == a->action) && (np->opaque == a->opaque))
230 if (a->add_del == 0 /* delete */ )
232 vec_delete (value->notifications, 1, i);
238 ("%s: ignore dup reg pid %d signum %d action %d opaque %x",
239 a->var, client->pid, a->signum, a->action, a->opaque);
251 vec_add2 (value->notifications, np, 1);
252 np->pid = client->pid;
253 np->signum = a->signum;
254 np->action = a->action;
255 np->opaque = a->opaque;
258 svm_pop_heap (oldheap);
259 region_unlock (client->db_rp);
265 local_unset_variable_nolock (svmdb_client_t * client,
266 svmdb_namespace_t namespace, char *var)
269 svmdb_value_t *oldvalue;
272 h = client->shm->namespaces[namespace];
273 hp = hash_get_pair_mem (h, var);
276 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
277 if (vec_len (oldvalue->notifications))
278 notify_value (oldvalue, SVMDB_ACTION_UNSET);
279 /* zero length value means unset */
280 _vec_len (oldvalue->value) = 0;
282 client->shm->namespaces[namespace] = h;
286 svmdb_local_unset_string_variable (svmdb_client_t * client, char *var)
290 region_lock (client->db_rp, 11);
291 oldheap = svm_push_data_heap (client->db_rp);
292 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
293 svm_pop_heap (oldheap);
294 region_unlock (client->db_rp);
298 local_set_variable_nolock (svmdb_client_t * client,
299 svmdb_namespace_t namespace,
300 u8 * var, u8 * val, u32 elsize)
305 svmdb_shm_hdr_t *shm;
308 h = shm->namespaces[namespace];
309 hp = hash_get_pair_mem (h, var);
312 svmdb_value_t *oldvalue;
313 oldvalue = pool_elt_at_index (client->shm->values, hp->value[0]);
314 vec_alloc (oldvalue->value, vec_len (val) * elsize);
315 clib_memcpy (oldvalue->value, val, vec_len (val) * elsize);
316 _vec_len (oldvalue->value) = vec_len (val);
317 notify_value (oldvalue, SVMDB_ACTION_SET);
321 svmdb_value_t *newvalue;
322 pool_get (shm->values, newvalue);
323 memset (newvalue, 0, sizeof (*newvalue));
324 newvalue->elsize = elsize;
325 vec_alloc (newvalue->value, vec_len (val) * elsize);
326 clib_memcpy (newvalue->value, val, vec_len (val) * elsize);
327 _vec_len (newvalue->value) = vec_len (val);
328 name = format (0, "%s%c", var, 0);
329 hash_set_mem (h, name, newvalue - shm->values);
331 shm->namespaces[namespace] = h;
335 svmdb_local_set_string_variable (svmdb_client_t * client,
336 char *var, char *val)
340 region_lock (client->db_rp, 12);
341 oldheap = svm_push_data_heap (client->db_rp);
343 local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var);
345 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
346 (u8 *) var, (u8 *) val, 1 /* elsize */ );
347 svm_pop_heap (oldheap);
348 region_unlock (client->db_rp);
352 local_get_variable_nolock (svmdb_client_t * client,
353 svmdb_namespace_t namespace, u8 * var)
357 svmdb_shm_hdr_t *shm;
358 svmdb_value_t *oldvalue;
361 h = shm->namespaces[namespace];
362 p = hash_get_mem (h, var);
365 oldvalue = pool_elt_at_index (shm->values, p[0]);
366 notify_value (oldvalue, SVMDB_ACTION_GET);
367 return (oldvalue->value);
373 svmdb_local_get_variable_reference (svmdb_client_t * client,
374 svmdb_namespace_t namespace, char *var)
378 region_lock (client->db_rp, 19);
379 rv = local_get_variable_nolock (client, namespace, (u8 *) var);
380 region_unlock (client->db_rp);
385 svmdb_local_get_string_variable (svmdb_client_t * client, char *var)
389 region_lock (client->db_rp, 13);
390 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_STRING, (u8 *) var);
392 if (rv && vec_len (rv))
394 rv = format (0, "%s", rv);
397 region_unlock (client->db_rp);
398 return ((char *) rv);
402 svmdb_local_dump_strings (svmdb_client_t * client)
407 svmdb_shm_hdr_t *shm = client->shm;
409 region_lock (client->db_rp, 14);
411 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
414 hash_foreach_mem(key, value, h,
416 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
418 fformat(stdout, "%s: %s\n", key,
419 vec_len(v->value) ? v->value : (u8 *)"(nil)");
422 region_unlock (client->db_rp);
426 svmdb_local_serialize_strings (svmdb_client_t * client, char *filename)
431 svmdb_shm_hdr_t *shm = client->shm;
432 serialize_main_t _sm, *sm = &_sm;
433 clib_error_t *error = 0;
434 u8 *sanitized_name = 0;
437 if (strstr (filename, "..") || index (filename, '/'))
439 error = clib_error_return (0, "Illegal characters in filename '%s'",
444 sanitized_name = format (0, "/tmp/%s%c", filename, 0);
446 fd = creat ((char *) sanitized_name, 0644);
450 error = clib_error_return_unix (0, "Create '%s'", sanitized_name);
454 serialize_open_unix_file_descriptor (sm, fd);
456 region_lock (client->db_rp, 20);
458 h = client->shm->namespaces[SVMDB_NAMESPACE_STRING];
460 serialize_likely_small_unsigned_integer (sm, hash_elts (h));
463 hash_foreach_mem(key, value, h,
465 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
467 /* Omit names with nil values */
468 if (vec_len(v->value))
470 serialize_cstring (sm, (char *)key);
471 serialize_cstring (sm, (char *)v->value);
475 region_unlock (client->db_rp);
477 serialize_close (sm);
480 if (fd > 0 && close (fd) < 0)
481 error = clib_error_return_unix (0, "close fd %d", fd);
485 clib_error_report (error);
492 svmdb_local_unserialize_strings (svmdb_client_t * client, char *filename)
494 serialize_main_t _sm, *sm = &_sm;
496 clib_error_t *error = 0;
502 fd = open (filename, O_RDONLY);
506 error = clib_error_return_unix (0, "Failed to open '%s'", filename);
510 unserialize_open_unix_file_descriptor (sm, fd);
512 region_lock (client->db_rp, 21);
513 oldheap = svm_push_data_heap (client->db_rp);
515 nelts = unserialize_likely_small_unsigned_integer (sm);
517 for (i = 0; i < nelts; i++)
519 unserialize_cstring (sm, (char **) &key);
520 unserialize_cstring (sm, (char **) &value);
521 local_set_variable_nolock (client, SVMDB_NAMESPACE_STRING,
522 key, value, 1 /* elsize */ );
526 svm_pop_heap (oldheap);
527 region_unlock (client->db_rp);
529 serialize_close (sm);
532 if (fd > 0 && close (fd) < 0)
533 error = clib_error_return_unix (0, "close fd %d", fd);
537 clib_error_report (error);
544 svmdb_local_unset_vec_variable (svmdb_client_t * client, char *var)
548 region_lock (client->db_rp, 15);
549 oldheap = svm_push_data_heap (client->db_rp);
550 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
551 svm_pop_heap (oldheap);
552 region_unlock (client->db_rp);
556 svmdb_local_set_vec_variable (svmdb_client_t * client,
557 char *var, void *val_arg, u32 elsize)
559 u8 *val = (u8 *) val_arg;
562 region_lock (client->db_rp, 16);
563 oldheap = svm_push_data_heap (client->db_rp);
565 local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var);
566 local_set_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var,
569 svm_pop_heap (oldheap);
570 region_unlock (client->db_rp);
574 svmdb_local_get_vec_variable (svmdb_client_t * client, char *var, u32 elsize)
579 region_lock (client->db_rp, 17);
581 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
583 if (rv && vec_len (rv))
585 /* Make a copy in process-local memory */
586 vec_alloc (copy, vec_len (rv) * elsize);
587 clib_memcpy (copy, rv, vec_len (rv) * elsize);
588 _vec_len (copy) = vec_len (rv);
589 region_unlock (client->db_rp);
592 region_unlock (client->db_rp);
597 svmdb_local_dump_vecs (svmdb_client_t * client)
602 svmdb_shm_hdr_t *shm;
604 region_lock (client->db_rp, 17);
607 h = client->shm->namespaces[SVMDB_NAMESPACE_VEC];
610 hash_foreach_mem(key, value, h,
612 svmdb_value_t *v = pool_elt_at_index (shm->values, value);
613 (void) fformat(stdout, "%s:\n %U (%.2f)\n", key,
614 format_hex_bytes, v->value,
615 vec_len(v->value)*v->elsize, ((f64 *)(v->value))[0]);
619 region_unlock (client->db_rp);
623 svmdb_local_find_or_add_vec_variable (svmdb_client_t * client,
624 char *var, u32 nbytes)
629 region_lock (client->db_rp, 18);
630 oldheap = svm_push_data_heap (client->db_rp);
632 rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var);
642 svmdb_shm_hdr_t *shm;
643 svmdb_value_t *newvalue;
646 h = shm->namespaces[SVMDB_NAMESPACE_VEC];
648 pool_get (shm->values, newvalue);
649 memset (newvalue, 0, sizeof (*newvalue));
650 newvalue->elsize = 1;
651 vec_alloc (newvalue->value, nbytes);
652 _vec_len (newvalue->value) = nbytes;
653 name = format (0, "%s%c", var, 0);
654 hash_set_mem (h, name, newvalue - shm->values);
655 shm->namespaces[SVMDB_NAMESPACE_VEC] = h;
656 rv = newvalue->value;
660 svm_pop_heap (oldheap);
661 region_unlock (client->db_rp);
666 * fd.io coding-style-patch-verification: ON
669 * eval: (c-set-style "gnu")