DPDK logo

Elixir Cross Referencer

   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
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2019 Intel Corporation
 */

#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include <rte_cycles.h>
#include <rte_errno.h>
#include <rte_ip.h>
#include <rte_random.h>
#include <rte_malloc.h>
#include <rte_lpm.h>
#include <rte_lpm6.h>
#include <rte_fib.h>
#include <rte_fib6.h>

#define	PRINT_USAGE_START	"%s [EAL options] --\n"

#define GET_CB_FIELD(in, fd, base, lim, dlm)	do {		\
	unsigned long val;					\
	char *end_fld;						\
	errno = 0;						\
	val = strtoul((in), &end_fld, (base));			\
	if (errno != 0 || end_fld[0] != (dlm) || val > (lim))	\
		return -EINVAL;					\
	(fd) = (typeof(fd))val;					\
	(in) = end_fld + 1;					\
} while (0)

#define	DEF_ROUTES_NUM		0x10000
#define	DEF_LOOKUP_IPS_NUM	0x100000
#define BURST_SZ		64
#define DEFAULT_LPM_TBL8	100000U

#define CMP_FLAG		(1 << 0)
#define CMP_ALL_FLAG		(1 << 1)
#define IPV6_FLAG		(1 << 2)
#define FIB_RIB_TYPE		(1 << 3)
#define FIB_V4_DIR_TYPE		(1 << 4)
#define FIB_V6_TRIE_TYPE	(1 << 4)
#define FIB_TYPE_MASK		(FIB_RIB_TYPE|FIB_V4_DIR_TYPE|FIB_V6_TRIE_TYPE)
#define SHUFFLE_FLAG		(1 << 7)
#define DRY_RUN_FLAG		(1 << 8)

static char *distrib_string;
static char line[LINE_MAX];

enum {
	RT_PREFIX,
	RT_NEXTHOP,
	RT_NUM
};

#ifndef NIPQUAD
#define NIPQUAD_FMT "%u.%u.%u.%u"
#define NIPQUAD(addr)				\
	(unsigned)((unsigned char *)&addr)[3],	\
	(unsigned)((unsigned char *)&addr)[2],	\
	(unsigned)((unsigned char *)&addr)[1],	\
	(unsigned)((unsigned char *)&addr)[0]

#define NIPQUAD6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
#define NIPQUAD6(addr)				\
	((uint8_t *)addr)[0] << 8 |	\
	((uint8_t *)addr)[1],		\
	((uint8_t *)addr)[2] << 8 |	\
	((uint8_t *)addr)[3],		\
	((uint8_t *)addr)[4] << 8 |	\
	((uint8_t *)addr)[5],		\
	((uint8_t *)addr)[6] << 8 |	\
	((uint8_t *)addr)[7],		\
	((uint8_t *)addr)[8] << 8 |	\
	((uint8_t *)addr)[9],		\
	((uint8_t *)addr)[10] << 8 |	\
	((uint8_t *)addr)[11],		\
	((uint8_t *)addr)[12] << 8 |	\
	((uint8_t *)addr)[13],		\
	((uint8_t *)addr)[14] << 8 |	\
	((uint8_t *)addr)[15]
#endif

static struct {
	const char	*prgname;
	const char	*routes_file;
	const char	*lookup_ips_file;
	const char	*routes_file_s;
	const char	*lookup_ips_file_s;
	void		*rt;
	void		*lookup_tbl;
	uint32_t	nb_routes;
	uint32_t	nb_lookup_ips;
	uint32_t	nb_lookup_ips_rnd;
	uint32_t	nb_routes_per_depth[128 + 1];
	uint32_t	flags;
	uint32_t	tbl8;
	uint8_t		ent_sz;
	uint8_t		rnd_lookup_ips_ratio;
	uint8_t		print_fract;
	uint8_t		lookup_fn;
} config = {
	.routes_file = NULL,
	.lookup_ips_file = NULL,
	.nb_routes = DEF_ROUTES_NUM,
	.nb_lookup_ips = DEF_LOOKUP_IPS_NUM,
	.nb_lookup_ips_rnd = 0,
	.nb_routes_per_depth = {0},
	.flags = FIB_V4_DIR_TYPE,
	.tbl8 = DEFAULT_LPM_TBL8,
	.ent_sz = 4,
	.rnd_lookup_ips_ratio = 0,
	.print_fract = 10,
	.lookup_fn = 0
};

struct rt_rule_4 {
	uint32_t	addr;
	uint8_t		depth;
	uint64_t	nh;
};

struct rt_rule_6 {
	uint8_t		addr[16];
	uint8_t		depth;
	uint64_t	nh;
};

static uint64_t
get_rnd_rng(uint64_t l, uint64_t u)
{
	if (l == u)
		return l;
	else
		return (rte_rand() % (u - l) + l);
}

static __rte_always_inline __attribute__((pure)) uint8_t
bits_in_nh(uint8_t nh_sz)
{
	return 8 * (1 << nh_sz);
}

static  __rte_always_inline __attribute__((pure)) uint64_t
get_max_nh(uint8_t nh_sz)
{
	/* min between fib and lpm6 which is 21 bits */
	return RTE_MIN(((1ULL << (bits_in_nh(nh_sz) - 1)) - 1),
			(1ULL << 21) - 1);
}

