Skip to content

Commit dfff86f

Browse files
author
Alexei Starovoitov
committed
Merge branch 'samples/bpf: modernize BPF functionality test programs'
"Daniel T. Lee" says: ==================== Currently, there are many programs under samples/bpf to test the various functionality of BPF that have been developed for a long time. However, the kernel (BPF) has changed a lot compared to the 2016 when some of these test programs were first introduced. Therefore, some of these programs use the deprecated function of BPF, and some programs no longer work normally due to changes in the API. To list some of the kernel changes that this patch set is focusing on, - legacy BPF map declaration syntax support had been dropped [1] - bpf_trace_printk() always append newline at the end [2] - deprecated styled BPF section header (bpf_load style) [3] - urandom_read tracepoint is removed (used for testing overhead) [4] - ping sends packet with SOCK_DGRAM instead of SOCK_RAW [5]* - use "vmlinux.h" instead of including individual headers In addition to this, this patchset tries to modernize the existing testing scripts a bit. And for network-related testing programs, a separate header file was created and applied. (To use the Endianness conversion function from xdp_sample and bunch of constants) [1]: libbpf/libbpf#282 [2]: commit ac5a72e ("bpf: Use dedicated bpf_trace_printk event instead of trace_printk()") [3]: commit ceb5dea ("samples: bpf: Remove bpf_load loader completely") [4]: commit 14c1746 ("random: remove unused tracepoints") [5]: https://lwn.net/Articles/422330/ *: This is quite old, but I'm not sure why the code was initially developed to filter only SOCK_RAW. ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 81bbbb6 + e04946f commit dfff86f

19 files changed

+179
-167
lines changed

samples/bpf/Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ always-y += tracex4_kern.o
131131
always-y += tracex5_kern.o
132132
always-y += tracex6_kern.o
133133
always-y += tracex7_kern.o
134-
always-y += sock_flags_kern.o
134+
always-y += sock_flags.bpf.o
135135
always-y += test_probe_write_user.bpf.o
136136
always-y += trace_output.bpf.o
137137
always-y += tcbpf1_kern.o
@@ -140,19 +140,19 @@ always-y += lathist_kern.o
140140
always-y += offwaketime_kern.o
141141
always-y += spintest_kern.o
142142
always-y += map_perf_test.bpf.o
143-
always-y += test_overhead_tp_kern.o
144-
always-y += test_overhead_raw_tp_kern.o
145-
always-y += test_overhead_kprobe_kern.o
143+
always-y += test_overhead_tp.bpf.o
144+
always-y += test_overhead_raw_tp.bpf.o
145+
always-y += test_overhead_kprobe.bpf.o
146146
always-y += parse_varlen.o parse_simple.o parse_ldabs.o
147-
always-y += test_cgrp2_tc_kern.o
147+
always-y += test_cgrp2_tc.bpf.o
148148
always-y += xdp1_kern.o
149149
always-y += xdp2_kern.o
150150
always-y += test_current_task_under_cgroup.bpf.o
151151
always-y += trace_event_kern.o
152152
always-y += sampleip_kern.o
153-
always-y += lwt_len_hist_kern.o
153+
always-y += lwt_len_hist.bpf.o
154154
always-y += xdp_tx_iptunnel_kern.o
155-
always-y += test_map_in_map_kern.o
155+
always-y += test_map_in_map.bpf.o
156156
always-y += tcp_synrto_kern.o
157157
always-y += tcp_rwnd_kern.o
158158
always-y += tcp_bufs_kern.o

samples/bpf/lwt_len_hist_kern.c renamed to samples/bpf/lwt_len_hist.bpf.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,16 @@
1010
* General Public License for more details.
1111
*/
1212

13-
#include <uapi/linux/bpf.h>
14-
#include <uapi/linux/if_ether.h>
15-
#include <uapi/linux/ip.h>
16-
#include <uapi/linux/in.h>
13+
#include "vmlinux.h"
1714
#include <bpf/bpf_helpers.h>
1815

19-
struct bpf_elf_map {
20-
__u32 type;
21-
__u32 size_key;
22-
__u32 size_value;
23-
__u32 max_elem;
24-
__u32 flags;
25-
__u32 id;
26-
__u32 pinning;
27-
};
28-
29-
struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
30-
.type = BPF_MAP_TYPE_PERCPU_HASH,
31-
.size_key = sizeof(__u64),
32-
.size_value = sizeof(__u64),
33-
.pinning = 2,
34-
.max_elem = 1024,
35-
};
16+
struct {
17+
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
18+
__type(key, u64);
19+
__type(value, u64);
20+
__uint(pinning, LIBBPF_PIN_BY_NAME);
21+
__uint(max_entries, 1024);
22+
} lwt_len_hist_map SEC(".maps");
3623

