session svm: support for segments larger than 4GB
[vpp.git] / src / svm / ssvm.h
1 /*
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:
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 #include <vppinfra/lock.h>
41
42 #ifndef MMAP_PAGESIZE
43 #define MMAP_PAGESIZE (clib_mem_get_page_size())
44 #endif
45
46 #define SSVM_N_OPAQUE 7
47
48 typedef enum ssvm_segment_type_
49 {
50   SSVM_SEGMENT_SHM = 0,
51   SSVM_SEGMENT_MEMFD,
52   SSVM_SEGMENT_PRIVATE,
53   SSVM_N_SEGMENT_TYPES          /**< Private segments */
54 } ssvm_segment_type_t;
55
56 typedef struct
57 {
58   /* Spin-lock */
59   volatile u32 lock;
60   volatile u32 owner_pid;
61   int recursion_count;
62   u32 tag;                      /* for debugging */
63
64   /* The allocation arena */
65   void *heap;
66
67   /* Segment must be mapped at this address, or no supper */
68   uword ssvm_va;
69   /* The actual mmap size */
70   uword ssvm_size;
71   u32 master_pid;
72   u32 slave_pid;
73   u8 *name;
74   void *opaque[SSVM_N_OPAQUE];
75
76   /* Set when the master application thinks it's time to make the donuts */
77   volatile u32 ready;
78
79   ssvm_segment_type_t type;
80 } ssvm_shared_header_t;
81
82 typedef struct
83 {
84   ssvm_shared_header_t *sh;
85   uword ssvm_size;
86   u32 my_pid;
87   u8 *name;
88   uword requested_va;
89   int i_am_master;
90
91   union
92   {
93     int fd;                     /**< memfd segments */
94     int attach_timeout;         /**< shm segments attach timeout (sec) */
95   };
96 } ssvm_private_t;
97
98 always_inline void
99 ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
100 {
101   if (h->owner_pid == my_pid)
102     {
103       h->recursion_count++;
104       return;
105     }
106
107   while (clib_atomic_test_and_set (&h->lock))
108     CLIB_PAUSE ();
109
110   h->owner_pid = my_pid;
111   h->recursion_count = 1;
112   h->tag = tag;
113 }
114
115 always_inline void
116 ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
117 {
118   while (clib_atomic_test_and_set (&h->lock))
119     CLIB_PAUSE ();
120
121   h->tag = tag;
122 }
123
124 always_inline void
125 ssvm_unlock (ssvm_shared_header_t * h)
126 {
127   if (--h->recursion_count == 0)
128     {
129       h->owner_pid = 0;
130       h->tag = 0;
131       clib_atomic_release (&h->lock);
132     }
133 }
134
135 always_inline void
136 ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
137 {
138   h->tag = 0;
139   clib_atomic_release (&h->lock);
140 }
141
142 static inline void *
143 ssvm_push_heap (ssvm_shared_header_t * sh)
144 {
145   u8 *oldheap;
146   oldheap = clib_mem_set_heap (sh->heap);
147   return ((void *) oldheap);
148 }
149
150 static inline void
151 ssvm_pop_heap (void *oldheap)
152 {
153   clib_mem_set_heap (oldheap);
154 }
155
156 static inline void *
157 ssvm_mem_alloc (ssvm_private_t * ssvm, uword size)
158 {
159   u8 *oldheap;
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
168 #define foreach_ssvm_api_error                  \
169 _(NO_NAME, "No shared segment name", -100)      \
170 _(NO_SIZE, "Size not set (master)", -101)       \
171 _(CREATE_FAILURE, "Create failed", -102)        \
172 _(SET_SIZE, "Set size failed", -103)            \
173 _(MMAP, "mmap failed", -104)                    \
174 _(SLAVE_TIMEOUT, "Slave map timeout", -105)
175
176 typedef enum
177 {
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
185 int ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
186 int ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
187 void ssvm_delete (ssvm_private_t * ssvm);
188
189 int ssvm_master_init_shm (ssvm_private_t * ssvm);
190 int ssvm_slave_init_shm (ssvm_private_t * ssvm);
191 void ssvm_delete_shm (ssvm_private_t * ssvm);
192
193 int ssvm_master_init_memfd (ssvm_private_t * memfd);
194 int ssvm_slave_init_memfd (ssvm_private_t * memfd);
195 void ssvm_delete_memfd (ssvm_private_t * memfd);
196
197 int ssvm_master_init_private (ssvm_private_t * ssvm);
198 int ssvm_slave_init_private (ssvm_private_t * ssvm);
199 void ssvm_delete_private (ssvm_private_t * ssvm);
200
201 ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm);
202 u8 *ssvm_name (const ssvm_private_t * ssvm);
203
204 #endif /* __included_ssvm_h__ */
205
206 /*
207  * fd.io coding-style-patch-verification: ON
208  *
209  * Local Variables:
210  * eval: (c-set-style "gnu")
211  * End:
212  */