static int
get_fib_type(void)
{
	if (config.flags & IPV6_FLAG) {
		if ((config.flags & FIB_TYPE_MASK) == FIB_V6_TRIE_TYPE)
			return RTE_FIB6_TRIE;
		else
			return RTE_FIB6_DUMMY;
	} else {
		if ((config.flags & FIB_TYPE_MASK) == FIB_V4_DIR_TYPE)
			return RTE_FIB_DIR24_8;
		if ((config.flags & FIB_TYPE_MASK) == FIB_RIB_TYPE)
			return RTE_FIB_DUMMY;
	}
	return -1;
}

static int
complete_distrib(uint8_t depth_lim, const uint32_t n, uint8_t rpd[],
	uint32_t nrpd[])
{
	uint8_t depth;
	uint32_t nr = 0;
	uint8_t m = 0;

	/*
	 * complete number of routes for every depth
	 * that was configured with ratio
	 */
	for (depth = 0; depth <= depth_lim; depth++) {
		if (rpd[depth] != 0) {
			if (rpd[depth] == UINT8_MAX)
				config.nb_routes_per_depth[depth] =
					nrpd[depth];
			else
				config.nb_routes_per_depth[depth] =
					(n * rpd[depth]) / 100;

			nr += config.nb_routes_per_depth[depth];
			m++;
		}
	}

	if (nr > n) {
		printf("Too much configured routes\n");
		return -1;
	}

	/*complete number of routes for every unspecified depths*/
	for (depth = 0; depth <= depth_lim; depth++) {
		if (rpd[depth] == 0) {
			/*we don't need more than two /1 routes*/
			uint64_t max_routes_per_depth =
				1ULL << RTE_MIN(depth, 63);
			uint32_t avg_routes_left = (n - nr) /
				(depth_lim + 1 - m++);
			config.nb_routes_per_depth[depth] =
				RTE_MIN(max_routes_per_depth, avg_routes_left);
			nr += config.nb_routes_per_depth[depth];
		}
	}

	return 0;
}

static int
parse_distrib(uint8_t depth_lim, const uint32_t n)
{
	uint8_t	rpd[128 + 1] = {0}; /*routes ratios per depth including /0 */
	uint32_t nrpd[128 + 1] = {0}; /* number of routes per depth */
	uint32_t n_routes;
	uint8_t depth, ratio, ratio_acc = 0;
	char *in;

	in = strtok(distrib_string, ",");

	/*parse configures routes percentage ratios*/
	while (in != NULL) {
		GET_CB_FIELD(in, depth, 0, UINT8_MAX, ':');
		if (in[strlen(in) - 1] == '%') {
			in[strlen(in) - 1] = 0;
			GET_CB_FIELD(in, ratio, 0, UINT8_MAX, '\0');
			if (depth > depth_lim) {
				printf("Depth /%d is bigger than maximum "
					"allowed depth /%d for this AF\n",
					depth, depth_lim);
				return -EINVAL;
			}
			if (ratio > 100) {
				printf("Ratio for depth /%d is bigger "
					"than 100%%\n", depth);
				return -EINVAL;
			}
			if ((depth < 64) && ((n * ratio) / 100) >
					(1ULL << depth)) {
				printf("Configured ratio %d%% for depth /%d "
					"has %d different routes, but maximum "
					"is %lu\n", ratio, depth,
					((n * ratio) / 100), (1UL << depth));
				return -EINVAL;
			}
			rpd[depth] = ratio;
			/*configured zero routes for a given depth*/
			if (ratio == 0)
				rpd[depth] = UINT8_MAX;
			/*sum of all percentage ratios*/
			ratio_acc += ratio;
		} else {
			GET_CB_FIELD(in, n_routes, 0, UINT32_MAX, '\0');
			rpd[depth] = UINT8_MAX;
			nrpd[depth] = n_routes;
		}

		/*number of configured depths in*/
		in = strtok(NULL, ",");
	}

	if (ratio_acc > 100) {
		printf("Total ratio's sum is bigger than 100%%\n");
		return -EINVAL;
	}

	return complete_distrib(depth_lim, n, rpd, nrpd);
}

static void
shuffle_rt_4(struct rt_rule_4 *rt, int n)
{
	struct rt_rule_4 tmp;
	int i, j;

	for (i = 0; i < n; i++) {
		j = rte_rand() % n;
		tmp.addr = rt[i].addr;
		tmp.depth = rt[i].depth;
		tmp.nh = rt[i].nh;

		rt[i].addr = rt[j].addr;
		rt[i].depth = rt[j].depth;
		rt[i].nh = rt[j].nh;

		rt[j].addr = tmp.addr;
		rt[j].depth = tmp.depth;
		rt[j].nh = tmp.nh;
	}
}

static void
shuffle_rt_6(struct rt_rule_6 *rt, int n)
{
	struct rt_rule_6 tmp;
	int i, j;

	for (i = 0; i < n; i++) {
		j = rte_rand() % n;
		memcpy(tmp.addr, rt[i].addr, 16);
		tmp.depth = rt[i].depth;
		tmp.nh = rt[i].nh;

		memcpy(rt[i].addr, rt[j].addr, 16);
		rt[i].depth = rt[j].depth;
		rt[i].nh = rt[j].nh;

		memcpy(rt[j].addr, tmp.addr, 16);
		rt[j].depth = tmp.depth;
		rt[j].nh = tmp.nh;
	}
}

