blob: 0a09916f3bf731dbf0e058832c82c09c8d2f0cba [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "svm_fifo_segment.h"
17
Florin Corasadc74d72018-12-02 13:36:00 -080018svm_fifo_segment_main_t segment_main;
19
Dave Barach68b0fb02017-02-28 15:15:56 -050020clib_error_t *
21hello_world (int verbose)
22{
23 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -080024 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050025 svm_fifo_segment_private_t *sp;
26 svm_fifo_t *f;
27 int rv;
28 u8 *test_data;
29 u8 *retrieved_data = 0;
30 clib_error_t *error = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050031
Dave Barachb7b92992018-10-17 10:38:51 -040032 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -050033
34 a->segment_name = "fifo-test1";
35 a->segment_size = 256 << 10;
36
Florin Corasadc74d72018-12-02 13:36:00 -080037 rv = svm_fifo_segment_create (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -050038
39 if (rv)
40 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
41
Florin Corasadc74d72018-12-02 13:36:00 -080042 sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -050043
Dave Barach10d8cc62017-05-30 09:30:07 -040044 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -050045
46 if (f == 0)
47 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
48
49 test_data = format (0, "Hello world%c", 0);
50 vec_validate (retrieved_data, vec_len (test_data) - 1);
51
52 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
Florin Corasa5464812017-04-19 13:00:05 -070053 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
Dave Barach68b0fb02017-02-28 15:15:56 -050054
55 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
Florin Corasa5464812017-04-19 13:00:05 -070056 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
Dave Barach68b0fb02017-02-28 15:15:56 -050057
58 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
Florin Corasa5464812017-04-19 13:00:05 -070059 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
Dave Barach68b0fb02017-02-28 15:15:56 -050060
61 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
Florin Corasa5464812017-04-19 13:00:05 -070062 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
Dave Barach68b0fb02017-02-28 15:15:56 -050063
64 if (!memcmp (retrieved_data, test_data, vec_len (test_data)))
65 error = clib_error_return (0, "data test OK, got '%s'", retrieved_data);
66 else
67 error = clib_error_return (0, "data test FAIL!");
68
Dave Barach10d8cc62017-05-30 09:30:07 -040069 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -050070
71 return error;
72}
73
74clib_error_t *
75master (int verbose)
76{
77 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -080078 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050079 svm_fifo_segment_private_t *sp;
80 svm_fifo_t *f;
81 int rv;
82 u8 *test_data;
83 u8 *retrieved_data = 0;
84 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -050085
Dave Barachb7b92992018-10-17 10:38:51 -040086 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -050087
88 a->segment_name = "fifo-test1";
89 a->segment_size = 256 << 10;
90
Florin Corasadc74d72018-12-02 13:36:00 -080091 rv = svm_fifo_segment_create (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -050092
93 if (rv)
94 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
95
Florin Corasadc74d72018-12-02 13:36:00 -080096 sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -050097
Dave Barach10d8cc62017-05-30 09:30:07 -040098 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -050099
100 if (f == 0)
101 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
102
103 test_data = format (0, "Hello world%c", 0);
104 vec_validate (retrieved_data, vec_len (test_data) - 1);
105
106 for (i = 0; i < 1000; i++)
Florin Corasa5464812017-04-19 13:00:05 -0700107 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500108
109 return clib_error_return (0, "master (enqueue) done");
110}
111
112clib_error_t *
113mempig (int verbose)
114{
115 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800116 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500117 svm_fifo_segment_private_t *sp;
118 svm_fifo_t *f;
119 svm_fifo_t **flist = 0;
120 int rv;
121 int i;
122
Dave Barachb7b92992018-10-17 10:38:51 -0400123 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500124
125 a->segment_name = "fifo-test1";
126 a->segment_size = 256 << 10;
127
Florin Corasadc74d72018-12-02 13:36:00 -0800128 rv = svm_fifo_segment_create (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500129
130 if (rv)
131 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
132
Florin Corasadc74d72018-12-02 13:36:00 -0800133 sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500134
135 for (i = 0; i < 1000; i++)
136 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400137 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500138 if (f == 0)
139 break;
140 vec_add1 (flist, f);
141 }
142
143 fformat (stdout, "Try #1: created %d fifos...\n", vec_len (flist));
144 for (i = 0; i < vec_len (flist); i++)
145 {
146 f = flist[i];
Dave Barach10d8cc62017-05-30 09:30:07 -0400147 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500148 }
149
150 _vec_len (flist) = 0;
151
152 for (i = 0; i < 1000; i++)
153 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400154 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500155 if (f == 0)
156 break;
157 vec_add1 (flist, f);
158 }
159
160 fformat (stdout, "Try #2: created %d fifos...\n", vec_len (flist));
161 for (i = 0; i < vec_len (flist); i++)
162 {
163 f = flist[i];
Dave Barach10d8cc62017-05-30 09:30:07 -0400164 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500165 }
166
167 return 0;
168}
169
170clib_error_t *
171offset (int verbose)
172{
173 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800174 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500175 svm_fifo_segment_private_t *sp;
176 svm_fifo_t *f;
177 int rv;
178 u32 *test_data = 0;
179 u32 *recovered_data = 0;
180 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500181
Dave Barachb7b92992018-10-17 10:38:51 -0400182 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500183
184 a->segment_name = "fifo-test1";
185 a->segment_size = 256 << 10;
186
Florin Corasadc74d72018-12-02 13:36:00 -0800187 rv = svm_fifo_segment_create (sm, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500188
189 if (rv)
190 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
191
Florin Corasadc74d72018-12-02 13:36:00 -0800192 sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500193
Dave Barach10d8cc62017-05-30 09:30:07 -0400194 f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500195
196 if (f == 0)
197 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
198
199 for (i = 0; i < (3 * 1024); i++)
200 vec_add1 (test_data, i);
201
202 /* Enqueue the first 1024 u32's */
Florin Corasa5464812017-04-19 13:00:05 -0700203 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
Dave Barach68b0fb02017-02-28 15:15:56 -0500204 (u8 *) test_data);
205
206 /* Enqueue the third 1024 u32's 2048 ahead of the current tail */
Florin Corasa5464812017-04-19 13:00:05 -0700207 svm_fifo_enqueue_with_offset (f, 4096, 4096, (u8 *) & test_data[2048]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500208
209 /* Enqueue the second 1024 u32's at the current tail */
Florin Corasa5464812017-04-19 13:00:05 -0700210 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
Dave Barach68b0fb02017-02-28 15:15:56 -0500211 (u8 *) & test_data[1024]);
212
213 vec_validate (recovered_data, (3 * 1024) - 1);
214
Florin Corasa5464812017-04-19 13:00:05 -0700215 svm_fifo_dequeue_nowait (f, 3 * 4096, (u8 *) recovered_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500216
217 for (i = 0; i < (3 * 1024); i++)
218 {
219 if (recovered_data[i] != test_data[i])
220 {
221 clib_warning ("[%d] expected %d recovered %d", i,
222 test_data[i], recovered_data[i]);
223 return clib_error_return (0, "offset test FAILED");
224 }
225 }
226
227 return clib_error_return (0, "offset test OK");
228}
229
230clib_error_t *
231slave (int verbose)
232{
233 svm_fifo_segment_create_args_t _a, *a = &_a;
Florin Corasadc74d72018-12-02 13:36:00 -0800234 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500235 svm_fifo_segment_private_t *sp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500236 svm_fifo_t *f;
237 ssvm_shared_header_t *sh;
Dave Barach10d8cc62017-05-30 09:30:07 -0400238 svm_fifo_segment_header_t *fsh;
Dave Barach68b0fb02017-02-28 15:15:56 -0500239 int rv;
240 u8 *test_data;
241 u8 *retrieved_data = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500242 int i;
243
Dave Barachb7b92992018-10-17 10:38:51 -0400244 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500245
246 a->segment_name = "fifo-test1";
247
248 rv = svm_fifo_segment_attach (a);
249
250 if (rv)
251 return clib_error_return (0, "svm_fifo_segment_attach returned %d", rv);
252
Florin Corasadc74d72018-12-02 13:36:00 -0800253 sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500254 sh = sp->ssvm.sh;
255 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
256
257 /* might wanna wait.. */
Dave Barach10d8cc62017-05-30 09:30:07 -0400258 f = fsh->fifos;
Dave Barach68b0fb02017-02-28 15:15:56 -0500259
260 /* Lazy bastards united */
261 test_data = format (0, "Hello world%c", 0);
262 vec_validate (retrieved_data, vec_len (test_data) - 1);
263
264 for (i = 0; i < 1000; i++)
265 {
Florin Corasa5464812017-04-19 13:00:05 -0700266 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500267 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
268 return clib_error_return (0, "retrieved data incorrect, '%s'",
269 retrieved_data);
270 }
271
272 return clib_error_return (0, "slave (dequeue) done");
273}
274
275
276int
277test_ssvm_fifo1 (unformat_input_t * input)
278{
Florin Corasadc74d72018-12-02 13:36:00 -0800279 svm_fifo_segment_main_t *sm = &segment_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500280 clib_error_t *error = 0;
281 int verbose = 0;
282 int test_id = 0;
283
Florin Corasadc74d72018-12-02 13:36:00 -0800284 svm_fifo_segment_main_init (sm, 0x200000000ULL, 20);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285
286 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287 {
288 if (unformat (input, "verbose %d", &verbose))
289 ;
290 else if (unformat (input, "verbose"))
291 verbose = 1;
292 else if (unformat (input, "master"))
293 test_id = 1;
294 else if (unformat (input, "slave"))
295 test_id = 2;
296 else if (unformat (input, "mempig"))
297 test_id = 3;
298 else if (unformat (input, "offset"))
299 test_id = 4;
300 else
301 {
302 error = clib_error_create ("unknown input `%U'\n",
303 format_unformat_error, input);
304 goto out;
305 }
306 }
307
308 switch (test_id)
309 {
310 case 0:
311 error = hello_world (verbose);
312 break;
313
314 case 1:
315 error = master (verbose);
316 break;
317
318 case 2:
319 error = slave (verbose);
320 break;
321
322 case 3:
323 error = mempig (verbose);
324 break;
325
326 case 4:
327 error = offset (verbose);
328 break;
329
330 default:
331 error = clib_error_return (0, "test id %d unknown", test_id);
332 break;
333 }
334
335out:
336 if (error)
337 clib_error_report (error);
338
339 return 0;
340}
341
342
343
344int
345main (int argc, char *argv[])
346{
347 unformat_input_t i;
348 int r;
349
350 unformat_init_command_line (&i, argv);
351 r = test_ssvm_fifo1 (&i);
352 unformat_free (&i);
353 return r;
354}
355
356/*
357 * fd.io coding-style-patch-verification: ON
358 *
359 * Local Variables:
360 * eval: (c-set-style "gnu")
361 * End:
362 */