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:
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_fifo_segment_h__
16 #define __included_fifo_segment_h__
19 #include <svm/svm_fifo.h>
23 FIFO_SEGMENT_FTYPE_NONE = -1,
24 FIFO_SEGMENT_RX_FIFO = 0,
27 } fifo_segment_ftype_t;
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 */
32 #define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32 /* Allocation quantum */
34 typedef enum fifo_segment_flags_
36 FIFO_SEGMENT_F_IS_PREALLOCATED = 1 << 0,
37 FIFO_SEGMENT_F_WILL_DELETE = 1 << 1,
38 } fifo_segment_flags_t;
40 typedef struct fifo_segment_slice_
42 svm_fifo_t *fifos; /**< Linked list of active RX fifos */
43 svm_fifo_t *free_fifos; /**< Freelists by fifo size */
44 svm_fifo_chunk_t **free_chunks; /**< Freelists by chunk size */
45 uword n_fl_chunk_bytes; /**< Chunk bytes on freelist */
46 } fifo_segment_slice_t;
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 */
57 } fifo_segment_header_t;
61 ssvm_private_t ssvm; /**< ssvm segment data */
62 fifo_segment_header_t *h; /**< fifo segment data */
63 u8 n_slices; /**< number of fifo segment slices */
68 fifo_segment_t *segments; /**< pool of fifo segments */
69 uword next_baseva; /**< Where to put the next one */
70 u32 timeout_in_seconds; /**< Time to wait during attach */
71 } fifo_segment_main_t;
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;
82 #define fifo_segment_flags(_fs) _fs->h->flags
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,
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);
96 * Allocate fifo in fifo segment
98 * @param fs fifo segment for fifo
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
103 svm_fifo_t *fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs,
106 fifo_segment_ftype_t ftype);
109 * Free fifo allocated in fifo segment
111 * @param fs fifo segment for fifo
112 * @param f fifo to be freed
114 void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);
117 * Try to preallocate fifo headers
119 * Tries to preallocate fifo headers and adds them to freelist.
121 * @param fs fifo segment
122 * @param batch_size number of chunks to be allocated
123 * @return 0 on success, negative number otherwise
125 int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
129 * Try to preallocate fifo chunks on segment
131 * Tries to preallocate chunks of requested size on segment and adds them
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
139 int fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
140 u32 chunk_size, u32 batch_size);
142 * Pre-allocates fifo pairs in fifo segment
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.
149 * @param fs fifo segment for fifo
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.
155 void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
160 * Grow fifo size by adding an additional chunk of memory
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
167 int fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f,
171 * Collect unused chunks for fifo
173 * @param fs fifo segment for fifo
174 * @param f fifo whose chunks are to be collected
175 * @return 0 on success, error otherwise
177 int fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f);
180 * Fifo segment estimate of number of free bytes
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
186 * @param fs fifo segment
187 * @return free bytes estimate
189 uword fifo_segment_free_bytes (fifo_segment_t * fs);
192 * Update fifo segment free bytes estimate
194 * Forces fifo segment free bytes estimate synchronization with underlying
197 * @param fs fifo segment
199 void fifo_segment_update_free_bytes (fifo_segment_t * fs);
202 * Number of bytes on chunk free lists
204 * @param fs fifo segment
205 * @return free bytes on chunk free lists
207 uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs);
208 u8 fifo_segment_has_fifos (fifo_segment_t * fs);
209 svm_fifo_t *fifo_segment_get_slice_fifo_list (fifo_segment_t * fs,
211 u32 fifo_segment_num_fifos (fifo_segment_t * fs);
212 u32 fifo_segment_num_free_fifos (fifo_segment_t * fs);
214 * Find number of free chunks of given size
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
220 u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size);
222 void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
223 u32 timeout_in_seconds);
225 format_function_t format_fifo_segment;
226 format_function_t format_fifo_segment_type;
228 #endif /* __included_fifo_segment_h__ */
231 * fd.io coding-style-patch-verification: ON
234 * eval: (c-set-style "gnu")