FD.io VPP  v19.04-6-g6f05f72
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/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 {
46  int uid, gid;
49 
51 
52 static inline svmdb_map_args_t *
53 map_arg_setup (char *chroot_path)
54 {
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 {
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 {
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);
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;
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));
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);
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;
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 */ ;
287  a->var = (char *) vbl;
288  a->elsize = 1;
289  a->signum = SIGUSR2;
291  a->opaque = 0x0eadbeef;
292 
294 
295  (void) svmdb_local_get_string_variable (c, (char *) vbl);
296 
297  a->add_del = 0; /* del */
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 
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  svmdbtool_main.uid = geteuid ();
382  svmdbtool_main.gid = getegid ();
383 
384  unformat_init_command_line (&input, argv);
385 
386  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
387  {
388  if (unformat (&input, "get-string %s", &vbl))
389  {
390  get_string (chroot_path, vbl);
391  vec_free (vbl);
392  parsed++;
393  }
394  else if (unformat (&input, "set-string %s %s", &vbl, &value))
395  {
396  set_string (chroot_path, vbl, value);
397  vec_free (vbl);
398  vec_free (value);
399  parsed++;
400  }
401  else if (unformat (&input, "unset-string %s", &vbl))
402  {
403  unset_string (chroot_path, vbl);
404  vec_free (vbl);
405  parsed++;
406  }
407  else if (unformat (&input, "dump-strings"))
408  {
409  dump_strings (chroot_path);
410  parsed++;
411  }
412  else if (unformat (&input, "unset-vec %s", &vbl))
413  {
414  unset_vec (chroot_path, vbl);
415  vec_free (vbl);
416  parsed++;
417  }
418  else if (unformat (&input, "dump-vecs"))
419  {
420  dump_vecs (chroot_path);
421  parsed++;
422  }
423  else if (unformat (&input, "test-vec %s", &vbl))
424  {
425  test_vec (chroot_path, vbl);
426  // vec_free(vbl);
427  parsed++;
428  }
429  else if (unformat (&input, "vlib-vec-rate %f", &vr))
430  {
431  test_vlib_vec_rate (chroot_path, vr);
432  parsed++;
433  }
434  else if (unformat (&input, "test-reg %s", &vbl))
435  {
436  test_reg (chroot_path, vbl);
437  parsed++;
438  }
439  else if (unformat (&input, "crash-test"))
440  {
441  crash_test (chroot_path);
442  }
443  else if (unformat (&input, "chroot %s", &chroot_path_u8))
444  {
445  chroot_path = (char *) chroot_path_u8;
446  }
447  else if (unformat (&input, "fake-install %s", &value))
448  {
449  fake_install (chroot_path, value);
450  parsed++;
451  }
452  else if (unformat (&input, "size %d", &size))
453  {
454  map_with_size (chroot_path, size);
455  parsed++;
456  }
457  else if (unformat (&input, "uid %d", &uid))
458  svmdbtool_main.uid = uid;
459  else if (unformat (&input, "gid %d", &gid))
460  svmdbtool_main.gid = gid;
461  else if (unformat (&input, "uid %s", &s))
462  {
463  /* lookup the username */
464  pw = NULL;
465  rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
466  if (rv < 0)
467  {
468  fformat (stderr, "cannot fetch username %s", s);
469  exit (1);
470  }
471  if (pw == NULL)
472  {
473  fformat (stderr, "username %s does not exist", s);
474  exit (1);
475  }
476  vec_free (s);
477  svmdbtool_main.uid = pw->pw_uid;
478  }
479  else if (unformat (&input, "gid %s", &s))
480  {
481  /* lookup the group name */
482  grp = NULL;
483  rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
484  if (rv != 0)
485  {
486  fformat (stderr, "cannot fetch group %s", s);
487  exit (1);
488  }
489  if (grp == NULL)
490  {
491  fformat (stderr, "group %s does not exist", s);
492  exit (1);
493  }
494  vec_free (s);
495  svmdbtool_main.gid = grp->gr_gid;
496  }
497  else if (unformat (&input, "serialize-strings %s", &filename))
498  {
499  vec_add1 (filename, 0);
500  serialize_strings (chroot_path, (char *) filename);
501  parsed++;
502  }
503  else if (unformat (&input, "unserialize-strings %s", &filename))
504  {
505  vec_add1 (filename, 0);
506  unserialize_strings (chroot_path, (char *) filename);
507  parsed++;
508  }
509  else
510  {
511  break;
512  }
513  }
514 
515  unformat_free (&input);
516 
517  if (!parsed)
518  {
519  fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
520  argv[0]);
521  fformat (stdout, " unset-string <name> | dump-strings\n");
522  fformat (stdout, " test-vec <name> |\n");
523  fformat (stdout, " unset-vec <name> | dump-vecs\n");
524  fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
525  fformat (stdout, " [gid <nnn-or-group-name>]\n");
526  }
527 
528  exit (0);
529 }
530 
531 /*
532  * fd.io coding-style-patch-verification: ON
533  *
534  * Local Variables:
535  * eval: (c-set-style "gnu")
536  * End:
537  */
static void get_string(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:67
static void crash_test(char *chroot_path)
Definition: svmdbtool.c:334
static void sigaction_handler(int signum, siginfo_t *i, void *notused)
Definition: svmdbtool.c:249
static u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:254
a
Definition: bitmap.h:538
Optimized string handling code, including c11-compliant "safe C library" variants.
void svmdb_local_dump_vecs(svmdb_client_t *client)
Definition: svmdb.c:602
unsigned long u64
Definition: types.h:89
Fixed length block allocator.
svmdb_namespace_t nspace
Definition: svmdb.h:76
#define NULL
Definition: clib.h:58
void svmdb_local_unset_vec_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:549
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int i
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
const char * root_path
Definition: svmdb.h:86
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
void svmdb_unmap(svmdb_client_t *client)
Definition: svmdb.c:139
void svmdb_local_dump_strings(svmdb_client_t *client)
Definition: svmdb.c:407
void unserialize_open_data(serialize_main_t *m, u8 *data, uword n_data_bytes)
Definition: serialize.c:891
svmdbtool_main_t svmdbtool_main
Definition: svmdbtool.c:50
int svmdb_local_serialize_strings(svmdb_client_t *client, char *filename)
Definition: svmdb.c:431
static svmdb_map_args_t * map_arg_setup(char *chroot_path)
Definition: svmdbtool.c:53
static void dump_strings(char *chroot_path)
Definition: svmdbtool.c:111
unsigned int u32
Definition: types.h:88
static void unset_string(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:98
int main(int argc, char **argv)
Definition: svmdbtool.c:366
uword size
static void serialize_likely_small_unsigned_integer(serialize_main_t *m, u64 x)
Definition: serialize.h:218
char * svmdb_local_get_string_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:390
static void unset_vec(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:306
struct _unformat_input_t unformat_input_t
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1007
static void serialize_strings(char *chroot_path, char *filename)
Definition: svmdbtool.c:124
static void test_vec(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:172
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:178
svm_region_t * db_rp
Definition: svmdb.h:69
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
svmdb_client_t * c
void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:909
int svmdb_local_unserialize_strings(svmdb_client_t *client, char *filename)
Definition: svmdb.c:497
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define clib_warning(format, args...)
Definition: error.h:59
static void unserialize_strings(char *chroot_path, char *filename)
Definition: svmdbtool.c:137
svmdb_map_args_t map_args
Definition: svmdbtool.c:45
void svmdb_local_set_vec_variable(svmdb_client_t *client, char *var, void *val_arg, u32 elsize)
Definition: svmdb.c:561
static void test_vlib_vec_rate(char *chroot_path, f64 vr)
Definition: svmdbtool.c:150
Bitmaps built as vectors of machine words.
int svmdb_local_add_del_notification(svmdb_client_t *client, svmdb_notification_args_t *a)
Definition: svmdb.c:194
int mutex_owner_tag
Definition: svm_common.h:40
static void dump_vecs(char *chroot_path)
Definition: svmdbtool.c:320
void serialize_cstring(serialize_main_t *m, char *s)
Definition: serialize.c:164
static void fake_install(char *chroot_path, u8 *add_value)
Definition: svmdbtool.c:196
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void svmdb_local_unset_string_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:291
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
#define clib_unix_warning(format, args...)
Definition: error.h:68
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:71
void * svmdb_local_get_vec_variable(svmdb_client_t *client, char *var, u32 elsize)
Definition: svmdb.c:579
static void set_string(char *chroot_path, u8 *vbl, u8 *value)
Definition: svmdbtool.c:85
uword size
Definition: svmdb.h:87
void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:919
void svmdb_local_set_string_variable(svmdb_client_t *client, char *var, char *val)
Definition: svmdb.c:340
int mutex_owner_pid
Definition: svm_common.h:39
static void map_with_size(char *chroot_path, uword size)
Definition: svmdbtool.c:352
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
pthread_mutex_t mutex
Definition: svm_common.h:37
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static void test_reg(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:262