API Reference Manual 1.51.0
Loading...
Searching...
No Matches
odp_packet_gen.c
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2020-2026 Nokia
3 */
4
13/* enable usleep */
14#ifndef _GNU_SOURCE
15#define _GNU_SOURCE
16#endif
17
18#include <stdio.h>
19#include <string.h>
20#include <stdint.h>
21#include <inttypes.h>
22#include <signal.h>
23#include <stdlib.h>
24#include <getopt.h>
25#include <unistd.h>
26#include <errno.h>
27#include <pthread.h>
28#include <time.h>
29
30#include <odp_api.h>
31#include <odp/helper/odph_api.h>
32#include <pktio_common.h>
33
34#if ODP_THREAD_COUNT_MAX > 33
35/* One control thread, even number of workers */
36#define MAX_THREADS 33
37#else
38#define MAX_THREADS ODP_THREAD_COUNT_MAX
39#endif
40
41#define MAX_WORKERS (MAX_THREADS - 1)
42
43/* At least one control and one worker thread */
44ODP_STATIC_ASSERT(MAX_WORKERS >= 1, "Too few threads");
45
46/* Maximum number of packet IO interfaces */
47#define MAX_PKTIOS 16
48/* Maximum number of packets to be allocated for
49 * one transmit round: bursts * burst_size * bins */
50#define MAX_ALLOC_PACKETS (64 * 1024)
51/* Maximum number of packet length bins */
52#define MAX_BINS 1024
53#define MAX_PKTIO_NAME 255
54#define RX_THREAD 1
55#define TX_THREAD 2
56#define MAX_VLANS 4
57#define ETH_TYPE_QINQ 0x88a8
58/* Number of random 16-bit words used to generate random length packets */
59#define RAND_16BIT_WORDS MAX_BINS
60/* Max retries to generate random data */
61#define MAX_RAND_RETRIES 1000
62#define MAX_HDR_NAME_LEN 32
63#define MAX_HDR_FIELDS 16
64#define MAX_HDR_VALUE_SZ 8
65#define TOKEN_DELIMITER ","
66#define FIELD_DELIMITER ":"
67
68/* Use don't free */
69#define TX_MODE_DF 0
70/* Use static references */
71#define TX_MODE_REF 1
72/* Use packet copy */
73#define TX_MODE_COPY 2
74
75/* Minimum number of packets to receive in CI test */
76#define MIN_RX_PACKETS_CI 160
77
78/* Identifier for payload-timestamped packets */
79#define TS_MAGIC 0xff88ee99ddaaccbb
80
81#define S_(x) #x
82#define S(x) S_(x)
83
84enum {
85 L4_PROTO_UDP = 0,
86 L4_PROTO_TCP,
87 L4_PROTO_NONE
88};
89
90ODP_STATIC_ASSERT(MAX_PKTIOS <= UINT8_MAX, "Interface index must fit into uint8_t\n");
91
92typedef struct {
93 char name[MAX_HDR_NAME_LEN + 1];
94 uint64_t value;
95 int64_t diff;
96 uint32_t len;
97} hdr_field_t;
98
99typedef struct {
100 hdr_field_t fields[MAX_HDR_FIELDS];
101 uint32_t tot_len;
102 uint16_t eth_type;
103} hdr_t;
104
105typedef struct test_options_t {
106 uint64_t gap_nsec;
107 uint64_t quit;
108 uint64_t update_msec;
109 uint32_t num_rx;
110 uint32_t num_tx;
111 uint32_t num_cpu;
112 uint32_t num_pktio;
113 uint32_t num_pkt;
114 uint32_t pkt_len;
115 uint32_t pool_align;
116 uint8_t use_rand_pkt_len;
117 uint8_t direct_rx;
118 uint32_t rand_pkt_len_min;
119 uint32_t rand_pkt_len_max;
120 uint32_t rand_pkt_len_bins;
121 uint32_t hdr_len;
122 uint32_t burst_size;
123 uint32_t bursts;
124 uint16_t eth_type;
125 uint32_t num_vlan;
126 uint32_t l3_len;
127 uint32_t ipv4_src;
128 uint32_t ipv4_dst;
129 uint16_t src_port;
130 uint16_t dst_port;
131 uint32_t wait_sec;
132 uint32_t wait_start_sec;
133 uint32_t mtu;
134 uint32_t num_custom_l3;
135 struct {
137 uint32_t payload_offset;
138 uint32_t max_payload_len;
139 odp_bool_t enabled;
140 } lso;
141 uint8_t l4_proto;
142 int tx_mode;
143 odp_bool_t promisc_mode;
144 odp_bool_t calc_latency;
145 odp_bool_t calc_cs;
146 odp_bool_t cs_offload;
147 odp_bool_t fill_pl;
148 odp_bool_t verbose;
149
150 struct vlan_hdr {
151 uint16_t tpid;
152 uint16_t tci;
153 } vlan[MAX_VLANS];
154
155 struct {
156 uint32_t src_port;
157 uint32_t dst_port;
158 } c_mode;
159
160 char pktio_name[MAX_PKTIOS][MAX_PKTIO_NAME + 1];
161 char ipv4_src_s[24];
162 char ipv4_dst_s[24];
163
164 hdr_t custom_l3;
165
166} test_options_t;
167
168typedef struct thread_arg_t {
169 void *global;
170 int tx_thr;
171
172 /* pktout queue per pktio interface (per thread) */
173 odp_pktout_queue_t pktout[MAX_PKTIOS];
174
175 /* In direct_rx mode, pktin queue per pktio interface (per thread) */
176 odp_pktin_queue_t pktin[MAX_PKTIOS];
177
178 /* LSO profile per pktio interface */
179 odp_lso_profile_t lso_profile[MAX_PKTIOS];
180
181 /* Pre-built packets for TX thread */
182 odp_packet_t packet[MAX_PKTIOS][MAX_ALLOC_PACKETS];
183
184} thread_arg_t;
185
186typedef struct ODP_ALIGNED_CACHE thread_stat_t {
187 uint64_t time_nsec;
188 uint64_t rx_timeouts;
189 uint64_t rx_packets;
190 uint64_t rx_bytes;
191 uint64_t rx_lat_nsec;
192 uint64_t rx_lat_min_nsec;
193 uint64_t rx_lat_max_nsec;
194 uint64_t rx_lat_packets;
195
196 uint64_t tx_timeouts;
197 uint64_t tx_packets;
198 uint64_t tx_bytes;
199 uint64_t tx_drops;
200
201 int thread_type;
202
203 struct {
204 uint64_t rx_packets;
205 uint64_t tx_packets;
206
207 } pktio[MAX_PKTIOS];
208
209} thread_stat_t;
210
211typedef struct test_global_t {
212 test_options_t test_options;
213 odp_atomic_u32_t exit_test;
214 odp_barrier_t barrier;
215 odp_cpumask_t cpumask;
216 odp_pool_t pool;
217 odp_spinlock_t verbose_lock;
218 uint64_t drained;
219 odph_thread_t thread_tbl[MAX_WORKERS];
220 thread_stat_t stat[MAX_THREADS];
221 thread_arg_t *thread_arg;
222 odp_shm_t thread_arg_shm;
223
224 struct {
225 odph_ethaddr_t eth_src;
226 odph_ethaddr_t eth_dst;
227 odp_pktio_t pktio;
228 odp_lso_profile_t lso_profile;
229 odp_pktout_queue_t pktout[MAX_WORKERS];
230 odp_pktin_queue_t pktin[MAX_WORKERS];
231 int started;
232
233 } pktio[MAX_PKTIOS];
234
235 /* Interface lookup table. Table index is pktio_index of the API. */
236 uint8_t if_from_pktio_idx[ODP_PKTIO_MAX_INDEX + 1];
237
238 uint32_t num_tx_pkt;
239 uint32_t num_bins;
240 uint32_t len_bin[MAX_BINS];
241
242 /* Per thread random data */
243 uint16_t rand_data[MAX_THREADS][RAND_16BIT_WORDS];
244
245} test_global_t;
246
247typedef struct ODP_PACKED {
248 uint64_t magic;
249 odp_time_t tx_ts;
250} ts_data_t;
251
252typedef struct {
253 uint64_t nsec;
254 uint64_t min;
255 uint64_t max;
256 uint64_t packets;
257} rx_lat_data_t;
258
259typedef int (*send_fn_t)(odp_pktout_queue_t pktout, odp_packet_t pkt[], uint32_t num,
260 int tx_mode, uint64_t *drop_bytes, const odp_packet_lso_opt_t *lso_opt);
261
262static volatile sig_atomic_t sigint_received;
263
264static void print_usage(void)
265{
266 printf("\n"
267 "ODP packet generator\n"
268 "\n"
269 "Usage: odp_packet_gen [options]\n"
270 "\n"
271 " Mandatory:\n"
272 " -i, --interface <name> Packet IO interfaces. Comma-separated list of\n"
273 " interface names (no spaces) e.g. eth0,eth1.\n"
274 " At least one interface is required.\n"
275 "\n");
276 printf(" Optional:\n"
277 " -e, --eth_dst <mac> Destination MAC address. Comma-separated list of\n"
278 " addresses (no spaces), one address per packet IO\n"
279 " interface e.g. AA:BB:CC:DD:EE:FF,11:22:33:44:55:66\n"
280 " Default per interface: 02:00:00:A0:B0:CX, where X = 0,1,...\n"
281 " -v, --vlan <tpid:tci> VLAN configuration. Comma-separated list of VLAN TPID:TCI\n"
282 " values in hexadecimal, starting from the outer most VLAN.\n"
283 " For example:\n"
284 " VLAN 200 (decimal): 0x8100:c8\n"
285 " Double tagged VLANs 1 and 2: 0x88a8:1,0x8100:2\n"
286 " -r, --num_rx Number of receive threads. Default: 1\n"
287 " -t, --num_tx Number of transmit threads. Default: 1\n"
288 " -T, --lso <options> Transmit packets with Large Send Offload (LSO). Specify\n"
289 " LSO options as comma-separated list (no spaces) in\n"
290 " format: protocol,payload_offset(B),max_payload_len(B). E.g.:\n"
291 " 0,34,1500\n"
292 " In case of ODP_LSO_PROTO_CUSTOM the list is extended by\n"
293 " up to %d custom modification options. For ODP_LSO_ADD_*\n"
294 " operations the format is:\n"
295 " mod_op:offset(B):size(B)\n"
296 " For ODP_LSO_WRITE_BITS the format is:\n"
297 " mod_op:offset(B):size(B):fm:fv:mm:mv:lm:lv\n"
298 " where size must be 1 and fm/fv, mm/mv, lm/lv are the\n"
299 " mask/value byte pairs to write to the first, middle\n"
300 " and last segments respectively. Custom modification\n"
301 " options are separated by commas. E.g.:\n"
302 " 2,22,1500,0:19:1\n"
303 " 2,22,1500,3:19:1:0xff:0x80:0xff:0x00:0xff:0x40\n"
304 " Supported protocols:\n"
305 " 0: ODP_LSO_PROTO_IPV4\n"
306 " 1: ODP_LSO_PROTO_TCP_IPV4\n"
307 " 2: ODP_LSO_PROTO_CUSTOM\n"
308 " Custom modification options:\n"
309 " 0: ODP_LSO_ADD_SEGMENT_NUM\n"
310 " 1: ODP_LSO_ADD_PAYLOAD_LEN\n"
311 " 2: ODP_LSO_ADD_PAYLOAD_OFFSET\n"
312 " 3: ODP_LSO_WRITE_BITS\n"
313 " Depending on the implementation, all listed LSO options\n"
314 " may not be always supported.\n"
315 " -n, --num_pkt Number of packets in the pool. Default: 1000\n"
316 " -l, --len Packet length. Default: 512\n"
317 " -I, --align <align> Packet data alignment in bytes. Must be a power of\n"
318 " two. Default: 0 (implementation default alignment)\n"
319 " -L, --len_range <min,max,bins>\n"
320 " Random packet length. Specify the minimum and maximum\n"
321 " packet lengths and the number of bins. To reduce pool size\n"
322 " requirement the length range can be divided into even sized\n"
323 " bins (max %u). Min and max size packets are always used and included\n"
324 " into the number of bins (bins >= 2). Bin value of 0 means\n"
325 " that each packet length is used. Comma-separated (no spaces).\n"
326 " Overrides standard packet length option.\n"
327 " -D, --direct_rx Direct input mode (default: 0)\n"
328 " 0: Use scheduler for packet input\n"
329 " 1: Poll packet input in direct mode\n",
330 ODP_LSO_MAX_CUSTOM, MAX_BINS);
331 printf(" -m, --tx_mode Transmit mode (default 1):\n"
332 " 0: Re-send packets with don't free option\n"
333 " 1: Send static packet references. Some features may\n"
334 " not be available with references.\n"
335 " 2: Send copies of packets\n"
336 " -M, --mtu <len> Interface MTU in bytes.\n"
337 " -b, --burst_size Transmit burst size. Default: 8\n"
338 " -x, --bursts Number of bursts per one transmit round. Default: 1\n"
339 " -g, --gap Gap between transmit rounds in nsec. Default: 1000000\n"
340 " Transmit packet rate per interface:\n"
341 " num_tx * burst_size * bursts * (10^9 / gap)\n"
342 " -s, --ipv4_src IPv4 source address. Default: 192.168.0.1\n"
343 " -d, --ipv4_dst IPv4 destination address. Default: 192.168.0.2\n"
344 " -o, --src_port UDP/TCP source port. Default: 10000\n"
345 " -p, --dst_port UDP/TCP destination port. Default: 20000\n"
346 " -N, --proto L4 protocol. Default: 0\n"
347 " 0: UDP\n"
348 " 1: TCP\n"
349 " 2: none\n"
350 " -P, --promisc_mode Enable promiscuous mode.\n"
351 " -a, --latency Calculate latency. Cannot be used with packet\n"
352 " references (see \"--tx_mode\").\n"
353 " -c, --c_mode <counts> Counter mode for incrementing UDP/TCP port numbers.\n"
354 " Specify the number of port numbers used starting from\n"
355 " src_port/dst_port. Comma-separated (no spaces) list of\n"
356 " count values: <src_port count>,<dst_port count>\n"
357 " Default value: 0,0\n"
358 " -C, --no_udp_checksum Do not calculate UDP SW checksum. Instead, set it to\n"
359 " zero in every packet, this may be overridden by\n"
360 " '--checksum_offload' option.\n"
361 " -X, --checksum_offload Enable L4 checksum offloads: checksums are checked\n"
362 " at input and inserted at output.\n"
363 " -A, --no_payload_fill Do not fill payload. By default, payload is filled\n"
364 " with a pattern until the end of first packet\n"
365 " segment.\n"
366 " -V, --verbose Print received packets on the receive side. Affects only\n"
367 " rx threads.\n"
368 " -q, --quit Quit after this many transmit rounds.\n"
369 " Default: 0 (don't quit)\n"
370 " -u, --update_stat <msec> Update and print statistics every <msec> milliseconds.\n"
371 " 0: Don't print statistics periodically (default)\n"
372 " -h, --help This help\n"
373 " -w, --wait <sec> Wait up to <sec> seconds for network links to be up.\n"
374 " Default: 0 (don't check link status)\n");
375 printf(" -U, --custom_l3 <definition>\n"
376 " Define a custom L3 header for packets. This\n"
377 " overrides the default IP header and any related\n"
378 " options. Elements should be comma-separated (no\n"
379 " spaces). Definition should begin with an EtherType\n"
380 " value, followed by field definitions. Each field\n"
381 " should be in the format\n"
382 " <name>:<length(B)>:<value>:<diff>, i.e.\n"
383 " colon-separated (no spaces). Name/length/value\n"
384 " elements are self-explanatory, the 'diff' element\n"
385 " defines a value that's added (subtracted if\n"
386 " negative) to the 'value' element in successive\n"
387 " packets. Fields are used in the order they are\n"
388 " defined in the string. E.g.:\n\n"
389 " 0x900,a:4:0xaaaaaaaa:1,b:1:0xff:-2\n\n"
390 " would result in a header of EtherType 0x900 with\n"
391 " fields 'a' and 'b' of values and lengths\n"
392 " '0xaaaaaaaa' (4 bytes) and '0xff' (1 byte)\n"
393 " respectively. Value 1 is added to '0xaaaaaaaa' and\n"
394 " -2 to '0xff' in successive packets. EtherType and\n"
395 " 'value' elements should be given in hexadecimals.\n"
396 " Field names are only for information and debugging\n"
397 " purposes. Maximum amount of fields supported is %u,\n"
398 " maximum name length of a field is %u and maximum\n"
399 " value size is %u bytes.\n"
400 " -W, --wait_start <sec> Wait <sec> seconds before starting traffic. Default: 0\n"
401 "\n", MAX_HDR_FIELDS, MAX_HDR_NAME_LEN, MAX_HDR_VALUE_SZ);
402}
403
404static int parse_vlan(const char *str, test_global_t *global)
405{
406 struct vlan_hdr *vlan;
407 const char *start = str;
408 char *end;
409 int num_vlan = 0;
410 intptr_t str_len = strlen(str);
411
412 while (num_vlan < MAX_VLANS) {
413 vlan = &global->test_options.vlan[num_vlan];
414
415 /* TPID in hexadecimal */
416 end = NULL;
417 vlan->tpid = strtoul(start, &end, 16);
418 if (end < start)
419 break;
420
421 /* Skip ':' */
422 start = end + 1;
423 if (start - str >= str_len)
424 break;
425
426 /* TCI in hexadecimal */
427 end = NULL;
428 vlan->tci = strtoul(start, &end, 16);
429 if (end < start)
430 break;
431
432 num_vlan++;
433
434 /* Skip ',' or stop at the string end */
435 start = end + 1;
436 if (start - str >= str_len)
437 break;
438 }
439
440 return num_vlan;
441}
442
443static inline uint64_t bswap(uint64_t in, uint32_t len)
444{
445 uint8_t byte;
446 uint64_t result = 0;
447
448 if (ODP_BIG_ENDIAN || len == sizeof(uint8_t))
449 return in;
450
451 for (uint32_t i = 0; i < len; i++) {
452 byte = (in >> (8 * i)) & 0xff;
453 result |= ((uint64_t)byte << (8 * (len - 1 - i)));
454 }
455
456 return result;
457}
458
459static odp_bool_t parse_custom_fields(const char *optarg, test_options_t *opts)
460{
461 char *tmp_str = strdup(optarg), *tmp;
462 uint32_t num_fields = 0;
463 char name[MAX_HDR_NAME_LEN + 1];
464 uint32_t len;
465 uint64_t value;
466 int64_t diff;
467 hdr_field_t *hdr;
468 int ret;
469
470 if (tmp_str == NULL)
471 return false;
472
473 tmp = strtok(tmp_str, TOKEN_DELIMITER);
474
475 if (tmp == NULL) {
476 free(tmp_str);
477 return false;
478 }
479
480 opts->custom_l3.eth_type = strtoul(tmp, NULL, 16);
481 tmp = strtok(NULL, TOKEN_DELIMITER);
482
483 while (tmp) {
484 if (num_fields == MAX_HDR_FIELDS) {
485 ODPH_ERR("Invalid custom header, too many fields: %u\n", num_fields + 1);
486 free(tmp_str);
487 return false;
488 }
489
490 ret = sscanf(tmp, "%" S(MAX_HDR_NAME_LEN) "[^" FIELD_DELIMITER "]" FIELD_DELIMITER
491 "%u" FIELD_DELIMITER "%" PRIx64 FIELD_DELIMITER "%" PRIi64 "", name,
492 &len, &value, &diff);
493
494 if (ret != 4) {
495 ODPH_ERR("Invalid custom header, bad field format\n");
496 free(tmp_str);
497 return false;
498 }
499
500 if (len > MAX_HDR_VALUE_SZ) {
501 ODPH_ERR("Invalid custom header, field length too long: %u\n", len);
502 free(tmp_str);
503 return false;
504 }
505
506 hdr = &opts->custom_l3.fields[num_fields];
507 odph_strcpy(hdr->name, name, MAX_HDR_NAME_LEN + 1);
508 hdr->value = value;
509 hdr->diff = diff;
510 hdr->len = len;
511 opts->custom_l3.tot_len += len;
512 ++num_fields;
513 tmp = strtok(NULL, TOKEN_DELIMITER);
514 }
515
516 opts->num_custom_l3 = num_fields;
517 free(tmp_str);
518
519 return true;
520}
521
522static odp_bool_t parse_lso_fields(const char *optarg, test_options_t *opts)
523{
524 char *tmp_str = strdup(optarg), *tmp;
525 uint8_t num_custom = 0;
526 uint32_t proto, mod_op, offset, size;
527 int ret;
528 odp_bool_t ret_val = true;
529
530 odp_lso_profile_param_init(&opts->lso.param);
531
532 if (tmp_str == NULL) {
533 ODPH_ERR("Error: strdup() failed\n");
534 return false;
535 }
536
537 /* Format: proto,payload_offset,max_payload_len[,mod_op:offset:size,mod_op:offset...] */
538
539 tmp = strtok(tmp_str, TOKEN_DELIMITER);
540 if (tmp == NULL) {
541 ODPH_ERR("Error: Unable to parse LSO protocol\n");
542 ret_val = false;
543 goto exit;
544 }
545
546 proto = strtoul(tmp, NULL, 0);
547
548 switch (proto) {
549 case 0:
550 opts->lso.param.lso_proto = ODP_LSO_PROTO_IPV4;
551 break;
552 case 1:
553 opts->lso.param.lso_proto = ODP_LSO_PROTO_TCP_IPV4;
554 break;
555 case 2:
556 opts->lso.param.lso_proto = ODP_LSO_PROTO_CUSTOM;
557 break;
558 default:
559 ODPH_ERR("Error: Invalid LSO protocol: %u\n", proto);
560 ret_val = false;
561 goto exit;
562 }
563
564 tmp = strtok(NULL, TOKEN_DELIMITER);
565 if (tmp == NULL) {
566 ODPH_ERR("Error: Unable to parse LSO payload offset\n");
567 ret_val = false;
568 goto exit;
569 }
570 opts->lso.payload_offset = strtoul(tmp, NULL, 0);
571
572 tmp = strtok(NULL, TOKEN_DELIMITER);
573 if (tmp == NULL) {
574 ODPH_ERR("Error: Unable to parse LSO max payload length\n");
575 ret_val = false;
576 goto exit;
577 }
578 opts->lso.max_payload_len = strtoul(tmp, NULL, 0);
579
580 tmp = strtok(NULL, TOKEN_DELIMITER);
581 while (opts->lso.param.lso_proto == ODP_LSO_PROTO_CUSTOM && tmp) {
582 if (num_custom == ODP_LSO_MAX_CUSTOM) {
583 ODPH_ERR("Error: Too many custom LSO operations (max %u)\n",
585 ret_val = false;
586 goto exit;
587 }
588
589 ret = sscanf(tmp, "%" PRIu32 "" FIELD_DELIMITER
590 "%" PRIu32 "" FIELD_DELIMITER "%" PRIu32 "",
591 &mod_op, &offset, &size);
592
593 if (ret != 3) {
594 ODPH_ERR("Error: Invalid custom LSO operation, bad field format\n");
595 ret_val = false;
596 goto exit;
597 }
598
599 switch (mod_op) {
600 case 0:
601 opts->lso.param.custom.field[num_custom].mod_op = ODP_LSO_ADD_SEGMENT_NUM;
602 break;
603 case 1:
604 opts->lso.param.custom.field[num_custom].mod_op = ODP_LSO_ADD_PAYLOAD_LEN;
605 break;
606 case 2:
607 opts->lso.param.custom.field[num_custom].mod_op =
609 break;
610 case 3:
611 opts->lso.param.custom.field[num_custom].mod_op = ODP_LSO_WRITE_BITS;
612 break;
613 default:
614 ODPH_ERR("Error: Invalid custom LSO operation: %" PRIu32 "\n", mod_op);
615 ret_val = false;
616 goto exit;
617 }
618
619 opts->lso.param.custom.field[num_custom].offset = offset;
620
621 if (mod_op == 3 && size != 1) {
622 ODPH_ERR("Error: ODP_LSO_WRITE_BITS requires size of 1 byte"
623 " (got %" PRIu32 ")\n", size);
624 ret_val = false;
625 goto exit;
626 } else if (mod_op != 3 && size != 1 && size != 2 && size != 4 && size != 8) {
627 ODPH_ERR("Error: Invalid custom field size: %" PRIu32 "\n", size);
628 ret_val = false;
629 goto exit;
630 }
631
632 opts->lso.param.custom.field[num_custom].size = size;
633
634 if (mod_op == 3) {
635 /* Parse first/middle/last mask and value byte pairs */
636 int32_t f_mask, f_val, m_mask, m_val, l_mask, l_val;
637
638 ret = sscanf(tmp, "%*u" FIELD_DELIMITER "%*u" FIELD_DELIMITER "%*u"
639 FIELD_DELIMITER "%" SCNi32 FIELD_DELIMITER "%" SCNi32
640 FIELD_DELIMITER "%" SCNi32 FIELD_DELIMITER "%" SCNi32
641 FIELD_DELIMITER "%" SCNi32 FIELD_DELIMITER "%" SCNi32,
642 &f_mask, &f_val, &m_mask, &m_val, &l_mask, &l_val);
643
644 if (ret != 6) {
645 ODPH_ERR("Error: ODP_LSO_WRITE_BITS requires 6 mask/value bytes, "
646 "parsed: %d\n", ret);
647 ret_val = false;
648 goto exit;
649 }
650
651 if (f_mask < 0 || f_mask > UINT8_MAX || f_val < 0 || f_val > UINT8_MAX ||
652 m_mask < 0 || m_mask > UINT8_MAX || m_val < 0 || m_val > UINT8_MAX ||
653 l_mask < 0 || l_mask > UINT8_MAX || l_val < 0 || l_val > UINT8_MAX) {
654 ODPH_ERR("Error: ODP_LSO_WRITE_BITS mask/value byte out of "
655 "range\n");
656 ret_val = false;
657 goto exit;
658 }
659
660 opts->lso.param.custom.field[num_custom].write_bits.first_seg.mask[0] =
661 f_mask;
662 opts->lso.param.custom.field[num_custom].write_bits.first_seg.value[0] =
663 f_val;
664 opts->lso.param.custom.field[num_custom].write_bits.middle_seg.mask[0] =
665 m_mask;
666 opts->lso.param.custom.field[num_custom].write_bits.middle_seg.value[0] =
667 m_val;
668 opts->lso.param.custom.field[num_custom].write_bits.last_seg.mask[0] =
669 l_mask;
670 opts->lso.param.custom.field[num_custom].write_bits.last_seg.value[0] =
671 l_val;
672 }
673
674 num_custom++;
675 tmp = strtok(NULL, TOKEN_DELIMITER);
676 }
677
678 if (opts->lso.param.lso_proto == ODP_LSO_PROTO_CUSTOM && num_custom == 0) {
679 ODPH_ERR("Error: Custom LSO protocol requires at least one custom field\n");
680 ret_val = false;
681 goto exit;
682 }
683
684 opts->lso.param.custom.num_custom = num_custom;
685 opts->lso.enabled = true;
686
687exit:
688 free(tmp_str);
689
690 return ret_val;
691}
692
693static int init_bins(test_global_t *global)
694{
695 uint32_t i, bin_size;
696 test_options_t *test_options = &global->test_options;
697 uint32_t num_bins = test_options->rand_pkt_len_bins;
698 uint32_t len_min = test_options->rand_pkt_len_min;
699 uint32_t len_max = test_options->rand_pkt_len_max;
700 uint32_t num_bytes = len_max - len_min + 1;
701
702 if (len_max <= len_min) {
703 ODPH_ERR("Error: Bad max packet length\n");
704 return -1;
705 }
706
707 if (num_bins == 0)
708 num_bins = num_bytes;
709
710 if (num_bins == 1 || num_bins > MAX_BINS || num_bins > num_bytes) {
711 ODPH_ERR("Error: Bad number of packet length bins: %u\n", num_bins);
712 return -1;
713 }
714
715 bin_size = (len_max - len_min + 1) / (num_bins - 1);
716
717 /* Min length is the first bin */
718 for (i = 0; i < num_bins - 1; i++)
719 global->len_bin[i] = len_min + (i * bin_size);
720
721 /* Max length is the last bin */
722 global->len_bin[i] = len_max;
723 global->num_bins = num_bins;
724
725 return 0;
726}
727
728static int parse_options(int argc, char *argv[], test_global_t *global)
729{
730 int opt, i, len, str_len, port;
731 unsigned long int count;
732 uint32_t min_packets, num_tx_pkt, num_tx_alloc, pkt_len, req_len, val, bins;
733 char *name, *str, *end;
734 test_options_t *test_options = &global->test_options;
735 int ret = 0;
736 uint8_t default_eth_dst[6] = {0x02, 0x00, 0x00, 0xa0, 0xb0, 0xc0};
737
738 static const struct option longopts[] = {
739 {"interface", required_argument, NULL, 'i'},
740 {"eth_dst", required_argument, NULL, 'e'},
741 {"num_rx", required_argument, NULL, 'r'},
742 {"num_tx", required_argument, NULL, 't'},
743 {"lso", required_argument, NULL, 'T'},
744 {"num_pkt", required_argument, NULL, 'n'},
745 {"proto", required_argument, NULL, 'N'},
746 {"len", required_argument, NULL, 'l'},
747 {"align", required_argument, NULL, 'I'},
748 {"len_range", required_argument, NULL, 'L'},
749 {"direct_rx", required_argument, NULL, 'D'},
750 {"tx_mode", required_argument, NULL, 'm'},
751 {"burst_size", required_argument, NULL, 'b'},
752 {"bursts", required_argument, NULL, 'x'},
753 {"gap", required_argument, NULL, 'g'},
754 {"vlan", required_argument, NULL, 'v'},
755 {"ipv4_src", required_argument, NULL, 's'},
756 {"ipv4_dst", required_argument, NULL, 'd'},
757 {"src_port", required_argument, NULL, 'o'},
758 {"dst_port", required_argument, NULL, 'p'},
759 {"promisc_mode", no_argument, NULL, 'P'},
760 {"latency", no_argument, NULL, 'a'},
761 {"c_mode", required_argument, NULL, 'c'},
762 {"no_udp_checksum", no_argument, NULL, 'C'},
763 {"checksum_offload", no_argument, NULL, 'X'},
764 {"no_payload_fill", no_argument, NULL, 'A'},
765 {"verbose", no_argument, NULL, 'V'},
766 {"mtu", required_argument, NULL, 'M'},
767 {"quit", required_argument, NULL, 'q'},
768 {"wait", required_argument, NULL, 'w'},
769 {"wait_start", required_argument, NULL, 'W'},
770 {"update_stat", required_argument, NULL, 'u'},
771 {"custom_l3", required_argument, NULL, 'U'},
772 {"help", no_argument, NULL, 'h'},
773 {NULL, 0, NULL, 0}
774 };
775
776 static const char *shortopts = "+i:e:r:t:T:n:N:l:I:L:D:m:M:b:x:g:v:s:d:o:"
777 "p:c:CXAVq:u:w:W:PaU:h";
778
779 test_options->num_pktio = 0;
780 test_options->num_rx = 1;
781 test_options->num_tx = 1;
782 test_options->num_pkt = 1000;
783 test_options->pkt_len = 512;
784 test_options->pool_align = 0;
785 test_options->use_rand_pkt_len = 0;
786 test_options->direct_rx = 0;
787 test_options->tx_mode = TX_MODE_REF;
788 test_options->burst_size = 8;
789 test_options->bursts = 1;
790 test_options->gap_nsec = 1000000;
791 test_options->num_vlan = 0;
792 test_options->promisc_mode = 0;
793 test_options->calc_latency = 0;
794 test_options->calc_cs = 1;
795 test_options->cs_offload = 0;
796 test_options->fill_pl = 1;
797 test_options->verbose = 0;
798 odph_strcpy(test_options->ipv4_src_s, "192.168.0.1",
799 sizeof(test_options->ipv4_src_s));
800 odph_strcpy(test_options->ipv4_dst_s, "192.168.0.2",
801 sizeof(test_options->ipv4_dst_s));
802 if (odph_ipv4_addr_parse(&test_options->ipv4_src, test_options->ipv4_src_s)) {
803 ODPH_ERR("Address parse failed\n");
804 return -1;
805 }
806 if (odph_ipv4_addr_parse(&test_options->ipv4_dst, test_options->ipv4_dst_s)) {
807 ODPH_ERR("Address parse failed\n");
808 return -1;
809 }
810 test_options->src_port = 10000;
811 test_options->dst_port = 20000;
812 test_options->c_mode.src_port = 0;
813 test_options->c_mode.dst_port = 0;
814 test_options->quit = 0;
815 test_options->update_msec = 0;
816 test_options->wait_sec = 0;
817 test_options->wait_start_sec = 0;
818 test_options->mtu = 0;
819 test_options->l4_proto = L4_PROTO_UDP;
820 test_options->lso.enabled = false;
821
822 for (i = 0; i < MAX_PKTIOS; i++) {
823 memcpy(global->pktio[i].eth_dst.addr, default_eth_dst, 6);
824 global->pktio[i].eth_dst.addr[5] += i;
825 }
826
827 while (1) {
828 opt = getopt_long(argc, argv, shortopts, longopts, NULL);
829
830 if (opt == -1)
831 break;
832
833 switch (opt) {
834 case 'i':
835 i = 0;
836 str = optarg;
837 str_len = strlen(str);
838
839 while (str_len > 0) {
840 len = strcspn(str, ",");
841 str_len -= len + 1;
842
843 if (i == MAX_PKTIOS) {
844 ODPH_ERR("Error: Too many interfaces\n");
845 ret = -1;
846 break;
847 }
848
849 if (len > MAX_PKTIO_NAME) {
850 ODPH_ERR("Error: Too long interface name %s\n", str);
851 ret = -1;
852 break;
853 }
854
855 name = test_options->pktio_name[i];
856 memcpy(name, str, len);
857 str += len + 1;
858 i++;
859 }
860
861 test_options->num_pktio = i;
862
863 break;
864 case 'e':
865 i = 0;
866 str = optarg;
867 str_len = strlen(str);
868
869 while (str_len > 0) {
870 odph_ethaddr_t *dst = &global->pktio[i].eth_dst;
871
872 len = strcspn(str, ",");
873 str_len -= len + 1;
874
875 if (i == MAX_PKTIOS) {
876 ODPH_ERR("Error: Too many MAC addresses\n");
877 ret = -1;
878 break;
879 }
880
881 if (odph_eth_addr_parse(dst, str)) {
882 ODPH_ERR("Error: Bad MAC address: %s\n", str);
883 ret = -1;
884 break;
885 }
886
887 str += len + 1;
888 i++;
889 }
890 break;
891 case 'o':
892 port = atoi(optarg);
893 if (port < 0 || port > UINT16_MAX) {
894 ODPH_ERR("Error: Bad source port: %d\n", port);
895 ret = -1;
896 break;
897 }
898 test_options->src_port = port;
899 break;
900 case 'p':
901 port = atoi(optarg);
902 if (port < 0 || port > UINT16_MAX) {
903 ODPH_ERR("Error: Bad destination port: %d\n", port);
904 ret = -1;
905 break;
906 }
907 test_options->dst_port = port;
908 break;
909 case 'P':
910 test_options->promisc_mode = 1;
911 break;
912 case 'a':
913 test_options->calc_latency = 1;
914 break;
915 case 'r':
916 test_options->num_rx = atoi(optarg);
917 break;
918 case 't':
919 test_options->num_tx = atoi(optarg);
920 break;
921 case 'T':
922 if (!parse_lso_fields(optarg, test_options))
923 ret = -1;
924 break;
925 case 'n':
926 test_options->num_pkt = atoi(optarg);
927 break;
928 case 'N':
929 test_options->l4_proto = atoi(optarg);
930 break;
931 case 'l':
932 test_options->pkt_len = atoi(optarg);
933 break;
934 case 'I':
935 test_options->pool_align = atoi(optarg);
936 break;
937 case 'L':
938 pkt_len = strtoul(optarg, &end, 0);
939 test_options->rand_pkt_len_min = pkt_len;
940 end++;
941 pkt_len = strtoul(end, &str, 0);
942 test_options->rand_pkt_len_max = pkt_len;
943 str++;
944 val = strtoul(str, NULL, 0);
945 test_options->rand_pkt_len_bins = val;
946 test_options->use_rand_pkt_len = 1;
947 break;
948 case 'D':
949 test_options->direct_rx = atoi(optarg);
950 break;
951 case 'm':
952 test_options->tx_mode = atoi(optarg);
953 break;
954 case 'M':
955 test_options->mtu = atoi(optarg);
956 break;
957 case 'b':
958 test_options->burst_size = atoi(optarg);
959 break;
960 case 'x':
961 test_options->bursts = atoi(optarg);
962 break;
963 case 'g':
964 test_options->gap_nsec = atoll(optarg);
965 break;
966 case 'v':
967 test_options->num_vlan = parse_vlan(optarg, global);
968 if (test_options->num_vlan == 0) {
969 ODPH_ERR("Error: Did not find any VLANs\n");
970 ret = -1;
971 }
972 break;
973 case 's':
974 if (odph_ipv4_addr_parse(&test_options->ipv4_src,
975 optarg)) {
976 ODPH_ERR("Error: Bad IPv4 source address: %s\n", optarg);
977 ret = -1;
978 }
979 odph_strcpy(test_options->ipv4_src_s, optarg,
980 sizeof(test_options->ipv4_src_s));
981 break;
982 case 'd':
983 if (odph_ipv4_addr_parse(&test_options->ipv4_dst,
984 optarg)) {
985 ODPH_ERR("Error: Bad IPv4 destination address: %s\n", optarg);
986 ret = -1;
987 }
988 odph_strcpy(test_options->ipv4_dst_s, optarg,
989 sizeof(test_options->ipv4_dst_s));
990 break;
991 case 'c':
992 count = strtoul(optarg, &end, 0);
993 test_options->c_mode.src_port = count;
994
995 end++;
996 count = strtoul(end, NULL, 0);
997 test_options->c_mode.dst_port = count;
998 break;
999 case 'C':
1000 test_options->calc_cs = 0;
1001 break;
1002 case 'X':
1003 test_options->cs_offload = 1;
1004 break;
1005 case 'A':
1006 test_options->fill_pl = 0;
1007 break;
1008 case 'V':
1009 test_options->verbose = 1;
1010 break;
1011 case 'q':
1012 test_options->quit = atoll(optarg);
1013 break;
1014 case 'u':
1015 test_options->update_msec = atoll(optarg);
1016 break;
1017 case 'w':
1018 test_options->wait_sec = atoi(optarg);
1019 break;
1020 case 'W':
1021 test_options->wait_start_sec = atoi(optarg);
1022 break;
1023 case 'U':
1024 if (!parse_custom_fields(optarg, test_options))
1025 ret = -1;
1026
1027 break;
1028 case 'h':
1029 /* fall through */
1030 default:
1031 print_usage();
1032 ret = -1;
1033 break;
1034 }
1035 }
1036
1037 if (ret)
1038 return -1;
1039
1040 if (test_options->num_pktio == 0) {
1041 ODPH_ERR("Error: At least one packet IO interface is needed.\n");
1042 ODPH_ERR(" Use -i <name> to specify interfaces.\n");
1043 return -1;
1044 }
1045
1046 if (test_options->num_rx < 1 && test_options->num_tx < 1) {
1047 ODPH_ERR("Error: At least one rx or tx thread needed.\n");
1048 return -1;
1049 }
1050
1051 test_options->num_cpu = test_options->num_rx + test_options->num_tx;
1052
1053 if (test_options->num_cpu > MAX_WORKERS) {
1054 ODPH_ERR("Error: Too many worker threads\n");
1055 return -1;
1056 }
1057
1058 num_tx_pkt = test_options->burst_size * test_options->bursts;
1059 global->num_tx_pkt = num_tx_pkt;
1060
1061 if (num_tx_pkt == 0) {
1062 ODPH_ERR("Error: Bad number of tx packets: %u\n", num_tx_pkt);
1063 return -1;
1064 }
1065
1066 if (test_options->use_rand_pkt_len) {
1067 if (init_bins(global))
1068 return -1;
1069 }
1070
1071 bins = global->num_bins ? global->num_bins : 1;
1072 num_tx_alloc = num_tx_pkt * bins;
1073 if (num_tx_alloc > MAX_ALLOC_PACKETS) {
1074 ODPH_ERR("Error: Too many tx packets: %u\n", num_tx_alloc);
1075 return -1;
1076 }
1077
1078 /* Pool needs to have enough packets for all TX side pre-allocated packets and
1079 * a burst per thread (for packet copies). RX side needs one burst per thread per pktio. */
1080 min_packets = test_options->num_pktio * test_options->num_tx * num_tx_alloc;
1081 min_packets += test_options->num_tx * test_options->burst_size;
1082 min_packets += test_options->num_pktio * test_options->num_rx * test_options->burst_size;
1083
1084 if (test_options->num_pkt < min_packets) {
1085 ODPH_ERR("Error: Pool needs to have at least %u packets\n", min_packets);
1086 return -1;
1087 }
1088
1089 if (test_options->calc_latency && test_options->tx_mode == TX_MODE_REF) {
1090 ODPH_ERR("Error: Latency test is not supported with packet references (--tx_mode 1)\n");
1091 return -1;
1092 }
1093 if (test_options->calc_latency && (test_options->num_rx < 1 || test_options->num_tx < 1)) {
1094 ODPH_ERR("Error: Latency test requires both rx and tx threads\n");
1095 return -1;
1096 }
1097
1098 if (test_options->gap_nsec) {
1099 double gap_hz = 1000000000.0 / test_options->gap_nsec;
1100
1101 if (gap_hz > (double)odp_time_local_res()) {
1102 ODPH_ERR("\nWARNING: Burst gap exceeds time counter resolution "
1103 "%" PRIu64 "\n\n", odp_time_local_res());
1104 }
1105 }
1106
1107 if (global->num_bins) {
1108 if (num_tx_pkt > global->num_bins && num_tx_pkt % global->num_bins)
1109 ODPH_ERR("\nWARNING: Transmit packet count is not evenly divisible into packet length bins.\n\n");
1110
1111 if (num_tx_pkt < global->num_bins)
1112 ODPH_ERR("\nWARNING: Not enough packets for every packet length bin.\n\n");
1113 }
1114
1115 if (test_options->c_mode.dst_port && num_tx_pkt % test_options->c_mode.dst_port)
1116 ODPH_ERR("\nWARNING: Transmit packet count is not evenly divisible by destination port count.\n\n");
1117
1118 if (test_options->c_mode.src_port && num_tx_pkt % test_options->c_mode.src_port)
1119 ODPH_ERR("\nWARNING: Transmit packet count is not evenly divisible by source port count.\n\n");
1120
1121 if (test_options->l4_proto != L4_PROTO_TCP && test_options->l4_proto != L4_PROTO_UDP &&
1122 test_options->l4_proto != L4_PROTO_NONE) {
1123 ODPH_ERR("Error: Invalid L4 protocol: %" PRIu8 "\n", test_options->l4_proto);
1124 return -1;
1125 }
1126 if (test_options->l4_proto == L4_PROTO_TCP && test_options->tx_mode != TX_MODE_COPY) {
1127 ODPH_ERR("Error: TCP protocol supported only with copy transmit mode\n");
1128 return -1;
1129 }
1130
1131 test_options->eth_type = test_options->num_custom_l3 ?
1132 test_options->custom_l3.eth_type : ODPH_ETHTYPE_IPV4;
1133 test_options->l3_len = test_options->num_custom_l3 ?
1134 test_options->custom_l3.tot_len : ODPH_IPV4HDR_LEN;
1135 test_options->hdr_len = ODPH_ETHHDR_LEN + (test_options->num_vlan * ODPH_VLANHDR_LEN) +
1136 test_options->l3_len;
1137
1138 if (test_options->l4_proto != L4_PROTO_NONE)
1139 test_options->hdr_len += test_options->l4_proto == L4_PROTO_UDP ?
1140 ODPH_UDPHDR_LEN : ODPH_TCPHDR_LEN;
1141
1142 pkt_len = test_options->use_rand_pkt_len ?
1143 test_options->rand_pkt_len_min : test_options->pkt_len;
1144
1145 req_len = test_options->hdr_len;
1146 if (test_options->calc_latency)
1147 req_len += sizeof(ts_data_t);
1148
1149 if (test_options->num_custom_l3 && test_options->cs_offload) {
1150 ODPH_ERR("Error: Checksum offloading supported only with IP\n");
1151 return -1;
1152 }
1153
1154 /* Keep '--no_udp_checksum' only if UDP is the L4 protocol */
1155 if (test_options->l4_proto != L4_PROTO_UDP)
1156 test_options->calc_cs = 1;
1157
1158 if (test_options->cs_offload)
1159 test_options->calc_cs = 0;
1160
1161 if (req_len > pkt_len) {
1162 ODPH_ERR("Error: Headers do not fit into packet length of %" PRIu32 " bytes "
1163 "(min %" PRIu32 " bytes required)\n", pkt_len, req_len);
1164 return -1;
1165 }
1166
1167 if (test_options->lso.enabled) {
1168 if (test_options->tx_mode != TX_MODE_COPY) {
1169 ODPH_ERR("Error: LSO supported only with copy transmit mode\n");
1170 return -1;
1171 }
1172
1173 if (test_options->num_custom_l3 &&
1174 test_options->lso.param.lso_proto != ODP_LSO_PROTO_CUSTOM) {
1175 ODPH_ERR("Error: LSO and L3 protocol mismatch\n");
1176 return -1;
1177 }
1178
1179 if (test_options->l4_proto != L4_PROTO_TCP &&
1180 test_options->lso.param.lso_proto == ODP_LSO_PROTO_TCP_IPV4) {
1181 ODPH_ERR("Error: LSO and L4 protocol mismatch\n");
1182 return -1;
1183 }
1184 }
1185 return 0;
1186}
1187
1188static int set_num_cpu(test_global_t *global)
1189{
1190 int ret;
1191 test_options_t *test_options = &global->test_options;
1192 int num_cpu = test_options->num_cpu;
1193
1194 ret = odp_cpumask_default_worker(&global->cpumask, num_cpu);
1195
1196 if (ret != num_cpu) {
1197 int cpu;
1198
1199 /* Normally we want to use only worker threads */
1200 if (ret > 1) {
1201 ODPH_ERR("Error: Too many workers. Maximum supported %i.\n", ret);
1202 return -1;
1203 }
1204
1205 /* When number of workers is very limited (e.g. ODP project CI),
1206 * we try to use any CPUs available. */
1207 ret = odp_cpumask_all_available(&global->cpumask);
1208 if (ret < num_cpu) {
1209 ODPH_ERR("Error: Not enough CPUs. Maximum supported %i.\n", ret);
1210 return -1;
1211 }
1212
1213 /* Remove extra CPUs from the mask */
1214 cpu = odp_cpumask_first(&global->cpumask);
1215 while (ret > num_cpu) {
1216 odp_cpumask_clr(&global->cpumask, cpu);
1217 cpu = odp_cpumask_first(&global->cpumask);
1218 ret--;
1219 }
1220 }
1221
1222 odp_barrier_init(&global->barrier, num_cpu + 1);
1223
1224 return 0;
1225}
1226
1227static int check_lso_capa(char *name, odp_pktio_capability_t *pktio_capa,
1228 test_options_t *opt, odp_pool_t pool)
1229{
1230 odp_packet_t pkt;
1231 uint32_t max_segments;
1232 uint32_t pkt_payload;
1233 const uint32_t max_pkt_len = opt->use_rand_pkt_len ?
1234 opt->rand_pkt_len_max : opt->pkt_len;
1235
1236 if (pktio_capa->lso.max_profiles < opt->num_pktio ||
1237 pktio_capa->lso.max_profiles_per_pktio == 0) {
1238 ODPH_ERR("Error (%s): Not enough LSO profiles (max %" PRIu32 ")\n", name,
1239 ODPH_MIN(pktio_capa->lso.max_profiles,
1240 pktio_capa->lso.max_profiles_per_pktio));
1241 return -1;
1242 }
1243
1244 if (opt->lso.param.lso_proto == ODP_LSO_PROTO_TCP_IPV4 && !pktio_capa->lso.proto.tcp_ipv4) {
1245 ODPH_ERR("Error (%s): ODP_LSO_PROTO_TCP_IPV4 not supported\n", name);
1246 return -1;
1247 }
1248 if (opt->lso.param.lso_proto == ODP_LSO_PROTO_IPV4 && !pktio_capa->lso.proto.ipv4) {
1249 ODPH_ERR("Error (%s): ODP_LSO_PROTO_IPV4 not supported\n", name);
1250 return -1;
1251 }
1252 if (opt->lso.param.lso_proto == ODP_LSO_PROTO_CUSTOM && !pktio_capa->lso.proto.custom) {
1253 ODPH_ERR("Error (%s): ODP_LSO_PROTO_CUSTOM not supported\n", name);
1254 return -1;
1255 }
1256
1257 /* Check number of segments in a max length packet */
1258 pkt = odp_packet_alloc(pool, max_pkt_len);
1259 if (pkt == ODP_PACKET_INVALID) {
1260 ODPH_ERR("Error (%s): Allocating test packet failed\n", name);
1261 return -1;
1262 }
1263
1264 max_segments = odp_packet_num_segs(pkt);
1265 odp_packet_free(pkt);
1266
1267 if (max_segments > pktio_capa->lso.max_packet_segments) {
1268 ODPH_ERR("Error (%s): Max LSO packet segments: %" PRIu32 "\n", name,
1269 pktio_capa->lso.max_packet_segments);
1270 return -1;
1271 }
1272
1273 /* Check max number of LSO segments */
1274 pkt_payload = max_pkt_len - opt->lso.payload_offset;
1275 max_segments = (pkt_payload + opt->lso.max_payload_len - 1) /
1276 opt->lso.max_payload_len;
1277 if (max_segments > pktio_capa->lso.max_segments) {
1278 ODPH_ERR("Error (%s): Max LSO segments: %" PRIu32 "\n", name,
1279 pktio_capa->lso.max_segments);
1280 return -1;
1281 }
1282
1283 if (opt->lso.max_payload_len > pktio_capa->lso.max_payload_len) {
1284 ODPH_ERR("Error (%s): Max LSO payload len: %" PRIu32 "\n", name,
1285 pktio_capa->lso.max_payload_len);
1286 return -1;
1287 }
1288
1289 if (opt->lso.payload_offset > pktio_capa->lso.max_payload_offset) {
1290 ODPH_ERR("Error (%s): Max LSO payload offset: %" PRIu32 "\n", name,
1291 pktio_capa->lso.max_payload_len);
1292 return -1;
1293 }
1294
1295 if (opt->lso.param.lso_proto == ODP_LSO_PROTO_CUSTOM) {
1296 if (opt->lso.param.custom.num_custom > pktio_capa->lso.max_num_custom) {
1297 ODPH_ERR("Error (%s): Max LSO custom fields: %" PRIu32 "\n", name,
1298 pktio_capa->lso.max_num_custom);
1299 return -1;
1300 }
1301
1302 for (uint8_t i = 0; i < opt->lso.param.custom.num_custom; i++) {
1303 if (opt->lso.param.custom.field[i].mod_op == ODP_LSO_ADD_SEGMENT_NUM &&
1304 !pktio_capa->lso.mod_op.add_segment_num) {
1305 ODPH_ERR("Error (%s): ODP_LSO_ADD_SEGMENT_NUM not supported\n",
1306 name);
1307 return -1;
1308 }
1309 if (opt->lso.param.custom.field[i].mod_op == ODP_LSO_ADD_PAYLOAD_LEN &&
1310 !pktio_capa->lso.mod_op.add_payload_len) {
1311 ODPH_ERR("Error (%s): ODP_LSO_ADD_PAYLOAD_LEN not supported\n",
1312 name);
1313 return -1;
1314 }
1315 if (opt->lso.param.custom.field[i].mod_op == ODP_LSO_ADD_PAYLOAD_OFFSET &&
1316 !pktio_capa->lso.mod_op.add_payload_offset) {
1317 ODPH_ERR("Error (%s): ODP_LSO_ADD_PAYLOAD_OFFSET not supported\n",
1318 name);
1319 return -1;
1320 }
1321 if (opt->lso.param.custom.field[i].mod_op == ODP_LSO_WRITE_BITS &&
1322 !pktio_capa->lso.mod_op.write_bits) {
1323 ODPH_ERR("Error (%s): ODP_LSO_WRITE_BITS not supported\n",
1324 name);
1325 return -1;
1326 }
1327 }
1328 }
1329
1330 return 0;
1331}
1332
1333static int create_packet_pool(test_global_t *global)
1334{
1335 test_options_t *test_options = &global->test_options;
1336 uint32_t num_pkt = test_options->num_pkt;
1337 uint32_t pkt_len = test_options->use_rand_pkt_len ? test_options->rand_pkt_len_max :
1338 test_options->pkt_len;
1339 uint32_t align = test_options->pool_align;
1340 uint32_t seg_len;
1341 odp_pool_capability_t pool_capa;
1342 odp_pool_param_t pool_param;
1343
1344 global->pool = ODP_POOL_INVALID;
1345
1346 if (odp_pool_capability(&pool_capa)) {
1347 ODPH_ERR("Error: Pool capability failed.\n");
1348 return -1;
1349 }
1350
1351 if (pool_capa.pkt.max_num && num_pkt > pool_capa.pkt.max_num) {
1352 ODPH_ERR("Error: Too many packets. Max %u supported.\n", pool_capa.pkt.max_num);
1353 return -1;
1354 }
1355
1356 if (pool_capa.pkt.max_len && pkt_len > pool_capa.pkt.max_len) {
1357 ODPH_ERR("Error: Too large packets. Max %u supported length.\n",
1358 pool_capa.pkt.max_len);
1359 return -1;
1360 }
1361
1362 seg_len = test_options->hdr_len;
1363 if (pool_capa.pkt.max_seg_len && seg_len > pool_capa.pkt.max_seg_len) {
1364 ODPH_ERR("Error: Max segment length is too small %u\n", pool_capa.pkt.max_seg_len);
1365 return -1;
1366 }
1367
1368 if (align & (align - 1)) {
1369 ODPH_ERR("Error: Packet alignment must be a power of two: %u\n", align);
1370 return -1;
1371 }
1372
1373 if (align > pool_capa.pkt.max_align) {
1374 ODPH_ERR("Error: Too large packet alignment. Max %u supported.\n",
1375 pool_capa.pkt.max_align);
1376 return -1;
1377 }
1378
1379 odp_pool_param_init(&pool_param);
1380 pool_param.type = ODP_POOL_PACKET;
1381 pool_param.pkt.num = num_pkt;
1382 pool_param.pkt.len = pkt_len;
1383 pool_param.pkt.align = align;
1384 pool_param.pkt.seg_len = seg_len;
1385
1386 global->pool = odp_pool_create("packet gen pool", &pool_param);
1387
1388 if (global->pool == ODP_POOL_INVALID) {
1389 ODPH_ERR("Error: Pool create failed.\n");
1390 return -1;
1391 }
1392 return 0;
1393}
1394
1395static int destroy_packet_pool(test_global_t *global)
1396{
1397 if (global->pool != ODP_POOL_INVALID && odp_pool_destroy(global->pool)) {
1398 ODPH_ERR("Error: Pool destroy failed.\n");
1399 return -1;
1400 }
1401 return 0;
1402}
1403
1404static int open_pktios(test_global_t *global)
1405{
1406 odp_pktio_capability_t pktio_capa;
1407 odp_pktio_param_t pktio_param;
1408 odp_pktio_t pktio;
1409 odp_pktio_config_t pktio_config;
1410 odp_pktin_queue_param_t pktin_param;
1411 odp_pktout_queue_param_t pktout_param;
1412 char *name;
1413 uint32_t i;
1414 int j, pktio_idx;
1415 test_options_t *test_options = &global->test_options;
1416 int num_rx = test_options->num_rx;
1417 int num_tx = test_options->num_tx;
1418 uint32_t num_pktio = test_options->num_pktio;
1419 uint32_t num_pkt = test_options->num_pkt;
1420 uint32_t pkt_len = test_options->use_rand_pkt_len ?
1421 test_options->rand_pkt_len_max : test_options->pkt_len;
1422
1423 printf("\nODP packet generator\n");
1424 printf(" quit test after: %" PRIu64 " rounds\n",
1425 test_options->quit);
1426 printf(" num rx threads: %i\n", num_rx);
1427 printf(" num tx threads: %i\n", num_tx);
1428 printf(" num packets: %u\n", num_pkt);
1429 if (test_options->use_rand_pkt_len)
1430 printf(" packet length: %u-%u bytes, %u bins\n",
1431 test_options->rand_pkt_len_min,
1432 test_options->rand_pkt_len_max,
1433 test_options->rand_pkt_len_bins);
1434 else
1435 printf(" packet length: %u bytes\n", pkt_len);
1436 printf(" packet align: ");
1437 if (test_options->pool_align)
1438 printf("%u bytes\n", test_options->pool_align);
1439 else
1440 printf("pool default\n");
1441 printf(" MTU: ");
1442 if (test_options->mtu)
1443 printf("%u bytes\n", test_options->mtu);
1444 else
1445 printf("interface default\n");
1446 printf(" packet input mode: %s\n", test_options->direct_rx ? "direct" : "scheduler");
1447 printf(" promisc mode: %s\n", test_options->promisc_mode ? "enabled" : "disabled");
1448 printf(" transmit mode: %i\n", test_options->tx_mode);
1449 printf(" measure latency: %s\n", test_options->calc_latency ? "enabled" : "disabled");
1450 printf(" UDP SW checksum: %s\n", test_options->calc_cs ? "enabled" : "disabled");
1451 printf(" Checksum offload: %s\n", test_options->cs_offload ? "enabled" : "disabled");
1452 printf(" payload filling: %s\n", test_options->fill_pl ? "enabled" : "disabled");
1453 printf(" verbose: %s\n", test_options->verbose ? "enabled" : "disabled");
1454 printf(" tx burst size: %u\n", test_options->burst_size);
1455 printf(" tx bursts: %u\n", test_options->bursts);
1456 printf(" tx burst gap: %" PRIu64 " nsec\n",
1457 test_options->gap_nsec);
1458
1459 if (test_options->lso.enabled) {
1460 printf(" LSO protocol: %s\n",
1461 test_options->lso.param.lso_proto == ODP_LSO_PROTO_IPV4 ?
1462 "ODP_LSO_PROTO_IPV4" :
1463 test_options->lso.param.lso_proto == ODP_LSO_PROTO_TCP_IPV4 ?
1464 "ODP_LSO_PROTO_TCP_IPV4" : "ODP_LSO_PROTO_CUSTOM");
1465 printf(" max payload offset: %" PRIu32 " bytes\n",
1466 test_options->lso.payload_offset);
1467 printf(" max payload len: %" PRIu32 " bytes\n",
1468 test_options->lso.max_payload_len);
1469
1470 for (i = 0; i < test_options->lso.param.custom.num_custom; i++) {
1471 const odp_lso_modify_t mod_op =
1472 test_options->lso.param.custom.field[i].mod_op;
1473 const char *mod_op_str;
1474
1475 switch (mod_op) {
1477 mod_op_str = "ODP_LSO_ADD_SEGMENT_NUM";
1478 break;
1480 mod_op_str = "ODP_LSO_ADD_PAYLOAD_LEN";
1481 break;
1483 mod_op_str = "ODP_LSO_ADD_PAYLOAD_OFFSET";
1484 break;
1485 case ODP_LSO_WRITE_BITS:
1486 mod_op_str = "ODP_LSO_WRITE_BITS";
1487 break;
1488 default:
1489 mod_op_str = "UNKNOWN";
1490 break;
1491 }
1492
1493 printf(" Custom operation %" PRIu32 ": %s\n", i + 1, mod_op_str);
1494 printf(" offset: %" PRIu32 " bytes\n",
1495 test_options->lso.param.custom.field[i].offset);
1496 printf(" size: %" PRIu32 " bytes\n",
1497 test_options->lso.param.custom.field[i].size);
1498
1499 if (mod_op == ODP_LSO_WRITE_BITS) {
1500 const odp_lso_write_bits_t *fs =
1501 &test_options->lso.param.custom.field[i].write_bits.first_seg;
1502 const odp_lso_write_bits_t *ms =
1503 &test_options->lso.param.custom.field[i].write_bits.middle_seg;
1504 const odp_lso_write_bits_t *ls =
1505 &test_options->lso.param.custom.field[i].write_bits.last_seg;
1506
1507 printf(" first seg: 0x%02x/0x%02x\n",
1508 fs->value[0], fs->mask[0]);
1509 printf(" middle seg: 0x%02x/0x%02x\n",
1510 ms->value[0], ms->mask[0]);
1511 printf(" last seg: 0x%02x/0x%02x\n",
1512 ls->value[0], ls->mask[0]);
1513 }
1514 }
1515 }
1516
1517 printf(" clock resolution: %" PRIu64 " Hz\n", odp_time_local_res());
1518 for (i = 0; i < test_options->num_vlan; i++) {
1519 printf(" VLAN[%i]: %x:%x\n", i,
1520 test_options->vlan[i].tpid, test_options->vlan[i].tci);
1521 }
1522
1523 printf(" L3 protocol: ");
1524
1525 if (test_options->num_custom_l3) {
1526 printf("custom\n"
1527 " ether type: %x\n"
1528 " total length: %u\n"
1529 " fields:\n", test_options->custom_l3.eth_type,
1530 test_options->custom_l3.tot_len);
1531
1532 for (i = 0; i < test_options->num_custom_l3; i++) {
1533 printf(" name: %s\n"
1534 " length: %u\n"
1535 " value: %" PRIx64 "\n"
1536 " diff: %" PRIi64 "\n\n",
1537 test_options->custom_l3.fields[i].name,
1538 test_options->custom_l3.fields[i].len,
1539 test_options->custom_l3.fields[i].value,
1540 test_options->custom_l3.fields[i].diff);
1541 }
1542 } else {
1543 printf("IPv4\n");
1544 printf(" IPv4 source: %s\n", test_options->ipv4_src_s);
1545 printf(" IPv4 destination: %s\n\n", test_options->ipv4_dst_s);
1546 }
1547
1548 printf(" L4 protocol: %s\n",
1549 test_options->l4_proto == L4_PROTO_UDP ?
1550 "UDP" : test_options->l4_proto == L4_PROTO_TCP ? "TCP" : "none");
1551 printf(" source port: %u\n", test_options->src_port);
1552 printf(" destination port: %u\n", test_options->dst_port);
1553 printf(" src port count: %u\n", test_options->c_mode.src_port);
1554 printf(" dst port count: %u\n", test_options->c_mode.dst_port);
1555 printf(" num pktio: %u\n", num_pktio);
1556
1557 printf(" interfaces names: ");
1558 for (i = 0; i < num_pktio; i++) {
1559 if (i > 0)
1560 printf(" ");
1561 printf("%s\n", test_options->pktio_name[i]);
1562 }
1563
1564 printf(" destination MACs: ");
1565 for (i = 0; i < num_pktio; i++) {
1566 uint8_t *eth_dst = global->pktio[i].eth_dst.addr;
1567
1568 if (i > 0)
1569 printf(" ");
1570 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
1571 eth_dst[0], eth_dst[1], eth_dst[2],
1572 eth_dst[3], eth_dst[4], eth_dst[5]);
1573 }
1574 printf("\n");
1575
1576 odp_pktio_param_init(&pktio_param);
1577
1578 pktio_param.in_mode = num_rx ? (test_options->direct_rx ?
1581
1583
1584 for (i = 0; i < num_pktio; i++) {
1585 global->pktio[i].pktio = ODP_PKTIO_INVALID;
1586 global->pktio[i].lso_profile = ODP_LSO_PROFILE_INVALID;
1587 }
1588
1589 /* Open and configure interfaces */
1590 for (i = 0; i < num_pktio; i++) {
1591 name = test_options->pktio_name[i];
1592 pktio = odp_pktio_open(name, global->pool, &pktio_param);
1593
1594 if (pktio == ODP_PKTIO_INVALID) {
1595 ODPH_ERR("Error (%s): Pktio open failed.\n", name);
1596 return -1;
1597 }
1598
1599 global->pktio[i].pktio = pktio;
1600
1601 odp_pktio_print(pktio);
1602
1603 pktio_idx = odp_pktio_index(pktio);
1604 if (pktio_idx < 0) {
1605 ODPH_ERR("Error (%s): Reading pktio index failed: %i\n", name, pktio_idx);
1606 return -1;
1607 }
1608 global->if_from_pktio_idx[pktio_idx] = i;
1609
1610 if (odp_pktio_capability(pktio, &pktio_capa)) {
1611 ODPH_ERR("Error (%s): Pktio capability failed.\n", name);
1612 return -1;
1613 }
1614
1615 if (num_rx > (int)pktio_capa.max_input_queues) {
1616 ODPH_ERR("Error (%s): Too many RX threads. Interface supports max %u input queues.\n",
1617 name, pktio_capa.max_input_queues);
1618 return -1;
1619 }
1620
1621 if (num_tx > (int)pktio_capa.max_output_queues) {
1622 ODPH_ERR("Error (%s): Too many TX threads. Interface supports max %u output queues.\n",
1623 name, pktio_capa.max_output_queues);
1624 return -1;
1625 }
1626
1627 if (odp_pktio_mac_addr(pktio,
1628 &global->pktio[i].eth_src.addr,
1629 ODPH_ETHADDR_LEN) != ODPH_ETHADDR_LEN) {
1630 ODPH_ERR("Error (%s): MAC address read failed.\n", name);
1631 return -1;
1632 }
1633
1634 if (test_options->mtu) {
1635 uint32_t maxlen_input = pktio_capa.maxlen.max_input ? test_options->mtu : 0;
1636 uint32_t maxlen_output = pktio_capa.maxlen.max_output ?
1637 test_options->mtu : 0;
1638
1639 if (!pktio_capa.set_op.op.maxlen) {
1640 ODPH_ERR("Error (%s): modifying interface MTU not supported.\n",
1641 name);
1642 return -1;
1643 }
1644
1645 if (maxlen_input &&
1646 (maxlen_input < pktio_capa.maxlen.min_input ||
1647 maxlen_input > pktio_capa.maxlen.max_input)) {
1648 ODPH_ERR("Error (%s): unsupported MTU value %" PRIu32 " "
1649 "(min %" PRIu32 ", max %" PRIu32 ")\n", name, maxlen_input,
1650 pktio_capa.maxlen.min_input, pktio_capa.maxlen.max_input);
1651 return -1;
1652 }
1653 if (maxlen_output &&
1654 (maxlen_output < pktio_capa.maxlen.min_output ||
1655 maxlen_output > pktio_capa.maxlen.max_output)) {
1656 ODPH_ERR("Error (%s): unsupported MTU value %" PRIu32 " "
1657 "(min %" PRIu32 ", max %" PRIu32 ")\n", name,
1658 maxlen_output, pktio_capa.maxlen.min_output,
1659 pktio_capa.maxlen.max_output);
1660 return -1;
1661 }
1662
1663 if (odp_pktio_maxlen_set(pktio, maxlen_input, maxlen_output)) {
1664 ODPH_ERR("Error (%s): setting MTU failed\n", name);
1665 return -1;
1666 }
1667 }
1668
1669 if (test_options->tx_mode == TX_MODE_DF && pktio_capa.free_ctrl.dont_free == 0) {
1670 ODPH_ERR("Error (%s): Don't free mode not supported\n", name);
1671 return -1;
1672 }
1673 if (test_options->tx_mode == TX_MODE_REF && !pktio_capa.packet_ref.static_ref) {
1674 ODPH_ERR("Error (%s): Static-reference TX mode not supported\n", name);
1675 return -1;
1676 }
1677
1678 odp_pktio_config_init(&pktio_config);
1679
1680 if (test_options->cs_offload) {
1681 if (test_options->l4_proto == L4_PROTO_UDP) {
1682 if (pktio_capa.config.pktin.bit.udp_chksum &&
1683 pktio_capa.config.pktin.bit.drop_udp_err) {
1684 pktio_config.pktin.bit.udp_chksum = 1;
1685 pktio_config.pktin.bit.drop_udp_err = 1;
1686 } else {
1687 ODPH_ERR("Warning (%s): UDP checksum offload at RX not "
1688 "properly supported, leaving it disabled.\n",
1689 name);
1690 }
1691
1692 if (!pktio_capa.config.pktout.bit.udp_chksum) {
1693 ODPH_ERR("Error (%s): UDP checksum offload at TX not "
1694 "properly supported.\n", name);
1695 return -1;
1696 }
1697
1698 pktio_config.pktout.bit.udp_chksum_ena = 1;
1699 pktio_config.pktout.bit.udp_chksum = 1;
1700 }
1701
1702 if (test_options->l4_proto == L4_PROTO_TCP) {
1703 if (pktio_capa.config.pktin.bit.tcp_chksum &&
1704 pktio_capa.config.pktin.bit.drop_tcp_err) {
1705 pktio_config.pktin.bit.tcp_chksum = 1;
1706 pktio_config.pktin.bit.drop_tcp_err = 1;
1707 } else {
1708 ODPH_ERR("Warning (%s): TCP checksum offload at RX not "
1709 "properly supported, leaving it disabled.\n",
1710 name);
1711 }
1712
1713 if (!pktio_capa.config.pktout.bit.tcp_chksum) {
1714 ODPH_ERR("Error (%s): TCP checksum offload at TX not "
1715 "properly supported.\n", name);
1716 return -1;
1717 }
1718
1719 pktio_config.pktout.bit.tcp_chksum_ena = 1;
1720 pktio_config.pktout.bit.tcp_chksum = 1;
1721 }
1722 }
1723
1724 pktio_config.parser.layer = ODP_PROTO_LAYER_ALL;
1725
1726 if (test_options->lso.enabled) {
1727 if (check_lso_capa(name, &pktio_capa, test_options, global->pool))
1728 return -1;
1729
1730 pktio_config.enable_lso = true;
1731 }
1732
1733 if (odp_pktio_config(pktio, &pktio_config)) {
1734 ODPH_ERR("Error (%s): Pktio config failed.\n", name);
1735 return -1;
1736 }
1737
1738 if (test_options->lso.enabled) {
1739 const odp_lso_profile_param_t *param = &test_options->lso.param;
1740
1741 global->pktio[i].lso_profile = odp_lso_profile_create(pktio, param);
1742 if (global->pktio[i].lso_profile == ODP_LSO_PROFILE_INVALID) {
1743 ODPH_ERR("Error (%s): LSO profile create failed.\n", name);
1744 return -1;
1745 }
1746 }
1747
1748 if (test_options->promisc_mode && odp_pktio_promisc_mode(pktio) != 1) {
1749 if (!pktio_capa.set_op.op.promisc_mode) {
1750 ODPH_ERR("Error (%s): promisc mode set not supported\n", name);
1751 return -1;
1752 }
1753
1754 if (odp_pktio_promisc_mode_set(pktio, true)) {
1755 ODPH_ERR("Error (%s): promisc mode enable failed\n", name);
1756 return -1;
1757 }
1758 }
1759
1760 odp_pktin_queue_param_init(&pktin_param);
1761
1762 if (test_options->direct_rx) {
1763 pktin_param.op_mode = ODP_PKTIO_OP_MT_UNSAFE;
1764 } else {
1768 }
1769
1770 pktin_param.num_queues = num_rx;
1771
1772 if (num_rx > 1) {
1773 pktin_param.hash_enable = 1;
1774 pktin_param.hash_proto.proto.ipv4_udp = 1;
1775 }
1776
1777 if (odp_pktin_queue_config(pktio, &pktin_param)) {
1778 ODPH_ERR("Error (%s): Pktin config failed.\n", name);
1779 return -1;
1780 }
1781
1782 odp_pktout_queue_param_init(&pktout_param);
1783 pktout_param.op_mode = ODP_PKTIO_OP_MT_UNSAFE;
1784 pktout_param.num_queues = num_tx;
1785
1786 if (odp_pktout_queue_config(pktio, &pktout_param)) {
1787 ODPH_ERR("Error (%s): Pktout config failed.\n", name);
1788 return -1;
1789 }
1790
1791 if (num_tx > 0) {
1792 odp_pktout_queue_t pktout[MAX_WORKERS];
1793
1794 if (odp_pktout_queue(pktio, pktout, num_tx) != num_tx) {
1795 ODPH_ERR("Error (%s): Pktout queue request failed.\n", name);
1796 return -1;
1797 }
1798
1799 for (j = 0; j < num_tx; j++)
1800 global->pktio[i].pktout[j] = pktout[j];
1801 }
1802
1803 if (num_rx > 0 && test_options->direct_rx) {
1804 odp_pktin_queue_t pktin[MAX_WORKERS];
1805
1806 if (odp_pktin_queue(pktio, pktin, num_rx) != num_rx) {
1807 ODPH_ERR("Error (%s): Pktin queue request failed.\n", name);
1808 return -1;
1809 }
1810
1811 for (j = 0; j < num_rx; j++)
1812 global->pktio[i].pktin[j] = pktin[j];
1813 }
1814 }
1815
1816 return 0;
1817}
1818
1819static int start_pktios(test_global_t *global)
1820{
1821 uint32_t i;
1822 test_options_t *test_options = &global->test_options;
1823 uint32_t num_pktio = test_options->num_pktio;
1824 odp_pktio_t pktios[MAX_PKTIOS];
1825 const char *names[MAX_PKTIOS];
1826
1827 for (i = 0; i < num_pktio; i++) {
1828 if (odp_pktio_start(global->pktio[i].pktio)) {
1829 ODPH_ERR("Error (%s): Pktio start failed.\n", test_options->pktio_name[i]);
1830
1831 return -1;
1832 }
1833
1834 global->pktio[i].started = 1;
1835 pktios[i] = global->pktio[i].pktio;
1836 names[i] = test_options->pktio_name[i];
1837 }
1838
1839 /* Wait until all links are up */
1840 if (test_options->wait_sec &&
1841 pktio_common_check_link_status_wait(pktios, names, num_pktio,
1842 test_options->wait_sec) == -1)
1843 return -1;
1844
1845 pktio_common_print_link_info_multi(pktios, names, num_pktio);
1846
1847 if (test_options->wait_start_sec)
1848 odp_time_wait_ns(test_options->wait_start_sec * ODP_TIME_SEC_IN_NS);
1849
1850 return 0;
1851}
1852
1853static int stop_pktios(test_global_t *global)
1854{
1855 uint32_t i;
1856 odp_pktio_t pktio;
1857 int ret = 0;
1858 test_options_t *test_options = &global->test_options;
1859 uint32_t num_pktio = test_options->num_pktio;
1860
1861 for (i = 0; i < num_pktio; i++) {
1862 pktio = global->pktio[i].pktio;
1863
1864 if (pktio == ODP_PKTIO_INVALID || global->pktio[i].started == 0)
1865 continue;
1866
1867 if (odp_pktio_stop(pktio)) {
1868 ODPH_ERR("Error (%s): Pktio stop failed.\n", test_options->pktio_name[i]);
1869 ret = -1;
1870 }
1871 }
1872
1873 return ret;
1874}
1875
1876static int close_pktios(test_global_t *global)
1877{
1878 uint32_t i;
1879 odp_pktio_t pktio;
1880 test_options_t *test_options = &global->test_options;
1881 uint32_t num_pktio = test_options->num_pktio;
1882 int ret = 0;
1883
1884 for (i = 0; i < num_pktio; i++) {
1885 pktio = global->pktio[i].pktio;
1886
1887 if (pktio == ODP_PKTIO_INVALID)
1888 continue;
1889
1890 if (global->pktio[i].lso_profile != ODP_LSO_PROFILE_INVALID &&
1891 odp_lso_profile_destroy(global->pktio[i].lso_profile)) {
1892 ODPH_ERR("Error (%s): LSO profile destroy failed.\n",
1893 test_options->pktio_name[i]);
1894 ret = -1;
1895 }
1896
1897 if (odp_pktio_close(pktio)) {
1898 ODPH_ERR("Error (%s): Pktio close failed.\n", test_options->pktio_name[i]);
1899 ret = -1;
1900 }
1901 }
1902
1903 return ret;
1904}
1905
1906static inline void get_timestamp(odp_packet_t pkt, uint32_t ts_off, rx_lat_data_t *lat_data,
1907 odp_time_t rx_ts)
1908{
1909 ts_data_t ts_data;
1910 uint64_t nsec;
1911
1912 if (odp_unlikely(odp_packet_copy_to_mem(pkt, ts_off, sizeof(ts_data), &ts_data) < 0 ||
1913 ts_data.magic != TS_MAGIC))
1914 return;
1915
1916 nsec = odp_time_diff_ns(rx_ts, ts_data.tx_ts);
1917
1918 if (nsec < lat_data->min)
1919 lat_data->min = nsec;
1920
1921 if (nsec > lat_data->max)
1922 lat_data->max = nsec;
1923
1924 lat_data->nsec += nsec;
1925 lat_data->packets++;
1926}
1927
1928static int rx_thread(void *arg)
1929{
1930 int i, thr, num;
1931 uint32_t exit_test;
1932 uint64_t bytes;
1933 odp_time_t t1, t2, exit_time;
1934 thread_arg_t *thread_arg = arg;
1935 test_global_t *global = thread_arg->global;
1936 int direct_rx = global->test_options.direct_rx;
1937 int periodic_stat = global->test_options.update_msec ? 1 : 0;
1938 int verbose = global->test_options.verbose;
1939 uint64_t rx_timeouts = 0;
1940 uint64_t rx_packets = 0;
1941 uint64_t rx_bytes = 0;
1942 uint64_t nsec = 0;
1943 int ret = 0;
1944 int clock_started = 0;
1945 int exit_timer_started = 0;
1946 int paused = 0;
1947 const int max_num = 32;
1948 int pktin = 0;
1949 int num_pktio = global->test_options.num_pktio;
1950 odp_pktin_queue_t pktin_queue[num_pktio];
1951 odp_packet_t pkt[max_num];
1952 uint32_t ts_off = global->test_options.calc_latency ? global->test_options.hdr_len : 0;
1953 odp_time_t rx_ts = ODP_TIME_NULL;
1954 rx_lat_data_t rx_lat_data = { .nsec = 0, .min = UINT64_MAX, .max = 0, .packets = 0 };
1955
1956 thr = odp_thread_id();
1957 global->stat[thr].thread_type = RX_THREAD;
1958
1959 if (direct_rx) {
1960 for (i = 0; i < num_pktio; i++)
1961 pktin_queue[i] = thread_arg->pktin[i];
1962 }
1963
1964 /* Start all workers at the same time */
1965 odp_barrier_wait(&global->barrier);
1966
1967 while (1) {
1968 if (direct_rx) {
1969 num = odp_pktin_recv(pktin_queue[pktin], pkt, max_num);
1970
1971 if (odp_unlikely(num < 0)) {
1972 ODPH_ERR("pktin (%i) recv failed: %i\n", pktin, num);
1973 ret = -1;
1974 num = 0;
1975 break;
1976 }
1977
1978 pktin++;
1979 if (pktin >= num_pktio)
1980 pktin = 0;
1981 } else {
1982 odp_event_t ev[max_num];
1983
1984 num = odp_schedule_multi_no_wait(NULL, ev, max_num);
1985
1986 if (num)
1987 odp_packet_from_event_multi(pkt, ev, num);
1988 }
1989
1990 if (ts_off && num)
1991 rx_ts = odp_time_global_strict();
1992
1993 exit_test = odp_atomic_load_u32(&global->exit_test);
1994 if (exit_test) {
1995 /* Wait 0.1 seconds for possible in flight packets sent by the tx threads */
1996 if (exit_timer_started == 0) {
1997 exit_time = odp_time_local();
1998 t2 = exit_time;
1999 exit_timer_started = 1;
2000 } else if (odp_time_diff_ns(odp_time_local(), exit_time) >
2001 ODP_TIME_SEC_IN_NS / 10) {
2002 if (direct_rx == 0 && paused == 0) {
2004 paused = 1;
2005 } else if (num == 0) {
2006 /* Exit main loop after (schedule paused and) no more
2007 * packets received */
2008 break;
2009 }
2010 }
2011 /* Use last received packet as stop time and don't increase rx_timeouts
2012 * counter since tx threads have already been stopped */
2013 if (num)
2014 t2 = odp_time_local();
2015 else
2016 continue;
2017 }
2018
2019 if (num == 0) {
2020 if (direct_rx == 0)
2021 rx_timeouts++;
2022
2023 continue;
2024 }
2025
2026 if (!clock_started) {
2027 t1 = odp_time_local();
2028 clock_started = 1;
2029 }
2030
2031 if (odp_unlikely(verbose)) {
2032 odp_spinlock_lock(&global->verbose_lock);
2033
2034 for (i = 0; i < num; i++)
2035 odp_packet_print(pkt[i]);
2036
2037 odp_spinlock_unlock(&global->verbose_lock);
2038 }
2039
2040 bytes = 0;
2041 for (i = 0; i < num; i++) {
2042 bytes += odp_packet_len(pkt[i]);
2043
2044 if (ts_off)
2045 get_timestamp(pkt[i], ts_off, &rx_lat_data, rx_ts);
2046 }
2047
2048 rx_packets += num;
2049 rx_bytes += bytes;
2050
2051 if (odp_unlikely(periodic_stat)) {
2052 /* All packets from the same queue are from the same pktio interface */
2053 int index = odp_packet_input_index(pkt[0]);
2054
2055 if (index >= 0) {
2056 int if_idx = global->if_from_pktio_idx[index];
2057
2058 global->stat[thr].pktio[if_idx].rx_packets += num;
2059 }
2060 }
2061
2062 odp_packet_free_multi(pkt, num);
2063 }
2064
2065 if (clock_started)
2066 nsec = odp_time_diff_ns(t2, t1);
2067
2068 /* Update stats*/
2069 global->stat[thr].time_nsec = nsec;
2070 global->stat[thr].rx_timeouts = rx_timeouts;
2071 global->stat[thr].rx_packets = rx_packets;
2072 global->stat[thr].rx_bytes = rx_bytes;
2073 global->stat[thr].rx_lat_nsec = rx_lat_data.nsec;
2074 global->stat[thr].rx_lat_min_nsec = rx_lat_data.min;
2075 global->stat[thr].rx_lat_max_nsec = rx_lat_data.max;
2076 global->stat[thr].rx_lat_packets = rx_lat_data.packets;
2077
2078 return ret;
2079}
2080
2081static void drain_scheduler(test_global_t *global)
2082{
2083 odp_event_t ev;
2084 uint64_t wait_time;
2085
2086 if (!global->test_options.num_rx)
2087 return;
2088
2090
2091 while ((ev = odp_schedule(NULL, wait_time)) != ODP_EVENT_INVALID) {
2092 global->drained++;
2093 odp_event_free(ev);
2094 }
2095}
2096
2097static void drain_direct_input(test_global_t *global)
2098{
2099 odp_pktin_queue_t pktin;
2100 odp_packet_t pkt;
2101 int i, j;
2102 int num_pktio = global->test_options.num_pktio;
2103 int num_rx = global->test_options.num_rx;
2104
2105 for (i = 0; i < num_pktio; i++) {
2106 for (j = 0; j < num_rx; j++) {
2107 pktin = global->pktio[i].pktin[j];
2108
2109 while (odp_pktin_recv(pktin, &pkt, 1) == 1) {
2110 global->drained++;
2111 odp_packet_free(pkt);
2112 }
2113 }
2114 }
2115}
2116
2117static inline uint8_t *copy_field(uint8_t *dst, uint8_t *src, uint32_t len)
2118{
2119 return (uint8_t *)memcpy(dst, src, len) + len;
2120}
2121
2122static int init_packets(test_global_t *global, int pktio,
2123 odp_packet_t packet[], uint32_t num, uint16_t seq)
2124{
2125 odp_packet_t pkt;
2126 uint32_t i, j, pkt_len, seg_len, payload_len, l2_len;
2127 void *data;
2128 uint8_t *u8;
2129 odph_ethhdr_t *eth;
2130 odph_ipv4hdr_t *ip;
2131 uint16_t tpid;
2132 test_options_t *test_options = &global->test_options;
2133 const int proto = test_options->l4_proto;
2134 uint32_t num_vlan = test_options->num_vlan;
2135 uint32_t num_custom_l3 = test_options->num_custom_l3;
2136 uint32_t hdr_len = test_options->hdr_len;
2137 uint16_t src_port = test_options->src_port;
2138 uint16_t dst_port = test_options->dst_port;
2139 uint32_t src_cnt = 0;
2140 uint32_t dst_cnt = 0;
2141 uint32_t tcp_seqnum = 0x1234;
2142 uint64_t value;
2143 odph_vlanhdr_t *vlan = NULL; /* Fixes bogus compiler warning */
2144 hdr_field_t *c_hdr = NULL;
2145
2146 if (num_vlan > MAX_VLANS)
2147 num_vlan = MAX_VLANS;
2148
2149 for (i = 0; i < num; i++) {
2150 pkt = packet[i];
2151 pkt_len = odp_packet_len(pkt);
2152 seg_len = odp_packet_seg_len(pkt);
2153 data = odp_packet_data(pkt);
2154 payload_len = pkt_len - hdr_len;
2155
2156 if (seg_len < hdr_len) {
2157 ODPH_ERR("Error: First segment too short %u\n", seg_len);
2158 return -1;
2159 }
2160
2161 /* Ethernet */
2162 eth = data;
2163 memcpy(eth->dst.addr, global->pktio[pktio].eth_dst.addr, 6);
2164 memcpy(eth->src.addr, global->pktio[pktio].eth_src.addr, 6);
2165 eth->type = odp_cpu_to_be_16(test_options->eth_type);
2166 l2_len = ODPH_ETHHDR_LEN;
2167 odp_packet_has_eth_set(pkt, 1);
2168
2169 /* VLAN(s) */
2170 if (num_vlan) {
2171 tpid = test_options->vlan[0].tpid;
2172 eth->type = odp_cpu_to_be_16(tpid);
2173
2174 if (tpid == ETH_TYPE_QINQ)
2176 else
2178 }
2179
2180 for (j = 0; j < num_vlan; j++) {
2181 vlan = (odph_vlanhdr_t *)((uint8_t *)data + l2_len);
2182 vlan->tci = odp_cpu_to_be_16(test_options->vlan[j].tci);
2183 if (j < num_vlan - 1) {
2184 tpid = test_options->vlan[j + 1].tpid;
2185 vlan->type = odp_cpu_to_be_16(tpid);
2186 }
2187
2188 l2_len += ODPH_VLANHDR_LEN;
2189 }
2190
2191 if (num_vlan)
2192 vlan->type = odp_cpu_to_be_16(test_options->eth_type);
2193
2194 odp_packet_l3_offset_set(pkt, l2_len);
2195
2196 /* L3 */
2197 if (num_custom_l3) {
2198 /* Custom */
2199 u8 = (uint8_t *)data + l2_len;
2200
2201 for (j = 0; j < num_custom_l3; j++) {
2202 c_hdr = &test_options->custom_l3.fields[j];
2203 value = bswap(c_hdr->value, c_hdr->len);
2204 u8 = copy_field(u8, (uint8_t *)&value, c_hdr->len);
2205 c_hdr->value += c_hdr->diff;
2206 }
2207 } else {
2208 /* IPv4 */
2209 ip = (odph_ipv4hdr_t *)((uint8_t *)data + l2_len);
2210 memset(ip, 0, ODPH_IPV4HDR_LEN);
2211 ip->ver_ihl = ODPH_IPV4 << 4 | ODPH_IPV4HDR_IHL_MIN;
2212 ip->tot_len = odp_cpu_to_be_16(pkt_len - l2_len);
2213 ip->id = odp_cpu_to_be_16(seq + i);
2214 ip->ttl = 64;
2215 /* Use experimental protocol number if L4 proto is none. */
2216 ip->proto = proto == L4_PROTO_UDP ?
2217 ODPH_IPPROTO_UDP : proto == L4_PROTO_TCP ?
2218 ODPH_IPPROTO_TCP : 0xFE;
2219 ip->src_addr = odp_cpu_to_be_32(test_options->ipv4_src);
2220 ip->dst_addr = odp_cpu_to_be_32(test_options->ipv4_dst);
2221 ip->chksum = ~odp_chksum_ones_comp16(ip, ODPH_IPV4HDR_LEN);
2223
2224 u8 = ((uint8_t *)data + l2_len + ODPH_IPV4HDR_LEN);
2225 }
2226
2227 odp_packet_l4_offset_set(pkt, l2_len + test_options->l3_len);
2228
2229 if (proto == L4_PROTO_TCP) {
2230 odph_tcphdr_t *tcp = (odph_tcphdr_t *)u8;
2231
2232 memset(tcp, 0, ODPH_TCPHDR_LEN);
2233 tcp->src_port = odp_cpu_to_be_16(src_port);
2234 tcp->dst_port = odp_cpu_to_be_16(dst_port);
2235 tcp->seq_no = odp_cpu_to_be_32(tcp_seqnum);
2236 tcp->ack_no = odp_cpu_to_be_32(0x12345678);
2237 tcp->window = odp_cpu_to_be_16(0x4000);
2238 tcp->hl = 5;
2239 tcp->ack = 1;
2240 tcp_seqnum += payload_len;
2241 odp_packet_has_tcp_set(pkt, 1);
2242 } else if (proto == L4_PROTO_UDP) {
2243 odph_udphdr_t *udp = (odph_udphdr_t *)u8;
2244
2245 memset(udp, 0, ODPH_UDPHDR_LEN);
2246 udp->src_port = odp_cpu_to_be_16(src_port);
2247 udp->dst_port = odp_cpu_to_be_16(dst_port);
2248 udp->length = odp_cpu_to_be_16(payload_len + ODPH_UDPHDR_LEN);
2249 udp->chksum = 0;
2250 odp_packet_has_udp_set(pkt, 1);
2251 }
2252
2253 u8 = data;
2254 u8 += hdr_len;
2255
2256 if (test_options->fill_pl) {
2257 /* Init payload until the end of the first segment */
2258 for (j = 0; j < seg_len - hdr_len; j++)
2259 u8[j] = j;
2260 }
2261
2262 /* Insert checksum (TCP checksum is updated before TX) */
2263 if (proto == L4_PROTO_UDP && !test_options->calc_latency && test_options->calc_cs)
2264 odph_udp_chksum_set(pkt);
2265
2266 /* Increment port numbers */
2267 if (test_options->c_mode.src_port) {
2268 src_cnt++;
2269 if (src_cnt < test_options->c_mode.src_port) {
2270 src_port++;
2271 } else {
2272 src_port = test_options->src_port;
2273 src_cnt = 0;
2274 }
2275 }
2276 if (test_options->c_mode.dst_port) {
2277 dst_cnt++;
2278 if (dst_cnt < test_options->c_mode.dst_port) {
2279 dst_port++;
2280 } else {
2281 dst_port = test_options->dst_port;
2282 dst_cnt = 0;
2283 }
2284 }
2285 }
2286
2287 return 0;
2288}
2289
2290static inline void update_tcp_hdr(odp_packet_t pkt, odp_packet_t base_pkt, uint32_t hdr_len,
2291 odp_bool_t calc_cs)
2292{
2293 odph_tcphdr_t *tcp = odp_packet_l4_ptr(pkt, NULL);
2294 odph_tcphdr_t *tcp_base = odp_packet_l4_ptr(base_pkt, NULL);
2295 uint32_t prev_seqnum = odp_be_to_cpu_32(tcp_base->seq_no);
2296
2297 tcp->seq_no = odp_cpu_to_be_32(prev_seqnum + (odp_packet_len(pkt) - hdr_len));
2298
2299 /* Last used sequence number is stored in the base packet */
2300 tcp_base->seq_no = tcp->seq_no;
2301
2302 if (calc_cs)
2303 odph_tcp_chksum_set(pkt);
2304}
2305
2306static inline int update_rand_data(uint8_t *data, uint32_t data_len)
2307{
2308 uint32_t generated = 0;
2309 uint32_t retries = 0;
2310
2311 while (generated < data_len) {
2312 int32_t ret = odp_random_data(data, data_len - generated, ODP_RANDOM_BASIC);
2313
2314 if (odp_unlikely(ret < 0)) {
2315 ODPH_ERR("Error: odp_random_data() failed: %" PRId32 "\n", ret);
2316 return -1;
2317 } else if (odp_unlikely(ret == 0)) {
2318 retries++;
2319 if (odp_unlikely(retries > MAX_RAND_RETRIES)) {
2320 ODPH_ERR("Error: Failed to create random data\n");
2321 return -1;
2322 }
2323 continue;
2324 }
2325 data += ret;
2326 generated += ret;
2327 }
2328 return 0;
2329}
2330
2331static int init_global_data(test_global_t *global)
2332{
2333 memset(global, 0, sizeof(test_global_t));
2334 odp_atomic_init_u32(&global->exit_test, 0);
2335 odp_spinlock_init(&global->verbose_lock);
2336 global->thread_arg_shm = ODP_SHM_INVALID;
2337
2338 for (int i = 0; i < MAX_THREADS; i++) {
2339 uint8_t *rand_data = (uint8_t *)global->rand_data[i];
2340
2341 if (odp_unlikely(update_rand_data(rand_data, RAND_16BIT_WORDS * 2)))
2342 return -1;
2343 }
2344 return 0;
2345}
2346
2347static int create_thread_args(test_global_t *global)
2348{
2349 uint32_t i;
2350 uint32_t num_cpu = global->test_options.num_cpu;
2351 uint64_t size = num_cpu * sizeof(thread_arg_t);
2352 odp_shm_t shm;
2353
2354 shm = odp_shm_reserve("packet_gen_thread_arg", size, ODP_CACHE_LINE_SIZE, 0);
2355 if (shm == ODP_SHM_INVALID) {
2356 ODPH_ERR("Error: Thread arg SHM reserve failed.\n");
2357 return -1;
2358 }
2359
2360 global->thread_arg_shm = shm;
2361 global->thread_arg = odp_shm_addr(shm);
2362 if (global->thread_arg == NULL) {
2363 ODPH_ERR("Error: Thread arg SHM addr failed.\n");
2364 return -1;
2365 }
2366
2367 memset(global->thread_arg, 0, size);
2368
2369 for (i = 0; i < num_cpu; i++)
2370 global->thread_arg[i].global = global;
2371
2372 return 0;
2373}
2374
2375static int destroy_thread_args(test_global_t *global)
2376{
2377 if (global->thread_arg_shm != ODP_SHM_INVALID && odp_shm_free(global->thread_arg_shm)) {
2378 ODPH_ERR("Error: Thread arg SHM free failed.\n");
2379 return -1;
2380 }
2381
2382 return 0;
2383}
2384
2385static inline void set_timestamp(odp_packet_t pkt, uint32_t ts_off)
2386{
2387 const ts_data_t ts_data = { .magic = TS_MAGIC, .tx_ts = odp_time_global_strict() };
2388
2389 (void)odp_packet_copy_from_mem(pkt, ts_off, sizeof(ts_data), &ts_data);
2390}
2391
2392static int alloc_packets(odp_pool_t pool, odp_packet_t *pkt_tbl, uint32_t num,
2393 test_global_t *global)
2394{
2395 uint32_t i, pkt_len;
2396 test_options_t *test_options = &global->test_options;
2397 uint32_t num_bins = global->num_bins;
2398
2399 pkt_len = test_options->pkt_len;
2400
2401 for (i = 0; i < num; i++) {
2402 if (num_bins)
2403 pkt_len = global->len_bin[i % num_bins];
2404
2405 pkt_tbl[i] = odp_packet_alloc(pool, pkt_len);
2406 if (pkt_tbl[i] == ODP_PACKET_INVALID) {
2407 ODPH_ERR("Error: Alloc of %uB packet failed\n", pkt_len);
2408 break;
2409 }
2410 }
2411
2412 if (i == 0)
2413 return -1;
2414
2415 if (i != num) {
2416 odp_packet_free_multi(pkt_tbl, i);
2417 return -1;
2418 }
2419
2420 return 0;
2421}
2422
2423static inline uint32_t form_burst(odp_packet_t out_pkt[], uint32_t burst_size, uint32_t num_bins,
2424 uint32_t burst, odp_packet_t *pkt_tbl, odp_pool_t pool,
2425 int tx_mode, odp_bool_t calc_latency, uint32_t hdr_len,
2426 odp_bool_t calc_cs, uint64_t *total_bytes, uint8_t l4_proto,
2427 const uint16_t *rand_data)
2428{
2429 uint32_t i, idx;
2430 odp_packet_t pkt;
2431 static __thread int rand_idx;
2432 uint64_t bytes = 0;
2433
2434 idx = burst * burst_size;
2435 if (num_bins)
2436 idx = burst * burst_size * num_bins;
2437
2438 for (i = 0; i < burst_size; i++) {
2439 if (num_bins) {
2440 uint32_t bin;
2441
2442 if (odp_unlikely(rand_idx >= RAND_16BIT_WORDS))
2443 rand_idx = 0;
2444
2445 /* Select random length bin */
2446 bin = rand_data[rand_idx++] % num_bins;
2447 pkt = pkt_tbl[idx + bin];
2448 idx += num_bins;
2449 } else {
2450 pkt = pkt_tbl[idx];
2451 idx++;
2452 }
2453
2454 if (tx_mode == TX_MODE_DF) {
2455 out_pkt[i] = pkt;
2456 } else if (tx_mode == TX_MODE_REF) {
2457 out_pkt[i] = odp_packet_ref_static(pkt);
2458
2459 if (odp_unlikely(out_pkt[i] == ODP_PACKET_INVALID))
2460 break;
2461 } else {
2462 out_pkt[i] = odp_packet_copy(pkt, pool);
2463
2464 if (odp_unlikely(out_pkt[i] == ODP_PACKET_INVALID))
2465 break;
2466
2467 if (calc_latency)
2468 set_timestamp(out_pkt[i], hdr_len);
2469
2470 if (l4_proto == L4_PROTO_TCP)
2471 update_tcp_hdr(out_pkt[i], pkt, hdr_len, calc_cs);
2472 else if (l4_proto == L4_PROTO_UDP && calc_latency && calc_cs)
2473 odph_udp_chksum_set(out_pkt[i]);
2474 }
2475
2476 bytes += odp_packet_len(out_pkt[i]);
2477 }
2478
2479 *total_bytes = bytes;
2480
2481 return i;
2482}
2483
2484static inline int send_burst(odp_pktout_queue_t pktout, odp_packet_t pkt[],
2485 uint32_t num, int tx_mode, uint64_t *drop_bytes,
2486 const odp_packet_lso_opt_t *lso_opt ODP_UNUSED)
2487{
2488 int ret;
2489 uint32_t sent;
2490 uint64_t bytes = 0;
2491
2492 ret = odp_pktout_send(pktout, pkt, num);
2493
2494 sent = ret;
2495 if (odp_unlikely(ret < 0))
2496 sent = 0;
2497
2498 if (odp_unlikely(sent != num)) {
2499 uint32_t i;
2500 uint32_t num_drop = num - sent;
2501
2502 for (i = sent; i < num; i++)
2503 bytes += odp_packet_len(pkt[i]);
2504
2505 if (tx_mode != TX_MODE_DF)
2506 odp_packet_free_multi(&pkt[sent], num_drop);
2507 }
2508
2509 *drop_bytes = bytes;
2510
2511 return ret;
2512}
2513
2514static inline int send_burst_lso(odp_pktout_queue_t pktout, odp_packet_t pkt[], uint32_t num,
2515 int tx_mode ODP_UNUSED, uint64_t *drop_bytes,
2516 const odp_packet_lso_opt_t *lso_opt)
2517{
2518 int ret;
2519 uint32_t sent;
2520 uint64_t bytes = 0;
2521
2522 ret = odp_pktout_send_lso(pktout, pkt, num, lso_opt);
2523
2524 sent = ret;
2525 if (odp_unlikely(ret < 0))
2526 sent = 0;
2527
2528 if (odp_unlikely(sent != num)) {
2529 uint32_t i;
2530 uint32_t num_drop = num - sent;
2531
2532 for (i = sent; i < num; i++)
2533 bytes += odp_packet_len(pkt[i]);
2534
2535 odp_packet_free_multi(&pkt[sent], num_drop);
2536 }
2537
2538 *drop_bytes = bytes;
2539
2540 return ret;
2541}
2542
2543static int tx_thread(void *arg)
2544{
2545 int i, tx_thr;
2546 uint32_t exit_test, num_alloc, j;
2547 odp_time_t t1, t2, next_tmo;
2548 uint64_t diff_ns, t1_nsec;
2549 odp_packet_t *pkt_tbl;
2550 thread_arg_t *thread_arg = arg;
2551 test_global_t *global = thread_arg->global;
2552 test_options_t *test_options = &global->test_options;
2553 int periodic_stat = test_options->update_msec ? 1 : 0;
2554 odp_pool_t pool = global->pool;
2555 uint64_t gap_nsec = test_options->gap_nsec;
2556 uint64_t quit = test_options->quit;
2557 uint64_t tx_timeouts = 0;
2558 uint64_t tx_bytes = 0;
2559 uint64_t tx_packets = 0;
2560 uint64_t tx_drops = 0;
2561 int ret = 0;
2562 const int thr = odp_thread_id();
2563 const uint32_t hdr_len = test_options->hdr_len;
2564 const uint32_t burst_size = test_options->burst_size;
2565 const uint32_t bursts = test_options->bursts;
2566 const uint32_t num_tx = test_options->num_tx;
2567 const uint16_t *rand_data = global->rand_data[thr];
2568 const uint8_t l4_proto = test_options->l4_proto;
2569 const int tx_mode = test_options->tx_mode;
2570 const odp_bool_t calc_cs = test_options->calc_cs;
2571 const odp_bool_t calc_latency = test_options->calc_latency;
2572 int num_pktio = test_options->num_pktio;
2573 odp_pktout_queue_t pktout[num_pktio];
2574 odp_packet_lso_opt_t lso_opt[num_pktio];
2575 uint32_t tot_packets = 0;
2576 uint32_t num_bins = global->num_bins;
2577 const send_fn_t send_burst_fn = test_options->lso.enabled ? send_burst_lso : send_burst;
2578
2579 tx_thr = thread_arg->tx_thr;
2580 global->stat[thr].thread_type = TX_THREAD;
2581
2582 num_alloc = global->num_tx_pkt;
2583 if (num_bins)
2584 num_alloc = global->num_tx_pkt * num_bins;
2585
2586 for (i = 0; i < num_pktio; i++) {
2587 int seq = i * num_alloc;
2588
2589 pktout[i] = thread_arg->pktout[i];
2590 pkt_tbl = thread_arg->packet[i];
2591
2592 lso_opt[i].lso_profile = thread_arg->lso_profile[i];
2593 lso_opt[i].payload_offset = test_options->lso.payload_offset;
2594 lso_opt[i].max_payload_len = test_options->lso.max_payload_len;
2595
2596 if (alloc_packets(pool, pkt_tbl, num_alloc, global)) {
2597 ret = -1;
2598 break;
2599 }
2600
2601 tot_packets += num_alloc;
2602
2603 if (init_packets(global, i, pkt_tbl, num_alloc, seq)) {
2604 ret = -1;
2605 break;
2606 }
2607
2608 if (tx_mode == TX_MODE_DF) {
2609 for (j = 0; j < num_alloc; j++)
2610 odp_packet_free_ctrl_set(pkt_tbl[j],
2612 }
2613 }
2614
2615 /* Start all workers at the same time */
2616 odp_barrier_wait(&global->barrier);
2617
2618 t1 = odp_time_local();
2619
2620 /* Start TX burst at different per thread offset */
2621 t1_nsec = odp_time_to_ns(t1) + gap_nsec + (tx_thr * gap_nsec / num_tx);
2622
2623 while (ret == 0) {
2624 exit_test = odp_atomic_load_u32(&global->exit_test);
2625 if (exit_test)
2626 break;
2627
2628 if (quit && tx_timeouts >= quit) {
2629 odp_atomic_inc_u32(&global->exit_test);
2630 break;
2631 }
2632
2633 if (gap_nsec) {
2634 uint64_t nsec = t1_nsec + tx_timeouts * gap_nsec;
2635
2636 next_tmo = odp_time_local_from_ns(nsec);
2637 odp_time_wait_until(next_tmo);
2638 }
2639 tx_timeouts++;
2640
2641 /* Send bursts to each pktio */
2642 for (i = 0; i < num_pktio; i++) {
2643 uint32_t num;
2644 int sent;
2645 uint64_t total_bytes, drop_bytes;
2646 odp_packet_t pkt[burst_size];
2647
2648 pkt_tbl = thread_arg->packet[i];
2649
2650 for (j = 0; j < bursts; j++) {
2651 num = form_burst(pkt, burst_size, num_bins, j, pkt_tbl, pool,
2652 tx_mode, calc_latency, hdr_len, calc_cs,
2653 &total_bytes, l4_proto, rand_data);
2654
2655 if (odp_unlikely(num == 0)) {
2656 tx_drops += burst_size;
2657 continue;
2658 }
2659
2660 sent = send_burst_fn(pktout[i], pkt, num, tx_mode, &drop_bytes,
2661 &lso_opt[i]);
2662
2663 if (odp_unlikely(sent < 0)) {
2664 ret = -1;
2665 tx_drops += burst_size;
2666 break;
2667 }
2668
2669 tx_bytes += total_bytes - drop_bytes;
2670 tx_packets += sent;
2671 if (odp_unlikely(sent < (int)burst_size))
2672 tx_drops += burst_size - sent;
2673
2674 if (odp_unlikely(periodic_stat))
2675 global->stat[thr].pktio[i].tx_packets += sent;
2676 }
2677 }
2678 }
2679
2680 t2 = odp_time_local();
2681 diff_ns = odp_time_diff_ns(t2, t1);
2682
2683 for (i = 0; i < num_pktio; i++) {
2684 pkt_tbl = thread_arg->packet[i];
2685
2686 if (tot_packets == 0)
2687 break;
2688
2689 odp_packet_free_multi(pkt_tbl, num_alloc);
2690 tot_packets -= num_alloc;
2691 }
2692
2693 /* Update stats */
2694 global->stat[thr].time_nsec = diff_ns;
2695 global->stat[thr].tx_timeouts = tx_timeouts;
2696 global->stat[thr].tx_bytes = tx_bytes;
2697 global->stat[thr].tx_packets = tx_packets;
2698 global->stat[thr].tx_drops = tx_drops;
2699
2700 return ret;
2701}
2702
2703static int start_workers(test_global_t *global, odp_instance_t instance)
2704{
2705 odph_thread_common_param_t thr_common;
2706 int i, j, ret, tx_thr;
2707 test_options_t *test_options = &global->test_options;
2708 int num_pktio = test_options->num_pktio;
2709 int num_rx = test_options->num_rx;
2710 int num_cpu = test_options->num_cpu;
2711 odph_thread_param_t thr_param[num_cpu];
2712
2713 memset(global->thread_tbl, 0, sizeof(global->thread_tbl));
2714 odph_thread_common_param_init(&thr_common);
2715
2716 thr_common.instance = instance;
2717 thr_common.cpumask = &global->cpumask;
2718
2719 /* Receive threads */
2720 for (i = 0; i < num_rx; i++) {
2721 /* In direct mode, dedicate a pktin queue per pktio interface (per RX thread) */
2722 for (j = 0; test_options->direct_rx && j < num_pktio; j++)
2723 global->thread_arg[i].pktin[j] = global->pktio[j].pktin[i];
2724
2725 odph_thread_param_init(&thr_param[i]);
2726 thr_param[i].start = rx_thread;
2727 thr_param[i].arg = &global->thread_arg[i];
2728 thr_param[i].thr_type = ODP_THREAD_WORKER;
2729 }
2730
2731 /* Transmit threads */
2732 tx_thr = 0;
2733 for (i = num_rx; i < num_cpu; i++) {
2734 for (j = 0; j < num_pktio; j++) {
2735 odp_pktout_queue_t pktout;
2736
2737 global->thread_arg[i].tx_thr = tx_thr;
2738
2739 /* Dedicate a pktout queue per pktio interface
2740 * (per TX thread) */
2741 pktout = global->pktio[j].pktout[tx_thr];
2742 global->thread_arg[i].pktout[j] = pktout;
2743
2744 global->thread_arg[i].lso_profile[j] = global->pktio[j].lso_profile;
2745 }
2746
2747 odph_thread_param_init(&thr_param[i]);
2748 thr_param[i].start = tx_thread;
2749 thr_param[i].arg = &global->thread_arg[i];
2750 thr_param[i].thr_type = ODP_THREAD_WORKER;
2751 tx_thr++;
2752 }
2753
2754 ret = odph_thread_create(global->thread_tbl, &thr_common, thr_param,
2755 num_cpu);
2756
2757 if (ret != num_cpu) {
2758 ODPH_ERR("Error: thread create failed %i\n", ret);
2759 return -1;
2760 }
2761
2762 return 0;
2763}
2764
2765static void print_periodic_stat(test_global_t *global, uint64_t nsec)
2766{
2767 int i, j;
2768 int num_pktio = global->test_options.num_pktio;
2769 double sec = nsec / 1000000000.0;
2770 uint64_t num_tx[num_pktio];
2771 uint64_t num_rx[num_pktio];
2772
2773 for (i = 0; i < num_pktio; i++) {
2774 num_tx[i] = 0;
2775 num_rx[i] = 0;
2776
2777 for (j = 0; j < MAX_THREADS; j++) {
2778 if (global->stat[j].thread_type == RX_THREAD)
2779 num_rx[i] += global->stat[j].pktio[i].rx_packets;
2780 else if (global->stat[j].thread_type == TX_THREAD)
2781 num_tx[i] += global->stat[j].pktio[i].tx_packets;
2782 }
2783 }
2784 if (global->test_options.num_tx) {
2785 printf(" TX: %12.6fs", sec);
2786 for (i = 0; i < num_pktio; i++)
2787 printf(" %10" PRIu64 "", num_tx[i]);
2788 printf("\n");
2789 }
2790
2791 if (global->test_options.num_rx) {
2792 printf(" RX: %12.6fs", sec);
2793 for (i = 0; i < num_pktio; i++)
2794 printf(" %10" PRIu64 "", num_rx[i]);
2795 printf("\n");
2796 }
2797}
2798
2799static void periodic_print_loop(test_global_t *global)
2800{
2801 odp_time_t t1, t2;
2802 uint64_t nsec;
2803 int i;
2804 int num_pktio = global->test_options.num_pktio;
2805
2806 printf("\n\nPackets per interface\n");
2807 printf(" Dir Time");
2808 for (i = 0; i < num_pktio; i++)
2809 printf(" %10i", i);
2810
2811 printf("\n -----------------");
2812 for (i = 0; i < num_pktio; i++)
2813 printf("-----------");
2814
2815 printf("\n");
2816
2817 t1 = odp_time_local();
2818 while (odp_atomic_load_u32(&global->exit_test) == 0) {
2819 if (sigint_received) {
2820 odp_atomic_store_u32(&global->exit_test, 1);
2821 break;
2822 }
2823 usleep(1000 * global->test_options.update_msec);
2824 t2 = odp_time_local();
2825 nsec = odp_time_diff_ns(t2, t1);
2826 print_periodic_stat(global, nsec);
2827 }
2828}
2829
2830static void wait_for_exit(test_global_t *global)
2831{
2832 while (odp_atomic_load_u32(&global->exit_test) == 0) {
2833 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100 * 1000 * 1000 };
2834
2835 if (sigint_received) {
2836 odp_atomic_store_u32(&global->exit_test, 1);
2837 break;
2838 }
2839 /*
2840 * This gets interrupted by the signal. But the signal may have
2841 * already come.
2842 */
2843 nanosleep(&ts, NULL);
2844 }
2845}
2846
2847static void print_humanised_time(double time_nsec)
2848{
2849 if (time_nsec > ODP_TIME_SEC_IN_NS)
2850 printf("%.2f s\n", time_nsec / ODP_TIME_SEC_IN_NS);
2851 else if (time_nsec > ODP_TIME_MSEC_IN_NS)
2852 printf("%.2f ms\n", time_nsec / ODP_TIME_MSEC_IN_NS);
2853 else if (time_nsec > ODP_TIME_USEC_IN_NS)
2854 printf("%.2f us\n", time_nsec / ODP_TIME_USEC_IN_NS);
2855 else
2856 printf("%.0f ns\n", time_nsec);
2857}
2858
2859static void print_humanised_latency(double lat_nsec, double lat_min_nsec, double lat_max_nsec)
2860{
2861 printf(" rx ave packet latency: ");
2862 print_humanised_time(lat_nsec);
2863 printf(" rx min packet latency: ");
2864 print_humanised_time(lat_min_nsec);
2865 printf(" rx max packet latency: ");
2866 print_humanised_time(lat_max_nsec);
2867}
2868
2869static int print_final_stat(test_global_t *global)
2870{
2871 int i, num_thr;
2872 double rx_mbit_per_sec, tx_mbit_per_sec;
2873 test_options_t *test_options = &global->test_options;
2874 int num_rx = test_options->num_rx;
2875 int num_tx = test_options->num_tx;
2876 uint64_t rx_nsec_sum = 0;
2877 uint64_t rx_pkt_sum = 0;
2878 uint64_t rx_byte_sum = 0;
2879 uint64_t rx_tmo_sum = 0;
2880 uint64_t rx_lat_nsec_sum = 0;
2881 uint64_t rx_lat_min_nsec = UINT64_MAX;
2882 uint64_t rx_lat_max_nsec = 0;
2883 uint64_t rx_lat_pkt_sum = 0;
2884 uint64_t tx_nsec_sum = 0;
2885 uint64_t tx_pkt_sum = 0;
2886 uint64_t tx_byte_sum = 0;
2887 uint64_t tx_drop_sum = 0;
2888 uint64_t tx_tmo_sum = 0;
2889 double rx_pkt_ave = 0.0;
2890 double rx_pkt_per_sec = 0.0;
2891 double rx_byte_per_sec = 0.0;
2892 double rx_pkt_len = 0.0;
2893 double rx_sec = 0.0;
2894 double rx_ave_lat_nsec = 0.0;
2895 double tx_pkt_per_sec = 0.0;
2896 double tx_byte_per_sec = 0.0;
2897 double tx_sec = 0.0;
2898
2899 printf("\nRESULTS PER THREAD\n");
2900 printf(" rx thread:\n");
2901 printf(" 1 2 3 4 5 6 7 8\n");
2902 printf(" ---------------------------------------------------------------------------------------\n");
2903 printf(" ");
2904
2905 num_thr = 0;
2906 for (i = 0; i < MAX_THREADS; i++) {
2907 if (global->stat[i].thread_type != RX_THREAD)
2908 continue;
2909
2910 if (num_thr && (num_thr % 8) == 0)
2911 printf("\n ");
2912
2913 printf("%10" PRIu64 " ", global->stat[i].rx_packets);
2914 num_thr++;
2915 }
2916
2917 printf("\n\n");
2918
2919 printf(" tx thread:\n");
2920 printf(" 1 2 3 4 5 6 7 8\n");
2921 printf(" ---------------------------------------------------------------------------------------\n");
2922 printf(" ");
2923
2924 num_thr = 0;
2925 for (i = 0; i < MAX_THREADS; i++) {
2926 if (global->stat[i].thread_type != TX_THREAD)
2927 continue;
2928
2929 if (num_thr && (num_thr % 8) == 0)
2930 printf("\n ");
2931
2932 printf("%10" PRIu64 " ", global->stat[i].tx_packets);
2933 num_thr++;
2934 }
2935
2936 printf("\n\n");
2937
2938 for (i = 0; i < MAX_THREADS; i++) {
2939 if (global->stat[i].thread_type == RX_THREAD) {
2940 rx_tmo_sum += global->stat[i].rx_timeouts;
2941 rx_pkt_sum += global->stat[i].rx_packets;
2942 rx_byte_sum += global->stat[i].rx_bytes;
2943 rx_nsec_sum += global->stat[i].time_nsec;
2944 rx_lat_nsec_sum += global->stat[i].rx_lat_nsec;
2945 rx_lat_pkt_sum += global->stat[i].rx_lat_packets;
2946
2947 if (global->stat[i].rx_lat_min_nsec < rx_lat_min_nsec)
2948 rx_lat_min_nsec = global->stat[i].rx_lat_min_nsec;
2949
2950 if (global->stat[i].rx_lat_max_nsec > rx_lat_max_nsec)
2951 rx_lat_max_nsec = global->stat[i].rx_lat_max_nsec;
2952 } else if (global->stat[i].thread_type == TX_THREAD) {
2953 tx_tmo_sum += global->stat[i].tx_timeouts;
2954 tx_pkt_sum += global->stat[i].tx_packets;
2955 tx_byte_sum += global->stat[i].tx_bytes;
2956 tx_drop_sum += global->stat[i].tx_drops;
2957 tx_nsec_sum += global->stat[i].time_nsec;
2958 }
2959 }
2960
2961 if (num_rx)
2962 rx_pkt_ave = (double)rx_pkt_sum / num_rx;
2963 rx_sec = rx_nsec_sum / 1000000000.0;
2964 tx_sec = tx_nsec_sum / 1000000000.0;
2965
2966 /* Packets and bytes per thread per sec */
2967 if (rx_nsec_sum) {
2968 rx_pkt_per_sec = (1000000000.0 * (double)rx_pkt_sum) /
2969 (double)rx_nsec_sum;
2970
2971 rx_byte_per_sec = 1000000000.0;
2972 rx_byte_per_sec *= (rx_byte_sum + 24 * rx_pkt_sum);
2973 rx_byte_per_sec /= (double)rx_nsec_sum;
2974 }
2975
2976 if (tx_nsec_sum) {
2977 tx_pkt_per_sec = (1000000000.0 * (double)tx_pkt_sum) /
2978 (double)tx_nsec_sum;
2979
2980 tx_byte_per_sec = 1000000000.0;
2981 tx_byte_per_sec *= (tx_byte_sum + 24 * tx_pkt_sum);
2982 tx_byte_per_sec /= (double)tx_nsec_sum;
2983 }
2984
2985 /* Total Mbit/s */
2986 rx_mbit_per_sec = (num_rx * 8 * rx_byte_per_sec) / 1000000.0;
2987 tx_mbit_per_sec = (num_tx * 8 * tx_byte_per_sec) / 1000000.0;
2988
2989 if (rx_pkt_sum)
2990 rx_pkt_len = (double)rx_byte_sum / rx_pkt_sum;
2991
2992 if (rx_lat_pkt_sum)
2993 rx_ave_lat_nsec = (double)rx_lat_nsec_sum / rx_lat_pkt_sum;
2994
2995 printf("TOTAL (%i rx and %i tx threads)\n", num_rx, num_tx);
2996 printf(" rx timeouts: %" PRIu64 "\n", rx_tmo_sum);
2997 printf(" rx time spent (sec): %.3f\n", rx_sec);
2998 printf(" rx packets: %" PRIu64 "\n", rx_pkt_sum);
2999 printf(" rx packets drained: %" PRIu64 "\n", global->drained);
3000 printf(" rx packets per thr: %.1f\n", rx_pkt_ave);
3001 printf(" rx packets per thr per sec: %.1f\n", rx_pkt_per_sec);
3002 printf(" rx packets per sec: %.1f\n", num_rx * rx_pkt_per_sec);
3003 printf(" rx ave packet len: %.1f\n", rx_pkt_len);
3004
3005 if (rx_lat_pkt_sum)
3006 print_humanised_latency(rx_ave_lat_nsec, rx_lat_min_nsec, rx_lat_max_nsec);
3007
3008 printf(" rx Mbit/s: %.1f\n", rx_mbit_per_sec);
3009 printf("\n");
3010 printf(" tx timeouts: %" PRIu64 "\n", tx_tmo_sum);
3011 printf(" tx time spent (sec): %.3f\n", tx_sec);
3012 printf(" tx packets: %" PRIu64 "\n", tx_pkt_sum);
3013 printf(" tx dropped packets: %" PRIu64 "\n", tx_drop_sum);
3014 printf(" tx packets per thr per sec: %.1f\n", tx_pkt_per_sec);
3015 printf(" tx packets per sec: %.1f\n", num_tx * tx_pkt_per_sec);
3016 printf(" tx Mbit/s: %.1f\n", tx_mbit_per_sec);
3017 printf("\n");
3018
3019 if (rx_pkt_sum < MIN_RX_PACKETS_CI)
3020 return -1;
3021
3022 return 0;
3023}
3024
3025static void sig_handler(int signo)
3026{
3027 (void)signo;
3028
3029 sigint_received = 1;
3030}
3031
3032static int setup_sig_handler(void)
3033{
3034 struct sigaction action;
3035
3036 memset(&action, 0, sizeof(action));
3037 action.sa_handler = sig_handler;
3038
3039 if (sigemptyset(&action.sa_mask))
3040 return -1;
3041 if (sigaction(SIGINT, &action, NULL))
3042 return -1;
3043 return 0;
3044}
3045
3046int main(int argc, char **argv)
3047{
3048 odph_helper_options_t helper_options;
3049 odp_instance_t instance;
3050 odp_init_t init;
3051 test_global_t *global;
3052 odp_shm_t shm;
3053 sigset_t sigint_set;
3054 int ret = 0;
3055
3056 /*
3057 * SIGINT may be ignored when this program is run from a script, so
3058 * install the handler before anything else.
3059 */
3060 if (setup_sig_handler()) {
3061 ODPH_ERR("Error: sigaction() failed: %s\n", strerror(errno));
3062 return 1;
3063 }
3064
3065 /*
3066 * We want SIGINT to be delivered to this initial thread/process.
3067 * Block it now so that child threads/processes inherit the blocking.
3068 */
3069 if (sigemptyset(&sigint_set) || sigaddset(&sigint_set, SIGINT) ||
3070 pthread_sigmask(SIG_BLOCK, &sigint_set, NULL)) {
3071 ODPH_ERR("Error: blocking SIGINT failed.\n");
3072 return 1;
3073 }
3074
3075 /* Let helper collect its own arguments (e.g. --odph_proc) */
3076 argc = odph_parse_options(argc, argv);
3077 if (odph_options(&helper_options)) {
3078 ODPH_ERR("Error: reading ODP helper options failed.\n");
3079 exit(EXIT_FAILURE);
3080 }
3081
3082 /* List features not to be used */
3083 odp_init_param_init(&init);
3084 init.not_used.feat.cls = 1;
3085 init.not_used.feat.compress = 1;
3086 init.not_used.feat.crypto = 1;
3087 init.not_used.feat.dma = 1;
3088 init.not_used.feat.ipsec = 1;
3089 init.not_used.feat.ml = 1;
3090 init.not_used.feat.stash = 1;
3091 init.not_used.feat.timer = 1;
3092 init.not_used.feat.tm = 1;
3093
3094 init.mem_model = helper_options.mem_model;
3095
3096 /* Init ODP before calling anything else */
3097 if (odp_init_global(&instance, &init, NULL)) {
3098 ODPH_ERR("Error: Global init failed.\n");
3099 return 1;
3100 }
3101
3102 /* Init this thread */
3103 if (odp_init_local(instance, ODP_THREAD_CONTROL)) {
3104 ODPH_ERR("Error: Local init failed.\n");
3105 return 1;
3106 }
3107
3108 shm = odp_shm_reserve("packet_gen_global", sizeof(test_global_t),
3109 ODP_CACHE_LINE_SIZE, 0);
3110
3111 if (shm == ODP_SHM_INVALID) {
3112 ODPH_ERR("Error: SHM reserve failed.\n");
3113 return 1;
3114 }
3115
3116 global = odp_shm_addr(shm);
3117
3118 if (init_global_data(global)) {
3119 ret = 1;
3120 goto term;
3121 }
3122
3123 if (parse_options(argc, argv, global)) {
3124 ret = 1;
3125 goto term;
3126 }
3127
3128 if (create_thread_args(global)) {
3129 ret = 1;
3130 goto term;
3131 }
3132
3134
3135 /* Avoid all scheduler API calls in direct input mode */
3136 if (global->test_options.direct_rx == 0)
3137 odp_schedule_config(NULL);
3138
3139 if (set_num_cpu(global)) {
3140 ret = 1;
3141 goto term;
3142 }
3143
3144 if (create_packet_pool(global)) {
3145 ret = 1;
3146 goto term;
3147 }
3148
3149 if (open_pktios(global)) {
3150 ret = 1;
3151 goto term;
3152 }
3153
3154 if (start_pktios(global)) {
3155 ret = 1;
3156 goto term;
3157 }
3158
3159 /* Start worker threads */
3160 start_workers(global, instance);
3161
3162 /* Wait until workers have started. */
3163 odp_barrier_wait(&global->barrier);
3164
3165 /*
3166 * Unblock SIGINT for this thread. If the signal is already pending,
3167 * it gets delivered now.
3168 */
3169 if (pthread_sigmask(SIG_UNBLOCK, &sigint_set, NULL)) {
3170 ODPH_ERR("Error: unblocking SIGINT failed.\n");
3171 odp_atomic_store_u32(&global->exit_test, 1);
3172 }
3173
3174 /* Periodic statistics printing */
3175 if (global->test_options.update_msec)
3176 periodic_print_loop(global);
3177 else
3178 wait_for_exit(global);
3179
3180 /* Wait workers to exit */
3181 odph_thread_join(global->thread_tbl,
3182 global->test_options.num_cpu);
3183
3184 if (stop_pktios(global))
3185 ret = 1;
3186
3187 if (global->test_options.direct_rx)
3188 drain_direct_input(global);
3189 else
3190 drain_scheduler(global);
3191
3192 if (close_pktios(global))
3193 ret = 1;
3194
3195 if (print_final_stat(global))
3196 ret = 2;
3197
3198term:
3199 if (destroy_packet_pool(global)) {
3200 ODPH_ERR("Error: destroy_packet_pool() failed.\n");
3201 return 1;
3202 }
3203
3204 if (destroy_thread_args(global)) {
3205 ODPH_ERR("Error: destroy_thread_args() failed.\n");
3206 return 1;
3207 }
3208
3209 if (odp_shm_free(shm)) {
3210 ODPH_ERR("Error: SHM free failed.\n");
3211 return 1;
3212 }
3213
3214 if (odp_term_local()) {
3215 ODPH_ERR("Error: term local failed.\n");
3216 return 1;
3217 }
3218
3219 if (odp_term_global(instance)) {
3220 ODPH_ERR("Error: term global failed.\n");
3221 return 1;
3222 }
3223
3224 return ret;
3225}
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_inc_u32(odp_atomic_u32_t *atom)
Increment atomic uint32 variable.
void odp_atomic_store_u32(odp_atomic_u32_t *atom, uint32_t val)
Store value to atomic uint32 variable.
void odp_barrier_init(odp_barrier_t *barr, int count)
Initialize barrier with thread count.
void odp_barrier_wait(odp_barrier_t *barr)
Synchronize thread execution on barrier.
#define ODP_PACKED
Defines type/struct to be packed.
#define ODP_ALIGNED_CACHE
Defines type/struct/variable to be cache line size aligned.
#define odp_unlikely(x)
Branch unlikely taken.
Definition spec/hints.h:64
odp_u16be_t odp_cpu_to_be_16(uint16_t cpu16)
Convert cpu native uint16_t to 16bit big endian.
#define ODP_UNUSED
Intentionally unused variables of functions.
Definition spec/hints.h:54
odp_u32be_t odp_cpu_to_be_32(uint32_t cpu32)
Convert cpu native uint32_t to 32bit big endian.
#define ODP_BIG_ENDIAN
Big endian byte order.
uint32_t odp_be_to_cpu_32(odp_u32be_t be32)
Convert 32bit big endian to cpu native uint32_t.
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.
int odp_cpumask_all_available(odp_cpumask_t *mask)
Report all the available CPUs.
void odp_cpumask_clr(odp_cpumask_t *mask, int cpu)
Remove CPU from mask.
void odp_event_free(odp_event_t event)
Free event.
#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.
#define ODP_STATIC_ASSERT(cond, msg)
Compile time assertion macro.
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_spinlock_lock(odp_spinlock_t *splock)
Acquire spin lock.
void odp_spinlock_init(odp_spinlock_t *splock)
Initialize spin lock.
void odp_spinlock_unlock(odp_spinlock_t *splock)
Release spin lock.
int odp_pktio_mac_addr(odp_pktio_t pktio, void *mac_addr, int size)
Get the default MAC address of a packet IO interface.
void odp_pktin_queue_param_init(odp_pktin_queue_param_t *param)
Initialize packet input queue parameters.
odp_lso_profile_t odp_lso_profile_create(odp_pktio_t pktio, const odp_lso_profile_param_t *param)
Create LSO profile.
void odp_pktio_param_init(odp_pktio_param_t *param)
Initialize pktio params.
int odp_pktio_promisc_mode(odp_pktio_t pktio)
Determine if promiscuous mode is enabled for a packet IO interface.
int odp_pktio_close(odp_pktio_t pktio)
Close a packet IO interface.
void odp_lso_profile_param_init(odp_lso_profile_param_t *param)
Initialize LSO profile parameters.
#define ODP_LSO_PROFILE_INVALID
Invalid LSO profile handle.
int odp_pktout_queue(odp_pktio_t pktio, odp_pktout_queue_t queues[], int num)
Direct packet output queues.
#define ODP_LSO_MAX_CUSTOM
Maximum number of custom LSO fields supported by ODP API.
int odp_pktio_maxlen_set(odp_pktio_t pktio, uint32_t maxlen_input, uint32_t maxlen_output)
Set maximum frame lengths.
int odp_pktio_promisc_mode_set(odp_pktio_t pktio, odp_bool_t enable)
Set promiscuous mode.
int odp_lso_profile_destroy(odp_lso_profile_t lso_profile)
Destroy LSO profile.
void odp_pktio_config_init(odp_pktio_config_t *config)
Initialize packet IO configuration options.
odp_lso_modify_t
LSO custom modification options.
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_pktout_send_lso(odp_pktout_queue_t queue, const odp_packet_t packet[], int num, const odp_packet_lso_opt_t *lso_opt)
Send packets with segmentation offload.
int odp_pktio_config(odp_pktio_t pktio, const odp_pktio_config_t *config)
Configure packet IO interface options.
void odp_pktio_print(odp_pktio_t pktio)
Print pktio info to the console.
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.
void odp_pktout_queue_param_init(odp_pktout_queue_param_t *param)
Initialize packet output queue parameters.
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_pktio_index(odp_pktio_t pktio)
Get pktio interface index.
int odp_pktio_capability(odp_pktio_t pktio, odp_pktio_capability_t *capa)
Query packet IO interface capabilities.
#define ODP_PKTIO_MAX_INDEX
Maximum packet IO interface index.
int odp_pktout_send(odp_pktout_queue_t queue, const odp_packet_t packets[], int num)
Send packets directly to an interface output queue.
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_LSO_PROTO_IPV4
LSO performs IPv4 fragmentation.
@ ODP_LSO_PROTO_TCP_IPV4
LSO performs TCP segmentation on top of IPv4.
@ ODP_LSO_PROTO_CUSTOM
Custom protocol.
@ ODP_PKTOUT_MODE_DIRECT
Direct packet output on the interface.
@ ODP_PKTOUT_MODE_DISABLED
Application will never send to this interface.
@ ODP_LSO_ADD_SEGMENT_NUM
Add current segment number.
@ ODP_LSO_WRITE_BITS
Write bits in the first, middle and last segment.
@ ODP_LSO_ADD_PAYLOAD_OFFSET
Add number of payload bytes in all previous segments.
@ ODP_LSO_ADD_PAYLOAD_LEN
Add number of payload bytes in the segment.
@ ODP_PKTIO_OP_MT_UNSAFE
Not multithread safe operation.
@ ODP_PKTIN_MODE_DIRECT
Direct packet input from the interface.
@ ODP_PKTIN_MODE_DISABLED
Application will never receive from this interface.
@ ODP_PKTIN_MODE_SCHED
Packet input through scheduler and scheduled event queues.
void odp_packet_from_event_multi(odp_packet_t pkt[], const odp_event_t ev[], int num)
Convert multiple packet events to packet handles.
void odp_packet_has_vlan_qinq_set(odp_packet_t pkt, int val)
Set flag for VLAN QinQ (stacked VLAN)
int odp_packet_l3_offset_set(odp_packet_t pkt, uint32_t offset)
Set layer 3 start offset.
int odp_packet_input_index(odp_packet_t pkt)
Packet input interface index.
uint32_t odp_packet_seg_len(odp_packet_t pkt)
Packet data length following the data pointer.
int odp_packet_num_segs(odp_packet_t pkt)
Number of segments.
odp_packet_t odp_packet_copy(odp_packet_t pkt, odp_pool_t pool)
Full copy of a packet.
void * odp_packet_data(odp_packet_t pkt)
Packet data pointer.
int odp_packet_l4_offset_set(odp_packet_t pkt, uint32_t offset)
Set layer 4 start offset.
void odp_packet_has_eth_set(odp_packet_t pkt, int val)
Set flag for Ethernet header.
uint32_t odp_packet_len(odp_packet_t pkt)
Packet data length.
odp_packet_t odp_packet_alloc(odp_pool_t pool, uint32_t len)
Allocate a packet from a packet pool.
void odp_packet_has_udp_set(odp_packet_t pkt, int val)
Set flag for UDP.
void odp_packet_free(odp_packet_t pkt)
Free packet.
void odp_packet_free_ctrl_set(odp_packet_t pkt, odp_packet_free_ctrl_t ctrl)
Set packet free control option.
void odp_packet_has_tcp_set(odp_packet_t pkt, int val)
Set flag for TCP.
int odp_packet_copy_from_mem(odp_packet_t pkt, uint32_t offset, uint32_t len, const void *src)
Copy data from memory to packet.
#define ODP_PACKET_INVALID
Invalid packet.
void odp_packet_free_multi(const odp_packet_t pkt[], int num)
Free multiple packets.
void odp_packet_print(odp_packet_t pkt)
Print packet debug information.
void * odp_packet_l4_ptr(odp_packet_t pkt, uint32_t *len)
Layer 4 start pointer.
int odp_packet_copy_to_mem(odp_packet_t pkt, uint32_t offset, uint32_t len, void *dst)
Copy data from packet to memory.
odp_packet_t odp_packet_ref_static(odp_packet_t pkt)
Create a static reference to a packet.
void odp_packet_has_vlan_set(odp_packet_t pkt, int val)
Set flag for VLAN.
void odp_packet_has_ipv4_set(odp_packet_t pkt, int val)
Set flag for IPv4.
@ ODP_PACKET_FREE_CTRL_DONT_FREE
Don't free packet after processing it.
@ ODP_PROTO_LAYER_ALL
All layers.
odp_pool_t odp_pool_create(const char *name, const odp_pool_param_t *param)
Create a pool.
int odp_pool_capability(odp_pool_capability_t *capa)
Query pool capabilities.
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()
#define ODP_POOL_INVALID
Invalid pool.
@ ODP_POOL_PACKET
Packet pool.
int32_t odp_random_data(uint8_t *buf, uint32_t len, odp_random_kind_t kind)
Generate random byte data.
@ ODP_RANDOM_BASIC
Basic random, presumably pseudo-random generated by SW.
int odp_schedule_multi_no_wait(odp_queue_t *from, odp_event_t events[], int num)
Schedule, do not wait for events.
#define ODP_SCHED_SYNC_PARALLEL
Parallel scheduled queues.
int odp_schedule_default_prio(void)
Default scheduling priority level.
void odp_schedule_pause(void)
Pause scheduling.
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.
#define ODP_SCHED_GROUP_ALL
Group of all threads.
int odp_shm_free(odp_shm_t shm)
Free a contiguous block of shared memory.
void * odp_shm_addr(odp_shm_t shm)
Shared memory block address.
#define ODP_SHM_INVALID
Invalid shared memory block.
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.
bool odp_bool_t
Boolean type.
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.
uint64_t odp_time_to_ns(odp_time_t time)
Convert time to nanoseconds.
#define ODP_TIME_SEC_IN_NS
A second in nanoseconds.
void odp_time_wait_until(odp_time_t time)
Wait until the specified (wall clock) time has been reached.
odp_time_t odp_time_local_from_ns(uint64_t ns)
Convert nanoseconds to local time.
void odp_time_wait_ns(uint64_t ns)
Wait the specified number of nanoseconds.
odp_time_t odp_time_local(void)
Current local time.
#define ODP_TIME_NULL
Zero time stamp.
uint64_t odp_time_local_res(void)
Local time resolution in hertz.
odp_time_t odp_time_global_strict(void)
Current global time (strict)
#define ODP_TIME_MSEC_IN_NS
A millisecond in nanoseconds.
#define ODP_TIME_USEC_IN_NS
A microsecond in nanoseconds.
uint64_t odp_time_diff_ns(odp_time_t t2, odp_time_t t1)
Time difference in nanoseconds.
The OpenDataPlane API.
Global initialization parameters.
odp_mem_model_t mem_model
Application memory model.
odp_feature_t not_used
Unused features.
uint32_t max_payload_len
Maximum payload length per an LSO generated packet (in bytes).
uint32_t max_packet_segments
Maximum number of segments in an input packet.
uint32_t max_segments
Maximum number of segments an LSO operation may create.
uint32_t max_profiles
Maximum number of LSO profiles.
uint32_t max_profiles_per_pktio
Maximum number of LSO profiles per packet IO interface.
uint16_t add_payload_len
ODP_LSO_ADD_PAYLOAD_LEN support.
uint8_t max_num_custom
Maximum number of custom fields supported per LSO profile.
uint16_t write_bits
ODP_LSO_WRITE_BITS support.
uint32_t tcp_ipv4
ODP_LSO_PROTO_TCP_IPV4 support.
uint16_t add_segment_num
ODP_LSO_ADD_SEGMENT_NUM support.
uint32_t ipv4
ODP_LSO_PROTO_IPV4 support.
uint32_t max_payload_offset
Maximum supported offset to the packet payload (in bytes).
struct odp_lso_capability_t::@113 mod_op
Supported LSO custom modification options.
struct odp_lso_capability_t::@114 proto
Supported LSO protocol options.
uint32_t custom
ODP_LSO_PROTO_CUSTOM support.
uint16_t add_payload_offset
ODP_LSO_ADD_PAYLOAD_OFFSET support.
Parameters for ODP_LSO_WRITE_BITS custom operation.
uint8_t value[1]
Value to be written using the mask: new_value[n] = (old_value[n] & ~mask[n]) | (value[n] & mask[n])
uint8_t mask[1]
Bitmask to select which bits to write.
odp_lso_profile_t lso_profile
LSO profile handle.
uint32_t payload_offset
LSO payload offset.
uint32_t max_payload_len
Maximum payload length in an LSO segment.
uint8_t static_ref
Static reference.
Packet input queue parameters.
uint32_t num_queues
Number of input queues to be created.
odp_pktio_op_mode_t op_mode
Operation mode.
odp_queue_param_t queue_param
Queue parameters.
odp_pktin_hash_proto_t hash_proto
Protocol field selection for hashing.
odp_bool_t hash_enable
Enable flow hashing.
struct odp_pktio_capability_t::@117 free_ctrl
Supported packet free control options.
odp_pktio_set_op_t set_op
Supported set operations.
uint32_t max_output
Maximum valid value for 'maxlen_output'.
uint32_t max_input_queues
Maximum number of input queues.
uint32_t max_input
Maximum valid value for 'maxlen_input'.
odp_pktio_config_t config
Supported pktio configuration options.
odp_packet_ref_types_t packet_ref
Supported packet reference types.
struct odp_pktio_capability_t::@115 maxlen
Supported frame lengths for odp_pktio_maxlen_set()
uint32_t min_output
Minimum valid value for 'maxlen_output'.
uint32_t max_output_queues
Maximum number of output queues.
uint32_t dont_free
Packet free control option ODP_PACKET_FREE_CTRL_DONT_FREE support with odp_packet_free_ctrl_set().
uint32_t min_input
Minimum valid value for 'maxlen_input'.
odp_lso_capability_t lso
LSO capabilities.
Packet IO configuration options.
odp_pktout_config_opt_t pktout
Packet output configuration options bit field.
odp_bool_t enable_lso
Enable Large Send Offload (LSO)
odp_pktio_parser_config_t parser
Packet input parser configuration.
odp_pktin_config_opt_t pktin
Packet input configuration options bit field.
Packet IO parameters.
odp_pktin_mode_t in_mode
Packet input mode.
odp_pktout_mode_t out_mode
Packet output mode.
odp_proto_layer_t layer
Protocol parsing level in packet input.
Packet output queue parameters.
odp_pktio_op_mode_t op_mode
Operation mode.
uint32_t num_queues
Number of output queues to be created.
uint32_t max_align
Maximum buffer data alignment in bytes.
uint32_t max_num
Maximum number of buffers of any size.
struct odp_pool_capability_t::@134 pkt
Packet pool capabilities
uint32_t max_seg_len
Maximum packet segment data length in bytes.
uint32_t max_len
Maximum packet data length in bytes.
uint32_t num
Number of buffers in the pool.
struct odp_pool_param_t::@139 pkt
Parameters for packet pools.
uint32_t align
Minimum buffer alignment in bytes.
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...
odp_schedule_param_t sched
Scheduler parameters.
odp_schedule_group_t group
Thread group.
odp_schedule_prio_t prio
Priority level.
odp_schedule_sync_t sync
Synchronization method.
uint32_t tm
Traffic Manager APIs, e.g., odp_tm_xxx()
uint32_t stash
Stash APIs, e.g., odp_stash_xxx()
uint32_t crypto
Crypto APIs, e.g., odp_crypto_xxx()
uint32_t ipsec
IPsec APIs, e.g., odp_ipsec_xxx()
uint32_t dma
DMA APIs, e.g., odp_dma_xxx()
uint32_t timer
Timer APIs, e.g., odp_timer_xxx(), odp_timeout_xxx()
uint32_t cls
Classifier APIs, e.g., odp_cls_xxx(), odp_cos_xxx()
uint32_t ml
Machine Learning APIs, e.g., odp_ml_xxx()
struct odp_feature_t::@174 feat
Individual feature bits.
uint32_t compress
Compression APIs, e.g., odp_comp_xxx()
uint64_t drop_udp_err
Drop packets with a UDP error on packet input.
struct odp_pktin_config_opt_t::@108 bit
Option flags.
uint64_t udp_chksum
Check UDP checksum on packet input.
uint64_t tcp_chksum
Check TCP checksum on packet input.
uint64_t drop_tcp_err
Drop packets with a TCP error on packet input.
struct odp_pktin_hash_proto_t::@107 proto
Protocol header fields for hashing.
uint32_t ipv4_udp
IPv4 addresses and UDP port numbers.
struct odp_pktio_set_op_t::@112 op
Operation flags.
uint32_t maxlen
Maximum frame length.
uint32_t promisc_mode
Promiscuous mode.
uint64_t tcp_chksum
Insert TCP checksum on packet by default.
struct odp_pktout_config_opt_t::@109 bit
Option flags for packet output.
uint64_t tcp_chksum_ena
Enable TCP checksum insertion.
uint64_t udp_chksum
Insert UDP checksum on packet by default.
uint64_t udp_chksum_ena
Enable UDP checksum insertion.