aboutsummaryrefslogtreecommitdiffstats
path: root/app/test-bbdev/test_bbdev_vector.c
blob: 81b8ee78f65e4da0be691266f1780cd3a59de66e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2017 Intel Corporation
 */

#ifdef RTE_EXEC_ENV_BSDAPP
	#define _WITH_GETLINE
#endif
#include <stdio.h>
#include <stdbool.h>
#include <rte_malloc.h>

#include "test_bbdev_vector.h"

#define VALUE_DELIMITER ","
#define ENTRY_DELIMITER "="

const char *op_data_prefixes[] = {
	"input",
	"soft_output",
	"hard_output",
};

/* trim leading and trailing spaces */
static void
trim_space(char *str)
{
	char *start, *end;

	for (start = str; *start; start++) {
		if (!isspace((unsigned char) start[0]))
			break;
	}

	for (end = start + strlen(start); end > start + 1; end--) {
		if (!isspace((unsigned char) end[-1]))
			break;
	}

	*end = 0;

	/* Shift from "start" to the beginning of the string */
	if (start > str)
		memmove(str, start, (end - start) + 1);
}

static bool
starts_with(const char *str, const char *pre)
{
	return strncmp(pre, str, strlen(pre)) == 0;
}

/* tokenization test values separated by a comma */
static int
parse_values(char *tokens, uint32_t **data, uint32_t *data_length)
{
	uint32_t n_tokens = 0;
	uint32_t data_size = 32;

	uint32_t *values, *values_resized;
	char *tok, *error = NULL;

	tok = strtok(tokens, VALUE_DELIMITER);
	if (tok == NULL)
		return -1;

	values = (uint32_t *)
			rte_zmalloc(NULL, sizeof(uint32_t) * data_size, 0);
	if (values == NULL)
		return -1;

	while (tok != NULL) {
		values_resized = NULL;

		if (n_tokens >= data_size) {
			data_size *= 2;

			values_resized = (uint32_t *) rte_realloc(values,
				sizeof(uint32_t) * data_size, 0);
			if (values_resized == NULL) {
				rte_free(values);
				return -1;
			}
			values = values_resized;
		}

		values[n_tokens] = (uint32_t) strtoul(tok, &error, 0);
		if ((error == NULL) || (*error != '\0')) {
			printf("Failed with convert '%s'\n", tok);
			rte_free(values);
			return -1;
		}

		*data_length = *data_length + (strlen(tok) - strlen("0x"))/2;

		tok = strtok(NULL, VALUE_DELIMITER);
		if (tok == NULL)
			break;

		n_tokens++;
	}

	values_resized = (uint32_t *) rte_realloc(values,
		sizeof(uint32_t) * (n_tokens + 1), 0);

	if (values_resized == NULL) {
		rte_free(values);
		return -1;
	}

	*data = values_resized;

	return 0;
}

/* convert turbo decoder flag from string to unsigned long int*/
static int
op_decoder_flag_strtoul(char *token, uint32_t *op_flag_value)
{
	if (!strcmp(token, "RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE"))
		*op_flag_value = RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_CRC_TYPE_24B"))
		*op_flag_value = RTE_BBDEV_TURBO_CRC_TYPE_24B;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_EQUALIZER"))
		*op_flag_value = RTE_BBDEV_TURBO_EQUALIZER;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_SOFT_OUT_SATURATE"))
		*op_flag_value = RTE_BBDEV_TURBO_SOFT_OUT_SATURATE;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_HALF_ITERATION_EVEN"))
		*op_flag_value = RTE_BBDEV_TURBO_HALF_ITERATION_EVEN;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_CONTINUE_CRC_MATCH"))
		*op_flag_value = RTE_BBDEV_TURBO_CONTINUE_CRC_MATCH;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_SOFT_OUTPUT"))
		*op_flag_value = RTE_BBDEV_TURBO_SOFT_OUTPUT;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_EARLY_TERMINATION"))
		*op_flag_value = RTE_BBDEV_TURBO_EARLY_TERMINATION;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN"))
		*op_flag_value = RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN"))
		*op_flag_value = RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_POS_LLR_1_BIT_SOFT_OUT"))
		*op_flag_value = RTE_BBDEV_TURBO_POS_LLR_1_BIT_SOFT_OUT;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_NEG_LLR_1_BIT_SOFT_OUT"))
		*op_flag_value = RTE_BBDEV_TURBO_NEG_LLR_1_BIT_SOFT_OUT;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_MAP_DEC"))
		*op_flag_value = RTE_BBDEV_TURBO_MAP_DEC;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_DEC_SCATTER_GATHER"))
		*op_flag_value = RTE_BBDEV_TURBO_DEC_SCATTER_GATHER;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP"))
		*op_flag_value = RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP;
	else {
		printf("The given value is not a turbo decoder flag\n");
		return -1;
	}

	return 0;
}

