Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * svmdb.c -- simple shared memory database |
| 4 | * |
| 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: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <signal.h> |
| 27 | #include <pthread.h> |
| 28 | #include <unistd.h> |
| 29 | #include <time.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <string.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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | #include <vppinfra/heap.h> |
| 39 | #include <vppinfra/pool.h> |
| 40 | #include <vppinfra/format.h> |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 41 | #include <vppinfra/serialize.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | |
| 43 | #include "svmdb.h" |
| 44 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 45 | static void local_set_variable_nolock (svmdb_client_t * client, |
| 46 | svmdb_namespace_t namespace, |
| 47 | u8 * var, u8 * val, u32 elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 49 | always_inline void |
| 50 | region_lock (svm_region_t * rp, int tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 52 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 54 | rp->mutex_owner_pid = getpid (); |
| 55 | rp->mutex_owner_tag = tag; |
| 56 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 59 | always_inline void |
| 60 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | { |
| 62 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 63 | rp->mutex_owner_pid = 0; |
| 64 | rp->mutex_owner_tag = 0; |
| 65 | #endif |
| 66 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 69 | svmdb_client_t * |
| 70 | svmdb_map (svmdb_map_args_t * dba) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 72 | svmdb_client_t *client = 0; |
| 73 | svm_map_region_args_t *a = 0; |
| 74 | svm_region_t *db_rp; |
| 75 | void *oldheap; |
| 76 | svmdb_shm_hdr_t *hp = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 78 | vec_validate (client, 0); |
| 79 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 81 | svm_region_init_chroot_uid_gid (dba->root_path, dba->uid, dba->gid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 83 | a->root_path = dba->root_path; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 84 | a->name = "/db"; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 85 | a->size = dba->size ? dba->size : SVMDB_DEFAULT_SIZE; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 86 | a->flags = SVM_FLAGS_MHEAP; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 87 | a->uid = dba->uid; |
| 88 | a->gid = dba->gid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 90 | db_rp = client->db_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 92 | ASSERT (db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 94 | vec_free (a); |
| 95 | |
| 96 | region_lock (client->db_rp, 10); |
| 97 | /* Has someone else set up the shared-memory variable table? */ |
| 98 | if (db_rp->user_ctx) |
| 99 | { |
| 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); |
| 104 | return (client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 106 | /* Nope, it's our problem... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 108 | if (CLIB_DEBUG > 2) |
| 109 | { |
| 110 | /* Add a bogus client (pid=0) so the svm won't be deallocated */ |
| 111 | clib_warning |
| 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); |
| 117 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 118 | oldheap = svm_push_data_heap (db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 127 | db_rp->user_ctx = hp; |
| 128 | client->shm = hp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 130 | svm_pop_heap (oldheap); |
| 131 | region_unlock (client->db_rp); |
| 132 | client->pid = getpid (); |
| 133 | |
| 134 | return (client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 137 | void |
| 138 | svmdb_unmap (svmdb_client_t * client) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 140 | ASSERT (client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | if (!svm_get_root_rp ()) |
| 143 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 145 | svm_region_unmap ((void *) client->db_rp); |
| 146 | svm_region_exit (); |
| 147 | vec_free (client); |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | notify_value (svmdb_value_t * v, svmdb_action_t a) |
| 152 | { |
| 153 | int i; |
| 154 | int rv; |
| 155 | union sigval sv; |
| 156 | u32 value; |
| 157 | u32 *dead_registrations = 0; |
| 158 | |
| 159 | svmdb_notify_t *np; |
| 160 | |
| 161 | for (i = 0; i < vec_len (v->notifications); i++) |
| 162 | { |
| 163 | np = vec_elt_at_index (v->notifications, i); |
| 164 | if (np->action == a) |
| 165 | { |
| 166 | value = (np->action << 28) | (np->opaque); |
| 167 | sv.sival_ptr = (void *) (uword) value; |
| 168 | do |
| 169 | { |
| 170 | rv = 0; |
| 171 | if (sigqueue (np->pid, np->signum, sv) == 0) |
| 172 | break; |
| 173 | rv = errno; |
| 174 | } |
| 175 | while (rv == EAGAIN); |
| 176 | if (rv == 0) |
| 177 | continue; |
| 178 | vec_add1 (dead_registrations, i); |
| 179 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 182 | for (i = 0; i < vec_len (dead_registrations); i++) |
| 183 | { |
| 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]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 189 | vec_free (dead_registrations); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 192 | int |
| 193 | svmdb_local_add_del_notification (svmdb_client_t * client, |
| 194 | svmdb_notification_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 196 | uword *h; |
| 197 | void *oldheap; |
| 198 | hash_pair_t *hp; |
| 199 | svmdb_shm_hdr_t *shm; |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 200 | u8 *placeholder_value = 0; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 201 | svmdb_value_t *value; |
| 202 | svmdb_notify_t *np; |
| 203 | int i; |
| 204 | int rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 206 | ASSERT (a->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 208 | region_lock (client->db_rp, 18); |
| 209 | shm = client->shm; |
| 210 | oldheap = svm_push_data_heap (client->db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 212 | h = shm->namespaces[a->nspace]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 214 | hp = hash_get_pair_mem (h, a->var); |
| 215 | if (hp == 0) |
| 216 | { |
| 217 | local_set_variable_nolock (client, a->nspace, (u8 *) a->var, |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 218 | placeholder_value, a->elsize); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 219 | /* might have moved */ |
| 220 | h = shm->namespaces[a->nspace]; |
| 221 | hp = hash_get_pair_mem (h, a->var); |
| 222 | ASSERT (hp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 224 | |
| 225 | value = pool_elt_at_index (shm->values, hp->value[0]); |
| 226 | |
| 227 | for (i = 0; i < vec_len (value->notifications); i++) |
| 228 | { |
| 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)) |
| 233 | { |
| 234 | if (a->add_del == 0 /* delete */ ) |
| 235 | { |
| 236 | vec_delete (value->notifications, 1, i); |
| 237 | goto out; |
| 238 | } |
| 239 | else |
| 240 | { /* add */ |
| 241 | clib_warning |
| 242 | ("%s: ignore dup reg pid %d signum %d action %d opaque %x", |
| 243 | a->var, client->pid, a->signum, a->action, a->opaque); |
| 244 | rv = -2; |
| 245 | goto out; |
| 246 | } |
| 247 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 249 | if (a->add_del == 0) |
| 250 | { |
| 251 | rv = -3; |
| 252 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 254 | |
| 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
| 261 | out: |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 262 | svm_pop_heap (oldheap); |
| 263 | region_unlock (client->db_rp); |
| 264 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 268 | static void |
| 269 | local_unset_variable_nolock (svmdb_client_t * client, |
| 270 | svmdb_namespace_t namespace, char *var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 272 | uword *h; |
| 273 | svmdb_value_t *oldvalue; |
| 274 | hash_pair_t *hp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 276 | h = client->shm->namespaces[namespace]; |
| 277 | hp = hash_get_pair_mem (h, var); |
| 278 | if (hp) |
| 279 | { |
| 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 */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 284 | vec_set_len (oldvalue->value, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 286 | client->shm->namespaces[namespace] = h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 289 | void |
| 290 | svmdb_local_unset_string_variable (svmdb_client_t * client, char *var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 292 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 301 | static void |
| 302 | local_set_variable_nolock (svmdb_client_t * client, |
| 303 | svmdb_namespace_t namespace, |
| 304 | u8 * var, u8 * val, u32 elsize) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 306 | uword *h; |
| 307 | hash_pair_t *hp; |
| 308 | u8 *name; |
| 309 | svmdb_shm_hdr_t *shm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 311 | shm = client->shm; |
| 312 | h = shm->namespaces[namespace]; |
| 313 | hp = hash_get_pair_mem (h, var); |
| 314 | if (hp) |
| 315 | { |
| 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); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 320 | vec_set_len (oldvalue->value, vec_len (val)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 321 | notify_value (oldvalue, SVMDB_ACTION_SET); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 323 | else |
| 324 | { |
| 325 | svmdb_value_t *newvalue; |
| 326 | pool_get (shm->values, newvalue); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 327 | clib_memset (newvalue, 0, sizeof (*newvalue)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 328 | newvalue->elsize = elsize; |
| 329 | vec_alloc (newvalue->value, vec_len (val) * elsize); |
| 330 | clib_memcpy (newvalue->value, val, vec_len (val) * elsize); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 331 | vec_set_len (newvalue->value, vec_len (val)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 332 | name = format (0, "%s%c", var, 0); |
| 333 | hash_set_mem (h, name, newvalue - shm->values); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 335 | shm->namespaces[namespace] = h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 338 | void |
| 339 | svmdb_local_set_string_variable (svmdb_client_t * client, |
| 340 | char *var, char *val) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 342 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 344 | region_lock (client->db_rp, 12); |
| 345 | oldheap = svm_push_data_heap (client->db_rp); |
| 346 | |
| 347 | local_unset_variable_nolock (client, SVMDB_NAMESPACE_STRING, var); |
| 348 | |
| 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 355 | static u8 * |
| 356 | local_get_variable_nolock (svmdb_client_t * client, |
| 357 | svmdb_namespace_t namespace, u8 * var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 359 | uword *h; |
| 360 | uword *p; |
| 361 | svmdb_shm_hdr_t *shm; |
| 362 | svmdb_value_t *oldvalue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 364 | shm = client->shm; |
| 365 | h = shm->namespaces[namespace]; |
| 366 | p = hash_get_mem (h, var); |
| 367 | if (p) |
| 368 | { |
| 369 | oldvalue = pool_elt_at_index (shm->values, p[0]); |
| 370 | notify_value (oldvalue, SVMDB_ACTION_GET); |
| 371 | return (oldvalue->value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 373 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 376 | void * |
| 377 | svmdb_local_get_variable_reference (svmdb_client_t * client, |
| 378 | svmdb_namespace_t namespace, char *var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 380 | u8 *rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 382 | region_lock (client->db_rp, 19); |
| 383 | rv = local_get_variable_nolock (client, namespace, (u8 *) var); |
| 384 | region_unlock (client->db_rp); |
| 385 | return (void *) rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 388 | char * |
| 389 | svmdb_local_get_string_variable (svmdb_client_t * client, char *var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 391 | u8 *rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 393 | region_lock (client->db_rp, 13); |
| 394 | rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_STRING, (u8 *) var); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 396 | if (rv && vec_len (rv)) |
| 397 | { |
| 398 | rv = format (0, "%s", rv); |
| 399 | vec_add1 (rv, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 401 | region_unlock (client->db_rp); |
| 402 | return ((char *) rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 405 | void |
| 406 | svmdb_local_dump_strings (svmdb_client_t * client) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 408 | uword *h; |
| 409 | u8 *key; |
| 410 | u32 value; |
| 411 | svmdb_shm_hdr_t *shm = client->shm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 413 | region_lock (client->db_rp, 14); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 415 | h = client->shm->namespaces[SVMDB_NAMESPACE_STRING]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 417 | hash_foreach_mem(key, value, h, |
| 418 | ({ |
| 419 | svmdb_value_t *v = pool_elt_at_index (shm->values, value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 421 | fformat(stdout, "%s: %s\n", key, |
| 422 | vec_len(v->value) ? v->value : (u8 *)"(nil)"); |
| 423 | })); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 424 | region_unlock (client->db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 427 | int |
| 428 | svmdb_local_serialize_strings (svmdb_client_t * client, char *filename) |
| 429 | { |
| 430 | uword *h; |
| 431 | u8 *key; |
| 432 | u32 value; |
| 433 | svmdb_shm_hdr_t *shm = client->shm; |
Andrew Yourtchenko | 3ebe1ef | 2022-08-17 13:44:16 +0000 | [diff] [blame] | 434 | serialize_main_t _sm = { 0 }, *sm = &_sm; |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 435 | clib_error_t *error = 0; |
| 436 | u8 *sanitized_name = 0; |
| 437 | int fd = 0; |
| 438 | |
Matus Fabian | 7f163b6 | 2024-07-22 09:56:01 +0200 | [diff] [blame^] | 439 | if (strstr (filename, "..") || strchr (filename, '/')) |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 440 | { |
| 441 | error = clib_error_return (0, "Illegal characters in filename '%s'", |
| 442 | filename); |
| 443 | goto out; |
| 444 | } |
| 445 | |
| 446 | sanitized_name = format (0, "/tmp/%s%c", filename, 0); |
| 447 | |
| 448 | fd = creat ((char *) sanitized_name, 0644); |
| 449 | |
| 450 | if (fd < 0) |
| 451 | { |
| 452 | error = clib_error_return_unix (0, "Create '%s'", sanitized_name); |
| 453 | goto out; |
| 454 | } |
| 455 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 456 | serialize_open_clib_file_descriptor (sm, fd); |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 457 | |
| 458 | region_lock (client->db_rp, 20); |
| 459 | |
| 460 | h = client->shm->namespaces[SVMDB_NAMESPACE_STRING]; |
| 461 | |
| 462 | serialize_likely_small_unsigned_integer (sm, hash_elts (h)); |
| 463 | |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 464 | hash_foreach_mem(key, value, h, |
| 465 | ({ |
| 466 | svmdb_value_t *v = pool_elt_at_index (shm->values, value); |
| 467 | |
| 468 | /* Omit names with nil values */ |
| 469 | if (vec_len(v->value)) |
| 470 | { |
| 471 | serialize_cstring (sm, (char *)key); |
| 472 | serialize_cstring (sm, (char *)v->value); |
| 473 | } |
| 474 | })); |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 475 | region_unlock (client->db_rp); |
| 476 | |
| 477 | serialize_close (sm); |
| 478 | |
| 479 | out: |
| 480 | if (fd > 0 && close (fd) < 0) |
| 481 | error = clib_error_return_unix (0, "close fd %d", fd); |
| 482 | |
| 483 | if (error) |
| 484 | { |
| 485 | clib_error_report (error); |
| 486 | return -1; |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | int |
| 492 | svmdb_local_unserialize_strings (svmdb_client_t * client, char *filename) |
| 493 | { |
Andrew Yourtchenko | 3ebe1ef | 2022-08-17 13:44:16 +0000 | [diff] [blame] | 494 | serialize_main_t _sm = { 0 }, *sm = &_sm; |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 495 | void *oldheap; |
| 496 | clib_error_t *error = 0; |
| 497 | u8 *key, *value; |
| 498 | int fd = 0; |
| 499 | u32 nelts; |
| 500 | int i; |
| 501 | |
| 502 | fd = open (filename, O_RDONLY); |
| 503 | |
| 504 | if (fd < 0) |
| 505 | { |
| 506 | error = clib_error_return_unix (0, "Failed to open '%s'", filename); |
| 507 | goto out; |
| 508 | } |
| 509 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 510 | unserialize_open_clib_file_descriptor (sm, fd); |
Dave Barach | 23a7412 | 2016-11-24 16:34:20 -0500 | [diff] [blame] | 511 | |
| 512 | region_lock (client->db_rp, 21); |
| 513 | oldheap = svm_push_data_heap (client->db_rp); |
| 514 | |
| 515 | nelts = unserialize_likely_small_unsigned_integer (sm); |
| 516 | |
| 517 | for (i = 0; i < nelts; i++) |
| 518 | { |
| 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 */ ); |
| 523 | vec_free (key); |
| 524 | vec_free (value); |
| 525 | } |
| 526 | svm_pop_heap (oldheap); |
| 527 | region_unlock (client->db_rp); |
| 528 | |
| 529 | serialize_close (sm); |
| 530 | |
| 531 | out: |
| 532 | if (fd > 0 && close (fd) < 0) |
| 533 | error = clib_error_return_unix (0, "close fd %d", fd); |
| 534 | |
| 535 | if (error) |
| 536 | { |
| 537 | clib_error_report (error); |
| 538 | return -1; |
| 539 | } |
| 540 | return 0; |
| 541 | } |
| 542 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 543 | void |
| 544 | svmdb_local_unset_vec_variable (svmdb_client_t * client, char *var) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 546 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
| 553 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 554 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 555 | void |
| 556 | svmdb_local_set_vec_variable (svmdb_client_t * client, |
| 557 | char *var, void *val_arg, u32 elsize) |
| 558 | { |
| 559 | u8 *val = (u8 *) val_arg; |
| 560 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 562 | region_lock (client->db_rp, 16); |
| 563 | oldheap = svm_push_data_heap (client->db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 565 | local_unset_variable_nolock (client, SVMDB_NAMESPACE_VEC, var); |
| 566 | local_set_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var, |
| 567 | val, elsize); |
| 568 | |
| 569 | svm_pop_heap (oldheap); |
| 570 | region_unlock (client->db_rp); |
| 571 | } |
| 572 | |
| 573 | void * |
| 574 | svmdb_local_get_vec_variable (svmdb_client_t * client, char *var, u32 elsize) |
| 575 | { |
| 576 | u8 *rv = 0; |
| 577 | u8 *copy = 0; |
| 578 | |
| 579 | region_lock (client->db_rp, 17); |
| 580 | |
| 581 | rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var); |
| 582 | |
| 583 | if (rv && vec_len (rv)) |
| 584 | { |
| 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); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 588 | vec_set_len (copy, vec_len (rv)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 589 | region_unlock (client->db_rp); |
| 590 | return (copy); |
| 591 | } |
| 592 | region_unlock (client->db_rp); |
| 593 | return (0); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | svmdb_local_dump_vecs (svmdb_client_t * client) |
| 598 | { |
| 599 | uword *h; |
| 600 | u8 *key; |
| 601 | u32 value; |
| 602 | svmdb_shm_hdr_t *shm; |
| 603 | |
| 604 | region_lock (client->db_rp, 17); |
| 605 | shm = client->shm; |
| 606 | |
| 607 | h = client->shm->namespaces[SVMDB_NAMESPACE_VEC]; |
| 608 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 609 | hash_foreach_mem(key, value, h, |
| 610 | ({ |
| 611 | svmdb_value_t *v = pool_elt_at_index (shm->values, value); |
| 612 | (void) fformat(stdout, "%s:\n %U (%.2f)\n", key, |
| 613 | format_hex_bytes, v->value, |
| 614 | vec_len(v->value)*v->elsize, ((f64 *)(v->value))[0]); |
| 615 | })); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 616 | |
| 617 | region_unlock (client->db_rp); |
| 618 | } |
| 619 | |
| 620 | void * |
| 621 | svmdb_local_find_or_add_vec_variable (svmdb_client_t * client, |
| 622 | char *var, u32 nbytes) |
| 623 | { |
| 624 | void *oldheap; |
| 625 | u8 *rv = 0; |
| 626 | |
| 627 | region_lock (client->db_rp, 18); |
| 628 | oldheap = svm_push_data_heap (client->db_rp); |
| 629 | |
| 630 | rv = local_get_variable_nolock (client, SVMDB_NAMESPACE_VEC, (u8 *) var); |
| 631 | |
| 632 | if (rv) |
| 633 | { |
| 634 | goto out; |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | uword *h; |
| 639 | u8 *name; |
| 640 | svmdb_shm_hdr_t *shm; |
| 641 | svmdb_value_t *newvalue; |
| 642 | |
| 643 | shm = client->shm; |
| 644 | h = shm->namespaces[SVMDB_NAMESPACE_VEC]; |
| 645 | |
| 646 | pool_get (shm->values, newvalue); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 647 | clib_memset (newvalue, 0, sizeof (*newvalue)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 648 | newvalue->elsize = 1; |
| 649 | vec_alloc (newvalue->value, nbytes); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 650 | vec_set_len (newvalue->value, nbytes); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 651 | name = format (0, "%s%c", var, 0); |
| 652 | hash_set_mem (h, name, newvalue - shm->values); |
| 653 | shm->namespaces[SVMDB_NAMESPACE_VEC] = h; |
| 654 | rv = newvalue->value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | out: |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 658 | svm_pop_heap (oldheap); |
| 659 | region_unlock (client->db_rp); |
| 660 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * fd.io coding-style-patch-verification: ON |
| 665 | * |
| 666 | * Local Variables: |
| 667 | * eval: (c-set-style "gnu") |
| 668 | * End: |
| 669 | */ |