2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include <vppinfra/clib.h>
31 #include <vppinfra/vec.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/bitmap.h>
34 #include <vppinfra/fifo.h>
35 #include <vppinfra/time.h>
36 #include <vppinfra/heap.h>
37 #include <vppinfra/pool.h>
38 #include <vppinfra/format.h>
39 #include <vppinfra/serialize.h>
44 svmdb_map_args_t map_args;
49 svmdbtool_main_t svmdbtool_main;
51 static inline svmdb_map_args_t *
52 map_arg_setup (char *chroot_path)
54 svmdbtool_main_t *sm = &svmdbtool_main;
55 svmdb_map_args_t *ma = &sm->map_args;
57 clib_memset (ma, 0, sizeof (*ma));
58 ma->root_path = chroot_path;
66 get_string (char *chroot_path, u8 * vbl)
72 ma = map_arg_setup (chroot_path);
76 rv = svmdb_local_get_string_variable (c, (char *) vbl);
78 fformat (stdout, "%s\n", rv ? rv : "UNSET");
84 set_string (char *chroot_path, u8 * vbl, u8 * value)
89 ma = map_arg_setup (chroot_path);
92 svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
97 unset_string (char *chroot_path, u8 * vbl)
100 svmdb_map_args_t *ma;
102 ma = map_arg_setup (chroot_path);
105 svmdb_local_unset_string_variable (c, (char *) vbl);
110 dump_strings (char *chroot_path)
113 svmdb_map_args_t *ma;
115 ma = map_arg_setup (chroot_path);
118 svmdb_local_dump_strings (c);
123 serialize_strings (char *chroot_path, char *filename)
126 svmdb_map_args_t *ma;
128 ma = map_arg_setup (chroot_path);
131 (void) svmdb_local_serialize_strings (c, filename);
136 unserialize_strings (char *chroot_path, char *filename)
139 svmdb_map_args_t *ma;
141 ma = map_arg_setup (chroot_path);
144 (void) svmdb_local_unserialize_strings (c, filename);
149 test_vlib_vec_rate (char *chroot_path, f64 vr)
153 svmdb_map_args_t *ma;
155 ma = map_arg_setup (chroot_path);
161 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
171 test_vec (char *chroot_path, u8 * vbl)
176 svmdb_map_args_t *ma;
178 ma = map_arg_setup (chroot_path);
182 /* my amp goes to 11 */
183 for (i = 0; i < 11; i++)
188 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
195 fake_install (char *chroot_path, u8 * add_value)
204 svmdb_map_args_t *ma;
206 ma = map_arg_setup (chroot_path);
210 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
213 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
214 nitems = unserialize_likely_small_unsigned_integer (&m);
215 for (i = 0; i < nitems; i++)
217 unserialize_cstring (&m, (char **) &value);
218 vec_add1 (values, value);
223 value = format (0, "%s%c", add_value, 0);
225 vec_add1 (values, value);
227 fformat (stdout, "Resulting installed_sw vector:\n");
229 serialize_open_vector (&m, v);
230 serialize_likely_small_unsigned_integer (&m, vec_len (values));
231 for (i = 0; i < vec_len (values); i++)
233 fformat (stdout, "%s\n", values[i]);
234 serialize_cstring (&m, (char *) values[i]);
237 v = serialize_close_vector (&m);
239 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
242 for (i = 0; i < vec_len (values); i++)
243 vec_free (values[i]);
248 sigaction_handler (int signum, siginfo_t * i, void *notused)
252 action = (u32) (uword) i->si_ptr;
254 opaque = (u32) (uword) i->si_ptr;
255 opaque &= ~(0xF0000000);
257 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
261 test_reg (char *chroot_path, u8 * vbl)
264 svmdb_notification_args_t args;
265 svmdb_notification_args_t *a = &args;
267 svmdb_map_args_t *ma;
269 ma = map_arg_setup (chroot_path);
271 clib_memset (&sa, 0, sizeof (sa));
272 sa.sa_sigaction = sigaction_handler;
273 sa.sa_flags = SA_SIGINFO;
274 if (sigaction (SIGUSR2, &sa, 0) < 0)
276 clib_unix_warning ("sigaction");
280 clib_memset (a, 0, sizeof (*a));
284 a->add_del = 1 /* add */ ;
285 a->nspace = SVMDB_NAMESPACE_STRING;
286 a->var = (char *) vbl;
289 a->action = SVMDB_ACTION_GET;
290 a->opaque = 0x0eadbeef;
292 svmdb_local_add_del_notification (c, a);
294 (void) svmdb_local_get_string_variable (c, (char *) vbl);
296 a->add_del = 0; /* del */
297 svmdb_local_add_del_notification (c, a);
305 unset_vec (char *chroot_path, u8 * vbl)
308 svmdb_map_args_t *ma;
310 ma = map_arg_setup (chroot_path);
314 svmdb_local_unset_vec_variable (c, (char *) vbl);
319 dump_vecs (char *chroot_path)
322 svmdb_map_args_t *ma;
324 ma = map_arg_setup (chroot_path);
328 svmdb_local_dump_vecs (c);
333 crash_test (char *chroot_path)
336 svmdb_map_args_t *ma;
338 ma = map_arg_setup (chroot_path);
342 clib_warning ("Grab region mutex and crash deliberately!");
343 c->db_rp->mutex_owner_pid = getpid ();
344 c->db_rp->mutex_owner_tag = -13;
345 pthread_mutex_lock (&c->db_rp->mutex);
351 map_with_size (char *chroot_path, uword size)
354 svmdb_map_args_t *ma;
356 svmdbtool_main.size = size;
357 ma = map_arg_setup (chroot_path);
365 main (int argc, char **argv)
367 unformat_input_t input;
369 u8 *vbl = 0, *value = 0;
370 char *chroot_path = 0;
376 struct passwd _pw, *pw;
377 struct group _grp, *grp;
380 clib_mem_init_thread_safe (0, 128 << 20);
382 svmdbtool_main.uid = geteuid ();
383 svmdbtool_main.gid = getegid ();
385 unformat_init_command_line (&input, argv);
387 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
389 if (unformat (&input, "get-string %s", &vbl))
391 get_string (chroot_path, vbl);
395 else if (unformat (&input, "set-string %s %s", &vbl, &value))
397 set_string (chroot_path, vbl, value);
402 else if (unformat (&input, "unset-string %s", &vbl))
404 unset_string (chroot_path, vbl);
408 else if (unformat (&input, "dump-strings"))
410 dump_strings (chroot_path);
413 else if (unformat (&input, "unset-vec %s", &vbl))
415 unset_vec (chroot_path, vbl);
419 else if (unformat (&input, "dump-vecs"))
421 dump_vecs (chroot_path);
424 else if (unformat (&input, "test-vec %s", &vbl))
426 test_vec (chroot_path, vbl);
430 else if (unformat (&input, "vlib-vec-rate %f", &vr))
432 test_vlib_vec_rate (chroot_path, vr);
435 else if (unformat (&input, "test-reg %s", &vbl))
437 test_reg (chroot_path, vbl);
440 else if (unformat (&input, "crash-test"))
442 crash_test (chroot_path);
444 else if (unformat (&input, "chroot %s", &chroot_path_u8))
446 chroot_path = (char *) chroot_path_u8;
448 else if (unformat (&input, "fake-install %s", &value))
450 fake_install (chroot_path, value);
453 else if (unformat (&input, "size %d", &size))
455 map_with_size (chroot_path, size);
458 else if (unformat (&input, "uid %d", &uid))
459 svmdbtool_main.uid = uid;
460 else if (unformat (&input, "gid %d", &gid))
461 svmdbtool_main.gid = gid;
462 else if (unformat (&input, "uid %s", &s))
464 /* lookup the username */
466 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
469 fformat (stderr, "cannot fetch username %s", s);
474 fformat (stderr, "username %s does not exist", s);
478 svmdbtool_main.uid = pw->pw_uid;
480 else if (unformat (&input, "gid %s", &s))
482 /* lookup the group name */
484 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
487 fformat (stderr, "cannot fetch group %s", s);
492 fformat (stderr, "group %s does not exist", s);
496 svmdbtool_main.gid = grp->gr_gid;
498 else if (unformat (&input, "serialize-strings %s", &filename))
500 vec_add1 (filename, 0);
501 serialize_strings (chroot_path, (char *) filename);
504 else if (unformat (&input, "unserialize-strings %s", &filename))
506 vec_add1 (filename, 0);
507 unserialize_strings (chroot_path, (char *) filename);
516 unformat_free (&input);
520 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
522 fformat (stdout, " unset-string <name> | dump-strings\n");
523 fformat (stdout, " test-vec <name> |\n");
524 fformat (stdout, " unset-vec <name> | dump-vecs\n");
525 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
526 fformat (stdout, " [gid <nnn-or-group-name>]\n");
533 * fd.io coding-style-patch-verification: ON
536 * eval: (c-set-style "gnu")