/* convert turbo encoder flag from string to unsigned long int*/
static int
op_encoder_flag_strtoul(char *token, uint32_t *op_flag_value)
{
	if (!strcmp(token, "RTE_BBDEV_TURBO_RV_INDEX_BYPASS"))
		*op_flag_value = RTE_BBDEV_TURBO_RV_INDEX_BYPASS;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_RATE_MATCH"))
		*op_flag_value = RTE_BBDEV_TURBO_RATE_MATCH;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_CRC_24B_ATTACH"))
		*op_flag_value = RTE_BBDEV_TURBO_CRC_24B_ATTACH;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_CRC_24A_ATTACH"))
		*op_flag_value = RTE_BBDEV_TURBO_CRC_24A_ATTACH;
	else if (!strcmp(token, "RTE_BBDEV_TURBO_ENC_SCATTER_GATHER"))
		*op_flag_value = RTE_BBDEV_TURBO_ENC_SCATTER_GATHER;
	else {
		printf("The given value is not a turbo encoder flag\n");
		return -1;
	}

	return 0;
}

/* tokenization turbo decoder/encoder flags values separated by a comma */
static int
parse_turbo_flags(char *tokens, uint32_t *op_flags,
		enum rte_bbdev_op_type op_type)
{
	char *tok = NULL;
	uint32_t op_flag_value = 0;

	tok = strtok(tokens, VALUE_DELIMITER);
	if (tok == NULL)
		return -1;

	while (tok != NULL) {
		trim_space(tok);
		if (op_type == RTE_BBDEV_OP_TURBO_DEC) {
			if (op_decoder_flag_strtoul(tok, &op_flag_value) == -1)
				return -1;
		} else if (op_type == RTE_BBDEV_OP_TURBO_ENC) {
			if (op_encoder_flag_strtoul(tok, &op_flag_value) == -1)
				return -1;
		} else {
			return -1;
		}

		*op_flags = *op_flags | op_flag_value;

		tok = strtok(NULL, VALUE_DELIMITER);
		if (tok == NULL)
			break;
	}

	return 0;
}

/* convert turbo encoder/decoder op_type from string to enum*/
static int
op_turbo_type_strtol(char *token, enum rte_bbdev_op_type *op_type)
{
	trim_space(token);
	if (!strcmp(token, "RTE_BBDEV_OP_TURBO_DEC"))
		*op_type = RTE_BBDEV_OP_TURBO_DEC;
	else if (!strcmp(token, "RTE_BBDEV_OP_TURBO_ENC"))
		*op_type = RTE_BBDEV_OP_TURBO_ENC;
	else if (!strcmp(token, "RTE_BBDEV_OP_NONE"))
		*op_type = RTE_BBDEV_OP_NONE;
	else {
		printf("Not valid turbo op_type: '%s'\n", token);
		return -1;
	}

	return 0;
}

