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