blob: 09e1707450bd4e612656defcd3d63f0663f7cc7a [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>
Florin Coras4d9b9d82018-01-14 12:25:50 -080040#include <vppinfra/linux/syscall.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
Dave Barach68b0fb02017-02-28 15:15:56 -050042#ifndef MMAP_PAGESIZE
43#define MMAP_PAGESIZE (clib_mem_get_page_size())
44#endif
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#define SSVM_N_OPAQUE 7
47
Florin Corasb384b542018-01-15 01:08:33 -080048typedef enum ssvm_segment_type_
49{
50 SSVM_SEGMENT_SHM = 0,
51 SSVM_SEGMENT_MEMFD,
Florin Corasa332c462018-01-31 06:52:17 -080052 SSVM_SEGMENT_PRIVATE,
Florin Corasb384b542018-01-15 01:08:33 -080053 SSVM_N_SEGMENT_TYPES /**< Private segments */
54} ssvm_segment_type_t;
55
Dave Barach8a7fb0c2016-07-08 14:44:23 -040056typedef struct
57{
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 /* Spin-lock */
59 volatile u32 lock;
60 volatile u32 owner_pid;
61 int recursion_count;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040062 u32 tag; /* for debugging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
64 /* The allocation arena */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040065 void *heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67 /* Segment must be mapped at this address, or no supper */
68 u64 ssvm_va;
69 /* The actual mmap size */
70 u64 ssvm_size;
71 u32 master_pid;
72 u32 slave_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040073 u8 *name;
74 void *opaque[SSVM_N_OPAQUE];
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
76 /* Set when the master application thinks it's time to make the donuts */
77 volatile u32 ready;
78
Florin Corasb384b542018-01-15 01:08:33 -080079 ssvm_segment_type_t type;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080} ssvm_shared_header_t;
81
Dave Barach8a7fb0c2016-07-08 14:44:23 -040082typedef struct
83{
84 ssvm_shared_header_t *sh;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 u64 ssvm_size;
86 u32 my_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040087 u8 *name;
Christophe Fontainefef15b42016-04-09 12:38:49 +090088 uword requested_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 int i_am_master;
Florin Coras4d9b9d82018-01-14 12:25:50 -080090
Florin Corasb384b542018-01-15 01:08:33 -080091 union
92 {
93 int fd; /**< memfd segments */
94 int attach_timeout; /**< shm segments attach timeout (sec) */
95 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070096} ssvm_private_t;
97
Dave Barach8a7fb0c2016-07-08 14:44:23 -040098always_inline void
99ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
101 if (h->owner_pid == my_pid)
102 {
103 h->recursion_count++;
104 return;
105 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400106
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 while (__sync_lock_test_and_set (&h->lock, 1))
108 ;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400109
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 h->owner_pid = my_pid;
111 h->recursion_count = 1;
112 h->tag = tag;
113}
114
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400115always_inline void
Dave Barach2c25a622017-06-26 11:35:07 -0400116ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
117{
118 while (__sync_lock_test_and_set (&h->lock, 1))
119 ;
120
121 h->tag = tag;
122}
123
124always_inline void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400125ssvm_unlock (ssvm_shared_header_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126{
127 if (--h->recursion_count == 0)
128 {
129 h->owner_pid = 0;
130 h->tag = 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400131 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 h->lock = 0;
133 }
134}
135
Dave Barach2c25a622017-06-26 11:35:07 -0400136always_inline void
137ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
138{
139 h->tag = 0;
140 CLIB_MEMORY_BARRIER ();
141 h->lock = 0;
142}
143
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400144static inline void *
145ssvm_push_heap (ssvm_shared_header_t * sh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400147 u8 *oldheap;
148 oldheap = clib_mem_set_heap (sh->heap);
149 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150}
151
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400152static inline void
153ssvm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400155 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156}
157
Florin Corasb384b542018-01-15 01:08:33 -0800158static inline void *
159ssvm_mem_alloc (ssvm_private_t * ssvm, uword size)
160{
161 u8 *oldheap;
162 void *rv;
163
164 oldheap = clib_mem_set_heap (ssvm->sh->heap);
165 rv = clib_mem_alloc (size);
166 clib_mem_set_heap (oldheap);
167 return (rv);
168}
169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170#define foreach_ssvm_api_error \
Dave Barach68b0fb02017-02-28 15:15:56 -0500171_(NO_NAME, "No shared segment name", -100) \
172_(NO_SIZE, "Size not set (master)", -101) \
173_(CREATE_FAILURE, "Create failed", -102) \
174_(SET_SIZE, "Set size failed", -103) \
175_(MMAP, "mmap failed", -104) \
176_(SLAVE_TIMEOUT, "Slave map timeout", -105)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400178typedef enum
179{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180#define _(n,s,c) SSVM_API_ERROR_##n = c,
181 foreach_ssvm_api_error
182#undef _
183} ssvm_api_error_enum_t;
184
185#define SSVM_API_ERROR_NO_NAME (-10)
186
Florin Corasb384b542018-01-15 01:08:33 -0800187int ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
188int ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500189void ssvm_delete (ssvm_private_t * ssvm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Florin Corasb384b542018-01-15 01:08:33 -0800191int ssvm_master_init_shm (ssvm_private_t * ssvm);
192int ssvm_slave_init_shm (ssvm_private_t * ssvm);
193void ssvm_delete_shm (ssvm_private_t * ssvm);
194
195int ssvm_master_init_memfd (ssvm_private_t * memfd);
Florin Coras4d9b9d82018-01-14 12:25:50 -0800196int ssvm_slave_init_memfd (ssvm_private_t * memfd);
Florin Corasd3e83a92018-01-16 02:40:18 -0800197void ssvm_delete_memfd (ssvm_private_t * memfd);
Florin Coras4d9b9d82018-01-14 12:25:50 -0800198
Florin Corasa332c462018-01-31 06:52:17 -0800199int ssvm_master_init_private (ssvm_private_t * ssvm);
200int ssvm_slave_init_private (ssvm_private_t * ssvm);
201void ssvm_delete_private (ssvm_private_t * ssvm);
202
Florin Corasb384b542018-01-15 01:08:33 -0800203ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm);
204u8 *ssvm_name (const ssvm_private_t * ssvm);
205
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206#endif /* __included_ssvm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400207
208/*
209 * fd.io coding-style-patch-verification: ON
210 *
211 * Local Variables:
212 * eval: (c-set-style "gnu")
213 * End:
214 */