cf475c4d1ba61e38b6d471a7e127d63461eff5bf
[vpp.git] / svm / svmdbtool.c
1 /*
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
41 static void
42 get_string (char *chroot_path, u8 * vbl)
43 {
44   svmdb_client_t *c;
45   char *rv;
46
47   c = svmdb_map_chroot (chroot_path);
48
49   rv = svmdb_local_get_string_variable (c, (char *) vbl);
50
51   fformat (stdout, "%s\n", rv ? rv : "UNSET");
52   vec_free (rv);
53   svmdb_unmap (c);
54 }
55
56 static void
57 set_string (char *chroot_path, u8 * vbl, u8 * value)
58 {
59   svmdb_client_t *c;
60
61   c = svmdb_map_chroot (chroot_path);
62   svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
63   svmdb_unmap (c);
64 }
65
66 static void
67 unset_string (char *chroot_path, u8 * vbl)
68 {
69   svmdb_client_t *c;
70
71   c = svmdb_map_chroot (chroot_path);
72   svmdb_local_unset_string_variable (c, (char *) vbl);
73   svmdb_unmap (c);
74 }
75
76 static void
77 dump_strings (char *chroot_path)
78 {
79   svmdb_client_t *c;
80
81   c = svmdb_map_chroot (chroot_path);
82   svmdb_local_dump_strings (c);
83   svmdb_unmap (c);
84 }
85
86 static void
87 test_vlib_vec_rate (char *chroot_path, f64 vr)
88 {
89   svmdb_client_t *c;
90   f64 *tv = 0;
91
92   vec_add1 (tv, vr);
93
94   c = svmdb_map_chroot (chroot_path);
95
96   svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
97                                 sizeof (*tv));
98   svmdb_unmap (c);
99
100   vec_free (tv);
101 }
102
103
104
105 static void
106 test_vec (char *chroot_path, u8 * vbl)
107 {
108   svmdb_client_t *c;
109   u64 *tv = 0;
110   int i;
111
112   /* my amp goes to 11 */
113   for (i = 0; i < 11; i++)
114     {
115       vec_add1 (tv, i);
116     }
117
118   c = svmdb_map_chroot (chroot_path);
119   svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
120   svmdb_unmap (c);
121
122   vec_free (tv);
123 }
124
125 static void
126 fake_install (char *chroot_path, u8 * add_value)
127 {
128   svmdb_client_t *c;
129   u8 *v = 0;
130   u8 **values = 0;
131   u8 *oldvalue;
132   u8 *value;
133   int nitems = 0, i;
134   serialize_main_t m;
135
136   c = svmdb_map_chroot (chroot_path);
137
138   oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
139   if (oldvalue)
140     {
141       unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
142       nitems = unserialize_likely_small_unsigned_integer (&m);
143       for (i = 0; i < nitems; i++)
144         {
145           unserialize_cstring (&m, (char **) &value);
146           vec_add1 (values, value);
147         }
148       vec_free (v);
149     }
150   nitems++;
151   value = format (0, "%s%c", add_value, 0);
152
153   vec_add1 (values, value);
154
155   fformat (stdout, "Resulting installed_sw vector:\n");
156
157   serialize_open_vector (&m, v);
158   serialize_likely_small_unsigned_integer (&m, vec_len (values));
159   for (i = 0; i < vec_len (values); i++)
160     {
161       fformat (stdout, "%s\n", values[i]);
162       serialize_cstring (&m, (char *) values[i]);
163     }
164
165   v = serialize_close_vector (&m);
166
167   svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
168   svmdb_unmap (c);
169
170   for (i = 0; i < vec_len (values); i++)
171     vec_free (values[i]);
172   vec_free (values);
173 }
174
175 static void
176 sigaction_handler (int signum, siginfo_t * i, void *notused)
177 {
178   u32 action, opaque;
179
180   action = (u32) (uword) i->si_ptr;
181   action >>= 28;
182   opaque = (u32) (uword) i->si_ptr;
183   opaque &= ~(0xF0000000);
184
185   clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
186 }
187
188 static void
189 test_reg (char *chroot_path, u8 * vbl)
190 {
191   svmdb_client_t *c;
192   svmdb_notification_args_t args;
193   svmdb_notification_args_t *a = &args;
194   struct sigaction sa;
195
196   memset (&sa, 0, sizeof (sa));
197   sa.sa_sigaction = sigaction_handler;
198   sa.sa_flags = SA_SIGINFO;
199   if (sigaction (SIGUSR2, &sa, 0) < 0)
200     {
201       clib_unix_warning ("sigaction");
202       return;
203     }
204
205   memset (a, 0, sizeof (*a));
206
207   c = svmdb_map_chroot (chroot_path);
208
209   a->add_del = 1 /* add */ ;
210   a->nspace = SVMDB_NAMESPACE_STRING;
211   a->var = (char *) vbl;
212   a->elsize = 1;
213   a->signum = SIGUSR2;
214   a->action = SVMDB_ACTION_GET;
215   a->opaque = 0x0eadbeef;
216
217   svmdb_local_add_del_notification (c, a);
218
219   (void) svmdb_local_get_string_variable (c, (char *) vbl);
220
221   a->add_del = 0;               /* del */
222   svmdb_local_add_del_notification (c, a);
223
224
225
226   svmdb_unmap (c);
227 }
228
229 static void
230 unset_vec (char *chroot_path, u8 * vbl)
231 {
232   svmdb_client_t *c;
233
234   c = svmdb_map_chroot (chroot_path);
235   svmdb_local_unset_vec_variable (c, (char *) vbl);
236   svmdb_unmap (c);
237 }
238
239 static void
240 dump_vecs (char *chroot_path)
241 {
242   svmdb_client_t *c;
243
244   c = svmdb_map_chroot (chroot_path);
245   svmdb_local_dump_vecs (c);
246   svmdb_unmap (c);
247 }
248
249 static void
250 crash_test (char *chroot_path)
251 {
252   svmdb_client_t *c;
253
254   c = svmdb_map_chroot (chroot_path);
255
256   clib_warning ("Grab region mutex and crash deliberately!");
257   c->db_rp->mutex_owner_pid = getpid ();
258   c->db_rp->mutex_owner_tag = -13;
259   pthread_mutex_lock (&c->db_rp->mutex);
260
261   abort ();
262 }
263
264 static void
265 map_with_size (char *chroot_path, uword size)
266 {
267   svmdb_client_t *c;
268   c = svmdb_map_chroot_size (chroot_path, size);
269   svmdb_unmap (c);
270 }
271
272 int
273 main (int argc, char **argv)
274 {
275   unformat_input_t input;
276   int parsed = 0;
277   u8 *vbl = 0, *value = 0;
278   char *chroot_path = 0;
279   u8 *chroot_path_u8;
280   uword size;
281   f64 vr;
282
283   unformat_init_command_line (&input, argv);
284
285   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
286     {
287       if (unformat (&input, "get-string %s", &vbl))
288         {
289           get_string (chroot_path, vbl);
290           vec_free (vbl);
291           parsed++;
292         }
293       else if (unformat (&input, "set-string %s %s", &vbl, &value))
294         {
295           set_string (chroot_path, vbl, value);
296           vec_free (vbl);
297           vec_free (value);
298           parsed++;
299         }
300       else if (unformat (&input, "unset-string %s", &vbl))
301         {
302           unset_string (chroot_path, vbl);
303           vec_free (vbl);
304           parsed++;
305         }
306       else if (unformat (&input, "dump-strings"))
307         {
308           dump_strings (chroot_path);
309           parsed++;
310         }
311       else if (unformat (&input, "unset-vec %s", &vbl))
312         {
313           unset_vec (chroot_path, vbl);
314           vec_free (vbl);
315           parsed++;
316         }
317       else if (unformat (&input, "dump-vecs"))
318         {
319           dump_vecs (chroot_path);
320           parsed++;
321         }
322       else if (unformat (&input, "test-vec %s", &vbl))
323         {
324           test_vec (chroot_path, vbl);
325           // vec_free(vbl);
326           parsed++;
327         }
328       else if (unformat (&input, "vlib-vec-rate %f", &vr))
329         {
330           test_vlib_vec_rate (chroot_path, vr);
331           parsed++;
332         }
333       else if (unformat (&input, "test-reg %s", &vbl))
334         {
335           test_reg (chroot_path, vbl);
336           parsed++;
337         }
338       else if (unformat (&input, "crash-test"))
339         {
340           crash_test (chroot_path);
341         }
342       else if (unformat (&input, "chroot %s", &chroot_path_u8))
343         {
344           chroot_path = (char *) chroot_path_u8;
345         }
346       else if (unformat (&input, "fake-install %s", &value))
347         {
348           fake_install (chroot_path, value);
349           parsed++;
350         }
351       else if (unformat (&input, "size %d", &size))
352         {
353           map_with_size (chroot_path, size);
354           parsed++;
355         }
356       else
357         {
358           break;
359         }
360     }
361
362   unformat_free (&input);
363
364   if (!parsed)
365     {
366       fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
367                argv[0]);
368       fformat (stdout, "      unset-string <name> | dump-strings\n");
369       fformat (stdout, "      test-vec <name> |\n");
370       fformat (stdout, "      unset-vec <name> | dump-vecs\n");
371     }
372
373   exit (0);
374 }
375
376 /*
377  * fd.io coding-style-patch-verification: ON
378  *
379  * Local Variables:
380  * eval: (c-set-style "gnu")
381  * End:
382  */