static void
gen_random_rt_4(struct rt_rule_4 *rt, int nh_sz)
{
	uint32_t i, j, k = 0;

	if (config.nb_routes_per_depth[0] != 0) {
		rt[k].addr = 0;
		rt[k].depth = 0;
		rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
	}

	for (i = 1; i <= 32; i++) {
		double edge = 0;
		double step;
		step = (double)(1ULL << i) / config.nb_routes_per_depth[i];
		for (j = 0; j < config.nb_routes_per_depth[i];
				j++, k++, edge += step) {
			uint64_t rnd_val = get_rnd_rng((uint64_t)edge,
				(uint64_t)(edge + step));
			rt[k].addr = rnd_val << (32 - i);
			rt[k].depth = i;
			rt[k].nh = rte_rand() & get_max_nh(nh_sz);
		}
	}
}

static void
complete_v6_addr(uint32_t *addr, uint32_t rnd, int n)
{
	int i;

	for (i = 0; i < n; i++)
		addr[i] = rte_rand();
	addr[i++] = rnd;
	for (; i < 4; i++)
		addr[i] = 0;
}

static void
gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
{
	uint32_t a, i, j, k = 0;

	if (config.nb_routes_per_depth[0] != 0) {
		memset(rt[k].addr, 0, 16);
		rt[k].depth = 0;
		rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
	}

	for (a = 0; a < 4; a++) {
		for (i = 1; i <= 32; i++) {
			uint32_t rnd;
			double edge = 0;
			double step = (double)(1ULL << i) /
				config.nb_routes_per_depth[(a * 32) + i];
			for (j = 0; j < config.nb_routes_per_depth[a * 32 + i];
					j++, k++, edge += step) {
				uint64_t rnd_val = get_rnd_rng((uint64_t)edge,
					(uint64_t)(edge + step));
				rnd = rte_cpu_to_be_32(rnd_val << (32 - i));
				complete_v6_addr((uint32_t *)rt[k].addr,
					rnd, a);
				rt[k].depth = (a * 32) + i;
				rt[k].nh = rte_rand() & get_max_nh(nh_sz);
			}
		}
	}
}

static inline void
set_rnd_ipv6(uint8_t *addr, uint8_t *route, int depth)
{
	int i;

	for (i = 0; i < 16; i++)
		addr[i] = rte_rand();

	for (i = 0; i < 16; i++) {
		if (depth >= 8)
			addr[i] = route[i];
		else if (depth > 0) {
			addr[i] &= (uint16_t)UINT8_MAX >> depth;
			addr[i] |= route[i] & UINT8_MAX << (8 - depth);
		} else
			return;
		depth -= 8;
	}
}

static void
gen_rnd_lookup_tbl(int af)
{
	uint32_t *tbl4 = config.lookup_tbl;
	uint8_t *tbl6 = config.lookup_tbl;
	struct rt_rule_4 *rt4 = (struct rt_rule_4 *)config.rt;
	struct rt_rule_6 *rt6 = (struct rt_rule_6 *)config.rt;
	uint32_t i, j;

	if (af == AF_INET) {
		for (i = 0, j = 0; i < config.nb_lookup_ips;
				i++, j = (j + 1) % config.nb_routes) {
			if ((rte_rand() % 100) < config.rnd_lookup_ips_ratio) {
				tbl4[i] = rte_rand();
				config.nb_lookup_ips_rnd++;
			} else
				tbl4[i] = rt4[j].addr | (rte_rand() &
					((1ULL << (32 - rt4[j].depth)) - 1));
		}
	} else {
		for (i = 0, j = 0; i < config.nb_lookup_ips;
				i++, j = (j + 1) % config.nb_routes) {
			if ((rte_rand() % 100) < config.rnd_lookup_ips_ratio) {
				set_rnd_ipv6(&tbl6[i * 16], rt6[j].addr, 0);
				config.nb_lookup_ips_rnd++;
			} else {
				set_rnd_ipv6(&tbl6[i * 16], rt6[j].addr,
					rt6[j].depth);
			}
		}
	}
}

static int
_inet_net_pton(int af, char *prefix, void *addr)
{
	const char *dlm = "/";
	char *s, *sp;
	int ret, depth;
	unsigned int max_depth;

	if ((prefix == NULL) || (addr == NULL))
		return -EINVAL;

	s = strtok_r(prefix, dlm, &sp);
	if (s == NULL)
		return -EINVAL;

	ret = inet_pton(af, s, addr);
	if (ret != 1)
		return -errno;

	s = strtok_r(NULL, dlm, &sp);
	max_depth = (af == AF_INET) ? 32 : 128;
	GET_CB_FIELD(s, depth, 0, max_depth, 0);

	return depth;
}

static int
parse_rt_4(FILE *f)
{
	int ret, i, j = 0;
	char *s, *sp, *in[RT_NUM];
	static const char *dlm = " \t\n";
	int string_tok_nb = RTE_DIM(in);
	struct rt_rule_4 *rt;

	rt = (struct rt_rule_4 *)config.rt;

	while (fgets(line, sizeof(line), f) != NULL) {
		s = line;
		for (i = 0; i != string_tok_nb; i++) {
			in[i] = strtok_r(s, dlm, &sp);
			if (in[i] == NULL)
				return -EINVAL;
			s = NULL;
		}

		ret = _inet_net_pton(AF_INET, in[RT_PREFIX], &rt[j].addr);
		if (ret == -1)
			return -errno;

		rt[j].addr = rte_be_to_cpu_32(rt[j].addr);
		rt[j].depth = ret;
		config.nb_routes_per_depth[ret]++;
		GET_CB_FIELD(in[RT_NEXTHOP], rt[j].nh, 0,
				UINT32_MAX, 0);
		j++;
	}
	return 0;
}

