Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 15 | #include <svm/ssvm.h> |
| 16 | #include <svm/svm_common.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 18 | typedef int (*init_fn) (ssvm_private_t *); |
| 19 | typedef void (*delete_fn) (ssvm_private_t *); |
| 20 | |
| 21 | static init_fn master_init_fns[SSVM_N_SEGMENT_TYPES] = |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 22 | { ssvm_master_init_shm, ssvm_master_init_memfd, ssvm_master_init_private }; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 23 | static init_fn slave_init_fns[SSVM_N_SEGMENT_TYPES] = |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 24 | { ssvm_slave_init_shm, ssvm_slave_init_memfd, ssvm_slave_init_private }; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 25 | static delete_fn delete_fns[SSVM_N_SEGMENT_TYPES] = |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 26 | { ssvm_delete_shm, ssvm_delete_memfd, ssvm_delete_private }; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 27 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 28 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 29 | ssvm_master_init_shm (ssvm_private_t * ssvm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 31 | int ssvm_fd, mh_flags = MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 32 | clib_mem_vm_map_t mapa = { 0 }; |
| 33 | u8 junk = 0, *ssvm_filename; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 34 | ssvm_shared_header_t *sh; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 35 | uword page_size, requested_va = 0; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 36 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
| 38 | if (ssvm->ssvm_size == 0) |
| 39 | return SSVM_API_ERROR_NO_SIZE; |
| 40 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 41 | if (CLIB_DEBUG > 1) |
| 42 | clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name); |
| 43 | |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 44 | ASSERT (vec_c_string_is_terminated (ssvm->name)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | unlink ((char *) ssvm_filename); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 47 | vec_free (ssvm_filename); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 49 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | if (ssvm_fd < 0) |
| 51 | { |
| 52 | clib_unix_warning ("create segment '%s'", ssvm->name); |
| 53 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 54 | } |
| 55 | |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 56 | if (fchmod (ssvm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) |
| 57 | clib_unix_warning ("ssvm segment chmod"); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 58 | if (svm_get_root_rp ()) |
| 59 | { |
| 60 | /* TODO: is this really needed? */ |
| 61 | svm_main_region_t *smr = svm_get_root_rp ()->data_base; |
| 62 | if (fchown (ssvm_fd, smr->uid, smr->gid) < 0) |
| 63 | clib_unix_warning ("ssvm segment chown"); |
| 64 | } |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 65 | |
Dave Barach | 56faee8 | 2016-08-04 18:58:05 -0400 | [diff] [blame] | 66 | if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0) |
| 67 | { |
| 68 | clib_unix_warning ("lseek"); |
| 69 | close (ssvm_fd); |
| 70 | return SSVM_API_ERROR_SET_SIZE; |
| 71 | } |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 72 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 73 | if (write (ssvm_fd, &junk, 1) != 1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
| 75 | clib_unix_warning ("set ssvm size"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 76 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | return SSVM_API_ERROR_SET_SIZE; |
| 78 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 79 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 80 | page_size = clib_mem_vm_get_page_size (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | if (ssvm->requested_va) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 82 | { |
| 83 | requested_va = ssvm->requested_va; |
| 84 | clib_mem_vm_randomize_va (&requested_va, min_log2 (page_size)); |
| 85 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 87 | mapa.requested_va = requested_va; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 88 | mapa.size = ssvm->ssvm_size; |
| 89 | mapa.fd = ssvm_fd; |
| 90 | if (clib_mem_vm_ext_map (&mapa)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | { |
| 92 | clib_unix_warning ("mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 93 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | return SSVM_API_ERROR_MMAP; |
| 95 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 96 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 98 | sh = mapa.addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | sh->master_pid = ssvm->my_pid; |
| 100 | sh->ssvm_size = ssvm->ssvm_size; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 101 | sh->ssvm_va = pointer_to_uword (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 102 | sh->type = SSVM_SEGMENT_SHM; |
| 103 | sh->heap = mheap_alloc_with_flags (((u8 *) sh) + page_size, |
| 104 | ssvm->ssvm_size - page_size, mh_flags); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
| 106 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 107 | sh->name = format (0, "%s", ssvm->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | ssvm_pop_heap (oldheap); |
| 109 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 110 | ssvm->sh = sh; |
| 111 | ssvm->my_pid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | ssvm->i_am_master = 1; |
| 113 | |
| 114 | /* The application has to set set sh->ready... */ |
| 115 | return 0; |
| 116 | } |
| 117 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 118 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 119 | ssvm_slave_init_shm (ssvm_private_t * ssvm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 121 | struct stat stat; |
| 122 | int ssvm_fd = -1; |
| 123 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 125 | ASSERT (vec_c_string_is_terminated (ssvm->name)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 126 | ssvm->i_am_master = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 128 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 129 | { |
| 130 | if (ssvm_fd < 0) |
| 131 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777); |
| 132 | if (ssvm_fd < 0) |
| 133 | { |
| 134 | sleep (1); |
| 135 | continue; |
| 136 | } |
| 137 | if (fstat (ssvm_fd, &stat) < 0) |
| 138 | { |
| 139 | sleep (1); |
| 140 | continue; |
| 141 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 143 | if (stat.st_size > 0) |
| 144 | goto map_it; |
| 145 | } |
| 146 | clib_warning ("slave timeout"); |
| 147 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 148 | |
| 149 | map_it: |
| 150 | sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 151 | ssvm_fd, 0); |
| 152 | if (sh == MAP_FAILED) |
| 153 | { |
| 154 | clib_unix_warning ("slave research mmap"); |
| 155 | close (ssvm_fd); |
| 156 | return SSVM_API_ERROR_MMAP; |
| 157 | } |
| 158 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 159 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 160 | { |
| 161 | if (sh->ready) |
| 162 | goto re_map_it; |
| 163 | } |
| 164 | close (ssvm_fd); |
| 165 | munmap (sh, MMAP_PAGESIZE); |
| 166 | clib_warning ("slave timeout 2"); |
| 167 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 168 | |
| 169 | re_map_it: |
| 170 | ssvm->requested_va = (u64) sh->ssvm_va; |
| 171 | ssvm->ssvm_size = sh->ssvm_size; |
| 172 | munmap (sh, MMAP_PAGESIZE); |
| 173 | |
| 174 | sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size, |
| 175 | PROT_READ | PROT_WRITE, |
| 176 | MAP_SHARED | MAP_FIXED, ssvm_fd, 0); |
| 177 | |
| 178 | if (sh == MAP_FAILED) |
| 179 | { |
| 180 | clib_unix_warning ("slave final mmap"); |
| 181 | close (ssvm_fd); |
| 182 | return SSVM_API_ERROR_MMAP; |
| 183 | } |
| 184 | sh->slave_pid = getpid (); |
| 185 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 187 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 188 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 189 | ssvm_delete_shm (ssvm_private_t * ssvm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | { |
| 191 | u8 *fn; |
| 192 | |
| 193 | fn = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
| 194 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 195 | if (CLIB_DEBUG > 1) |
| 196 | clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (), |
| 197 | ssvm->name, fn); |
| 198 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 199 | /* Throw away the backing file */ |
| 200 | if (unlink ((char *) fn) < 0) |
| 201 | clib_unix_warning ("unlink segment '%s'", ssvm->name); |
| 202 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 203 | vec_free (fn); |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 204 | vec_free (ssvm->name); |
| 205 | |
| 206 | munmap ((void *) ssvm->requested_va, ssvm->ssvm_size); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 209 | /** |
| 210 | * Initialize memfd segment master |
| 211 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 212 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 213 | ssvm_master_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 214 | { |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 215 | uword page_size, flags = MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 216 | ssvm_shared_header_t *sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 217 | void *oldheap; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 218 | clib_mem_vm_alloc_t alloc = { 0 }; |
| 219 | clib_error_t *err; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 220 | |
| 221 | if (memfd->ssvm_size == 0) |
| 222 | return SSVM_API_ERROR_NO_SIZE; |
| 223 | |
| 224 | ASSERT (vec_c_string_is_terminated (memfd->name)); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 225 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 226 | alloc.name = (char *) memfd->name; |
| 227 | alloc.size = memfd->ssvm_size; |
| 228 | alloc.flags = CLIB_MEM_VM_F_SHARED; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 229 | alloc.requested_va = memfd->requested_va; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 230 | if ((err = clib_mem_vm_ext_alloc (&alloc))) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 231 | { |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 232 | clib_error_report (err); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 233 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 234 | } |
| 235 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 236 | memfd->fd = alloc.fd; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 237 | memfd->sh = (ssvm_shared_header_t *) alloc.addr; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 238 | memfd->my_pid = getpid (); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 239 | memfd->i_am_master = 1; |
| 240 | |
| 241 | page_size = 1 << alloc.log2_page_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 242 | sh = memfd->sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 243 | sh->master_pid = memfd->my_pid; |
| 244 | sh->ssvm_size = memfd->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 245 | sh->ssvm_va = pointer_to_uword (sh); |
| 246 | sh->type = SSVM_SEGMENT_MEMFD; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 247 | sh->heap = mheap_alloc_with_flags (((u8 *) sh) + page_size, |
| 248 | memfd->ssvm_size - page_size, flags); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 249 | |
| 250 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 251 | sh->name = format (0, "%s", memfd->name, 0); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 252 | ssvm_pop_heap (oldheap); |
| 253 | |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 254 | /* The application has to set set sh->ready... */ |
| 255 | return 0; |
| 256 | } |
| 257 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Initialize memfd segment slave |
| 260 | * |
| 261 | * Subtly different than svm_slave_init. The caller needs to acquire |
| 262 | * a usable file descriptor for the memfd segment e.g. via |
| 263 | * vppinfra/socket.c:default_socket_recvmsg |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 264 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 265 | int |
| 266 | ssvm_slave_init_memfd (ssvm_private_t * memfd) |
| 267 | { |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 268 | clib_mem_vm_map_t mapa = { 0 }; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 269 | ssvm_shared_header_t *sh; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 270 | uword page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 271 | |
| 272 | memfd->i_am_master = 0; |
| 273 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 274 | page_size = clib_mem_vm_get_page_size (memfd->fd); |
| 275 | if (!page_size) |
| 276 | { |
| 277 | clib_unix_warning ("page size unknown"); |
| 278 | return SSVM_API_ERROR_MMAP; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Map the segment once, to look at the shared header |
| 283 | */ |
| 284 | mapa.fd = memfd->fd; |
| 285 | mapa.size = page_size; |
| 286 | |
| 287 | if (clib_mem_vm_ext_map (&mapa)) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 288 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 289 | clib_unix_warning ("slave research mmap (fd %d)", mapa.fd); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 290 | close (memfd->fd); |
| 291 | return SSVM_API_ERROR_MMAP; |
| 292 | } |
| 293 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 294 | sh = mapa.addr; |
| 295 | memfd->requested_va = sh->ssvm_va; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 296 | memfd->ssvm_size = sh->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 297 | clib_mem_vm_free (sh, page_size); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 298 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 299 | /* |
| 300 | * Remap the segment at the 'right' address |
| 301 | */ |
| 302 | mapa.requested_va = memfd->requested_va; |
| 303 | mapa.size = memfd->ssvm_size; |
| 304 | if (clib_mem_vm_ext_map (&mapa)) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 305 | { |
| 306 | clib_unix_warning ("slave final mmap"); |
| 307 | close (memfd->fd); |
| 308 | return SSVM_API_ERROR_MMAP; |
| 309 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 310 | |
| 311 | sh = mapa.addr; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 312 | sh->slave_pid = getpid (); |
| 313 | memfd->sh = sh; |
| 314 | return 0; |
| 315 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 317 | void |
| 318 | ssvm_delete_memfd (ssvm_private_t * memfd) |
| 319 | { |
| 320 | vec_free (memfd->name); |
| 321 | clib_mem_vm_free (memfd->sh, memfd->ssvm_size); |
| 322 | close (memfd->fd); |
| 323 | } |
| 324 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Initialize segment in a private heap |
| 327 | */ |
| 328 | int |
| 329 | ssvm_master_init_private (ssvm_private_t * ssvm) |
| 330 | { |
| 331 | u32 pagesize = clib_mem_get_page_size (); |
| 332 | ssvm_shared_header_t *sh; |
| 333 | mheap_t *heap_header; |
Florin Coras | 5da96a7 | 2018-07-12 01:45:13 -0700 | [diff] [blame] | 334 | u64 rnd_size = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 335 | u8 *heap; |
| 336 | |
Florin Coras | 5da96a7 | 2018-07-12 01:45:13 -0700 | [diff] [blame] | 337 | rnd_size = (ssvm->ssvm_size + (pagesize - 1)) & ~(pagesize - 1); |
| 338 | rnd_size = clib_min (rnd_size, ((u64) 1 << 32) - pagesize); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 339 | heap = mheap_alloc (0, rnd_size); |
| 340 | if (heap == 0) |
| 341 | { |
| 342 | clib_unix_warning ("mheap alloc"); |
| 343 | return -1; |
| 344 | } |
| 345 | heap_header = mheap_header (heap); |
| 346 | heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 347 | |
| 348 | ssvm->ssvm_size = rnd_size; |
| 349 | ssvm->i_am_master = 1; |
| 350 | ssvm->my_pid = getpid (); |
| 351 | ssvm->requested_va = ~0; |
| 352 | |
| 353 | /* Allocate a [sic] shared memory header, in process memory... */ |
| 354 | sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); |
| 355 | ssvm->sh = sh; |
| 356 | |
| 357 | memset (sh, 0, sizeof (*sh)); |
| 358 | sh->heap = heap; |
| 359 | sh->type = SSVM_SEGMENT_PRIVATE; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int |
| 365 | ssvm_slave_init_private (ssvm_private_t * ssvm) |
| 366 | { |
| 367 | clib_warning ("BUG: this should not be called!"); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | ssvm_delete_private (ssvm_private_t * ssvm) |
| 373 | { |
| 374 | vec_free (ssvm->name); |
| 375 | mheap_free (ssvm->sh->heap); |
| 376 | clib_mem_free (ssvm->sh); |
| 377 | } |
| 378 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 379 | int |
| 380 | ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
| 381 | { |
| 382 | return (master_init_fns[type]) (ssvm); |
| 383 | } |
| 384 | |
| 385 | int |
| 386 | ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
| 387 | { |
| 388 | return (slave_init_fns[type]) (ssvm); |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | ssvm_delete (ssvm_private_t * ssvm) |
| 393 | { |
| 394 | delete_fns[ssvm->sh->type] (ssvm); |
| 395 | } |
| 396 | |
| 397 | ssvm_segment_type_t |
| 398 | ssvm_type (const ssvm_private_t * ssvm) |
| 399 | { |
| 400 | return ssvm->sh->type; |
| 401 | } |
| 402 | |
| 403 | u8 * |
| 404 | ssvm_name (const ssvm_private_t * ssvm) |
| 405 | { |
| 406 | return ssvm->sh->name; |
| 407 | } |
| 408 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 409 | /* |
| 410 | * fd.io coding-style-patch-verification: ON |
| 411 | * |
| 412 | * Local Variables: |
| 413 | * eval: (c-set-style "gnu") |
| 414 | * End: |
| 415 | */ |