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 | */ |
| 15 | #ifndef __included_ssvm_h__ |
| 16 | #define __included_ssvm_h__ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <signal.h> |
| 25 | #include <pthread.h> |
| 26 | #include <unistd.h> |
| 27 | #include <time.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <string.h> |
| 30 | #include <vppinfra/clib.h> |
| 31 | #include <vppinfra/vec.h> |
| 32 | #include <vppinfra/hash.h> |
| 33 | #include <vppinfra/bitmap.h> |
| 34 | #include <vppinfra/fifo.h> |
| 35 | #include <vppinfra/time.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | #include <vppinfra/heap.h> |
| 37 | #include <vppinfra/pool.h> |
| 38 | #include <vppinfra/format.h> |
jaszha03 | 0455c43 | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 39 | #include <vppinfra/lock.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 41 | #ifndef MMAP_PAGESIZE |
| 42 | #define MMAP_PAGESIZE (clib_mem_get_page_size()) |
| 43 | #endif |
| 44 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #define SSVM_N_OPAQUE 7 |
| 46 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 47 | typedef enum ssvm_segment_type_ |
| 48 | { |
| 49 | SSVM_SEGMENT_SHM = 0, |
| 50 | SSVM_SEGMENT_MEMFD, |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 51 | SSVM_SEGMENT_PRIVATE, |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 52 | SSVM_N_SEGMENT_TYPES /**< Private segments */ |
| 53 | } ssvm_segment_type_t; |
| 54 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 55 | typedef struct |
| 56 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | /* Spin-lock */ |
| 58 | volatile u32 lock; |
| 59 | volatile u32 owner_pid; |
| 60 | int recursion_count; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 61 | u32 tag; /* for debugging */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
| 63 | /* The allocation arena */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 64 | void *heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
| 66 | /* Segment must be mapped at this address, or no supper */ |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 67 | uword ssvm_va; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | /* The actual mmap size */ |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 69 | uword ssvm_size; |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 70 | u32 server_pid; |
| 71 | u32 client_pid; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 72 | u8 *name; |
| 73 | void *opaque[SSVM_N_OPAQUE]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 75 | /* Set when server init done */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | volatile u32 ready; |
| 77 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 78 | ssvm_segment_type_t type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | } ssvm_shared_header_t; |
| 80 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 81 | typedef struct |
| 82 | { |
| 83 | ssvm_shared_header_t *sh; |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 84 | uword ssvm_size; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 85 | uword requested_va; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | u32 my_pid; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 87 | u8 *name; |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 88 | u8 numa; /**< UNUSED: numa requested at alloc time */ |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 89 | int is_server; |
Junfeng Wang | c795b88 | 2022-08-12 16:24:46 +0800 | [diff] [blame^] | 90 | int huge_page; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 91 | union |
| 92 | { |
| 93 | int fd; /**< memfd segments */ |
| 94 | int attach_timeout; /**< shm segments attach timeout (sec) */ |
| 95 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } ssvm_private_t; |
| 97 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 98 | always_inline void |
| 99 | ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | { |
| 101 | if (h->owner_pid == my_pid) |
| 102 | { |
| 103 | h->recursion_count++; |
| 104 | return; |
| 105 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 106 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 107 | while (clib_atomic_test_and_set (&h->lock)) |
jaszha03 | 0455c43 | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 108 | CLIB_PAUSE (); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 109 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | h->owner_pid = my_pid; |
| 111 | h->recursion_count = 1; |
| 112 | h->tag = tag; |
| 113 | } |
| 114 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 115 | always_inline void |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 116 | ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag) |
| 117 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 118 | while (clib_atomic_test_and_set (&h->lock)) |
jaszha03 | 0455c43 | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 119 | CLIB_PAUSE (); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 120 | |
| 121 | h->tag = tag; |
| 122 | } |
| 123 | |
| 124 | always_inline void |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 125 | ssvm_unlock (ssvm_shared_header_t * h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
| 127 | if (--h->recursion_count == 0) |
| 128 | { |
| 129 | h->owner_pid = 0; |
| 130 | h->tag = 0; |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 131 | clib_atomic_release (&h->lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 135 | always_inline void |
| 136 | ssvm_unlock_non_recursive (ssvm_shared_header_t * h) |
| 137 | { |
| 138 | h->tag = 0; |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 139 | clib_atomic_release (&h->lock); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | static inline void * |
| 143 | ssvm_push_heap (ssvm_shared_header_t * sh) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 145 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 146 | oldheap = clib_mem_set_heap (sh->heap); |
| 147 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 150 | static inline void |
| 151 | ssvm_pop_heap (void *oldheap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 153 | clib_mem_set_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 156 | static inline void * |
| 157 | ssvm_mem_alloc (ssvm_private_t * ssvm, uword size) |
| 158 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 159 | clib_mem_heap_t *oldheap; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 160 | void *rv; |
| 161 | |
| 162 | oldheap = clib_mem_set_heap (ssvm->sh->heap); |
| 163 | rv = clib_mem_alloc (size); |
| 164 | clib_mem_set_heap (oldheap); |
| 165 | return (rv); |
| 166 | } |
| 167 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | #define foreach_ssvm_api_error \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 169 | _(NO_NAME, "No shared segment name", -100) \ |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 170 | _(NO_SIZE, "Size not set (server)", -101) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 171 | _(CREATE_FAILURE, "Create failed", -102) \ |
| 172 | _(SET_SIZE, "Set size failed", -103) \ |
| 173 | _(MMAP, "mmap failed", -104) \ |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 174 | _(CLIENT_TIMEOUT, "Client map timeout", -105) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 176 | typedef enum |
| 177 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | #define _(n,s,c) SSVM_API_ERROR_##n = c, |
| 179 | foreach_ssvm_api_error |
| 180 | #undef _ |
| 181 | } ssvm_api_error_enum_t; |
| 182 | |
| 183 | #define SSVM_API_ERROR_NO_NAME (-10) |
| 184 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 185 | int ssvm_server_init (ssvm_private_t * ssvm, ssvm_segment_type_t type); |
| 186 | int ssvm_client_init (ssvm_private_t * ssvm, ssvm_segment_type_t type); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 187 | void ssvm_delete (ssvm_private_t * ssvm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 189 | int ssvm_server_init_shm (ssvm_private_t * ssvm); |
| 190 | int ssvm_client_init_shm (ssvm_private_t * ssvm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 191 | void ssvm_delete_shm (ssvm_private_t * ssvm); |
| 192 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 193 | int ssvm_server_init_memfd (ssvm_private_t * memfd); |
| 194 | int ssvm_client_init_memfd (ssvm_private_t * memfd); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 195 | void ssvm_delete_memfd (ssvm_private_t * memfd); |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 196 | |
Florin Coras | 5220a26 | 2020-09-29 18:11:24 -0700 | [diff] [blame] | 197 | int ssvm_server_init_private (ssvm_private_t * ssvm); |
| 198 | int ssvm_client_init_private (ssvm_private_t * ssvm); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 199 | void ssvm_delete_private (ssvm_private_t * ssvm); |
| 200 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 201 | ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm); |
| 202 | u8 *ssvm_name (const ssvm_private_t * ssvm); |
| 203 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | #endif /* __included_ssvm_h__ */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * fd.io coding-style-patch-verification: ON |
| 208 | * |
| 209 | * Local Variables: |
| 210 | * eval: (c-set-style "gnu") |
| 211 | * End: |
| 212 | */ |