3724
static unsigned int log2(unsigned int v)
3825
{

samples/bpf/lwt_len_hist.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
NS1=lwt_ns1
55
VETH0=tst_lwt1a
66
VETH1=tst_lwt1b
7-
7+
BPF_PROG=lwt_len_hist.bpf.o
88
TRACE_ROOT=/sys/kernel/debug/tracing
99

1010
function cleanup {
@@ -30,7 +30,7 @@ ip netns exec $NS1 netserver
3030

3131
echo 1 > ${TRACE_ROOT}/tracing_on
3232
cp /dev/null ${TRACE_ROOT}/trace
33-
ip route add 192.168.253.2/32 encap bpf out obj lwt_len_hist_kern.o section len_hist dev $VETH0
33+
ip route add 192.168.253.2/32 encap bpf out obj $BPF_PROG section len_hist dev $VETH0
3434
netperf -H 192.168.253.2 -t TCP_STREAM
3535
cat ${TRACE_ROOT}/trace | grep -v '^#'
3636
./lwt_len_hist

samples/bpf/net_shared.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#ifndef _NET_SHARED_H
3+
#define _NET_SHARED_H
4+
5+
#define AF_INET 2
6+
#define AF_INET6 10
7+
8+
#define ETH_ALEN 6
9+
#define ETH_P_802_3_MIN 0x0600
10+
#define ETH_P_8021Q 0x8100
11+
#define ETH_P_8021AD 0x88A8
12+
#define ETH_P_IP 0x0800
13+
#define ETH_P_IPV6 0x86DD
14+
#define ETH_P_ARP 0x0806
15+
#define IPPROTO_ICMPV6 58
16+
17+
#define TC_ACT_OK 0
18+
#define TC_ACT_SHOT 2
19+
20+
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
21+
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
22+
#define bpf_ntohs(x) __builtin_bswap16(x)
23+
#define bpf_htons(x) __builtin_bswap16(x)
24+
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
25+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
26+
#define bpf_ntohs(x) (x)
27+
#define bpf_htons(x) (x)
28+
#else
29+
# error "Endianness detection needs to be set up for your compiler?!"
30+
#endif
31+
32+
#endif

samples/bpf/sock_flags_kern.c renamed to samples/bpf/sock_flags.bpf.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#include <uapi/linux/bpf.h>
2-
#include <linux/socket.h>
3-
#include <linux/net.h>
4-
#include <uapi/linux/in.h>
5-
#include <uapi/linux/in6.h>
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include "net_shared.h"
64
#include <bpf/bpf_helpers.h>
75

8-
SEC("cgroup/sock1")
6+
SEC("cgroup/sock")
97
int bpf_prog1(struct bpf_sock *sk)
108
{
119
char fmt[] = "socket: family %d type %d protocol %d\n";
@@ -17,29 +15,29 @@ int bpf_prog1(struct bpf_sock *sk)
1715
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
1816
bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
1917

20-
/* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
18+
/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
2119
* ie., make ping6 fail
2220
*/
23-
if (sk->family == PF_INET6 &&
24-
sk->type == SOCK_RAW &&
21+
if (sk->family == AF_INET6 &&
22+
sk->type == SOCK_DGRAM &&
2523
sk->protocol == IPPROTO_ICMPV6)
2624
return 0;
2725

2826
return 1;
2927
}
3028

31-
SEC("cgroup/sock2")
29+
SEC("cgroup/sock")
3230
int bpf_prog2(struct bpf_sock *sk)
3331
{
3432
char fmt[] = "socket: family %d type %d protocol %d\n";
3533

3634
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
3735

38-
/* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
36+
/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
3937
* ie., make ping fail
4038
*/
41-
if (sk->family == PF_INET &&
42-
sk->type == SOCK_RAW &&
39+
if (sk->family == AF_INET &&
40+
sk->type == SOCK_DGRAM &&
4341
sk->protocol == IPPROTO_ICMP)
4442
return 0;
4543

samples/bpf/tc_l2_redirect.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ REDIRECT_USER='./tc_l2_redirect'
88
REDIRECT_BPF='./tc_l2_redirect_kern.o'
99

1010
RP_FILTER=$(< /proc/sys/net/ipv4/conf/all/rp_filter)
11+
IPV6_DISABLED=$(< /proc/sys/net/ipv6/conf/all/disable_ipv6)
1112
IPV6_FORWARDING=$(< /proc/sys/net/ipv6/conf/all/forwarding)
1213

1314
function config_common {
@@ -64,6 +65,7 @@ function config_common {
6465

6566
sysctl -q -w net.ipv4.conf.all.rp_filter=0
6667
sysctl -q -w net.ipv6.conf.all.forwarding=1
68+
sysctl -q -w net.ipv6.conf.all.disable_ipv6=0
6769
}
6870

6971
function cleanup {
@@ -77,6 +79,7 @@ function cleanup {
7779
$IP link del ip6t >& /dev/null
7880
sysctl -q -w net.ipv4.conf.all.rp_filter=$RP_FILTER
7981
sysctl -q -w net.ipv6.conf.all.forwarding=$IPV6_FORWARDING
82+
sysctl -q -w net.ipv6.conf.all.disable_ipv6=$IPV6_DISABLED
8083
rm -f /sys/fs/bpf/tc/globals/tun_iface
8184
[[ -z $DEBUG ]] || set -x
8285
set -e

samples/bpf/test_cgrp2_sock.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# Test various socket options that can be set by attaching programs to cgroups.
55

6+
MY_DIR=$(dirname $0)
7+
TEST=$MY_DIR/test_cgrp2_sock
68
CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"
79

810
################################################################################
@@ -19,7 +21,7 @@ print_result()
1921

2022
check_sock()
2123
{
22-
out=$(test_cgrp2_sock)
24+
out=$($TEST)
2325
echo $out | grep -q "$1"
2426
if [ $? -ne 0 ]; then
2527
print_result 1 "IPv4: $2"
@@ -33,7 +35,7 @@ check_sock()
3335

3436
check_sock6()
3537
{
36-
out=$(test_cgrp2_sock -6)
38+
out=$($TEST -6)
3739
echo $out | grep -q "$1"
3840
if [ $? -ne 0 ]; then
3941
print_result 1 "IPv6: $2"
@@ -61,7 +63,7 @@ cleanup_and_exit()
6163

6264
[ -n "$msg" ] && echo "ERROR: $msg"
6365

64-
test_cgrp2_sock -d ${CGRP_MNT}/sockopts
66+
$TEST -d ${CGRP_MNT}/sockopts
6567
ip li del cgrp2_sock
6668
umount ${CGRP_MNT}
6769

@@ -98,7 +100,7 @@ check_sock6 "dev , mark 0, priority 0" "No programs attached"
98100

99101
# verify device is set
100102
#
101-
test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
103+
$TEST -b cgrp2_sock ${CGRP_MNT}/sockopts
102104
if [ $? -ne 0 ]; then
103105
cleanup_and_exit 1 "Failed to install program to set device"
104106
fi
@@ -107,7 +109,7 @@ check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
107109

108110
# verify mark is set
109111
#
110-
test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
112+
$TEST -m 666 ${CGRP_MNT}/sockopts
111113
if [ $? -ne 0 ]; then
112114
cleanup_and_exit 1 "Failed to install program to set mark"
113115
fi
@@ -116,7 +118,7 @@ check_sock6 "dev , mark 666, priority 0" "Mark set"
116118

117119
# verify priority is set
118120
#
119-
test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
121+
$TEST -p 123 ${CGRP_MNT}/sockopts
120122
if [ $? -ne 0 ]; then
121123
cleanup_and_exit 1 "Failed to install program to set priority"
122124
fi
@@ -125,7 +127,7 @@ check_sock6 "dev , mark 0, priority 123" "Priority set"
125127

126128
# all 3 at once
127129
#
128-
test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
130+
$TEST -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
129131
if [ $? -ne 0 ]; then
130132
cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
131133
fi

samples/bpf/test_cgrp2_sock2.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
# SPDX-License-Identifier: GPL-2.0
33

44
BPFFS=/sys/fs/bpf
5+
MY_DIR=$(dirname $0)
6+
TEST=$MY_DIR/test_cgrp2_sock2
57
LINK_PIN=$BPFFS/test_cgrp2_sock2
8+
BPF_PROG=$MY_DIR/sock_flags.bpf.o
69

710
function config_device {
811
ip netns add at_ns0
912
ip link add veth0 type veth peer name veth0b
10-
ip link set veth0b up
1113
ip link set veth0 netns at_ns0
14+
ip netns exec at_ns0 sysctl -q net.ipv6.conf.veth0.disable_ipv6=0
1215
ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
1316
ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
1417
ip netns exec at_ns0 ip link set dev veth0 up
18+
sysctl -q net.ipv6.conf.veth0b.disable_ipv6=0
1519
ip addr add 172.16.1.101/24 dev veth0b
1620
ip addr add 2401:db00::2/64 dev veth0b nodad
21+
ip link set veth0b up
1722
}
1823

1924
function config_cgroup {
@@ -34,7 +39,7 @@ function config_bpffs {
3439
}
3540

3641
function attach_bpf {
37-
./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
42+
$TEST /tmp/cgroupv2/foo $BPF_PROG $1
3843
[ $? -ne 0 ] && exit 1
3944
}
4045

samples/bpf/test_cgrp2_tc_kern.c renamed to samples/bpf/test_cgrp2_tc.bpf.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
* License as published by the Free Software Foundation.
66
*/
77
#define KBUILD_MODNAME "foo"
8-
#include <uapi/linux/if_ether.h>
9-
#include <uapi/linux/in6.h>
10-
#include <uapi/linux/ipv6.h>
11-
#include <uapi/linux/pkt_cls.h>
12-
#include <uapi/linux/bpf.h>
8+
#include "vmlinux.h"
9+
#include "net_shared.h"
1310
#include <bpf/bpf_helpers.h>
1411

1512
/* copy of 'struct ethhdr' without __packed */
@@ -19,24 +16,13 @@ struct eth_hdr {
1916
unsigned short h_proto;
2017
};
2118

22-
#define PIN_GLOBAL_NS 2
23-
struct bpf_elf_map {
24-
__u32 type;
25-
__u32 size_key;
26-
__u32 size_value;
27-
__u32 max_elem;
28-
__u32 flags;
29-
__u32 id;
30-
__u32 pinning;
31-
};
32-
33-
struct bpf_elf_map SEC("maps") test_cgrp2_array_pin = {
34-
.type = BPF_MAP_TYPE_CGROUP_ARRAY,
35-
.size_key = sizeof(uint32_t),
36-
.size_value = sizeof(uint32_t),
37-
.pinning = PIN_GLOBAL_NS,
38-
.max_elem = 1,
39-
};
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
21+
__type(key, u32);
22+
__type(value, u32);
23+
__uint(pinning, LIBBPF_PIN_BY_NAME);
24+
__uint(max_entries, 1);
25+
} test_cgrp2_array_pin SEC(".maps");
4026

4127
SEC("filter")
4228
int handle_egress(struct __sk_buff *skb)
@@ -53,7 +39,7 @@ int handle_egress(struct __sk_buff *skb)
5339
if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
5440
return TC_ACT_OK;
5541

56-
if (eth->h_proto != htons(ETH_P_IPV6) ||
42+
if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
5743
ip6h->nexthdr != IPPROTO_ICMPV6) {
5844
bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
5945
eth->h_proto, ip6h->nexthdr);

samples/bpf/test_cgrp2_tc.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
MY_DIR=$(dirname $0)
55
# Details on the bpf prog
66
BPF_CGRP2_ARRAY_NAME='test_cgrp2_array_pin'
7-
BPF_PROG="$MY_DIR/test_cgrp2_tc_kern.o"
7+
BPF_PROG="$MY_DIR/test_cgrp2_tc.bpf.o"
88
BPF_SECTION='filter'
99

1010
[ -z "$TC" ] && TC='tc'
@@ -73,11 +73,13 @@ setup_net() {
7373
start)
7474
$IP link add $HOST_IFC type veth peer name $NS_IFC || return $?
7575
$IP link set dev $HOST_IFC up || return $?
76+
sysctl -q net.ipv6.conf.$HOST_IFC.disable_ipv6=0
7677
sysctl -q net.ipv6.conf.$HOST_IFC.accept_dad=0
7778

78-
$IP netns add ns || return $?
79-
$IP link set dev $NS_IFC netns ns || return $?
79+
$IP netns add $NS || return $?
80+
$IP link set dev $NS_IFC netns $NS || return $?
8081
$IP -n $NS link set dev $NS_IFC up || return $?
82+
$IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.disable_ipv6=0
8183
$IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.accept_dad=0
8284
$TC qdisc add dev $HOST_IFC clsact || return $?
8385
$TC filter add dev $HOST_IFC egress bpf da obj $BPF_PROG sec $BPF_SECTION || return $?

0 commit comments

Comments
 (0)