static int
parse_rt_6(FILE *f)
{
	int ret, i, j = 0;
	char *s, *sp, *in[RT_NUM];
	static const char *dlm = " \t\n";
	int string_tok_nb = RTE_DIM(in);
	struct rt_rule_6 *rt;

	rt = (struct rt_rule_6 *)config.rt;

	while (fgets(line, sizeof(line), f) != NULL) {
		s = line;
		for (i = 0; i != string_tok_nb; i++) {
			in[i] = strtok_r(s, dlm, &sp);
			if (in[i] == NULL)
				return -EINVAL;
			s = NULL;
		}

		ret = _inet_net_pton(AF_INET6, in[RT_PREFIX], rt[j].addr);
		if (ret < 0)
			return ret;

		rt[j].depth = ret;
		config.nb_routes_per_depth[ret]++;
		GET_CB_FIELD(in[RT_NEXTHOP], rt[j].nh, 0,
				UINT32_MAX, 0);
		j++;
	}

	return 0;
}

static int
parse_lookup(FILE *f, int af)
{
	int ret, i = 0;
	uint8_t *tbl = (uint8_t *)config.lookup_tbl;
	int step = (af == AF_INET) ? 4 : 16;
	char *s;

	while (fgets(line, sizeof(line), f) != NULL) {
		s = strtok(line, " \t\n");
		if (s == NULL)
			return -EINVAL;
		ret = inet_pton(af, s, &tbl[i]);
		if (ret != 1)
			return -EINVAL;
		i += step;
	}
	return 0;
}

static int
dump_lookup(int af)
{
	FILE *f;
	uint32_t *tbl4 = config.lookup_tbl;
	uint8_t *tbl6 = config.lookup_tbl;
	uint32_t i;

	f = fopen(config.lookup_ips_file_s, "w");
	if (f == NULL) {
		printf("Can not open file %s\n", config.lookup_ips_file_s);
		return -1;
	}

	if (af == AF_INET) {
		for (i = 0; i < config.nb_lookup_ips; i++)
			fprintf(f, NIPQUAD_FMT"\n", NIPQUAD(tbl4[i]));
	} else {
		for (i = 0; i < config.nb_lookup_ips; i++)
			fprintf(f, NIPQUAD6_FMT"\n", NIPQUAD6(&tbl6[i * 16]));
	}
	fclose(f);
	return 0;
}

static void
print_config(void)
{
	uint8_t depth_lim;
	char dlm;
	int i;

	depth_lim = ((config.flags & IPV6_FLAG) == IPV6_FLAG) ? 128 : 32;

	fprintf(stdout,
		"Routes total: %u\n"
		"Routes distribution:\n", config.nb_routes);

	for (i = 1; i <= depth_lim; i++) {
		fprintf(stdout,
			"depth /%d:%u", i, config.nb_routes_per_depth[i]);
		if (i % 4 == 0)
			dlm = '\n';
		else
			dlm = '\t';
		fprintf(stdout, "%c", dlm);
	}

	fprintf(stdout,
		"Lookup tuples: %u\n"
		"Configured ratios of random ips for lookup: %u\n"
		"Random lookup ips: %u\n",
		config.nb_lookup_ips, config.rnd_lookup_ips_ratio,
		config.nb_lookup_ips_rnd);
}

static void
print_usage(void)
{
	fprintf(stdout,
		PRINT_USAGE_START
		"[-f <routes file>]\n"
		"[-t <ip's file for lookup>]\n"
		"[-n <number of routes (if -f is not specified)>]\n"
		"[-l <number of ip's for lookup (if -t is not specified)>]\n"
		"[-d <\",\" separated \"depth:n%%\"routes depth distribution"
		"(if -f is not specified)>]\n"
		"[-r <percentage ratio of random ip's to lookup"
		"(if -t is not specified)>]\n"
		"[-c <do comarison with LPM library>]\n"
		"[-6 <do tests with ipv6 (default ipv4)>]\n"
		"[-s <shuffle randomly generated routes>]\n"
		"[-a <check nexthops for all ipv4 address space"
		"(only valid with -c)>]\n"
		"[-b <fib algorithm>]\n\tavailable options for ipv4\n"
		"\t\trib - RIB based FIB\n"
		"\t\tdir - DIR24_8 based FIB\n"
		"\tavailable options for ipv6:\n"
		"\t\trib - RIB based FIB\n"
		"\t\ttrie - TRIE based FIB\n"
		"defaults are: dir for ipv4 and trie for ipv6\n"
		"[-e <entry size (valid only for dir and trie fib types): "
		"1/2/4/8 (default 4)>]\n"
		"[-g <number of tbl8's for dir24_8 or trie FIBs>]\n"
		"[-w <path to the file to dump routing table>]\n"
		"[-u <path to the file to dump ip's for lookup>]\n"
		"[-v <type of loookup function:"
		"\ts1, s2, s3 (3 types of scalar), v (vector) -"
		" for DIR24_8 based FIB\n"
		"\ts, v - for TRIE based ipv6 FIB>]\n",
		config.prgname);
}

static int
check_config(void)
{
	if ((config.routes_file == NULL) && (config.lookup_ips_file != NULL)) {
		printf("-t option only valid with -f option\n");
		return -1;
	}

	if ((config.flags & CMP_ALL_FLAG) && (config.flags & IPV6_FLAG)) {
		printf("-a flag is only valid for ipv4\n");
		return -1;
	}

	if ((config.flags & CMP_ALL_FLAG) &&
			((config.flags & CMP_FLAG) != CMP_FLAG)) {
		printf("-a flag is valid only with -c flag\n");
		return -1;
	}

	if (!((config.ent_sz == 1) || (config.ent_sz == 2) ||
			(config.ent_sz == 4) || (config.ent_sz == 8))) {
		printf("wrong -e option %d, can be 1 or 2 or 4 or 8\n",
			config.ent_sz);
		return -1;
	}

	if ((config.ent_sz == 1) && (config.flags & IPV6_FLAG)) {
		printf("-e 1 is valid only for ipv4\n");
		return -1;
	}
	return 0;
}

