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