Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-2019 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | { |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 31 | int ssvm_fd; |
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 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 80 | page_size = clib_mem_get_fd_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; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 90 | mapa.numa_node = ssvm->numa; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 91 | if (clib_mem_vm_ext_map (&mapa)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
| 93 | clib_unix_warning ("mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 94 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | return SSVM_API_ERROR_MMAP; |
| 96 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 97 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 99 | sh = mapa.addr; |
Benoît Ganne | 77d42fc | 2020-04-20 09:52:39 +0200 | [diff] [blame] | 100 | CLIB_MEM_UNPOISON (sh, sizeof (*sh)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | sh->master_pid = ssvm->my_pid; |
| 102 | sh->ssvm_size = ssvm->ssvm_size; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 103 | sh->ssvm_va = pointer_to_uword (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 104 | sh->type = SSVM_SEGMENT_SHM; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 105 | sh->heap = create_mspace_with_base (((u8 *) sh) + page_size, |
| 106 | ssvm->ssvm_size - page_size, |
| 107 | 1 /* locked */ ); |
| 108 | mspace_disable_expand (sh->heap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
| 110 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 111 | sh->name = format (0, "%s", ssvm->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | ssvm_pop_heap (oldheap); |
| 113 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 114 | ssvm->sh = sh; |
| 115 | ssvm->my_pid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | ssvm->i_am_master = 1; |
| 117 | |
| 118 | /* The application has to set set sh->ready... */ |
| 119 | return 0; |
| 120 | } |
| 121 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 122 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 123 | ssvm_slave_init_shm (ssvm_private_t * ssvm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 125 | struct stat stat; |
| 126 | int ssvm_fd = -1; |
| 127 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 129 | ASSERT (vec_c_string_is_terminated (ssvm->name)); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 130 | ssvm->i_am_master = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 132 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 133 | { |
| 134 | if (ssvm_fd < 0) |
| 135 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777); |
| 136 | if (ssvm_fd < 0) |
| 137 | { |
| 138 | sleep (1); |
| 139 | continue; |
| 140 | } |
| 141 | if (fstat (ssvm_fd, &stat) < 0) |
| 142 | { |
| 143 | sleep (1); |
| 144 | continue; |
| 145 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 147 | if (stat.st_size > 0) |
| 148 | goto map_it; |
| 149 | } |
| 150 | clib_warning ("slave timeout"); |
| 151 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 152 | |
| 153 | map_it: |
| 154 | sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 155 | ssvm_fd, 0); |
| 156 | if (sh == MAP_FAILED) |
| 157 | { |
| 158 | clib_unix_warning ("slave research mmap"); |
| 159 | close (ssvm_fd); |
| 160 | return SSVM_API_ERROR_MMAP; |
| 161 | } |
| 162 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 163 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 164 | { |
| 165 | if (sh->ready) |
| 166 | goto re_map_it; |
| 167 | } |
| 168 | close (ssvm_fd); |
| 169 | munmap (sh, MMAP_PAGESIZE); |
| 170 | clib_warning ("slave timeout 2"); |
| 171 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 172 | |
| 173 | re_map_it: |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 174 | ssvm->requested_va = sh->ssvm_va; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 175 | ssvm->ssvm_size = sh->ssvm_size; |
| 176 | munmap (sh, MMAP_PAGESIZE); |
| 177 | |
| 178 | sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size, |
| 179 | PROT_READ | PROT_WRITE, |
| 180 | MAP_SHARED | MAP_FIXED, ssvm_fd, 0); |
| 181 | |
| 182 | if (sh == MAP_FAILED) |
| 183 | { |
| 184 | clib_unix_warning ("slave final mmap"); |
| 185 | close (ssvm_fd); |
| 186 | return SSVM_API_ERROR_MMAP; |
| 187 | } |
| 188 | sh->slave_pid = getpid (); |
| 189 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 191 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 193 | ssvm_delete_shm (ssvm_private_t * ssvm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 194 | { |
| 195 | u8 *fn; |
| 196 | |
| 197 | fn = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
| 198 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 199 | if (CLIB_DEBUG > 1) |
| 200 | clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (), |
| 201 | ssvm->name, fn); |
| 202 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 203 | /* Throw away the backing file */ |
| 204 | if (unlink ((char *) fn) < 0) |
| 205 | clib_unix_warning ("unlink segment '%s'", ssvm->name); |
| 206 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 207 | vec_free (fn); |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 208 | vec_free (ssvm->name); |
| 209 | |
Florin Coras | 6898e5c | 2019-07-01 08:57:42 -0700 | [diff] [blame] | 210 | munmap ((void *) ssvm->sh, ssvm->ssvm_size); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Initialize memfd segment master |
| 215 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 216 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 217 | ssvm_master_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 218 | { |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 219 | uword page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 220 | ssvm_shared_header_t *sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 221 | void *oldheap; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 222 | clib_mem_vm_alloc_t alloc = { 0 }; |
| 223 | clib_error_t *err; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 224 | |
| 225 | if (memfd->ssvm_size == 0) |
| 226 | return SSVM_API_ERROR_NO_SIZE; |
| 227 | |
| 228 | ASSERT (vec_c_string_is_terminated (memfd->name)); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 229 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 230 | alloc.name = (char *) memfd->name; |
| 231 | alloc.size = memfd->ssvm_size; |
| 232 | alloc.flags = CLIB_MEM_VM_F_SHARED; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 233 | alloc.requested_va = memfd->requested_va; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 234 | if (memfd->numa) |
| 235 | { |
| 236 | alloc.numa_node = memfd->numa; |
| 237 | alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER; |
| 238 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 239 | if ((err = clib_mem_vm_ext_alloc (&alloc))) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 240 | { |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 241 | clib_error_report (err); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 242 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 243 | } |
| 244 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 245 | memfd->fd = alloc.fd; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 246 | memfd->sh = (ssvm_shared_header_t *) alloc.addr; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 247 | memfd->my_pid = getpid (); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 248 | memfd->i_am_master = 1; |
| 249 | |
Haiyang Tan | a5ab503 | 2018-10-15 06:17:55 -0700 | [diff] [blame] | 250 | page_size = 1ull << alloc.log2_page_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 251 | sh = memfd->sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 252 | sh->master_pid = memfd->my_pid; |
| 253 | sh->ssvm_size = memfd->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 254 | sh->ssvm_va = pointer_to_uword (sh); |
| 255 | sh->type = SSVM_SEGMENT_MEMFD; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 256 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 257 | sh->heap = create_mspace_with_base (((u8 *) sh) + page_size, |
| 258 | memfd->ssvm_size - page_size, |
| 259 | 1 /* locked */ ); |
| 260 | mspace_disable_expand (sh->heap); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 261 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 262 | sh->name = format (0, "%s", memfd->name, 0); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 263 | ssvm_pop_heap (oldheap); |
| 264 | |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 265 | /* The application has to set set sh->ready... */ |
| 266 | return 0; |
| 267 | } |
| 268 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Initialize memfd segment slave |
| 271 | * |
| 272 | * Subtly different than svm_slave_init. The caller needs to acquire |
| 273 | * a usable file descriptor for the memfd segment e.g. via |
| 274 | * vppinfra/socket.c:default_socket_recvmsg |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 275 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 276 | int |
| 277 | ssvm_slave_init_memfd (ssvm_private_t * memfd) |
| 278 | { |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 279 | clib_mem_vm_map_t mapa = { 0 }; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 280 | ssvm_shared_header_t *sh; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 281 | uword page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 282 | |
| 283 | memfd->i_am_master = 0; |
| 284 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 285 | page_size = clib_mem_get_fd_page_size (memfd->fd); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 286 | if (!page_size) |
| 287 | { |
| 288 | clib_unix_warning ("page size unknown"); |
| 289 | return SSVM_API_ERROR_MMAP; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Map the segment once, to look at the shared header |
| 294 | */ |
| 295 | mapa.fd = memfd->fd; |
| 296 | mapa.size = page_size; |
| 297 | |
| 298 | if (clib_mem_vm_ext_map (&mapa)) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 299 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 300 | clib_unix_warning ("slave research mmap (fd %d)", mapa.fd); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 301 | close (memfd->fd); |
| 302 | return SSVM_API_ERROR_MMAP; |
| 303 | } |
| 304 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 305 | sh = mapa.addr; |
| 306 | memfd->requested_va = sh->ssvm_va; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 307 | memfd->ssvm_size = sh->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 308 | clib_mem_vm_free (sh, page_size); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 309 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 310 | /* |
| 311 | * Remap the segment at the 'right' address |
| 312 | */ |
| 313 | mapa.requested_va = memfd->requested_va; |
| 314 | mapa.size = memfd->ssvm_size; |
| 315 | if (clib_mem_vm_ext_map (&mapa)) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 316 | { |
| 317 | clib_unix_warning ("slave final mmap"); |
| 318 | close (memfd->fd); |
| 319 | return SSVM_API_ERROR_MMAP; |
| 320 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 321 | |
| 322 | sh = mapa.addr; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 323 | sh->slave_pid = getpid (); |
| 324 | memfd->sh = sh; |
| 325 | return 0; |
| 326 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 328 | void |
| 329 | ssvm_delete_memfd (ssvm_private_t * memfd) |
| 330 | { |
| 331 | vec_free (memfd->name); |
| 332 | clib_mem_vm_free (memfd->sh, memfd->ssvm_size); |
| 333 | close (memfd->fd); |
| 334 | } |
| 335 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 336 | /** |
| 337 | * Initialize segment in a private heap |
| 338 | */ |
| 339 | int |
| 340 | ssvm_master_init_private (ssvm_private_t * ssvm) |
| 341 | { |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 342 | uword pagesize = clib_mem_get_page_size (), rnd_size = 0; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 343 | clib_mem_vm_alloc_t alloc = { 0 }; |
| 344 | struct dlmallinfo dlminfo; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 345 | ssvm_shared_header_t *sh; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 346 | clib_error_t *err; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 347 | u8 *heap; |
| 348 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 349 | rnd_size = clib_max (ssvm->ssvm_size + (pagesize - 1), ssvm->ssvm_size); |
| 350 | rnd_size &= ~(pagesize - 1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 351 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 352 | alloc.name = (char *) ssvm->name; |
| 353 | alloc.size = rnd_size + pagesize; |
| 354 | if (ssvm->numa) |
| 355 | { |
| 356 | alloc.numa_node = ssvm->numa; |
| 357 | alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER; |
| 358 | } |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 359 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 360 | if ((err = clib_mem_vm_ext_alloc (&alloc))) |
| 361 | { |
| 362 | clib_error_report (err); |
| 363 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 364 | } |
| 365 | |
| 366 | heap = create_mspace_with_base ((u8 *) alloc.addr + pagesize, rnd_size, |
| 367 | 1 /* locked */ ); |
Benoît Ganne | 0af49a8 | 2019-07-12 11:34:16 +0200 | [diff] [blame] | 368 | if (heap == 0) |
| 369 | { |
| 370 | clib_unix_warning ("mheap alloc"); |
| 371 | return -1; |
| 372 | } |
| 373 | |
Florin Coras | f0b68a5 | 2019-06-10 16:22:48 -0700 | [diff] [blame] | 374 | mspace_disable_expand (heap); |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 375 | |
| 376 | /* Find actual size because mspace size is rounded up by dlmalloc */ |
Florin Coras | f8461bf | 2019-11-07 17:00:15 -0800 | [diff] [blame] | 377 | dlminfo = mspace_mallinfo (heap); |
| 378 | rnd_size = dlminfo.fordblks; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 379 | |
| 380 | ssvm->ssvm_size = rnd_size; |
| 381 | ssvm->i_am_master = 1; |
| 382 | ssvm->my_pid = getpid (); |
| 383 | ssvm->requested_va = ~0; |
| 384 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 385 | /* First page in allocated memory is set aside for the shared header */ |
| 386 | sh = alloc.addr; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 387 | ssvm->sh = sh; |
| 388 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 389 | clib_memset (sh, 0, sizeof (*sh)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 390 | sh->heap = heap; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 391 | sh->ssvm_size = rnd_size; |
Florin Coras | 404b8a3 | 2019-05-09 12:08:06 -0700 | [diff] [blame] | 392 | sh->ssvm_va = pointer_to_uword (heap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 393 | sh->type = SSVM_SEGMENT_PRIVATE; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 394 | sh->name = ssvm->name; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | int |
| 400 | ssvm_slave_init_private (ssvm_private_t * ssvm) |
| 401 | { |
| 402 | clib_warning ("BUG: this should not be called!"); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | ssvm_delete_private (ssvm_private_t * ssvm) |
| 408 | { |
| 409 | vec_free (ssvm->name); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 410 | destroy_mspace (ssvm->sh->heap); |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 411 | clib_mem_vm_free (ssvm->sh, ssvm->ssvm_size + clib_mem_get_page_size ()); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 414 | int |
| 415 | ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
| 416 | { |
| 417 | return (master_init_fns[type]) (ssvm); |
| 418 | } |
| 419 | |
| 420 | int |
| 421 | ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
| 422 | { |
| 423 | return (slave_init_fns[type]) (ssvm); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | ssvm_delete (ssvm_private_t * ssvm) |
| 428 | { |
| 429 | delete_fns[ssvm->sh->type] (ssvm); |
| 430 | } |
| 431 | |
| 432 | ssvm_segment_type_t |
| 433 | ssvm_type (const ssvm_private_t * ssvm) |
| 434 | { |
| 435 | return ssvm->sh->type; |
| 436 | } |
| 437 | |
| 438 | u8 * |
| 439 | ssvm_name (const ssvm_private_t * ssvm) |
| 440 | { |
| 441 | return ssvm->sh->name; |
| 442 | } |
| 443 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 444 | /* |
| 445 | * fd.io coding-style-patch-verification: ON |
| 446 | * |
| 447 | * Local Variables: |
| 448 | * eval: (c-set-style "gnu") |
| 449 | * End: |
| 450 | */ |