static void
parse_opts(int argc, char **argv)
{
	int opt;
	char *endptr;

	while ((opt = getopt(argc, argv, "f:t:n:d:l:r:c6ab:e:g:w:u:sv:")) !=
			-1) {
		switch (opt) {
		case 'f':
			config.routes_file = optarg;
			break;
		case 't':
			config.lookup_ips_file = optarg;
			break;
		case 'w':
			config.routes_file_s = optarg;
			config.flags |= DRY_RUN_FLAG;
			break;
		case 'u':
			config.lookup_ips_file_s = optarg;
			config.flags |= DRY_RUN_FLAG;
			break;
		case 'n':
			errno = 0;
			config.nb_routes = strtoul(optarg, &endptr, 10);
			if ((errno != 0) || (config.nb_routes == 0)) {
				print_usage();
				rte_exit(-EINVAL, "Invalid option -n\n");
			}
			break;
		case 'd':
			distrib_string = optarg;
			break;
		case 'l':
			errno = 0;
			config.nb_lookup_ips = strtoul(optarg, &endptr, 10);
			if ((errno != 0) || (config.nb_lookup_ips == 0)) {
				print_usage();
				rte_exit(-EINVAL, "Invalid option -l\n");
			}
			break;
		case 'r':
			errno = 0;
			config.rnd_lookup_ips_ratio =
				strtoul(optarg, &endptr, 10);
			if ((errno != 0) ||
					(config.rnd_lookup_ips_ratio == 0) ||
					(config.rnd_lookup_ips_ratio >= 100)) {
				print_usage();
				rte_exit(-EINVAL, "Invalid option -r\n");
			}
			break;
		case 's':
			config.flags |= SHUFFLE_FLAG;
			break;
		case 'c':
			config.flags |= CMP_FLAG;
			break;
		case '6':
			config.flags |= IPV6_FLAG;
			break;
		case 'a':
			config.flags |= CMP_ALL_FLAG;
			break;
		case 'b':
			if (strcmp(optarg, "rib") == 0) {
				config.flags &= ~FIB_TYPE_MASK;
				config.flags |= FIB_RIB_TYPE;
			} else if (strcmp(optarg, "dir") == 0) {
				config.flags &= ~FIB_TYPE_MASK;
				config.flags |= FIB_V4_DIR_TYPE;
			} else if (strcmp(optarg, "trie") == 0) {
				config.flags &= ~FIB_TYPE_MASK;
				config.flags |= FIB_V6_TRIE_TYPE;
			} else
				rte_exit(-EINVAL, "Invalid option -b\n");
			break;
		case 'e':
			errno = 0;
			config.ent_sz = strtoul(optarg, &endptr, 10);
			if (errno != 0) {
				print_usage();
				rte_exit(-EINVAL, "Invalid option -e\n");
			}
			break;
		case 'g':
			errno = 0;
			config.tbl8 = strtoul(optarg, &endptr, 10);
			if ((errno != 0) || (config.tbl8 == 0)) {
				print_usage();
				rte_exit(-EINVAL, "Invalid option -g\n");
			}
			break;
		case 'v':
			if ((strcmp(optarg, "s1") == 0) ||
					(strcmp(optarg, "s") == 0)) {
				config.lookup_fn = 1;
				break;
			} else if (strcmp(optarg, "v") == 0) {
				config.lookup_fn = 2;
				break;
			} else if (strcmp(optarg, "s2") == 0) {
				config.lookup_fn = 3;
				break;
			} else if (strcmp(optarg, "s3") == 0) {
				config.lookup_fn = 4;
				break;
			}
			print_usage();
			rte_exit(-EINVAL, "Invalid option -v %s\n", optarg);
		default:
			print_usage();
			rte_exit(-EINVAL, "Invalid options\n");
		}
	}
}

static int
dump_rt_4(struct rt_rule_4 *rt)
{
	FILE *f;
	uint32_t i;

	f = fopen(config.routes_file_s, "w");
	if (f == NULL) {
		printf("Can not open file %s\n", config.routes_file_s);
		return -1;
	}

	for (i = 0; i < config.nb_routes; i++)
		fprintf(f, NIPQUAD_FMT"/%d %"PRIu64"\n", NIPQUAD(rt[i].addr),
			rt[i].depth, rt[i].nh);

	fclose(f);
	return 0;
}

static inline void
print_depth_err(void)
{
	printf("LPM does not support /0 prefix length (default route), use "
		"-d 0:0 option or remove /0 prefix from routes file\n");
}

