VPP-321: Fix filtering in LISP EID dump call
[vpp.git] / svm / svmtool.c
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/mheap.h>
39 #include <vppinfra/heap.h>
40 #include <vppinfra/pool.h>
41 #include <vppinfra/format.h>
42
43 #include "svm.h"
44
45
46
47 /*
48  * format_all_svm_regions
49  * Maps / unmaps regions. Do NOT call from client code!
50  */
51 u8 *
52 format_all_svm_regions (u8 * s, va_list * args)
53 {
54   int verbose = va_arg (*args, int);
55   svm_region_t *root_rp = svm_get_root_rp ();
56   svm_main_region_t *mp;
57   svm_subregion_t *subp;
58   svm_region_t *rp;
59   svm_map_region_args_t *a = 0;
60   u8 **svm_names = 0;
61   u8 *name = 0;
62   int i;
63
64   ASSERT (root_rp);
65
66   pthread_mutex_lock (&root_rp->mutex);
67
68   s = format (s, "%U", format_svm_region, root_rp, verbose);
69
70   mp = root_rp->data_base;
71
72   /*
73    * Snapshoot names, can't hold root rp mutex across
74    * find_or_create.
75    */
76   /* *INDENT-OFF* */
77   pool_foreach (subp, mp->subregions, ({
78         name = vec_dup (subp->subregion_name);
79         vec_add1(svm_names, name);
80       }));
81   /* *INDENT-ON* */
82
83   pthread_mutex_unlock (&root_rp->mutex);
84
85   for (i = 0; i < vec_len (svm_names); i++)
86     {
87       vec_validate (a, 0);
88       a->name = (char *) svm_names[i];
89       rp = svm_region_find_or_create (a);
90       if (rp)
91         {
92           pthread_mutex_lock (&rp->mutex);
93           s = format (s, "%U", format_svm_region, rp, verbose);
94           pthread_mutex_unlock (&rp->mutex);
95           svm_region_unmap (rp);
96           vec_free (svm_names[i]);
97         }
98       vec_free (a);
99     }
100   vec_free (svm_names);
101   return (s);
102 }
103
104 void
105 show (char *chroot_path, int verbose)
106 {
107   svm_map_region_args_t *a = 0;
108
109   vec_validate (a, 0);
110
111   svm_region_init_chroot (chroot_path);
112
113   fformat (stdout, "My pid is %d\n", getpid ());
114
115   fformat (stdout, "%U", format_all_svm_regions, verbose);
116
117   svm_region_exit ();
118
119   vec_free (a);
120 }
121
122
123 static void *
124 svm_map_region_nolock (svm_map_region_args_t * a)
125 {
126   int svm_fd;
127   svm_region_t *rp;
128   int deadman = 0;
129   u8 *shm_name;
130
131   ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
132
133   shm_name = shm_name_from_svm_map_region_args (a);
134
135   svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
136
137   if (svm_fd < 0)
138     {
139       perror ("svm_region_map(mmap open)");
140       return (0);
141     }
142   vec_free (shm_name);
143
144   rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
145
146   if (rp == (svm_region_t *) MAP_FAILED)
147     {
148       close (svm_fd);
149       clib_warning ("mmap");
150       return (0);
151     }
152   /*
153    * We lost the footrace to create this region; make sure
154    * the winner has crossed the finish line.
155    */
156   while (rp->version == 0 && deadman++ < 5)
157     {
158       sleep (1);
159     }
160
161   /*
162    * <bleep>-ed?
163    */
164   if (rp->version == 0)
165     {
166       clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
167       munmap (rp, MMAP_PAGESIZE);
168       return (0);
169     }
170   /* Remap now that the region has been placed */
171   a->baseva = rp->virtual_base;
172   a->size = rp->virtual_size;
173   munmap (rp, MMAP_PAGESIZE);
174
175   rp = (void *) mmap ((void *) a->baseva, a->size,
176                       PROT_READ | PROT_WRITE,
177                       MAP_SHARED | MAP_FIXED, svm_fd, 0);
178   if ((uword) rp == (uword) MAP_FAILED)
179     {
180       clib_unix_warning ("mmap");
181       return (0);
182     }
183
184   if ((uword) rp != rp->virtual_base)
185     {
186       clib_warning ("mmap botch");
187     }
188
189   if (pthread_mutex_trylock (&rp->mutex))
190     {
191       clib_warning ("rp->mutex LOCKED by pid %d, tag %d, cleared...",
192                     rp->mutex_owner_pid, rp->mutex_owner_tag);
193       memset (&rp->mutex, 0, sizeof (rp->mutex));
194
195     }
196   else
197     {
198       clib_warning ("mutex OK...\n");
199       pthread_mutex_unlock (&rp->mutex);
200     }
201
202   return ((void *) rp);
203 }
204
205 /*
206  * rnd_pagesize
207  * Round to a pagesize multiple, presumably 4k works
208  */
209 static unsigned int
210 rnd_pagesize (unsigned int size)
211 {
212   unsigned int rv;
213
214   rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
215   return (rv);
216 }
217
218 #define MUTEX_DEBUG
219
220 always_inline void
221 region_lock (svm_region_t * rp, int tag)
222 {
223   pthread_mutex_lock (&rp->mutex);
224 #ifdef MUTEX_DEBUG
225   rp->mutex_owner_pid = getpid ();
226   rp->mutex_owner_tag = tag;
227 #endif
228 }
229
230 always_inline void
231 region_unlock (svm_region_t * rp)
232 {
233 #ifdef MUTEX_DEBUG
234   rp->mutex_owner_pid = 0;
235   rp->mutex_owner_tag = 0;
236 #endif
237   pthread_mutex_unlock (&rp->mutex);
238 }
239
240
241 static void *
242 svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a)
243 {
244   svm_region_t *root_rp = root_arg;
245   svm_main_region_t *mp;
246   svm_region_t *rp;
247   void *oldheap;
248   uword *p;
249
250   a->size += MMAP_PAGESIZE + 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);
264       region_unlock (root_rp);
265       svm_pop_heap (oldheap);
266       return rp;
267     }
268   return 0;
269
270 }
271
272 static void
273 trace (char *chroot_path, char *name, int enable_disable)
274 {
275   svm_map_region_args_t *a = 0;
276   svm_region_t *db_rp;
277   void *oldheap;
278
279   vec_validate (a, 0);
280
281   svm_region_init_chroot (chroot_path);
282
283   a->name = name;
284   a->size = 1 << 20;
285   a->flags = SVM_FLAGS_MHEAP;
286
287   db_rp = svm_region_find_or_create (a);
288
289   ASSERT (db_rp);
290
291   region_lock (db_rp, 20);
292
293   oldheap = svm_push_data_heap (db_rp);
294
295   mheap_trace (db_rp->data_heap, enable_disable);
296
297   svm_pop_heap (oldheap);
298   region_unlock (db_rp);
299
300   svm_region_unmap ((void *) db_rp);
301   svm_region_exit ();
302   vec_free (a);
303 }
304
305
306
307 static void
308 subregion_repair (char *chroot_path)
309 {
310   int i;
311   svm_main_region_t *mp;
312   svm_map_region_args_t a;
313   svm_region_t *root_rp;
314   svm_region_t *rp;
315   svm_subregion_t *subp;
316   u8 *name = 0;
317   u8 **svm_names = 0;
318
319   svm_region_init_chroot (chroot_path);
320   root_rp = svm_get_root_rp ();
321
322   pthread_mutex_lock (&root_rp->mutex);
323
324   mp = root_rp->data_base;
325
326   /*
327    * Snapshoot names, can't hold root rp mutex across
328    * find_or_create.
329    */
330   /* *INDENT-OFF* */
331   pool_foreach (subp, mp->subregions, ({
332         name = vec_dup (subp->subregion_name);
333         vec_add1(svm_names, name);
334       }));
335   /* *INDENT-ON* */
336
337   pthread_mutex_unlock (&root_rp->mutex);
338
339   for (i = 0; i < vec_len (svm_names); i++)
340     {
341       memset (&a, 0, sizeof (a));
342       a.root_path = chroot_path;
343       a.name = (char *) svm_names[i];
344       fformat (stdout, "Checking %s region...\n", a.name);
345       rp = svm_existing_region_map_nolock (root_rp, &a);
346       if (rp)
347         {
348           svm_region_unmap (rp);
349           vec_free (svm_names[i]);
350         }
351     }
352   vec_free (svm_names);
353 }
354
355 void
356 repair (char *chroot_path, int crash_root_region)
357 {
358   svm_region_t *root_rp = 0;
359   svm_map_region_args_t *a = 0;
360   void *svm_map_region (svm_map_region_args_t * a);
361   int svm_fd;
362   u8 *shm_name;
363
364   fformat (stdout, "our pid: %d\n", getpid ());
365
366   vec_validate (a, 0);
367
368   a->root_path = chroot_path;
369   a->name = SVM_GLOBAL_REGION_NAME;
370   a->baseva = SVM_GLOBAL_REGION_BASEVA;
371   a->size = SVM_GLOBAL_REGION_SIZE;
372   a->flags = SVM_FLAGS_NODATA;
373
374   shm_name = shm_name_from_svm_map_region_args (a);
375
376   svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
377
378   if (svm_fd < 0)
379     {
380       perror ("svm_region_map(mmap open)");
381       goto out;
382     }
383
384   vec_free (shm_name);
385
386   root_rp = mmap (0, MMAP_PAGESIZE,
387                   PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
388
389   if (root_rp == (svm_region_t *) MAP_FAILED)
390     {
391       close (svm_fd);
392       clib_warning ("mmap");
393       goto out;
394     }
395
396   /* Remap now that the region has been placed */
397   clib_warning ("remap to 0x%x", root_rp->virtual_base);
398
399   a->baseva = root_rp->virtual_base;
400   a->size = root_rp->virtual_size;
401   munmap (root_rp, MMAP_PAGESIZE);
402
403   root_rp = (void *) mmap ((void *) a->baseva, a->size,
404                            PROT_READ | PROT_WRITE,
405                            MAP_SHARED | MAP_FIXED, svm_fd, 0);
406   if ((uword) root_rp == (uword) MAP_FAILED)
407     {
408       clib_unix_warning ("mmap");
409       goto out;
410     }
411
412   close (svm_fd);
413
414   if ((uword) root_rp != root_rp->virtual_base)
415     {
416       clib_warning ("mmap botch");
417       goto out;
418     }
419
420   if (pthread_mutex_trylock (&root_rp->mutex))
421     {
422       clib_warning ("root_rp->mutex LOCKED by pid %d, tag %d, cleared...",
423                     root_rp->mutex_owner_pid, root_rp->mutex_owner_tag);
424       memset (&root_rp->mutex, 0, sizeof (root_rp->mutex));
425       goto out;
426     }
427   else
428     {
429       clib_warning ("root_rp->mutex OK...\n");
430       pthread_mutex_unlock (&root_rp->mutex);
431     }
432
433 out:
434   vec_free (a);
435   /*
436    * Now that the root region is known to be OK,
437    * fix broken subregions
438    */
439   subregion_repair (chroot_path);
440
441   if (crash_root_region)
442     {
443       clib_warning ("Leaving root region locked on purpose...");
444       pthread_mutex_lock (&root_rp->mutex);
445       root_rp->mutex_owner_pid = getpid ();
446       root_rp->mutex_owner_tag = 99;
447     }
448   svm_region_exit ();
449 }
450
451 int
452 main (int argc, char **argv)
453 {
454   unformat_input_t input;
455   int parsed = 0;
456   char *name;
457   char *chroot_path = 0;
458   u8 *chroot_u8;
459
460   unformat_init_command_line (&input, argv);
461
462   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
463     {
464       if (unformat (&input, "show-verbose"))
465         {
466           show (chroot_path, 1);
467           parsed++;
468         }
469       else if (unformat (&input, "show"))
470         {
471           show (chroot_path, 0);
472           parsed++;
473         }
474       else if (unformat (&input, "client-scan"))
475         {
476           svm_client_scan (chroot_path);
477           parsed++;
478         }
479       else if (unformat (&input, "repair"))
480         {
481           repair (chroot_path, 0 /* fix it */ );
482           parsed++;
483         }
484       else if (unformat (&input, "crash"))
485         {
486           repair (chroot_path, 1 /* crash it */ );
487           parsed++;
488         }
489       else if (unformat (&input, "trace-on %s", &name))
490         {
491           trace (chroot_path, name, 1);
492           parsed++;
493         }
494       else if (unformat (&input, "trace-off %s", &name))
495         {
496           trace (chroot_path, name, 0);
497           parsed++;
498         }
499       else if (unformat (&input, "chroot %s", &chroot_u8))
500         {
501           chroot_path = (char *) chroot_u8;
502         }
503       else
504         {
505           break;
506         }
507     }
508
509   unformat_free (&input);
510
511   if (!parsed)
512     {
513       fformat (stdout,
514                "%s: show | show-verbose | client-scan | trace-on <region-name>\n",
515                argv[0]);
516       fformat (stdout, "      trace-off <region-name>\n");
517     }
518   exit (0);
519 }
520
521 /*
522  * fd.io coding-style-patch-verification: ON
523  *
524  * Local Variables:
525  * eval: (c-set-style "gnu")
526  * End:
527  */