FD.io VPP  v19.08.2-294-g37e99c22d
Vector Packet Processing
fifo_segment.h
Go to the documentation of this file.
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 {
28 
29 #define FIFO_SEGMENT_MIN_FIFO_SIZE 4096 /* 4kB min fifo size */
30 #define FIFO_SEGMENT_MAX_FIFO_SIZE (2 << 30) /* 2GB max fifo size */
31 #define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32 /* Allocation quantum */
32 
33 typedef enum fifo_segment_flags_
34 {
38 
39 typedef struct
40 {
41  svm_fifo_t *fifos; /**< Linked list of active RX fifos */
42  svm_fifo_t *free_fifos; /**< Freelists by fifo size */
43  svm_fifo_chunk_t **free_chunks; /**< Freelists by chunk size */
44  u32 n_active_fifos; /**< Number of active fifos */
45  u8 flags; /**< Segment flags */
46  u32 n_free_bytes; /**< Bytes usable for new allocs */
47  u32 n_fl_chunk_bytes; /**< Chunk bytes on freelist */
49 
50 typedef struct
51 {
52  ssvm_private_t ssvm; /**< ssvm segment data */
53  fifo_segment_header_t *h; /**< fifo segment data */
55 
56 typedef struct
57 {
58  fifo_segment_t *segments; /**< pool of fifo segments */
59  u64 next_baseva; /**< Where to put the next one */
60  u32 timeout_in_seconds; /**< Time to wait during attach */
62 
63 typedef struct
64 {
65  ssvm_segment_type_t segment_type; /**< type of segment requested */
66  u32 segment_size; /**< size of the segment */
67  int memfd_fd; /**< fd for memfd segments */
68  char *segment_name; /**< segment name */
69  u32 *new_segment_indices; /**< return vec of new seg indices */
71 
72 #define fifo_segment_flags(_fs) _fs->h->flags
73 
81  u32 fs_index);
83 void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);
84 
85 /**
86  * Allocate fifo in fifo segment
87  *
88  * @param fs fifo segment for fifo
89  * @param data_bytes size of default fifo chunk in bytes
90  * @param ftype fifo type @ref fifo_segment_ftype_t
91  * @return new fifo or 0 if alloc failed
92  */
94  u32 data_bytes,
95  fifo_segment_ftype_t ftype);
96 
97 /**
98  * Free fifo allocated in fifo segment
99  *
100  * @param fs fifo segment for fifo
101  * @param f fifo to be freed
102  */
104 
105 /**
106  * Try to preallocate fifo headers
107  *
108  * Tries to preallocate fifo headers and adds them to freelist.
109  *
110  * @param fs fifo segment
111  * @param batch_size number of chunks to be allocated
112  * @return 0 on success, negative number otherwise
113  */
114 int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 batch_size);
115 
116 /**
117  * Try to preallocate fifo chunks on segment
118  *
119  * Tries to preallocate chunks of requested size on segment and adds them
120  * to chunk freelist.
121  *
122  * @param fs fifo segment
123  * @param chunk_size size of chunks to be allocated in bytes
124  * @param batch_size number of chunks to be allocated
125  * @return 0 on success, negative number otherwise
126  */
128  u32 batch_size);
129 /**
130  * Pre-allocates fifo pairs in fifo segment
131  *
132  * The number of fifos pre-allocated is the minimum of the requested number
133  * of pairs and the maximum number that fit within the segment. If the maximum
134  * is hit, the number of fifo pairs requested is updated by subtracting the
135  * number of fifos that have been successfully allocated.
136  *
137  * @param fs fifo segment for fifo
138  * @param rx_fifo_size data size of rx fifos
139  * @param tx_fifo_size data size of tx fifos
140  * @param n_fifo_pairs number of pairs requested. Prior to returning, this
141  * is decremented by the the number of pairs allocated.
142  */
144  u32 rx_fifo_size,
145  u32 tx_fifo_size,
146  u32 * n_fifo_pairs);
147 /**
148  * Grow fifo size by adding an additional chunk of memory
149  *
150  * @param fs fifo segment for fifo
151  * @param f fifo to be grown
152  * @param chunk_size number of bytes to be added to fifo
153  * @return 0 on success or a negative number otherwise
154  */
156  u32 chunk_size);
157 
158 /**
159  * Collect unused chunks for fifo
160  *
161  * @param fs fifo segment for fifo
162  * @param f fifo whose chunks are to be collected
163  * @return 0 on success, error otherwise
164  */
166 
167 /**
168  * Fifo segment estimate of number of free bytes
169  *
170  * Returns fifo segment's internal estimate of the number of free bytes.
171  * To force a synchronization between the segment and the underlying
172  * memory allocator, call @ref fifo_segment_update_free_bytes
173  *
174  * @param fs fifo segment
175  * @return free bytes estimate
176  */
178 
179 /**
180  * Update fifo segment free bytes estimate
181  *
182  * Forces fifo segment free bytes estimate synchronization with underlying
183  * memory allocator.
184  *
185  * @param fs fifo segment
186  */
188 
189 /**
190  * Number of bytes on chunk free lists
191  *
192  * @param fs fifo segment
193  * @return free bytes on chunk free lists
194  */
200 /**
201  * Find number of free chunks of given size
202  *
203  * @param fs fifo segment
204  * @param size chunk size of interest or ~0 if all should be counted
205  * @return number of chunks of given size
206  */
208 
210  u32 timeout_in_seconds);
211 
214 
215 #endif /* __included_fifo_segment_h__ */
216 
217 /*
218  * fd.io coding-style-patch-verification: ON
219  *
220  * Local Variables:
221  * eval: (c-set-style "gnu")
222  * End:
223  */
typedef address
Definition: ip_types.api:83
fifo_segment_header_t * h
fifo segment data
Definition: fifo_segment.h:53
a
Definition: bitmap.h:538
u8 flags
Segment flags.
Definition: fifo_segment.h:45
int fifo_segment_collect_fifo_chunks(fifo_segment_t *fs, svm_fifo_t *f)
Collect unused chunks for fifo.
Definition: fifo_segment.c:694
unsigned long u64
Definition: types.h:89
u32 n_active_fifos
Number of active fifos.
Definition: fifo_segment.h:44
int fifo_segment_attach(fifo_segment_main_t *sm, fifo_segment_create_args_t *a)
Attach as slave to a fifo segment.
Definition: fifo_segment.c:99
u32 fifo_segment_num_fifos(fifo_segment_t *fs)
Get number of active fifos.
Definition: fifo_segment.c:726
u32 n_free_bytes
Bytes usable for new allocs.
Definition: fifo_segment.h:46
unsigned char u8
Definition: types.h:56
enum fifo_segment_flags_ fifo_segment_flags_t
struct _svm_fifo svm_fifo_t
void fifo_segment_update_free_bytes(fifo_segment_t *fs)
Update fifo segment free bytes estimate.
Definition: fifo_segment.c:797
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
u8 fifo_segment_has_fifos(fifo_segment_t *fs)
Definition: fifo_segment.c:815
u32 n_fl_chunk_bytes
Chunk bytes on freelist.
Definition: fifo_segment.h:47
enum ssvm_segment_type_ ssvm_segment_type_t
int fifo_segment_create(fifo_segment_main_t *sm, fifo_segment_create_args_t *a)
Create a fifo segment and initialize as master.
Definition: fifo_segment.c:65
fifo_segment_flags_
Definition: fifo_segment.h:33
void fifo_segment_delete(fifo_segment_main_t *sm, fifo_segment_t *fs)
Definition: fifo_segment.c:129
unsigned int u32
Definition: types.h:88
u64 next_baseva
Where to put the next one.
Definition: fifo_segment.h:59
char * segment_name
segment name
Definition: fifo_segment.h:68
svm_fifo_chunk_t ** free_chunks
Freelists by chunk size.
Definition: fifo_segment.h:43
uword size
u32 fifo_segment_num_free_fifos(fifo_segment_t *fs)
Definition: fifo_segment.c:732
u32 fifo_segment_index(fifo_segment_main_t *sm, fifo_segment_t *fs)
Definition: fifo_segment.c:137
svm_fifo_t * free_fifos
Freelists by fifo size.
Definition: fifo_segment.h:42
svm_fifo_t * fifo_segment_alloc_fifo(fifo_segment_t *fs, u32 data_bytes, fifo_segment_ftype_t ftype)
Allocate fifo in fifo segment.
Definition: fifo_segment.c:380
int fifo_segment_init(fifo_segment_t *fs)
Initialize fifo segment shared header.
Definition: fifo_segment.c:41
u32 segment_size
size of the segment
Definition: fifo_segment.h:66
ssvm_private_t ssvm
ssvm segment data
Definition: fifo_segment.h:52
format_function_t format_fifo_segment_type
Definition: fifo_segment.h:213
int memfd_fd
fd for memfd segments
Definition: fifo_segment.h:67
fifo_segment_t * fifo_segment_get_segment(fifo_segment_main_t *sm, u32 fs_index)
Definition: fifo_segment.c:149
fifo_segment_t * segments
pool of fifo segments
Definition: fifo_segment.h:58
fifo_segment_ftype_t
Definition: fifo_segment.h:21
ssvm_segment_type_t segment_type
type of segment requested
Definition: fifo_segment.h:65
svm_fifo_t * fifos
Linked list of active RX fifos.
Definition: fifo_segment.h:41
void fifo_segment_info(fifo_segment_t *seg, char **address, size_t *size)
Definition: fifo_segment.c:155
void fifo_segment_main_init(fifo_segment_main_t *sm, u64 baseva, u32 timeout_in_seconds)
Definition: fifo_segment.c:162
u32 fifo_segment_num_free_chunks(fifo_segment_t *fs, u32 size)
Find number of free chunks of given size.
Definition: fifo_segment.c:751
u32 fifo_segment_free_bytes(fifo_segment_t *fs)
Fifo segment estimate of number of free bytes.
Definition: fifo_segment.c:803
int fifo_segment_prealloc_fifo_hdrs(fifo_segment_t *fs, u32 batch_size)
Try to preallocate fifo headers.
Definition: fifo_segment.c:505
u32 fifo_segment_fl_chunk_bytes(fifo_segment_t *fs)
Number of bytes on chunk free lists.
Definition: fifo_segment.c:809
int fifo_segment_grow_fifo(fifo_segment_t *fs, svm_fifo_t *f, u32 chunk_size)
Grow fifo size by adding an additional chunk of memory.
Definition: fifo_segment.c:645
svm_fifo_t * fifo_segment_get_fifo_list(fifo_segment_t *fs)
Definition: fifo_segment.c:821
int fifo_segment_prealloc_fifo_chunks(fifo_segment_t *fs, u32 chunk_size, u32 batch_size)
Try to preallocate fifo chunks on segment.
Definition: fifo_segment.c:542
u32 timeout_in_seconds
Time to wait during attach.
Definition: fifo_segment.h:60
void fifo_segment_free_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Free fifo allocated in fifo segment.
Definition: fifo_segment.c:435
format_function_t format_fifo_segment
Definition: fifo_segment.h:212
u32 * new_segment_indices
return vec of new seg indices
Definition: fifo_segment.h:69
void fifo_segment_preallocate_fifo_pairs(fifo_segment_t *fs, u32 rx_fifo_size, u32 tx_fifo_size, u32 *n_fifo_pairs)
Pre-allocates fifo pairs in fifo segment.
Definition: fifo_segment.c:594