nat: use correct data types for memory sizes
[vpp.git] / src / 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 <pwd.h>
22 #include <grp.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <string.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/mheap.h>
37 #include <vppinfra/heap.h>
38 #include <vppinfra/pool.h>
39 #include <vppinfra/format.h>
40 #include <vppinfra/serialize.h>
41 #include "svmdb.h"
42
43 typedef struct
44 {
45   svmdb_map_args_t map_args;
46   int uid, gid;
47   uword size;
48 } svmdbtool_main_t;
49
50 svmdbtool_main_t svmdbtool_main;
51
52 static inline svmdb_map_args_t *
53 map_arg_setup (char *chroot_path)
54 {
55   svmdbtool_main_t *sm = &svmdbtool_main;
56   svmdb_map_args_t *ma = &sm->map_args;
57
58   clib_memset (ma, 0, sizeof (*ma));
59   ma->root_path = chroot_path;
60   ma->size = sm->size;
61   ma->uid = sm->uid;
62   ma->gid = sm->gid;
63   return ma;
64 }
65
66 static void
67 get_string (char *chroot_path, u8 * vbl)
68 {
69   svmdb_client_t *c;
70   char *rv;
71   svmdb_map_args_t *ma;
72
73   ma = map_arg_setup (chroot_path);
74
75   c = svmdb_map (ma);
76
77   rv = svmdb_local_get_string_variable (c, (char *) vbl);
78
79   fformat (stdout, "%s\n", rv ? rv : "UNSET");
80   vec_free (rv);
81   svmdb_unmap (c);
82 }
83
84 static void
85 set_string (char *chroot_path, u8 * vbl, u8 * value)
86 {
87   svmdb_client_t *c;
88   svmdb_map_args_t *ma;
89
90   ma = map_arg_setup (chroot_path);
91
92   c = svmdb_map (ma);
93   svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
94   svmdb_unmap (c);
95 }
96
97 static void
98 unset_string (char *chroot_path, u8 * vbl)
99 {
100   svmdb_client_t *c;
101   svmdb_map_args_t *ma;
102
103   ma = map_arg_setup (chroot_path);
104
105   c = svmdb_map (ma);
106   svmdb_local_unset_string_variable (c, (char *) vbl);
107   svmdb_unmap (c);
108 }
109
110 static void
111 dump_strings (char *chroot_path)
112 {
113   svmdb_client_t *c;
114   svmdb_map_args_t *ma;
115
116   ma = map_arg_setup (chroot_path);
117
118   c = svmdb_map (ma);
119   svmdb_local_dump_strings (c);
120   svmdb_unmap (c);
121 }
122
123 static void
124 serialize_strings (char *chroot_path, char *filename)
125 {
126   svmdb_client_t *c;
127   svmdb_map_args_t *ma;
128
129   ma = map_arg_setup (chroot_path);
130
131   c = svmdb_map (ma);
132   (void) svmdb_local_serialize_strings (c, filename);
133   svmdb_unmap (c);
134 }
135
136 static void
137 unserialize_strings (char *chroot_path, char *filename)
138 {
139   svmdb_client_t *c;
140   svmdb_map_args_t *ma;
141
142   ma = map_arg_setup (chroot_path);
143
144   c = svmdb_map (ma);
145   (void) svmdb_local_unserialize_strings (c, filename);
146   svmdb_unmap (c);
147 }
148
149 static void
150 test_vlib_vec_rate (char *chroot_path, f64 vr)
151 {
152   svmdb_client_t *c;
153   f64 *tv = 0;
154   svmdb_map_args_t *ma;
155
156   ma = map_arg_setup (chroot_path);
157
158   c = svmdb_map (ma);
159
160   vec_add1 (tv, vr);
161
162   svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
163                                 sizeof (*tv));
164   svmdb_unmap (c);
165
166   vec_free (tv);
167 }
168
169
170
171 static void
172 test_vec (char *chroot_path, u8 * vbl)
173 {
174   svmdb_client_t *c;
175   u64 *tv = 0;
176   int i;
177   svmdb_map_args_t *ma;
178
179   ma = map_arg_setup (chroot_path);
180
181   c = svmdb_map (ma);
182
183   /* my amp goes to 11 */
184   for (i = 0; i < 11; i++)
185     {
186       vec_add1 (tv, i);
187     }
188
189   svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
190   svmdb_unmap (c);
191
192   vec_free (tv);
193 }
194
195 static void
196 fake_install (char *chroot_path, u8 * add_value)
197 {
198   svmdb_client_t *c;
199   u8 *v = 0;
200   u8 **values = 0;
201   u8 *oldvalue;
202   u8 *value;
203   int nitems = 0, i;
204   serialize_main_t m;
205   svmdb_map_args_t *ma;
206
207   ma = map_arg_setup (chroot_path);
208
209   c = svmdb_map (ma);
210
211   oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
212   if (oldvalue)
213     {
214       unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
215       nitems = unserialize_likely_small_unsigned_integer (&m);
216       for (i = 0; i < nitems; i++)
217         {
218           unserialize_cstring (&m, (char **) &value);
219           vec_add1 (values, value);
220         }
221       vec_free (v);
222     }
223   nitems++;
224   value = format (0, "%s%c", add_value, 0);
225
226   vec_add1 (values, value);
227
228   fformat (stdout, "Resulting installed_sw vector:\n");
229
230   serialize_open_vector (&m, v);
231   serialize_likely_small_unsigned_integer (&m, vec_len (values));
232   for (i = 0; i < vec_len (values); i++)
233     {
234       fformat (stdout, "%s\n", values[i]);
235       serialize_cstring (&m, (char *) values[i]);
236     }
237
238   v = serialize_close_vector (&m);
239
240   svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
241   svmdb_unmap (c);
242
243   for (i = 0; i < vec_len (values); i++)
244     vec_free (values[i]);
245   vec_free (values);
246 }
247
248 static void
249 sigaction_handler (int signum, siginfo_t * i, void *notused)
250 {
251   u32 action, opaque;
252
253   action = (u32) (uword) i->si_ptr;
254   action >>= 28;
255   opaque = (u32) (uword) i->si_ptr;
256   opaque &= ~(0xF0000000);
257
258   clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
259 }
260
261 static void
262 test_reg (char *chroot_path, u8 * vbl)
263 {
264   svmdb_client_t *c;
265   svmdb_notification_args_t args;
266   svmdb_notification_args_t *a = &args;
267   struct sigaction sa;
268   svmdb_map_args_t *ma;
269
270   ma = map_arg_setup (chroot_path);
271
272   clib_memset (&sa, 0, sizeof (sa));
273   sa.sa_sigaction = sigaction_handler;
274   sa.sa_flags = SA_SIGINFO;
275   if (sigaction (SIGUSR2, &sa, 0) < 0)
276     {
277       clib_unix_warning ("sigaction");
278       return;
279     }
280
281   clib_memset (a, 0, sizeof (*a));
282
283   c = svmdb_map (ma);
284
285   a->add_del = 1 /* add */ ;
286   a->nspace = SVMDB_NAMESPACE_STRING;
287   a->var = (char *) vbl;
288   a->elsize = 1;
289   a->signum = SIGUSR2;
290   a->action = SVMDB_ACTION_GET;
291   a->opaque = 0x0eadbeef;
292
293   svmdb_local_add_del_notification (c, a);
294
295   (void) svmdb_local_get_string_variable (c, (char *) vbl);
296
297   a->add_del = 0;               /* del */
298   svmdb_local_add_del_notification (c, a);
299
300
301
302   svmdb_unmap (c);
303 }
304
305 static void
306 unset_vec (char *chroot_path, u8 * vbl)
307 {
308   svmdb_client_t *c;
309   svmdb_map_args_t *ma;
310
311   ma = map_arg_setup (chroot_path);
312
313   c = svmdb_map (ma);
314
315   svmdb_local_unset_vec_variable (c, (char *) vbl);
316   svmdb_unmap (c);
317 }
318
319 static void
320 dump_vecs (char *chroot_path)
321 {
322   svmdb_client_t *c;
323   svmdb_map_args_t *ma;
324
325   ma = map_arg_setup (chroot_path);
326
327   c = svmdb_map (ma);
328
329   svmdb_local_dump_vecs (c);
330   svmdb_unmap (c);
331 }
332
333 static void
334 crash_test (char *chroot_path)
335 {
336   svmdb_client_t *c;
337   svmdb_map_args_t *ma;
338
339   ma = map_arg_setup (chroot_path);
340
341   c = svmdb_map (ma);
342
343   clib_warning ("Grab region mutex and crash deliberately!");
344   c->db_rp->mutex_owner_pid = getpid ();
345   c->db_rp->mutex_owner_tag = -13;
346   pthread_mutex_lock (&c->db_rp->mutex);
347
348   abort ();
349 }
350
351 static void
352 map_with_size (char *chroot_path, uword size)
353 {
354   svmdb_client_t *c;
355   svmdb_map_args_t *ma;
356
357   svmdbtool_main.size = size;
358   ma = map_arg_setup (chroot_path);
359
360   c = svmdb_map (ma);
361
362   svmdb_unmap (c);
363 }
364
365 int
366 main (int argc, char **argv)
367 {
368   unformat_input_t input;
369   int parsed = 0;
370   u8 *vbl = 0, *value = 0;
371   char *chroot_path = 0;
372   u8 *chroot_path_u8;
373   u8 *filename;
374   uword size;
375   f64 vr;
376   int uid, gid, rv;
377   struct passwd _pw, *pw;
378   struct group _grp, *grp;
379   char *s, buf[128];
380
381   clib_mem_init_thread_safe (0, 128 << 20);
382
383   svmdbtool_main.uid = geteuid ();
384   svmdbtool_main.gid = getegid ();
385
386   unformat_init_command_line (&input, argv);
387
388   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
389     {
390       if (unformat (&input, "get-string %s", &vbl))
391         {
392           get_string (chroot_path, vbl);
393           vec_free (vbl);
394           parsed++;
395         }
396       else if (unformat (&input, "set-string %s %s", &vbl, &value))
397         {
398           set_string (chroot_path, vbl, value);
399           vec_free (vbl);
400           vec_free (value);
401           parsed++;
402         }
403       else if (unformat (&input, "unset-string %s", &vbl))
404         {
405           unset_string (chroot_path, vbl);
406           vec_free (vbl);
407           parsed++;
408         }
409       else if (unformat (&input, "dump-strings"))
410         {
411           dump_strings (chroot_path);
412           parsed++;
413         }
414       else if (unformat (&input, "unset-vec %s", &vbl))
415         {
416           unset_vec (chroot_path, vbl);
417           vec_free (vbl);
418           parsed++;
419         }
420       else if (unformat (&input, "dump-vecs"))
421         {
422           dump_vecs (chroot_path);
423           parsed++;
424         }
425       else if (unformat (&input, "test-vec %s", &vbl))
426         {
427           test_vec (chroot_path, vbl);
428           // vec_free(vbl);
429           parsed++;
430         }
431       else if (unformat (&input, "vlib-vec-rate %f", &vr))
432         {
433           test_vlib_vec_rate (chroot_path, vr);
434           parsed++;
435         }
436       else if (unformat (&input, "test-reg %s", &vbl))
437         {
438           test_reg (chroot_path, vbl);
439           parsed++;
440         }
441       else if (unformat (&input, "crash-test"))
442         {
443           crash_test (chroot_path);
444         }
445       else if (unformat (&input, "chroot %s", &chroot_path_u8))
446         {
447           chroot_path = (char *) chroot_path_u8;
448         }
449       else if (unformat (&input, "fake-install %s", &value))
450         {
451           fake_install (chroot_path, value);
452           parsed++;
453         }
454       else if (unformat (&input, "size %d", &size))
455         {
456           map_with_size (chroot_path, size);
457           parsed++;
458         }
459       else if (unformat (&input, "uid %d", &uid))
460         svmdbtool_main.uid = uid;
461       else if (unformat (&input, "gid %d", &gid))
462         svmdbtool_main.gid = gid;
463       else if (unformat (&input, "uid %s", &s))
464         {
465           /* lookup the username */
466           pw = NULL;
467           rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
468           if (rv < 0)
469             {
470               fformat (stderr, "cannot fetch username %s", s);
471               exit (1);
472             }
473           if (pw == NULL)
474             {
475               fformat (stderr, "username %s does not exist", s);
476               exit (1);
477             }
478           vec_free (s);
479           svmdbtool_main.uid = pw->pw_uid;
480         }
481       else if (unformat (&input, "gid %s", &s))
482         {
483           /* lookup the group name */
484           grp = NULL;
485           rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
486           if (rv != 0)
487             {
488               fformat (stderr, "cannot fetch group %s", s);
489               exit (1);
490             }
491           if (grp == NULL)
492             {
493               fformat (stderr, "group %s does not exist", s);
494               exit (1);
495             }
496           vec_free (s);
497           svmdbtool_main.gid = grp->gr_gid;
498         }
499       else if (unformat (&input, "serialize-strings %s", &filename))
500         {
501           vec_add1 (filename, 0);
502           serialize_strings (chroot_path, (char *) filename);
503           parsed++;
504         }
505       else if (unformat (&input, "unserialize-strings %s", &filename))
506         {
507           vec_add1 (filename, 0);
508           unserialize_strings (chroot_path, (char *) filename);
509           parsed++;
510         }
511       else
512         {
513           break;
514         }
515     }
516
517   unformat_free (&input);
518
519   if (!parsed)
520     {
521       fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
522                argv[0]);
523       fformat (stdout, "      unset-string <name> | dump-strings\n");
524       fformat (stdout, "      test-vec <name> |\n");
525       fformat (stdout, "      unset-vec <name> | dump-vecs\n");
526       fformat (stdout, "      chroot <prefix> [uid <nnn-or-userid>]\n");
527       fformat (stdout, "      [gid <nnn-or-group-name>]\n");
528     }
529
530   exit (0);
531 }
532
533 /*
534  * fd.io coding-style-patch-verification: ON
535  *
536  * Local Variables:
537  * eval: (c-set-style "gnu")
538  * End:
539  */