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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_memfd_h__
16 #define __included_memfd_h__
20 #include <sys/types.h>
23 #include <netinet/in.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>
44 #define MMAP_PAGESIZE (clib_mem_get_page_size())
47 #define MEMFD_N_OPAQUE 7
53 volatile u32 owner_pid;
55 u32 tag; /* for debugging */
57 /* The allocation arena */
60 /* Segment must be mapped at this address, or no supper */
62 /* The actual mmap size */
67 void *opaque[MEMFD_N_OPAQUE];
69 /* Set when the master application thinks it's time to make the donuts */
72 /* Needed to make unique MAC addresses, etc. */
74 } memfd_shared_header_t;
78 memfd_shared_header_t *sh;
85 u32 per_interface_next_index;
91 memfd_lock (memfd_shared_header_t * h, u32 my_pid, u32 tag)
93 if (h->owner_pid == my_pid)
99 while (__sync_lock_test_and_set (&h->lock, 1))
102 h->owner_pid = my_pid;
103 h->recursion_count = 1;
108 memfd_lock_non_recursive (memfd_shared_header_t * h, u32 tag)
110 while (__sync_lock_test_and_set (&h->lock, 1))
117 memfd_unlock (memfd_shared_header_t * h)
119 if (--h->recursion_count == 0)
123 CLIB_MEMORY_BARRIER ();
129 memfd_unlock_non_recursive (memfd_shared_header_t * h)
132 CLIB_MEMORY_BARRIER ();
137 memfd_push_heap (memfd_shared_header_t * sh)
140 oldheap = clib_mem_set_heap (sh->heap);
141 return ((void *) oldheap);
145 memfd_pop_heap (void *oldheap)
147 clib_mem_set_heap (oldheap);
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)
160 #define _(n,s,c) MEMFD_API_ERROR_##n = c,
161 foreach_memfd_api_error
163 } memfd_api_error_enum_t;
165 #define MEMFD_API_ERROR_NO_NAME (-10)
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);
171 /* These do not belong here, but the original keeps running around... */
172 /* $$$$ work w/ Damjan to fix properly */
174 #ifndef F_LINUX_SPECIFIC_BASE
175 #define F_LINUX_SPECIFIC_BASE 1024
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)
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 */
186 #endif /* __included_memfd_h__ */
189 * fd.io coding-style-patch-verification: ON
192 * eval: (c-set-style "gnu")