blob: 0f38d7fe0eb73cd01b2a8ecd29e3e43f6a6298a8 [file] [log] [blame]
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
Dave Barach8a7fb0c2016-07-08 14:44:23 -04003 * svmtool.c
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 *
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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#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 */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040050u8 *
51format_all_svm_regions (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040053 int verbose = va_arg (*args, int);
54 svm_region_t *root_rp = svm_get_root_rp ();
55 svm_main_region_t *mp;
56 svm_subregion_t *subp;
57 svm_region_t *rp;
58 svm_map_region_args_t *a = 0;
59 u8 **svm_names = 0;
60 u8 *name = 0;
61 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
Dave Barach8a7fb0c2016-07-08 14:44:23 -040063 ASSERT (root_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
Dave Barach8a7fb0c2016-07-08 14:44:23 -040065 pthread_mutex_lock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Dave Barach8a7fb0c2016-07-08 14:44:23 -040067 s = format (s, "%U", format_svm_region, root_rp, verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Dave Barach8a7fb0c2016-07-08 14:44:23 -040069 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Dave Barach8a7fb0c2016-07-08 14:44:23 -040071 /*
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* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Dave Barach8a7fb0c2016-07-08 14:44:23 -040082 pthread_mutex_unlock (&root_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Dave Barach8a7fb0c2016-07-08 14:44:23 -040084 for (i = 0; i < vec_len (svm_names); i++)
85 {
86 vec_validate (a, 0);
87 a->name = (char *) svm_names[i];
88 rp = svm_region_find_or_create (a);
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -040099 vec_free (svm_names);
100 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101}
102
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400103void
104show (char *chroot_path, int verbose)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400106 svm_map_region_args_t *a = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400108 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400110 svm_region_init_chroot (chroot_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400112 fformat (stdout, "My pid is %d\n", getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400114 fformat (stdout, "%U", format_all_svm_regions, verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400116 svm_region_exit ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400118 vec_free (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
121
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122static void *
123svm_map_region_nolock (svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400125 int svm_fd;
126 svm_region_t *rp;
127 int deadman = 0;
128 u8 *shm_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400130 ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400132 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400134 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400136 if (svm_fd < 0)
137 {
138 perror ("svm_region_map(mmap open)");
139 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400141 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400143 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400151 /*
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 }
159
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400160 /*
161 * <bleep>-ed?
162 */
163 if (rp->version == 0)
164 {
165 clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION);
Dave Barachac0798d2016-07-26 10:30:50 -0400166 munmap (rp, MMAP_PAGESIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400167 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400169 /* 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
Damjan Marion7bee80c2017-04-26 15:32:12 +0200174 rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400175 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 }
182
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400183 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...",
191 rp->mutex_owner_pid, rp->mutex_owner_tag);
Dave Barachb7b92992018-10-17 10:38:51 -0400192 clib_memset (&rp->mutex, 0, sizeof (rp->mutex));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400193
194 }
195 else
196 {
197 clib_warning ("mutex OK...\n");
198 pthread_mutex_unlock (&rp->mutex);
199 }
200
201 return ((void *) rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202}
203
204/*
205 * rnd_pagesize
206 * Round to a pagesize multiple, presumably 4k works
207 */
Dave Barachb3d93da2016-08-03 14:34:38 -0400208static u64
209rnd_pagesize (u64 size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210{
Dave Barachb3d93da2016-08-03 14:34:38 -0400211 u64 rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400213 rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1);
214 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215}
216
217#define MUTEX_DEBUG
218
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400219always_inline void
220region_lock (svm_region_t * rp, int tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400222 pthread_mutex_lock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400224 rp->mutex_owner_pid = getpid ();
225 rp->mutex_owner_tag = tag;
226#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227}
228
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400229always_inline void
230region_unlock (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
232#ifdef MUTEX_DEBUG
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400233 rp->mutex_owner_pid = 0;
234 rp->mutex_owner_tag = 0;
235#endif
236 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237}
238
239
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400240static void *
241svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400243 svm_region_t *root_rp = root_arg;
244 svm_main_region_t *mp;
245 svm_region_t *rp;
246 void *oldheap;
247 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Dave Barachc3799992016-08-15 11:12:27 -0400249 a->size += MMAP_PAGESIZE +
Dave Barachb3d93da2016-08-03 14:34:38 -0400250 (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400251 a->size = rnd_pagesize (a->size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400253 region_lock (root_rp, 4);
254 oldheap = svm_push_pvt_heap (root_rp);
255 mp = root_rp->data_base;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400257 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400268 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270}
271
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400272static void
273trace (char *chroot_path, char *name, int enable_disable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400275 svm_map_region_args_t *a = 0;
276 svm_region_t *db_rp;
277 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400279 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400281 svm_region_init_chroot (chroot_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400283 a->name = name;
284 a->size = 1 << 20;
285 a->flags = SVM_FLAGS_MHEAP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400287 db_rp = svm_region_find_or_create (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400289 ASSERT (db_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400291 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303}
304
305
306
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400307static void
308subregion_repair (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400310 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400319 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 {
Dave Barachb7b92992018-10-17 10:38:51 -0400341 clib_memset (&a, 0, sizeof (a));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400342 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400352 vec_free (svm_names);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353}
354
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400355void
356repair (char *chroot_path, int crash_root_region)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400358 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400364 fformat (stdout, "our pid: %d\n", getpid ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400366 vec_validate (a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400368 a->root_path = chroot_path;
369 a->name = SVM_GLOBAL_REGION_NAME;
Damjan Marionaec8f892018-01-08 16:35:35 +0100370 a->baseva = svm_get_global_region_base_va ();
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400371 a->size = SVM_GLOBAL_REGION_SIZE;
372 a->flags = SVM_FLAGS_NODATA;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400374 shm_name = shm_name_from_svm_map_region_args (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400376 svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400378 if (svm_fd < 0)
379 {
380 perror ("svm_region_map(mmap open)");
381 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 }
383
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400384 vec_free (shm_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400386 root_rp = mmap (0, MMAP_PAGESIZE,
387 PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400389 if (root_rp == (svm_region_t *) MAP_FAILED)
390 {
391 close (svm_fd);
392 clib_warning ("mmap");
393 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400396 /* Remap now that the region has been placed */
397 clib_warning ("remap to 0x%x", root_rp->virtual_base);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400399 a->baseva = root_rp->virtual_base;
400 a->size = root_rp->virtual_size;
401 munmap (root_rp, MMAP_PAGESIZE);
402
Damjan Marion7bee80c2017-04-26 15:32:12 +0200403 root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size,
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400404 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400411
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);
Dave Barachb7b92992018-10-17 10:38:51 -0400424 clib_memset (&root_rp->mutex, 0, sizeof (root_rp->mutex));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400425 goto out;
426 }
427 else
428 {
429 clib_warning ("root_rp->mutex OK...\n");
430 pthread_mutex_unlock (&root_rp->mutex);
431 }
432
433out:
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 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449}
450
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400451int
452main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400454 unformat_input_t input;
455 int parsed = 0;
456 char *name;
457 char *chroot_path = 0;
458 u8 *chroot_u8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
Guanghua Zhang60f16c22019-08-12 09:04:35 +0800460 clib_mem_init_thread_safe (0, 128 << 20);
461
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400462 unformat_init_command_line (&input, argv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400464 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
465 {
466 if (unformat (&input, "show-verbose"))
467 {
468 show (chroot_path, 1);
469 parsed++;
470 }
471 else if (unformat (&input, "show"))
472 {
473 show (chroot_path, 0);
474 parsed++;
475 }
476 else if (unformat (&input, "client-scan"))
477 {
478 svm_client_scan (chroot_path);
479 parsed++;
480 }
481 else if (unformat (&input, "repair"))
482 {
483 repair (chroot_path, 0 /* fix it */ );
484 parsed++;
485 }
486 else if (unformat (&input, "crash"))
487 {
488 repair (chroot_path, 1 /* crash it */ );
489 parsed++;
490 }
491 else if (unformat (&input, "trace-on %s", &name))
492 {
493 trace (chroot_path, name, 1);
494 parsed++;
495 }
496 else if (unformat (&input, "trace-off %s", &name))
497 {
498 trace (chroot_path, name, 0);
499 parsed++;
500 }
501 else if (unformat (&input, "chroot %s", &chroot_u8))
502 {
503 chroot_path = (char *) chroot_u8;
504 }
505 else
506 {
507 break;
508 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 }
510
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400511 unformat_free (&input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400513 if (!parsed)
514 {
515 fformat (stdout,
516 "%s: show | show-verbose | client-scan | trace-on <region-name>\n",
517 argv[0]);
518 fformat (stdout, " trace-off <region-name>\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400520 exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521}
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400522
523/*
524 * fd.io coding-style-patch-verification: ON
525 *
526 * Local Variables:
527 * eval: (c-set-style "gnu")
528 * End:
529 */