3 #include <rte_malloc.h>
4 #include "cperf_options.h"
5 #include "cperf_test_vectors.h"
6 #include "cperf_verify_parser.h"
9 free_test_vector(struct cperf_test_vector *vector, struct cperf_options *opts)
11 if (vector == NULL || opts == NULL)
14 if (opts->test_file == NULL) {
16 rte_free(vector->iv.data);
18 rte_free(vector->aad.data);
19 if (vector->digest.data)
20 rte_free(vector->digest.data);
24 if (vector->plaintext.data)
25 rte_free(vector->plaintext.data);
26 if (vector->cipher_key.data)
27 rte_free(vector->cipher_key.data);
28 if (vector->auth_key.data)
29 rte_free(vector->auth_key.data);
31 rte_free(vector->iv.data);
32 if (vector->ciphertext.data)
33 rte_free(vector->ciphertext.data);
35 rte_free(vector->aad.data);
36 if (vector->digest.data)
37 rte_free(vector->digest.data);
44 /* trim leading and trailing spaces */
50 for (start = str; *start; start++) {
51 if (!isspace((unsigned char) start[0]))
55 for (end = start + strlen(start); end > start + 1; end--) {
56 if (!isspace((unsigned char) end[-1]))
62 /* Shift from "start" to the beginning of the string */
64 memmove(str, start, (end - start) + 1);
69 /* tokenization test values separated by a comma */
71 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
74 uint32_t data_size = 32;
76 char *tok, *error = NULL;
78 tok = strtok(tokens, VALUE_DELIMITER);
82 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
88 uint8_t *values_extended = NULL;
90 if (n_tokens >= data_size) {
94 values_extended = (uint8_t *) rte_realloc(values,
95 sizeof(uint8_t) * data_size, 0);
96 if (values_extended == NULL) {
101 values = values_extended;
104 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
105 if ((error == NULL) || (*error != '\0')) {
106 printf("Failed with convert '%s'\n", tok);
111 tok = strtok(NULL, VALUE_DELIMITER);
118 uint8_t *resize_values = (uint8_t *) rte_realloc(values,
119 sizeof(uint8_t) * (n_tokens + 1), 0);
121 if (resize_values == NULL) {
126 *data = resize_values;
127 *data_length = n_tokens + 1;
132 /* checks the type of key and assigns data */
134 parse_entry(char *entry, struct cperf_test_vector *vector)
136 char *token, *key_token;
137 uint8_t *data = NULL;
139 uint32_t data_length;
142 token = strtok(entry, ENTRY_DELIMITER);
145 /* get values for key */
146 token = strtok(NULL, ENTRY_DELIMITER);
149 printf("Expected 'key = values' but was '%.40s'..\n",
154 status = parse_values(token, &data, &data_length);
159 if (strstr(key_token, "plaintext")) {
160 if (vector->plaintext.data)
161 rte_free(vector->plaintext.data);
162 vector->plaintext.data = data;
163 vector->plaintext.length = data_length;
164 } else if (strstr(key_token, "cipher_key")) {
165 if (vector->cipher_key.data)
166 rte_free(vector->cipher_key.data);
167 vector->cipher_key.data = data;
168 vector->cipher_key.length = data_length;
169 } else if (strstr(key_token, "auth_key")) {
170 if (vector->auth_key.data)
171 rte_free(vector->auth_key.data);
172 vector->auth_key.data = data;
173 vector->auth_key.length = data_length;
174 } else if (strstr(key_token, "iv")) {
176 rte_free(vector->iv.data);
177 vector->iv.data = data;
178 vector->iv.phys_addr = rte_malloc_virt2phy(vector->iv.data);
179 vector->iv.length = data_length;
180 } else if (strstr(key_token, "ciphertext")) {
181 if (vector->ciphertext.data)
182 rte_free(vector->ciphertext.data);
183 vector->ciphertext.data = data;
184 vector->ciphertext.length = data_length;
185 } else if (strstr(key_token, "aad")) {
186 if (vector->aad.data)
187 rte_free(vector->aad.data);
188 vector->aad.data = data;
189 vector->aad.phys_addr = rte_malloc_virt2phy(vector->aad.data);
190 vector->aad.length = data_length;
191 } else if (strstr(key_token, "digest")) {
192 if (vector->digest.data)
193 rte_free(vector->digest.data);
194 vector->digest.data = data;
195 vector->digest.phys_addr = rte_malloc_virt2phy(
196 vector->digest.data);
197 vector->digest.length = data_length;
199 printf("Not valid key: '%s'\n", trim(key_token));
206 /* searches in the file for registry keys and values */
208 parse_file(struct cperf_test_vector *v_vec, const char *path)
211 char *line = NULL, *entry = NULL;
216 fp = fopen(path, "r");
218 printf("File %s does not exists\n", path);
222 while ((read = getline(&line, &len, fp)) != -1) {
223 /* ignore comments and new lines */
224 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
225 || line[0] == '\r' || line[0] == ' ')
230 /* buffer for multiline */
231 entry = (char *) rte_realloc(entry,
232 sizeof(char) * strlen(line) + 1, 0);
236 memset(entry, 0, strlen(line) + 1);
237 strncpy(entry, line, strlen(line));
239 /* check if entry ends with , or = */
240 if (entry[strlen(entry) - 1] == ','
241 || entry[strlen(entry) - 1] == '=') {
242 while ((read = getline(&line, &len, fp)) != -1) {
245 /* extend entry about length of new line */
246 char *entry_extended = (char *) rte_realloc(
248 * (strlen(line) + strlen(entry))
251 if (entry_extended == NULL)
253 entry = entry_extended;
255 strncat(entry, line, strlen(line));
257 if (entry[strlen(entry) - 1] != ',')
261 status = parse_entry(entry, v_vec);
263 printf("An error occurred while parsing!\n");
285 struct cperf_test_vector*
286 cperf_test_vector_get_from_file(struct cperf_options *opts)
289 struct cperf_test_vector *test_vector = NULL;
291 if (opts == NULL || opts->test_file == NULL)
294 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
295 sizeof(struct cperf_test_vector), 0);
296 if (test_vector == NULL)
299 /* filling the vector with data from a file */
300 status = parse_file(test_vector, opts->test_file);
302 free_test_vector(test_vector, opts);
306 /* other values not included in the file */
307 test_vector->data.cipher_offset = 0;
308 test_vector->data.cipher_length = opts->buffer_sz;
310 test_vector->data.auth_offset = 0;
311 test_vector->data.auth_length = opts->buffer_sz;