VPP-355: add PBB (802.1ah) tag rewrite
[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 <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   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 test_vlib_vec_rate (char *chroot_path, f64 vr)
125 {
126   svmdb_client_t *c;
127   f64 *tv = 0;
128   svmdb_map_args_t *ma;
129
130   ma = map_arg_setup (chroot_path);
131
132   c = svmdb_map (ma);
133
134   vec_add1 (tv, vr);
135
136   svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
137                                 sizeof (*tv));
138   svmdb_unmap (c);
139
140   vec_free (tv);
141 }
142
143
144
145 static void
146 test_vec (char *chroot_path, u8 * vbl)
147 {
148   svmdb_client_t *c;
149   u64 *tv = 0;
150   int i;
151   svmdb_map_args_t *ma;
152
153   ma = map_arg_setup (chroot_path);
154
155   c = svmdb_map (ma);
156
157   /* my amp goes to 11 */
158   for (i = 0; i < 11; i++)
159     {
160       vec_add1 (tv, i);
161     }
162
163   svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
164   svmdb_unmap (c);
165
166   vec_free (tv);
167 }
168
169 static void
170 fake_install (char *chroot_path, u8 * add_value)
171 {
172   svmdb_client_t *c;
173   u8 *v = 0;
174   u8 **values = 0;
175   u8 *oldvalue;
176   u8 *value;
177   int nitems = 0, i;
178   serialize_main_t m;
179   svmdb_map_args_t *ma;
180
181   ma = map_arg_setup (chroot_path);
182
183   c = svmdb_map (ma);
184
185   oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
186   if (oldvalue)
187     {
188       unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
189       nitems = unserialize_likely_small_unsigned_integer (&m);
190       for (i = 0; i < nitems; i++)
191         {
192           unserialize_cstring (&m, (char **) &value);
193           vec_add1 (values, value);
194         }
195       vec_free (v);
196     }
197   nitems++;
198   value = format (0, "%s%c", add_value, 0);
199
200   vec_add1 (values, value);
201
202   fformat (stdout, "Resulting installed_sw vector:\n");
203
204   serialize_open_vector (&m, v);
205   serialize_likely_small_unsigned_integer (&m, vec_len (values));
206   for (i = 0; i < vec_len (values); i++)
207     {
208       fformat (stdout, "%s\n", values[i]);
209       serialize_cstring (&m, (char *) values[i]);
210     }
211
212   v = serialize_close_vector (&m);
213
214   svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
215   svmdb_unmap (c);
216
217   for (i = 0; i < vec_len (values); i++)
218     vec_free (values[i]);
219   vec_free (values);
220 }
221
222 static void
223 sigaction_handler (int signum, siginfo_t * i, void *notused)
224 {
225   u32 action, opaque;
226
227   action = (u32) (uword) i->si_ptr;
228   action >>= 28;
229   opaque = (u32) (uword) i->si_ptr;
230   opaque &= ~(0xF0000000);
231
232   clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
233 }
234
235 static void
236 test_reg (char *chroot_path, u8 * vbl)
237 {
238   svmdb_client_t *c;
239   svmdb_notification_args_t args;
240   svmdb_notification_args_t *a = &args;
241   struct sigaction sa;
242   svmdb_map_args_t *ma;
243
244   ma = map_arg_setup (chroot_path);
245
246   memset (&sa, 0, sizeof (sa));
247   sa.sa_sigaction = sigaction_handler;
248   sa.sa_flags = SA_SIGINFO;
249   if (sigaction (SIGUSR2, &sa, 0) < 0)
250     {
251       clib_unix_warning ("sigaction");
252       return;
253     }
254
255   memset (a, 0, sizeof (*a));
256
257   c = svmdb_map (ma);
258
259   a->add_del = 1 /* add */ ;
260   a->nspace = SVMDB_NAMESPACE_STRING;
261   a->var = (char *) vbl;
262   a->elsize = 1;
263   a->signum = SIGUSR2;
264   a->action = SVMDB_ACTION_GET;
265   a->opaque = 0x0eadbeef;
266
267   svmdb_local_add_del_notification (c, a);
268
269   (void) svmdb_local_get_string_variable (c, (char *) vbl);
270
271   a->add_del = 0;               /* del */
272   svmdb_local_add_del_notification (c, a);
273
274
275
276   svmdb_unmap (c);
277 }
278
279 static void
280 unset_vec (char *chroot_path, u8 * vbl)
281 {
282   svmdb_client_t *c;
283   svmdb_map_args_t *ma;
284
285   ma = map_arg_setup (chroot_path);
286
287   c = svmdb_map (ma);
288
289   svmdb_local_unset_vec_variable (c, (char *) vbl);
290   svmdb_unmap (c);
291 }
292
293 static void
294 dump_vecs (char *chroot_path)
295 {
296   svmdb_client_t *c;
297   svmdb_map_args_t *ma;
298
299   ma = map_arg_setup (chroot_path);
300
301   c = svmdb_map (ma);
302
303   svmdb_local_dump_vecs (c);
304   svmdb_unmap (c);
305 }
306
307 static void
308 crash_test (char *chroot_path)
309 {
310   svmdb_client_t *c;
311   svmdb_map_args_t *ma;
312
313   ma = map_arg_setup (chroot_path);
314
315   c = svmdb_map (ma);
316
317   clib_warning ("Grab region mutex and crash deliberately!");
318   c->db_rp->mutex_owner_pid = getpid ();
319   c->db_rp->mutex_owner_tag = -13;
320   pthread_mutex_lock (&c->db_rp->mutex);
321
322   abort ();
323 }
324
325 static void
326 map_with_size (char *chroot_path, uword size)
327 {
328   svmdb_client_t *c;
329   svmdb_map_args_t *ma;
330
331   svmdbtool_main.size = size;
332   ma = map_arg_setup (chroot_path);
333
334   c = svmdb_map (ma);
335
336   svmdb_unmap (c);
337 }
338
339 int
340 main (int argc, char **argv)
341 {
342   unformat_input_t input;
343   int parsed = 0;
344   u8 *vbl = 0, *value = 0;
345   char *chroot_path = 0;
346   u8 *chroot_path_u8;
347   uword size;
348   f64 vr;
349   int uid, gid, rv;
350   struct passwd _pw, *pw;
351   struct group _grp, *grp;
352   char *s, buf[128];
353
354   svmdbtool_main.uid = geteuid ();
355   svmdbtool_main.gid = getegid ();
356
357   unformat_init_command_line (&input, argv);
358
359   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
360     {
361       if (unformat (&input, "get-string %s", &vbl))
362         {
363           get_string (chroot_path, vbl);
364           vec_free (vbl);
365           parsed++;
366         }
367       else if (unformat (&input, "set-string %s %s", &vbl, &value))
368         {
369           set_string (chroot_path, vbl, value);
370           vec_free (vbl);
371           vec_free (value);
372           parsed++;
373         }
374       else if (unformat (&input, "unset-string %s", &vbl))
375         {
376           unset_string (chroot_path, vbl);
377           vec_free (vbl);
378           parsed++;
379         }
380       else if (unformat (&input, "dump-strings"))
381         {
382           dump_strings (chroot_path);
383           parsed++;
384         }
385       else if (unformat (&input, "unset-vec %s", &vbl))
386         {
387           unset_vec (chroot_path, vbl);
388           vec_free (vbl);
389           parsed++;
390         }
391       else if (unformat (&input, "dump-vecs"))
392         {
393           dump_vecs (chroot_path);
394           parsed++;
395         }
396       else if (unformat (&input, "test-vec %s", &vbl))
397         {
398           test_vec (chroot_path, vbl);
399           // vec_free(vbl);
400           parsed++;
401         }
402       else if (unformat (&input, "vlib-vec-rate %f", &vr))
403         {
404           test_vlib_vec_rate (chroot_path, vr);
405           parsed++;
406         }
407       else if (unformat (&input, "test-reg %s", &vbl))
408         {
409           test_reg (chroot_path, vbl);
410           parsed++;
411         }
412       else if (unformat (&input, "crash-test"))
413         {
414           crash_test (chroot_path);
415         }
416       else if (unformat (&input, "chroot %s", &chroot_path_u8))
417         {
418           chroot_path = (char *) chroot_path_u8;
419         }
420       else if (unformat (&input, "fake-install %s", &value))
421         {
422           fake_install (chroot_path, value);
423           parsed++;
424         }
425       else if (unformat (&input, "size %d", &size))
426         {
427           map_with_size (chroot_path, size);
428           parsed++;
429         }
430       else if (unformat (&input, "uid %d", &uid))
431         svmdbtool_main.uid = uid;
432       else if (unformat (&input, "gid %d", &gid))
433         svmdbtool_main.gid = gid;
434       else if (unformat (&input, "uid %s", &s))
435         {
436           /* lookup the username */
437           pw = NULL;
438           rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
439           if (rv < 0)
440             {
441               fformat (stderr, "cannot fetch username %s", s);
442               exit (1);
443             }
444           if (pw == NULL)
445             {
446               fformat (stderr, "username %s does not exist", s);
447               exit (1);
448             }
449           vec_free (s);
450           svmdbtool_main.uid = pw->pw_uid;
451         }
452       else if (unformat (&input, "gid %s", &s))
453         {
454           /* lookup the group name */
455           grp = NULL;
456           rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
457           if (rv != 0)
458             {
459               fformat (stderr, "cannot fetch group %s", s);
460               exit (1);
461             }
462           if (grp == NULL)
463             {
464               fformat (stderr, "group %s does not exist", s);
465               exit (1);
466             }
467           vec_free (s);
468           svmdbtool_main.gid = grp->gr_gid;
469         }
470       else
471         {
472           break;
473         }
474     }
475
476   unformat_free (&input);
477
478   if (!parsed)
479     {
480       fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
481                argv[0]);
482       fformat (stdout, "      unset-string <name> | dump-strings\n");
483       fformat (stdout, "      test-vec <name> |\n");
484       fformat (stdout, "      unset-vec <name> | dump-vecs\n");
485       fformat (stdout, "      chroot <prefix> [uid <nnn-or-userid>]\n");
486       fformat (stdout, "      [gid <nnn-or-group-name>]\n");
487     }
488
489   exit (0);
490 }
491
492 /*
493  * fd.io coding-style-patch-verification: ON
494  *
495  * Local Variables:
496  * eval: (c-set-style "gnu")
497  * End:
498  */