1 #ifdef RTE_EXEC_ENV_BSDAPP
6 #include <rte_malloc.h>
8 #include "cperf_options.h"
9 #include "cperf_test_vectors.h"
10 #include "cperf_test_vector_parsing.h"
13 free_test_vector(struct cperf_test_vector *vector, struct cperf_options *opts)
15 if (vector == NULL || opts == NULL)
18 rte_free(vector->iv.data);
19 rte_free(vector->aad.data);
20 rte_free(vector->digest.data);
22 if (opts->test_file != NULL) {
23 rte_free(vector->plaintext.data);
24 rte_free(vector->cipher_key.data);
25 rte_free(vector->auth_key.data);
26 rte_free(vector->ciphertext.data);
35 show_test_vector(struct cperf_test_vector *test_vector)
37 const uint8_t wrap = 32;
40 if (test_vector == NULL)
43 if (test_vector->plaintext.data) {
44 printf("\nplaintext =\n");
45 for (i = 0; i < test_vector->plaintext.length; ++i) {
46 if ((i % wrap == 0) && (i != 0))
48 if (i == test_vector->plaintext.length - 1)
50 test_vector->plaintext.data[i]);
53 test_vector->plaintext.data[i]);
58 if (test_vector->cipher_key.data) {
59 printf("\ncipher_key =\n");
60 for (i = 0; i < test_vector->cipher_key.length; ++i) {
61 if ((i % wrap == 0) && (i != 0))
63 if (i == (uint32_t)(test_vector->cipher_key.length - 1))
65 test_vector->cipher_key.data[i]);
68 test_vector->cipher_key.data[i]);
73 if (test_vector->auth_key.data) {
74 printf("\nauth_key =\n");
75 for (i = 0; i < test_vector->auth_key.length; ++i) {
76 if ((i % wrap == 0) && (i != 0))
78 if (i == (uint32_t)(test_vector->auth_key.length - 1))
79 printf("0x%02x", test_vector->auth_key.data[i]);
82 test_vector->auth_key.data[i]);
87 if (test_vector->iv.data) {
89 for (i = 0; i < test_vector->iv.length; ++i) {
90 if ((i % wrap == 0) && (i != 0))
92 if (i == (uint32_t)(test_vector->iv.length - 1))
93 printf("0x%02x", test_vector->iv.data[i]);
95 printf("0x%02x, ", test_vector->iv.data[i]);
100 if (test_vector->ciphertext.data) {
101 printf("\nciphertext =\n");
102 for (i = 0; i < test_vector->ciphertext.length; ++i) {
103 if ((i % wrap == 0) && (i != 0))
105 if (i == test_vector->ciphertext.length - 1)
107 test_vector->ciphertext.data[i]);
110 test_vector->ciphertext.data[i]);
115 if (test_vector->aad.data) {
117 for (i = 0; i < test_vector->aad.length; ++i) {
118 if ((i % wrap == 0) && (i != 0))
120 if (i == (uint32_t)(test_vector->aad.length - 1))
121 printf("0x%02x", test_vector->aad.data[i]);
123 printf("0x%02x, ", test_vector->aad.data[i]);
128 if (test_vector->digest.data) {
129 printf("\ndigest =\n");
130 for (i = 0; i < test_vector->digest.length; ++i) {
131 if ((i % wrap == 0) && (i != 0))
133 if (i == (uint32_t)(test_vector->digest.length - 1))
134 printf("0x%02x", test_vector->digest.data[i]);
136 printf("0x%02x, ", test_vector->digest.data[i]);
142 /* trim leading and trailing spaces */
144 trim_space(char *str)
148 for (start = str; *start; start++) {
149 if (!isspace((unsigned char) start[0]))
153 for (end = start + strlen(start); end > start + 1; end--) {
154 if (!isspace((unsigned char) end[-1]))
160 /* Shift from "start" to the beginning of the string */
162 memmove(str, start, (end - start) + 1);
167 /* tokenization test values separated by a comma */
169 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
172 uint32_t data_size = 32;
174 uint8_t *values, *values_resized;
175 char *tok, *error = NULL;
177 tok = strtok(tokens, CPERF_VALUE_DELIMITER);
181 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
186 while (tok != NULL) {
187 values_resized = NULL;
189 if (n_tokens >= data_size) {
192 values_resized = (uint8_t *) rte_realloc(values,
193 sizeof(uint8_t) * data_size, 0);
194 if (values_resized == NULL) {
198 values = values_resized;
201 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
202 if ((error == NULL) || (*error != '\0')) {
203 printf("Failed with convert '%s'\n", tok);
208 tok = strtok(NULL, CPERF_VALUE_DELIMITER);
215 values_resized = (uint8_t *) rte_realloc(values,
216 sizeof(uint8_t) * (n_tokens + 1), 0);
218 if (values_resized == NULL) {
223 *data = values_resized;
224 *data_length = n_tokens + 1;
229 /* checks the type of key and assigns data */
231 parse_entry(char *entry, struct cperf_test_vector *vector,
232 struct cperf_options *opts, uint8_t tc_found)
235 uint32_t data_length;
237 uint8_t *data = NULL;
238 char *token, *key_token;
241 printf("Expected entry value\n");
246 token = strtok(entry, CPERF_ENTRY_DELIMITER);
248 /* get values for key */
249 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
251 if (key_token == NULL || token == NULL) {
252 printf("Expected 'key = values' but was '%.40s'..\n", entry);
256 status = parse_values(token, &data, &data_length);
261 if (strstr(key_token, "plaintext")) {
262 rte_free(vector->plaintext.data);
263 vector->plaintext.data = data;
265 vector->plaintext.length = data_length;
267 if (opts->max_buffer_size > data_length) {
268 printf("Global plaintext shorter than "
272 vector->plaintext.length = opts->max_buffer_size;
275 } else if (strstr(key_token, "cipher_key")) {
276 rte_free(vector->cipher_key.data);
277 vector->cipher_key.data = data;
279 vector->cipher_key.length = data_length;
281 if (opts->cipher_key_sz > data_length) {
282 printf("Global cipher_key shorter than "
286 vector->cipher_key.length = opts->cipher_key_sz;
289 } else if (strstr(key_token, "auth_key")) {
290 rte_free(vector->auth_key.data);
291 vector->auth_key.data = data;
293 vector->auth_key.length = data_length;
295 if (opts->auth_key_sz > data_length) {
296 printf("Global auth_key shorter than "
300 vector->auth_key.length = opts->auth_key_sz;
303 } else if (strstr(key_token, "iv")) {
304 rte_free(vector->iv.data);
305 vector->iv.data = data;
306 vector->iv.phys_addr = rte_malloc_virt2phy(vector->iv.data);
308 vector->iv.length = data_length;
310 if (opts->cipher_iv_sz > data_length) {
311 printf("Global iv shorter than "
315 vector->iv.length = opts->cipher_iv_sz;
318 } else if (strstr(key_token, "ciphertext")) {
319 rte_free(vector->ciphertext.data);
320 vector->ciphertext.data = data;
322 vector->ciphertext.length = data_length;
324 if (opts->max_buffer_size > data_length) {
325 printf("Global ciphertext shorter than "
329 vector->ciphertext.length = opts->max_buffer_size;
332 } else if (strstr(key_token, "aad")) {
333 rte_free(vector->aad.data);
334 vector->aad.data = data;
335 vector->aad.phys_addr = rte_malloc_virt2phy(vector->aad.data);
337 vector->aad.length = data_length;
339 if (opts->auth_aad_sz > data_length) {
340 printf("Global aad shorter than "
344 vector->aad.length = opts->auth_aad_sz;
347 } else if (strstr(key_token, "digest")) {
348 rte_free(vector->digest.data);
349 vector->digest.data = data;
350 vector->digest.phys_addr = rte_malloc_virt2phy(
351 vector->digest.data);
353 vector->digest.length = data_length;
355 if (opts->auth_digest_sz > data_length) {
356 printf("Global digest shorter than "
360 vector->digest.length = opts->auth_digest_sz;
363 printf("Not valid key: '%s'\n", trim_space(key_token));
370 /* searches in the file for test keys and values */
372 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
374 uint8_t tc_found = 0;
375 uint8_t tc_data_start = 0;
384 fp = fopen(opts->test_file, "r");
386 printf("File %s does not exists\n", opts->test_file);
390 while ((read = getline(&line, &len, fp)) != -1) {
392 /* ignore comments and new lines */
393 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
394 || line[0] == '\r' || line[0] == ' ')
399 /* next test case is started */
400 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
402 /* test case section started, end of global data */
403 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
406 /* test name unspecified, end after global data */
407 if (tc_data_start && opts->test_name == NULL)
409 /* searching for a suitable test */
410 else if (tc_data_start && tc_found == 0) {
411 if (!strcmp(line, opts->test_name)) {
418 /* buffer for multiline */
419 entry = (char *) rte_realloc(entry,
420 sizeof(char) * strlen(line) + 1, 0);
424 memset(entry, 0, strlen(line) + 1);
425 strncpy(entry, line, strlen(line));
427 /* check if entry ends with , or = */
428 if (entry[strlen(entry) - 1] == ','
429 || entry[strlen(entry) - 1] == '=') {
430 while ((read = getline(&line, &len, fp)) != -1) {
433 /* extend entry about length of new line */
434 char *entry_extended = (char *) rte_realloc(
436 * (strlen(line) + strlen(entry))
439 if (entry_extended == NULL)
441 entry = entry_extended;
443 strncat(entry, line, strlen(line));
445 if (entry[strlen(entry) - 1] != ',')
449 status = parse_entry(entry, vector, opts, tc_found);
451 printf("An error occurred while parsing!\n");
456 if (tc_found == 0 && opts->test_name != NULL) {
457 printf("Not found '%s' case in test file\n", opts->test_name);
478 struct cperf_test_vector*
479 cperf_test_vector_get_from_file(struct cperf_options *opts)
482 struct cperf_test_vector *test_vector = NULL;
484 if (opts == NULL || opts->test_file == NULL)
487 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
488 sizeof(struct cperf_test_vector), 0);
489 if (test_vector == NULL)
492 /* filling the vector with data from a file */
493 status = parse_file(test_vector, opts);
495 free_test_vector(test_vector, opts);
499 /* other values not included in the file */
500 test_vector->data.cipher_offset = 0;
501 test_vector->data.cipher_length = opts->max_buffer_size;
503 test_vector->data.auth_offset = 0;
504 test_vector->data.auth_length = opts->max_buffer_size;