static int
run_v4(void)
{
	uint64_t start, acc;
	uint64_t def_nh = 0;
	struct rte_fib *fib;
	struct rte_fib_conf conf = {0};
	struct rt_rule_4 *rt;
	uint32_t i, j, k;
	int ret = 0;
	struct rte_lpm	*lpm = NULL;
	struct rte_lpm_config lpm_conf;
	uint32_t *tbl4 = config.lookup_tbl;
	uint64_t fib_nh[BURST_SZ];
	uint32_t lpm_nh[BURST_SZ];

	rt = (struct rt_rule_4 *)config.rt;

	if (config.flags & DRY_RUN_FLAG) {
		if (config.routes_file_s != NULL)
			ret = dump_rt_4(rt);
		if (ret != 0)
			return ret;
		if (config.lookup_ips_file_s != NULL)
			ret = dump_lookup(AF_INET);
		return ret;
	}

	conf.type = get_fib_type();
	conf.default_nh = def_nh;
	conf.max_routes = config.nb_routes * 2;
	if (conf.type == RTE_FIB_DIR24_8) {
		conf.dir24_8.nh_sz = __builtin_ctz(config.ent_sz);
		conf.dir24_8.num_tbl8 = RTE_MIN(config.tbl8,
			get_max_nh(conf.dir24_8.nh_sz));
	}

	fib = rte_fib_create("test", -1, &conf);
	if (fib == NULL) {
		printf("Can not alloc FIB, err %d\n", rte_errno);
		return -rte_errno;
	}

	if (config.lookup_fn != 0) {
		if (config.lookup_fn == 1)
			ret = rte_fib_select_lookup(fib,
				RTE_FIB_LOOKUP_DIR24_8_SCALAR_MACRO);
		else if (config.lookup_fn == 2)
			ret = rte_fib_select_lookup(fib,
				RTE_FIB_LOOKUP_DIR24_8_VECTOR_AVX512);
		else if (config.lookup_fn == 3)
			ret = rte_fib_select_lookup(fib,
				RTE_FIB_LOOKUP_DIR24_8_SCALAR_INLINE);
		else if (config.lookup_fn == 4)
			ret = rte_fib_select_lookup(fib,
				RTE_FIB_LOOKUP_DIR24_8_SCALAR_UNI);
		else
			ret = -EINVAL;
		if (ret != 0) {
			printf("Can not init lookup function\n");
			return ret;
		}
	}

	for (k = config.print_fract, i = 0; k > 0; k--) {
		start = rte_rdtsc_precise();
		for (j = 0; j < (config.nb_routes - i) / k; j++) {
			ret = rte_fib_add(fib, rt[i + j].addr, rt[i + j].depth,
				rt[i + j].nh);
			if (unlikely(ret != 0)) {
				printf("Can not add a route to FIB, err %d\n",
					ret);
				return -ret;
			}
		}
		printf("AVG FIB add %"PRIu64"\n",
			(rte_rdtsc_precise() - start) / j);
		i += j;
	}

	if (config.flags & CMP_FLAG) {
		lpm_conf.max_rules = config.nb_routes * 2;
		lpm_conf.number_tbl8s = RTE_MAX(conf.dir24_8.num_tbl8,
			config.tbl8);

		lpm = rte_lpm_create("test_lpm", -1, &lpm_conf);
		if (lpm == NULL) {
			printf("Can not alloc LPM, err %d\n", rte_errno);
			return -rte_errno;
		}
		for (k = config.print_fract, i = 0; k > 0; k--) {
			start = rte_rdtsc_precise();
			for (j = 0; j < (config.nb_routes - i) / k; j++) {
				ret = rte_lpm_add(lpm, rt[i + j].addr,
					rt[i + j].depth, rt[i + j].nh);
				if (ret != 0) {
					if (rt[i + j].depth == 0)
						print_depth_err();
					printf("Can not add a route to LPM, "
						"err %d\n", ret);
					return -ret;
				}
			}
			printf("AVG LPM add %"PRIu64"\n",
				(rte_rdtsc_precise() - start) / j);
			i += j;
		}
	}

	acc = 0;
	for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
		start = rte_rdtsc_precise();
		ret = rte_fib_lookup_bulk(fib, tbl4 + i, fib_nh, BURST_SZ);
		acc += rte_rdtsc_precise() - start;
		if (ret != 0) {
			printf("FIB lookup fails, err %d\n", ret);
			return -ret;
		}
	}
	printf("AVG FIB lookup %.1f\n", (double)acc / (double)i);

	if (config.flags & CMP_FLAG) {
		acc = 0;
		for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
			start = rte_rdtsc_precise();
			ret = rte_lpm_lookup_bulk(lpm, tbl4 + i, lpm_nh,
				BURST_SZ);
			acc += rte_rdtsc_precise() - start;
			if (ret != 0) {
				printf("LPM lookup fails, err %d\n", ret);
				return -ret;
			}
		}
		printf("AVG LPM lookup %.1f\n", (double)acc / (double)i);

		for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
			rte_fib_lookup_bulk(fib, tbl4 + i, fib_nh, BURST_SZ);
			rte_lpm_lookup_bulk(lpm, tbl4 + i, lpm_nh, BURST_SZ);
			for (j = 0; j < BURST_SZ; j++) {
				struct rte_lpm_tbl_entry *tbl;
				tbl = (struct rte_lpm_tbl_entry *)&lpm_nh[j];
				if ((fib_nh[j] != tbl->next_hop) &&
						!((tbl->valid == 0) &&
						(fib_nh[j] == def_nh))) {
					printf("FAIL\n");
					return -1;
				}
			}
		}
		printf("FIB and LPM lookup returns same values\n");
	}

	for (k = config.print_fract, i = 0; k > 0; k--) {
		start = rte_rdtsc_precise();
		for (j = 0; j < (config.nb_routes - i) / k; j++)
			rte_fib_delete(fib, rt[i + j].addr, rt[i + j].depth);

		printf("AVG FIB delete %"PRIu64"\n",
			(rte_rdtsc_precise() - start) / j);
		i += j;
	}

	if (config.flags & CMP_FLAG) {
		for (k = config.print_fract, i = 0; k > 0; k--) {
			start = rte_rdtsc_precise();
			for (j = 0; j < (config.nb_routes - i) / k; j++)
				rte_lpm_delete(lpm, rt[i + j].addr,
					rt[i + j].depth);

			printf("AVG LPM delete %"PRIu64"\n",
				(rte_rdtsc_precise() - start) / j);
			i += j;
		}
	}

	return 0;
}

