blob: a55f7d3472c0c0a45c9372e64de541304757117e [file] [log] [blame]
Florin Coras6cf30ad2017-04-04 23:08:23 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras6cf30ad2017-04-04 23:08:23 -07003 * 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
16#include <vnet/session/segment_manager.h>
17#include <vnet/session/session.h>
18#include <vnet/session/application.h>
19
Florin Coras88001c62019-04-24 14:44:46 -070020typedef struct segment_manager_main_
21{
22 segment_manager_t *segment_managers; /**< Pool of segment managers */
Florin Coras88001c62019-04-24 14:44:46 -070023 u32 seg_name_counter; /**< Counter for segment names */
Florin Corasa332c462018-01-31 06:52:17 -080024
Florin Coras88001c62019-04-24 14:44:46 -070025 /*
26 * Configuration
27 */
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000028 u32 default_fifo_size; /**< default rx/tx fifo size */
29 u32 default_segment_size; /**< default fifo segment size */
30 u32 default_app_mq_size; /**< default app msg q size */
31 u32 default_max_fifo_size; /**< default max fifo size */
32 u8 default_high_watermark; /**< default high watermark % */
33 u8 default_low_watermark; /**< default low watermark % */
Florin Coras88001c62019-04-24 14:44:46 -070034} segment_manager_main_t;
Florin Corasa5464812017-04-19 13:00:05 -070035
Florin Coras88001c62019-04-24 14:44:46 -070036static segment_manager_main_t sm_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -070037
Florin Coras88001c62019-04-24 14:44:46 -070038#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
39do { \
40 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
Damjan Marionb2c31b62020-12-13 21:47:40 +010041 pool_foreach((VAR), ((SM)->segments)) (BODY); \
Florin Coras88001c62019-04-24 14:44:46 -070042 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
43} while (0)
44
45static segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -080046segment_manager_properties_get (segment_manager_t * sm)
Florin Corasad0c77f2017-11-09 18:00:15 -080047{
Florin Corasab2f6db2018-08-31 14:31:41 -070048 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
49 return application_get_segment_manager_properties (app_wrk->app_index);
Florin Corasa332c462018-01-31 06:52:17 -080050}
51
Florin Coras88001c62019-04-24 14:44:46 -070052segment_manager_props_t *
53segment_manager_props_init (segment_manager_props_t * props)
Florin Corasa332c462018-01-31 06:52:17 -080054{
Florin Coras88001c62019-04-24 14:44:46 -070055 props->add_segment_size = sm_main.default_segment_size;
56 props->rx_fifo_size = sm_main.default_fifo_size;
57 props->tx_fifo_size = sm_main.default_fifo_size;
58 props->evt_q_size = sm_main.default_app_mq_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000059 props->max_fifo_size = sm_main.default_max_fifo_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000060 props->high_watermark = sm_main.default_high_watermark;
61 props->low_watermark = sm_main.default_low_watermark;
Florin Coras62ddc032019-12-08 18:30:42 -080062 props->n_slices = vlib_num_workers () + 1;
Florin Corasad0c77f2017-11-09 18:00:15 -080063 return props;
64}
65
Florin Corasbf395972020-04-30 15:05:24 +000066u8
Florin Coras9d063042017-09-14 03:08:00 -040067segment_manager_app_detached (segment_manager_t * sm)
68{
Florin Corasc8e812f2020-05-14 05:32:18 +000069 return (sm->flags & SEG_MANAGER_F_DETACHED);
Dave Wallace7b749fe2017-07-05 14:30:46 -040070}
71
Florin Coras4e4531e2017-11-06 23:27:56 -080072void
73segment_manager_app_detach (segment_manager_t * sm)
74{
Florin Corasc8e812f2020-05-14 05:32:18 +000075 sm->flags |= SEG_MANAGER_F_DETACHED;
Florin Coras4e4531e2017-11-06 23:27:56 -080076}
77
Florin Corasa332c462018-01-31 06:52:17 -080078always_inline u32
Florin Coras88001c62019-04-24 14:44:46 -070079segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070080{
Florin Corasa332c462018-01-31 06:52:17 -080081 return (seg - sm->segments);
82}
83
84/**
Florin Coras88001c62019-04-24 14:44:46 -070085 * Adds segment to segment manager's pool
86 *
87 * If needed a writer's lock is acquired before allocating a new segment
88 * to avoid affecting any of the segments pool readers.
89 */
Florin Coras6973c732021-06-17 15:53:38 -070090static inline int
91segment_manager_add_segment_inline (segment_manager_t *sm, uword segment_size,
92 u8 notify_app, u8 flags)
Florin Coras88001c62019-04-24 14:44:46 -070093{
94 segment_manager_main_t *smm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -070095 segment_manager_props_t *props;
96 fifo_segment_t *fs;
Florin Coras9a45bd82020-12-28 16:28:07 -080097 u32 fs_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -070098 u8 *seg_name;
99 int rv;
100
101 props = segment_manager_properties_get (sm);
102
103 /* Not configured for addition of new segments and not first */
104 if (!props->add_segment && !segment_size)
105 {
106 clib_warning ("cannot allocate new segment");
107 return VNET_API_ERROR_INVALID_VALUE;
108 }
109
110 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700111 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700112 */
113 if (vlib_num_workers ())
114 clib_rwlock_writer_lock (&sm->segments_rwlock);
115
116 pool_get_zero (sm->segments, fs);
117
118 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700119 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700120 */
121 segment_size = segment_size ? segment_size : props->add_segment_size;
Filip Tehlar2711ca72021-11-24 10:30:59 +0000122 /* add overhead to ensure the result segment size is at least
123 * of that requested */
124 segment_size +=
125 sizeof (fifo_segment_header_t) +
126 vlib_thread_main.n_vlib_mains * sizeof (fifo_segment_slice_t) +
127 FIFO_SEGMENT_ALLOC_OVERHEAD;
Florin Coras9a45bd82020-12-28 16:28:07 -0800128 segment_size = round_pow2 (segment_size, clib_mem_get_page_size ());
Florin Corasf9d4ab42019-05-11 16:55:53 -0700129
Florin Coras88001c62019-04-24 14:44:46 -0700130 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
131 {
132 seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
Florin Coras88001c62019-04-24 14:44:46 -0700133 }
134 else
Florin Coras2a7c0b62020-09-28 23:40:28 -0700135 {
136 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
137 application_t *app = application_get (app_wrk->app_index);
138 seg_name = format (0, "%v segment%c", app->name, 0);
139 }
Florin Coras88001c62019-04-24 14:44:46 -0700140
141 fs->ssvm.ssvm_size = segment_size;
142 fs->ssvm.name = seg_name;
Florin Coras9a45bd82020-12-28 16:28:07 -0800143 fs->ssvm.requested_va = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700144
Florin Coras5220a262020-09-29 18:11:24 -0700145 if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700146 {
147 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
148 segment_size);
Florin Coras88001c62019-04-24 14:44:46 -0700149 pool_put (sm->segments, fs);
150 goto done;
151 }
152
Florin Corasf9d4ab42019-05-11 16:55:53 -0700153 /*
154 * Initialize fifo segment
155 */
Florin Coras62ddc032019-12-08 18:30:42 -0800156 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700157 fifo_segment_init (fs);
158
159 /*
160 * Save segment index before dropping lock, if any held
161 */
162 fs_index = fs - sm->segments;
163
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000164 /*
165 * Set watermarks in segment
166 */
Florin Corasafbb33a2021-08-10 16:56:34 -0700167 fs->high_watermark = sm->high_watermark;
168 fs->low_watermark = sm->low_watermark;
169 fs->flags = flags;
170 fs->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000171 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000172
liuyacan9f299032021-04-25 20:11:30 +0800173 if (notify_app)
174 {
175 app_worker_t *app_wrk;
176 u64 fs_handle;
177 fs_handle = segment_manager_segment_handle (sm, fs);
178 app_wrk = app_worker_get (sm->app_wrk_index);
179 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
180 if (rv)
181 return rv;
182 }
Florin Coras88001c62019-04-24 14:44:46 -0700183done:
184
185 if (vlib_num_workers ())
186 clib_rwlock_writer_unlock (&sm->segments_rwlock);
187
188 return fs_index;
189}
190
Florin Coras6973c732021-06-17 15:53:38 -0700191int
192segment_manager_add_segment (segment_manager_t *sm, uword segment_size,
193 u8 notify_app)
194{
195 return segment_manager_add_segment_inline (sm, segment_size, notify_app, 0);
196}
197
198int
199segment_manager_add_segment2 (segment_manager_t *sm, uword segment_size,
200 u8 flags)
201{
202 return segment_manager_add_segment_inline (sm, segment_size, 0, flags);
203}
204
Florin Coras88001c62019-04-24 14:44:46 -0700205/**
Florin Corasa332c462018-01-31 06:52:17 -0800206 * Remove segment without lock
207 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800208void
Florin Coras88001c62019-04-24 14:44:46 -0700209segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800210{
Florin Corasa332c462018-01-31 06:52:17 -0800211 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800212 {
Florin Corasea7e7082020-07-07 17:57:28 -0700213 if (!segment_manager_app_detached (sm))
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800214 {
215 app_worker_t *app_wrk;
216 u64 segment_handle;
217 app_wrk = app_worker_get (sm->app_wrk_index);
218 segment_handle = segment_manager_segment_handle (sm, fs);
219 app_worker_del_segment_notify (app_wrk, segment_handle);
220 }
221 }
Florin Corasa332c462018-01-31 06:52:17 -0800222
Florin Corasc547e912020-12-08 17:50:45 -0800223 fifo_segment_cleanup (fs);
Florin Corasa332c462018-01-31 06:52:17 -0800224 ssvm_delete (&fs->ssvm);
225
226 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400227 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800228 pool_put (sm->segments, fs);
229}
230
Florin Coras57660d92020-04-04 22:45:34 +0000231static fifo_segment_t *
232segment_manager_get_segment_if_valid (segment_manager_t * sm,
233 u32 segment_index)
234{
235 if (pool_is_free_index (sm->segments, segment_index))
236 return 0;
237 return pool_elt_at_index (sm->segments, segment_index);
238}
239
Florin Corasa332c462018-01-31 06:52:17 -0800240/**
241 * Removes segment after acquiring writer lock
242 */
Florin Coras99368312018-08-02 10:45:44 -0700243static inline void
Florin Coras2d0e3de2020-10-23 16:31:40 -0700244sm_lock_and_del_segment_inline (segment_manager_t * sm, u32 fs_index)
Florin Corasa332c462018-01-31 06:52:17 -0800245{
Florin Coras88001c62019-04-24 14:44:46 -0700246 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800247 u8 is_prealloc;
248
249 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000250
251 fs = segment_manager_get_segment_if_valid (sm, fs_index);
252 if (!fs)
253 goto done;
254
Florin Coras88001c62019-04-24 14:44:46 -0700255 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800256 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000257 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800258
259 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000260
261done:
Florin Corasa332c462018-01-31 06:52:17 -0800262 clib_rwlock_writer_unlock (&sm->segments_rwlock);
263}
264
Florin Coras2d0e3de2020-10-23 16:31:40 -0700265void
266segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
267{
268 sm_lock_and_del_segment_inline (sm, fs_index);
269}
270
Florin Corasa332c462018-01-31 06:52:17 -0800271/**
272 * Reads a segment from the segment manager's pool without lock
273 */
Florin Coras88001c62019-04-24 14:44:46 -0700274fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800275segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
276{
277 return pool_elt_at_index (sm->segments, segment_index);
278}
279
Florin Corasfa76a762018-11-29 12:40:10 -0800280u64
281segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700282 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800283{
284 u32 segment_index = segment_manager_segment_index (sm, segment);
285 return (((u64) segment_manager_index (sm) << 32) | segment_index);
286}
287
Florin Coras88001c62019-04-24 14:44:46 -0700288u64
289segment_manager_make_segment_handle (u32 segment_manager_index,
290 u32 segment_index)
291{
292 return (((u64) segment_manager_index << 32) | segment_index);
293}
294
295fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800296segment_manager_get_segment_w_handle (u64 segment_handle)
297{
298 u32 sm_index, segment_index;
299 segment_manager_t *sm;
300
301 segment_manager_parse_segment_handle (segment_handle, &sm_index,
302 &segment_index);
303 sm = segment_manager_get (sm_index);
304 if (!sm || pool_is_free_index (sm->segments, segment_index))
305 return 0;
306 return pool_elt_at_index (sm->segments, segment_index);
307}
308
Florin Corasa332c462018-01-31 06:52:17 -0800309/**
310 * Reads a segment from the segment manager's pool and acquires reader lock
311 *
312 * Caller must drop the reader's lock by calling
313 * @ref segment_manager_segment_reader_unlock once it finishes working with
314 * the segment.
315 */
Florin Coras88001c62019-04-24 14:44:46 -0700316fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800317segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
318{
319 clib_rwlock_reader_lock (&sm->segments_rwlock);
320 return pool_elt_at_index (sm->segments, segment_index);
321}
322
323void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000324segment_manager_segment_reader_lock (segment_manager_t * sm)
325{
326 clib_rwlock_reader_lock (&sm->segments_rwlock);
327}
328
329void
Florin Corasa332c462018-01-31 06:52:17 -0800330segment_manager_segment_reader_unlock (segment_manager_t * sm)
331{
332 clib_rwlock_reader_unlock (&sm->segments_rwlock);
333}
334
335void
336segment_manager_segment_writer_unlock (segment_manager_t * sm)
337{
338 clib_rwlock_writer_unlock (&sm->segments_rwlock);
339}
340
Florin Corasa332c462018-01-31 06:52:17 -0800341segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700342segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800343{
Florin Coras88001c62019-04-24 14:44:46 -0700344 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800345 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700346
347 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800348 clib_rwlock_init (&sm->segments_rwlock);
349 return sm;
350}
351
Florin Corasa332c462018-01-31 06:52:17 -0800352int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000353segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800354{
Florin Coras88001c62019-04-24 14:44:46 -0700355 segment_manager_props_t *props;
Florin Corasa332c462018-01-31 06:52:17 -0800356
357 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000358
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000359 sm->max_fifo_size = props->max_fifo_size ?
360 props->max_fifo_size : sm_main.default_max_fifo_size;
361 sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
362
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000363 segment_manager_set_watermarks (sm,
364 props->high_watermark,
365 props->low_watermark);
Florin Corasa107f402020-09-29 19:18:46 -0700366 return 0;
367}
368
369/**
370 * Initializes segment manager based on options provided.
371 * Returns error if ssvm segment(s) allocation fails.
372 */
373int
374segment_manager_init_first (segment_manager_t * sm)
375{
376 segment_manager_props_t *props;
377 uword first_seg_size;
378 fifo_segment_t *fs;
379 int fs_index, i;
380
381 segment_manager_init (sm);
382 props = segment_manager_properties_get (sm);
383 first_seg_size = clib_max (props->segment_size,
384 sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800385
Florin Coras9845c202020-04-28 01:54:22 +0000386 if (props->prealloc_fifos)
Florin Corasa332c462018-01-31 06:52:17 -0800387 {
Florin Coras9845c202020-04-28 01:54:22 +0000388 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
389 u32 rx_rounded_data_size, tx_rounded_data_size;
390 u32 prealloc_fifo_pairs = props->prealloc_fifos;
391 u32 rx_fifo_size, tx_fifo_size, pair_size;
392 u32 approx_segment_count;
393
Florin Corasa332c462018-01-31 06:52:17 -0800394 /* Figure out how many segments should be preallocated */
395 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
396 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
397
398 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
399 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
400 pair_size = rx_fifo_size + tx_fifo_size;
401
402 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
403 if (first_seg_size > approx_total_size)
404 max_seg_size = first_seg_size;
405 approx_segment_count = (approx_total_size + (max_seg_size - 1))
406 / max_seg_size;
407
408 /* Allocate the segments */
409 for (i = 0; i < approx_segment_count + 1; i++)
410 {
liuyacan9f299032021-04-25 20:11:30 +0800411 fs_index = segment_manager_add_segment (sm, max_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000412 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800413 {
414 clib_warning ("Failed to preallocate segment %d", i);
Florin Coras9845c202020-04-28 01:54:22 +0000415 return fs_index;
Florin Corasa332c462018-01-31 06:52:17 -0800416 }
417
Florin Coras9845c202020-04-28 01:54:22 +0000418 fs = segment_manager_get_segment (sm, fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800419 if (i == 0)
Florin Coras9845c202020-04-28 01:54:22 +0000420 sm->event_queue = segment_manager_alloc_queue (fs, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800421
Florin Coras9845c202020-04-28 01:54:22 +0000422 fifo_segment_preallocate_fifo_pairs (fs,
Florin Coras88001c62019-04-24 14:44:46 -0700423 props->rx_fifo_size,
424 props->tx_fifo_size,
425 &prealloc_fifo_pairs);
Florin Coras9845c202020-04-28 01:54:22 +0000426 fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800427 if (prealloc_fifo_pairs == 0)
428 break;
429 }
Florin Coras9845c202020-04-28 01:54:22 +0000430 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800431 }
Florin Coras9845c202020-04-28 01:54:22 +0000432
liuyacan9f299032021-04-25 20:11:30 +0800433 fs_index = segment_manager_add_segment (sm, first_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000434 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800435 {
Florin Coras9845c202020-04-28 01:54:22 +0000436 clib_warning ("Failed to allocate segment");
437 return fs_index;
438 }
439
440 fs = segment_manager_get_segment (sm, fs_index);
441 sm->event_queue = segment_manager_alloc_queue (fs, props);
442
443 if (props->prealloc_fifo_hdrs)
444 {
445 u32 hdrs_per_slice;
446
447 /* Do not preallocate on slice associated to main thread */
448 i = (vlib_num_workers ()? 1 : 0);
449 hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
450
451 for (; i < fs->n_slices; i++)
Florin Corasa332c462018-01-31 06:52:17 -0800452 {
Florin Coras9845c202020-04-28 01:54:22 +0000453 if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
454 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
Florin Corasa332c462018-01-31 06:52:17 -0800455 }
Florin Corasa332c462018-01-31 06:52:17 -0800456 }
457
458 return 0;
459}
460
Florin Corasc8e812f2020-05-14 05:32:18 +0000461void
462segment_manager_cleanup_detached_listener (segment_manager_t * sm)
463{
464 app_worker_t *app_wrk;
465
466 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
467 if (!app_wrk)
468 return;
469
470 app_worker_del_detached_sm (app_wrk, segment_manager_index (sm));
471}
472
Florin Corasc87c91d2017-08-16 19:55:49 -0700473/**
Florin Coras88001c62019-04-24 14:44:46 -0700474 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700475 */
476void
Florin Coras88001c62019-04-24 14:44:46 -0700477segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700478{
Florin Coras88001c62019-04-24 14:44:46 -0700479 segment_manager_main_t *smm = &sm_main;
480 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400481
Florin Coras506aa3c2020-12-13 21:09:59 -0800482 ASSERT (vlib_get_thread_index () == 0
483 && !segment_manager_has_fifos (sm)
Florin Coras9d063042017-09-14 03:08:00 -0400484 && segment_manager_app_detached (sm));
485
Florin Corasc8e812f2020-05-14 05:32:18 +0000486 if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
487 segment_manager_cleanup_detached_listener (sm);
488
Florin Coras9d063042017-09-14 03:08:00 -0400489 /* If we have empty preallocated segments that haven't been removed, remove
490 * them now. Apart from that, the first segment in the first segment manager
491 * is not removed when all fifos are removed. It can only be removed when
492 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800493 clib_rwlock_writer_lock (&sm->segments_rwlock);
494
495 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100496 pool_foreach (fifo_segment, sm->segments) {
Florin Corasa332c462018-01-31 06:52:17 -0800497 segment_manager_del_segment (sm, fifo_segment);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100498 }
Florin Corasa332c462018-01-31 06:52:17 -0800499 /* *INDENT-ON* */
500
Florin Corasf1af21c2021-02-26 19:19:11 -0800501 pool_free (sm->segments);
Florin Corasa332c462018-01-31 06:52:17 -0800502 clib_rwlock_writer_unlock (&sm->segments_rwlock);
503
504 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700505 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400506 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800507 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700508}
509
Florin Coras506aa3c2020-12-13 21:09:59 -0800510static void
511sm_free_w_index_helper (void *arg)
512{
513 u32 sm_index = *(u32 *) arg;
514 segment_manager_t *sm;
515
516 ASSERT (vlib_get_thread_index () == 0);
517
518 if ((sm = segment_manager_get_if_valid (sm_index)))
519 segment_manager_free (sm);
520}
521
Florin Corasda78c5a2021-06-09 14:55:24 -0700522void
523segment_manager_free_safe (segment_manager_t *sm)
Florin Coras506aa3c2020-12-13 21:09:59 -0800524{
525 if (!vlib_thread_is_main_w_barrier ())
526 {
527 u32 sm_index = segment_manager_index (sm);
528 vlib_rpc_call_main_thread (sm_free_w_index_helper, (u8 *) & sm_index,
529 sizeof (sm_index));
530 }
531 else
532 {
533 segment_manager_free (sm);
534 }
535}
536
Florin Corasc87c91d2017-08-16 19:55:49 -0700537void
Florin Coras88001c62019-04-24 14:44:46 -0700538segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700539{
Florin Coras506aa3c2020-12-13 21:09:59 -0800540 ASSERT (vlib_get_thread_index () == 0);
541
Florin Coras4e4531e2017-11-06 23:27:56 -0800542 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700543 if (segment_manager_has_fifos (sm))
544 segment_manager_del_sessions (sm);
545 else
546 {
Florin Coras9d063042017-09-14 03:08:00 -0400547 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700548 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700549 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700550}
551
Florin Coras88001c62019-04-24 14:44:46 -0700552segment_manager_t *
553segment_manager_get (u32 index)
554{
555 return pool_elt_at_index (sm_main.segment_managers, index);
556}
557
558segment_manager_t *
559segment_manager_get_if_valid (u32 index)
560{
561 if (pool_is_free_index (sm_main.segment_managers, index))
562 return 0;
563 return pool_elt_at_index (sm_main.segment_managers, index);
564}
565
566u32
567segment_manager_index (segment_manager_t * sm)
568{
569 return sm - sm_main.segment_managers;
570}
571
572u8
573segment_manager_has_fifos (segment_manager_t * sm)
574{
575 fifo_segment_t *seg;
576 u8 first = 1;
577
578 /* *INDENT-OFF* */
579 segment_manager_foreach_segment_w_lock (seg, sm, ({
580 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
581 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
582 {
583 clib_warning ("segment %d has no fifos!",
584 segment_manager_segment_index (sm, seg));
585 first = 0;
586 }
587 if (fifo_segment_has_fifos (seg))
588 {
589 segment_manager_segment_reader_unlock (sm);
590 return 1;
591 }
592 }));
593 /* *INDENT-ON* */
594
595 return 0;
596}
597
598/**
599 * Initiate disconnects for all sessions 'owned' by a segment manager
600 */
601void
602segment_manager_del_sessions (segment_manager_t * sm)
603{
Florin Coras88001c62019-04-24 14:44:46 -0700604 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800605 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700606 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800607 int slice_index;
608 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700609
610 ASSERT (pool_elts (sm->segments) != 0);
611
612 /* Across all fifo segments used by the server */
613 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800614 segment_manager_foreach_segment_w_lock (fs, sm, ({
615 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700616 {
Florin Coras62ddc032019-12-08 18:30:42 -0800617 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
618
619 /*
620 * Remove any residual sessions from the session lookup table
621 * Don't bother deleting the individual fifos, we're going to
622 * throw away the fifo segment in a minute.
623 */
624 while (f)
625 {
Florin Corasc547e912020-12-08 17:50:45 -0800626 session = session_get_if_valid (f->shr->master_session_index,
627 f->master_thread_index);
628 if (session)
629 vec_add1 (handles, session_handle (session));
630 f = f->next;
631 }
Florin Coras88001c62019-04-24 14:44:46 -0700632 }
633
634 /* Instead of removing the segment, test when cleaning up disconnected
635 * sessions if the segment can be removed.
636 */
637 }));
638 /* *INDENT-ON* */
639
640 vec_foreach (handle, handles)
Florin Coras77ea42b2020-04-14 23:52:12 +0000641 {
642 session = session_get_from_handle (*handle);
643 session_close (session);
644 /* Avoid propagating notifications back to the app */
645 session->app_wrk_index = APP_INVALID_INDEX;
646 }
Florin Corasf1af21c2021-02-26 19:19:11 -0800647 vec_free (handles);
Florin Coras88001c62019-04-24 14:44:46 -0700648}
649
liuyacan87d48ad2021-04-28 11:34:03 +0000650/**
651 * Initiate disconnects for sessions in specified state 'owned' by a segment
652 * manager
653 */
654void
655segment_manager_del_sessions_filter (segment_manager_t *sm,
656 session_state_t *states)
657{
658 session_handle_t *handles = 0, *handle;
659 fifo_segment_t *fs;
660 session_t *session;
661 int slice_index;
662 svm_fifo_t *f;
663
664 ASSERT (pool_elts (sm->segments) != 0);
665
666 /* Across all fifo segments used by the server */
667 segment_manager_foreach_segment_w_lock (
668 fs, sm, ({
669 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
670 {
671 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
672 while (f)
673 {
674 session = session_get_if_valid (f->shr->master_session_index,
675 f->master_thread_index);
676 if (session)
677 {
678 session_state_t *state;
679 vec_foreach (state, states)
680 {
681 if (session->session_state == *state)
682 {
683 vec_add1 (handles, session_handle (session));
684 break;
685 }
686 }
687 }
688 f = f->next;
689 }
690 }
691 }));
692
693 vec_foreach (handle, handles)
694 {
695 session = session_get_from_handle (*handle);
696 session_close (session);
697 /* Avoid propagating notifications back to the app */
698 session->app_wrk_index = APP_INVALID_INDEX;
699 }
700 vec_free (handles);
701}
702
Florin Corasf8f516a2018-02-08 15:10:09 -0800703int
Florin Coras88001c62019-04-24 14:44:46 -0700704segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Coras62ddc032019-12-08 18:30:42 -0800705 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800706 u32 rx_fifo_size, u32 tx_fifo_size,
707 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800708{
Florin Coras88001c62019-04-24 14:44:46 -0700709 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800710 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
711 rx_fifo_size,
712 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800713
Florin Coras88001c62019-04-24 14:44:46 -0700714 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800715 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
716 tx_fifo_size,
717 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800718
719 if (*rx_fifo == 0)
720 {
721 /* This would be very odd, but handle it... */
722 if (*tx_fifo != 0)
723 {
Florin Coras88001c62019-04-24 14:44:46 -0700724 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800725 *tx_fifo = 0;
726 }
727 return -1;
728 }
729 if (*tx_fifo == 0)
730 {
731 if (*rx_fifo != 0)
732 {
Florin Coras88001c62019-04-24 14:44:46 -0700733 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800734 *rx_fifo = 0;
735 }
736 return -1;
737 }
738
739 return 0;
740}
741
Florin Coras6cf30ad2017-04-04 23:08:23 -0700742int
743segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800744 u32 thread_index,
Florin Corasb384b542018-01-15 01:08:33 -0800745 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700746 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700747{
Florin Corasa332c462018-01-31 06:52:17 -0800748 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000749 uword free_bytes, max_free_bytes = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700750 segment_manager_props_t *props;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000751 fifo_segment_t *fs = 0, *cur;
Florin Coras88001c62019-04-24 14:44:46 -0700752 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700753
Florin Corasa332c462018-01-31 06:52:17 -0800754 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700755
Florin Corasa332c462018-01-31 06:52:17 -0800756 /*
757 * Find the first free segment to allocate the fifos in
758 */
Florin Coras75ccf7b2020-03-05 19:44:02 +0000759
760 segment_manager_segment_reader_lock (sm);
761
Damjan Marionb2c31b62020-12-13 21:47:40 +0100762 pool_foreach (cur, sm->segments) {
Florin Corasda78c5a2021-06-09 14:55:24 -0700763 if (fifo_segment_flags (cur) & FIFO_SEGMENT_F_CUSTOM_USE)
764 continue;
765 free_bytes = fifo_segment_available_bytes (cur);
766 if (free_bytes > max_free_bytes)
767 {
768 max_free_bytes = free_bytes;
769 fs = cur;
770 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100771 }
Florin Corasa5464812017-04-19 13:00:05 -0700772
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000773 if (fs)
774 {
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000775 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
776 props->rx_fifo_size,
777 props->tx_fifo_size,
778 rx_fifo, tx_fifo);
779 /* On success, keep lock until fifos are initialized */
780 if (!alloc_fail)
781 goto alloc_success;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000782 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000783
Florin Coras75ccf7b2020-03-05 19:44:02 +0000784 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800785
Florin Corasa332c462018-01-31 06:52:17 -0800786 /*
787 * Allocation failed, see if we can add a new segment
788 */
789 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700790 {
liuyacan9f299032021-04-25 20:11:30 +0800791 if ((new_fs_index = segment_manager_add_segment (sm, 0, 1)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700792 {
Florin Corasa332c462018-01-31 06:52:17 -0800793 clib_warning ("Failed to add new segment");
Florin Coras00e01d32019-10-21 16:07:46 -0700794 return SESSION_E_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700795 }
Florin Coras88001c62019-04-24 14:44:46 -0700796 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Coras62ddc032019-12-08 18:30:42 -0800797 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
798 props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800799 props->tx_fifo_size,
800 rx_fifo, tx_fifo);
liuyacan9f299032021-04-25 20:11:30 +0800801 if (alloc_fail)
802 {
803 clib_warning ("Added a segment, still can't allocate a fifo");
804 segment_manager_segment_reader_unlock (sm);
805 return SESSION_E_SEG_NO_SPACE2;
806 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700807 }
Florin Corasa332c462018-01-31 06:52:17 -0800808 else
809 {
Florin Coras47cb2482020-07-13 08:52:53 -0700810 SESSION_DBG ("Can't add new seg and no space to allocate fifos!");
Florin Coras00e01d32019-10-21 16:07:46 -0700811 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800812 }
liuyacan9f299032021-04-25 20:11:30 +0800813
814alloc_success:
815 ASSERT (rx_fifo && tx_fifo);
816
817 sm_index = segment_manager_index (sm);
818 fs_index = segment_manager_segment_index (sm, fs);
819 (*tx_fifo)->segment_manager = sm_index;
820 (*rx_fifo)->segment_manager = sm_index;
821 (*tx_fifo)->segment_index = fs_index;
822 (*rx_fifo)->segment_index = fs_index;
823
824 /* Drop the lock after app is notified */
825 segment_manager_segment_reader_unlock (sm);
826
827 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700828}
829
830void
Florin Coras19223e02019-03-03 14:56:05 -0800831segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700832{
Florin Corasa332c462018-01-31 06:52:17 -0800833 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700834 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800835 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700836
Florin Coras58a93e82019-01-14 23:33:46 -0800837 if (!rx_fifo || !tx_fifo)
838 return;
839
Florin Corasa5464812017-04-19 13:00:05 -0700840 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700841 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800842 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700843 return;
844
Florin Coras19223e02019-03-03 14:56:05 -0800845 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700846 fs = segment_manager_get_segment_w_lock (sm, segment_index);
847 fifo_segment_free_fifo (fs, rx_fifo);
848 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700849
Florin Corasc87c91d2017-08-16 19:55:49 -0700850 /*
851 * Try to remove svm segment if it has no fifos. This can be done only if
852 * the segment is not the first in the segment manager or if it is first
853 * and it is not protected. Moreover, if the segment is first and the app
854 * has detached from the segment manager, remove the segment manager.
855 */
Florin Coras88001c62019-04-24 14:44:46 -0700856 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700857 {
Florin Corasa332c462018-01-31 06:52:17 -0800858 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700859
860 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800861 if (segment_index != 0 || !sm->first_is_protected)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700862 sm_lock_and_del_segment_inline (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700863
864 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400865 if (segment_manager_app_detached (sm)
866 && !segment_manager_has_fifos (sm))
Florin Coras506aa3c2020-12-13 21:09:59 -0800867 segment_manager_free_safe (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700868 }
Florin Corasa332c462018-01-31 06:52:17 -0800869 else
870 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700871}
872
Florin Coras6d7552c2020-04-09 01:49:45 +0000873void
Florin Coras0bc78d82021-01-09 14:34:01 -0800874segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000875{
876 fifo_segment_t *fs;
877
Florin Coras0bc78d82021-01-09 14:34:01 -0800878 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000879 fifo_segment_detach_fifo (fs, f);
880 segment_manager_segment_reader_unlock (sm);
881}
882
883void
Florin Coras0bc78d82021-01-09 14:34:01 -0800884segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
885 session_t *s)
Florin Coras6d7552c2020-04-09 01:49:45 +0000886{
887 fifo_segment_t *fs;
888
Florin Coras0bc78d82021-01-09 14:34:01 -0800889 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000890 fifo_segment_attach_fifo (fs, f, s->thread_index);
891 segment_manager_segment_reader_unlock (sm);
892
Florin Coras0bc78d82021-01-09 14:34:01 -0800893 (*f)->shr->master_session_index = s->session_index;
894 (*f)->master_thread_index = s->thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +0000895}
896
Florin Coras3c2fed52018-07-04 04:15:05 -0700897u32
898segment_manager_evt_q_expected_size (u32 q_len)
899{
900 u32 fifo_evt_size, notif_q_size, q_hdrs;
901 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
902
Florin Coras52207f12018-07-12 14:48:06 -0700903 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700904 notif_q_size = clib_max (16, q_len >> 4);
905
906 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
907 fifo_evt_ring_sz = q_len * fifo_evt_size;
908 session_ntf_ring_sz = notif_q_size * 256;
909 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
910
911 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
912}
913
Florin Corasa5464812017-04-19 13:00:05 -0700914/**
915 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800916 *
917 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700918 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700919svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700920segment_manager_alloc_queue (fifo_segment_t * segment,
921 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700922{
Florin Coras3c2fed52018-07-04 04:15:05 -0700923 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
924 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
925 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700926
Florin Coras52207f12018-07-12 14:48:06 -0700927 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700928 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700929 /* *INDENT-OFF* */
930 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700931 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700932 {notif_q_size, session_evt_size, 0}
933 };
934 /* *INDENT-ON* */
935 cfg->consumer_pid = 0;
936 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700937 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700938 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700939
Florin Corasb4624182020-12-11 13:58:12 -0800940 q = fifo_segment_msg_q_alloc (segment, 0, cfg);
Florin Coras99368312018-08-02 10:45:44 -0700941
942 if (props->use_mq_eventfd)
943 {
Florin Coras86f12322021-01-22 15:05:14 -0800944 if (svm_msg_q_alloc_eventfd (q))
Florin Coras99368312018-08-02 10:45:44 -0700945 clib_warning ("failed to alloc eventfd");
946 }
Florin Corasa5464812017-04-19 13:00:05 -0700947 return q;
948}
949
Florin Coras88001c62019-04-24 14:44:46 -0700950svm_msg_q_t *
951segment_manager_event_queue (segment_manager_t * sm)
952{
953 return sm->event_queue;
954}
955
Florin Corasa5464812017-04-19 13:00:05 -0700956/**
957 * Frees shm queue allocated in the first segment
958 */
959void
Florin Corase86a8ed2018-01-05 03:20:25 -0800960segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700961{
Florin Coras88001c62019-04-24 14:44:46 -0700962 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800963 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700964 void *oldheap;
965
Florin Corasa332c462018-01-31 06:52:17 -0800966 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700967
Florin Corasa332c462018-01-31 06:52:17 -0800968 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700969 sh = segment->ssvm.sh;
970
971 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800972 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700973 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800974 segment_manager_segment_reader_unlock (sm);
975}
976
977/*
978 * Init segment vm address allocator
979 */
980void
Florin Coras9a45bd82020-12-28 16:28:07 -0800981segment_manager_main_init (void)
Florin Corasa332c462018-01-31 06:52:17 -0800982{
Florin Coras88001c62019-04-24 14:44:46 -0700983 segment_manager_main_t *sm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -0700984
985 sm->default_fifo_size = 1 << 12;
986 sm->default_segment_size = 1 << 20;
987 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000988 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000989 sm->default_high_watermark = 80;
990 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -0700991}
992
Florin Corasc87c91d2017-08-16 19:55:49 -0700993static clib_error_t *
994segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
995 vlib_cli_command_t * cmd)
996{
Florin Coras88001c62019-04-24 14:44:46 -0700997 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800998 u8 show_segments = 0, verbose = 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000999 uword max_fifo_size;
Florin Coras5368bb02019-06-09 09:24:33 -07001000 segment_manager_t *sm;
1001 fifo_segment_t *seg;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001002 app_worker_t *app_wrk;
1003 application_t *app;
1004 u8 custom_logic;
Dave Barach91f3e742017-09-01 19:12:11 -04001005
Florin Corasc87c91d2017-08-16 19:55:49 -07001006 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1007 {
1008 if (unformat (input, "segments"))
1009 show_segments = 1;
1010 else if (unformat (input, "verbose"))
1011 verbose = 1;
1012 else
1013 return clib_error_return (0, "unknown input `%U'",
1014 format_unformat_error, input);
1015 }
1016 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -08001017 pool_elts (smm->segment_managers));
1018 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -07001019 {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001020 vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
1021 "Index", "AppIndex", "Segments", "MaxFifoSize",
1022 "HighWater", "LowWater", "FifoTuning");
Florin Corasc87c91d2017-08-16 19:55:49 -07001023
1024 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001025 pool_foreach (sm, smm->segment_managers) {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001026 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
1027 app = app_wrk ? application_get (app_wrk->app_index) : 0;
1028 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001029 max_fifo_size = sm->max_fifo_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001030
1031 vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
1032 segment_manager_index (sm),
1033 sm->app_wrk_index, pool_elts (sm->segments),
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001034 format_memory_size, max_fifo_size,
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001035 sm->high_watermark, sm->low_watermark,
1036 custom_logic ? "custom" : "none");
Damjan Marionb2c31b62020-12-13 21:47:40 +01001037 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001038 /* *INDENT-ON* */
1039
Florin Coras2a7c0b62020-09-28 23:40:28 -07001040 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -07001041 }
1042 if (show_segments)
1043 {
Florin Coras5368bb02019-06-09 09:24:33 -07001044 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -07001045
1046 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001047 pool_foreach (sm, smm->segment_managers) {
Florin Corasa332c462018-01-31 06:52:17 -08001048 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -07001049 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -08001050 }));
Damjan Marionb2c31b62020-12-13 21:47:40 +01001051 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001052 /* *INDENT-ON* */
1053
1054 }
1055 return 0;
1056}
1057
Florin Corasad0c77f2017-11-09 18:00:15 -08001058/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -07001059VLIB_CLI_COMMAND (segment_manager_show_command, static) =
1060{
1061 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -04001062 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -07001063 .function = segment_manager_show_fn,
1064};
1065/* *INDENT-ON* */
1066
Florin Coras88001c62019-04-24 14:44:46 -07001067void
1068segment_manager_format_sessions (segment_manager_t * sm, int verbose)
1069{
Florin Coras88001c62019-04-24 14:44:46 -07001070 vlib_main_t *vm = vlib_get_main ();
1071 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -08001072 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001073 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001074 int slice_index;
1075 u8 *s = 0, *str;
1076 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001077
1078 if (!sm)
1079 {
1080 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001081 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s%-15s%-10s",
1082 "Connection", "App", "API Client", "SegManager");
Florin Coras88001c62019-04-24 14:44:46 -07001083 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001084 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s", "Connection",
1085 "App");
Florin Coras88001c62019-04-24 14:44:46 -07001086 return;
1087 }
1088
1089 app_wrk = app_worker_get (sm->app_wrk_index);
1090 app_name = application_name_from_index (app_wrk->app_index);
1091
1092 clib_rwlock_reader_lock (&sm->segments_rwlock);
1093
1094 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001095 pool_foreach (fs, sm->segments) {
Florin Coras62ddc032019-12-08 18:30:42 -08001096 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001097 {
Florin Coras62ddc032019-12-08 18:30:42 -08001098 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1099 while (f)
1100 {
1101 u32 session_index, thread_index;
1102 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001103
Florin Corasc547e912020-12-08 17:50:45 -08001104 session_index = f->shr->master_session_index;
1105 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001106
Florin Corasc547e912020-12-08 17:50:45 -08001107 session = session_get (session_index, thread_index);
1108 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001109
Florin Corasc547e912020-12-08 17:50:45 -08001110 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001111 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v%-15u%-10u", str,
1112 app_name, app_wrk->api_client_index,
Florin Corasc547e912020-12-08 17:50:45 -08001113 app_wrk->connects_seg_manager);
1114 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001115 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001116
Florin Corasc547e912020-12-08 17:50:45 -08001117 vlib_cli_output (vm, "%v", s);
1118 vec_reset_length (s);
1119 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001120
Florin Corasc547e912020-12-08 17:50:45 -08001121 f = f->next;
1122 }
1123 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001124 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001125 }
Florin Coras88001c62019-04-24 14:44:46 -07001126 /* *INDENT-ON* */
1127
1128 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1129}
1130
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001131void
1132segment_manager_set_watermarks (segment_manager_t * sm,
1133 u8 high_watermark, u8 low_watermark)
1134{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001135 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001136 low_watermark <= high_watermark);
1137
1138 sm->high_watermark = high_watermark;
1139 sm->low_watermark = low_watermark;
1140}
1141
Florin Coras6cf30ad2017-04-04 23:08:23 -07001142/*
1143 * fd.io coding-style-patch-verification: ON
1144 *
1145 * Local Variables:
1146 * eval: (c-set-style "gnu")
1147 * End:
1148 */