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 token = strtok(entry, CPERF_ENTRY_DELIMITER);
244 /* get values for key */
245 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
247 printf("Expected 'key = values' but was '%.40s'..\n",
252 status = parse_values(token, &data, &data_length);
257 if (strstr(key_token, "plaintext")) {
258 rte_free(vector->plaintext.data);
259 vector->plaintext.data = data;
261 vector->plaintext.length = data_length;
263 if (opts->buffer_sz > data_length) {
264 printf("Global plaintext shorter than "
268 vector->plaintext.length = opts->buffer_sz;
271 } else if (strstr(key_token, "cipher_key")) {
272 rte_free(vector->cipher_key.data);
273 vector->cipher_key.data = data;
275 vector->cipher_key.length = data_length;
277 if (opts->cipher_key_sz > data_length) {
278 printf("Global cipher_key shorter than "
282 vector->cipher_key.length = opts->cipher_key_sz;
285 } else if (strstr(key_token, "auth_key")) {
286 rte_free(vector->auth_key.data);
287 vector->auth_key.data = data;
289 vector->auth_key.length = data_length;
291 if (opts->auth_key_sz > data_length) {
292 printf("Global auth_key shorter than "
296 vector->auth_key.length = opts->auth_key_sz;
299 } else if (strstr(key_token, "iv")) {
300 rte_free(vector->iv.data);
301 vector->iv.data = data;
302 vector->iv.phys_addr = rte_malloc_virt2phy(vector->iv.data);
304 vector->iv.length = data_length;
306 if (opts->cipher_iv_sz > data_length) {
307 printf("Global iv shorter than "
311 vector->iv.length = opts->cipher_iv_sz;
314 } else if (strstr(key_token, "ciphertext")) {
315 rte_free(vector->ciphertext.data);
316 vector->ciphertext.data = data;
318 vector->ciphertext.length = data_length;
320 if (opts->buffer_sz > data_length) {
321 printf("Global ciphertext shorter than "
325 vector->ciphertext.length = opts->buffer_sz;
328 } else if (strstr(key_token, "aad")) {
329 rte_free(vector->aad.data);
330 vector->aad.data = data;
331 vector->aad.phys_addr = rte_malloc_virt2phy(vector->aad.data);
333 vector->aad.length = data_length;
335 if (opts->auth_aad_sz > data_length) {
336 printf("Global aad shorter than "
340 vector->aad.length = opts->auth_aad_sz;
343 } else if (strstr(key_token, "digest")) {
344 rte_free(vector->digest.data);
345 vector->digest.data = data;
346 vector->digest.phys_addr = rte_malloc_virt2phy(
347 vector->digest.data);
349 vector->digest.length = data_length;
351 if (opts->auth_digest_sz > data_length) {
352 printf("Global digest shorter than "
356 vector->digest.length = opts->auth_digest_sz;
359 printf("Not valid key: '%s'\n", trim_space(key_token));
366 /* searches in the file for test keys and values */
368 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
370 uint8_t tc_found = 0;
371 uint8_t tc_data_start = 0;
380 fp = fopen(opts->test_file, "r");
382 printf("File %s does not exists\n", opts->test_file);
386 while ((read = getline(&line, &len, fp)) != -1) {
388 /* ignore comments and new lines */
389 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
390 || line[0] == '\r' || line[0] == ' ')
395 /* next test case is started */
396 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
398 /* test case section started, end of global data */
399 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
402 /* test name unspecified, end after global data */
403 if (tc_data_start && opts->test_name == NULL)
405 /* searching for a suitable test */
406 else if (tc_data_start && tc_found == 0) {
407 if (!strcmp(line, opts->test_name)) {
414 /* buffer for multiline */
415 entry = (char *) rte_realloc(entry,
416 sizeof(char) * strlen(line) + 1, 0);
420 memset(entry, 0, strlen(line) + 1);
421 strncpy(entry, line, strlen(line));
423 /* check if entry ends with , or = */
424 if (entry[strlen(entry) - 1] == ','
425 || entry[strlen(entry) - 1] == '=') {
426 while ((read = getline(&line, &len, fp)) != -1) {
429 /* extend entry about length of new line */
430 char *entry_extended = (char *) rte_realloc(
432 * (strlen(line) + strlen(entry))
435 if (entry_extended == NULL)
437 entry = entry_extended;
439 strncat(entry, line, strlen(line));
441 if (entry[strlen(entry) - 1] != ',')
445 status = parse_entry(entry, vector, opts, tc_found);
447 printf("An error occurred while parsing!\n");
452 if (tc_found == 0 && opts->test_name != NULL) {
453 printf("Not found '%s' case in test file\n", opts->test_name);
474 struct cperf_test_vector*
475 cperf_test_vector_get_from_file(struct cperf_options *opts)
478 struct cperf_test_vector *test_vector = NULL;
480 if (opts == NULL || opts->test_file == NULL)
483 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
484 sizeof(struct cperf_test_vector), 0);
485 if (test_vector == NULL)
488 /* filling the vector with data from a file */
489 status = parse_file(test_vector, opts);
491 free_test_vector(test_vector, opts);
495 /* other values not included in the file */
496 test_vector->data.cipher_offset = 0;
497 test_vector->data.cipher_length = opts->buffer_sz;
499 test_vector->data.auth_offset = 0;
500 test_vector->data.auth_length = opts->buffer_sz;