blob: ff36f45c4f27523a019996e296dc16e90db96d90 [file] [log] [blame]
Florin Coras88001c62019-04-24 14:44:46 -07001/*
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
21typedef 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 Coras62ddc032019-12-08 18:30:42 -080029#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 Coras88001c62019-04-24 14:44:46 -070032#define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32 /* Allocation quantum */
33
34typedef 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 Coras62ddc032019-12-08 18:30:42 -080040typedef struct fifo_segment_slice_
Florin Coras88001c62019-04-24 14:44:46 -070041{
Florin Corasb095a3c2019-04-25 12:58:46 -070042 svm_fifo_t *fifos; /**< Linked list of active RX fifos */
Florin Corascefd5d82019-05-05 13:19:57 -070043 svm_fifo_t *free_fifos; /**< Freelists by fifo size */
Florin Corasb095a3c2019-04-25 12:58:46 -070044 svm_fifo_chunk_t **free_chunks; /**< Freelists by chunk size */
Florin Corasf9d4ab42019-05-11 16:55:53 -070045 u32 n_fl_chunk_bytes; /**< Chunk bytes on freelist */
Florin Coras62ddc032019-12-08 18:30:42 -080046} fifo_segment_slice_t;
47
48typedef 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 Coras88001c62019-04-24 14:44:46 -070057} fifo_segment_header_t;
58
59typedef struct
60{
61 ssvm_private_t ssvm; /**< ssvm segment data */
62 fifo_segment_header_t *h; /**< fifo segment data */
Florin Coras62ddc032019-12-08 18:30:42 -080063 u8 n_slices; /**< number of fifo segment slices */
Florin Coras88001c62019-04-24 14:44:46 -070064} fifo_segment_t;
65
66typedef 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
73typedef 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
84int fifo_segment_init (fifo_segment_t * fs);
85int fifo_segment_create (fifo_segment_main_t * sm,
86 fifo_segment_create_args_t * a);
87int fifo_segment_attach (fifo_segment_main_t * sm,
88 fifo_segment_create_args_t * a);
89void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs);
90fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm,
91 u32 fs_index);
92u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs);
93void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);
94
95/**
96 * Allocate fifo in fifo segment
97 *
Florin Corasb095a3c2019-04-25 12:58:46 -070098 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -070099 * @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 Coras62ddc032019-12-08 18:30:42 -0800103svm_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 Coras88001c62019-04-24 14:44:46 -0700107
108/**
109 * Free fifo allocated in fifo segment
110 *
Florin Corasb095a3c2019-04-25 12:58:46 -0700111 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -0700112 * @param f fifo to be freed
113 */
114void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);
115
116/**
Florin Corasf9d4ab42019-05-11 16:55:53 -0700117 * 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 Coras62ddc032019-12-08 18:30:42 -0800125int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
126 u32 batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700127
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 Coras62ddc032019-12-08 18:30:42 -0800139int fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
140 u32 chunk_size, u32 batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700141/**
Florin Coras88001c62019-04-24 14:44:46 -0700142 * 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 Corasb095a3c2019-04-25 12:58:46 -0700149 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -0700150 * @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 */
155void 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 Corasb095a3c2019-04-25 12:58:46 -0700159/**
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 */
167int fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f,
168 u32 chunk_size);
Florin Coras344ce422019-05-03 11:46:55 -0700169
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 */
177int fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700178
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 */
189u32 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 */
199void 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 Coras62ddc032019-12-08 18:30:42 -0800207uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs);
Florin Coras88001c62019-04-24 14:44:46 -0700208u8 fifo_segment_has_fifos (fifo_segment_t * fs);
Florin Coras62ddc032019-12-08 18:30:42 -0800209svm_fifo_t *fifo_segment_get_slice_fifo_list (fifo_segment_t * fs,
210 u32 slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700211u32 fifo_segment_num_fifos (fifo_segment_t * fs);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700212u32 fifo_segment_num_free_fifos (fifo_segment_t * fs);
Florin Corasb095a3c2019-04-25 12:58:46 -0700213/**
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 */
220u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size);
Florin Coras88001c62019-04-24 14:44:46 -0700221
222void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
223 u32 timeout_in_seconds);
224
225format_function_t format_fifo_segment;
226format_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 */