static int
dump_rt_6(struct rt_rule_6 *rt)
{
	FILE *f;
	uint32_t i;

	f = fopen(config.routes_file_s, "w");
	if (f == NULL) {
		printf("Can not open file %s\n", config.routes_file_s);
		return -1;
	}

	for (i = 0; i < config.nb_routes; i++) {
		fprintf(f, NIPQUAD6_FMT"/%d %"PRIu64"\n", NIPQUAD6(rt[i].addr),
			rt[i].depth, rt[i].nh);

	}
	fclose(f);
	return 0;
}

static int
run_v6(void)
{
	uint64_t start, acc;
	uint64_t def_nh = 0;
	struct rte_fib6 *fib;
	struct rte_fib6_conf conf = {0};
	struct rt_rule_6 *rt;
	uint32_t i, j, k;
	int ret = 0;
	struct rte_lpm6	*lpm = NULL;
	struct rte_lpm6_config lpm_conf;
	uint8_t *tbl6;
	uint64_t fib_nh[BURST_SZ];
	int32_t lpm_nh[BURST_SZ];

	rt = (struct rt_rule_6 *)config.rt;
	tbl6 = config.lookup_tbl;

	if (config.flags & DRY_RUN_FLAG) {
		if (config.routes_file_s != NULL)
			ret =  dump_rt_6(rt);
		if (ret != 0)
			return ret;
		if (config.lookup_ips_file_s != NULL)
			ret = dump_lookup(AF_INET6);
		return ret;
	}

	conf.type = get_fib_type();
	conf.default_nh = def_nh;
	conf.max_routes = config.nb_routes * 2;
	if (conf.type == RTE_FIB6_TRIE) {
		conf.trie.nh_sz = __builtin_ctz(config.ent_sz);
		conf.trie.num_tbl8 = RTE_MIN(config.tbl8,
			get_max_nh(conf.trie.nh_sz));
	}

	fib = rte_fib6_create("test", -1, &conf);
	if (fib == NULL) {
		printf("Can not alloc FIB, err %d\n", rte_errno);
		return -rte_errno;
	}

	if (config.lookup_fn != 0) {
		if (config.lookup_fn == 1)
			ret = rte_fib6_select_lookup(fib,
				RTE_FIB6_LOOKUP_TRIE_SCALAR);
		else if (config.lookup_fn == 2)
			ret = rte_fib6_select_lookup(fib,
				RTE_FIB6_LOOKUP_TRIE_VECTOR_AVX512);
		else
			ret = -EINVAL;
		if (ret != 0) {
			printf("Can not init lookup function\n");
			return ret;
		}
	}

	for (k = config.print_fract, i = 0; k > 0; k--) {
		start = rte_rdtsc_precise();
		for (j = 0; j < (config.nb_routes - i) / k; j++) {
			ret = rte_fib6_add(fib, rt[i + j].addr,
				rt[i + j].depth, rt[i + j].nh);
			if (unlikely(ret != 0)) {
				printf("Can not add a route to FIB, err %d\n",
					ret);
				return -ret;
			}
		}
		printf("AVG FIB add %"PRIu64"\n",
			(rte_rdtsc_precise() - start) / j);
		i += j;
	}

	if (config.flags & CMP_FLAG) {
		lpm_conf.max_rules = config.nb_routes * 2;
		lpm_conf.number_tbl8s = RTE_MAX(conf.trie.num_tbl8,
			config.tbl8);

		lpm = rte_lpm6_create("test_lpm", -1, &lpm_conf);
		if (lpm == NULL) {
			printf("Can not alloc LPM, err %d\n", rte_errno);
			return -rte_errno;
		}
		for (k = config.print_fract, i = 0; k > 0; k--) {
			start = rte_rdtsc_precise();
			for (j = 0; j < (config.nb_routes - i) / k; j++) {
				ret = rte_lpm6_add(lpm, rt[i + j].addr,
					rt[i + j].depth, rt[i + j].nh);
				if (ret != 0) {
					if (rt[i + j].depth == 0)
						print_depth_err();
					printf("Can not add a route to LPM, "
						"err %d\n", ret);
					return -ret;
				}
			}
			printf("AVG LPM add %"PRIu64"\n",
				(rte_rdtsc_precise() - start) / j);
			i += j;
		}
	}

	acc = 0;
	for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
		start = rte_rdtsc_precise();
		ret = rte_fib6_lookup_bulk(fib, (uint8_t (*)[16])(tbl6 + i*16),
			fib_nh, BURST_SZ);
		acc += rte_rdtsc_precise() - start;
		if (ret != 0) {
			printf("FIB lookup fails, err %d\n", ret);
			return -ret;
		}
	}
	printf("AVG FIB lookup %.1f\n", (double)acc / (double)i);

	if (config.flags & CMP_FLAG) {
		acc = 0;
		for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
			start = rte_rdtsc_precise();
			ret = rte_lpm6_lookup_bulk_func(lpm,
				(uint8_t (*)[16])(tbl6 + i*16),
				lpm_nh, BURST_SZ);
			acc += rte_rdtsc_precise() - start;
			if (ret != 0) {
				printf("LPM lookup fails, err %d\n", ret);
				return -ret;
			}
		}
		printf("AVG LPM lookup %.1f\n", (double)acc / (double)i);

		for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
			rte_fib6_lookup_bulk(fib,
				(uint8_t (*)[16])(tbl6 + i*16),
				fib_nh, BURST_SZ);
			rte_lpm6_lookup_bulk_func(lpm,
				(uint8_t (*)[16])(tbl6 + i*16),
				lpm_nh, BURST_SZ);
			for (j = 0; j < BURST_SZ; j++) {
				if ((fib_nh[j] != (uint32_t)lpm_nh[j]) &&
						!((lpm_nh[j] == -1) &&
						(fib_nh[j] == def_nh))) {
					printf("FAIL\n");
					return -1;
				}
			}
		}
		printf("FIB and LPM lookup returns same values\n");
	}

	for (k = config.print_fract, i = 0; k > 0; k--) {
		start = rte_rdtsc_precise();
		for (j = 0; j < (config.nb_routes - i) / k; j++)
			rte_fib6_delete(fib, rt[i + j].addr, rt[i + j].depth);

		printf("AVG FIB delete %"PRIu64"\n",
			(rte_rdtsc_precise() - start) / j);
		i += j;
	}

	if (config.flags & CMP_FLAG) {
		for (k = config.print_fract, i = 0; k > 0; k--) {
			start = rte_rdtsc_precise();
			for (j = 0; j < (config.nb_routes - i) / k; j++)
				rte_lpm6_delete(lpm, rt[i + j].addr,
					rt[i + j].depth);

			printf("AVG LPM delete %"PRIu64"\n",
				(rte_rdtsc_precise() - start) / j);
			i += j;
		}
	}
	return 0;
}

