Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 3 | * svmtool.c |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 51 | u8 * |
| 52 | format_all_svm_regions (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 64 | ASSERT (root_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 66 | pthread_mutex_lock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 68 | s = format (s, "%U", format_svm_region, root_rp, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 70 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 83 | pthread_mutex_unlock (&root_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 100 | vec_free (svm_names); |
| 101 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 104 | void |
| 105 | show (char *chroot_path, int verbose) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 107 | svm_map_region_args_t *a = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 109 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 111 | svm_region_init_chroot (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 113 | fformat (stdout, "My pid is %d\n", getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 115 | fformat (stdout, "%U", format_all_svm_regions, verbose); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 117 | svm_region_exit (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 119 | vec_free (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 123 | static void * |
| 124 | svm_map_region_nolock (svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 126 | int svm_fd; |
| 127 | svm_region_t *rp; |
| 128 | int deadman = 0; |
| 129 | u8 *shm_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 131 | ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 133 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 135 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 137 | if (svm_fd < 0) |
| 138 | { |
| 139 | perror ("svm_region_map(mmap open)"); |
| 140 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 161 | /* |
| 162 | * <bleep>-ed? |
| 163 | */ |
| 164 | if (rp->version == 0) |
| 165 | { |
| 166 | clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); |
Dave Barach | ac0798d | 2016-07-26 10:30:50 -0400 | [diff] [blame] | 167 | munmap (rp, MMAP_PAGESIZE); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 168 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 175 | rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 193 | clib_memset (&rp->mutex, 0, sizeof (rp->mutex)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | clib_warning ("mutex OK...\n"); |
| 199 | pthread_mutex_unlock (&rp->mutex); |
| 200 | } |
| 201 | |
| 202 | return ((void *) rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * rnd_pagesize |
| 207 | * Round to a pagesize multiple, presumably 4k works |
| 208 | */ |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 209 | static u64 |
| 210 | rnd_pagesize (u64 size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | { |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 212 | u64 rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 214 | rv = (size + (MMAP_PAGESIZE - 1)) & ~(MMAP_PAGESIZE - 1); |
| 215 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | #define MUTEX_DEBUG |
| 219 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 220 | always_inline void |
| 221 | region_lock (svm_region_t * rp, int tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 223 | pthread_mutex_lock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 225 | rp->mutex_owner_pid = getpid (); |
| 226 | rp->mutex_owner_tag = tag; |
| 227 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 230 | always_inline void |
| 231 | region_unlock (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | { |
| 233 | #ifdef MUTEX_DEBUG |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 234 | rp->mutex_owner_pid = 0; |
| 235 | rp->mutex_owner_tag = 0; |
| 236 | #endif |
| 237 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 241 | static void * |
| 242 | svm_existing_region_map_nolock (void *root_arg, svm_map_region_args_t * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 250 | a->size += MMAP_PAGESIZE + |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 251 | (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 252 | a->size = rnd_pagesize (a->size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 254 | region_lock (root_rp, 4); |
| 255 | oldheap = svm_push_pvt_heap (root_rp); |
| 256 | mp = root_rp->data_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 258 | ASSERT (mp); |
| 259 | |
| 260 | p = hash_get_mem (mp->name_hash, a->name); |
| 261 | |
| 262 | if (p) |
| 263 | { |
| 264 | rp = svm_map_region_nolock (a); |
| 265 | region_unlock (root_rp); |
| 266 | svm_pop_heap (oldheap); |
| 267 | return rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 269 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | |
| 271 | } |
| 272 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 273 | static void |
| 274 | trace (char *chroot_path, char *name, int enable_disable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 276 | svm_map_region_args_t *a = 0; |
| 277 | svm_region_t *db_rp; |
| 278 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 280 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 282 | svm_region_init_chroot (chroot_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 284 | a->name = name; |
| 285 | a->size = 1 << 20; |
| 286 | a->flags = SVM_FLAGS_MHEAP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 288 | db_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 290 | ASSERT (db_rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 308 | static void |
| 309 | subregion_repair (char *chroot_path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 311 | int i; |
| 312 | svm_main_region_t *mp; |
| 313 | svm_map_region_args_t a; |
| 314 | svm_region_t *root_rp; |
| 315 | svm_region_t *rp; |
| 316 | svm_subregion_t *subp; |
| 317 | u8 *name = 0; |
| 318 | u8 **svm_names = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 320 | svm_region_init_chroot (chroot_path); |
| 321 | root_rp = svm_get_root_rp (); |
| 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 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 342 | clib_memset (&a, 0, sizeof (a)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 343 | a.root_path = chroot_path; |
| 344 | a.name = (char *) svm_names[i]; |
| 345 | fformat (stdout, "Checking %s region...\n", a.name); |
| 346 | rp = svm_existing_region_map_nolock (root_rp, &a); |
| 347 | if (rp) |
| 348 | { |
| 349 | svm_region_unmap (rp); |
| 350 | vec_free (svm_names[i]); |
| 351 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 353 | vec_free (svm_names); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 356 | void |
| 357 | repair (char *chroot_path, int crash_root_region) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 359 | svm_region_t *root_rp = 0; |
| 360 | svm_map_region_args_t *a = 0; |
| 361 | void *svm_map_region (svm_map_region_args_t * a); |
| 362 | int svm_fd; |
| 363 | u8 *shm_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 365 | fformat (stdout, "our pid: %d\n", getpid ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 367 | vec_validate (a, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 369 | a->root_path = chroot_path; |
| 370 | a->name = SVM_GLOBAL_REGION_NAME; |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 371 | a->baseva = svm_get_global_region_base_va (); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 372 | a->size = SVM_GLOBAL_REGION_SIZE; |
| 373 | a->flags = SVM_FLAGS_NODATA; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 375 | shm_name = shm_name_from_svm_map_region_args (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 377 | svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 379 | if (svm_fd < 0) |
| 380 | { |
| 381 | perror ("svm_region_map(mmap open)"); |
| 382 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 385 | vec_free (shm_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 387 | root_rp = mmap (0, MMAP_PAGESIZE, |
| 388 | PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 390 | if (root_rp == (svm_region_t *) MAP_FAILED) |
| 391 | { |
| 392 | close (svm_fd); |
| 393 | clib_warning ("mmap"); |
| 394 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 397 | /* Remap now that the region has been placed */ |
| 398 | clib_warning ("remap to 0x%x", root_rp->virtual_base); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 400 | a->baseva = root_rp->virtual_base; |
| 401 | a->size = root_rp->virtual_size; |
| 402 | munmap (root_rp, MMAP_PAGESIZE); |
| 403 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 404 | root_rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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...", |
| 424 | root_rp->mutex_owner_pid, root_rp->mutex_owner_tag); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 425 | clib_memset (&root_rp->mutex, 0, sizeof (root_rp->mutex)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 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 (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 452 | int |
| 453 | main (int argc, char **argv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 455 | unformat_input_t input; |
| 456 | int parsed = 0; |
| 457 | char *name; |
| 458 | char *chroot_path = 0; |
| 459 | u8 *chroot_u8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 461 | unformat_init_command_line (&input, argv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 463 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 464 | { |
| 465 | if (unformat (&input, "show-verbose")) |
| 466 | { |
| 467 | show (chroot_path, 1); |
| 468 | parsed++; |
| 469 | } |
| 470 | else if (unformat (&input, "show")) |
| 471 | { |
| 472 | show (chroot_path, 0); |
| 473 | parsed++; |
| 474 | } |
| 475 | else if (unformat (&input, "client-scan")) |
| 476 | { |
| 477 | svm_client_scan (chroot_path); |
| 478 | parsed++; |
| 479 | } |
| 480 | else if (unformat (&input, "repair")) |
| 481 | { |
| 482 | repair (chroot_path, 0 /* fix it */ ); |
| 483 | parsed++; |
| 484 | } |
| 485 | else if (unformat (&input, "crash")) |
| 486 | { |
| 487 | repair (chroot_path, 1 /* crash it */ ); |
| 488 | parsed++; |
| 489 | } |
| 490 | else if (unformat (&input, "trace-on %s", &name)) |
| 491 | { |
| 492 | trace (chroot_path, name, 1); |
| 493 | parsed++; |
| 494 | } |
| 495 | else if (unformat (&input, "trace-off %s", &name)) |
| 496 | { |
| 497 | trace (chroot_path, name, 0); |
| 498 | parsed++; |
| 499 | } |
| 500 | else if (unformat (&input, "chroot %s", &chroot_u8)) |
| 501 | { |
| 502 | chroot_path = (char *) chroot_u8; |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | break; |
| 507 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 510 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 512 | if (!parsed) |
| 513 | { |
| 514 | fformat (stdout, |
| 515 | "%s: show | show-verbose | client-scan | trace-on <region-name>\n", |
| 516 | argv[0]); |
| 517 | fformat (stdout, " trace-off <region-name>\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 519 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * fd.io coding-style-patch-verification: ON |
| 524 | * |
| 525 | * Local Variables: |
| 526 | * eval: (c-set-style "gnu") |
| 527 | * End: |
| 528 | */ |