/* tokenization expected status values separated by a comma */
static int
parse_expected_status(char *tokens, int *status, enum rte_bbdev_op_type op_type)
{
	char *tok = NULL;
	bool status_ok = false;

	tok = strtok(tokens, VALUE_DELIMITER);
	if (tok == NULL)
		return -1;

	while (tok != NULL) {
		trim_space(tok);
		if (!strcmp(tok, "OK"))
			status_ok = true;
		else if (!strcmp(tok, "DMA"))
			*status = *status | (1 << RTE_BBDEV_DRV_ERROR);
		else if (!strcmp(tok, "FCW"))
			*status = *status | (1 << RTE_BBDEV_DATA_ERROR);
		else if (!strcmp(tok, "CRC")) {
			if (op_type == RTE_BBDEV_OP_TURBO_DEC)
				*status = *status | (1 << RTE_BBDEV_CRC_ERROR);
			else {
				printf(
						"CRC is only a valid value for turbo decoder\n");
				return -1;
			}
		} else {
			printf("Not valid status: '%s'\n", tok);
			return -1;
		}

		tok = strtok(NULL, VALUE_DELIMITER);
		if (tok == NULL)
			break;
	}

	if (status_ok && *status != 0) {
		printf(
				"Not valid status values. Cannot be OK and ERROR at the same time.\n");
		return -1;
	}

	return 0;
}

/* parse ops data entry (there can be more than 1 input entry, each will be
 * contained in a separate op_data_buf struct)
 */
static int
parse_data_entry(const char *key_token, char *token,
		struct test_bbdev_vector *vector, enum op_data_type type,
		const char *prefix)
{
	int ret;
	uint32_t data_length = 0;
	uint32_t *data = NULL;
	unsigned int id;
	struct op_data_buf *op_data;
	unsigned int *nb_ops;

	if (type >= DATA_NUM_TYPES) {
		printf("Unknown op type: %d!\n", type);
		return -1;
	}

	op_data = vector->entries[type].segments;
	nb_ops = &vector->entries[type].nb_segments;

	if (*nb_ops >= RTE_BBDEV_MAX_CODE_BLOCKS) {
		printf("Too many segments (code blocks defined): %u, max %d!\n",
				*nb_ops, RTE_BBDEV_MAX_CODE_BLOCKS);
		return -1;
	}

	if (sscanf(key_token + strlen(prefix), "%u", &id) != 1) {
		printf("Missing ID of %s\n", prefix);
		return -1;
	}
	if (id != *nb_ops) {
		printf(
			"Please order data entries sequentially, i.e. %s0, %s1, ...\n",
				prefix, prefix);
		return -1;
	}

	/* Clear new op data struct */
	memset(op_data + *nb_ops, 0, sizeof(struct op_data_buf));

	ret = parse_values(token, &data, &data_length);
	if (!ret) {
		op_data[*nb_ops].addr = data;
		op_data[*nb_ops].length = data_length;
		++(*nb_ops);
	}

	return ret;
}

/* parses turbo decoder parameters and assigns to global variable */
static int
parse_decoder_params(const char *key_token, char *token,
		struct test_bbdev_vector *vector)
{
	int ret = 0, status = 0;
	uint32_t op_flags = 0;
	char *err = NULL;

	struct rte_bbdev_op_turbo_dec *turbo_dec = &vector->turbo_dec;

	/* compare keys */
	if (starts_with(key_token, op_data_prefixes[DATA_INPUT]))
		ret = parse_data_entry(key_token, token, vector,
				DATA_INPUT, op_data_prefixes[DATA_INPUT]);

	else if (starts_with(key_token, op_data_prefixes[DATA_SOFT_OUTPUT]))
		ret = parse_data_entry(key_token, token, vector,
				DATA_SOFT_OUTPUT,
				op_data_prefixes[DATA_SOFT_OUTPUT]);

