blob: 63b4a9b7ba57eba59a2c5b2a8f8da7e5ef5b6543 [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
18clib_error_t *
19hello_world (int verbose)
20{
21 svm_fifo_segment_create_args_t _a, *a = &_a;
22 svm_fifo_segment_private_t *sp;
23 svm_fifo_t *f;
24 int rv;
25 u8 *test_data;
26 u8 *retrieved_data = 0;
27 clib_error_t *error = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050028
29 memset (a, 0, sizeof (*a));
30
31 a->segment_name = "fifo-test1";
32 a->segment_size = 256 << 10;
Dave Barach10d8cc62017-05-30 09:30:07 -040033 a->rx_fifo_size = 4096;
34 a->tx_fifo_size = 4096;
35 a->preallocated_fifo_pairs = 4;
Dave Barach68b0fb02017-02-28 15:15:56 -050036
37 rv = svm_fifo_segment_create (a);
38
39 if (rv)
40 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
41
42 sp = svm_fifo_get_segment (a->new_segment_index);
43
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;
78 svm_fifo_segment_private_t *sp;
79 svm_fifo_t *f;
80 int rv;
81 u8 *test_data;
82 u8 *retrieved_data = 0;
83 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -050084
85 memset (a, 0, sizeof (*a));
86
87 a->segment_name = "fifo-test1";
88 a->segment_size = 256 << 10;
89
90 rv = svm_fifo_segment_create (a);
91
92 if (rv)
93 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
94
95 sp = svm_fifo_get_segment (a->new_segment_index);
96
Dave Barach10d8cc62017-05-30 09:30:07 -040097 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -050098
99 if (f == 0)
100 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
101
102 test_data = format (0, "Hello world%c", 0);
103 vec_validate (retrieved_data, vec_len (test_data) - 1);
104
105 for (i = 0; i < 1000; i++)
Florin Corasa5464812017-04-19 13:00:05 -0700106 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500107
108 return clib_error_return (0, "master (enqueue) done");
109}
110
111clib_error_t *
112mempig (int verbose)
113{
114 svm_fifo_segment_create_args_t _a, *a = &_a;
115 svm_fifo_segment_private_t *sp;
116 svm_fifo_t *f;
117 svm_fifo_t **flist = 0;
118 int rv;
119 int i;
120
121 memset (a, 0, sizeof (*a));
122
123 a->segment_name = "fifo-test1";
124 a->segment_size = 256 << 10;
125
126 rv = svm_fifo_segment_create (a);
127
128 if (rv)
129 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
130
131 sp = svm_fifo_get_segment (a->new_segment_index);
132
133 for (i = 0; i < 1000; i++)
134 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400135 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500136 if (f == 0)
137 break;
138 vec_add1 (flist, f);
139 }
140
141 fformat (stdout, "Try #1: created %d fifos...\n", vec_len (flist));
142 for (i = 0; i < vec_len (flist); i++)
143 {
144 f = flist[i];
Dave Barach10d8cc62017-05-30 09:30:07 -0400145 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500146 }
147
148 _vec_len (flist) = 0;
149
150 for (i = 0; i < 1000; i++)
151 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400152 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 if (f == 0)
154 break;
155 vec_add1 (flist, f);
156 }
157
158 fformat (stdout, "Try #2: created %d fifos...\n", vec_len (flist));
159 for (i = 0; i < vec_len (flist); i++)
160 {
161 f = flist[i];
Dave Barach10d8cc62017-05-30 09:30:07 -0400162 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500163 }
164
165 return 0;
166}
167
168clib_error_t *
169offset (int verbose)
170{
171 svm_fifo_segment_create_args_t _a, *a = &_a;
172 svm_fifo_segment_private_t *sp;
173 svm_fifo_t *f;
174 int rv;
175 u32 *test_data = 0;
176 u32 *recovered_data = 0;
177 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500178
179 memset (a, 0, sizeof (*a));
180
181 a->segment_name = "fifo-test1";
182 a->segment_size = 256 << 10;
183
184 rv = svm_fifo_segment_create (a);
185
186 if (rv)
187 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
188
189 sp = svm_fifo_get_segment (a->new_segment_index);
190
Dave Barach10d8cc62017-05-30 09:30:07 -0400191 f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST);
Dave Barach68b0fb02017-02-28 15:15:56 -0500192
193 if (f == 0)
194 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
195
196 for (i = 0; i < (3 * 1024); i++)
197 vec_add1 (test_data, i);
198
199 /* Enqueue the first 1024 u32's */
Florin Corasa5464812017-04-19 13:00:05 -0700200 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
Dave Barach68b0fb02017-02-28 15:15:56 -0500201 (u8 *) test_data);
202
203 /* Enqueue the third 1024 u32's 2048 ahead of the current tail */
Florin Corasa5464812017-04-19 13:00:05 -0700204 svm_fifo_enqueue_with_offset (f, 4096, 4096, (u8 *) & test_data[2048]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500205
206 /* Enqueue the second 1024 u32's at the current tail */
Florin Corasa5464812017-04-19 13:00:05 -0700207 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
Dave Barach68b0fb02017-02-28 15:15:56 -0500208 (u8 *) & test_data[1024]);
209
210 vec_validate (recovered_data, (3 * 1024) - 1);
211
Florin Corasa5464812017-04-19 13:00:05 -0700212 svm_fifo_dequeue_nowait (f, 3 * 4096, (u8 *) recovered_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500213
214 for (i = 0; i < (3 * 1024); i++)
215 {
216 if (recovered_data[i] != test_data[i])
217 {
218 clib_warning ("[%d] expected %d recovered %d", i,
219 test_data[i], recovered_data[i]);
220 return clib_error_return (0, "offset test FAILED");
221 }
222 }
223
224 return clib_error_return (0, "offset test OK");
225}
226
227clib_error_t *
228slave (int verbose)
229{
230 svm_fifo_segment_create_args_t _a, *a = &_a;
231 svm_fifo_segment_private_t *sp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500232 svm_fifo_t *f;
233 ssvm_shared_header_t *sh;
Dave Barach10d8cc62017-05-30 09:30:07 -0400234 svm_fifo_segment_header_t *fsh;
Dave Barach68b0fb02017-02-28 15:15:56 -0500235 int rv;
236 u8 *test_data;
237 u8 *retrieved_data = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500238 int i;
239
240 memset (a, 0, sizeof (*a));
241
242 a->segment_name = "fifo-test1";
243
244 rv = svm_fifo_segment_attach (a);
245
246 if (rv)
247 return clib_error_return (0, "svm_fifo_segment_attach returned %d", rv);
248
249 sp = svm_fifo_get_segment (a->new_segment_index);
250 sh = sp->ssvm.sh;
251 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
252
253 /* might wanna wait.. */
Dave Barach10d8cc62017-05-30 09:30:07 -0400254 f = fsh->fifos;
Dave Barach68b0fb02017-02-28 15:15:56 -0500255
256 /* Lazy bastards united */
257 test_data = format (0, "Hello world%c", 0);
258 vec_validate (retrieved_data, vec_len (test_data) - 1);
259
260 for (i = 0; i < 1000; i++)
261 {
Florin Corasa5464812017-04-19 13:00:05 -0700262 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
264 return clib_error_return (0, "retrieved data incorrect, '%s'",
265 retrieved_data);
266 }
267
268 return clib_error_return (0, "slave (dequeue) done");
269}
270
271
272int
273test_ssvm_fifo1 (unformat_input_t * input)
274{
275 clib_error_t *error = 0;
276 int verbose = 0;
277 int test_id = 0;
278
279 svm_fifo_segment_init (0x200000000ULL, 20);
280
281 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
282 {
283 if (unformat (input, "verbose %d", &verbose))
284 ;
285 else if (unformat (input, "verbose"))
286 verbose = 1;
287 else if (unformat (input, "master"))
288 test_id = 1;
289 else if (unformat (input, "slave"))
290 test_id = 2;
291 else if (unformat (input, "mempig"))
292 test_id = 3;
293 else if (unformat (input, "offset"))
294 test_id = 4;
295 else
296 {
297 error = clib_error_create ("unknown input `%U'\n",
298 format_unformat_error, input);
299 goto out;
300 }
301 }
302
303 switch (test_id)
304 {
305 case 0:
306 error = hello_world (verbose);
307 break;
308
309 case 1:
310 error = master (verbose);
311 break;
312
313 case 2:
314 error = slave (verbose);
315 break;
316
317 case 3:
318 error = mempig (verbose);
319 break;
320
321 case 4:
322 error = offset (verbose);
323 break;
324
325 default:
326 error = clib_error_return (0, "test id %d unknown", test_id);
327 break;
328 }
329
330out:
331 if (error)
332 clib_error_report (error);
333
334 return 0;
335}
336
337
338
339int
340main (int argc, char *argv[])
341{
342 unformat_input_t i;
343 int r;
344
345 unformat_init_command_line (&i, argv);
346 r = test_ssvm_fifo1 (&i);
347 unformat_free (&i);
348 return r;
349}
350
351/*
352 * fd.io coding-style-patch-verification: ON
353 *
354 * Local Variables:
355 * eval: (c-set-style "gnu")
356 * End:
357 */