FD.io VPP  v16.06
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 <netinet/in.h>
22 #include <signal.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/fifo.h>
33 #include <vppinfra/time.h>
34 #include <vppinfra/mheap.h>
35 #include <vppinfra/heap.h>
36 #include <vppinfra/pool.h>
37 #include <vppinfra/format.h>
38 #include <vppinfra/serialize.h>
39 #include "svmdb.h"
40 
41 static void get_string (char *chroot_path, u8 *vbl)
42 {
43  svmdb_client_t *c;
44  char *rv;
45 
46  c = svmdb_map_chroot(chroot_path);
47 
48  rv = svmdb_local_get_string_variable (c, (char *)vbl);
49 
50  fformat(stdout, "%s\n", rv ? rv : "UNSET");
51  vec_free(rv);
52  svmdb_unmap (c);
53 }
54 
55 static void set_string (char *chroot_path, u8 *vbl, u8 *value)
56 {
57  svmdb_client_t *c;
58 
59  c = svmdb_map_chroot(chroot_path);
60  svmdb_local_set_string_variable (c, (char *)vbl, (char *)value);
61  svmdb_unmap (c);
62 }
63 
64 static void unset_string (char *chroot_path, u8 *vbl)
65 {
66  svmdb_client_t *c;
67 
68  c = svmdb_map_chroot(chroot_path);
69  svmdb_local_unset_string_variable (c, (char *)vbl);
70  svmdb_unmap (c);
71 }
72 
73 static void dump_strings(char *chroot_path)
74 {
75  svmdb_client_t *c;
76 
77  c = svmdb_map_chroot(chroot_path);
79  svmdb_unmap (c);
80 }
81 
82 static void test_vlib_vec_rate (char *chroot_path, f64 vr)
83 {
84  svmdb_client_t *c;
85  f64 *tv = 0;
86 
87  vec_add1 (tv, vr);
88 
89  c = svmdb_map_chroot(chroot_path);
90 
91  svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *)tv, sizeof (*tv));
92  svmdb_unmap (c);
93 
94  vec_free(tv);
95 }
96 
97 
98 
99 static void test_vec (char *chroot_path, u8 *vbl)
100 {
101  svmdb_client_t *c;
102  u64 *tv = 0;
103  int i;
104 
105  /* my amp goes to 11 */
106  for (i = 0; i < 11; i++) {
107  vec_add1(tv, i);
108  }
109 
110  c = svmdb_map_chroot(chroot_path);
111  svmdb_local_set_vec_variable (c, (char *)vbl, (char *)tv, sizeof (tv[0]));
112  svmdb_unmap (c);
113 
114  vec_free(tv);
115 }
116 
117 static void fake_install (char *chroot_path, u8 *add_value)
118 {
119  svmdb_client_t *c;
120  u8 *v = 0;
121  u8 **values = 0;
122  u8 *oldvalue;
123  u8 *value;
124  int nitems = 0, i;
126 
127  c = svmdb_map_chroot(chroot_path);
128 
129  oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
130  if (oldvalue) {
131  unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
133  for (i = 0; i < nitems; i++) {
134  unserialize_cstring (&m, (char **)&value);
135  vec_add1 (values, value);
136  }
137  vec_free (v);
138  }
139  nitems++;
140  value = format (0, "%s%c", add_value, 0);
141 
142  vec_add1 (values, value);
143 
144  fformat (stdout, "Resulting installed_sw vector:\n");
145 
146  serialize_open_vector (&m, v);
148  for (i = 0; i < vec_len (values); i++) {
149  fformat (stdout, "%s\n", values[i]);
150  serialize_cstring (&m, (char *)values[i]);
151  }
152 
153  v = serialize_close_vector (&m);
154 
155  svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
156  svmdb_unmap (c);
157 
158  for (i = 0; i < vec_len (values); i++)
159  vec_free (values[i]);
160  vec_free (values);
161 }
162 
163 static void sigaction_handler (int signum, siginfo_t *i, void *notused)
164 {
165  u32 action, opaque;
166 
167  action = (u32)(uword) i->si_ptr;
168  action >>= 28;
169  opaque = (u32)(uword) i->si_ptr;
170  opaque &= ~(0xF0000000);
171 
172  clib_warning ("signal %d, action %d, opaque %x",
173  signum, action, opaque);
174 }
175 
176 static void test_reg (char *chroot_path, u8 *vbl)
177 {
178  svmdb_client_t *c;
180  svmdb_notification_args_t *a = &args;
181  struct sigaction sa;
182 
183  memset (&sa, 0, sizeof (sa));
184  sa.sa_sigaction = sigaction_handler;
185  sa.sa_flags = SA_SIGINFO;
186  if (sigaction (SIGUSR2, &sa, 0) < 0) {
187  clib_unix_warning ("sigaction");
188  return;
189  }
190 
191  memset (a, 0, sizeof (*a));
192 
193  c = svmdb_map_chroot(chroot_path);
194 
195  a->add_del = 1 /* add */;
197  a->var = (char *) vbl;
198  a->elsize = 1;
199  a->signum = SIGUSR2;
201  a->opaque = 0x0eadbeef;
202 
204 
205  (void) svmdb_local_get_string_variable (c, (char *)vbl);
206 
207  a->add_del = 0; /* del */
209 
210 
211 
212  svmdb_unmap (c);
213 }
214 
215 static void unset_vec (char *chroot_path, u8 *vbl)
216 {
217  svmdb_client_t *c;
218 
219  c = svmdb_map_chroot(chroot_path);
220  svmdb_local_unset_vec_variable (c, (char *)vbl);
221  svmdb_unmap (c);
222 }
223 
224 static void dump_vecs(char *chroot_path)
225 {
226  svmdb_client_t *c;
227 
228  c = svmdb_map_chroot(chroot_path);
230  svmdb_unmap (c);
231 }
232 
233 static void crash_test(char *chroot_path)
234 {
235  svmdb_client_t *c;
236 
237  c = svmdb_map_chroot(chroot_path);
238 
239  clib_warning ("Grab region mutex and crash deliberately!");
240  c->db_rp->mutex_owner_pid = getpid();
241  c->db_rp->mutex_owner_tag = -13;
242  pthread_mutex_lock(&c->db_rp->mutex);
243 
244  abort();
245 }
246 
247 static void map_with_size (char *chroot_path, uword size)
248 {
249  svmdb_client_t *c;
250  c = svmdb_map_chroot_size (chroot_path, size);
251  svmdb_unmap (c);
252 }
253 
254 int main (int argc, char **argv)
255 {
256  unformat_input_t input;
257  int parsed =0;
258  u8 *vbl=0, *value=0;
259  char *chroot_path = 0;
260  u8 *chroot_path_u8;
261  uword size;
262  f64 vr;
263 
264  unformat_init_command_line (&input, argv);
265 
266  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) {
267  if (unformat (&input, "get-string %s", &vbl)) {
268  get_string (chroot_path, vbl);
269  vec_free(vbl);
270  parsed++;
271  } else if (unformat (&input, "set-string %s %s", &vbl, &value)) {
272  set_string (chroot_path, vbl, value);
273  vec_free(vbl);
274  vec_free(value);
275  parsed++;
276  } else if (unformat(&input, "unset-string %s", &vbl)) {
277  unset_string (chroot_path, vbl);
278  vec_free(vbl);
279  parsed++;
280  } else if (unformat(&input, "dump-strings")) {
281  dump_strings(chroot_path);
282  parsed++;
283  } else if (unformat(&input, "unset-vec %s", &vbl)) {
284  unset_vec (chroot_path, vbl);
285  vec_free(vbl);
286  parsed++;
287  } else if (unformat(&input, "dump-vecs")) {
288  dump_vecs(chroot_path);
289  parsed++;
290  } else if (unformat (&input, "test-vec %s", &vbl)) {
291  test_vec(chroot_path, vbl);
292  // vec_free(vbl);
293  parsed++;
294  } else if (unformat (&input, "vlib-vec-rate %f", &vr)) {
295  test_vlib_vec_rate(chroot_path, vr);
296  parsed++;
297  } else if (unformat (&input, "test-reg %s", &vbl)) {
298  test_reg(chroot_path, vbl);
299  parsed++;
300  } else if (unformat (&input, "crash-test")) {
301  crash_test (chroot_path);
302  } else if (unformat (&input, "chroot %s", &chroot_path_u8)) {
303  chroot_path = (char *) chroot_path_u8;
304  } else if (unformat (&input, "fake-install %s", &value)) {
305  fake_install (chroot_path, value);
306  parsed++;
307  } else if (unformat (&input, "size %d", &size)) {
308  map_with_size (chroot_path, size);
309  parsed++;
310  } else {
311  break;
312  }
313  }
314 
315  unformat_free (&input);
316 
317  if (!parsed) {
318  fformat(stdout, "%s: get-string <name> | set-string <name> <value>\n",
319  argv[0]);
320  fformat(stdout, " unset-string <name> | dump-strings\n");
321  fformat(stdout, " test-vec <name> |\n");
322  fformat(stdout, " unset-vec <name> | dump-vecs\n");
323  }
324 
325  exit (0);
326 }
static void get_string(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:41
static void crash_test(char *chroot_path)
Definition: svmdbtool.c:233
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
static void sigaction_handler(int signum, siginfo_t *i, void *notused)
Definition: svmdbtool.c:163
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
a
Definition: bitmap.h:393
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
void svmdb_local_dump_vecs(svmdb_client_t *client)
Definition: svmdb.c:460
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
svmdb_namespace_t nspace
Definition: svmdb.h:69
always_inline void serialize_likely_small_unsigned_integer(serialize_main_t *m, u64 x)
Definition: serialize.h:198
void svmdb_local_unset_vec_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:410
svmdb_client_t * svmdb_map_chroot_size(char *root_path, uword size)
Definition: svmdb.c:140
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
void svmdb_unmap(svmdb_client_t *client)
Definition: svmdb.c:145
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
void svmdb_local_dump_strings(svmdb_client_t *client)
Definition: svmdb.c:389
void unserialize_open_data(serialize_main_t *m, u8 *data, uword n_data_bytes)
Definition: serialize.c:835
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
static void dump_strings(char *chroot_path)
Definition: svmdbtool.c:73
static void unset_string(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:64
int main(int argc, char **argv)
Definition: svmdbtool.c:254
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:975
char * svmdb_local_get_string_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:374
static void unset_vec(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:215
static void test_vec(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:99
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:148
svm_region_t * db_rp
Definition: svmdb.h:63
void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:849
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define clib_unix_warning(format, args...)
Definition: error.h:68
void svmdb_local_set_vec_variable(svmdb_client_t *client, char *var, void *val_arg, u32 elsize)
Definition: svmdb.c:421
unsigned int u32
Definition: types.h:88
static void test_vlib_vec_rate(char *chroot_path, f64 vr)
Definition: svmdbtool.c:82
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
u32 size
Definition: vhost-user.h:74
int svmdb_local_add_del_notification(svmdb_client_t *client, svmdb_notification_args_t *a)
Definition: svmdb.c:193
int mutex_owner_tag
Definition: svm.h:44
static void dump_vecs(char *chroot_path)
Definition: svmdbtool.c:224
u64 uword
Definition: types.h:112
void serialize_cstring(serialize_main_t *m, char *s)
Definition: serialize.c:135
svmdb_client_t * svmdb_map_chroot(char *root_path)
Definition: svmdb.c:135
static void fake_install(char *chroot_path, u8 *add_value)
Definition: svmdbtool.c:117
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
unsigned char u8
Definition: types.h:56
void svmdb_local_unset_string_variable(svmdb_client_t *client, char *var)
Definition: svmdb.c:282
word fformat(FILE *f, char *fmt,...)
Definition: format.c:437
void * svmdb_local_get_vec_variable(svmdb_client_t *client, char *var, u32 elsize)
Definition: svmdb.c:438
static void set_string(char *chroot_path, u8 *vbl, u8 *value)
Definition: svmdbtool.c:55
void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:858
void svmdb_local_set_string_variable(svmdb_client_t *client, char *var, char *val)
Definition: svmdb.c:326
always_inline u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:234
struct _unformat_input_t unformat_input_t
int mutex_owner_pid
Definition: svm.h:43
static void map_with_size(char *chroot_path, uword size)
Definition: svmdbtool.c:247
pthread_mutex_t mutex
Definition: svm.h:41
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static void test_reg(char *chroot_path, u8 *vbl)
Definition: svmdbtool.c:176