samples/bpf: xdpsock: Add cyclic TX operation capability

Tx cycle time is in micro-seconds unit. By combining the batch size (-b M)
and Tx cycle time (-T|--tx-cycle N), xdpsock now can transmit batch-size of
packets every N-us periodically. Cyclic TX operation is not applicable if
--poll mode is used.

To transmit 16 packets every 1ms cycle time for total of 100000 packets
silently:
 $ xdpsock -i eth0 -T -N -z -T 1000 -b 16 -C 100000

To print cyclic TX schedule variance stats, use --app-stats|-a:
 $ xdpsock -i eth0 -T -N -z -T 1000 -b 16 -C 100000 -a

 sock0@eth0:0 txonly xdp-drv
                   pps            pkts           0.00
rx                 0              0
tx                 0              100000

                   calls/s        count
rx empty polls     0              0
fill fail polls    0              0
copy tx sendtos    0              0
tx wakeup sendtos  0              6254
opt polls          0              0

                   period     min        ave        max        cycle
Cyclic TX          1000000    53507      75334      712642     6250

Signed-off-by: Ong Boon Leong <boon.leong.ong@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211230035447.523177-5-boon.leong.ong@intel.com
This commit is contained in:
Ong Boon Leong 2021-12-30 11:54:44 +08:00 committed by Alexei Starovoitov
parent 5a3882542a
commit fa0d27a1d5

View File

