FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
svmtool.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * svmtool.c
4  *
5  * Copyright (c) 2009 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <signal.h>
27 #include <pthread.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <vppinfra/clib.h>
33 #include <vppinfra/vec.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/bitmap.h>
36 #include <vppinfra/fifo.h>
37 #include <vppinfra/time.h>
38 #include <vppinfra/heap.h>
39 #include <vppinfra/pool.h>
40 #include <vppinfra/format.h>
41 
42 #include "svm.h"
43 
44 
45 
46 /*
47  * format_all_svm_regions
48  * Maps / unmaps regions. Do NOT call from client code!
49  */
50 u8 *
51 format_all_svm_regions (u8 * s, va_list * args)
52 {
53  int verbose = va_arg (*args, int);
56  svm_subregion_t *subp;
57  svm_region_t *rp;
59  u8 **svm_names = 0;
60  u8 *name = 0;
61  int i;
62 
63  ASSERT (root_rp);
64 
65  pthread_mutex_lock (&root_rp->mutex);
66 
67  s = format (s, "%U", format_svm_region, root_rp, verbose);
68 
69  mp = root_rp->data_base;
70 
71  /*
72  * Snapshoot names, can't hold root rp mutex across
73  * find_or_create.
74  */
75  /* *INDENT-OFF* */
76  pool_foreach (subp, mp->subregions) {
77  name = vec_dup (subp->subregion_name);
78  vec_add1(svm_names, name);
79  }
80  /* *INDENT-ON* */
81 
82  pthread_mutex_unlock (&root_rp->mutex);
83 
84  for (i = 0; i < vec_len (svm_names); i++)
85  {
86  vec_validate (a, 0);
87  a->name = (char *) svm_names[i];
89  if (rp)
90  {
91  pthread_mutex_lock (&rp->mutex);
92  s = format (s, "%U", format_svm_region, rp, verbose);
93  pthread_mutex_unlock (&rp->mutex);
94  svm_region_unmap (rp);
95  vec_free (svm_names[i]);
96  }
97  vec_free (a);
98  }
99  vec_free (svm_names);
100  return (s);
101 }
102 
103 void
104 show (char *chroot_path, int verbose)
105 {
107 
108  vec_validate (a, 0);
109 
110  svm_region_init_chroot (chroot_path);
111 
112  fformat (stdout, "My pid is %d\n", getpid ());
113 
114  fformat (stdout, "%U", format_all_svm_regions, verbose);
115 
116  svm_region_exit ();
117 
118  vec_free (a);
119 }
120 
121 
122 static void *
124 {
125  int svm_fd;
126  svm_region_t *rp;
127  int deadman = 0;
128  u8 *shm_name;
129 
130  ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
131 
133 
134  svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
135 
136  if (svm_fd < 0)
137  {
138  perror ("svm_region_map(mmap open)");
139  return (0);
140  }
141  vec_free (shm_name);
142 
143  rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
144 
145  if (rp == (svm_region_t *) MAP_FAILED)
146  {
147  close (svm_fd);
148  clib_warning ("mmap");
149  return (0);
150  }
151  /*
152  * We lost the footrace to create this region; make sure
153  * the winner has crossed the finish line.
154  */
155  while (rp->version == 0 && deadman++ < 5)
156  {
157  sleep (1);
158  }
159 
160  /*
161  * <bleep>-ed?
162  */
163  if (rp->version == 0)
164  {
165  clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
166  munmap (rp, MMAP_PAGESIZE);
167  return (0);
168  }
169  /* Remap now that the region has been placed */
170  a->baseva = rp->virtual_base;
171  a->size = rp->virtual_size;
172  munmap (rp, MMAP_PAGESIZE);
173 
174  rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
175  PROT_READ | PROT_WRITE,
176  MAP_SHARED | MAP_FIXED, svm_fd, 0);
177  if ((uword) rp == (uword) MAP_FAILED)
178  {
179  clib_unix_warning ("mmap");
180  return (0);
181  }
182 
183  if ((uword) rp != rp->virtual_base)
184  {
185  clib_warning ("mmap botch");
186  }
187 
188  if (pthread_mutex_trylock (&rp->mutex))
189  {
190  clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...",
192  clib_memset (&rp->mutex, 0, sizeof (rp->mutex));
193 
194  }
195  else
196  {
197  clib_warning ("mutex OK...\n");
198  pthread_mutex_unlock (&rp->mutex);
199  }
200 
201  return ((void *) rp);
202 }
203 
204 /*
205  * rnd_pagesize
206  * Round to a pagesize multiple, presumably 4k works
207  */
208 static u64
210 {
211  u64 rv;
212 
213  rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
214  return (rv);
215 }
216 
217 #define MUTEX_DEBUG
218 
219 always_inline void
220 region_lock (svm_region_t * rp, int tag)
221 {
222  pthread_mutex_lock (&rp->mutex);
223 #ifdef MUTEX_DEBUG
224  rp->mutex_owner_pid = getpid ();
225  rp->mutex_owner_tag = tag;
226 #endif
227 }
228 
229 always_inline void
231 {
232 #ifdef MUTEX_DEBUG
233  rp->mutex_owner_pid = 0;
234  rp->mutex_owner_tag = 0;
235 #endif
236  pthread_mutex_unlock (&rp->mutex);
237 }
238 
239 
240 static void *
242 {
243  svm_region_t *root_rp = root_arg;
244  svm_main_region_t *mp;
245  svm_region_t *rp;
246  void *oldheap;
247  uword *p;
248 
249  a->size += MMAP_PAGESIZE +
250  (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
251  a->size = rnd_pagesize (a->size);
252 
253  region_lock (root_rp, 4);
254  oldheap = svm_push_pvt_heap (root_rp);
255  mp = root_rp->data_base;
256 
257  ASSERT (mp);
258 
259  p = hash_get_mem (mp->name_hash, a->name);
260 
261  if (p)
262  {
263  rp = svm_map_region_nolock (a);
265  svm_pop_heap (oldheap);
266  return rp;
267  }
269  return 0;
270 
271 }
272 
273 static void
274 trace (char *chroot_path, char *name, int enable_disable)
275 {
277  svm_region_t *db_rp;
278  void *oldheap;
279 
280  vec_validate (a, 0);
281 
282  svm_region_init_chroot (chroot_path);
283 
284  a->name = name;
285  a->size = 1 << 20;
286  a->flags = SVM_FLAGS_MHEAP;
287 
288  db_rp = svm_region_find_or_create (a);
289 
290  ASSERT (db_rp);
291 
292  region_lock (db_rp, 20);
293 
294  oldheap = svm_push_data_heap (db_rp);
295 
296  mheap_trace (db_rp->data_heap, enable_disable);
297 
298  svm_pop_heap (oldheap);
299  region_unlock (db_rp);
300 
301  svm_region_unmap ((void *) db_rp);
302  svm_region_exit ();
303  vec_free (a);
304 }
305 
306 
307 
308 static void
309 subregion_repair (char *chroot_path)
310 {
311  int i;
312  svm_main_region_t *mp;
315  svm_region_t *rp;
316  svm_subregion_t *subp;
317  u8 *name = 0;
318  u8 **svm_names = 0;
319 
320  svm_region_init_chroot (chroot_path);
322 
323  pthread_mutex_lock (&root_rp->mutex);
324 
325  mp = root_rp->data_base;
326 
327  /*
328  * Snapshoot names, can't hold root rp mutex across
329  * find_or_create.
330  */
331  /* *INDENT-OFF* */
332  pool_foreach (subp, mp->subregions) {
333  name = vec_dup (subp->subregion_name);
334  vec_add1(svm_names, name);
335  }
336  /* *INDENT-ON* */
337 
338  pthread_mutex_unlock (&root_rp->mutex);
339 
340  for (i = 0; i < vec_len (svm_names); i++)
341  {
342  clib_memset (&a, 0, sizeof (a));
343  a.root_path = chroot_path;
344  a.name = (char *) svm_names[i];
345  fformat (stdout, "Checking %s region...\n", a.name);
347  if (rp)
348  {
349  svm_region_unmap (rp);
350  vec_free (svm_names[i]);
351  }
352  }
353  vec_free (svm_names);
354 }
355 
356 void
357 repair (char *chroot_path, int crash_root_region)
358 {
359  svm_region_t *root_rp = 0;
362  int svm_fd;
363  u8 *shm_name;
364 
365  fformat (stdout, "our pid: %d\n", getpid ());
366 
367  vec_validate (a, 0);
368 
369  a->root_path = chroot_path;
370  a->name = SVM_GLOBAL_REGION_NAME;
371  a->baseva = svm_get_global_region_base_va ();
372  a->size = SVM_GLOBAL_REGION_SIZE;
373  a->flags = SVM_FLAGS_NODATA;
374 
376 
377  svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
378 
379  if (svm_fd < 0)
380  {
381  perror ("svm_region_map(mmap open)");
382  goto out;
383  }
384 
385  vec_free (shm_name);
386 
387  root_rp = mmap (0, MMAP_PAGESIZE,
388  PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
389 
390  if (root_rp == (svm_region_t *) MAP_FAILED)
391  {
392  close (svm_fd);
393  clib_warning ("mmap");
394  goto out;
395  }
396 
397  /* Remap now that the region has been placed */
398  clib_warning ("remap to 0x%x", root_rp->virtual_base);
399 
400  a->baseva = root_rp->virtual_base;
401  a->size = root_rp->virtual_size;
402  munmap (root_rp, MMAP_PAGESIZE);
403 
404  root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
405  PROT_READ | PROT_WRITE,
406  MAP_SHARED | MAP_FIXED, svm_fd, 0);
407  if ((uword) root_rp == (uword) MAP_FAILED)
408  {
409  clib_unix_warning ("mmap");
410  goto out;
411  }
412 
413  close (svm_fd);
414 
415  if ((uword) root_rp != root_rp->virtual_base)
416  {
417  clib_warning ("mmap botch");
418  goto out;
419  }
420 
421  if (pthread_mutex_trylock (&root_rp->mutex))
422  {
423  clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...",
425  clib_memset (&root_rp->mutex, 0, sizeof (root_rp->mutex));
426  goto out;
427  }
428  else
429  {
430  clib_warning ("root_rp->mutex OK...\n");
431  pthread_mutex_unlock (&root_rp->mutex);
432  }
433 
434 out:
435  vec_free (a);
436  /*
437  * Now that the root region is known to be OK,
438  * fix broken subregions
439  */
440  subregion_repair (chroot_path);
441 
442  if (crash_root_region)
443  {
444  clib_warning ("Leaving root region locked on purpose...");
445  pthread_mutex_lock (&root_rp->mutex);
446  root_rp->mutex_owner_pid = getpid ();
447  root_rp->mutex_owner_tag = 99;
448  }
449  svm_region_exit ();
450 }
451 
452 int
453 main (int argc, char **argv)
454 {
455  unformat_input_t input;
456  int parsed = 0;
457  char *name;
458  char *chroot_path = 0;
459  u8 *chroot_u8;
460 
461  clib_mem_init_thread_safe (0, 128 << 20);
462 
463  unformat_init_command_line (&input, argv);
464 
465  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
466  {
467  if (unformat (&input, "show-verbose"))
468  {
469  show (chroot_path, 1);
470  parsed++;
471  }
472  else if (unformat (&input, "show"))
473  {
474  show (chroot_path, 0);
475  parsed++;
476  }
477  else if (unformat (&input, "client-scan"))
478  {
479  svm_client_scan (chroot_path);
480  parsed++;
481  }
482  else if (unformat (&input, "repair"))
483  {
484  repair (chroot_path, 0 /* fix it */ );
485  parsed++;
486  }
487  else if (unformat (&input, "crash"))
488  {
489  repair (chroot_path, 1 /* crash it */ );
490  parsed++;
491  }
492  else if (unformat (&input, "trace-on %s", &name))
493  {
494  trace (chroot_path, name, 1);
495  parsed++;
496  }
497  else if (unformat (&input, "trace-off %s", &name))
498  {
499  trace (chroot_path, name, 0);
500  parsed++;
501  }
502  else if (unformat (&input, "chroot %s", &chroot_u8))
503  {
504  chroot_path = (char *) chroot_u8;
505  }
506  else
507  {
508  break;
509  }
510  }
511 
512  unformat_free (&input);
513 
514  if (!parsed)
515  {
516  fformat (stdout,
517  "%s: show | show-verbose | client-scan | trace-on <region-name>\n",
518  argv[0]);
519  fformat (stdout, " trace-off <region-name>\n");
520  }
521  exit (0);
522 }
523 
524 /*
525  * fd.io coding-style-patch-verification: ON
526  *
527  * Local Variables:
528  * eval: (c-set-style "gnu")
529  * End:
530  */
svm_region_::data_heap
void * data_heap
Definition: svm_common.h:47
SVM_GLOBAL_REGION_SIZE
#define SVM_GLOBAL_REGION_SIZE
Definition: svm_common.h:101
svm_region_exit
void svm_region_exit(void)
Definition: svm.c:1211
svm_get_root_rp
svm_region_t * svm_get_root_rp(void)
Definition: svm.c:53
name
string name[64]
Definition: fib.api:25
SVM_FLAGS_NODATA
#define SVM_FLAGS_NODATA
Definition: svm_common.h:30
bitmap.h
clib.h
svm_region_init_chroot
int svm_region_init_chroot(const char *root_path)
Definition: svm.c:841
string.h
svm_region_unmap
void svm_region_unmap(void *rp_arg)
Definition: svm.c:1144
region_unlock
static void region_unlock(svm_region_t *rp)
Definition: svmtool.c:230
fifo.h
svm_subregion_t
Definition: svm_common.h:113
svm_map_region
void * svm_map_region(svm_map_region_args_t *a)
Definition: svm.c:549
unformat_input_t
struct _unformat_input_t unformat_input_t
rnd_pagesize
static u64 rnd_pagesize(u64 size)
Definition: svmtool.c:209
clib_unix_warning
#define clib_unix_warning(format, args...)
Definition: error.h:68
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
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
svm_region_::data_base
void * data_base
Definition: svm_common.h:46
hash.h
svm_existing_region_map_nolock
static void * svm_existing_region_map_nolock(void *root_arg, svm_map_region_args_t *a)
Definition: svmtool.c:241
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
svm_region_find_or_create
void * svm_region_find_or_create(svm_map_region_args_t *a)
Definition: svm.c:881
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
svm.h
vec_dup
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:444
svm_region_::mutex_owner_pid
int mutex_owner_pid
Definition: svm_common.h:40
repair
void repair(char *chroot_path, int crash_root_region)
Definition: svmtool.c:357
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
uword
u64 uword
Definition: types.h:112
SVM_VERSION
#define SVM_VERSION
Definition: svm_common.h:26
svm_pop_heap
static void svm_pop_heap(void *oldheap)
Definition: svm.h:94
svm_main_region_t::subregions
svm_subregion_t * subregions
Definition: svm_common.h:120
pool.h
Fixed length block allocator. Pools are built from clib vectors and bitmaps. Use pools when repeatedl...
svm_main_region_t
Definition: svm_common.h:118
time.h
svm_map_region_nolock
static void * svm_map_region_nolock(svm_map_region_args_t *a)
Definition: svmtool.c:123
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
format.h
svm_region_::virtual_size
uword virtual_size
Definition: svm_common.h:44
svm_subregion_t::subregion_name
u8 * subregion_name
Definition: svm_common.h:115
MMAP_PAGESIZE
#define MMAP_PAGESIZE
Definition: ssvm.h:42
clib_mem_init_thread_safe
void * clib_mem_init_thread_safe(void *memory, uword memory_size)
Definition: mem_dlmalloc.c:280
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
size
u32 size
Definition: vhost_user.h:125
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
root_rp
static svm_region_t * root_rp
Definition: svm.c:45
svm_get_global_region_base_va
u64 svm_get_global_region_base_va()
Definition: svm.c:61
u64
unsigned long u64
Definition: types.h:89
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
main
int main(int argc, char **argv)
Definition: svmtool.c:453
svm_main_region_t::name_hash
uword * name_hash
Definition: svm_common.h:121
format_svm_region
u8 * format_svm_region(u8 *s, va_list *args)
Definition: svm.c:182
svm_region_::virtual_base
uword virtual_base
Definition: svm_common.h:43
fformat
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:466
format_all_svm_regions
u8 * format_all_svm_regions(u8 *s, va_list *args)
Definition: svmtool.c:51
vec.h
svm_push_pvt_heap
static void * svm_push_pvt_heap(svm_region_t *rp)
Definition: svm.h:78
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
subregion_repair
static void subregion_repair(char *chroot_path)
Definition: svmtool.c:309
svm_region_
Definition: svm_common.h:35
SVM_GLOBAL_REGION_NAME
#define SVM_GLOBAL_REGION_NAME
Definition: svm_common.h:102
u8
unsigned char u8
Definition: types.h:56
mheap_trace
void mheap_trace(clib_mem_heap_t *v, int enable)
Definition: mem_dlmalloc.c:494
a
a
Definition: bitmap.h:525
region_lock
static void region_lock(svm_region_t *rp, int tag)
Definition: svmtool.c:220
uword_to_pointer
#define uword_to_pointer(u, type)
Definition: types.h:136
svm_client_scan
void svm_client_scan(const char *root_path)
Definition: svm.c:1251
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
SVM_PVT_MHEAP_SIZE
#define SVM_PVT_MHEAP_SIZE
Definition: svm_common.h:33
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
rv
int __clib_unused rv
Definition: application.c:491
SVM_FLAGS_MHEAP
#define SVM_FLAGS_MHEAP
Definition: svm_common.h:28
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
svm_region_::version
volatile uword version
Definition: svm_common.h:37
svm_push_data_heap
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
svm_region_::mutex_owner_tag
int mutex_owner_tag
Definition: svm_common.h:41
shm_name_from_svm_map_region_args
u8 * shm_name_from_svm_map_region_args(svm_map_region_args_t *a)
Definition: svm.c:421
trace
static void trace(char *chroot_path, char *name, int enable_disable)
Definition: svmtool.c:274
heap.h
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
svm_map_region_args_
Definition: svm_common.h:66