Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | clib_error_t * |
| 19 | hello_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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 28 | |
| 29 | memset (a, 0, sizeof (*a)); |
| 30 | |
| 31 | a->segment_name = "fifo-test1"; |
| 32 | a->segment_size = 256 << 10; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 33 | a->rx_fifo_size = 4096; |
| 34 | a->tx_fifo_size = 4096; |
| 35 | a->preallocated_fifo_pairs = 4; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 36 | |
| 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 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 42 | sp = svm_fifo_get_segment (a->new_segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 43 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 44 | f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 45 | |
| 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 Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 53 | svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 54 | |
| 55 | while (svm_fifo_max_dequeue (f) >= vec_len (test_data)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 56 | svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 57 | |
| 58 | while (svm_fifo_max_enqueue (f) >= vec_len (test_data)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 59 | svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 60 | |
| 61 | while (svm_fifo_max_dequeue (f) >= vec_len (test_data)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 62 | svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 63 | |
| 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 Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 69 | svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 70 | |
| 71 | return error; |
| 72 | } |
| 73 | |
| 74 | clib_error_t * |
| 75 | master (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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 84 | |
| 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 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 95 | sp = svm_fifo_get_segment (a->new_segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 96 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 97 | f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | |
| 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 Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 106 | svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 107 | |
| 108 | return clib_error_return (0, "master (enqueue) done"); |
| 109 | } |
| 110 | |
| 111 | clib_error_t * |
| 112 | mempig (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 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 131 | sp = svm_fifo_get_segment (a->new_segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | |
| 133 | for (i = 0; i < 1000; i++) |
| 134 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 135 | f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 136 | 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 Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 145 | svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | _vec_len (flist) = 0; |
| 149 | |
| 150 | for (i = 0; i < 1000; i++) |
| 151 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 152 | f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 153 | 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 Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 162 | svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | clib_error_t * |
| 169 | offset (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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | |
| 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 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 189 | sp = svm_fifo_get_segment (a->new_segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 191 | f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | |
| 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 Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 200 | svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ , |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | (u8 *) test_data); |
| 202 | |
| 203 | /* Enqueue the third 1024 u32's 2048 ahead of the current tail */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 204 | svm_fifo_enqueue_with_offset (f, 4096, 4096, (u8 *) & test_data[2048]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 205 | |
| 206 | /* Enqueue the second 1024 u32's at the current tail */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 207 | svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ , |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | (u8 *) & test_data[1024]); |
| 209 | |
| 210 | vec_validate (recovered_data, (3 * 1024) - 1); |
| 211 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 212 | svm_fifo_dequeue_nowait (f, 3 * 4096, (u8 *) recovered_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 213 | |
| 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 | |
| 227 | clib_error_t * |
| 228 | slave (int verbose) |
| 229 | { |
| 230 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 231 | svm_fifo_segment_private_t *sp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 232 | svm_fifo_t *f; |
| 233 | ssvm_shared_header_t *sh; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 234 | svm_fifo_segment_header_t *fsh; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 235 | int rv; |
| 236 | u8 *test_data; |
| 237 | u8 *retrieved_data = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 238 | 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 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 249 | sp = svm_fifo_get_segment (a->new_segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 250 | sh = sp->ssvm.sh; |
| 251 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 252 | |
| 253 | /* might wanna wait.. */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 254 | f = fsh->fifos; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 255 | |
| 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 Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 262 | svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 263 | 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 | |
| 272 | int |
| 273 | test_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 | |
| 330 | out: |
| 331 | if (error) |
| 332 | clib_error_report (error); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | |
| 339 | int |
| 340 | main (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 | */ |