Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2019 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_fifo_segment_h__ |
| 16 | #define __included_fifo_segment_h__ |
| 17 | |
| 18 | #include <svm/ssvm.h> |
| 19 | #include <svm/svm_fifo.h> |
| 20 | |
| 21 | typedef enum |
| 22 | { |
| 23 | FIFO_SEGMENT_FTYPE_NONE = -1, |
| 24 | FIFO_SEGMENT_RX_FIFO = 0, |
| 25 | FIFO_SEGMENT_TX_FIFO, |
| 26 | FIFO_SEGMENT_N_FTYPES |
| 27 | } fifo_segment_ftype_t; |
| 28 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 29 | #define FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE 12 /**< 4kB min fifo size */ |
| 30 | #define FIFO_SEGMENT_MIN_FIFO_SIZE 4096 /**< 4kB min fifo size */ |
| 31 | #define FIFO_SEGMENT_MAX_FIFO_SIZE (2 << 30) /**< 2GB max fifo size */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 32 | #define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32 /* Allocation quantum */ |
| 33 | |
| 34 | typedef enum fifo_segment_flags_ |
| 35 | { |
| 36 | FIFO_SEGMENT_F_IS_PREALLOCATED = 1 << 0, |
| 37 | FIFO_SEGMENT_F_WILL_DELETE = 1 << 1, |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 38 | FIFO_SEGMENT_F_MEM_LIMIT = 1 << 2, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 39 | } fifo_segment_flags_t; |
| 40 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 41 | typedef struct fifo_segment_slice_ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 42 | { |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 43 | svm_fifo_t *fifos; /**< Linked list of active RX fifos */ |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 44 | svm_fifo_t *free_fifos; /**< Freelists by fifo size */ |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 45 | svm_fifo_chunk_t **free_chunks; /**< Freelists by chunk size */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 46 | uword n_fl_chunk_bytes; /**< Chunk bytes on freelist */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 47 | } fifo_segment_slice_t; |
| 48 | |
| 49 | typedef struct |
| 50 | { |
| 51 | fifo_segment_slice_t *slices; /** Fixed array of slices */ |
| 52 | ssvm_shared_header_t *ssvm_sh; /**< Pointer to fs ssvm shared hdr */ |
| 53 | uword n_free_bytes; /**< Segment free bytes */ |
| 54 | u32 n_active_fifos; /**< Number of active fifos */ |
Florin Coras | 8122cc2 | 2019-12-18 13:06:41 -0800 | [diff] [blame] | 55 | u32 n_reserved_bytes; /**< Bytes not to be allocated */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 56 | u32 max_log2_chunk_size; /**< Max log2(chunk size) for fs */ |
| 57 | u8 flags; /**< Segment flags */ |
| 58 | u8 n_slices; /**< Number of slices */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 59 | } fifo_segment_header_t; |
| 60 | |
| 61 | typedef struct |
| 62 | { |
| 63 | ssvm_private_t ssvm; /**< ssvm segment data */ |
| 64 | fifo_segment_header_t *h; /**< fifo segment data */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 65 | u8 n_slices; /**< number of fifo segment slices */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 66 | } fifo_segment_t; |
| 67 | |
| 68 | typedef struct |
| 69 | { |
| 70 | fifo_segment_t *segments; /**< pool of fifo segments */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 71 | uword next_baseva; /**< Where to put the next one */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 72 | u32 timeout_in_seconds; /**< Time to wait during attach */ |
| 73 | } fifo_segment_main_t; |
| 74 | |
| 75 | typedef struct |
| 76 | { |
| 77 | ssvm_segment_type_t segment_type; /**< type of segment requested */ |
| 78 | u32 segment_size; /**< size of the segment */ |
| 79 | int memfd_fd; /**< fd for memfd segments */ |
| 80 | char *segment_name; /**< segment name */ |
| 81 | u32 *new_segment_indices; /**< return vec of new seg indices */ |
| 82 | } fifo_segment_create_args_t; |
| 83 | |
| 84 | #define fifo_segment_flags(_fs) _fs->h->flags |
| 85 | |
| 86 | int fifo_segment_init (fifo_segment_t * fs); |
| 87 | int fifo_segment_create (fifo_segment_main_t * sm, |
| 88 | fifo_segment_create_args_t * a); |
| 89 | int fifo_segment_attach (fifo_segment_main_t * sm, |
| 90 | fifo_segment_create_args_t * a); |
| 91 | void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs); |
| 92 | fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm, |
| 93 | u32 fs_index); |
| 94 | u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs); |
| 95 | void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size); |
| 96 | |
| 97 | /** |
| 98 | * Allocate fifo in fifo segment |
| 99 | * |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 100 | * @param fs fifo segment for fifo |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 101 | * @param data_bytes size of default fifo chunk in bytes |
| 102 | * @param ftype fifo type @ref fifo_segment_ftype_t |
| 103 | * @return new fifo or 0 if alloc failed |
| 104 | */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 105 | svm_fifo_t *fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, |
| 106 | u32 slice_index, |
| 107 | u32 data_bytes, |
| 108 | fifo_segment_ftype_t ftype); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Free fifo allocated in fifo segment |
| 112 | * |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 113 | * @param fs fifo segment for fifo |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 114 | * @param f fifo to be freed |
| 115 | */ |
| 116 | void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f); |
| 117 | |
| 118 | /** |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 119 | * Try to preallocate fifo headers |
| 120 | * |
| 121 | * Tries to preallocate fifo headers and adds them to freelist. |
| 122 | * |
| 123 | * @param fs fifo segment |
| 124 | * @param batch_size number of chunks to be allocated |
| 125 | * @return 0 on success, negative number otherwise |
| 126 | */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 127 | int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index, |
| 128 | u32 batch_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Try to preallocate fifo chunks on segment |
| 132 | * |
| 133 | * Tries to preallocate chunks of requested size on segment and adds them |
| 134 | * to chunk freelist. |
| 135 | * |
| 136 | * @param fs fifo segment |
| 137 | * @param chunk_size size of chunks to be allocated in bytes |
| 138 | * @param batch_size number of chunks to be allocated |
| 139 | * @return 0 on success, negative number otherwise |
| 140 | */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 141 | int fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index, |
| 142 | u32 chunk_size, u32 batch_size); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 143 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 144 | * Pre-allocates fifo pairs in fifo segment |
| 145 | * |
| 146 | * The number of fifos pre-allocated is the minimum of the requested number |
| 147 | * of pairs and the maximum number that fit within the segment. If the maximum |
| 148 | * is hit, the number of fifo pairs requested is updated by subtracting the |
| 149 | * number of fifos that have been successfully allocated. |
| 150 | * |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 151 | * @param fs fifo segment for fifo |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 152 | * @param rx_fifo_size data size of rx fifos |
| 153 | * @param tx_fifo_size data size of tx fifos |
| 154 | * @param n_fifo_pairs number of pairs requested. Prior to returning, this |
| 155 | * is decremented by the the number of pairs allocated. |
| 156 | */ |
| 157 | void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs, |
| 158 | u32 rx_fifo_size, |
| 159 | u32 tx_fifo_size, |
| 160 | u32 * n_fifo_pairs); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Grow fifo size by adding an additional chunk of memory |
| 163 | * |
| 164 | * @param fs fifo segment for fifo |
| 165 | * @param f fifo to be grown |
| 166 | * @param chunk_size number of bytes to be added to fifo |
| 167 | * @return 0 on success or a negative number otherwise |
| 168 | */ |
| 169 | int fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, |
| 170 | u32 chunk_size); |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Collect unused chunks for fifo |
| 174 | * |
| 175 | * @param fs fifo segment for fifo |
| 176 | * @param f fifo whose chunks are to be collected |
| 177 | * @return 0 on success, error otherwise |
| 178 | */ |
| 179 | int fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Fifo segment estimate of number of free bytes |
| 183 | * |
| 184 | * Returns fifo segment's internal estimate of the number of free bytes. |
| 185 | * To force a synchronization between the segment and the underlying |
| 186 | * memory allocator, call @ref fifo_segment_update_free_bytes |
| 187 | * |
| 188 | * @param fs fifo segment |
| 189 | * @return free bytes estimate |
| 190 | */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 191 | uword fifo_segment_free_bytes (fifo_segment_t * fs); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Update fifo segment free bytes estimate |
| 195 | * |
| 196 | * Forces fifo segment free bytes estimate synchronization with underlying |
| 197 | * memory allocator. |
| 198 | * |
| 199 | * @param fs fifo segment |
| 200 | */ |
| 201 | void fifo_segment_update_free_bytes (fifo_segment_t * fs); |
| 202 | |
| 203 | /** |
| 204 | * Number of bytes on chunk free lists |
| 205 | * |
| 206 | * @param fs fifo segment |
| 207 | * @return free bytes on chunk free lists |
| 208 | */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 209 | uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 210 | u8 fifo_segment_has_fifos (fifo_segment_t * fs); |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 211 | svm_fifo_t *fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, |
| 212 | u32 slice_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 213 | u32 fifo_segment_num_fifos (fifo_segment_t * fs); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 214 | u32 fifo_segment_num_free_fifos (fifo_segment_t * fs); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Find number of free chunks of given size |
| 217 | * |
| 218 | * @param fs fifo segment |
| 219 | * @param size chunk size of interest or ~0 if all should be counted |
| 220 | * @return number of chunks of given size |
| 221 | */ |
| 222 | u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 223 | |
| 224 | void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva, |
| 225 | u32 timeout_in_seconds); |
| 226 | |
| 227 | format_function_t format_fifo_segment; |
| 228 | format_function_t format_fifo_segment_type; |
| 229 | |
| 230 | #endif /* __included_fifo_segment_h__ */ |
| 231 | |
| 232 | /* |
| 233 | * fd.io coding-style-patch-verification: ON |
| 234 | * |
| 235 | * Local Variables: |
| 236 | * eval: (c-set-style "gnu") |
| 237 | * End: |
| 238 | */ |