Skip to content

Commit

Permalink
xdp-bench: Use basic_program_mode enum in BPF code as well
Browse files Browse the repository at this point in the history
Instead of converting the basic_program_mode enum into boolean variables,
just use it directly in the BPF code. This makes it easier to add new modes
in the future, as the boolean variables quickly explodes. Move the struct
definition itself into its own shared header file so it's kept in sync.

This includes a small functional change in that we no longer perform the
"touch data" action when we're swapping MACs. However, since swapping MACs
touches the same part of the packet data, the performance impact of this
should be negligible (it's just a single inline branch that's being
removed).

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
  • Loading branch information
tohojo committed Jun 8, 2023
1 parent 870db97 commit 7fa5ef3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion xdp-bench/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST_FILE := tests/test-xdp-bench.sh
USER_TARGETS := xdp-bench
USER_EXTRA_C := xdp_redirect_basic.c xdp_redirect_cpumap.c xdp_redirect_devmap.c \
xdp_redirect_devmap_multi.c xdp_basic.c
EXTRA_USER_DEPS := xdp-bench.h
EXTRA_USER_DEPS := xdp-bench.h xdp_basic.shared.h

LIB_DIR = ../lib

Expand Down
8 changes: 2 additions & 6 deletions xdp-bench/xdp-bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "params.h"
#include "util.h"

#include "xdp_basic.shared.h"

#define MAX_IFACE_NUM 32

int do_drop(const void *cfg, const char *pin_root_path);
Expand All @@ -16,12 +18,6 @@ int do_redirect_cpumap(const void *cfg, const char *pin_root_path);
int do_redirect_devmap(const void *cfg, const char *pin_root_path);
int do_redirect_devmap_multi(const void *cfg, const char *pin_root_path);

enum basic_program_mode {
BASIC_NO_TOUCH,
BASIC_READ_DATA,
BASIC_SWAP_MACS,
};

struct basic_opts {
bool extended;
bool rxq_stats;
Expand Down
18 changes: 12 additions & 6 deletions xdp-bench/xdp_basic.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
#include <xdp/xdp_sample_common.bpf.h>
#include <xdp/parsing_helpers.h>

const volatile bool read_data = 0;
const volatile bool swap_macs = 0;
#include "xdp_basic.shared.h"

const volatile enum basic_program_mode prog_mode = BASIC_NO_TOUCH;
const volatile bool rxq_stats = 0;
const volatile enum xdp_action action = XDP_DROP;

Expand Down Expand Up @@ -48,12 +49,17 @@ int xdp_basic_prog(struct xdp_md *ctx)
NO_TEAR_INC(rxq_rec->processed);
}

if (read_data) {
switch (prog_mode) {
case BASIC_READ_DATA:
if (bpf_ntohs(eth->h_proto) < ETH_P_802_3_MIN)
return XDP_ABORTED;

if (swap_macs)
swap_src_dst_mac(data);
break;
case BASIC_SWAP_MACS:
swap_src_dst_mac(data);
break;
case BASIC_NO_TOUCH:
default:
break;
}

if (action == XDP_DROP) {
Expand Down
5 changes: 1 addition & 4 deletions xdp-bench/xdp_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ static int do_basic(const struct basic_opts *opt, enum xdp_action action)
if (action == XDP_DROP)
mask |= SAMPLE_DROP_OK;

if (opt->program_mode >= BASIC_READ_DATA)
skel->rodata->read_data = true;
if (opt->program_mode >= BASIC_SWAP_MACS)
skel->rodata->swap_macs = true;
skel->rodata->prog_mode = opt->program_mode;
if (opt->rxq_stats) {
skel->rodata->rxq_stats = true;
mask |= SAMPLE_RXQ_STATS;
Expand Down
13 changes: 13 additions & 0 deletions xdp-bench/xdp_basic.shared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-2.0-only

#ifndef _XDP_BASIC_SHARED_H
#define _XDP_BASIC_SHARED_H

enum basic_program_mode {
BASIC_NO_TOUCH,
BASIC_READ_DATA,
BASIC_PARSE_IPHDR,
BASIC_SWAP_MACS,
};

#endif

0 comments on commit 7fa5ef3

Please sign in to comment.