session svm: tracking segment memory usage
[vpp.git] / src / plugins / unittest / segment_manager_test.c
1 #include <vnet/vnet.h>
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>
7
8 #define SEG_MGR_TEST_I(_cond, _comment, _args...)               \
9 ({                                                              \
10   int _evald = (_cond);                                         \
11   if (!(_evald)) {                                              \
12     fformat(stderr, "FAIL:%d: " _comment "\n",                  \
13             __LINE__, ##_args);                                 \
14   } else {                                                      \
15     fformat(stderr, "PASS:%d: " _comment "\n",                  \
16             __LINE__, ##_args);                                 \
17   }                                                             \
18   _evald;                                                       \
19 })
20
21 #define SEG_MGR_TEST(_cond, _comment, _args...)                 \
22 {                                                               \
23     if (!SEG_MGR_TEST_I(_cond, _comment, ##_args)) {            \
24         return 1;                                               \
25     }                                                           \
26 }
27
28 #define ST_DBG(_comment, _args...)                              \
29     fformat(stderr,  _comment "\n",  ##_args);                  \
30
31 #define SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE(x) (x >> 32)
32
33 /* dummy callback functions */
34 static void
35 dummy_session_reset_callback (session_t * s)
36 {
37   clib_warning ("called...");
38 }
39
40 static int
41 dummy_session_connected_callback (u32 app_index, u32 api_context,
42                                   session_t * s, u8 is_fail)
43 {
44   clib_warning ("called...");
45   return 0;
46 }
47
48 static int
49 dummy_add_segment_callback (u32 client_index, u64 segment_handle)
50 {
51   clib_warning ("called...");
52   return 0;
53 }
54
55 static int
56 dummy_del_segment_callback (u32 client_index, u64 segment_handle)
57 {
58   clib_warning ("called...");
59   return 0;
60 }
61
62 static void
63 dummy_session_disconnect_callback (session_t * s)
64 {
65   clib_warning ("called...");
66 }
67
68 static int
69 dummy_session_accept_callback (session_t * s)
70 {
71   clib_warning ("called...");
72   return 0;
73 }
74
75 static int
76 dummy_server_rx_callback (session_t * s)
77 {
78   clib_warning ("called...");
79   return -1;
80 }
81
82 /* *INDENT-OFF* */
83 static session_cb_vft_t dummy_session_cbs = {
84   .session_reset_callback = dummy_session_reset_callback,
85   .session_connected_callback = dummy_session_connected_callback,
86   .session_accept_callback = dummy_session_accept_callback,
87   .session_disconnect_callback = dummy_session_disconnect_callback,
88   .builtin_app_rx_callback = dummy_server_rx_callback,
89   .add_segment_callback = dummy_add_segment_callback,
90   .del_segment_callback = dummy_del_segment_callback,
91 };
92 /* *INDENT-ON* */
93
94 static char *states_str[] = {
95 #define _(sym,str) str,
96   foreach_segment_mem_status
97 #undef _
98 };
99
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;
111
112
113 static int
114 segment_manager_test_pressure_1 (vlib_main_t * vm, unformat_input_t * input)
115 {
116   int rv;
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];
123   u8 data[size_128KB];
124
125   memset (&options, 0, sizeof (options));
126
127   vnet_app_attach_args_t attach_args = {
128     .api_client_index = ~0,
129     .options = options,
130     .namespace_id = 0,
131     .session_cb_vft = &dummy_session_cbs,
132     .name = format (0, "segment_manager_test_pressure_1"),
133   };
134
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);
141
142   sm =
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);
146
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]);
152
153
154   /* allocate a fifo : 128KB x2 */
155   rv = segment_manager_alloc_session_fifos (sm,
156                                             vlib_get_thread_index (),
157                                             &rx_fifo, &tx_fifo);
158   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
159
160   svm_fifo_set_size (rx_fifo, size_1MB);
161   svm_fifo_set_size (tx_fifo, size_1MB);
162
163   fs = segment_manager_get_segment (sm, rx_fifo->segment_index);
164   SEG_MGR_TEST ((fs == fs0), "fs %p", fs);
165
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);
169
170   /* 256KB+ / 2048KB+ => ~12% */
171   fifo_segment_update_free_bytes (fs);
172   rv = fifo_segment_get_mem_status (fs);
173   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
174                 "fifo_segment_get_mem_status %s", states_str[rv]);
175
176   /* grow fifos */
177   svm_fifo_enqueue (rx_fifo, fifo_size, data);
178   svm_fifo_enqueue (rx_fifo, fifo_size, data);
179   svm_fifo_enqueue (rx_fifo, fifo_size, data);
180   svm_fifo_enqueue (tx_fifo, fifo_size, data);
181   svm_fifo_enqueue (tx_fifo, fifo_size, data);
182   svm_fifo_enqueue (tx_fifo, fifo_size, data);
183
184   /* 8 chunks : 49% */
185   fifo_segment_update_free_bytes (fs);
186   rv = fifo_segment_get_mem_status (fs);
187   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
188                 "fifo_segment_get_mem_status %s", states_str[rv]);
189
190   /* grow fifos */
191   svm_fifo_enqueue (rx_fifo, fifo_size, data);
192   svm_fifo_enqueue (tx_fifo, fifo_size, data);
193
194   /* 10 chunks : 61% */
195   fifo_segment_update_free_bytes (fs);
196   rv = fifo_segment_get_mem_status (fs);
197   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
198                 "fifo_segment_get_mem_status %s", states_str[rv]);
199
200   /* grow fifos */
201   svm_fifo_enqueue (rx_fifo, fifo_size, data);
202   svm_fifo_enqueue (rx_fifo, fifo_size, data);
203   svm_fifo_enqueue (tx_fifo, fifo_size, data);
204   svm_fifo_enqueue (tx_fifo, fifo_size, data);
205
206   /* 14 chunks : 85% */
207   fifo_segment_update_free_bytes (fs);
208   rv = fifo_segment_get_mem_status (fs);
209   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
210                 "fifo_segment_get_mem_status %s", states_str[rv]);
211
212
213   /* shrink fifos */
214   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
215   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
216   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
217   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
218
219   /* 10 chunks : 61% */
220   fifo_segment_update_free_bytes (fs);
221   rv = fifo_segment_get_mem_status (fs);
222   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
223                 "fifo_segment_get_mem_status %s", states_str[rv]);
224
225
226   /* grow fifos */
227   svm_fifo_enqueue (rx_fifo, fifo_size, data);
228   svm_fifo_enqueue (rx_fifo, fifo_size, data);
229   svm_fifo_enqueue (tx_fifo, fifo_size, data);
230   svm_fifo_enqueue (tx_fifo, fifo_size, data);
231
232   /* 14 chunks : 85% */
233   fifo_segment_update_free_bytes (fs);
234   rv = fifo_segment_get_mem_status (fs);
235   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
236                 "fifo_segment_get_mem_status %s", states_str[rv]);
237
238   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
239   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
240   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
241   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
242
243
244   /* 10 chunks : 61% */
245   fifo_segment_update_free_bytes (fs);
246   rv = fifo_segment_get_mem_status (fs);
247   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
248                 "fifo_segment_get_mem_status %s", states_str[rv]);
249
250   /* shrink fifos */
251   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
252   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
253   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
254   svm_fifo_dequeue_drop (rx_fifo, fifo_size);
255   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
256   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
257   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
258   svm_fifo_dequeue_drop (tx_fifo, fifo_size);
259
260   /* 2 chunks : 12% */
261   fifo_segment_update_free_bytes (fs);
262   rv = fifo_segment_get_mem_status (fs);
263   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
264                 "fifo_segment_get_mem_status %s", states_str[rv]);
265
266
267   vnet_app_detach_args_t detach_args = {
268     .app_index = attach_args.app_index,
269     .api_client_index = ~0,
270   };
271   rv = vnet_application_detach (&detach_args);
272   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
273
274   return 0;
275 }
276
277 static int
278 segment_manager_test_pressure_2 (vlib_main_t * vm, unformat_input_t * input)
279 {
280   int rv, i;
281   segment_manager_t *sm;
282   fifo_segment_t *fs0, *fs;
283   svm_fifo_t *rx_fifo, *tx_fifo;
284   uword app_seg_size = size_2MB;
285   u32 fifo_size = size_4KB;
286   u64 options[APP_OPTIONS_N_OPTIONS];
287   u8 data[size_4KB];
288
289   memset (&options, 0, sizeof (options));
290
291   vnet_app_attach_args_t attach_args = {
292     .api_client_index = ~0,
293     .options = options,
294     .namespace_id = 0,
295     .session_cb_vft = &dummy_session_cbs,
296     .name = format (0, "segment_manager_test_pressure_1"),
297   };
298
299   attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
300   attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
301   attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
302   attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
303   rv = vnet_application_attach (&attach_args);
304   SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
305
306   sm =
307     segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
308                          (attach_args.segment_handle));
309   SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
310
311   /* initial status : (0 / 2MB) */
312   fs0 = segment_manager_get_segment (sm, 0);
313   fifo_segment_update_free_bytes (fs0);
314   rv = fifo_segment_get_mem_status (fs0);
315   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
316                 "fifo_segment_get_mem_status %s", states_str[rv]);
317
318
319   /* allocate fifos : 4KB x2 */
320   rv = segment_manager_alloc_session_fifos (sm,
321                                             vlib_get_thread_index (),
322                                             &rx_fifo, &tx_fifo);
323   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
324
325   svm_fifo_set_size (rx_fifo, size_2MB);
326   svm_fifo_set_size (tx_fifo, size_2MB);
327
328   /* fill fifos (but not add chunks) */
329   svm_fifo_enqueue (rx_fifo, fifo_size - 1, data);
330   svm_fifo_enqueue (tx_fifo, fifo_size - 1, data);
331
332   fs = segment_manager_get_segment (sm, rx_fifo->segment_index);
333
334   /* grow fifos */
335   for (i = 0; i < 509; ++i)
336     {
337       svm_fifo_enqueue (rx_fifo, fifo_size, data);
338     }
339
340   /* 510 chunks : 100% of 2MB */
341   fifo_segment_update_free_bytes (fs);
342   rv = fifo_segment_get_mem_status (fs);
343   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
344                 "fifo_segment_get_mem_status %s", states_str[rv]);
345
346   /* this fifo growth is expected to fail */
347   rv = svm_fifo_enqueue (rx_fifo, fifo_size, data);
348   SEG_MGR_TEST ((rv == SVM_FIFO_EGROW), "svm_fifo_enqueue %d", rv);
349
350   /* then, no-memory is detected */
351   fifo_segment_update_free_bytes (fs);
352   rv = fifo_segment_get_mem_status (fs);
353   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY),
354                 "fifo_segment_get_mem_status %s", states_str[rv]);
355
356   /* shrink fifos */
357   for (i = 0; i < 20; ++i)
358     {
359       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
360     }
361
362   /* 489 chunks : 96%, it is high-pressure level
363    * but the reached-mem-limit record is not reset
364    * so the no-memory state lasts.
365    */
366   fifo_segment_update_free_bytes (fs);
367   rv = fifo_segment_get_mem_status (fs);
368   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY),
369                 "fifo_segment_get_mem_status %s", states_str[rv]);
370
371   /* shrink fifos */
372   for (i = 0; i < 133; ++i)
373     {
374       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
375     }
376
377   /* 356 chunks : 70% of 2MB */
378   fifo_segment_update_free_bytes (fs);
379   rv = fifo_segment_get_mem_status (fs);
380   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
381                 "fifo_segment_get_mem_status %s", states_str[rv]);
382
383   /* shrink fifos */
384   for (i = 0; i < 354; ++i)
385     {
386       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
387     }
388
389   /* 2 chunks : 3% of 2MB */
390   fifo_segment_update_free_bytes (fs);
391   rv = fifo_segment_get_mem_status (fs);
392   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
393                 "fifo_segment_get_mem_status %s", states_str[rv]);
394
395
396   vnet_app_detach_args_t detach_args = {
397     .app_index = attach_args.app_index,
398     .api_client_index = ~0,
399   };
400   rv = vnet_application_detach (&detach_args);
401   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
402
403   return 0;
404 }
405
406 static int
407 segment_manager_test_fifo_balanced_alloc (vlib_main_t * vm,
408                                           unformat_input_t * input)
409 {
410   int rv, i, fs_index;
411   segment_manager_t *sm;
412   fifo_segment_t *fs[4];
413   svm_fifo_t *rx_fifo[4], *tx_fifo[4];
414   uword app_seg_size = size_2MB;
415   u32 fifo_size = size_4KB;
416   u64 options[APP_OPTIONS_N_OPTIONS];
417   u8 data[size_4KB];
418
419   memset (&options, 0, sizeof (options));
420
421   vnet_app_attach_args_t attach_args = {
422     .api_client_index = ~0,
423     .options = options,
424     .namespace_id = 0,
425     .session_cb_vft = &dummy_session_cbs,
426     .name = format (0, "segment_manager_test_pressure_1"),
427   };
428
429   attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
430   attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
431   attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
432   attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
433   rv = vnet_application_attach (&attach_args);
434   SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
435
436   sm =
437     segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
438                          (attach_args.segment_handle));
439   SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
440
441   /* initial status : (0 / 2MB) */
442   fs[0] = segment_manager_get_segment (sm, 0);
443   rv = fifo_segment_get_mem_status (fs[0]);
444   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
445                 "fifo_segment_get_mem_status %s", states_str[rv]);
446
447   /* allocate fifos : 4KB x2 */
448   rv = segment_manager_alloc_session_fifos (sm,
449                                             vlib_get_thread_index (),
450                                             &rx_fifo[0], &tx_fifo[0]);
451   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
452   SEG_MGR_TEST ((rx_fifo[0]->segment_index == 0),
453                 "segment_index %d", rx_fifo[0]->segment_index);
454   SEG_MGR_TEST ((tx_fifo[0]->segment_index == 0),
455                 "segment_index %d", tx_fifo[0]->segment_index);
456
457   /* grow fifos */
458   svm_fifo_set_size (rx_fifo[0], size_1MB);
459   for (i = 0; i < 200; ++i)
460     {
461       svm_fifo_enqueue (rx_fifo[0], fifo_size, data);
462     }
463
464   /* add another 2MB segment */
465   fs_index = segment_manager_add_segment (sm, size_2MB);
466   SEG_MGR_TEST ((fs_index == 1), "fs_index %d", fs_index);
467
468   /* allocate fifos : 4KB x2
469    * expected to be allocated on the newer segment,
470    * because the usage of the first segment is high.
471    */
472   rv = segment_manager_alloc_session_fifos (sm,
473                                             vlib_get_thread_index (),
474                                             &rx_fifo[1], &tx_fifo[1]);
475   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
476   SEG_MGR_TEST ((rx_fifo[1]->segment_index == 1),
477                 "segment_index %d", rx_fifo[1]->segment_index);
478   SEG_MGR_TEST ((tx_fifo[1]->segment_index == 1),
479                 "segment_index %d", tx_fifo[1]->segment_index);
480
481   /* allocate fifos : 4KB x2
482    * expected to be allocated on the newer segment.
483    */
484   rv = segment_manager_alloc_session_fifos (sm,
485                                             vlib_get_thread_index (),
486                                             &rx_fifo[2], &tx_fifo[2]);
487   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
488   SEG_MGR_TEST ((rx_fifo[2]->segment_index == 1),
489                 "segment_index %d", rx_fifo[2]->segment_index);
490   SEG_MGR_TEST ((tx_fifo[2]->segment_index == 1),
491                 "segment_index %d", tx_fifo[2]->segment_index);
492
493   /* grow fifos, so the usage of the secong segment becomes
494    * higher than the first one.
495    */
496   svm_fifo_set_size (rx_fifo[1], size_1MB);
497   for (i = 0; i < 400; ++i)
498     {
499       svm_fifo_enqueue (rx_fifo[1], fifo_size, data);
500     }
501
502   /* allocate fifos : 4KB x2
503    * expected to be allocated on the first segment.
504    */
505   rv = segment_manager_alloc_session_fifos (sm,
506                                             vlib_get_thread_index (),
507                                             &rx_fifo[3], &tx_fifo[3]);
508   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
509   SEG_MGR_TEST ((rx_fifo[3]->segment_index == 0),
510                 "segment_index %d", rx_fifo[3]->segment_index);
511   SEG_MGR_TEST ((tx_fifo[3]->segment_index == 0),
512                 "segment_index %d", tx_fifo[3]->segment_index);
513
514
515
516   vnet_app_detach_args_t detach_args = {
517     .app_index = attach_args.app_index,
518     .api_client_index = ~0,
519   };
520   rv = vnet_application_detach (&detach_args);
521   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
522
523   return 0;
524 }
525
526 static int
527 segment_manager_test_fifo_ops (vlib_main_t * vm, unformat_input_t * input)
528 {
529   int rv, i;
530   segment_manager_t *sm;
531   fifo_segment_t *fs;
532   svm_fifo_t *rx_fifo, *tx_fifo;
533   uword app_seg_size = size_2MB, most_grown = 0;
534   u32 fifo_size = size_4KB;
535   u32 max_dequeue = 0;
536   u64 options[APP_OPTIONS_N_OPTIONS];
537   u8 data[size_128KB];
538
539   memset (&options, 0, sizeof (options));
540
541   vnet_app_attach_args_t attach_args = {
542     .api_client_index = ~0,
543     .options = options,
544     .namespace_id = 0,
545     .session_cb_vft = &dummy_session_cbs,
546     .name = format (0, "segment_manager_test_pressure_1"),
547   };
548
549   attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size;
550   attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
551   attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
552   attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
553   rv = vnet_application_attach (&attach_args);
554   SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
555
556   sm =
557     segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE
558                          (attach_args.segment_handle));
559   SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm);
560
561   /* initial status : (0 / 2MB) */
562   fs = segment_manager_get_segment (sm, 0);
563   rv = fifo_segment_get_mem_status (fs);
564   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
565                 "fifo_segment_get_mem_status %s", states_str[rv]);
566
567   /* allocate fifos : 4KB x2 */
568   rv = segment_manager_alloc_session_fifos (sm,
569                                             vlib_get_thread_index (),
570                                             &rx_fifo, &tx_fifo);
571   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
572
573   /* check the initial fifo size : 4KB */
574   rv = svm_fifo_size (rx_fifo);
575   SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_size %d", rv);
576
577   /* fill 4KB */
578   rv = svm_fifo_enqueue (rx_fifo, size_4KB, data);
579   SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv);
580   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
581   SEG_MGR_TEST ((max_dequeue == size_4KB), "max_dequeue %u", max_dequeue);
582
583   /* grow the fifo size : 4KB -> 8KB */
584   svm_fifo_set_size (rx_fifo, size_8KB);
585   rv = svm_fifo_size (rx_fifo);
586   SEG_MGR_TEST ((rv == size_8KB), "svm_fifo_size %d", rv);
587
588   /* verify that fifo cannot grow larger than the fifo size */
589   /* 4KB + 8KB > 8KB, so only 4KB is queued */
590   rv = svm_fifo_enqueue (rx_fifo, size_8KB, data);
591   SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv);
592   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
593   SEG_MGR_TEST ((max_dequeue == size_8KB), "max_dequeue %u", max_dequeue);
594
595   /* grow the fifo size : 8KB -> 16KB */
596   svm_fifo_set_size (rx_fifo, size_16KB);
597
598   /* 8KB + 4KB = 12KB */
599   svm_fifo_enqueue (rx_fifo, size_4KB, data);
600   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
601   SEG_MGR_TEST ((max_dequeue == size_12KB), "max_dequeue %u", max_dequeue);
602
603   /* grow the fifo size : 16KB -> 32KB */
604   svm_fifo_set_size (rx_fifo, size_32KB);
605
606   /* 12KB + 8KB = 20KB */
607   svm_fifo_enqueue (rx_fifo, size_8KB, data);
608   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
609   SEG_MGR_TEST ((max_dequeue == size_20KB), "max_dequeue %u", max_dequeue);
610
611   /* grow the fifo size : 32KB -> 64KB */
612   svm_fifo_set_size (rx_fifo, size_64KB);
613
614   /* 20KB + 32KB = 52KB */
615   svm_fifo_enqueue (rx_fifo, size_32KB, data);
616   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
617   SEG_MGR_TEST ((max_dequeue == size_52KB), "max_dequeue %u", max_dequeue);
618
619   /* bulk enqueue */
620   for (i = 0; i < 55; ++i)
621     {
622       svm_fifo_set_size (rx_fifo, svm_fifo_size (rx_fifo) + size_32KB);
623       svm_fifo_enqueue (rx_fifo, size_32KB, data);
624     }
625   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
626   SEG_MGR_TEST ((max_dequeue == (size_52KB + size_32KB * 55)),
627                 "max_dequeue %u", max_dequeue);
628   rv = fifo_segment_get_mem_status (fs);
629   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
630                 "fifo_segment_get_mem_status %s", states_str[rv]);
631   most_grown = svm_fifo_size (rx_fifo);
632
633   /* dequeue */
634   svm_fifo_dequeue_drop (rx_fifo, size_20KB);
635   svm_fifo_dequeue_drop (rx_fifo, size_32KB);
636
637   /* bulk dequeue */
638   for (i = 0; i < 20; ++i)
639     svm_fifo_dequeue_drop (rx_fifo, size_32KB);
640   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
641   SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u",
642                 max_dequeue);
643   rv = fifo_segment_get_mem_status (fs);
644   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
645                 "fifo_segment_get_mem_status %s", states_str[rv]);
646
647   /* bulk dequeue */
648   for (i = 0; i < 10; ++i)
649     svm_fifo_dequeue_drop (rx_fifo, size_32KB);
650   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
651   SEG_MGR_TEST ((max_dequeue == (size_32KB * 25)), "max_dequeue %u",
652                 max_dequeue);
653   rv = fifo_segment_get_mem_status (fs);
654   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
655                 "fifo_segment_get_mem_status %s", states_str[rv]);
656
657   /* bulk enqueue */
658   for (i = 0; i < 30; ++i)
659     svm_fifo_enqueue (rx_fifo, size_32KB, data);
660   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
661   SEG_MGR_TEST ((max_dequeue == (size_32KB * 55)), "max_dequeue %u",
662                 max_dequeue);
663   rv = fifo_segment_get_mem_status (fs);
664   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE),
665                 "fifo_segment_get_mem_status %s", states_str[rv]);
666
667   /* bulk dequeue */
668   for (i = 0; i < 20; ++i)
669     svm_fifo_dequeue_drop (rx_fifo, size_32KB);
670   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
671   SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u",
672                 max_dequeue);
673   rv = fifo_segment_get_mem_status (fs);
674   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE),
675                 "fifo_segment_get_mem_status %s", states_str[rv]);
676
677   /* bulk dequeue */
678   for (i = 0; i < 35; ++i)
679     svm_fifo_dequeue_drop (rx_fifo, size_32KB);
680   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
681   SEG_MGR_TEST ((max_dequeue == 0), "max_dequeue %u", max_dequeue);
682   rv = fifo_segment_get_mem_status (fs);
683   SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE),
684                 "fifo_segment_get_mem_status %s", states_str[rv]);
685
686   /* (virtual) fifo size is still large as it is not updated */
687   SEG_MGR_TEST ((rx_fifo->size == most_grown), "rx_fifo->size %u",
688                 rx_fifo->size);
689
690   vnet_app_detach_args_t detach_args = {
691     .app_index = attach_args.app_index,
692     .api_client_index = ~0,
693   };
694   rv = vnet_application_detach (&detach_args);
695   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
696
697   return 0;
698 }
699
700
701 static clib_error_t *
702 segment_manager_test (vlib_main_t * vm,
703                       unformat_input_t * input, vlib_cli_command_t * cmd_arg)
704 {
705   int res = 0;
706
707   vnet_session_enable_disable (vm, 1);
708
709   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
710     {
711       if (unformat (input, "pressure_levels_1"))
712         res = segment_manager_test_pressure_1 (vm, input);
713       else if (unformat (input, "pressure_levels_2"))
714         res = segment_manager_test_pressure_2 (vm, input);
715       else if (unformat (input, "alloc"))
716         res = segment_manager_test_fifo_balanced_alloc (vm, input);
717       else if (unformat (input, "fifo_ops"))
718         res = segment_manager_test_fifo_ops (vm, input);
719
720       else if (unformat (input, "all"))
721         {
722           if ((res = segment_manager_test_pressure_1 (vm, input)))
723             goto done;
724           if ((res = segment_manager_test_pressure_2 (vm, input)))
725             goto done;
726           if ((res = segment_manager_test_fifo_balanced_alloc (vm, input)))
727             goto done;
728           if ((res = segment_manager_test_fifo_ops (vm, input)))
729             goto done;
730         }
731       else
732         break;
733     }
734
735 done:
736   if (res)
737     return clib_error_return (0, "Segment manager unit test failed.");
738   return 0;
739 }
740
741 /* *INDENT-OFF* */
742 VLIB_CLI_COMMAND (tcp_test_command, static) =
743 {
744   .path = "test segment-manager",
745   .short_help = "test segment manager [pressure_levels_1]"
746                 "[pressure_level_2][alloc][fifo_ops][all]",
747   .function = segment_manager_test,
748 };
749
750 /*
751  * fd.io coding-style-patch-verification: ON
752  *
753  * Local Variables:
754  * eval: (c-set-style "gnu")
755  * End:
756  */
757