aboutsummaryrefslogtreecommitdiff
path: root/example/ipsec_offload/odp_ipsec_offload.c
blob: 63c8f12646e436933982b2398978a7877078d7c2 (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
/* Copyright (c) 2017-2018, Linaro Limited
 * Copyright (C) 2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * @example odp_ipsec_offload.c  ODP basic packet IO cross connect with IPsec
 * test application
 */

#define _DEFAULT_SOURCE
/* enable strtok */
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <inttypes.h>

#include <example_debug.h>

#include <odp_api.h>
#include <odp/helper/linux.h>
#include <odp/helper/eth.h>
#include <odp/helper/ip.h>
#include <odp/helper/icmp.h>
#include <odp/helper/udp.h>
#include <odp/helper/ipsec.h>

#include <stdbool.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>

#include <odp_ipsec_offload_misc.h>
#include <odp_ipsec_offload_sa_db.h>
#include <odp_ipsec_offload_sp_db.h>
#include <odp_ipsec_offload_fwd_db.h>
#include <odp_ipsec_offload_cache.h>

/* maximum number of worker threads */
#define MAX_WORKERS     (ODP_THREAD_COUNT_MAX - 1)

/**
 * Parsed command line application arguments
 */
typedef struct {
	unsigned int cpu_count;
	int flows;
	int if_count;		/**< Number of interfaces to be used */
	char **if_names;	/**< Array of pointers to interface names */
	char *if_str;		/**< Storage for interface names */
	int queue_type;		/**< Queue synchronization type*/
} appl_args_t;

/**
 * Grouping of both parsed CL args and thread specific args - alloc together
 */
typedef struct {
	/** Application (parsed) arguments */
	appl_args_t appl;
} args_t;

/* helper funcs */
static void parse_args(int argc, char *argv[], appl_args_t *appl_args);
static void print_info(char *progname, appl_args_t *appl_args);
static void usage(char *progname);

/** Global pointer to args */
static args_t *args;

/**
 * Buffer pool for packet IO
 */
#define SHM_PKT_POOL_BUF_COUNT 1024
#define SHM_PKT_POOL_BUF_SIZE  4096
#define SHM_PKT_POOL_SIZE      (SHM_PKT_POOL_BUF_COUNT * SHM_PKT_POOL_BUF_SIZE)

static odp_pool_t pkt_pool = ODP_POOL_INVALID;

/** Synchronize threads before packet processing begins */
static odp_barrier_t sync_barrier;

/**
 * Packet processing result codes
 */
typedef enum {
	PKT_CONTINUE,    /**< No events posted, keep processing */
	PKT_POSTED,      /**< Event posted, stop processing */
	PKT_DROP,        /**< Reason to drop detected, stop processing */
	PKT_DONE         /**< Finished with packet, stop processing */
} pkt_disposition_e;

#define MAX_COMPL_QUEUES		32
#define GET_THR_QUEUE_ID(x)		((odp_thread_id() - 1) % (x))

/** Atomic queue IPSEC completion events */
static odp_queue_t completionq[MAX_COMPL_QUEUES];

static int num_compl_queues;
static int num_workers;

/**
 * Calculate hash value on given 2-tuple i.e. sip, dip
 *
 * @param ip_src	Source IP Address
 * @param ip_dst	Destination IP Address
 *
 * @return Resultant hash value
 */
static inline uint64_t calculate_flow_hash(uint32_t ip_src, uint32_t ip_dst)
{
	uint64_t hash = 0;

	ip_dst += JHASH_GOLDEN_RATIO;
	BJ3_MIX(ip_src, ip_dst, hash);
	return hash;
}

/**
 * IPsec pre argument processing initialization
 */
static
void ipsec_init_pre(void)
{
	/* Initialize our data bases */
	init_sp_db();
	init_sa_db();
	init_tun_db();
	init_ipsec_cache();
}

/**
 * IPsec post argument processing initialization
 *
 * Resolve SP DB with SA DB and create corresponding IPsec cache entries
 */
static
void ipsec_init_post(void)
{
	sp_db_entry_t *entry;
	int queue_id = 0;

	/* Attempt to find appropriate SA for each SP */
	for (entry = sp_db->list; NULL != entry; entry = entry->next) {
		sa_db_entry_t *cipher_sa = NULL;
		sa_db_entry_t *auth_sa = NULL;
		tun_db_entry_t *tun = NULL;

		queue_id %= num_workers;
		if (num_compl_queues < num_workers)
			num_compl_queues++;
		queue_id++;
		if (entry->esp) {
			cipher_sa = find_sa_db_entry(&entry->src_subnet,
						     &entry->dst_subnet, 1);
			tun = find_tun_db_entry(cipher_sa->src_ip,
						cipher_sa->dst_ip);
		}
		if (entry->ah) {
			auth_sa = find_sa_db_entry(&entry->src_subnet,
						   &entry->dst_subnet, 0);
			tun = find_tun_db_entry(auth_sa->src_ip,
						auth_sa->dst_ip);
		}

		if (cipher_sa && auth_sa) {
			if (create_ipsec_cache_entry(cipher_sa,
						     auth_sa,
						     tun,
						     entry->input,
						     completionq[queue_id - 1])
			    ) {
				EXAMPLE_ABORT("Error: IPSec cache entry failed.\n");
			}
		} else {
			printf(" WARNING: SA not found for SP\n");
			dump_sp_db_entry(entry);
		}
	}
}

/**
 * Initialize interface
 *
 * Initialize ODP pktio and queues, query MAC address and update
 * forwarding database.
 *
 * @param intf		Interface name string
 * @param queue_type	Type of queue to configure.
 */
static void initialize_intf(char *intf, int queue_type)
{
	odp_pktio_t pktio;
	odp_pktout_queue_t pktout;
	int ret;
	uint8_t src_mac[ODPH_ETHADDR_LEN];
	odp_pktio_param_t pktio_param;
	odp_pktio_capability_t capa;
	odp_pktin_queue_param_t pktin_param;

	odp_pktio_param_init(&pktio_param);

	pktio_param.in_mode = ODP_PKTIN_MODE_SCHED;

	/*
	 * Open a packet IO instance for thread and get default output queue
	 */
	pktio = odp_pktio_open(intf, pkt_pool, &pktio_param);
	if (ODP_PKTIO_INVALID == pktio)
		EXAMPLE_ABORT("Error: pktio create failed for %s\n", intf);

	odp_pktin_queue_param_init(&pktin_param);

	ret = odp_pktio_capability(pktio, &capa);
	if (ret != 0)
		EXAMPLE_ABORT("Error: Unable to get pktio capability %s\n",
			      intf);

	pktin_param.queue_param.type = ODP_QUEUE_TYPE_SCHED;
	pktin_param.queue_param.sched.sync = queue_type;
	pktin_param.queue_param.sched.prio = ODP_SCHED_PRIO_DEFAULT;
	pktin_param.num_queues = capa.max_input_queues;

	if (pktin_param.num_queues > 1)
		pktin_param.hash_enable = 1;

	if (odp_pktin_queue_config(pktio, &pktin_param))
		EXAMPLE_ABORT("Error: pktin config failed for %s\n", intf);

	if (odp_pktout_queue_config(pktio, NULL))
		EXAMPLE_ABORT("Error: pktout config failed for %s\n", intf);

	if (odp_pktout_queue(pktio, &pktout, 1) != 1)
		EXAMPLE_ABORT("Error: failed to get pktout queue for %s\n",
			      intf);

	ret = odp_pktio_start(pktio);
	if (ret)
		EXAMPLE_ABORT("Error: unable to start %s\n", intf);

	/* Read the source MAC address for this interface */
	ret = odp_pktio_mac_addr(pktio, src_mac, sizeof(src_mac));
	if (ret < 0) {
		EXAMPLE_ABORT("Error: failed during MAC address get for %s\n",
			      intf);
	}

	printf("Created pktio:%02" PRIu64 "\n", odp_pktio_to_u64(pktio));

	/* Resolve any routes using this interface for output */
	resolve_fwd_db(intf, pktout, src_mac);
}

/**
 * Packet Processing - Input verification
 *
 * @param pkt  Packet to inspect
 *
 * @return PKT_CONTINUE if good, supported packet else PKT_DROP
 */
static pkt_disposition_e do_input_verify(odp_packet_t pkt)
{
	if (odp_unlikely(odp_packet_has_error(pkt))) {
		odp_packet_free(pkt);
		return PKT_DROP;
	}

	if (!odp_packet_has_eth(pkt)) {
		odp_packet_free(pkt);
		return PKT_DROP;
	}

	if (!odp_packet_has_ipv4(pkt)) {
		odp_packet_free(pkt);
		return PKT_DROP;
	}

	return PKT_CONTINUE;
}

/**
 * Packet Processing - Route lookup in forwarding database
 *
 * @param pkt  Packet to route
 *
 * @return PKT_CONTINUE if route found else PKT_DROP
 */
static
pkt_disposition_e do_route_fwd_db(odp_packet_t pkt)
{
	odph_ipv4hdr_t *ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL);
	fwd_db_entry_t *fwd_entry;
	ipsec_cache_entry_t *ipsec_entry;
	odp_ipsec_out_param_t params;
	uint32_t	sip, dip;
	uint64_t	hash;
	flow_entry_t	*flow;

	if (ip->ttl > 1) {
		ip->ttl -= 1;
		if (ip->chksum >= odp_cpu_to_be_16(0xffff - 0x100))
			ip->chksum += odp_cpu_to_be_16(0x100) + 1;
		else
			ip->chksum += odp_cpu_to_be_16(0x100);
	} else {
		odp_packet_free(pkt);
		return PKT_DROP;
	}

	sip = odp_be_to_cpu_32(ip->src_addr);
	dip = odp_be_to_cpu_32(ip->dst_addr);

	hash = calculate_flow_hash(sip, dip);

	flow = route_flow_lookup_in_bucket(sip, dip,
					   &flow_table[hash &
					   (bucket_count - 1)]);
	if (!flow) {
		/*Check into Routing table*/
		fwd_entry = find_fwd_db_entry(dip);
		if (!fwd_entry) {
			EXAMPLE_DBG("No flow match found. Packet is dropped.\n");
			odp_packet_free(pkt);
			return PKT_DROP;
		}

		/*Entry found. Updated in Flow table first.*/
		flow = calloc(1, sizeof(flow_entry_t));
		if (!flow) {
			EXAMPLE_ABORT("Failure to allocate memory");
			return PKT_DROP;
		}
		flow->l3_src = sip;
		flow->l3_dst = dip;
		flow->out_port.pktout = fwd_entry->pktout;
		memcpy(flow->out_port.addr.addr,
		       fwd_entry->src_mac,
		       ODPH_ETHADDR_LEN);
		memcpy(flow->out_port.next_hop_addr.addr,
		       fwd_entry->dst_mac,
		       ODPH_ETHADDR_LEN);
		ipsec_entry = find_ipsec_cache_entry_out(sip, dip);
		if (ipsec_entry)
			flow->out_port.sa = ipsec_entry->sa;
		else
			flow->out_port.sa = ODP_IPSEC_SA_INVALID;
		flow->next = NULL;
		/*Insert new flow into flow cache table*/
		route_flow_insert_in_bucket(flow, &flow_table[hash &
					    (bucket_count - 1)]);
	}

	odp_packet_user_ptr_set(pkt, &flow->out_port);
	if (flow->out_port.sa == ODP_IPSEC_SA_INVALID)
		return PKT_CONTINUE;

	/* Initialize parameters block */
	params.sa = &flow->out_port.sa;
	params.opt = NULL;
	params.num_sa = 1;
	params.num_opt = 1;

	/* Issue ipsec request */
	if (odp_unlikely(odp_ipsec_out_enq(&pkt, 1, &params) < 0)) {
		EXAMPLE_DBG("Unable to out enqueue\n");
		odp_packet_free(pkt);
		return PKT_DROP;
	}
	return PKT_POSTED;
}

