FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
memfd.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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_memfd_h__
16 #define __included_memfd_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 /* DGMS, memfd syscall not in glibc... */
41 #include <vppinfra/linux/syscall.h>
42 
43 #ifndef MMAP_PAGESIZE
44 #define MMAP_PAGESIZE (clib_mem_get_page_size())
45 #endif
46 
47 #define MEMFD_N_OPAQUE 7
48 
49 typedef struct
50 {
51  /* Spin-lock */
52  volatile u32 lock;
53  volatile u32 owner_pid;
55  u32 tag; /* for debugging */
56 
57  /* The allocation arena */
58  void *heap;
59 
60  /* Segment must be mapped at this address, or no supper */
62  /* The actual mmap size */
66  u8 *name;
67  void *opaque[MEMFD_N_OPAQUE];
68 
69  /* Set when the master application thinks it's time to make the donuts */
70  volatile u32 ready;
71 
72  /* Needed to make unique MAC addresses, etc. */
75 
76 typedef struct
77 {
79  int fd;
87  u8 *name;
89 
90 always_inline void
92 {
93  if (h->owner_pid == my_pid)
94  {
95  h->recursion_count++;
96  return;
97  }
98 
99  while (__sync_lock_test_and_set (&h->lock, 1))
100  ;
101 
102  h->owner_pid = my_pid;
103  h->recursion_count = 1;
104  h->tag = tag;
105 }
106 
107 always_inline void
109 {
110  while (__sync_lock_test_and_set (&h->lock, 1))
111  ;
112 
113  h->tag = tag;
114 }
115 
116 always_inline void
118 {
119  if (--h->recursion_count == 0)
120  {
121  h->owner_pid = 0;
122  h->tag = 0;
124  h->lock = 0;
125  }
126 }
127 
128 always_inline void
130 {
131  h->tag = 0;
133  h->lock = 0;
134 }
135 
136 static inline void *
138 {
139  u8 *oldheap;
140  oldheap = clib_mem_set_heap (sh->heap);
141  return ((void *) oldheap);
142 }
143 
144 static inline void
145 memfd_pop_heap (void *oldheap)
146 {
147  clib_mem_set_heap (oldheap);
148 }
149 
150 #define foreach_memfd_api_error \
151 _(NO_NAME, "No shared segment name", -100) \
152 _(NO_SIZE, "Size not set (master)", -101) \
153 _(CREATE_FAILURE, "Create failed", -102) \
154 _(SET_SIZE, "Set size failed", -103) \
155 _(MMAP, "mmap failed", -104) \
156 _(SLAVE_TIMEOUT, "Slave map timeout", -105)
157 
158 typedef enum
159 {
160 #define _(n,s,c) MEMFD_API_ERROR_##n = c,
162 #undef _
164 
165 #define MEMFD_API_ERROR_NO_NAME (-10)
166 
167 int memfd_master_init (memfd_private_t * memfd, u32 master_index);
168 int memfd_slave_init (memfd_private_t * memfd);
169 void memfd_delete (memfd_private_t * memfd);
170 
171 /* These do not belong here, but the original keeps running around... */
172 /* $$$$ work w/ Damjan to fix properly */
173 
174 #ifndef F_LINUX_SPECIFIC_BASE
175 #define F_LINUX_SPECIFIC_BASE 1024
176 #endif
177 #define MFD_ALLOW_SEALING 0x0002U
178 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
179 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
180 
181 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
182 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
183 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
184 #define F_SEAL_WRITE 0x0008 /* prevent writes */
185 
186 #endif /* __included_memfd_h__ */
187 
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */
volatile u32 lock
Definition: memfd.h:52
volatile u32 owner_pid
Definition: memfd.h:53
volatile u32 ready
Definition: memfd.h:70
Fixed length block allocator.
#define foreach_memfd_api_error
Definition: memfd.h:150
uword requested_va
Definition: memfd.h:83
u8 * name
Definition: memfd.h:87
u32 per_interface_next_index
Definition: memfd.h:85
#define MEMFD_N_OPAQUE
Definition: memfd.h:47
#define always_inline
Definition: clib.h:92
u64 memfd_size
Definition: memfd.h:80
unsigned long u64
Definition: types.h:89
static void memfd_unlock(memfd_shared_header_t *h)
Definition: memfd.h:117
u32 vlib_hw_if_index
Definition: memfd.h:82
int memfd_slave_init(memfd_private_t *memfd)
Definition: memfd.c:96
memfd_api_error_enum_t
Definition: memfd.h:158
int i_am_master
Definition: memfd.h:84
static void memfd_unlock_non_recursive(memfd_shared_header_t *h)
Definition: memfd.h:129
memfd_shared_header_t * sh
Definition: memfd.h:78
vec_header_t h
Definition: buffer.c:282
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:226
int memfd_master_init(memfd_private_t *memfd, u32 master_index)
Definition: memfd.c:18
void memfd_delete(memfd_private_t *memfd)
static void * memfd_push_heap(memfd_shared_header_t *sh)
Definition: memfd.h:137
unsigned int u32
Definition: types.h:88
static void memfd_lock(memfd_shared_header_t *h, u32 my_pid, u32 tag)
Definition: memfd.h:91
Bitmaps built as vectors of machine words.
u32 * rx_queue
Definition: memfd.h:86
u32 my_pid
Definition: memfd.h:81
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:109
static void memfd_pop_heap(void *oldheap)
Definition: memfd.h:145
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static void memfd_lock_non_recursive(memfd_shared_header_t *h, u32 tag)
Definition: memfd.h:108