2 #include <vnet/plugin/plugin.h>
3 #include <vpp/app/version.h>
4 #include <vnet/session/session.h>
5 #include <vnet/session/segment_manager.h>
6 #include <vnet/session/application.h>
8 #define SEG_MGR_TEST_I(_cond, _comment, _args...) \
10 int _evald = (_cond); \
12 fformat(stderr, "FAIL:%d: " _comment "\n", \
15 fformat(stderr, "PASS:%d: " _comment "\n", \
21 #define SEG_MGR_TEST(_cond, _comment, _args...) \
23 if (!SEG_MGR_TEST_I(_cond, _comment, ##_args)) { \
28 #define ST_DBG(_comment, _args...) \
29 fformat(stderr, _comment "\n", ##_args); \
31 #define SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE(x) (x >> 32)
33 /* placeholder callback functions */
35 placeholder_session_reset_callback (session_t * s)
37 clib_warning ("called...");
41 placeholder_session_connected_callback (u32 app_index, u32 api_context,
42 session_t * s, session_error_t err)
44 clib_warning ("called...");
49 placeholder_add_segment_callback (u32 client_index, u64 segment_handle)
51 clib_warning ("called...");
56 placeholder_del_segment_callback (u32 client_index, u64 segment_handle)
58 clib_warning ("called...");
63 placeholder_session_disconnect_callback (session_t * s)
65 clib_warning ("called...");
69 placeholder_session_accept_callback (session_t * s)
71 clib_warning ("called...");
76 placeholder_server_rx_callback (session_t * s)
78 clib_warning ("called...");
83 static session_cb_vft_t placeholder_session_cbs = {
84 .session_reset_callback = placeholder_session_reset_callback,
85 .session_connected_callback = placeholder_session_connected_callback,
86 .session_accept_callback = placeholder_session_accept_callback,
87 .session_disconnect_callback = placeholder_session_disconnect_callback,
88 .builtin_app_rx_callback = placeholder_server_rx_callback,
89 .add_segment_callback = placeholder_add_segment_callback,
90 .del_segment_callback = placeholder_del_segment_callback,
94 static char *states_str[] = {
95 #define _(sym,str) str,
96 foreach_segment_mem_status
100 static u32 size_4KB = 4 << 10;
101 static u32 size_8KB = 8 << 10;
102 static u32 size_12KB = 12 << 10;
103 static u32 size_16KB = 16 << 10;
104 static u32 size_20KB = 20 << 10;
105 static u32 size_32KB = 32 << 10;
106 static u32 size_52KB = 52 << 10;
107 static u32 size_64KB = 64 << 10;
108 static u32 size_128KB = 128 << 10;
109 static u32 size_1MB = 1 << 20;
110 static u32 size_2MB = 2 << 20;
114 segment_manager_test_pressure_1 (vlib_main_t * vm, unformat_input_t * input)
117 segment_manager_t *sm;
118 fifo_segment_t *fs0, *fs;
119 svm_fifo_t *rx_fifo, *tx_fifo;
120 uword app_seg_size = size_2MB;
121 u32 fifo_size = size_128KB;
122 u64 options[APP_OPTIONS_N_OPTIONS];
125 memset (&options, 0, sizeof (options));
127 vnet_app_attach_args_t attach_args = {
128 .api_client_index = ~0,
131 .session_cb_vft = &placeholder_session_cbs,
132 .name = format (0, "segment_manager_test_pressure_1"),
135 attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
136 attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
137 attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
138 attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
139 rv = vnet_application_attach (&attach_args);
140 SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
143 segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
144 (attach_args.segment_handle));
145 SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
147 /* initial status : (0 / 2MB) */
148 fs0 = segment_manager_get_segment (sm, 0);
149 rv = fifo_segment_get_mem_status (fs0);
150 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
151 "fifo_segment_get_mem_status %s", states_str[rv]);
154 /* allocate a fifo : 128KB x2 */
155 rv = segment_manager_alloc_session_fifos (sm,
156 vlib_get_thread_index (),
158 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
160 svm_fifo_set_size (rx_fifo, size_1MB);
161 svm_fifo_set_size (tx_fifo, size_1MB);
163 fs = segment_manager_get_segment (sm, rx_fifo->segment_index);
164 SEG_MGR_TEST ((fs == fs0), "fs %p", fs);
166 /* fill fifos (but not add chunks) */
167 svm_fifo_enqueue (rx_fifo, fifo_size - 1, data);
168 svm_fifo_enqueue (tx_fifo, fifo_size - 1, data);
170 /* 256KB+ / 2048KB+ => ~12% */
171 rv = fifo_segment_get_mem_status (fs);
172 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
173 "fifo_segment_get_mem_status %s", states_str[rv]);
176 svm_fifo_enqueue (rx_fifo, fifo_size, data);
177 svm_fifo_enqueue (rx_fifo, fifo_size, data);
178 svm_fifo_enqueue (rx_fifo, fifo_size, data);
179 svm_fifo_enqueue (tx_fifo, fifo_size, data);
180 svm_fifo_enqueue (tx_fifo, fifo_size, data);
181 svm_fifo_enqueue (tx_fifo, fifo_size, data);
184 rv = fifo_segment_get_mem_status (fs);
185 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
186 "fifo_segment_get_mem_status %s", states_str[rv]);
189 svm_fifo_enqueue (rx_fifo, fifo_size, data);
190 svm_fifo_enqueue (tx_fifo, fifo_size, data);
192 /* 10 chunks : 61% */
193 rv = fifo_segment_get_mem_status (fs);
194 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
195 "fifo_segment_get_mem_status %s", states_str[rv]);
198 svm_fifo_enqueue (rx_fifo, fifo_size, data);
199 svm_fifo_enqueue (rx_fifo, fifo_size, data);
200 svm_fifo_enqueue (tx_fifo, fifo_size, data);
201 svm_fifo_enqueue (tx_fifo, fifo_size, data);
203 /* 14 chunks : 85% */
204 rv = fifo_segment_get_mem_status (fs);
205 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
206 "fifo_segment_get_mem_status %s", states_str[rv]);
210 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
211 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
212 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
213 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
215 /* 10 chunks : 61% */
216 rv = fifo_segment_get_mem_status (fs);
217 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
218 "fifo_segment_get_mem_status %s", states_str[rv]);
222 svm_fifo_enqueue (rx_fifo, fifo_size, data);
223 svm_fifo_enqueue (rx_fifo, fifo_size, data);
224 svm_fifo_enqueue (tx_fifo, fifo_size, data);
225 svm_fifo_enqueue (tx_fifo, fifo_size, data);
227 /* 14 chunks : 85% */
228 rv = fifo_segment_get_mem_status (fs);
229 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
230 "fifo_segment_get_mem_status %s", states_str[rv]);
232 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
233 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
234 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
235 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
238 /* 10 chunks : 61% */
239 rv = fifo_segment_get_mem_status (fs);
240 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
241 "fifo_segment_get_mem_status %s", states_str[rv]);
244 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
245 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
246 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
247 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
248 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
249 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
250 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
251 svm_fifo_dequeue_drop (tx_fifo, fifo_size);
254 rv = fifo_segment_get_mem_status (fs);
255 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
256 "fifo_segment_get_mem_status %s", states_str[rv]);
259 vnet_app_detach_args_t detach_args = {
260 .app_index = attach_args.app_index,
261 .api_client_index = ~0,
263 rv = vnet_application_detach (&detach_args);
264 SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
270 segment_manager_test_pressure_2 (vlib_main_t * vm, unformat_input_t * input)
273 segment_manager_t *sm;
274 fifo_segment_t *fs0, *fs;
275 svm_fifo_t *rx_fifo, *tx_fifo;
276 uword app_seg_size = size_2MB;
277 u32 fifo_size = size_4KB;
278 u64 options[APP_OPTIONS_N_OPTIONS];
281 memset (&options, 0, sizeof (options));
283 vnet_app_attach_args_t attach_args = {
284 .api_client_index = ~0,
287 .session_cb_vft = &placeholder_session_cbs,
288 .name = format (0, "segment_manager_test_pressure_1"),
291 attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
292 attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
293 attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
294 attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
295 rv = vnet_application_attach (&attach_args);
296 SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
299 segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
300 (attach_args.segment_handle));
301 SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
303 /* initial status : (0 / 2MB) */
304 fs0 = segment_manager_get_segment (sm, 0);
305 rv = fifo_segment_get_mem_status (fs0);
306 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
307 "fifo_segment_get_mem_status %s", states_str[rv]);
310 /* allocate fifos : 4KB x2 */
311 rv = segment_manager_alloc_session_fifos (sm,
312 vlib_get_thread_index (),
314 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
316 svm_fifo_set_size (rx_fifo, size_2MB);
317 svm_fifo_set_size (tx_fifo, size_2MB);
319 /* fill fifos (but not add chunks) */
320 svm_fifo_enqueue (rx_fifo, fifo_size - 1, data);
321 svm_fifo_enqueue (tx_fifo, fifo_size - 1, data);
323 fs = segment_manager_get_segment (sm, rx_fifo->segment_index);
326 for (i = 0; i < 509; ++i)
328 svm_fifo_enqueue (rx_fifo, fifo_size, data);
331 /* 510 chunks : 100% of 2MB */
332 rv = fifo_segment_get_mem_status (fs);
333 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
334 "fifo_segment_get_mem_status %s", states_str[rv]);
336 /* this fifo growth is expected to fail */
337 rv = svm_fifo_enqueue (rx_fifo, fifo_size, data);
338 SEG_MGR_TEST ((rv == SVM_FIFO_EGROW), "svm_fifo_enqueue %d", rv);
340 /* then, no-memory is detected */
341 rv = fifo_segment_get_mem_status (fs);
342 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY),
343 "fifo_segment_get_mem_status %s", states_str[rv]);
346 for (i = 0; i < 20; ++i)
348 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
351 /* 489 chunks : 96%, it is high-pressure level
352 * but the reached-mem-limit record is not reset
353 * so the no-memory state lasts.
355 rv = fifo_segment_get_mem_status (fs);
356 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY),
357 "fifo_segment_get_mem_status %s", states_str[rv]);
360 for (i = 0; i < 133; ++i)
362 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
365 /* 356 chunks : 70% of 2MB */
366 rv = fifo_segment_get_mem_status (fs);
367 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
368 "fifo_segment_get_mem_status %s", states_str[rv]);
371 for (i = 0; i < 354; ++i)
373 svm_fifo_dequeue_drop (rx_fifo, fifo_size);
376 /* 2 chunks : 3% of 2MB */
377 rv = fifo_segment_get_mem_status (fs);
378 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
379 "fifo_segment_get_mem_status %s", states_str[rv]);
382 vnet_app_detach_args_t detach_args = {
383 .app_index = attach_args.app_index,
384 .api_client_index = ~0,
386 rv = vnet_application_detach (&detach_args);
387 SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
393 segment_manager_test_fifo_balanced_alloc (vlib_main_t * vm,
394 unformat_input_t * input)
397 segment_manager_t *sm;
398 fifo_segment_t *fs[4];
399 svm_fifo_t *rx_fifo[4], *tx_fifo[4];
400 uword app_seg_size = size_2MB;
401 u32 fifo_size = size_4KB;
402 u64 options[APP_OPTIONS_N_OPTIONS];
405 memset (&options, 0, sizeof (options));
407 vnet_app_attach_args_t attach_args = {
408 .api_client_index = ~0,
411 .session_cb_vft = &placeholder_session_cbs,
412 .name = format (0, "segment_manager_test_pressure_1"),
415 attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
416 attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
417 attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
418 attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
419 rv = vnet_application_attach (&attach_args);
420 SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
423 segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
424 (attach_args.segment_handle));
425 SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
427 /* initial status : (0 / 2MB) */
428 fs[0] = segment_manager_get_segment (sm, 0);
429 rv = fifo_segment_get_mem_status (fs[0]);
430 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
431 "fifo_segment_get_mem_status %s", states_str[rv]);
433 /* allocate fifos : 4KB x2 */
434 rv = segment_manager_alloc_session_fifos (sm,
435 vlib_get_thread_index (),
436 &rx_fifo[0], &tx_fifo[0]);
437 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
438 SEG_MGR_TEST ((rx_fifo[0]->segment_index == 0),
439 "segment_index %d", rx_fifo[0]->segment_index);
440 SEG_MGR_TEST ((tx_fifo[0]->segment_index == 0),
441 "segment_index %d", tx_fifo[0]->segment_index);
444 svm_fifo_set_size (rx_fifo[0], size_1MB);
445 for (i = 0; i < 200; ++i)
447 svm_fifo_enqueue (rx_fifo[0], fifo_size, data);
450 /* add another 2MB segment */
451 fs_index = segment_manager_add_segment (sm, size_2MB, 0);
452 SEG_MGR_TEST ((fs_index == 1), "fs_index %d", fs_index);
454 /* allocate fifos : 4KB x2
455 * expected to be allocated on the newer segment,
456 * because the usage of the first segment is high.
458 rv = segment_manager_alloc_session_fifos (sm,
459 vlib_get_thread_index (),
460 &rx_fifo[1], &tx_fifo[1]);
461 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
462 SEG_MGR_TEST ((rx_fifo[1]->segment_index == 1),
463 "segment_index %d", rx_fifo[1]->segment_index);
464 SEG_MGR_TEST ((tx_fifo[1]->segment_index == 1),
465 "segment_index %d", tx_fifo[1]->segment_index);
467 /* allocate fifos : 4KB x2
468 * expected to be allocated on the newer segment.
470 rv = segment_manager_alloc_session_fifos (sm,
471 vlib_get_thread_index (),
472 &rx_fifo[2], &tx_fifo[2]);
473 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
474 SEG_MGR_TEST ((rx_fifo[2]->segment_index == 1),
475 "segment_index %d", rx_fifo[2]->segment_index);
476 SEG_MGR_TEST ((tx_fifo[2]->segment_index == 1),
477 "segment_index %d", tx_fifo[2]->segment_index);
479 /* grow fifos, so the usage of the secong segment becomes
480 * higher than the first one.
482 svm_fifo_set_size (rx_fifo[1], size_1MB);
483 for (i = 0; i < 400; ++i)
485 svm_fifo_enqueue (rx_fifo[1], fifo_size, data);
488 /* allocate fifos : 4KB x2
489 * expected to be allocated on the first segment.
491 rv = segment_manager_alloc_session_fifos (sm,
492 vlib_get_thread_index (),
493 &rx_fifo[3], &tx_fifo[3]);
494 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
495 SEG_MGR_TEST ((rx_fifo[3]->segment_index == 0),
496 "segment_index %d", rx_fifo[3]->segment_index);
497 SEG_MGR_TEST ((tx_fifo[3]->segment_index == 0),
498 "segment_index %d", tx_fifo[3]->segment_index);
502 vnet_app_detach_args_t detach_args = {
503 .app_index = attach_args.app_index,
504 .api_client_index = ~0,
506 rv = vnet_application_detach (&detach_args);
507 SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
513 segment_manager_test_fifo_ops (vlib_main_t * vm, unformat_input_t * input)
516 segment_manager_t *sm;
518 svm_fifo_t *rx_fifo, *tx_fifo;
519 uword app_seg_size = size_2MB, most_grown = 0;
520 u32 fifo_size = size_4KB;
522 u64 options[APP_OPTIONS_N_OPTIONS];
525 memset (&options, 0, sizeof (options));
527 vnet_app_attach_args_t attach_args = {
528 .api_client_index = ~0,
531 .session_cb_vft = &placeholder_session_cbs,
532 .name = format (0, "segment_manager_test_pressure_1"),
535 attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
536 attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
537 attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
538 attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
539 rv = vnet_application_attach (&attach_args);
540 SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
543 segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
544 (attach_args.segment_handle));
545 SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
547 /* initial status : (0 / 2MB) */
548 fs = segment_manager_get_segment (sm, 0);
549 rv = fifo_segment_get_mem_status (fs);
550 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
551 "fifo_segment_get_mem_status %s", states_str[rv]);
553 /* allocate fifos : 4KB x2 */
554 rv = segment_manager_alloc_session_fifos (sm,
555 vlib_get_thread_index (),
557 SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
559 /* check the initial fifo size : 4KB */
560 rv = svm_fifo_size (rx_fifo);
561 SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_size %d", rv);
564 rv = svm_fifo_enqueue (rx_fifo, size_4KB, data);
565 SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv);
566 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
567 SEG_MGR_TEST ((max_dequeue == size_4KB), "max_dequeue %u", max_dequeue);
569 /* grow the fifo size : 4KB -> 8KB */
570 svm_fifo_set_size (rx_fifo, size_8KB);
571 rv = svm_fifo_size (rx_fifo);
572 SEG_MGR_TEST ((rv == size_8KB), "svm_fifo_size %d", rv);
574 /* verify that fifo cannot grow larger than the fifo size */
575 /* 4KB + 8KB > 8KB, so only 4KB is queued */
576 rv = svm_fifo_enqueue (rx_fifo, size_8KB, data);
577 SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv);
578 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
579 SEG_MGR_TEST ((max_dequeue == size_8KB), "max_dequeue %u", max_dequeue);
581 /* grow the fifo size : 8KB -> 16KB */
582 svm_fifo_set_size (rx_fifo, size_16KB);
584 /* 8KB + 4KB = 12KB */
585 svm_fifo_enqueue (rx_fifo, size_4KB, data);
586 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
587 SEG_MGR_TEST ((max_dequeue == size_12KB), "max_dequeue %u", max_dequeue);
589 /* grow the fifo size : 16KB -> 32KB */
590 svm_fifo_set_size (rx_fifo, size_32KB);
592 /* 12KB + 8KB = 20KB */
593 svm_fifo_enqueue (rx_fifo, size_8KB, data);
594 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
595 SEG_MGR_TEST ((max_dequeue == size_20KB), "max_dequeue %u", max_dequeue);
597 /* grow the fifo size : 32KB -> 64KB */
598 svm_fifo_set_size (rx_fifo, size_64KB);
600 /* 20KB + 32KB = 52KB */
601 svm_fifo_enqueue (rx_fifo, size_32KB, data);
602 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
603 SEG_MGR_TEST ((max_dequeue == size_52KB), "max_dequeue %u", max_dequeue);
606 for (i = 0; i < 55; ++i)
608 svm_fifo_set_size (rx_fifo, svm_fifo_size (rx_fifo) + size_32KB);
609 svm_fifo_enqueue (rx_fifo, size_32KB, data);
611 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
612 SEG_MGR_TEST ((max_dequeue == (size_52KB + size_32KB * 55)),
613 "max_dequeue %u", max_dequeue);
614 rv = fifo_segment_get_mem_status (fs);
615 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
616 "fifo_segment_get_mem_status %s", states_str[rv]);
617 most_grown = svm_fifo_size (rx_fifo);
620 svm_fifo_dequeue_drop (rx_fifo, size_20KB);
621 svm_fifo_dequeue_drop (rx_fifo, size_32KB);
624 for (i = 0; i < 20; ++i)
625 svm_fifo_dequeue_drop (rx_fifo, size_32KB);
626 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
627 SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u",
629 rv = fifo_segment_get_mem_status (fs);
630 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
631 "fifo_segment_get_mem_status %s", states_str[rv]);
634 for (i = 0; i < 10; ++i)
635 svm_fifo_dequeue_drop (rx_fifo, size_32KB);
636 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
637 SEG_MGR_TEST ((max_dequeue == (size_32KB * 25)), "max_dequeue %u",
639 rv = fifo_segment_get_mem_status (fs);
640 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
641 "fifo_segment_get_mem_status %s", states_str[rv]);
644 for (i = 0; i < 30; ++i)
645 svm_fifo_enqueue (rx_fifo, size_32KB, data);
646 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
647 SEG_MGR_TEST ((max_dequeue == (size_32KB * 55)), "max_dequeue %u",
649 rv = fifo_segment_get_mem_status (fs);
650 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
651 "fifo_segment_get_mem_status %s", states_str[rv]);
654 for (i = 0; i < 20; ++i)
655 svm_fifo_dequeue_drop (rx_fifo, size_32KB);
656 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
657 SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u",
659 rv = fifo_segment_get_mem_status (fs);
660 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
661 "fifo_segment_get_mem_status %s", states_str[rv]);
664 for (i = 0; i < 35; ++i)
665 svm_fifo_dequeue_drop (rx_fifo, size_32KB);
666 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
667 SEG_MGR_TEST ((max_dequeue == 0), "max_dequeue %u", max_dequeue);
668 rv = fifo_segment_get_mem_status (fs);
669 SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
670 "fifo_segment_get_mem_status %s", states_str[rv]);
672 /* (virtual) fifo size is still large as it is not updated */
673 SEG_MGR_TEST ((rx_fifo->shr->size == most_grown), "rx_fifo->size %u",
676 vnet_app_detach_args_t detach_args = {
677 .app_index = attach_args.app_index,
678 .api_client_index = ~0,
680 rv = vnet_application_detach (&detach_args);
681 SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
687 segment_manager_test_prealloc_hdrs (vlib_main_t * vm,
688 unformat_input_t * input)
690 u32 fifo_size = size_4KB, prealloc_hdrs, sm_index, fs_index;
691 u64 options[APP_OPTIONS_N_OPTIONS];
692 uword app_seg_size = size_2MB;
693 segment_manager_t *sm;
697 memset (&options, 0, sizeof (options));
699 vnet_app_attach_args_t attach_args = {
700 .api_client_index = ~0,
703 .session_cb_vft = &placeholder_session_cbs,
704 .name = format (0, "segment_manager_prealloc_hdrs"),
707 prealloc_hdrs = (app_seg_size - (16 << 10)) / sizeof (svm_fifo_t);
709 attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
710 attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
711 attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
712 attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
713 attach_args.options[APP_OPTIONS_PREALLOC_FIFO_HDRS] = prealloc_hdrs;
715 rv = vnet_application_attach (&attach_args);
716 vec_free (attach_args.name);
718 SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
720 segment_manager_parse_segment_handle (attach_args.segment_handle, &sm_index,
722 sm = segment_manager_get (sm_index);
724 SEG_MGR_TEST ((sm != 0), "seg manager is valid", sm);
726 fs = segment_manager_get_segment (sm, fs_index);
727 SEG_MGR_TEST (fifo_segment_num_free_fifos (fs) == prealloc_hdrs,
728 "prealloc should be %u", prealloc_hdrs);
730 vnet_app_detach_args_t detach_args = {
731 .app_index = attach_args.app_index,
732 .api_client_index = ~0,
734 rv = vnet_application_detach (&detach_args);
735 SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
739 static clib_error_t *
740 segment_manager_test (vlib_main_t * vm,
741 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
745 vnet_session_enable_disable (vm, 1);
747 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
749 if (unformat (input, "pressure_levels_1"))
750 res = segment_manager_test_pressure_1 (vm, input);
751 else if (unformat (input, "pressure_levels_2"))
752 res = segment_manager_test_pressure_2 (vm, input);
753 else if (unformat (input, "alloc"))
754 res = segment_manager_test_fifo_balanced_alloc (vm, input);
755 else if (unformat (input, "fifo_ops"))
756 res = segment_manager_test_fifo_ops (vm, input);
757 else if (unformat (input, "prealloc_hdrs"))
758 res = segment_manager_test_prealloc_hdrs (vm, input);
760 else if (unformat (input, "all"))
762 if ((res = segment_manager_test_pressure_1 (vm, input)))
764 if ((res = segment_manager_test_pressure_2 (vm, input)))
766 if ((res = segment_manager_test_fifo_balanced_alloc (vm, input)))
768 if ((res = segment_manager_test_fifo_ops (vm, input)))
770 if ((res = segment_manager_test_prealloc_hdrs (vm, input)))
779 return clib_error_return (0, "Segment manager unit test failed.");
784 VLIB_CLI_COMMAND (tcp_test_command, static) =
786 .path = "test segment-manager",
787 .short_help = "test segment manager [pressure_levels_1]"
788 "[pressure_level_2][alloc][fifo_ops][prealloc_hdrs][all]",
789 .function = segment_manager_test,
793 * fd.io coding-style-patch-verification: ON
796 * eval: (c-set-style "gnu")