/**
 * Packet Processing - Input IPsec packet classification
 *
 * Verify the received packet has IPsec headers,
 * if so issue ipsec request else skip.
 *
 * @param pkt   Packet to classify
 *
 * @return PKT_CONTINUE if done else PKT_POSTED
 */
static
pkt_disposition_e do_ipsec_in_classify(odp_packet_t pkt)
{
	odp_ipsec_in_param_t params;

	if (!odp_packet_has_ipsec(pkt))
		return PKT_CONTINUE;

	/* Initialize parameters block */
	params.num_sa = 0;
	params.sa = NULL;

	/* Issue ipsec request */
	if (odp_unlikely(odp_ipsec_in_enq(&pkt, 1, &params) < 0)) {
		EXAMPLE_DBG("Unable to in enqueue\n");
		odp_packet_free(pkt);
		return PKT_DROP;
	}
	return PKT_POSTED;
}

/**
 * Packet IO worker thread
 *
 * Loop calling odp_schedule to obtain packet from the two sources,
 * and continue processing the packet.
 *
 *  - Input interfaces (i.e. new work)
 *  - Per packet ipsec API completion queue
 *
 * @param arg  Required by "odph_linux_pthread_create", unused
 *
 * @return NULL (should never return)
 */
static
int pktio_thread(void *arg EXAMPLE_UNUSED)
{
	int thr = odp_thread_id();
	odp_packet_t pkt;
	odp_pktout_queue_t out_queue;
	ipsec_out_entry_t	*out_port;
	odp_event_t ev = ODP_EVENT_INVALID;

	printf("Pktio thread [%02i] starts\n", thr);
	odp_barrier_wait(&sync_barrier);

	/* Loop packets */
	for (;;) {
		pkt_disposition_e rc;
		odp_event_subtype_t subtype;

		ev = odp_schedule(NULL, ODP_SCHED_WAIT);
		/* Use schedule to get event from any input queue */
		/* Determine new work versus IPsec result */
		if (ODP_EVENT_PACKET == odp_event_types(ev, &subtype)) {
			pkt = odp_packet_from_event(ev);

			if (ODP_EVENT_PACKET_IPSEC == subtype) {
				odp_ipsec_packet_result_t res;

				if (odp_unlikely(odp_ipsec_result(&res,
								  pkt) < 0)) {
					EXAMPLE_DBG("Error Event\n");
					odp_event_free((odp_event_t)ev);
					continue;
				}

				if (odp_unlikely(res.status.error.all)) {
					odp_packet_free(pkt);
					continue;
				}
			} else {
				rc = do_input_verify(pkt);
				if (odp_unlikely(rc))
					continue;

				rc = do_ipsec_in_classify(pkt);
				if (rc)
					continue;
			}

			rc = do_route_fwd_db(pkt);
			if (rc)
				continue;

			out_port = odp_packet_user_ptr(pkt);
			out_queue = out_port->pktout;

			if (odp_unlikely(odp_pktout_send(out_queue,
							 &pkt, 1) < 0))
				odp_packet_free(pkt);

		} else {
			EXAMPLE_DBG("Invalid Event\n");
			odp_event_free(ev);
			continue;
		}
	}

	/* unreachable */
	return 0;
}