	else if (starts_with(key_token, op_data_prefixes[DATA_HARD_OUTPUT]))
		ret = parse_data_entry(key_token, token, vector,
				DATA_HARD_OUTPUT,
				op_data_prefixes[DATA_HARD_OUTPUT]);
	else if (!strcmp(key_token, "e")) {
		vector->mask |= TEST_BBDEV_VF_E;
		turbo_dec->cb_params.e = (uint32_t) strtoul(token, &err, 0);
	} else if (!strcmp(key_token, "ea")) {
		vector->mask |= TEST_BBDEV_VF_EA;
		turbo_dec->tb_params.ea = (uint32_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "eb")) {
		vector->mask |= TEST_BBDEV_VF_EB;
		turbo_dec->tb_params.eb = (uint32_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k")) {
		vector->mask |= TEST_BBDEV_VF_K;
		turbo_dec->cb_params.k = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k_pos")) {
		vector->mask |= TEST_BBDEV_VF_K_POS;
		turbo_dec->tb_params.k_pos = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k_neg")) {
		vector->mask |= TEST_BBDEV_VF_K_NEG;
		turbo_dec->tb_params.k_neg = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "c")) {
		vector->mask |= TEST_BBDEV_VF_C;
		turbo_dec->tb_params.c = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "c_neg")) {
		vector->mask |= TEST_BBDEV_VF_C_NEG;
		turbo_dec->tb_params.c_neg = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "cab")) {
		vector->mask |= TEST_BBDEV_VF_CAB;
		turbo_dec->tb_params.cab = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "rv_index")) {
		vector->mask |= TEST_BBDEV_VF_RV_INDEX;
		turbo_dec->rv_index = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "iter_max")) {
		vector->mask |= TEST_BBDEV_VF_ITER_MAX;
		turbo_dec->iter_max = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "iter_min")) {
		vector->mask |= TEST_BBDEV_VF_ITER_MIN;
		turbo_dec->iter_min = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "expected_iter_count")) {
		vector->mask |= TEST_BBDEV_VF_EXPECTED_ITER_COUNT;
		turbo_dec->iter_count = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "ext_scale")) {
		vector->mask |= TEST_BBDEV_VF_EXT_SCALE;
		turbo_dec->ext_scale = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "num_maps")) {
		vector->mask |= TEST_BBDEV_VF_NUM_MAPS;
		turbo_dec->num_maps = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "code_block_mode")) {
		vector->mask |= TEST_BBDEV_VF_CODE_BLOCK_MODE;
		turbo_dec->code_block_mode = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "op_flags")) {
		vector->mask |= TEST_BBDEV_VF_OP_FLAGS;
		ret = parse_turbo_flags(token, &op_flags,
			vector->op_type);
		if (!ret)
			turbo_dec->op_flags = op_flags;
	} else if (!strcmp(key_token, "expected_status")) {
		vector->mask |= TEST_BBDEV_VF_EXPECTED_STATUS;
		ret = parse_expected_status(token, &status, vector->op_type);
		if (!ret)
			vector->expected_status = status;
	} else {
		printf("Not valid dec key: '%s'\n", key_token);
		return -1;
	}

	if (ret != 0) {
		printf("Failed with convert '%s\t%s'\n", key_token, token);
		return -1;
	}

	return 0;
}

/* parses turbo encoder parameters and assigns to global variable */
static int
parse_encoder_params(const char *key_token, char *token,
		struct test_bbdev_vector *vector)
{
	int ret = 0, status = 0;
	uint32_t op_flags = 0;
	char *err = NULL;


	struct rte_bbdev_op_turbo_enc *turbo_enc = &vector->turbo_enc;

