Skip to content

Commit 9cc873e

Browse files
Alexei Starovoitovborkmann
authored andcommitted
selftests/bpf: Add skb_pkt_end test
Add a test that currently makes LLVM generate assembly code: $ llvm-objdump -S skb_pkt_end.o 0000000000000000 <main_prog>: ; if (skb_shorter(skb, ETH_IPV4_TCP_SIZE)) 0: 61 12 50 00 00 00 00 00 r2 = *(u32 *)(r1 + 80) 1: 61 14 4c 00 00 00 00 00 r4 = *(u32 *)(r1 + 76) 2: bf 43 00 00 00 00 00 00 r3 = r4 3: 07 03 00 00 36 00 00 00 r3 += 54 4: b7 01 00 00 00 00 00 00 r1 = 0 5: 2d 23 02 00 00 00 00 00 if r3 > r2 goto +2 <LBB0_2> 6: 07 04 00 00 0e 00 00 00 r4 += 14 ; if (skb_shorter(skb, ETH_IPV4_TCP_SIZE)) 7: bf 41 00 00 00 00 00 00 r1 = r4 0000000000000040 <LBB0_2>: 8: b4 00 00 00 ff ff ff ff w0 = -1 ; if (!(ip = get_iphdr(skb))) 9: 2d 23 05 00 00 00 00 00 if r3 > r2 goto +5 <LBB0_6> ; proto = ip->protocol; 10: 71 12 09 00 00 00 00 00 r2 = *(u8 *)(r1 + 9) ; if (proto != IPPROTO_TCP) 11: 56 02 03 00 06 00 00 00 if w2 != 6 goto +3 <LBB0_6> ; if (tcp->dest != 0) 12: 69 12 16 00 00 00 00 00 r2 = *(u16 *)(r1 + 22) 13: 56 02 01 00 00 00 00 00 if w2 != 0 goto +1 <LBB0_6> ; return tcp->urg_ptr; 14: 69 10 26 00 00 00 00 00 r0 = *(u16 *)(r1 + 38) 0000000000000078 <LBB0_6>: ; } 15: 95 00 00 00 00 00 00 00 exit Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6d94e74 commit 9cc873e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020 Facebook */
3+
#include <test_progs.h>
4+
#include <network_helpers.h>
5+
#include "skb_pkt_end.skel.h"
6+
7+
static int sanity_run(struct bpf_program *prog)
8+
{
9+
__u32 duration, retval;
10+
int err, prog_fd;
11+
12+
prog_fd = bpf_program__fd(prog);
13+
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
14+
NULL, NULL, &retval, &duration);
15+
if (CHECK(err || retval != 123, "test_run",
16+
"err %d errno %d retval %d duration %d\n",
17+
err, errno, retval, duration))
18+
return -1;
19+
return 0;
20+
}
21+
22+
void test_test_skb_pkt_end(void)
23+
{
24+
struct skb_pkt_end *skb_pkt_end_skel = NULL;
25+
__u32 duration = 0;
26+
int err;
27+
28+
skb_pkt_end_skel = skb_pkt_end__open_and_load();
29+
if (CHECK(!skb_pkt_end_skel, "skb_pkt_end_skel_load", "skb_pkt_end skeleton failed\n"))
30+
goto cleanup;
31+
32+
err = skb_pkt_end__attach(skb_pkt_end_skel);
33+
if (CHECK(err, "skb_pkt_end_attach", "skb_pkt_end attach failed: %d\n", err))
34+
goto cleanup;
35+
36+
if (sanity_run(skb_pkt_end_skel->progs.main_prog))
37+
goto cleanup;
38+
39+
cleanup:
40+
skb_pkt_end__destroy(skb_pkt_end_skel);
41+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define BPF_NO_PRESERVE_ACCESS_INDEX
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_core_read.h>
5+
#include <bpf/bpf_helpers.h>
6+
7+
#define NULL 0
8+
#define INLINE __always_inline
9+
10+
#define skb_shorter(skb, len) ((void *)(long)(skb)->data + (len) > (void *)(long)skb->data_end)
11+
12+
#define ETH_IPV4_TCP_SIZE (14 + sizeof(struct iphdr) + sizeof(struct tcphdr))
13+
14+
static INLINE struct iphdr *get_iphdr(struct __sk_buff *skb)
15+
{
16+
struct iphdr *ip = NULL;
17+
struct ethhdr *eth;
18+
19+
if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
20+
goto out;
21+
22+
eth = (void *)(long)skb->data;
23+
ip = (void *)(eth + 1);
24+
25+
out:
26+
return ip;
27+
}
28+
29+
SEC("classifier/cls")
30+
int main_prog(struct __sk_buff *skb)
31+
{
32+
struct iphdr *ip = NULL;
33+
struct tcphdr *tcp;
34+
__u8 proto = 0;
35+
36+
if (!(ip = get_iphdr(skb)))
37+
goto out;
38+
39+
proto = ip->protocol;
40+
41+
if (proto != IPPROTO_TCP)
42+
goto out;
43+
44+
tcp = (void*)(ip + 1);
45+
if (tcp->dest != 0)
46+
goto out;
47+
if (!tcp)
48+
goto out;
49+
50+
return tcp->urg_ptr;
51+
out:
52+
return -1;
53+
}
54+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)