/**
 * ODP ipsec proto example main function
 */
int
main(int argc, char *argv[])
{
	odph_odpthread_t thread_tbl[MAX_WORKERS];
	int i;
	odp_shm_t shm;
	odp_cpumask_t cpumask;
	char cpumaskstr[ODP_CPUMASK_STR_SIZE];
	odp_pool_param_t params;
	odp_queue_param_t qparam;
	odp_instance_t instance;
	odph_odpthread_params_t thr_params;
	odp_ipsec_config_t config;
	odp_ipsec_capability_t capa;

	/*Validate if user has passed only help option*/
	if (argc == 2) {
		if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
			usage(argv[0]);
			exit(EXIT_SUCCESS);
		}
	}

	/* Initialize ODP before calling anything else */
	if (odp_init_global(&instance, NULL, NULL))
		EXAMPLE_ABORT("Error: ODP global init failed.\n");
	/* Initialize this thread */
	if (odp_init_local(instance, ODP_THREAD_CONTROL))
		EXAMPLE_ABORT("Error: ODP local init failed.\n");
	/* Reserve memory for arguments from shared memory */
	shm = odp_shm_reserve("shm_args", sizeof(args_t),
			      ODP_CACHE_LINE_SIZE, 0);
	args = odp_shm_addr(shm);

	if (NULL == args)
		EXAMPLE_ABORT("Error: shared mem alloc failed.\n");
	memset(args, 0, sizeof(*args));

	/* Must init our databases before parsing args */
	ipsec_init_pre();
	init_fwd_db();

	/* Parse and store the application arguments */
	parse_args(argc, argv, &args->appl);

	/*Initialize route table for user given parameter*/
	init_routing_table();

	/* Print both system and application information */
	print_info(NO_PATH(argv[0]), &args->appl);

	if (odp_ipsec_capability(&capa))
		EXAMPLE_ABORT("Error: Capability not configured.\n");

	odp_ipsec_config_init(&config);

	if (capa.op_mode_async && (capa.op_mode_async >= capa.op_mode_sync)) {
		config.inbound_mode = ODP_IPSEC_OP_MODE_ASYNC;
		config.outbound_mode = ODP_IPSEC_OP_MODE_ASYNC;
	} else {
		EXAMPLE_ABORT("Error: Sync mode not supported.\n");
	}

	if (odp_ipsec_config(&config))
		EXAMPLE_ABORT("Error: IPSec not configured.\n");

	num_workers = MAX_WORKERS;
	if (args->appl.cpu_count && args->appl.cpu_count < MAX_WORKERS)
		num_workers = args->appl.cpu_count;

	/*
	 * By default CPU #0 runs Linux kernel background tasks.
	 * Start mapping thread from CPU #1
	 */
	num_workers = odp_cpumask_default_worker(&cpumask, num_workers);
	(void)odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr));

	/*
	 * Create completion queues
	 */
	odp_queue_param_init(&qparam);
	qparam.type       = ODP_QUEUE_TYPE_SCHED;
	qparam.sched.prio  = ODP_SCHED_PRIO_HIGHEST;
	qparam.sched.sync  = args->appl.queue_type;
	qparam.sched.group = ODP_SCHED_GROUP_ALL;

	for (i = 0; i < num_workers; i++) {
		completionq[i] = odp_queue_create("completion", &qparam);
		if (ODP_QUEUE_INVALID == completionq[i])
			EXAMPLE_ABORT("Error: completion queue creation failed\n");
	}
	printf("num worker threads: %i\n", num_workers);
	printf("first CPU:          %i\n", odp_cpumask_first(&cpumask));
	printf("cpu mask:           %s\n", cpumaskstr);

	/* Create a barrier to synchronize thread startup */
	odp_barrier_init(&sync_barrier, num_workers);

	/* Create packet buffer pool */
	odp_pool_param_init(&params);
	params.pkt.seg_len = SHM_PKT_POOL_BUF_SIZE;
	params.pkt.len     = SHM_PKT_POOL_BUF_SIZE;
	params.pkt.num     = SHM_PKT_POOL_BUF_COUNT;
	params.type        = ODP_POOL_PACKET;

	pkt_pool = odp_pool_create("packet_pool", &params);

	if (ODP_POOL_INVALID == pkt_pool)
		EXAMPLE_ABORT("Error: packet pool create failed.\n");

	ipsec_init_post();

	/* Initialize interfaces (which resolves FWD DB entries */
	for (i = 0; i < args->appl.if_count; i++)
		initialize_intf(args->appl.if_names[i], args->appl.queue_type);

	printf("  Configured queues SYNC type: [%s]\n",
	       (args->appl.queue_type == 0) ?
	       "PARALLEL" :
	       (args->appl.queue_type == 1) ?
	       "ATOMIC" : "ORDERED");
	memset(&thr_params, 0, sizeof(thr_params));
	thr_params.start    = pktio_thread;
	thr_params.arg      = NULL;
	thr_params.thr_type = ODP_THREAD_WORKER;
	thr_params.instance = instance;

	/* Create and initialize worker threads */
	odph_odpthreads_create(thread_tbl, &cpumask,
			       &thr_params);
	odph_odpthreads_join(thread_tbl);

	free(args->appl.if_names);
	free(args->appl.if_str);
	printf("Exit\n\n");
	return 0;
}

