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>
21 #include <netinet/in.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/fifo.h>
33 #include <vppinfra/time.h>
34 #include <vppinfra/mheap.h>
35 #include <vppinfra/heap.h>
36 #include <vppinfra/pool.h>
37 #include <vppinfra/format.h>
38 #include <vppinfra/serialize.h>
41 static void get_string (char *chroot_path, u8 *vbl)
46 c = svmdb_map_chroot(chroot_path);
48 rv = svmdb_local_get_string_variable (c, (char *)vbl);
50 fformat(stdout, "%s\n", rv ? rv : "UNSET");
55 static void set_string (char *chroot_path, u8 *vbl, u8 *value)
59 c = svmdb_map_chroot(chroot_path);
60 svmdb_local_set_string_variable (c, (char *)vbl, (char *)value);
64 static void unset_string (char *chroot_path, u8 *vbl)
68 c = svmdb_map_chroot(chroot_path);
69 svmdb_local_unset_string_variable (c, (char *)vbl);
73 static void dump_strings(char *chroot_path)
77 c = svmdb_map_chroot(chroot_path);
78 svmdb_local_dump_strings (c);
82 static void test_vlib_vec_rate (char *chroot_path, f64 vr)
89 c = svmdb_map_chroot(chroot_path);
91 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *)tv, sizeof (*tv));
99 static void test_vec (char *chroot_path, u8 *vbl)
105 /* my amp goes to 11 */
106 for (i = 0; i < 11; i++) {
110 c = svmdb_map_chroot(chroot_path);
111 svmdb_local_set_vec_variable (c, (char *)vbl, (char *)tv, sizeof (tv[0]));
117 static void fake_install (char *chroot_path, u8 *add_value)
127 c = svmdb_map_chroot(chroot_path);
129 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
131 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
132 nitems = unserialize_likely_small_unsigned_integer (&m);
133 for (i = 0; i < nitems; i++) {
134 unserialize_cstring (&m, (char **)&value);
135 vec_add1 (values, value);
140 value = format (0, "%s%c", add_value, 0);
142 vec_add1 (values, value);
144 fformat (stdout, "Resulting installed_sw vector:\n");
146 serialize_open_vector (&m, v);
147 serialize_likely_small_unsigned_integer (&m, vec_len (values));
148 for (i = 0; i < vec_len (values); i++) {
149 fformat (stdout, "%s\n", values[i]);
150 serialize_cstring (&m, (char *)values[i]);
153 v = serialize_close_vector (&m);
155 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
158 for (i = 0; i < vec_len (values); i++)
159 vec_free (values[i]);
163 static void sigaction_handler (int signum, siginfo_t *i, void *notused)
167 action = (u32)(uword) i->si_ptr;
169 opaque = (u32)(uword) i->si_ptr;
170 opaque &= ~(0xF0000000);
172 clib_warning ("signal %d, action %d, opaque %x",
173 signum, action, opaque);
176 static void test_reg (char *chroot_path, u8 *vbl)
179 svmdb_notification_args_t args;
180 svmdb_notification_args_t *a = &args;
183 memset (&sa, 0, sizeof (sa));
184 sa.sa_sigaction = sigaction_handler;
185 sa.sa_flags = SA_SIGINFO;
186 if (sigaction (SIGUSR2, &sa, 0) < 0) {
187 clib_unix_warning ("sigaction");
191 memset (a, 0, sizeof (*a));
193 c = svmdb_map_chroot(chroot_path);
195 a->add_del = 1 /* add */;
196 a->nspace = SVMDB_NAMESPACE_STRING;
197 a->var = (char *) vbl;
200 a->action = SVMDB_ACTION_GET;
201 a->opaque = 0x0eadbeef;
203 svmdb_local_add_del_notification (c, a);
205 (void) svmdb_local_get_string_variable (c, (char *)vbl);
207 a->add_del = 0; /* del */
208 svmdb_local_add_del_notification (c, a);
215 static void unset_vec (char *chroot_path, u8 *vbl)
219 c = svmdb_map_chroot(chroot_path);
220 svmdb_local_unset_vec_variable (c, (char *)vbl);
224 static void dump_vecs(char *chroot_path)
228 c = svmdb_map_chroot(chroot_path);
229 svmdb_local_dump_vecs (c);
233 static void crash_test(char *chroot_path)
237 c = svmdb_map_chroot(chroot_path);
239 clib_warning ("Grab region mutex and crash deliberately!");
240 c->db_rp->mutex_owner_pid = getpid();
241 c->db_rp->mutex_owner_tag = -13;
242 pthread_mutex_lock(&c->db_rp->mutex);
247 static void map_with_size (char *chroot_path, uword size)
250 c = svmdb_map_chroot_size (chroot_path, size);
254 int main (int argc, char **argv)
256 unformat_input_t input;
259 char *chroot_path = 0;
264 unformat_init_command_line (&input, argv);
266 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) {
267 if (unformat (&input, "get-string %s", &vbl)) {
268 get_string (chroot_path, vbl);
271 } else if (unformat (&input, "set-string %s %s", &vbl, &value)) {
272 set_string (chroot_path, vbl, value);
276 } else if (unformat(&input, "unset-string %s", &vbl)) {
277 unset_string (chroot_path, vbl);
280 } else if (unformat(&input, "dump-strings")) {
281 dump_strings(chroot_path);
283 } else if (unformat(&input, "unset-vec %s", &vbl)) {
284 unset_vec (chroot_path, vbl);
287 } else if (unformat(&input, "dump-vecs")) {
288 dump_vecs(chroot_path);
290 } else if (unformat (&input, "test-vec %s", &vbl)) {
291 test_vec(chroot_path, vbl);
294 } else if (unformat (&input, "vlib-vec-rate %f", &vr)) {
295 test_vlib_vec_rate(chroot_path, vr);
297 } else if (unformat (&input, "test-reg %s", &vbl)) {
298 test_reg(chroot_path, vbl);
300 } else if (unformat (&input, "crash-test")) {
301 crash_test (chroot_path);
302 } else if (unformat (&input, "chroot %s", &chroot_path_u8)) {
303 chroot_path = (char *) chroot_path_u8;
304 } else if (unformat (&input, "fake-install %s", &value)) {
305 fake_install (chroot_path, value);
307 } else if (unformat (&input, "size %d", &size)) {
308 map_with_size (chroot_path, size);
315 unformat_free (&input);
318 fformat(stdout, "%s: get-string <name> | set-string <name> <value>\n",
320 fformat(stdout, " unset-string <name> | dump-strings\n");
321 fformat(stdout, " test-vec <name> |\n");
322 fformat(stdout, " unset-vec <name> | dump-vecs\n");