blob: 59b582ecc4279d93ce6168d78a01a38e136c9a7a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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:
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>
36#include <vppinfra/mheap.h>
37#include <vppinfra/heap.h>
38#include <vppinfra/pool.h>
39#include <vppinfra/format.h>
40
Dave Barach68b0fb02017-02-28 15:15:56 -050041#ifndef MMAP_PAGESIZE
42#define MMAP_PAGESIZE (clib_mem_get_page_size())
43#endif
44
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#define SSVM_N_OPAQUE 7
46
Florin Corasb384b542018-01-15 01:08:33 -080047typedef enum ssvm_segment_type_
48{
49 SSVM_SEGMENT_SHM = 0,
50 SSVM_SEGMENT_MEMFD,
Florin Corasa332c462018-01-31 06:52:17 -080051 SSVM_SEGMENT_PRIVATE,
Florin Corasb384b542018-01-15 01:08:33 -080052 SSVM_N_SEGMENT_TYPES /**< Private segments */
53} ssvm_segment_type_t;
54
Dave Barach8a7fb0c2016-07-08 14:44:23 -040055typedef struct
56{
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 /* Spin-lock */
58 volatile u32 lock;
59 volatile u32 owner_pid;
60 int recursion_count;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040061 u32 tag; /* for debugging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63 /* The allocation arena */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040064 void *heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
66 /* Segment must be mapped at this address, or no supper */
David Johnsond9818dd2018-12-14 14:53:41 -050067 uword ssvm_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 /* The actual mmap size */
David Johnsond9818dd2018-12-14 14:53:41 -050069 uword ssvm_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 u32 master_pid;
71 u32 slave_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040072 u8 *name;
73 void *opaque[SSVM_N_OPAQUE];
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 /* Set when the master application thinks it's time to make the donuts */
76 volatile u32 ready;
77
Florin Corasb384b542018-01-15 01:08:33 -080078 ssvm_segment_type_t type;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079} ssvm_shared_header_t;
80
Dave Barach8a7fb0c2016-07-08 14:44:23 -040081typedef struct
82{
83 ssvm_shared_header_t *sh;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 u64 ssvm_size;
85 u32 my_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040086 u8 *name;
Christophe Fontainefef15b42016-04-09 12:38:49 +090087 uword requested_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 int i_am_master;
Florin Coras4d9b9d82018-01-14 12:25:50 -080089
Florin Corasb384b542018-01-15 01:08:33 -080090 union
91 {
92 int fd; /**< memfd segments */
93 int attach_timeout; /**< shm segments attach timeout (sec) */
94 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070095} ssvm_private_t;
96
Dave Barach8a7fb0c2016-07-08 14:44:23 -040097always_inline void
98ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
100 if (h->owner_pid == my_pid)
101 {
102 h->recursion_count++;
103 return;
104 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000106 while (clib_atomic_test_and_set (&h->lock))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 ;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400108
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 h->owner_pid = my_pid;
110 h->recursion_count = 1;
111 h->tag = tag;
112}
113
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400114always_inline void
Dave Barach2c25a622017-06-26 11:35:07 -0400115ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
116{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000117 while (clib_atomic_test_and_set (&h->lock))
Dave Barach2c25a622017-06-26 11:35:07 -0400118 ;
119
120 h->tag = tag;
121}
122
123always_inline void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400124ssvm_unlock (ssvm_shared_header_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
126 if (--h->recursion_count == 0)
127 {
128 h->owner_pid = 0;
129 h->tag = 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400130 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 h->lock = 0;
132 }
133}
134
Dave Barach2c25a622017-06-26 11:35:07 -0400135always_inline void
136ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
137{
138 h->tag = 0;
139 CLIB_MEMORY_BARRIER ();
140 h->lock = 0;
141}
142
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400143static inline void *
144ssvm_push_heap (ssvm_shared_header_t * sh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400146 u8 *oldheap;
147 oldheap = clib_mem_set_heap (sh->heap);
148 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149}
150
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400151static inline void
152ssvm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400154 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155}
156
Florin Corasb384b542018-01-15 01:08:33 -0800157static inline void *
158ssvm_mem_alloc (ssvm_private_t * ssvm, uword size)
159{
160 u8 *oldheap;
161 void *rv;
162
163 oldheap = clib_mem_set_heap (ssvm->sh->heap);
164 rv = clib_mem_alloc (size);
165 clib_mem_set_heap (oldheap);
166 return (rv);
167}
168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169#define foreach_ssvm_api_error \
Dave Barach68b0fb02017-02-28 15:15:56 -0500170_(NO_NAME, "No shared segment name", -100) \
171_(NO_SIZE, "Size not set (master)", -101) \
172_(CREATE_FAILURE, "Create failed", -102) \
173_(SET_SIZE, "Set size failed", -103) \
174_(MMAP, "mmap failed", -104) \
175_(SLAVE_TIMEOUT, "Slave map timeout", -105)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400177typedef enum
178{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179#define _(n,s,c) SSVM_API_ERROR_##n = c,
180 foreach_ssvm_api_error
181#undef _
182} ssvm_api_error_enum_t;
183
184#define SSVM_API_ERROR_NO_NAME (-10)
185
Florin Corasb384b542018-01-15 01:08:33 -0800186int ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
187int ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500188void ssvm_delete (ssvm_private_t * ssvm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Florin Corasb384b542018-01-15 01:08:33 -0800190int ssvm_master_init_shm (ssvm_private_t * ssvm);
191int ssvm_slave_init_shm (ssvm_private_t * ssvm);
192void ssvm_delete_shm (ssvm_private_t * ssvm);
193
194int ssvm_master_init_memfd (ssvm_private_t * memfd);
Florin Coras4d9b9d82018-01-14 12:25:50 -0800195int ssvm_slave_init_memfd (ssvm_private_t * memfd);
Florin Corasd3e83a92018-01-16 02:40:18 -0800196void ssvm_delete_memfd (ssvm_private_t * memfd);
Florin Coras4d9b9d82018-01-14 12:25:50 -0800197
Florin Corasa332c462018-01-31 06:52:17 -0800198int ssvm_master_init_private (ssvm_private_t * ssvm);
199int ssvm_slave_init_private (ssvm_private_t * ssvm);
200void ssvm_delete_private (ssvm_private_t * ssvm);
201
Florin Corasb384b542018-01-15 01:08:33 -0800202ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm);
203u8 *ssvm_name (const ssvm_private_t * ssvm);
204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205#endif /* __included_ssvm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400206
207/*
208 * fd.io coding-style-patch-verification: ON
209 *
210 * Local Variables:
211 * eval: (c-set-style "gnu")
212 * End:
213 */