int
main(int argc, char **argv)
{
	int ret, af, rt_ent_sz, lookup_ent_sz;
	FILE *fr = NULL;
	FILE *fl = NULL;
	uint8_t depth_lim;

	ret = rte_eal_init(argc, argv);
	if (ret < 0)
		rte_panic("Cannot init EAL\n");

	argc -= ret;
	argv += ret;

	config.prgname = argv[0];

	parse_opts(argc, argv);

	ret = check_config();
	if (ret != 0)
		rte_exit(-ret, "Bad configuration\n");

	af = ((config.flags & IPV6_FLAG) == 0) ? AF_INET : AF_INET6;
	depth_lim = (af == AF_INET) ? 32 : 128;
	rt_ent_sz = (af == AF_INET) ? sizeof(struct rt_rule_4) :
		sizeof(struct rt_rule_6);
	lookup_ent_sz = (af == AF_INET) ? 4 : 16;

	/* Count number of rules in file*/
	if (config.routes_file != NULL) {
		fr = fopen(config.routes_file, "r");
		if (fr == NULL)
			rte_exit(-errno, "Can not open file with routes %s\n",
				config.routes_file);

		config.nb_routes = 0;
		while (fgets(line, sizeof(line), fr) != NULL)
			config.nb_routes++;
		rewind(fr);
	}

	/* Count number of ip's in file*/
	if (config.lookup_ips_file != NULL) {
		fl = fopen(config.lookup_ips_file, "r");
		if (fl == NULL)
			rte_exit(-errno, "Can not open file with ip's %s\n",
				config.lookup_ips_file);

		config.nb_lookup_ips = 0;
		while (fgets(line, sizeof(line), fl) != NULL)
			config.nb_lookup_ips++;
		rewind(fl);
	}

	/* Alloc routes table*/
	config.rt  = rte_malloc(NULL, rt_ent_sz * config.nb_routes, 0);
	if (config.rt == NULL)
		rte_exit(-ENOMEM, "Can not alloc rt\n");

	/* Alloc table with ip's for lookup*/
	config.lookup_tbl  = rte_malloc(NULL, lookup_ent_sz *
		config.nb_lookup_ips, 0);
	if (config.lookup_tbl == NULL)
		rte_exit(-ENOMEM, "Can not alloc lookup table\n");

	/* Fill routes table */
	if (fr == NULL) {
		if (distrib_string != NULL)
			ret = parse_distrib(depth_lim, config.nb_routes);
		else {
			uint8_t rpd[129] = {0};
			uint32_t nrpd[129] = {0};
			ret = complete_distrib(depth_lim, config.nb_routes,
				rpd, nrpd);
		}
		if (ret != 0)
			rte_exit(-ret,
				"Bad routes distribution configuration\n");
		if (af == AF_INET) {
			gen_random_rt_4(config.rt,
				__builtin_ctz(config.ent_sz));
			if (config.flags & SHUFFLE_FLAG)
				shuffle_rt_4(config.rt, config.nb_routes);
		} else {
			gen_random_rt_6(config.rt,
				__builtin_ctz(config.ent_sz));
			if (config.flags & SHUFFLE_FLAG)
				shuffle_rt_6(config.rt, config.nb_routes);
		}
	} else {
		if (af == AF_INET)
			ret = parse_rt_4(fr);
		else
			ret = parse_rt_6(fr);

		if (ret != 0) {
			rte_exit(-ret, "failed to parse routes file %s\n",
				config.routes_file);
		}
	}

	/* Fill lookup table with ip's*/
	if (fl == NULL)
		gen_rnd_lookup_tbl(af);
	else {
		ret = parse_lookup(fl, af);
		if (ret != 0)
			rte_exit(-ret, "failed to parse lookup file\n");
	}

	print_config();

	if (af == AF_INET)
		ret = run_v4();
	else
		ret = run_v6();

	return ret;
}