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 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 21 | static init_fn server_init_fns[SSVM_N_SEGMENT_TYPES] = |
| 22 | { ssvm_server_init_shm, ssvm_server_init_memfd, ssvm_server_init_private }; |
| 23 | static init_fn client_init_fns[SSVM_N_SEGMENT_TYPES] = |
| 24 | { ssvm_client_init_shm, ssvm_client_init_memfd, ssvm_client_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 | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 29 | ssvm_server_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 | u8 junk = 0, *ssvm_filename; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 33 | ssvm_shared_header_t *sh; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 34 | uword page_size, requested_va = 0; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 35 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | |
| 37 | if (ssvm->ssvm_size == 0) |
| 38 | return SSVM_API_ERROR_NO_SIZE; |
| 39 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 40 | if (CLIB_DEBUG > 1) |
| 41 | clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name); |
| 42 | |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 43 | ASSERT (vec_c_string_is_terminated (ssvm->name)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | unlink ((char *) ssvm_filename); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 46 | vec_free (ssvm_filename); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 48 | 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] | 49 | if (ssvm_fd < 0) |
| 50 | { |
| 51 | clib_unix_warning ("create segment '%s'", ssvm->name); |
| 52 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 53 | } |
| 54 | |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 55 | if (fchmod (ssvm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) |
| 56 | clib_unix_warning ("ssvm segment chmod"); |
Florin Coras | 65784c1 | 2018-07-04 04:17:41 -0700 | [diff] [blame] | 57 | if (svm_get_root_rp ()) |
| 58 | { |
| 59 | /* TODO: is this really needed? */ |
| 60 | svm_main_region_t *smr = svm_get_root_rp ()->data_base; |
| 61 | if (fchown (ssvm_fd, smr->uid, smr->gid) < 0) |
| 62 | clib_unix_warning ("ssvm segment chown"); |
| 63 | } |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 64 | |
Dave Barach | 56faee8 | 2016-08-04 18:58:05 -0400 | [diff] [blame] | 65 | if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0) |
| 66 | { |
| 67 | clib_unix_warning ("lseek"); |
| 68 | close (ssvm_fd); |
| 69 | return SSVM_API_ERROR_SET_SIZE; |
| 70 | } |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 71 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 72 | if (write (ssvm_fd, &junk, 1) != 1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
| 74 | clib_unix_warning ("set ssvm size"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 75 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | return SSVM_API_ERROR_SET_SIZE; |
| 77 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 78 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 79 | page_size = clib_mem_get_fd_page_size (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | if (ssvm->requested_va) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 81 | { |
| 82 | requested_va = ssvm->requested_va; |
| 83 | clib_mem_vm_randomize_va (&requested_va, min_log2 (page_size)); |
| 84 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 86 | sh = clib_mem_vm_map_shared (uword_to_pointer (requested_va, void *), |
| 87 | ssvm->ssvm_size, ssvm_fd, 0, |
| 88 | (char *) ssvm->name); |
| 89 | if (sh == CLIB_MEM_VM_MAP_FAILED) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
| 91 | clib_unix_warning ("mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 92 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | return SSVM_API_ERROR_MMAP; |
| 94 | } |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 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 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 98 | clib_mem_unpoison (sh, sizeof (*sh)); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 99 | sh->server_pid = ssvm->my_pid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 103 | sh->heap = clib_mem_create_heap (((u8 *) sh) + page_size, |
| 104 | ssvm->ssvm_size - page_size, |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 105 | 1 /* locked */ , "ssvm server shm"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
| 107 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 108 | sh->name = format (0, "%s", ssvm->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | ssvm_pop_heap (oldheap); |
| 110 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 111 | ssvm->sh = sh; |
| 112 | ssvm->my_pid = getpid (); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 113 | ssvm->is_server = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
| 115 | /* The application has to set set sh->ready... */ |
| 116 | return 0; |
| 117 | } |
| 118 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 119 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 120 | ssvm_client_init_shm (ssvm_private_t * ssvm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 122 | struct stat stat; |
| 123 | int ssvm_fd = -1; |
| 124 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 126 | ASSERT (vec_c_string_is_terminated (ssvm->name)); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 127 | ssvm->is_server = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 129 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 130 | { |
| 131 | if (ssvm_fd < 0) |
| 132 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777); |
| 133 | if (ssvm_fd < 0) |
| 134 | { |
| 135 | sleep (1); |
| 136 | continue; |
| 137 | } |
| 138 | if (fstat (ssvm_fd, &stat) < 0) |
| 139 | { |
| 140 | sleep (1); |
| 141 | continue; |
| 142 | } |
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 | if (stat.st_size > 0) |
| 145 | goto map_it; |
| 146 | } |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 147 | clib_warning ("client timeout"); |
| 148 | return SSVM_API_ERROR_CLIENT_TIMEOUT; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 149 | |
| 150 | map_it: |
| 151 | sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 152 | ssvm_fd, 0); |
| 153 | if (sh == MAP_FAILED) |
| 154 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 155 | clib_unix_warning ("client research mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 156 | close (ssvm_fd); |
| 157 | return SSVM_API_ERROR_MMAP; |
| 158 | } |
| 159 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 160 | while (ssvm->attach_timeout-- > 0) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 161 | { |
| 162 | if (sh->ready) |
| 163 | goto re_map_it; |
| 164 | } |
| 165 | close (ssvm_fd); |
| 166 | munmap (sh, MMAP_PAGESIZE); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 167 | clib_warning ("client timeout 2"); |
| 168 | return SSVM_API_ERROR_CLIENT_TIMEOUT; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 169 | |
| 170 | re_map_it: |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 171 | ssvm->requested_va = sh->ssvm_va; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 172 | ssvm->ssvm_size = sh->ssvm_size; |
| 173 | munmap (sh, MMAP_PAGESIZE); |
| 174 | |
| 175 | sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size, |
| 176 | PROT_READ | PROT_WRITE, |
| 177 | MAP_SHARED | MAP_FIXED, ssvm_fd, 0); |
| 178 | |
| 179 | if (sh == MAP_FAILED) |
| 180 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 181 | clib_unix_warning ("client final mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 182 | close (ssvm_fd); |
| 183 | return SSVM_API_ERROR_MMAP; |
| 184 | } |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 185 | sh->client_pid = getpid (); |
root | 5e06e4f | 2022-04-01 09:09:05 +0000 | [diff] [blame] | 186 | close (ssvm_fd); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 187 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 189 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 191 | ssvm_delete_shm (ssvm_private_t * ssvm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | { |
| 193 | u8 *fn; |
| 194 | |
| 195 | fn = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
| 196 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 197 | if (CLIB_DEBUG > 1) |
| 198 | clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (), |
| 199 | ssvm->name, fn); |
| 200 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | /* Throw away the backing file */ |
| 202 | if (unlink ((char *) fn) < 0) |
| 203 | clib_unix_warning ("unlink segment '%s'", ssvm->name); |
| 204 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 205 | vec_free (fn); |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 206 | vec_free (ssvm->name); |
| 207 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 208 | if (ssvm->is_server) |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 209 | clib_mem_vm_unmap (ssvm->sh); |
| 210 | else |
| 211 | munmap ((void *) ssvm->sh, ssvm->ssvm_size); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 214 | /** |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 215 | * Initialize memfd segment server |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 216 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 217 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 218 | ssvm_server_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 219 | { |
Florin Coras | bf693ff | 2020-10-22 11:59:55 -0700 | [diff] [blame] | 220 | uword page_size, n_pages; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 221 | ssvm_shared_header_t *sh; |
Florin Coras | bf693ff | 2020-10-22 11:59:55 -0700 | [diff] [blame] | 222 | int log2_page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 223 | void *oldheap; |
| 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 | |
Junfeng Wang | c795b88 | 2022-08-12 16:24:46 +0800 | [diff] [blame] | 230 | if (memfd->huge_page) |
| 231 | memfd->fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT_HUGE, |
| 232 | (char *) memfd->name); |
| 233 | else |
| 234 | memfd->fd = |
| 235 | clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, (char *) memfd->name); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 236 | |
| 237 | if (memfd->fd == CLIB_MEM_ERROR) |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 238 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 239 | clib_unix_warning ("failed to create memfd"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 240 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 241 | } |
| 242 | |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 243 | log2_page_size = clib_mem_get_fd_log2_page_size (memfd->fd); |
| 244 | if (log2_page_size == 0) |
| 245 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 246 | clib_unix_warning ("cannot determine page size"); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 247 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 248 | } |
| 249 | |
| 250 | n_pages = ((memfd->ssvm_size - 1) >> log2_page_size) + 1; |
| 251 | |
| 252 | if ((ftruncate (memfd->fd, n_pages << log2_page_size)) == -1) |
| 253 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 254 | clib_unix_warning ("memfd ftruncate failure"); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 255 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 256 | } |
| 257 | |
| 258 | sh = clib_mem_vm_map_shared (uword_to_pointer (memfd->requested_va, void *), |
| 259 | memfd->ssvm_size, memfd->fd, 0, |
| 260 | (char *) memfd->name); |
| 261 | if (sh == CLIB_MEM_VM_MAP_FAILED) |
| 262 | { |
| 263 | clib_unix_warning ("memfd map (fd %d)", memfd->fd); |
| 264 | close (memfd->fd); |
| 265 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 266 | } |
| 267 | |
| 268 | memfd->sh = sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 269 | memfd->my_pid = getpid (); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 270 | memfd->is_server = 1; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 271 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 272 | sh->server_pid = memfd->my_pid; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 273 | sh->ssvm_size = memfd->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 274 | sh->ssvm_va = pointer_to_uword (sh); |
| 275 | sh->type = SSVM_SEGMENT_MEMFD; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 276 | |
Junfeng Wang | c795b88 | 2022-08-12 16:24:46 +0800 | [diff] [blame] | 277 | page_size = clib_mem_get_page_size (); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 278 | sh->heap = clib_mem_create_heap (((u8 *) sh) + page_size, |
| 279 | memfd->ssvm_size - page_size, |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 280 | 1 /* locked */ , "ssvm server memfd"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 281 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 282 | sh->name = format (0, "%s", memfd->name, 0); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 283 | ssvm_pop_heap (oldheap); |
| 284 | |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 285 | /* The application has to set set sh->ready... */ |
| 286 | return 0; |
| 287 | } |
| 288 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 289 | /** |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 290 | * Initialize memfd segment client |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 291 | * |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 292 | * Subtly different than svm_client_init. The caller needs to acquire |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 293 | * a usable file descriptor for the memfd segment e.g. via |
| 294 | * vppinfra/socket.c:default_socket_recvmsg |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 295 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 296 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 297 | ssvm_client_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 298 | { |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 299 | int mmap_flags = MAP_SHARED; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 300 | ssvm_shared_header_t *sh; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 301 | uword page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 302 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 303 | memfd->is_server = 0; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 304 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 305 | page_size = clib_mem_get_fd_page_size (memfd->fd); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 306 | if (!page_size) |
| 307 | { |
| 308 | clib_unix_warning ("page size unknown"); |
| 309 | return SSVM_API_ERROR_MMAP; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Map the segment once, to look at the shared header |
| 314 | */ |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 315 | sh = (void *) mmap (0, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 316 | memfd->fd, 0); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 317 | |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 318 | if (sh == MAP_FAILED) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 319 | { |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 320 | clib_unix_warning ("client research mmap (fd %d)", memfd->fd); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 321 | close (memfd->fd); |
| 322 | return SSVM_API_ERROR_MMAP; |
| 323 | } |
| 324 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 325 | memfd->requested_va = sh->ssvm_va; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 326 | memfd->ssvm_size = sh->ssvm_size; |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 327 | munmap (sh, page_size); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 328 | |
Florin Coras | 4da0ea9 | 2021-01-05 23:15:49 -0800 | [diff] [blame] | 329 | if (memfd->requested_va) |
| 330 | mmap_flags |= MAP_FIXED; |
| 331 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 332 | /* |
| 333 | * Remap the segment at the 'right' address |
| 334 | */ |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 335 | sh = (void *) mmap (uword_to_pointer (memfd->requested_va, void *), |
| 336 | memfd->ssvm_size, |
| 337 | PROT_READ | PROT_WRITE, mmap_flags, memfd->fd, 0); |
| 338 | |
| 339 | if (sh == MAP_FAILED) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 340 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 341 | clib_unix_warning ("client final mmap"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 342 | close (memfd->fd); |
| 343 | return SSVM_API_ERROR_MMAP; |
| 344 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 345 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 346 | sh->client_pid = getpid (); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 347 | memfd->sh = sh; |
| 348 | return 0; |
| 349 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 350 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 351 | void |
| 352 | ssvm_delete_memfd (ssvm_private_t * memfd) |
| 353 | { |
| 354 | vec_free (memfd->name); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 355 | if (memfd->is_server) |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 356 | clib_mem_vm_unmap (memfd->sh); |
| 357 | else |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 358 | munmap (memfd->sh, memfd->ssvm_size); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 359 | close (memfd->fd); |
| 360 | } |
| 361 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 362 | /** |
| 363 | * Initialize segment in a private heap |
| 364 | */ |
| 365 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 366 | ssvm_server_init_private (ssvm_private_t * ssvm) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 367 | { |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 368 | uword page_size, log2_page_size, rnd_size = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 369 | ssvm_shared_header_t *sh; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 370 | clib_mem_heap_t *heap, *oldheap; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 371 | |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 372 | log2_page_size = clib_mem_get_log2_page_size (); |
| 373 | if (log2_page_size == 0) |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 374 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 375 | clib_unix_warning ("cannot determine page size"); |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 376 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 377 | } |
| 378 | |
Florin Coras | 927eee4 | 2020-10-12 19:08:22 -0700 | [diff] [blame] | 379 | page_size = 1ULL << log2_page_size; |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 380 | rnd_size = clib_max (ssvm->ssvm_size + (page_size - 1), ssvm->ssvm_size); |
| 381 | rnd_size &= ~(page_size - 1); |
| 382 | |
| 383 | sh = clib_mem_vm_map (0, rnd_size + page_size, log2_page_size, |
| 384 | (char *) ssvm->name); |
| 385 | if (sh == CLIB_MEM_VM_MAP_FAILED) |
| 386 | { |
| 387 | clib_unix_warning ("private map failed"); |
| 388 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 389 | } |
| 390 | |
| 391 | heap = clib_mem_create_heap ((u8 *) sh + page_size, rnd_size, |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 392 | 1 /* locked */ , "ssvm server private"); |
Benoît Ganne | 0af49a8 | 2019-07-12 11:34:16 +0200 | [diff] [blame] | 393 | if (heap == 0) |
| 394 | { |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 395 | clib_unix_warning ("heap alloc"); |
Benoît Ganne | 0af49a8 | 2019-07-12 11:34:16 +0200 | [diff] [blame] | 396 | return -1; |
| 397 | } |
| 398 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 399 | rnd_size = clib_mem_get_heap_free_space (heap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 400 | |
| 401 | ssvm->ssvm_size = rnd_size; |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 402 | ssvm->is_server = 1; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 403 | ssvm->my_pid = getpid (); |
| 404 | ssvm->requested_va = ~0; |
| 405 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 406 | /* First page in allocated memory is set aside for the shared header */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 407 | ssvm->sh = sh; |
| 408 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 409 | clib_memset (sh, 0, sizeof (*sh)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 410 | sh->heap = heap; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 411 | sh->ssvm_size = rnd_size; |
Florin Coras | d2fb601 | 2020-12-07 18:35:12 -0800 | [diff] [blame] | 412 | sh->ssvm_va = pointer_to_uword (sh); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 413 | sh->type = SSVM_SEGMENT_PRIVATE; |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 414 | |
| 415 | oldheap = ssvm_push_heap (sh); |
| 416 | sh->name = format (0, "%s", ssvm->name, 0); |
| 417 | ssvm_pop_heap (oldheap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 423 | ssvm_client_init_private (ssvm_private_t * ssvm) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 424 | { |
| 425 | clib_warning ("BUG: this should not be called!"); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | ssvm_delete_private (ssvm_private_t * ssvm) |
| 431 | { |
| 432 | vec_free (ssvm->name); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 433 | clib_mem_destroy_heap (ssvm->sh->heap); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 434 | clib_mem_vm_unmap (ssvm->sh); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 437 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 438 | ssvm_server_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 439 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 440 | return (server_init_fns[type]) (ssvm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 444 | ssvm_client_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 445 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 446 | return (client_init_fns[type]) (ssvm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void |
| 450 | ssvm_delete (ssvm_private_t * ssvm) |
| 451 | { |
| 452 | delete_fns[ssvm->sh->type] (ssvm); |
| 453 | } |
| 454 | |
| 455 | ssvm_segment_type_t |
| 456 | ssvm_type (const ssvm_private_t * ssvm) |
| 457 | { |
| 458 | return ssvm->sh->type; |
| 459 | } |
| 460 | |
| 461 | u8 * |
| 462 | ssvm_name (const ssvm_private_t * ssvm) |
| 463 | { |
| 464 | return ssvm->sh->name; |
| 465 | } |
| 466 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 467 | /* |
| 468 | * fd.io coding-style-patch-verification: ON |
| 469 | * |
| 470 | * Local Variables: |
| 471 | * eval: (c-set-style "gnu") |
| 472 | * End: |
| 473 | */ |