Skip to content

Commit

Permalink
xdp-bench: Add parse-ip packet operation to basic actions
Browse files Browse the repository at this point in the history
Add a 'parse-ip' packet operation to the basic actions (for drop, pass and
tx commands). This will parse the packet all the way down to the IP header
instead of just touching a single field in the ethernet header like the
'touch' operation does.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
  • Loading branch information
tohojo committed Jun 8, 2023
1 parent 7fa5ef3 commit b43595e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions xdp-bench/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ following actions are available:
#+begin_src sh
no-touch - Drop the packet without touching the packet data
touch - Read a field in the packet header before dropping
parse-ip - Parse the IP header field before dropping
swap-macs - Swap the source and destination MAC addresses before dropping
#+end_src

Expand Down Expand Up @@ -130,6 +131,7 @@ following actions are available:
#+begin_src sh
no-touch - Pass the packet without touching the packet data
touch - Read a field in the packet header before passing
parse-ip - Parse the IP header field before passing
swap-macs - Swap the source and destination MAC addresses before passing
#+end_src

Expand Down Expand Up @@ -189,6 +191,7 @@ following actions are available:
#+begin_src sh
no-touch - Transmit the packet without touching the packet data
touch - Read a field in the packet header before transmitting
parse-ip - Parse the IP header field before transmitting
swap-macs - Swap the source and destination MAC addresses before transmitting
#+end_src

Expand Down
1 change: 1 addition & 0 deletions xdp-bench/tests/test-xdp-bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test_basic()
export XDP_SAMPLE_IMMEDIATE_EXIT=1
check_run $XDP_BENCH $action $NS -vv
check_run $XDP_BENCH $action $NS -p read-data -vv
check_run $XDP_BENCH $action $NS -p parse-ip -vv
check_run $XDP_BENCH $action $NS -p swap-macs -vv
check_run $XDP_BENCH $action $NS -m skb -vv
check_run $XDP_BENCH $action $NS -e -vv
Expand Down
3 changes: 3 additions & 0 deletions xdp-bench/xdp-bench.8
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ following actions are available:
.nf
\fCno-touch - Drop the packet without touching the packet data
touch - Read a field in the packet header before dropping
parse-ip - Parse the IP header field before dropping
swap-macs - Swap the source and destination MAC addresses before dropping
\fP
.fi
Expand Down Expand Up @@ -157,6 +158,7 @@ following actions are available:
.nf
\fCno-touch - Pass the packet without touching the packet data
touch - Read a field in the packet header before passing
parse-ip - Parse the IP header field before passing
swap-macs - Swap the source and destination MAC addresses before passing
\fP
.fi
Expand Down Expand Up @@ -233,6 +235,7 @@ following actions are available:
.nf
\fCno-touch - Transmit the packet without touching the packet data
touch - Read a field in the packet header before transmitting
parse-ip - Parse the IP header field before transmitting
swap-macs - Swap the source and destination MAC addresses before transmitting
\fP
.fi
Expand Down
1 change: 1 addition & 0 deletions xdp-bench/xdp-bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct enum_val xdp_modes[] = {
struct enum_val basic_program_modes[] = {
{"no-touch", BASIC_NO_TOUCH},
{"read-data", BASIC_READ_DATA},
{"parse-ip", BASIC_PARSE_IPHDR},
{"swap-macs", BASIC_SWAP_MACS},
{NULL, 0}
};
Expand Down
31 changes: 31 additions & 0 deletions xdp-bench/xdp_basic.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ 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;

static int parse_ip_header(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct hdr_cursor nh = { .pos = data };
struct ipv6hdr *ipv6hdr;
struct iphdr *iphdr;
struct ethhdr *eth;
int eth_type, ip_type;

eth_type = parse_ethhdr(&nh, data_end, &eth);
if (eth_type < 0)
return eth_type;

if (eth_type == bpf_htons(ETH_P_IP)) {
ip_type = parse_iphdr(&nh, data_end, &iphdr);
if (ip_type < 0)
return ip_type;
} else if (eth_type == bpf_htons(ETH_P_IPV6)) {
ip_type = parse_ip6hdr(&nh, data_end, &ipv6hdr);
if (ip_type < 0)
return ip_type;
}

return 0;
}

SEC("xdp")
int xdp_basic_prog(struct xdp_md *ctx)
{
Expand Down Expand Up @@ -54,6 +81,10 @@ int xdp_basic_prog(struct xdp_md *ctx)
if (bpf_ntohs(eth->h_proto) < ETH_P_802_3_MIN)
return XDP_ABORTED;
break;
case BASIC_PARSE_IPHDR:
if (parse_ip_header(ctx))
return XDP_ABORTED;
break;
case BASIC_SWAP_MACS:
swap_src_dst_mac(data);
break;
Expand Down

0 comments on commit b43595e

Please sign in to comment.