@ -63,12 +63,19 @@
#define VLAN_VID__DEFAULT 1 #define VLAN_VID__DEFAULT 1
#define VLAN_PRI__DEFAULT 0 #define VLAN_PRI__DEFAULT 0
#define NSEC_PER_SEC 1000000000UL
#define NSEC_PER_USEC 1000
typedef __u64 u64; typedef __u64 u64;
typedef __u32 u32; typedef __u32 u32;
typedef __u16 u16; typedef __u16 u16;
typedef __u8 u8; typedef __u8 u8;
static unsigned long prev_time; static unsigned long prev_time;
static long tx_cycle_diff_min;
static long tx_cycle_diff_max;
static double tx_cycle_diff_ave;
static long tx_cycle_cnt;
enum benchmark_type { enum benchmark_type {
BENCH_RXDROP = 0, BENCH_RXDROP = 0,
@ -115,6 +122,7 @@ static u32 prog_id;
static bool opt_busy_poll; static bool opt_busy_poll;
static bool opt_reduced_cap; static bool opt_reduced_cap;
static clockid_t opt_clock = CLOCK_MONOTONIC; static clockid_t opt_clock = CLOCK_MONOTONIC;
static unsigned long opt_tx_cycle_ns;
struct vlan_ethhdr { struct vlan_ethhdr {
unsigned char h_dest[6]; unsigned char h_dest[6];
@ -305,6 +313,15 @@ static void dump_app_stats(long dt)
xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos; xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos;
xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls; xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls;
} }
if (opt_tx_cycle_ns) {
printf("\n%-18s %-10s %-10s %-10s %-10s %-10s\n",
"", "period", "min", "ave", "max", "cycle");
printf("%-18s %-10lu %-10lu %-10lu %-10lu %-10lu\n",
"Cyclic TX", opt_tx_cycle_ns, tx_cycle_diff_min,
(long)(tx_cycle_diff_ave / tx_cycle_cnt),
tx_cycle_diff_max, tx_cycle_cnt);
}
} }
static bool get_interrupt_number(void) static bool get_interrupt_number(void)
@ -1001,6 +1018,7 @@ static struct option long_options[] = {
{"tx-vlan-pri", required_argument, 0, 'K'}, {"tx-vlan-pri", required_argument, 0, 'K'},
{"tx-dmac", required_argument, 0, 'G'}, {"tx-dmac", required_argument, 0, 'G'},
{"tx-smac", required_argument, 0, 'H'}, {"tx-smac", required_argument, 0, 'H'},
{"tx-cycle", required_argument, 0, 'T'},
{"extra-stats", no_argument, 0, 'x'}, {"extra-stats", no_argument, 0, 'x'},
{"quiet", no_argument, 0, 'Q'}, {"quiet", no_argument, 0, 'Q'},
{"app-stats", no_argument, 0, 'a'}, {"app-stats", no_argument, 0, 'a'},
@ -1047,6 +1065,7 @@ static void usage(const char *prog)
" -K, --tx-vlan-pri=n Tx VLAN Priority [0-7]. Default: %d (For -V|--tx-vlan)\n" " -K, --tx-vlan-pri=n Tx VLAN Priority [0-7]. Default: %d (For -V|--tx-vlan)\n"
" -G, --tx-dmac=<MAC> Dest MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n" " -G, --tx-dmac=<MAC> Dest MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n"
" -H, --tx-smac=<MAC> Src MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n" " -H, --tx-smac=<MAC> Src MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n"
" -T, --tx-cycle=n Tx cycle time in micro-seconds (For -t|--txonly).\n"
" -x, --extra-stats Display extra statistics.\n" " -x, --extra-stats Display extra statistics.\n"
" -Q, --quiet Do not display any stats.\n" " -Q, --quiet Do not display any stats.\n"
" -a, --app-stats Display application (syscall) statistics.\n" " -a, --app-stats Display application (syscall) statistics.\n"
@ -1069,7 +1088,7 @@ static void parse_command_line(int argc, char **argv)
opterr = 0; opterr = 0;
for (;;) { for (;;) {
c = getopt_long(argc, argv, "Frtli:q:pSNn:w:czf:muMd:b:C:s:P:VJ:K:G:H:xQaI:BR", c = getopt_long(argc, argv, "Frtli:q:pSNn:w:czf:muMd:b:C:s:P:VJ:K:G:H:T:xQaI:BR",
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -1183,6 +1202,10 @@ static void parse_command_line(int argc, char **argv)
usage(basename(argv[0])); usage(basename(argv[0]));
} }
break; break;
case 'T':
opt_tx_cycle_ns = atoi(optarg);
opt_tx_cycle_ns *= NSEC_PER_USEC;
break;
case 'x': case 'x':
opt_extra_stats = 1; opt_extra_stats = 1;
break; break;
@ -1388,7 +1411,7 @@ static void rx_drop_all(void)
} }
} }
static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size) static int tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
{ {
u32 idx; u32 idx;
unsigned int i; unsigned int i;
@ -1397,7 +1420,7 @@ static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
batch_size) { batch_size) {
complete_tx_only(xsk, batch_size); complete_tx_only(xsk, batch_size);
if (benchmark_done) if (benchmark_done)
return; return 0;
} }
for (i = 0; i < batch_size; i++) { for (i = 0; i < batch_size; i++) {
@ -1413,6 +1436,8 @@ static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
*frame_nb += batch_size; *frame_nb += batch_size;
*frame_nb %= NUM_FRAMES; *frame_nb %= NUM_FRAMES;
complete_tx_only(xsk, batch_size); complete_tx_only(xsk, batch_size);
return batch_size;
} }
static inline int get_batch_size(int pkt_cnt) static inline int get_batch_size(int pkt_cnt)
@ -1446,16 +1471,39 @@ static void tx_only_all(void)
{ {
struct pollfd fds[MAX_SOCKS] = {}; struct pollfd fds[MAX_SOCKS] = {};
u32 frame_nb[MAX_SOCKS] = {}; u32 frame_nb[MAX_SOCKS] = {};
unsigned long next_tx_ns = 0;
int pkt_cnt = 0; int pkt_cnt = 0;
int i, ret; int i, ret;
if (opt_poll && opt_tx_cycle_ns) {
fprintf(stderr,
"Error: --poll and --tx-cycles are both set\n");
return;
}
for (i = 0; i < num_socks; i++) { for (i = 0; i < num_socks; i++) {
fds[0].fd = xsk_socket__fd(xsks[i]->xsk); fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
fds[0].events = POLLOUT; fds[0].events = POLLOUT;
} }
if (opt_tx_cycle_ns) {
/* Align Tx time to micro-second boundary */
next_tx_ns = (get_nsecs() / NSEC_PER_USEC + 1) *
NSEC_PER_USEC;
next_tx_ns += opt_tx_cycle_ns;
/* Initialize periodic Tx scheduling variance */
tx_cycle_diff_min = 1000000000;
tx_cycle_diff_max = 0;
tx_cycle_diff_ave = 0.0;
}
while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) { while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
int batch_size = get_batch_size(pkt_cnt); int batch_size = get_batch_size(pkt_cnt);
struct timespec next;
int tx_cnt = 0;
long diff;
int err;
if (opt_poll) { if (opt_poll) {
for (i = 0; i < num_socks; i++) for (i = 0; i < num_socks; i++)
@ -1468,13 +1516,40 @@ static void tx_only_all(void)
continue; continue;
} }
for (i = 0; i < num_socks; i++) if (opt_tx_cycle_ns) {
tx_only(xsks[i], &frame_nb[i], batch_size); next.tv_sec = next_tx_ns / NSEC_PER_SEC;
next.tv_nsec = next_tx_ns % NSEC_PER_SEC;
err = clock_nanosleep(opt_clock, TIMER_ABSTIME, &next, NULL);
if (err) {
if (err != EINTR)
fprintf(stderr,
"clock_nanosleep failed. Err:%d errno:%d\n",
err, errno);
break;
}
pkt_cnt += batch_size; /* Measure periodic Tx scheduling variance */
diff = get_nsecs() - next_tx_ns;
if (diff < tx_cycle_diff_min)
tx_cycle_diff_min = diff;
if (diff > tx_cycle_diff_max)
tx_cycle_diff_max = diff;
tx_cycle_diff_ave += (double)diff;
tx_cycle_cnt++;
}
for (i = 0; i < num_socks; i++)
tx_cnt += tx_only(xsks[i], &frame_nb[i], batch_size);
pkt_cnt += tx_cnt;
if (benchmark_done) if (benchmark_done)
break; break;
if (opt_tx_cycle_ns)
next_tx_ns += opt_tx_cycle_ns;
} }
if (opt_pkt_count) if (opt_pkt_count)