/**
 * Parse and store the command line arguments
 *
 * @param argc       argument count
 * @param argv[]     argument vector
 * @param appl_args  Store application arguments here
 */
static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
{
	int opt;
	int long_index;
	char *token;
	size_t len;
	int rc = 0;
	int i;

	static struct option longopts[] = {
		{"count", required_argument, NULL, 'c'},
		{"interface", required_argument, NULL, 'i'},	/* return 'i' */
		{"route", required_argument, NULL, 'r'},	/* return 'r' */
		{"policy", required_argument, NULL, 'p'},	/* return 'p' */
		{"ah", required_argument, NULL, 'a'},		/* return 'a' */
		{"esp", required_argument, NULL, 'e'},		/* return 'e' */
		{"tunnel", required_argument, NULL, 't'},       /* return 't' */
		{"flows", no_argument, NULL, 'f'},		/* return 'f' */
		{"queue type", required_argument, NULL, 'q'},	/* return 'q' */
		{"help", no_argument, NULL, 'h'},		/* return 'h' */
		{NULL, 0, NULL, 0}
	};

	appl_args->cpu_count = 1; /* use one worker by default */
	appl_args->flows = 1;
	appl_args->queue_type = ODP_SCHED_SYNC_ATOMIC;

	while (!rc) {
		opt = getopt_long(argc, argv, "+c:i:h:r:p:a:e:t:s:q:f:",
				  longopts, &long_index);
		if (opt < 0)
			break;	/* No more options */
		switch (opt) {
		case 'f':
			appl_args->flows = atoi(optarg);
			if (appl_args->flows > 256) {
				printf("Maximum acceptable value for -f is 256\n");
				rc = -1;
			}
			if (optind != 3) {
				printf("-f must be the 1st argument of the command\n");
				rc = -1;
			}
			EXAMPLE_DBG("Bucket count = %d\n", bucket_count);
			break;
		case 'c':
			appl_args->cpu_count = atoi(optarg);
			break;
		case 'i':
			/* parse packet-io interface names */
			len = strlen(optarg);
			if (0 == len) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			len += 1;	/* add room for '\0' */

			appl_args->if_str = malloc(len);
			if (appl_args->if_str == NULL) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}

			/* count the number of tokens separated by ',' */
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token;
			     token = strtok(NULL, ","), i++)
				;
			appl_args->if_count = i;
			if (!appl_args->if_count) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			/* Allocate storage for the if names */
			appl_args->if_names =
				calloc(appl_args->if_count, sizeof(char *));
			if (!appl_args->if_names)
				EXAMPLE_ABORT("Memory allocation failure\n");
			/* Store the if names (reset names string) */
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token; token = strtok(NULL, ","), i++) {
				appl_args->if_names[i] = token;
			}
			break;
		case 'r':
			rc = create_fwd_db_entry(optarg, appl_args->if_names,
						 appl_args->if_count,
						 appl_args->flows);
			break;
		case 'p':
			rc = create_sp_db_entry(optarg, appl_args->flows);
			break;
		case 'a':
			rc = create_sa_db_entry(optarg, FALSE,
						appl_args->flows);
			break;
		case 'e':
			rc = create_sa_db_entry(optarg, TRUE, appl_args->flows);
			break;
		case 't':
			rc = create_tun_db_entry(optarg, appl_args->flows);
			break;
		case 'q':
			i = atoi(optarg);
			if (i > ODP_SCHED_SYNC_ORDERED ||
			    i < ODP_SCHED_SYNC_PARALLEL) {
				printf("Invalid queue type: setting default to atomic");
				break;
			}
			appl_args->queue_type = i;
			break;
		case 'h':
			usage(argv[0]);
			exit(EXIT_SUCCESS);
			break;
		default:
			break;
		}
	}

	if (rc) {
		printf("ERROR: failed parsing -%c option\n", opt);
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	if (0 == appl_args->if_count) {
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	optind = 1;		/* reset 'extern optind' from the getopt lib */
}

/**
 * Print system and application info
 */
static void print_info(char *progname, appl_args_t *appl_args)
{
	int i;

	printf("\n"
	       "ODP system info\n"
	       "---------------\n"
	       "ODP API version: %s\n"
	       "CPU model:       %s\n"
	       "CPU freq (hz):   %" PRIu64 "\n"
	       "Cache line size: %i\n"
	       "CPU count:       %i\n"
	       "\n",
	       odp_version_api_str(), odp_cpu_model_str(), odp_cpu_hz_max(),
	       odp_sys_cache_line_size(), odp_cpu_count());
	printf("Running ODP application: \"%s\"\n"
	       "------------------------\n"
	       "IF-count:        %i\n"
	       "Using IFs:      ",
	       progname, appl_args->if_count);
	for (i = 0; i < appl_args->if_count; ++i)
		printf(" %s", appl_args->if_names[i]);
	printf("\n");
	dump_fwd_db();
	dump_sp_db();
	dump_sa_db();
	dump_tun_db();
	printf("\n\n");
	fflush(NULL);
}

/**
 * Prinf usage information
 */
static void usage(char *progname)
{
	printf("\n"
	       "Usage: %s OPTIONS\n"
	       "  E.g. %s -i eth1,eth2,eth3 -m 0\n"
	       "\n"
	       "OpenDataPlane example application.\n"
	       "\n"
	       "Mandatory OPTIONS:\n"
	       " -i, --interface Eth interfaces (comma-separated, no spaces)\n"
	       "Routing / IPSec OPTIONS:\n"
	       " -r, --route SubNet:Intf:NextHopMAC\n"
	       " -p, --policy SrcSubNet:DstSubNet:(in|out):(ah|esp|both)\n"
	       " -e, --esp SrcIP:DstIP:(3des|null):SPI:Key192\n"
	       " -a, --ah SrcIP:DstIP:(md5|null):SPI:Key128\n"
	       " -t, --tun SrcIP:DstIP:TunSrcIP:TunDstIP\n"
	       "\n"
	       "  Where: NextHopMAC is raw hex/dot notation, i.e. 03.BA.44.9A.CE.02\n"
	       "         IP is decimal/dot notation, i.e. 192.168.1.1\n"
	       "         SubNet is decimal/dot/slash notation, i.e 192.168.0.0/16\n"
	       "         SPI is raw hex, 32 bits\n"
	       "         KeyXXX is raw hex, XXX bits long\n"
	       "\n"
	       "  Examples:\n"
	       "     -r 192.168.222.0/24:p8p1:08.00.27.F5.8B.DB\n"
	       "     -p 192.168.111.0/24:192.168.222.0/24:out:esp\n"
	       "     -e 192.168.111.2:192.168.222.2:3des:201:656c8523255ccc23a66c1917aa0cf30991fce83532a4b224\n"
	       "     -a 192.168.111.2:192.168.222.2:md5:201:a731649644c5dee92cbd9c2e7e188ee6\n"
	       "     -t 192.168.111.2:192.168.222.2:192.168.150.1:192.168.150.2\n"
	       "\n"
	       "Optional OPTIONS\n"
	       "  -f, --flows <number> routes count.\n"
	       "  -c, --count <number> CPU count, 0=all available, default=1\n"
	       "  -q		specify the queue type\n"
	       "		0:	ODP_SCHED_SYNC_PARALLEL\n"
	       "		1:	ODP_SCHED_SYNC_ATOMIC\n"
	       "		2:	ODP_SCHED_SYNC_ORDERED\n"
	       "			default is ODP_SCHED_SYNC_ATOMIC\n"
	       "  -h, --help           Display help and exit.\n"
	       "\n", NO_PATH(progname), NO_PATH(progname)
		);
}