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