FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
segment_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 SRC_VNET_SESSION_SEGMENT_MANAGER_H_
16 #define SRC_VNET_SESSION_SEGMENT_MANAGER_H_
17 
18 #include <svm/message_queue.h>
19 #include <vppinfra/lock.h>
20 #include <vppinfra/valloc.h>
21 #include <svm/fifo_segment.h>
22 
23 typedef struct _segment_manager_props
24 {
25  u32 rx_fifo_size; /**< receive fifo size */
26  u32 tx_fifo_size; /**< transmit fifo size */
27  u32 evt_q_size; /**< event queue length */
28  u32 segment_size; /**< first segment size */
29  u32 prealloc_fifos; /**< preallocated fifo pairs */
30  u32 add_segment_size; /**< additional segment size */
31  u8 add_segment:1; /**< can add new segments flag */
32  u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */
33  u8 reserved:6; /**< reserved flags */
34  ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
35  private segments are used */
37 
38 typedef struct _segment_manager
39 {
40  /** Pool of segments allocated by this manager */
41  fifo_segment_t *segments;
42 
43  /** rwlock that protects the segments pool */
44  clib_rwlock_t segments_rwlock;
45 
46  /** Owner app worker index */
47  u32 app_wrk_index;
48 
49  /**
50  * First segment should not be deleted unless segment manger is deleted.
51  * This also indicates that the segment manager is the first to have been
52  * allocated for the app.
53  */
54  u8 first_is_protected;
55 
56  /**
57  * App event queue allocated in first segment
58  */
59  svm_msg_q_t *event_queue;
61 
63 {
67 
68 #define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
69 
71 int segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
72  u32 prealloc_fifo_pairs);
73 
74 /**
75  * Cleanup segment manager
76  *
77  * @param sm segment manager to be freed
78  */
80 
81 /**
82  * Initiate segment manager cleanup
83  *
84  * @param sm segment manager to be freed
85  */
90 
91 int segment_manager_add_segment (segment_manager_t * sm, u32 segment_size);
93  fifo_segment_t * fs);
95  u32 segment_index);
98  u32 segment_index);
100  u32 segment_size);
102  u32 segment_index);
104  fifo_segment_t * segment);
107 
109  svm_fifo_t ** rx_fifo,
110  svm_fifo_t ** tx_fifo);
112  u32 rx_fifo_size, u32 tx_fifo_size,
113  svm_fifo_t ** rx_fifo,
114  svm_fifo_t ** tx_fifo);
116  svm_fifo_t * tx_fifo);
117 
118 /**
119  * Grows fifo owned by segment manager
120  *
121  * @param sm segment manager that owns the fifo
122  * @param f fifo to be grown
123  * @param size amount of bytes to add to fifo
124  * @return 0 on success, negative number otherwise
125  */
127  u32 size);
128 
129 /**
130  * Request to shrink fifo owned by segment manager
131  *
132  * If this is not called by the producer, no attempt is made to reduce the
133  * size until the producer tries to enqueue more data. To collect the chunks
134  * that are to be removed call @ref segment_manager_collect_fifo_chunks
135  *
136  * Size reduction does not affect fifo chunk boundaries. Therefore chunks are
137  * not split and the amount of bytes to be removed can be equal to or less
138  * than what was requested.
139  *
140  * @param sm segment manager that owns the fifo
141  * @param f fifo to be shrunk
142  * @param size amount of bytes to remove from fifo
143  * @param is_producer flag that indicates is caller is the producer for the
144  * fifo.
145  * @return actual number of bytes to be removed
146  */
148  u32 size, u8 is_producer);
149 
150 /**
151  * Collect fifo chunks that are no longer used
152  *
153  * This should not be called unless SVM_FIFO_F_COLLECT_CHUNKS is set for
154  * the fifo. The chunks are returned to the fifo segment freelist.
155  *
156  * @param sm segment manager that owns the fifo
157  * @param f fifo whose chunks are to be collected
158  * @return 0 on success, error otherwise
159  */
161  svm_fifo_t * f);
163 
165  segment_manager_props_t * props);
169 
171 
172 /**
173  * Cleanup segment manager sessions
174  *
175  * Initiates disconnects for all sessions 'owned' by a segment manager by
176  * leveraging the backpointers that fifos keep.
177  *
178  * @param sm segment manager whose sessions are to be disconnected
179  */
181 void segment_manager_format_sessions (segment_manager_t * sm, int verbose);
182 
184 
186  sm);
187 
188 #endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
189 /*
190  * fd.io coding-style-patch-verification: ON
191  *
192  * Local Variables:
193  * eval: (c-set-style "gnu")
194  * End:
195  */
void segment_manager_free(segment_manager_t *sm)
Cleanup segment manager.
u32 segment_manager_index(segment_manager_t *sm)
a
Definition: bitmap.h:538
int segment_manager_init(segment_manager_t *sm, u32 first_seg_size, u32 prealloc_fifo_pairs)
Initializes segment manager based on options provided.
unsigned long u64
Definition: types.h:89
struct segment_manager_main_init_args_ segment_manager_main_init_args_t
fifo_segment_t * segment_manager_get_segment_w_handle(u64 sh)
unsigned char u8
Definition: types.h:56
u8 segment_manager_has_fifos(segment_manager_t *sm)
segment_manager_t * segment_manager_get(u32 index)
struct _svm_fifo svm_fifo_t
void segment_manager_segment_reader_unlock(segment_manager_t *sm)
int segment_manager_try_alloc_fifos(fifo_segment_t *fs, u32 rx_fifo_size, u32 tx_fifo_size, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
enum ssvm_segment_type_ ssvm_segment_type_t
segment_manager_props_t * segment_manager_props_init(segment_manager_props_t *sm)
void segment_manager_app_detach(segment_manager_t *sm)
int segment_manager_add_first_segment(segment_manager_t *sm, u32 segment_size)
Simple first-fit virtual space allocator.
segment_manager_t * segment_manager_alloc(void)
unsigned int u32
Definition: types.h:88
segment_manager_t * segment_manager_get_if_valid(u32 index)
int segment_manager_shrink_fifo(segment_manager_t *sm, svm_fifo_t *f, u32 size, u8 is_producer)
Request to shrink fifo owned by segment manager.
u32 segment_manager_evt_q_expected_size(u32 q_size)
svm_msg_q_t * segment_manager_event_queue(segment_manager_t *sm)
u64 segment_manager_make_segment_handle(u32 segment_manager_index, u32 segment_index)
uword size
svm_msg_q_t * segment_manager_alloc_queue(fifo_segment_t *fs, segment_manager_props_t *props)
Allocates shm queue in the first segment.
void segment_manager_dealloc_fifos(svm_fifo_t *rx_fifo, svm_fifo_t *tx_fifo)
int segment_manager_add_segment(segment_manager_t *sm, u32 segment_size)
Adds segment to segment manager&#39;s pool.
struct _segment_manager_props segment_manager_props_t
void segment_manager_del_segment(segment_manager_t *sm, fifo_segment_t *fs)
Remove segment without lock.
fifo_segment_t * segment_manager_get_segment(segment_manager_t *sm, u32 segment_index)
Reads a segment from the segment manager&#39;s pool without lock.
int segment_manager_alloc_session_fifos(segment_manager_t *sm, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
Unidirectional shared-memory multi-ring message queue.
int segment_manager_collect_fifo_chunks(segment_manager_t *sm, svm_fifo_t *f)
Collect fifo chunks that are no longer used.
void segment_manager_segment_writer_unlock(segment_manager_t *sm)
u64 segment_manager_segment_handle(segment_manager_t *sm, fifo_segment_t *segment)
struct _segment_manager segment_manager_t
struct _svm_queue svm_queue_t
void segment_manager_dealloc_queue(segment_manager_t *sm, svm_queue_t *q)
Frees shm queue allocated in the first segment.
void segment_manager_init_free(segment_manager_t *sm)
Initiate segment manager cleanup.
static void add_segment(mstate m, char *tbase, size_t tsize, flag_t mmapped)
Definition: dlmalloc.c:2576
void segment_manager_format_sessions(segment_manager_t *sm, int verbose)
int segment_manager_grow_fifo(segment_manager_t *sm, svm_fifo_t *f, u32 size)
Grows fifo owned by segment manager.
fifo_segment_t * segment_manager_get_segment_w_lock(segment_manager_t *sm, u32 segment_index)
Reads a segment from the segment manager&#39;s pool and acquires reader lock.
void segment_manager_main_init(segment_manager_main_init_args_t *a)
void segment_manager_del_sessions(segment_manager_t *sm)
Cleanup segment manager sessions.