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 | |
Benoît Ganne | 77d42fc | 2020-04-20 09:52:39 +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 (); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 186 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 188 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 189 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 190 | ssvm_delete_shm (ssvm_private_t * ssvm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 191 | { |
| 192 | u8 *fn; |
| 193 | |
| 194 | fn = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
| 195 | |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 196 | if (CLIB_DEBUG > 1) |
| 197 | clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (), |
| 198 | ssvm->name, fn); |
| 199 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 200 | /* Throw away the backing file */ |
| 201 | if (unlink ((char *) fn) < 0) |
| 202 | clib_unix_warning ("unlink segment '%s'", ssvm->name); |
| 203 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | vec_free (fn); |
Dave Wallace | d756b35 | 2017-07-03 13:11:38 -0400 | [diff] [blame] | 205 | vec_free (ssvm->name); |
| 206 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 207 | if (ssvm->is_server) |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 208 | clib_mem_vm_unmap (ssvm->sh); |
| 209 | else |
| 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 | /** |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 214 | * Initialize memfd segment server |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 215 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 216 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 217 | ssvm_server_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 218 | { |
Florin Coras | bf693ff | 2020-10-22 11:59:55 -0700 | [diff] [blame] | 219 | uword page_size, n_pages; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 220 | ssvm_shared_header_t *sh; |
Florin Coras | bf693ff | 2020-10-22 11:59:55 -0700 | [diff] [blame] | 221 | int log2_page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 222 | void *oldheap; |
| 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 | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 229 | memfd->fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, |
| 230 | (char *) memfd->name); |
| 231 | |
| 232 | if (memfd->fd == CLIB_MEM_ERROR) |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 233 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 234 | clib_unix_warning ("failed to create memfd"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 235 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 236 | } |
| 237 | |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 238 | log2_page_size = clib_mem_get_fd_log2_page_size (memfd->fd); |
| 239 | if (log2_page_size == 0) |
| 240 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 241 | clib_unix_warning ("cannot determine page size"); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 242 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 243 | } |
| 244 | |
| 245 | n_pages = ((memfd->ssvm_size - 1) >> log2_page_size) + 1; |
| 246 | |
| 247 | if ((ftruncate (memfd->fd, n_pages << log2_page_size)) == -1) |
| 248 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 249 | clib_unix_warning ("memfd ftruncate failure"); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 250 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 251 | } |
| 252 | |
| 253 | sh = clib_mem_vm_map_shared (uword_to_pointer (memfd->requested_va, void *), |
| 254 | memfd->ssvm_size, memfd->fd, 0, |
| 255 | (char *) memfd->name); |
| 256 | if (sh == CLIB_MEM_VM_MAP_FAILED) |
| 257 | { |
| 258 | clib_unix_warning ("memfd map (fd %d)", memfd->fd); |
| 259 | close (memfd->fd); |
| 260 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 261 | } |
| 262 | |
| 263 | memfd->sh = sh; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 264 | memfd->my_pid = getpid (); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 265 | memfd->is_server = 1; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 266 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 267 | sh->server_pid = memfd->my_pid; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 268 | sh->ssvm_size = memfd->ssvm_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 269 | sh->ssvm_va = pointer_to_uword (sh); |
| 270 | sh->type = SSVM_SEGMENT_MEMFD; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 271 | |
Florin Coras | 927eee4 | 2020-10-12 19:08:22 -0700 | [diff] [blame] | 272 | page_size = 1ULL << log2_page_size; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 273 | sh->heap = clib_mem_create_heap (((u8 *) sh) + page_size, |
| 274 | memfd->ssvm_size - page_size, |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 275 | 1 /* locked */ , "ssvm server memfd"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 276 | oldheap = ssvm_push_heap (sh); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 277 | sh->name = format (0, "%s", memfd->name, 0); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 278 | ssvm_pop_heap (oldheap); |
| 279 | |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 280 | /* The application has to set set sh->ready... */ |
| 281 | return 0; |
| 282 | } |
| 283 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 284 | /** |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 285 | * Initialize memfd segment client |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 286 | * |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 287 | * Subtly different than svm_client_init. The caller needs to acquire |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 288 | * a usable file descriptor for the memfd segment e.g. via |
| 289 | * vppinfra/socket.c:default_socket_recvmsg |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 290 | */ |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 291 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 292 | ssvm_client_init_memfd (ssvm_private_t * memfd) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 293 | { |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 294 | int mmap_flags = MAP_SHARED; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 295 | ssvm_shared_header_t *sh; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 296 | uword page_size; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 297 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 298 | memfd->is_server = 0; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 299 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 300 | page_size = clib_mem_get_fd_page_size (memfd->fd); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 301 | if (!page_size) |
| 302 | { |
| 303 | clib_unix_warning ("page size unknown"); |
| 304 | return SSVM_API_ERROR_MMAP; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Map the segment once, to look at the shared header |
| 309 | */ |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 310 | sh = (void *) mmap (0, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 311 | memfd->fd, 0); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 312 | |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 313 | if (sh == MAP_FAILED) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 314 | { |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 315 | clib_unix_warning ("client research mmap (fd %d)", memfd->fd); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 316 | close (memfd->fd); |
| 317 | return SSVM_API_ERROR_MMAP; |
| 318 | } |
| 319 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 320 | memfd->requested_va = sh->ssvm_va; |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 321 | memfd->ssvm_size = sh->ssvm_size; |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 322 | munmap (sh, page_size); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 323 | |
Florin Coras | 4da0ea9 | 2021-01-05 23:15:49 -0800 | [diff] [blame] | 324 | if (memfd->requested_va) |
| 325 | mmap_flags |= MAP_FIXED; |
| 326 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 327 | /* |
| 328 | * Remap the segment at the 'right' address |
| 329 | */ |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 330 | sh = (void *) mmap (uword_to_pointer (memfd->requested_va, void *), |
| 331 | memfd->ssvm_size, |
| 332 | PROT_READ | PROT_WRITE, mmap_flags, memfd->fd, 0); |
| 333 | |
| 334 | if (sh == MAP_FAILED) |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 335 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 336 | clib_unix_warning ("client final mmap"); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 337 | close (memfd->fd); |
| 338 | return SSVM_API_ERROR_MMAP; |
| 339 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 340 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 341 | sh->client_pid = getpid (); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 342 | memfd->sh = sh; |
| 343 | return 0; |
| 344 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 346 | void |
| 347 | ssvm_delete_memfd (ssvm_private_t * memfd) |
| 348 | { |
| 349 | vec_free (memfd->name); |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 350 | if (memfd->is_server) |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 351 | clib_mem_vm_unmap (memfd->sh); |
| 352 | else |
Florin Coras | 459a0c4 | 2020-09-30 08:13:44 -0700 | [diff] [blame] | 353 | munmap (memfd->sh, memfd->ssvm_size); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 354 | close (memfd->fd); |
| 355 | } |
| 356 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 357 | /** |
| 358 | * Initialize segment in a private heap |
| 359 | */ |
| 360 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 361 | ssvm_server_init_private (ssvm_private_t * ssvm) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 362 | { |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 363 | uword page_size, log2_page_size, rnd_size = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 364 | ssvm_shared_header_t *sh; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 365 | clib_mem_heap_t *heap, *oldheap; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 366 | |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 367 | log2_page_size = clib_mem_get_log2_page_size (); |
| 368 | if (log2_page_size == 0) |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 369 | { |
jiangxiaoming | c9e9b97 | 2020-10-10 17:32:11 +0800 | [diff] [blame] | 370 | clib_unix_warning ("cannot determine page size"); |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 371 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 372 | } |
| 373 | |
Florin Coras | 927eee4 | 2020-10-12 19:08:22 -0700 | [diff] [blame] | 374 | page_size = 1ULL << log2_page_size; |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 375 | rnd_size = clib_max (ssvm->ssvm_size + (page_size - 1), ssvm->ssvm_size); |
| 376 | rnd_size &= ~(page_size - 1); |
| 377 | |
| 378 | sh = clib_mem_vm_map (0, rnd_size + page_size, log2_page_size, |
| 379 | (char *) ssvm->name); |
| 380 | if (sh == CLIB_MEM_VM_MAP_FAILED) |
| 381 | { |
| 382 | clib_unix_warning ("private map failed"); |
| 383 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 384 | } |
| 385 | |
| 386 | heap = clib_mem_create_heap ((u8 *) sh + page_size, rnd_size, |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 387 | 1 /* locked */ , "ssvm server private"); |
Benoît Ganne | 0af49a8 | 2019-07-12 11:34:16 +0200 | [diff] [blame] | 388 | if (heap == 0) |
| 389 | { |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 390 | clib_unix_warning ("heap alloc"); |
Benoît Ganne | 0af49a8 | 2019-07-12 11:34:16 +0200 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 394 | rnd_size = clib_mem_get_heap_free_space (heap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 395 | |
| 396 | ssvm->ssvm_size = rnd_size; |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 397 | ssvm->is_server = 1; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 398 | ssvm->my_pid = getpid (); |
| 399 | ssvm->requested_va = ~0; |
| 400 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 401 | /* First page in allocated memory is set aside for the shared header */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 402 | ssvm->sh = sh; |
| 403 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 404 | clib_memset (sh, 0, sizeof (*sh)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 405 | sh->heap = heap; |
Florin Coras | 4453c09 | 2019-12-19 10:13:15 -0800 | [diff] [blame] | 406 | sh->ssvm_size = rnd_size; |
Florin Coras | d2fb601 | 2020-12-07 18:35:12 -0800 | [diff] [blame] | 407 | sh->ssvm_va = pointer_to_uword (sh); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 408 | sh->type = SSVM_SEGMENT_PRIVATE; |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 409 | |
| 410 | oldheap = ssvm_push_heap (sh); |
| 411 | sh->name = format (0, "%s", ssvm->name, 0); |
| 412 | ssvm_pop_heap (oldheap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 418 | ssvm_client_init_private (ssvm_private_t * ssvm) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 419 | { |
| 420 | clib_warning ("BUG: this should not be called!"); |
| 421 | return -1; |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | ssvm_delete_private (ssvm_private_t * ssvm) |
| 426 | { |
| 427 | vec_free (ssvm->name); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 428 | clib_mem_destroy_heap (ssvm->sh->heap); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 429 | clib_mem_vm_unmap (ssvm->sh); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 432 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 433 | ssvm_server_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 434 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 435 | return (server_init_fns[type]) (ssvm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | int |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 439 | ssvm_client_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 440 | { |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 441 | return (client_init_fns[type]) (ssvm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void |
| 445 | ssvm_delete (ssvm_private_t * ssvm) |
| 446 | { |
| 447 | delete_fns[ssvm->sh->type] (ssvm); |
| 448 | } |
| 449 | |
| 450 | ssvm_segment_type_t |
| 451 | ssvm_type (const ssvm_private_t * ssvm) |
| 452 | { |
| 453 | return ssvm->sh->type; |
| 454 | } |
| 455 | |
| 456 | u8 * |
| 457 | ssvm_name (const ssvm_private_t * ssvm) |
| 458 | { |
| 459 | return ssvm->sh->name; |
| 460 | } |
| 461 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 462 | /* |
| 463 | * fd.io coding-style-patch-verification: ON |
| 464 | * |
| 465 | * Local Variables: |
| 466 | * eval: (c-set-style "gnu") |
| 467 | * End: |
| 468 | */ |