	if (starts_with(key_token, op_data_prefixes[DATA_INPUT]))
		ret = parse_data_entry(key_token, token, vector,
				DATA_INPUT, op_data_prefixes[DATA_INPUT]);
	else if (starts_with(key_token, "output"))
		ret = parse_data_entry(key_token, token, vector,
				DATA_HARD_OUTPUT, "output");
	else if (!strcmp(key_token, "e")) {
		vector->mask |= TEST_BBDEV_VF_E;
		turbo_enc->cb_params.e = (uint32_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "ea")) {
		vector->mask |= TEST_BBDEV_VF_EA;
		turbo_enc->tb_params.ea = (uint32_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "eb")) {
		vector->mask |= TEST_BBDEV_VF_EB;
		turbo_enc->tb_params.eb = (uint32_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k")) {
		vector->mask |= TEST_BBDEV_VF_K;
		turbo_enc->cb_params.k = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k_neg")) {
		vector->mask |= TEST_BBDEV_VF_K_NEG;
		turbo_enc->tb_params.k_neg = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "k_pos")) {
		vector->mask |= TEST_BBDEV_VF_K_POS;
		turbo_enc->tb_params.k_pos = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "c_neg")) {
		vector->mask |= TEST_BBDEV_VF_C_NEG;
		turbo_enc->tb_params.c_neg = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "c")) {
		vector->mask |= TEST_BBDEV_VF_C;
		turbo_enc->tb_params.c = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "cab")) {
		vector->mask |= TEST_BBDEV_VF_CAB;
		turbo_enc->tb_params.cab = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "rv_index")) {
		vector->mask |= TEST_BBDEV_VF_RV_INDEX;
		turbo_enc->rv_index = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "ncb")) {
		vector->mask |= TEST_BBDEV_VF_NCB;
		turbo_enc->cb_params.ncb = (uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "ncb_neg")) {
		vector->mask |= TEST_BBDEV_VF_NCB_NEG;
		turbo_enc->tb_params.ncb_neg =
				(uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "ncb_pos")) {
		vector->mask |= TEST_BBDEV_VF_NCB_POS;
		turbo_enc->tb_params.ncb_pos =
				(uint16_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "r")) {
		vector->mask |= TEST_BBDEV_VF_R;
		turbo_enc->tb_params.r = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "code_block_mode")) {
		vector->mask |= TEST_BBDEV_VF_CODE_BLOCK_MODE;
		turbo_enc->code_block_mode = (uint8_t) strtoul(token, &err, 0);
		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
	} else if (!strcmp(key_token, "op_flags")) {
		vector->mask |= TEST_BBDEV_VF_OP_FLAGS;
		ret = parse_turbo_flags(token, &op_flags,
				vector->op_type);
		if (!ret)
			turbo_enc->op_flags = op_flags;
	} else if (!strcmp(key_token, "expected_status")) {
		vector->mask |= TEST_BBDEV_VF_EXPECTED_STATUS;
		ret = parse_expected_status(token, &status, vector->op_type);
		if (!ret)
			vector->expected_status = status;
	} else {
		printf("Not valid enc key: '%s'\n", key_token);
		return -1;
	}

	if (ret != 0) {
		printf("Failed with convert '%s\t%s'\n", key_token, token);
		return -1;
	}

	return 0;
}

/* checks the type of key and assigns data */
static int
parse_entry(char *entry, struct test_bbdev_vector *vector)
{
	int ret = 0;
	char *token, *key_token;
	enum rte_bbdev_op_type op_type = RTE_BBDEV_OP_NONE;

	if (entry == NULL) {
		printf("Expected entry value\n");
		return -1;
	}

	/* get key */
	token = strtok(entry, ENTRY_DELIMITER);
	key_token = token;
	/* get values for key */
	token = strtok(NULL, ENTRY_DELIMITER);

	if (key_token == NULL || token == NULL) {
		printf("Expected 'key = values' but was '%.40s'..\n", entry);
		return -1;
	}
	trim_space(key_token);

	/* first key_token has to specify type of operation */
	if (vector->op_type == RTE_BBDEV_OP_NONE) {
		if (!strcmp(key_token, "op_type")) {
			ret = op_turbo_type_strtol(token, &op_type);
			if (!ret)
				vector->op_type = op_type;
			return (!ret) ? 0 : -1;
		}
		printf("First key_token (%s) does not specify op_type\n",
				key_token);
		return -1;
	}

	/* compare keys */
	if (vector->op_type == RTE_BBDEV_OP_TURBO_DEC) {
		if (parse_decoder_params(key_token, token, vector) == -1)
			return -1;
	} else if (vector->op_type == RTE_BBDEV_OP_TURBO_ENC) {
		if (parse_encoder_params(key_token, token, vector) == -1)
			return -1;
	}

	return 0;
}

