3 #include <rte_malloc.h>
5 #include "cperf_options.h"
6 #include "cperf_test_vectors.h"
7 #include "cperf_test_vector_parsing.h"
10 free_test_vector(struct cperf_test_vector *vector, struct cperf_options *opts)
12 if (vector == NULL || opts == NULL)
15 rte_free(vector->iv.data);
16 rte_free(vector->aad.data);
17 rte_free(vector->digest.data);
19 if (opts->test_file != NULL) {
20 rte_free(vector->plaintext.data);
21 rte_free(vector->cipher_key.data);
22 rte_free(vector->auth_key.data);
23 rte_free(vector->ciphertext.data);
32 show_test_vector(struct cperf_test_vector *test_vector)
34 const uint8_t wrap = 32;
37 if (test_vector == NULL)
40 if (test_vector->plaintext.data) {
41 printf("\nplaintext =\n");
42 for (i = 0; i < test_vector->plaintext.length; ++i) {
43 if ((i % wrap == 0) && (i != 0))
45 if (i == test_vector->plaintext.length - 1)
47 test_vector->plaintext.data[i]);
50 test_vector->plaintext.data[i]);
55 if (test_vector->cipher_key.data) {
56 printf("\ncipher_key =\n");
57 for (i = 0; i < test_vector->cipher_key.length; ++i) {
58 if ((i % wrap == 0) && (i != 0))
60 if (i == (uint32_t)(test_vector->cipher_key.length - 1))
62 test_vector->cipher_key.data[i]);
65 test_vector->cipher_key.data[i]);
70 if (test_vector->auth_key.data) {
71 printf("\nauth_key =\n");
72 for (i = 0; i < test_vector->auth_key.length; ++i) {
73 if ((i % wrap == 0) && (i != 0))
75 if (i == (uint32_t)(test_vector->auth_key.length - 1))
76 printf("0x%02x", test_vector->auth_key.data[i]);
79 test_vector->auth_key.data[i]);
84 if (test_vector->iv.data) {
86 for (i = 0; i < test_vector->iv.length; ++i) {
87 if ((i % wrap == 0) && (i != 0))
89 if (i == (uint32_t)(test_vector->iv.length - 1))
90 printf("0x%02x", test_vector->iv.data[i]);
92 printf("0x%02x, ", test_vector->iv.data[i]);
97 if (test_vector->ciphertext.data) {
98 printf("\nciphertext =\n");
99 for (i = 0; i < test_vector->ciphertext.length; ++i) {
100 if ((i % wrap == 0) && (i != 0))
102 if (i == test_vector->ciphertext.length - 1)
104 test_vector->ciphertext.data[i]);
107 test_vector->ciphertext.data[i]);
112 if (test_vector->aad.data) {
114 for (i = 0; i < test_vector->aad.length; ++i) {
115 if ((i % wrap == 0) && (i != 0))
117 if (i == (uint32_t)(test_vector->aad.length - 1))
118 printf("0x%02x", test_vector->aad.data[i]);
120 printf("0x%02x, ", test_vector->aad.data[i]);
125 if (test_vector->digest.data) {
126 printf("\ndigest =\n");
127 for (i = 0; i < test_vector->digest.length; ++i) {
128 if ((i % wrap == 0) && (i != 0))
130 if (i == (uint32_t)(test_vector->digest.length - 1))
131 printf("0x%02x", test_vector->digest.data[i]);
133 printf("0x%02x, ", test_vector->digest.data[i]);
139 /* trim leading and trailing spaces */
141 trim_space(char *str)
145 for (start = str; *start; start++) {
146 if (!isspace((unsigned char) start[0]))
150 for (end = start + strlen(start); end > start + 1; end--) {
151 if (!isspace((unsigned char) end[-1]))
157 /* Shift from "start" to the beginning of the string */
159 memmove(str, start, (end - start) + 1);
164 /* tokenization test values separated by a comma */
166 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
169 uint32_t data_size = 32;
171 uint8_t *values, *values_resized;
172 char *tok, *error = NULL;
174 tok = strtok(tokens, CPERF_VALUE_DELIMITER);
178 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
183 while (tok != NULL) {
184 values_resized = NULL;
186 if (n_tokens >= data_size) {
189 values_resized = (uint8_t *) rte_realloc(values,
190 sizeof(uint8_t) * data_size, 0);
191 if (values_resized == NULL) {
195 values = values_resized;
198 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
199 if ((error == NULL) || (*error != '\0')) {
200 printf("Failed with convert '%s'\n", tok);
205 tok = strtok(NULL, CPERF_VALUE_DELIMITER);
212 values_resized = (uint8_t *) rte_realloc(values,
213 sizeof(uint8_t) * (n_tokens + 1), 0);
215 if (values_resized == NULL) {
220 *data = values_resized;
221 *data_length = n_tokens + 1;
226 /* checks the type of key and assigns data */
228 parse_entry(char *entry, struct cperf_test_vector *vector,
229 struct cperf_options *opts, uint8_t tc_found)
232 uint32_t data_length;
234 uint8_t *data = NULL;
235 char *token, *key_token;
238 token = strtok(entry, CPERF_ENTRY_DELIMITER);
241 /* get values for key */
242 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
244 printf("Expected 'key = values' but was '%.40s'..\n",
249 status = parse_values(token, &data, &data_length);
254 if (strstr(key_token, "plaintext")) {
255 rte_free(vector->plaintext.data);
256 vector->plaintext.data = data;
258 vector->plaintext.length = data_length;
260 if (opts->buffer_sz > data_length) {
261 printf("Global plaintext shorter than "
265 vector->plaintext.length = opts->buffer_sz;
268 } else if (strstr(key_token, "cipher_key")) {
269 rte_free(vector->cipher_key.data);
270 vector->cipher_key.data = data;
272 vector->cipher_key.length = data_length;
274 if (opts->cipher_key_sz > data_length) {
275 printf("Global cipher_key shorter than "
279 vector->cipher_key.length = opts->cipher_key_sz;
282 } else if (strstr(key_token, "auth_key")) {
283 rte_free(vector->auth_key.data);
284 vector->auth_key.data = data;
286 vector->auth_key.length = data_length;
288 if (opts->auth_key_sz > data_length) {
289 printf("Global auth_key shorter than "
293 vector->auth_key.length = opts->auth_key_sz;
296 } else if (strstr(key_token, "iv")) {
297 rte_free(vector->iv.data);
298 vector->iv.data = data;
299 vector->iv.phys_addr = rte_malloc_virt2phy(vector->iv.data);
301 vector->iv.length = data_length;
303 if (opts->cipher_iv_sz > data_length) {
304 printf("Global iv shorter than "
308 vector->iv.length = opts->cipher_iv_sz;
311 } else if (strstr(key_token, "ciphertext")) {
312 rte_free(vector->ciphertext.data);
313 vector->ciphertext.data = data;
315 vector->ciphertext.length = data_length;
317 if (opts->buffer_sz > data_length) {
318 printf("Global ciphertext shorter than "
322 vector->ciphertext.length = opts->buffer_sz;
325 } else if (strstr(key_token, "aad")) {
326 rte_free(vector->aad.data);
327 vector->aad.data = data;
328 vector->aad.phys_addr = rte_malloc_virt2phy(vector->aad.data);
330 vector->aad.length = data_length;
332 if (opts->auth_aad_sz > data_length) {
333 printf("Global aad shorter than "
337 vector->aad.length = opts->auth_aad_sz;
340 } else if (strstr(key_token, "digest")) {
341 rte_free(vector->digest.data);
342 vector->digest.data = data;
343 vector->digest.phys_addr = rte_malloc_virt2phy(
344 vector->digest.data);
346 vector->digest.length = data_length;
348 if (opts->auth_digest_sz > data_length) {
349 printf("Global digest shorter than "
353 vector->digest.length = opts->auth_digest_sz;
356 printf("Not valid key: '%s'\n", trim_space(key_token));
363 /* searches in the file for test keys and values */
365 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
367 uint8_t tc_found = 0;
368 uint8_t tc_data_start = 0;
377 fp = fopen(opts->test_file, "r");
379 printf("File %s does not exists\n", opts->test_file);
383 while ((read = getline(&line, &len, fp)) != -1) {
385 /* ignore comments and new lines */
386 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
387 || line[0] == '\r' || line[0] == ' ')
392 /* next test case is started */
393 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
395 /* test case section started, end of global data */
396 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
399 /* test name unspecified, end after global data */
400 if (tc_data_start && opts->test_name == NULL)
402 /* searching for a suitable test */
403 else if (tc_data_start && tc_found == 0) {
404 if (!strcmp(line, opts->test_name)) {
411 /* buffer for multiline */
412 entry = (char *) rte_realloc(entry,
413 sizeof(char) * strlen(line) + 1, 0);
417 memset(entry, 0, strlen(line) + 1);
418 strncpy(entry, line, strlen(line));
420 /* check if entry ends with , or = */
421 if (entry[strlen(entry) - 1] == ','
422 || entry[strlen(entry) - 1] == '=') {
423 while ((read = getline(&line, &len, fp)) != -1) {
426 /* extend entry about length of new line */
427 char *entry_extended = (char *) rte_realloc(
429 * (strlen(line) + strlen(entry))
432 if (entry_extended == NULL)
434 entry = entry_extended;
436 strncat(entry, line, strlen(line));
438 if (entry[strlen(entry) - 1] != ',')
442 status = parse_entry(entry, vector, opts, tc_found);
444 printf("An error occurred while parsing!\n");
449 if (tc_found == 0 && opts->test_name != NULL) {
450 printf("Not found '%s' case in test file\n", opts->test_name);
471 struct cperf_test_vector*
472 cperf_test_vector_get_from_file(struct cperf_options *opts)
475 struct cperf_test_vector *test_vector = NULL;
477 if (opts == NULL || opts->test_file == NULL)
480 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
481 sizeof(struct cperf_test_vector), 0);
482 if (test_vector == NULL)
485 /* filling the vector with data from a file */
486 status = parse_file(test_vector, opts);
488 free_test_vector(test_vector, opts);
492 /* other values not included in the file */
493 test_vector->data.cipher_offset = 0;
494 test_vector->data.cipher_length = opts->buffer_sz;
496 test_vector->data.auth_offset = 0;
497 test_vector->data.auth_length = opts->buffer_sz;