Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 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; |
| 54 | int recursion_count; |
| 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 */ |
| 61 | u64 memfd_va; |
| 62 | /* The actual mmap size */ |
| 63 | u64 memfd_size; |
| 64 | u32 master_pid; |
| 65 | u32 slave_pid; |
| 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. */ |
| 73 | u32 master_index; |
| 74 | } memfd_shared_header_t; |
| 75 | |
| 76 | typedef struct |
| 77 | { |
| 78 | memfd_shared_header_t *sh; |
| 79 | int fd; |
| 80 | u64 memfd_size; |
| 81 | u32 my_pid; |
| 82 | u32 vlib_hw_if_index; |
| 83 | uword requested_va; |
| 84 | int i_am_master; |
| 85 | u32 per_interface_next_index; |
| 86 | u32 *rx_queue; |
| 87 | u8 *name; |
| 88 | } memfd_private_t; |
| 89 | |
| 90 | always_inline void |
| 91 | memfd_lock (memfd_shared_header_t * h, u32 my_pid, u32 tag) |
| 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 |
| 108 | memfd_lock_non_recursive (memfd_shared_header_t * h, u32 tag) |
| 109 | { |
| 110 | while (__sync_lock_test_and_set (&h->lock, 1)) |
| 111 | ; |
| 112 | |
| 113 | h->tag = tag; |
| 114 | } |
| 115 | |
| 116 | always_inline void |
| 117 | memfd_unlock (memfd_shared_header_t * h) |
| 118 | { |
| 119 | if (--h->recursion_count == 0) |
| 120 | { |
| 121 | h->owner_pid = 0; |
| 122 | h->tag = 0; |
| 123 | CLIB_MEMORY_BARRIER (); |
| 124 | h->lock = 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | always_inline void |
| 129 | memfd_unlock_non_recursive (memfd_shared_header_t * h) |
| 130 | { |
| 131 | h->tag = 0; |
| 132 | CLIB_MEMORY_BARRIER (); |
| 133 | h->lock = 0; |
| 134 | } |
| 135 | |
| 136 | static inline void * |
| 137 | memfd_push_heap (memfd_shared_header_t * sh) |
| 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, |
| 161 | foreach_memfd_api_error |
| 162 | #undef _ |
| 163 | } memfd_api_error_enum_t; |
| 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 | */ |