20 #include <odp/helper/odph_api.h>
25 #define MAX_WORKERS (ODP_THREAD_COUNT_MAX - 1)
30 #define SHM_PKT_POOL_SIZE (512*2048)
35 #define SHM_PKT_POOL_BUF_SIZE 1856
40 #define MAX_PKT_BURST 16
45 #define APPL_MODE_PKT_BURST 0
50 #define APPL_MODE_PKT_QUEUE 1
55 #define APPL_MODE_PKT_SCHED 2
61 #define PRINT_APPL_MODE(x) printf("%s(%i)\n", #x, (x))
64 #define NO_PATH(file_name) (strrchr((file_name), '/') ? \
65 strrchr((file_name), '/') + 1 : (file_name))
70 unsigned int cpu_count;
95 thread_args_t thread[MAX_WORKERS];
104 static int drop_err_pkts(
odp_packet_t pkt_tbl[],
unsigned len);
105 static void swap_pkt_addrs(
odp_packet_t pkt_tbl[],
unsigned len);
106 static void parse_args(
int argc,
char *argv[], appl_args_t *appl_args);
107 static void print_info(
char *progname, appl_args_t *appl_args);
108 static void usage(
char *progname);
130 case APPL_MODE_PKT_BURST:
133 case APPL_MODE_PKT_QUEUE:
136 case APPL_MODE_PKT_SCHED:
140 ODPH_ABORT(
"invalid mode %d\n", mode);
146 ODPH_ABORT(
"Error: pktio create failed for %s\n", dev);
150 if (mode == APPL_MODE_PKT_SCHED)
154 ODPH_ABORT(
"Error: pktin config failed for %s\n", dev);
157 ODPH_ABORT(
"Error: pktout config failed for %s\n", dev);
161 ODPH_ABORT(
"Error: unable to start %s\n", dev);
163 printf(
" created pktio:%02" PRIu64
164 ", dev:%s, queue mode (ATOMIC queues)\n"
165 " \tdefault pktio%02" PRIu64
"\n",
177 static int pktio_queue_thread(
void *arg)
181 thread_args_t *thr_args;
186 unsigned long pkt_cnt = 0;
187 unsigned long err_cnt = 0;
195 ODPH_ERR(
" [%02i] Error: lookup of pktio %s failed\n",
196 thr, thr_args->pktio_dev);
200 printf(
" [%02i] looked up pktio:%02" PRIu64
201 ", queue mode (ATOMIC queues)\n"
202 " default pktio%02" PRIu64
"\n",
207 if ((thr_args->mode == APPL_MODE_PKT_QUEUE) &&
209 ODPH_ERR(
" [%02i] Error: no input queue for %s\n",
210 thr, thr_args->pktio_dev);
232 ODPH_ERR(
"Drop frame - err_cnt:%lu\n", ++err_cnt);
239 ODPH_ERR(
" [%02i] Error: no pktout queue\n", thr);
244 swap_pkt_addrs(&pkt, 1);
248 ODPH_ERR(
" [%i] Packet send failed.\n", thr);
255 printf(
" [%02i] pkt_cnt:%lu\n", thr, pkt_cnt);
268 static int pktio_ifburst_thread(
void *arg)
274 thread_args_t *thr_args;
277 unsigned long pkt_cnt = 0;
278 unsigned long err_cnt = 0;
279 unsigned long tmp = 0;
286 ODPH_ERR(
" [%02i] Error: lookup of pktio %s failed\n",
287 thr, thr_args->pktio_dev);
291 printf(
" [%02i] looked up pktio:%02" PRIu64
", burst mode\n",
295 ODPH_ERR(
" [%02i] Error: no pktin queue\n", thr);
300 ODPH_ERR(
" [%02i] Error: no pktout queue\n", thr);
309 pkts_ok = drop_err_pkts(pkt_tbl, pkts);
314 swap_pkt_addrs(pkt_tbl, pkts_ok);
317 sent = sent > 0 ? sent : 0;
319 err_cnt += pkts_ok - sent;
322 while (++sent < pkts_ok);
327 ODPH_ERR(
"Dropped frames:%u - err_cnt:%lu\n",
328 pkts - pkts_ok, ++err_cnt);
333 ((pkt_cnt == 0) && ((tmp-1) < MAX_PKT_BURST)))) {
335 printf(
" [%02i] pkt_cnt:%lu\n", thr, pkt_cnt);
348 int main(
int argc,
char *argv[])
350 odph_helper_options_t helper_options;
351 odph_thread_t thread_tbl[MAX_WORKERS];
352 odph_thread_common_param_t thr_common;
353 odph_thread_param_t thr_param[MAX_WORKERS];
365 argc = odph_parse_options(argc, argv);
366 if (odph_options(&helper_options)) {
367 ODPH_ERR(
"Error: reading ODP helper options failed.\n");
372 init_param.
mem_model = helper_options.mem_model;
376 ODPH_ERR(
"Error: ODP global init failed.\n");
382 ODPH_ERR(
"Error: ODP local init failed.\n");
388 ODP_CACHE_LINE_SIZE, 0);
390 ODPH_ERR(
"Error: shared mem reserve failed.\n");
396 ODPH_ERR(
"Error: shared mem alloc failed.\n");
400 memset(args, 0,
sizeof(args_t));
405 parse_args(argc, argv, &args->appl);
408 print_info(NO_PATH(argv[0]), &args->appl);
410 num_workers = MAX_WORKERS;
411 if (args->appl.cpu_count && args->appl.cpu_count < MAX_WORKERS)
412 num_workers = args->appl.cpu_count;
418 printf(
"num worker threads: %i\n", num_workers);
420 printf(
"cpu mask: %s\n", cpumaskstr);
425 params.
pkt.
len = SHM_PKT_POOL_BUF_SIZE;
426 params.
pkt.
num = SHM_PKT_POOL_SIZE/SHM_PKT_POOL_BUF_SIZE;
432 ODPH_ERR(
"Error: packet pool create failed.\n");
441 for (i = 0; i < args->appl.if_count; ++i)
442 create_pktio(args->appl.if_names[i], pool, args->appl.mode);
445 odph_thread_common_param_init(&thr_common);
446 thr_common.instance = instance;
447 thr_common.cpumask = &cpumask;
449 for (i = 0; i < num_workers; ++i) {
450 int (*thr_run_func)(
void *);
453 if_idx = i % args->appl.if_count;
455 args->thread[i].pktio_dev = args->appl.if_names[if_idx];
456 args->thread[i].mode = args->appl.mode;
458 if (args->appl.mode == APPL_MODE_PKT_BURST)
459 thr_run_func = pktio_ifburst_thread;
461 thr_run_func = pktio_queue_thread;
463 odph_thread_param_init(&thr_param[i]);
464 thr_param[i].start = thr_run_func;
465 thr_param[i].arg = &args->thread[i];
469 memset(thread_tbl, 0,
sizeof(thread_tbl));
470 odph_thread_create(thread_tbl, &thr_common, thr_param, num_workers);
472 if (args->appl.time > 0.0) {
475 for (i = 0; i < args->appl.if_count; ++i) {
487 odph_thread_join(thread_tbl, num_workers);
489 for (i = 0; i < args->appl.if_count; ++i)
492 free(args->appl.if_names);
493 free(args->appl.if_str);
498 ODPH_ERR(
"Error: shm free global data\n");
517 static int drop_err_pkts(
odp_packet_t pkt_tbl[],
unsigned len)
520 unsigned pkt_cnt = len;
523 for (i = 0, j = 0; i < len; ++i) {
530 #pragma GCC diagnostic push
531 #pragma GCC diagnostic ignored "-Warray-bounds"
533 #pragma GCC diagnostic pop
547 static void swap_pkt_addrs(
odp_packet_t pkt_tbl[],
unsigned len)
551 odph_ethaddr_t tmp_addr;
556 for (i = 0; i < len; ++i) {
567 ip = (odph_ipv4hdr_t *)
570 ip_tmp_addr = ip->src_addr;
571 ip->src_addr = ip->dst_addr;
572 ip->dst_addr = ip_tmp_addr;
585 static void parse_args(
int argc,
char *argv[], appl_args_t *appl_args)
591 static const struct option longopts[] = {
592 {
"count", required_argument, NULL,
'c'},
593 {
"time", required_argument, NULL,
't'},
594 {
"interface", required_argument, NULL,
'i'},
595 {
"mode", required_argument, NULL,
'm'},
596 {
"help", no_argument, NULL,
'h'},
600 static const char *shortopts =
"+c:i:m:t:h";
602 appl_args->cpu_count = 1;
603 appl_args->mode = APPL_MODE_PKT_SCHED;
607 opt = getopt_long(argc, argv, shortopts, longopts, NULL);
614 appl_args->cpu_count = atoi(optarg);
617 appl_args->time = atof(optarg);
621 len = strlen(optarg);
628 appl_args->if_str = malloc(len);
629 if (appl_args->if_str == NULL) {
635 strcpy(appl_args->if_str, optarg);
636 for (token = strtok(appl_args->if_str,
","), i = 0;
638 token = strtok(NULL,
","), i++)
641 appl_args->if_count = i;
643 if (appl_args->if_count == 0) {
649 appl_args->if_names =
650 calloc(appl_args->if_count,
sizeof(
char *));
653 strcpy(appl_args->if_str, optarg);
654 for (token = strtok(appl_args->if_str,
","), i = 0;
655 token != NULL; token = strtok(NULL,
","), i++) {
656 appl_args->if_names[i] = token;
664 appl_args->mode = APPL_MODE_PKT_BURST;
667 appl_args->mode = APPL_MODE_PKT_QUEUE;
670 appl_args->mode = APPL_MODE_PKT_SCHED;
687 if (appl_args->if_count == 0 || appl_args->mode == -1) {
698 static void print_info(
char *progname, appl_args_t *appl_args)
704 printf(
"Running ODP appl: \"%s\"\n"
705 "-----------------\n"
708 progname, appl_args->if_count);
709 for (i = 0; i < appl_args->if_count; ++i)
710 printf(
" %s", appl_args->if_names[i]);
713 switch (appl_args->mode) {
714 case APPL_MODE_PKT_BURST:
715 PRINT_APPL_MODE(APPL_MODE_PKT_BURST);
717 case APPL_MODE_PKT_QUEUE:
718 PRINT_APPL_MODE(APPL_MODE_PKT_QUEUE);
720 case APPL_MODE_PKT_SCHED:
721 PRINT_APPL_MODE(APPL_MODE_PKT_SCHED);
731 static void usage(
char *progname)
734 "Usage: %s OPTIONS\n"
735 " E.g. %s -i eth1,eth2,eth3 -m 0\n"
737 "OpenDataPlane example application.\n"
739 "Mandatory OPTIONS:\n"
740 " -i, --interface Eth interfaces (comma-separated, no spaces)\n"
743 " -c, --count <number> CPU count, 0=all available, default=1\n"
744 " -t, --time <seconds> Number of seconds to run (e.g. 0.1).\n"
745 " -m, --mode 0: Receive and send directly (no queues)\n"
746 " 1: Receive and send via queues.\n"
747 " 2: Receive via scheduler, send via queues.\n"
748 " -h, --help Display help and exit.\n"
749 "\n", NO_PATH(progname), NO_PATH(progname)
void odp_atomic_init_u32(odp_atomic_u32_t *atom, uint32_t val)
Initialize atomic uint32 variable.
uint32_t odp_atomic_load_u32(odp_atomic_u32_t *atom)
Load value of atomic uint32 variable.
void odp_atomic_store_u32(odp_atomic_u32_t *atom, uint32_t val)
Store value to atomic uint32 variable.
#define odp_unlikely(x)
Branch unlikely taken.
uint32_t odp_u32be_t
unsigned 32bit big endian
int odp_cpumask_default_worker(odp_cpumask_t *mask, int num)
Default CPU mask for worker threads.
int odp_cpumask_first(const odp_cpumask_t *mask)
Find first set CPU in mask.
int32_t odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int32_t size)
Format a string from CPU mask.
#define ODP_CPUMASK_STR_SIZE
The maximum number of characters needed to record any CPU mask as a string (output of odp_cpumask_to_...
#define ODP_EVENT_INVALID
Invalid event.
void odp_init_param_init(odp_init_t *param)
Initialize the odp_init_t to default values for all fields.
int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type)
Thread local ODP initialization.
int odp_init_global(odp_instance_t *instance, const odp_init_t *params, const odp_platform_init_t *platform_params)
Global ODP initialization.
int odp_term_local(void)
Thread local ODP termination.
int odp_term_global(odp_instance_t instance)
Global ODP termination.
uint64_t odp_instance_t
ODP instance ID.
void odp_pktin_queue_param_init(odp_pktin_queue_param_t *param)
Initialize packet input queue parameters.
void odp_pktio_param_init(odp_pktio_param_t *param)
Initialize pktio params.
int odp_pktio_close(odp_pktio_t pktio)
Close a packet IO interface.
odp_pktio_t odp_pktio_lookup(const char *name)
Return a packet IO handle for an already open device.
int odp_pktout_queue(odp_pktio_t pktio, odp_pktout_queue_t queues[], int num)
Direct packet output queues.
int odp_pktin_event_queue(odp_pktio_t pktio, odp_queue_t queues[], int num)
Event queues for packet input.
odp_pktio_t odp_pktio_open(const char *name, odp_pool_t pool, const odp_pktio_param_t *param)
Open a packet IO interface.
int odp_pktio_start(odp_pktio_t pktio)
Start packet receive and transmit.
#define ODP_PKTIO_INVALID
Invalid packet IO handle.
int odp_pktin_queue(odp_pktio_t pktio, odp_pktin_queue_t queues[], int num)
Direct packet input queues.
int odp_pktio_stop(odp_pktio_t pktio)
Stop packet receive and transmit.
int odp_pktin_recv(odp_pktin_queue_t queue, odp_packet_t packets[], int num)
Receive packets directly from an interface input queue.
int odp_pktout_send(odp_pktout_queue_t queue, const odp_packet_t packets[], int num)
Send packets directly to an interface output queue.
uint64_t odp_pktio_to_u64(odp_pktio_t pktio)
Get printable value for an odp_pktio_t.
int odp_pktin_queue_config(odp_pktio_t pktio, const odp_pktin_queue_param_t *param)
Configure packet input queues.
int odp_pktout_queue_config(odp_pktio_t pktio, const odp_pktout_queue_param_t *param)
Configure packet output queues.
@ ODP_PKTIN_MODE_DIRECT
Direct packet input from the interface.
@ ODP_PKTIN_MODE_QUEUE
Packet input through plain event queues.
@ ODP_PKTIN_MODE_SCHED
Packet input through scheduler and scheduled event queues.
int odp_packet_has_ipv4(odp_packet_t pkt)
Check for IPv4.
int odp_packet_has_eth(odp_packet_t pkt)
Check for Ethernet header.
int odp_packet_has_error(odp_packet_t pkt)
Check for all parse errors in packet.
odp_packet_t odp_packet_from_event(odp_event_t ev)
Get packet handle from event.
void odp_packet_free(odp_packet_t pkt)
Free packet.
void * odp_packet_l2_ptr(odp_packet_t pkt, uint32_t *len)
Layer 2 start pointer.
void * odp_packet_l3_ptr(odp_packet_t pkt, uint32_t *len)
Layer 3 start pointer.
int odp_packet_is_valid(odp_packet_t pkt)
Check that packet is valid.
odp_pktio_t odp_packet_input(odp_packet_t pkt)
Packet input interface.
odp_pool_t odp_pool_create(const char *name, const odp_pool_param_t *param)
Create a pool.
void odp_pool_param_init(odp_pool_param_t *param)
Initialize pool params.
int odp_pool_destroy(odp_pool_t pool)
Destroy a pool previously created by odp_pool_create()
void odp_pool_print(odp_pool_t pool)
Print pool info.
#define ODP_POOL_INVALID
Invalid pool.
@ ODP_POOL_PACKET
Packet pool.
#define ODP_QUEUE_INVALID
Invalid queue.
odp_event_t odp_queue_deq(odp_queue_t queue)
Dequeue an event from a queue.
#define ODP_SCHED_SYNC_ATOMIC
Atomic queue synchronization.
int odp_schedule_config(const odp_schedule_config_t *config)
Global schedule configuration.
uint64_t odp_schedule_wait_time(uint64_t ns)
Schedule wait time.
odp_event_t odp_schedule(odp_queue_t *from, uint64_t wait)
Schedule an event.
int odp_shm_free(odp_shm_t shm)
Free a contiguous block of shared memory.
#define ODP_SHM_INVALID
Invalid shared memory block.
void * odp_shm_addr(odp_shm_t shm)
Shared memory block address.
odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, uint32_t flags)
Reserve a contiguous block of shared memory.
void odp_sys_info_print(void)
Print system info.
int odp_thread_id(void)
Get thread identifier.
@ ODP_THREAD_WORKER
Worker thread.
@ ODP_THREAD_CONTROL
Control thread.
#define ODP_TIME_SEC_IN_NS
A second in nanoseconds.
void odp_time_wait_ns(uint64_t ns)
Wait the specified number of nanoseconds.
#define ODP_TIME_MSEC_IN_NS
A millisecond in nanoseconds.
Global initialization parameters.
odp_mem_model_t mem_model
Application memory model.
Packet input queue parameters.
odp_queue_param_t queue_param
Queue parameters.
odp_pktin_mode_t in_mode
Packet input mode.
uint32_t num
Number of buffers in the pool.
odp_pool_type_t type
Pool type.
uint32_t len
Minimum length of 'num' packets.
uint32_t seg_len
Minimum number of packet data bytes that can be stored in the first segment of a newly allocated pack...
struct odp_pool_param_t::@126 pkt
Parameters for packet pools.
odp_schedule_param_t sched
Scheduler parameters.
odp_schedule_sync_t sync
Synchronization method.