2 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_ssvm_h__
16 #define __included_ssvm_h__
20 #include <sys/types.h>
23 #include <netinet/in.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>
36 #include <vppinfra/heap.h>
37 #include <vppinfra/pool.h>
38 #include <vppinfra/format.h>
39 #include <vppinfra/lock.h>
42 #define MMAP_PAGESIZE (clib_mem_get_page_size())
45 #define SSVM_N_OPAQUE 7
47 typedef enum ssvm_segment_type_
52 SSVM_N_SEGMENT_TYPES /**< Private segments */
53 } ssvm_segment_type_t;
59 volatile u32 owner_pid;
61 u32 tag; /* for debugging */
63 /* The allocation arena */
66 /* Segment must be mapped at this address, or no supper */
68 /* The actual mmap size */
73 void *opaque[SSVM_N_OPAQUE];
75 /* Set when server init done */
78 ssvm_segment_type_t type;
79 } ssvm_shared_header_t;
83 ssvm_shared_header_t *sh;
88 u8 numa; /**< UNUSED: numa requested at alloc time */
93 int fd; /**< memfd segments */
94 int attach_timeout; /**< shm segments attach timeout (sec) */
99 ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
101 if (h->owner_pid == my_pid)
103 h->recursion_count++;
107 while (clib_atomic_test_and_set (&h->lock))
110 h->owner_pid = my_pid;
111 h->recursion_count = 1;
116 ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
118 while (clib_atomic_test_and_set (&h->lock))
125 ssvm_unlock (ssvm_shared_header_t * h)
127 if (--h->recursion_count == 0)
131 clib_atomic_release (&h->lock);
136 ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
139 clib_atomic_release (&h->lock);
143 ssvm_push_heap (ssvm_shared_header_t * sh)
145 clib_mem_heap_t *oldheap;
146 oldheap = clib_mem_set_heap (sh->heap);
147 return ((void *) oldheap);
151 ssvm_pop_heap (void *oldheap)
153 clib_mem_set_heap (oldheap);
157 ssvm_mem_alloc (ssvm_private_t * ssvm, uword size)
159 clib_mem_heap_t *oldheap;
162 oldheap = clib_mem_set_heap (ssvm->sh->heap);
163 rv = clib_mem_alloc (size);
164 clib_mem_set_heap (oldheap);
168 #define foreach_ssvm_api_error \
169 _(NO_NAME, "No shared segment name", -100) \
170 _(NO_SIZE, "Size not set (server)", -101) \
171 _(CREATE_FAILURE, "Create failed", -102) \
172 _(SET_SIZE, "Set size failed", -103) \
173 _(MMAP, "mmap failed", -104) \
174 _(CLIENT_TIMEOUT, "Client map timeout", -105)
178 #define _(n,s,c) SSVM_API_ERROR_##n = c,
179 foreach_ssvm_api_error
181 } ssvm_api_error_enum_t;
183 #define SSVM_API_ERROR_NO_NAME (-10)
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);
187 void ssvm_delete (ssvm_private_t * ssvm);
189 int ssvm_server_init_shm (ssvm_private_t * ssvm);
190 int ssvm_client_init_shm (ssvm_private_t * ssvm);
191 void ssvm_delete_shm (ssvm_private_t * ssvm);
193 int ssvm_server_init_memfd (ssvm_private_t * memfd);
194 int ssvm_client_init_memfd (ssvm_private_t * memfd);
195 void ssvm_delete_memfd (ssvm_private_t * memfd);
197 int ssvm_server_init_private (ssvm_private_t * ssvm);
198 int ssvm_client_init_private (ssvm_private_t * ssvm);
199 void ssvm_delete_private (ssvm_private_t * ssvm);
201 ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm);
202 u8 *ssvm_name (const ssvm_private_t * ssvm);
204 #endif /* __included_ssvm_h__ */
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")