static int
check_decoder_segments(struct test_bbdev_vector *vector)
{
	unsigned char i;
	struct rte_bbdev_op_turbo_dec *turbo_dec = &vector->turbo_dec;

	if (vector->entries[DATA_INPUT].nb_segments == 0)
		return -1;

	for (i = 0; i < vector->entries[DATA_INPUT].nb_segments; i++)
		if (vector->entries[DATA_INPUT].segments[i].addr == NULL)
			return -1;

	if (vector->entries[DATA_HARD_OUTPUT].nb_segments == 0)
		return -1;

	for (i = 0; i < vector->entries[DATA_HARD_OUTPUT].nb_segments;
			i++)
		if (vector->entries[DATA_HARD_OUTPUT].segments[i].addr == NULL)
			return -1;

	if ((turbo_dec->op_flags & RTE_BBDEV_TURBO_SOFT_OUTPUT) &&
			(vector->entries[DATA_SOFT_OUTPUT].nb_segments == 0))
		return -1;

	for (i = 0; i < vector->entries[DATA_SOFT_OUTPUT].nb_segments;
			i++)
		if (vector->entries[DATA_SOFT_OUTPUT].segments[i].addr == NULL)
			return -1;

	return 0;
}

static int
check_decoder_llr_spec(struct test_bbdev_vector *vector)
{
	struct rte_bbdev_op_turbo_dec *turbo_dec = &vector->turbo_dec;

	/* Check input LLR sign formalism specification */
	if ((turbo_dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN) &&
			(turbo_dec->op_flags &
			RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)) {
		printf(
			"Both positive and negative LLR input flags were set!\n");
		return -1;
	}
	if (!(turbo_dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN) &&
			!(turbo_dec->op_flags &
			RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)) {
		printf(
			"WARNING: input LLR sign formalism was not specified and will be set to negative LLR for '1' bit\n");
		turbo_dec->op_flags |= RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN;
	}

	if (!(turbo_dec->op_flags & RTE_BBDEV_TURBO_SOFT_OUTPUT))
		return 0;

	/* Check output LLR sign formalism specification */
	if ((turbo_dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_SOFT_OUT) &&
			(turbo_dec->op_flags &
			RTE_BBDEV_TURBO_NEG_LLR_1_BIT_SOFT_OUT)) {
		printf(
			"Both positive and negative LLR output flags were set!\n");
		return -1;
	}
	if (!(turbo_dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_SOFT_OUT) &&
			!(turbo_dec->op_flags &
			RTE_BBDEV_TURBO_NEG_LLR_1_BIT_SOFT_OUT)) {
		printf(
			"WARNING: soft output LLR sign formalism was not specified and will be set to negative LLR for '1' bit\n");
		turbo_dec->op_flags |=
				RTE_BBDEV_TURBO_NEG_LLR_1_BIT_SOFT_OUT;
	}

	return 0;
}

