FD.io VPP  v20.05.1-6-gf53edbc3b
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  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  */
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
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
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:590
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
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
u64 size
Definition: vhost_user.h:150
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1013
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:145
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
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
#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
u8 value
Definition: qos.api:54
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
vl_api_mac_event_action_t action
Definition: l2.api:181
#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:163
#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
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:235
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:978
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:171
static void test_reg(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:262