svm: split fifo into private and shared structs
[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 /* placeholder callback functions */
34 static void
35 placeholder_session_reset_callback (session_t * s)
36 {
37   clib_warning ("called...");
38 }
39
40 static int
41 placeholder_session_connected_callback (u32 app_index, u32 api_context,
42                                         session_t * s, session_error_t err)
43 {
44   clib_warning ("called...");
45   return 0;
46 }
47
48 static int
49 placeholder_add_segment_callback (u32 client_index, u64 segment_handle)
50 {
51   clib_warning ("called...");
52   return 0;
53 }
54
55 static int
56 placeholder_del_segment_callback (u32 client_index, u64 segment_handle)
57 {
58   clib_warning ("called...");
59   return 0;
60 }
61
62 static void
63 placeholder_session_disconnect_callback (session_t * s)
64 {
65   clib_warning ("called...");
66 }
67
68 static int
69 placeholder_session_accept_callback (session_t * s)
70 {
71   clib_warning ("called...");
72   return 0;
73 }
74
75 static int
76 placeholder_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 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,
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 = &placeholder_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   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]);
174
175   /* grow fifos */
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);
182
183   /* 8 chunks : 49% */
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]);
187
188   /* grow fifos */
189   svm_fifo_enqueue (rx_fifo, fifo_size, data);
190   svm_fifo_enqueue (tx_fifo, fifo_size, data);
191
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]);
196
197   /* grow fifos */
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);
202
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]);
207
208
209   /* shrink fifos */
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);
214
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]);
219
220
221   /* grow fifos */
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);
226
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]);
231
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);
236
237
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]);
242
243   /* shrink fifos */
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);
252
253   /* 2 chunks : 12% */
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]);
257
258
259   vnet_app_detach_args_t detach_args = {
260     .app_index = attach_args.app_index,
261     .api_client_index = ~0,
262   };
263   rv = vnet_application_detach (&detach_args);
264   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
265
266   return 0;
267 }
268
269 static int
270 segment_manager_test_pressure_2 (vlib_main_t * vm, unformat_input_t * input)
271 {
272   int rv, i;
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];
279   u8 data[size_4KB];
280
281   memset (&options, 0, sizeof (options));
282
283   vnet_app_attach_args_t attach_args = {
284     .api_client_index = ~0,
285     .options = options,
286     .namespace_id = 0,
287     .session_cb_vft = &placeholder_session_cbs,
288     .name = format (0, "segment_manager_test_pressure_1"),
289   };
290
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);
297
298   sm =
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);
302
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]);
308
309
310   /* allocate fifos : 4KB x2 */
311   rv = segment_manager_alloc_session_fifos (sm,
312                                             vlib_get_thread_index (),
313                                             &rx_fifo, &tx_fifo);
314   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
315
316   svm_fifo_set_size (rx_fifo, size_2MB);
317   svm_fifo_set_size (tx_fifo, size_2MB);
318
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);
322
323   fs = segment_manager_get_segment (sm, rx_fifo->segment_index);
324
325   /* grow fifos */
326   for (i = 0; i < 509; ++i)
327     {
328       svm_fifo_enqueue (rx_fifo, fifo_size, data);
329     }
330
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]);
335
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);
339
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]);
344
345   /* shrink fifos */
346   for (i = 0; i < 20; ++i)
347     {
348       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
349     }
350
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.
354    */
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]);
358
359   /* shrink fifos */
360   for (i = 0; i < 133; ++i)
361     {
362       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
363     }
364
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]);
369
370   /* shrink fifos */
371   for (i = 0; i < 354; ++i)
372     {
373       svm_fifo_dequeue_drop (rx_fifo, fifo_size);
374     }
375
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]);
380
381
382   vnet_app_detach_args_t detach_args = {
383     .app_index = attach_args.app_index,
384     .api_client_index = ~0,
385   };
386   rv = vnet_application_detach (&detach_args);
387   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
388
389   return 0;
390 }
391
392 static int
393 segment_manager_test_fifo_balanced_alloc (vlib_main_t * vm,
394                                           unformat_input_t * input)
395 {
396   int rv, i, fs_index;
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];
403   u8 data[size_4KB];
404
405   memset (&options, 0, sizeof (options));
406
407   vnet_app_attach_args_t attach_args = {
408     .api_client_index = ~0,
409     .options = options,
410     .namespace_id = 0,
411     .session_cb_vft = &placeholder_session_cbs,
412     .name = format (0, "segment_manager_test_pressure_1"),
413   };
414
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);
421
422   sm =
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);
426
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]);
432
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);
442
443   /* grow fifos */
444   svm_fifo_set_size (rx_fifo[0], size_1MB);
445   for (i = 0; i < 200; ++i)
446     {
447       svm_fifo_enqueue (rx_fifo[0], fifo_size, data);
448     }
449
450   /* add another 2MB segment */
451   fs_index = segment_manager_add_segment (sm, size_2MB);
452   SEG_MGR_TEST ((fs_index == 1), "fs_index %d", fs_index);
453
454   /* allocate fifos : 4KB x2
455    * expected to be allocated on the newer segment,
456    * because the usage of the first segment is high.
457    */
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);
466
467   /* allocate fifos : 4KB x2
468    * expected to be allocated on the newer segment.
469    */
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);
478
479   /* grow fifos, so the usage of the secong segment becomes
480    * higher than the first one.
481    */
482   svm_fifo_set_size (rx_fifo[1], size_1MB);
483   for (i = 0; i < 400; ++i)
484     {
485       svm_fifo_enqueue (rx_fifo[1], fifo_size, data);
486     }
487
488   /* allocate fifos : 4KB x2
489    * expected to be allocated on the first segment.
490    */
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);
499
500
501
502   vnet_app_detach_args_t detach_args = {
503     .app_index = attach_args.app_index,
504     .api_client_index = ~0,
505   };
506   rv = vnet_application_detach (&detach_args);
507   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
508
509   return 0;
510 }
511
512 static int
513 segment_manager_test_fifo_ops (vlib_main_t * vm, unformat_input_t * input)
514 {
515   int rv, i;
516   segment_manager_t *sm;
517   fifo_segment_t *fs;
518   svm_fifo_t *rx_fifo, *tx_fifo;
519   uword app_seg_size = size_2MB, most_grown = 0;
520   u32 fifo_size = size_4KB;
521   u32 max_dequeue = 0;
522   u64 options[APP_OPTIONS_N_OPTIONS];
523   u8 data[size_128KB];
524
525   memset (&options, 0, sizeof (options));
526
527   vnet_app_attach_args_t attach_args = {
528     .api_client_index = ~0,
529     .options = options,
530     .namespace_id = 0,
531     .session_cb_vft = &placeholder_session_cbs,
532     .name = format (0, "segment_manager_test_pressure_1"),
533   };
534
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);
541
542   sm =
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);
546
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]);
552
553   /* allocate fifos : 4KB x2 */
554   rv = segment_manager_alloc_session_fifos (sm,
555                                             vlib_get_thread_index (),
556                                             &rx_fifo, &tx_fifo);
557   SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv);
558
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);
562
563   /* fill 4KB */
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);
568
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);
573
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);
580
581   /* grow the fifo size : 8KB -> 16KB */
582   svm_fifo_set_size (rx_fifo, size_16KB);
583
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);
588
589   /* grow the fifo size : 16KB -> 32KB */
590   svm_fifo_set_size (rx_fifo, size_32KB);
591
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);
596
597   /* grow the fifo size : 32KB -> 64KB */
598   svm_fifo_set_size (rx_fifo, size_64KB);
599
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);
604
605   /* bulk enqueue */
606   for (i = 0; i < 55; ++i)
607     {
608       svm_fifo_set_size (rx_fifo, svm_fifo_size (rx_fifo) + size_32KB);
609       svm_fifo_enqueue (rx_fifo, size_32KB, data);
610     }
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);
618
619   /* dequeue */
620   svm_fifo_dequeue_drop (rx_fifo, size_20KB);
621   svm_fifo_dequeue_drop (rx_fifo, size_32KB);
622
623   /* bulk dequeue */
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",
628                 max_dequeue);
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]);
632
633   /* bulk dequeue */
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",
638                 max_dequeue);
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]);
642
643   /* bulk enqueue */
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",
648                 max_dequeue);
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]);
652
653   /* bulk dequeue */
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",
658                 max_dequeue);
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]);
662
663   /* bulk dequeue */
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]);
671
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",
674                 rx_fifo->shr->size);
675
676   vnet_app_detach_args_t detach_args = {
677     .app_index = attach_args.app_index,
678     .api_client_index = ~0,
679   };
680   rv = vnet_application_detach (&detach_args);
681   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
682
683   return 0;
684 }
685
686 static int
687 segment_manager_test_prealloc_hdrs (vlib_main_t * vm,
688                                     unformat_input_t * input)
689 {
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;
694   fifo_segment_t *fs;
695   int rv;
696
697   memset (&options, 0, sizeof (options));
698
699   vnet_app_attach_args_t attach_args = {
700     .api_client_index = ~0,
701     .options = options,
702     .namespace_id = 0,
703     .session_cb_vft = &placeholder_session_cbs,
704     .name = format (0, "segment_manager_prealloc_hdrs"),
705   };
706
707   prealloc_hdrs = (app_seg_size - (16 << 10)) / sizeof (svm_fifo_t);
708
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;
714
715   rv = vnet_application_attach (&attach_args);
716   vec_free (attach_args.name);
717
718   SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv);
719
720   segment_manager_parse_segment_handle (attach_args.segment_handle, &sm_index,
721                                         &fs_index);
722   sm = segment_manager_get (sm_index);
723
724   SEG_MGR_TEST ((sm != 0), "seg manager is valid", sm);
725
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);
729
730   vnet_app_detach_args_t detach_args = {
731     .app_index = attach_args.app_index,
732     .api_client_index = ~0,
733   };
734   rv = vnet_application_detach (&detach_args);
735   SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv);
736   return 0;
737 }
738
739 static clib_error_t *
740 segment_manager_test (vlib_main_t * vm,
741                       unformat_input_t * input, vlib_cli_command_t * cmd_arg)
742 {
743   int res = 0;
744
745   vnet_session_enable_disable (vm, 1);
746
747   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
748     {
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);
759
760       else if (unformat (input, "all"))
761         {
762           if ((res = segment_manager_test_pressure_1 (vm, input)))
763             goto done;
764           if ((res = segment_manager_test_pressure_2 (vm, input)))
765             goto done;
766           if ((res = segment_manager_test_fifo_balanced_alloc (vm, input)))
767             goto done;
768           if ((res = segment_manager_test_fifo_ops (vm, input)))
769             goto done;
770           if ((res = segment_manager_test_prealloc_hdrs (vm, input)))
771             goto done;
772         }
773       else
774         break;
775     }
776
777 done:
778   if (res)
779     return clib_error_return (0, "Segment manager unit test failed.");
780   return 0;
781 }
782
783 /* *INDENT-OFF* */
784 VLIB_CLI_COMMAND (tcp_test_command, static) =
785 {
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,
790 };
791
792 /*
793  * fd.io coding-style-patch-verification: ON
794  *
795  * Local Variables:
796  * eval: (c-set-style "gnu")
797  * End:
798  */