blob: a181f2ef864884ef4c187144c7a8a025df3da0a9 [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
Dave Barach8a7fb0c2016-07-08 14:44:23 -040048typedef struct
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 /* Spin-lock */
51 volatile u32 lock;
52 volatile u32 owner_pid;
53 int recursion_count;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040054 u32 tag; /* for debugging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
56 /* The allocation arena */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040057 void *heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
59 /* Segment must be mapped at this address, or no supper */
60 u64 ssvm_va;
61 /* The actual mmap size */
62 u64 ssvm_size;
63 u32 master_pid;
64 u32 slave_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040065 u8 *name;
66 void *opaque[SSVM_N_OPAQUE];
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
68 /* Set when the master application thinks it's time to make the donuts */
69 volatile u32 ready;
70
71 /* Needed to make unique MAC addresses, etc. */
72 u32 master_index;
73} ssvm_shared_header_t;
74
Dave Barach8a7fb0c2016-07-08 14:44:23 -040075typedef struct
76{
77 ssvm_shared_header_t *sh;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 u64 ssvm_size;
79 u32 my_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040080 u8 *name;
Christophe Fontainefef15b42016-04-09 12:38:49 +090081 uword requested_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 int i_am_master;
Florin Coras4d9b9d82018-01-14 12:25:50 -080083
84 /* Needed by memfd segments */
85 int fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086} ssvm_private_t;
87
Dave Barach8a7fb0c2016-07-08 14:44:23 -040088always_inline void
89ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
91 if (h->owner_pid == my_pid)
92 {
93 h->recursion_count++;
94 return;
95 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -040096
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 while (__sync_lock_test_and_set (&h->lock, 1))
98 ;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040099
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 h->owner_pid = my_pid;
101 h->recursion_count = 1;
102 h->tag = tag;
103}
104
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105always_inline void
Dave Barach2c25a622017-06-26 11:35:07 -0400106ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
107{
108 while (__sync_lock_test_and_set (&h->lock, 1))
109 ;
110
111 h->tag = tag;
112}
113
114always_inline void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400115ssvm_unlock (ssvm_shared_header_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116{
117 if (--h->recursion_count == 0)
118 {
119 h->owner_pid = 0;
120 h->tag = 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400121 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 h->lock = 0;
123 }
124}
125
Dave Barach2c25a622017-06-26 11:35:07 -0400126always_inline void
127ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
128{
129 h->tag = 0;
130 CLIB_MEMORY_BARRIER ();
131 h->lock = 0;
132}
133
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400134static inline void *
135ssvm_push_heap (ssvm_shared_header_t * sh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400137 u8 *oldheap;
138 oldheap = clib_mem_set_heap (sh->heap);
139 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140}
141
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400142static inline void
143ssvm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400145 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146}
147
148#define foreach_ssvm_api_error \
Dave Barach68b0fb02017-02-28 15:15:56 -0500149_(NO_NAME, "No shared segment name", -100) \
150_(NO_SIZE, "Size not set (master)", -101) \
151_(CREATE_FAILURE, "Create failed", -102) \
152_(SET_SIZE, "Set size failed", -103) \
153_(MMAP, "mmap failed", -104) \
154_(SLAVE_TIMEOUT, "Slave map timeout", -105)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400156typedef enum
157{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158#define _(n,s,c) SSVM_API_ERROR_##n = c,
159 foreach_ssvm_api_error
160#undef _
161} ssvm_api_error_enum_t;
162
163#define SSVM_API_ERROR_NO_NAME (-10)
164
165int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index);
166int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500167void ssvm_delete (ssvm_private_t * ssvm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Florin Coras4d9b9d82018-01-14 12:25:50 -0800169int ssvm_master_init_memfd (ssvm_private_t * memfd, u32 master_index);
170int ssvm_slave_init_memfd (ssvm_private_t * memfd);
Florin Corasd3e83a92018-01-16 02:40:18 -0800171void ssvm_delete_memfd (ssvm_private_t * memfd);
Florin Coras4d9b9d82018-01-14 12:25:50 -0800172
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173#endif /* __included_ssvm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400174
175/*
176 * fd.io coding-style-patch-verification: ON
177 *
178 * Local Variables:
179 * eval: (c-set-style "gnu")
180 * End:
181 */