blob: c2efbc52a34becdabe1ee0282962173c0da05f84 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <netinet/in.h>
22#include <signal.h>
23#include <pthread.h>
24#include <unistd.h>
25#include <time.h>
26#include <fcntl.h>
27#include <string.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>
39#include "svmdb.h"
40
41static void get_string (char *chroot_path, u8 *vbl)
42{
43 svmdb_client_t *c;
44 char *rv;
45
46 c = svmdb_map_chroot(chroot_path);
47
48 rv = svmdb_local_get_string_variable (c, (char *)vbl);
49
50 fformat(stdout, "%s\n", rv ? rv : "UNSET");
51 vec_free(rv);
52 svmdb_unmap (c);
53}
54
55static void set_string (char *chroot_path, u8 *vbl, u8 *value)
56{
57 svmdb_client_t *c;
58
59 c = svmdb_map_chroot(chroot_path);
60 svmdb_local_set_string_variable (c, (char *)vbl, (char *)value);
61 svmdb_unmap (c);
62}
63
64static void unset_string (char *chroot_path, u8 *vbl)
65{
66 svmdb_client_t *c;
67
68 c = svmdb_map_chroot(chroot_path);
69 svmdb_local_unset_string_variable (c, (char *)vbl);
70 svmdb_unmap (c);
71}
72
73static void dump_strings(char *chroot_path)
74{
75 svmdb_client_t *c;
76
77 c = svmdb_map_chroot(chroot_path);
78 svmdb_local_dump_strings (c);
79 svmdb_unmap (c);
80}
81
82static void test_vlib_vec_rate (char *chroot_path, f64 vr)
83{
84 svmdb_client_t *c;
85 f64 *tv = 0;
86
87 vec_add1 (tv, vr);
88
89 c = svmdb_map_chroot(chroot_path);
90
91 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *)tv, sizeof (*tv));
92 svmdb_unmap (c);
93
94 vec_free(tv);
95}
96
97
98
99static void test_vec (char *chroot_path, u8 *vbl)
100{
101 svmdb_client_t *c;
102 u64 *tv = 0;
103 int i;
104
105 /* my amp goes to 11 */
106 for (i = 0; i < 11; i++) {
107 vec_add1(tv, i);
108 }
109
110 c = svmdb_map_chroot(chroot_path);
111 svmdb_local_set_vec_variable (c, (char *)vbl, (char *)tv, sizeof (tv[0]));
112 svmdb_unmap (c);
113
114 vec_free(tv);
115}
116
117static void fake_install (char *chroot_path, u8 *add_value)
118{
119 svmdb_client_t *c;
120 u8 *v = 0;
121 u8 **values = 0;
122 u8 *oldvalue;
123 u8 *value;
124 int nitems = 0, i;
125 serialize_main_t m;
126
127 c = svmdb_map_chroot(chroot_path);
128
129 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
130 if (oldvalue) {
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);
136 }
137 vec_free (v);
138 }
139 nitems++;
140 value = format (0, "%s%c", add_value, 0);
141
142 vec_add1 (values, value);
143
144 fformat (stdout, "Resulting installed_sw vector:\n");
145
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]);
151 }
152
153 v = serialize_close_vector (&m);
154
155 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
156 svmdb_unmap (c);
157
158 for (i = 0; i < vec_len (values); i++)
159 vec_free (values[i]);
160 vec_free (values);
161}
162
163static void sigaction_handler (int signum, siginfo_t *i, void *notused)
164{
165 u32 action, opaque;
166
167 action = (u32)(uword) i->si_ptr;
168 action >>= 28;
169 opaque = (u32)(uword) i->si_ptr;
170 opaque &= ~(0xF0000000);
171
172 clib_warning ("signal %d, action %d, opaque %x",
173 signum, action, opaque);
174}
175
176static void test_reg (char *chroot_path, u8 *vbl)
177{
178 svmdb_client_t *c;
179 svmdb_notification_args_t args;
180 svmdb_notification_args_t *a = &args;
181 struct sigaction sa;
182
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");
188 return;
189 }
190
191 memset (a, 0, sizeof (*a));
192
193 c = svmdb_map_chroot(chroot_path);
194
195 a->add_del = 1 /* add */;
196 a->nspace = SVMDB_NAMESPACE_STRING;
197 a->var = (char *) vbl;
198 a->elsize = 1;
199 a->signum = SIGUSR2;
200 a->action = SVMDB_ACTION_GET;
201 a->opaque = 0x0eadbeef;
202
203 svmdb_local_add_del_notification (c, a);
204
205 (void) svmdb_local_get_string_variable (c, (char *)vbl);
206
207 a->add_del = 0; /* del */
208 svmdb_local_add_del_notification (c, a);
209
210
211
212 svmdb_unmap (c);
213}
214
215static void unset_vec (char *chroot_path, u8 *vbl)
216{
217 svmdb_client_t *c;
218
219 c = svmdb_map_chroot(chroot_path);
220 svmdb_local_unset_vec_variable (c, (char *)vbl);
221 svmdb_unmap (c);
222}
223
224static void dump_vecs(char *chroot_path)
225{
226 svmdb_client_t *c;
227
228 c = svmdb_map_chroot(chroot_path);
229 svmdb_local_dump_vecs (c);
230 svmdb_unmap (c);
231}
232
233static void crash_test(char *chroot_path)
234{
235 svmdb_client_t *c;
236
237 c = svmdb_map_chroot(chroot_path);
238
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);
243
244 abort();
245}
246
247static void map_with_size (char *chroot_path, uword size)
248{
249 svmdb_client_t *c;
250 c = svmdb_map_chroot_size (chroot_path, size);
251 svmdb_unmap (c);
252}
253
254int main (int argc, char **argv)
255{
256 unformat_input_t input;
257 int parsed =0;
258 u8 *vbl=0, *value=0;
259 char *chroot_path = 0;
260 u8 *chroot_path_u8;
261 uword size;
262 f64 vr;
263
264 unformat_init_command_line (&input, argv);
265
266 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) {
267 if (unformat (&input, "get-string %s", &vbl)) {
268 get_string (chroot_path, vbl);
269 vec_free(vbl);
270 parsed++;
271 } else if (unformat (&input, "set-string %s %s", &vbl, &value)) {
272 set_string (chroot_path, vbl, value);
273 vec_free(vbl);
274 vec_free(value);
275 parsed++;
276 } else if (unformat(&input, "unset-string %s", &vbl)) {
277 unset_string (chroot_path, vbl);
278 vec_free(vbl);
279 parsed++;
280 } else if (unformat(&input, "dump-strings")) {
281 dump_strings(chroot_path);
282 parsed++;
283 } else if (unformat(&input, "unset-vec %s", &vbl)) {
284 unset_vec (chroot_path, vbl);
285 vec_free(vbl);
286 parsed++;
287 } else if (unformat(&input, "dump-vecs")) {
288 dump_vecs(chroot_path);
289 parsed++;
290 } else if (unformat (&input, "test-vec %s", &vbl)) {
291 test_vec(chroot_path, vbl);
292 // vec_free(vbl);
293 parsed++;
294 } else if (unformat (&input, "vlib-vec-rate %f", &vr)) {
295 test_vlib_vec_rate(chroot_path, vr);
296 parsed++;
297 } else if (unformat (&input, "test-reg %s", &vbl)) {
298 test_reg(chroot_path, vbl);
299 parsed++;
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);
306 parsed++;
307 } else if (unformat (&input, "size %d", &size)) {
308 map_with_size (chroot_path, size);
309 parsed++;
310 } else {
311 break;
312 }
313 }
314
315 unformat_free (&input);
316
317 if (!parsed) {
318 fformat(stdout, "%s: get-string <name> | set-string <name> <value>\n",
319 argv[0]);
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");
323 }
324
325 exit (0);
326}