/* checks decoder parameters */
static int
check_decoder(struct test_bbdev_vector *vector)
{
	struct rte_bbdev_op_turbo_dec *turbo_dec = &vector->turbo_dec;
	const int mask = vector->mask;

	if (check_decoder_segments(vector) < 0)
		return -1;

	if (check_decoder_llr_spec(vector) < 0)
		return -1;

	/* Check which params were set */
	if (!(mask & TEST_BBDEV_VF_CODE_BLOCK_MODE)) {
		printf(
			"WARNING: code_block_mode was not specified in vector file and will be set to 1 (0 - TB Mode, 1 - CB mode)\n");
		turbo_dec->code_block_mode = 1;
	}
	if (turbo_dec->code_block_mode == 0) {
		if (!(mask & TEST_BBDEV_VF_EA))
			printf(
				"WARNING: ea was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_EB))
			printf(
				"WARNING: eb was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K_NEG))
			printf(
				"WARNING: k_neg was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K_POS))
			printf(
				"WARNING: k_pos was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_C_NEG))
			printf(
				"WARNING: c_neg was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_C)) {
			printf(
				"WARNING: c was not specified in vector file and will be set to 1\n");
			turbo_dec->tb_params.c = 1;
		}
		if (!(mask & TEST_BBDEV_VF_CAB))
			printf(
				"WARNING: cab was not specified in vector file and will be set to 0\n");
	} else {
		if (!(mask & TEST_BBDEV_VF_E))
			printf(
				"WARNING: e was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K))
			printf(
				"WARNING: k was not specified in vector file and will be set to 0\n");
	}
	if (!(mask & TEST_BBDEV_VF_RV_INDEX))
		printf(
			"WARNING: rv_index was not specified in vector file and will be set to 0\n");
	if (!(mask & TEST_BBDEV_VF_ITER_MIN))
		printf(
			"WARNING: iter_min was not specified in vector file and will be set to 0\n");
	if (!(mask & TEST_BBDEV_VF_ITER_MAX))
		printf(
			"WARNING: iter_max was not specified in vector file and will be set to 0\n");
	if (!(mask & TEST_BBDEV_VF_EXPECTED_ITER_COUNT))
		printf(
			"WARNING: expected_iter_count was not specified in vector file and iter_count will not be validated\n");
	if (!(mask & TEST_BBDEV_VF_EXT_SCALE))
		printf(
			"WARNING: ext_scale was not specified in vector file and will be set to 0\n");
	if (!(mask & TEST_BBDEV_VF_OP_FLAGS)) {
		printf(
			"WARNING: op_flags was not specified in vector file and capabilities will not be validated\n");
		turbo_dec->num_maps = 0;
	} else if (!(turbo_dec->op_flags & RTE_BBDEV_TURBO_MAP_DEC) &&
			mask & TEST_BBDEV_VF_NUM_MAPS) {
		printf(
			"WARNING: RTE_BBDEV_TURBO_MAP_DEC was not set in vector file and num_maps will be set to 0\n");
		turbo_dec->num_maps = 0;
	}
	if (!(mask & TEST_BBDEV_VF_EXPECTED_STATUS))
		printf(
			"WARNING: expected_status was not specified in vector file and will be set to 0\n");
	return 0;
}

/* checks encoder parameters */
static int
check_encoder(struct test_bbdev_vector *vector)
{
	unsigned char i;
	const int mask = vector->mask;

	if (vector->entries[DATA_INPUT].nb_segments == 0)
		return -1;

	for (i = 0; i < vector->entries[DATA_INPUT].nb_segments; i++)
		if (vector->entries[DATA_INPUT].segments[i].addr == NULL)
			return -1;

	if (vector->entries[DATA_HARD_OUTPUT].nb_segments == 0)
		return -1;

	for (i = 0; i < vector->entries[DATA_HARD_OUTPUT].nb_segments; i++)
		if (vector->entries[DATA_HARD_OUTPUT].segments[i].addr == NULL)
			return -1;

	if (!(mask & TEST_BBDEV_VF_CODE_BLOCK_MODE)) {
		printf(
			"WARNING: code_block_mode was not specified in vector file and will be set to 1\n");
		vector->turbo_enc.code_block_mode = 1;
	}
	if (vector->turbo_enc.code_block_mode == 0) {
		if (!(mask & TEST_BBDEV_VF_EA) && (vector->turbo_enc.op_flags &
				RTE_BBDEV_TURBO_RATE_MATCH))
			printf(
				"WARNING: ea was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_EB) && (vector->turbo_enc.op_flags &
				RTE_BBDEV_TURBO_RATE_MATCH))
			printf(
				"WARNING: eb was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K_NEG))
			printf(
				"WARNING: k_neg was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K_POS))
			printf(
				"WARNING: k_pos was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_C_NEG))
			printf(
				"WARNING: c_neg was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_C)) {
			printf(
				"WARNING: c was not specified in vector file and will be set to 1\n");
			vector->turbo_enc.tb_params.c = 1;
		}
		if (!(mask & TEST_BBDEV_VF_CAB) && (vector->turbo_enc.op_flags &
				RTE_BBDEV_TURBO_RATE_MATCH))
			printf(
				"WARNING: cab was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_NCB_NEG))
			printf(
				"WARNING: ncb_neg was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_NCB_POS))
			printf(
				"WARNING: ncb_pos was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_R))
			printf(
				"WARNING: r was not specified in vector file and will be set to 0\n");
	} else {
		if (!(mask & TEST_BBDEV_VF_E) && (vector->turbo_enc.op_flags &
				RTE_BBDEV_TURBO_RATE_MATCH))
			printf(
				"WARNING: e was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_K))
			printf(
				"WARNING: k was not specified in vector file and will be set to 0\n");
		if (!(mask & TEST_BBDEV_VF_NCB))
			printf(
				"WARNING: ncb was not specified in vector file and will be set to 0\n");
	}
	if (!(mask & TEST_BBDEV_VF_RV_INDEX))
		printf(
			"WARNING: rv_index was not specified in vector file and will be set to 0\n");
	if (!(mask & TEST_BBDEV_VF_OP_FLAGS))
		printf(
			"WARNING: op_flags was not specified in vector file and capabilities will not be validated\n");
	if (!(mask & TEST_BBDEV_VF_EXPECTED_STATUS))
		printf(
			"WARNING: expected_status was not specified in vector file and will be set to 0\n");

	return 0;
}

static int
bbdev_check_vector(struct test_bbdev_vector *vector)
{
	if (vector->op_type == RTE_BBDEV_OP_TURBO_DEC) {
		if (check_decoder(vector) == -1)
			return -1;
	} else if (vector->op_type == RTE_BBDEV_OP_TURBO_ENC) {
		if (check_encoder(vector) == -1)
			return -1;
	} else if (vector->op_type != RTE_BBDEV_OP_NONE) {
		printf("Vector was not filled\n");
		return -1;
	}

	return 0;
}

int
test_bbdev_vector_read(const char *filename,
		struct test_bbdev_vector *vector)
{
	int ret = 0;
	size_t len = 0;

	FILE *fp = NULL;
	char *line = NULL;
	char *entry = NULL;

	fp = fopen(filename, "r");
	if (fp == NULL) {
		printf("File %s does not exist\n", filename);
		return -1;
	}

	while (getline(&line, &len, fp) != -1) {

		/* ignore comments and new lines */
		if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
			|| line[0] == '\r')
			continue;

		trim_space(line);

		/* buffer for multiline */
		entry = realloc(entry, strlen(line) + 1);
		if (entry == NULL) {
			printf("Fail to realloc %zu bytes\n", strlen(line) + 1);
			ret = -ENOMEM;
			goto exit;
		}

		strcpy(entry, line);

		/* check if entry ends with , or = */
		if (entry[strlen(entry) - 1] == ','
			|| entry[strlen(entry) - 1] == '=') {
			while (getline(&line, &len, fp) != -1) {
				trim_space(line);

				/* extend entry about length of new line */
				char *entry_extended = realloc(entry,
						strlen(line) +
						strlen(entry) + 1);

				if (entry_extended == NULL) {
					printf("Fail to allocate %zu bytes\n",
							strlen(line) +
							strlen(entry) + 1);
					ret = -ENOMEM;
					goto exit;
				}

				entry = entry_extended;
				/* entry has been allocated accordingly */
				strcpy(&entry[strlen(entry)], line);

				if (entry[strlen(entry) - 1] != ',')
					break;
			}
		}
		ret = parse_entry(entry, vector);
		if (ret != 0) {
			printf("An error occurred while parsing!\n");
			goto exit;
		}
	}
	ret = bbdev_check_vector(vector);
	if (ret != 0)
		printf("An error occurred while checking!\n");

exit:
	fclose(fp);
	free(line);
	free(entry);

	return ret;
}