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]: https://github.com/libbpf/libbpf/issues/282 [2]: commitac5a72ea5c
("bpf: Use dedicated bpf_trace_printk event instead of trace_printk()") [3]: commitceb5dea565
("samples: bpf: Remove bpf_load loader completely") [4]: commit14c174633f
("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 <ast@kernel.org>
This commit is contained in:
commit
dfff86f8eb
|
@ -131,7 +131,7 @@ always-y += tracex4_kern.o
|
|||
always-y += tracex5_kern.o
|
||||
always-y += tracex6_kern.o
|
||||
always-y += tracex7_kern.o
|
||||
always-y += sock_flags_kern.o
|
||||
always-y += sock_flags.bpf.o
|
||||
always-y += test_probe_write_user.bpf.o
|
||||
always-y += trace_output.bpf.o
|
||||
always-y += tcbpf1_kern.o
|
||||
|
@ -140,19 +140,19 @@ always-y += lathist_kern.o
|
|||
always-y += offwaketime_kern.o
|
||||
always-y += spintest_kern.o
|
||||
always-y += map_perf_test.bpf.o
|
||||
always-y += test_overhead_tp_kern.o
|
||||
always-y += test_overhead_raw_tp_kern.o
|
||||
always-y += test_overhead_kprobe_kern.o
|
||||
always-y += test_overhead_tp.bpf.o
|
||||
always-y += test_overhead_raw_tp.bpf.o
|
||||
always-y += test_overhead_kprobe.bpf.o
|
||||
always-y += parse_varlen.o parse_simple.o parse_ldabs.o
|
||||
always-y += test_cgrp2_tc_kern.o
|
||||
always-y += test_cgrp2_tc.bpf.o
|
||||
always-y += xdp1_kern.o
|
||||
always-y += xdp2_kern.o
|
||||
always-y += test_current_task_under_cgroup.bpf.o
|
||||
always-y += trace_event_kern.o
|
||||
always-y += sampleip_kern.o
|
||||
always-y += lwt_len_hist_kern.o
|
||||
always-y += lwt_len_hist.bpf.o
|
||||
always-y += xdp_tx_iptunnel_kern.o
|
||||
always-y += test_map_in_map_kern.o
|
||||
always-y += test_map_in_map.bpf.o
|
||||
always-y += tcp_synrto_kern.o
|
||||
always-y += tcp_rwnd_kern.o
|
||||
always-y += tcp_bufs_kern.o
|
||||
|
|
|
@ -10,29 +10,16 @@
|
|||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include <uapi/linux/if_ether.h>
|
||||
#include <uapi/linux/ip.h>
|
||||
#include <uapi/linux/in.h>
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
struct bpf_elf_map {
|
||||
__u32 type;
|
||||
__u32 size_key;
|
||||
__u32 size_value;
|
||||
__u32 max_elem;
|
||||
__u32 flags;
|
||||
__u32 id;
|
||||
__u32 pinning;
|
||||
};
|
||||
|
||||
struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
|
||||
.type = BPF_MAP_TYPE_PERCPU_HASH,
|
||||
.size_key = sizeof(__u64),
|
||||
.size_value = sizeof(__u64),
|
||||
.pinning = 2,
|
||||
.max_elem = 1024,
|
||||
};
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
|
||||
__type(key, u64);
|
||||
__type(value, u64);
|
||||
__uint(pinning, LIBBPF_PIN_BY_NAME);
|
||||
__uint(max_entries, 1024);
|
||||
} lwt_len_hist_map SEC(".maps");
|
||||
|
||||
static unsigned int log2(unsigned int v)
|
||||
{
|
|
@ -4,7 +4,7 @@
|
|||
NS1=lwt_ns1
|
||||
VETH0=tst_lwt1a
|
||||
VETH1=tst_lwt1b
|
||||
|
||||
BPF_PROG=lwt_len_hist.bpf.o
|
||||
TRACE_ROOT=/sys/kernel/debug/tracing
|
||||
|
||||
function cleanup {
|
||||
|
@ -30,7 +30,7 @@ ip netns exec $NS1 netserver
|
|||
|
||||
echo 1 > ${TRACE_ROOT}/tracing_on
|
||||
cp /dev/null ${TRACE_ROOT}/trace
|
||||
ip route add 192.168.253.2/32 encap bpf out obj lwt_len_hist_kern.o section len_hist dev $VETH0
|
||||
ip route add 192.168.253.2/32 encap bpf out obj $BPF_PROG section len_hist dev $VETH0
|
||||
netperf -H 192.168.253.2 -t TCP_STREAM
|
||||
cat ${TRACE_ROOT}/trace | grep -v '^#'
|
||||
./lwt_len_hist
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#ifndef _NET_SHARED_H
|
||||
#define _NET_SHARED_H
|
||||
|
||||
#define AF_INET 2
|
||||
#define AF_INET6 10
|
||||
|
||||
#define ETH_ALEN 6
|
||||
#define ETH_P_802_3_MIN 0x0600
|
||||
#define ETH_P_8021Q 0x8100
|
||||
#define ETH_P_8021AD 0x88A8
|
||||
#define ETH_P_IP 0x0800
|
||||
#define ETH_P_IPV6 0x86DD
|
||||
#define ETH_P_ARP 0x0806
|
||||
#define IPPROTO_ICMPV6 58
|
||||
|
||||
#define TC_ACT_OK 0
|
||||
#define TC_ACT_SHOT 2
|
||||
|
||||
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define bpf_ntohs(x) __builtin_bswap16(x)
|
||||
#define bpf_htons(x) __builtin_bswap16(x)
|
||||
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define bpf_ntohs(x) (x)
|
||||
#define bpf_htons(x) (x)
|
||||
#else
|
||||
# error "Endianness detection needs to be set up for your compiler?!"
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,11 +1,9 @@
|
|||
#include <uapi/linux/bpf.h>
|
||||
#include <linux/socket.h>
|
||||
#include <linux/net.h>
|
||||
#include <uapi/linux/in.h>
|
||||
#include <uapi/linux/in6.h>
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include "vmlinux.h"
|
||||
#include "net_shared.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
SEC("cgroup/sock1")
|
||||
SEC("cgroup/sock")
|
||||
int bpf_prog1(struct bpf_sock *sk)
|
||||
{
|
||||
char fmt[] = "socket: family %d type %d protocol %d\n";
|
||||
|
@ -17,29 +15,29 @@ int bpf_prog1(struct bpf_sock *sk)
|
|||
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
|
||||
bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
|
||||
|
||||
/* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
|
||||
/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
|
||||
* ie., make ping6 fail
|
||||
*/
|
||||
if (sk->family == PF_INET6 &&
|
||||
sk->type == SOCK_RAW &&
|
||||
if (sk->family == AF_INET6 &&
|
||||
sk->type == SOCK_DGRAM &&
|
||||
sk->protocol == IPPROTO_ICMPV6)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("cgroup/sock2")
|
||||
SEC("cgroup/sock")
|
||||
int bpf_prog2(struct bpf_sock *sk)
|
||||
{
|
||||
char fmt[] = "socket: family %d type %d protocol %d\n";
|
||||
|
||||
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
|
||||
|
||||
/* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
|
||||
/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
|
||||
* ie., make ping fail
|
||||
*/
|
||||
if (sk->family == PF_INET &&
|
||||
sk->type == SOCK_RAW &&
|
||||
if (sk->family == AF_INET &&
|
||||
sk->type == SOCK_DGRAM &&
|
||||
sk->protocol == IPPROTO_ICMP)
|
||||
return 0;
|
||||
|
|
@ -8,6 +8,7 @@ REDIRECT_USER='./tc_l2_redirect'
|
|||
REDIRECT_BPF='./tc_l2_redirect_kern.o'
|
||||
|
||||
RP_FILTER=$(< /proc/sys/net/ipv4/conf/all/rp_filter)
|
||||
IPV6_DISABLED=$(< /proc/sys/net/ipv6/conf/all/disable_ipv6)
|
||||
IPV6_FORWARDING=$(< /proc/sys/net/ipv6/conf/all/forwarding)
|
||||
|
||||
function config_common {
|
||||
|
@ -64,6 +65,7 @@ function config_common {
|
|||
|
||||
sysctl -q -w net.ipv4.conf.all.rp_filter=0
|
||||
sysctl -q -w net.ipv6.conf.all.forwarding=1
|
||||
sysctl -q -w net.ipv6.conf.all.disable_ipv6=0
|
||||
}
|
||||
|
||||
function cleanup {
|
||||
|
@ -77,6 +79,7 @@ function cleanup {
|
|||
$IP link del ip6t >& /dev/null
|
||||
sysctl -q -w net.ipv4.conf.all.rp_filter=$RP_FILTER
|
||||
sysctl -q -w net.ipv6.conf.all.forwarding=$IPV6_FORWARDING
|
||||
sysctl -q -w net.ipv6.conf.all.disable_ipv6=$IPV6_DISABLED
|
||||
rm -f /sys/fs/bpf/tc/globals/tun_iface
|
||||
[[ -z $DEBUG ]] || set -x
|
||||
set -e
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
# Test various socket options that can be set by attaching programs to cgroups.
|
||||
|
||||
MY_DIR=$(dirname $0)
|
||||
TEST=$MY_DIR/test_cgrp2_sock
|
||||
CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"
|
||||
|
||||
################################################################################
|
||||
|
@ -19,7 +21,7 @@ print_result()
|
|||
|
||||
check_sock()
|
||||
{
|
||||
out=$(test_cgrp2_sock)
|
||||
out=$($TEST)
|
||||
echo $out | grep -q "$1"
|
||||
if [ $? -ne 0 ]; then
|
||||
print_result 1 "IPv4: $2"
|
||||
|
@ -33,7 +35,7 @@ check_sock()
|
|||
|
||||
check_sock6()
|
||||
{
|
||||
out=$(test_cgrp2_sock -6)
|
||||
out=$($TEST -6)
|
||||
echo $out | grep -q "$1"
|
||||
if [ $? -ne 0 ]; then
|
||||
print_result 1 "IPv6: $2"
|
||||
|
@ -61,7 +63,7 @@ cleanup_and_exit()
|
|||
|
||||
[ -n "$msg" ] && echo "ERROR: $msg"
|
||||
|
||||
test_cgrp2_sock -d ${CGRP_MNT}/sockopts
|
||||
$TEST -d ${CGRP_MNT}/sockopts
|
||||
ip li del cgrp2_sock
|
||||
umount ${CGRP_MNT}
|
||||
|
||||
|
@ -98,7 +100,7 @@ check_sock6 "dev , mark 0, priority 0" "No programs attached"
|
|||
|
||||
# verify device is set
|
||||
#
|
||||
test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
|
||||
$TEST -b cgrp2_sock ${CGRP_MNT}/sockopts
|
||||
if [ $? -ne 0 ]; then
|
||||
cleanup_and_exit 1 "Failed to install program to set device"
|
||||
fi
|
||||
|
@ -107,7 +109,7 @@ check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
|
|||
|
||||
# verify mark is set
|
||||
#
|
||||
test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
|
||||
$TEST -m 666 ${CGRP_MNT}/sockopts
|
||||
if [ $? -ne 0 ]; then
|
||||
cleanup_and_exit 1 "Failed to install program to set mark"
|
||||
fi
|
||||
|
@ -116,7 +118,7 @@ check_sock6 "dev , mark 666, priority 0" "Mark set"
|
|||
|
||||
# verify priority is set
|
||||
#
|
||||
test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
|
||||
$TEST -p 123 ${CGRP_MNT}/sockopts
|
||||
if [ $? -ne 0 ]; then
|
||||
cleanup_and_exit 1 "Failed to install program to set priority"
|
||||
fi
|
||||
|
@ -125,7 +127,7 @@ check_sock6 "dev , mark 0, priority 123" "Priority set"
|
|||
|
||||
# all 3 at once
|
||||
#
|
||||
test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
|
||||
$TEST -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
|
||||
if [ $? -ne 0 ]; then
|
||||
cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
|
||||
fi
|
||||
|
|
|
@ -2,18 +2,23 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
BPFFS=/sys/fs/bpf
|
||||
MY_DIR=$(dirname $0)
|
||||
TEST=$MY_DIR/test_cgrp2_sock2
|
||||
LINK_PIN=$BPFFS/test_cgrp2_sock2
|
||||
BPF_PROG=$MY_DIR/sock_flags.bpf.o
|
||||
|
||||
function config_device {
|
||||
ip netns add at_ns0
|
||||
ip link add veth0 type veth peer name veth0b
|
||||
ip link set veth0b up
|
||||
ip link set veth0 netns at_ns0
|
||||
ip netns exec at_ns0 sysctl -q net.ipv6.conf.veth0.disable_ipv6=0
|
||||
ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
|
||||
ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
|
||||
ip netns exec at_ns0 ip link set dev veth0 up
|
||||
sysctl -q net.ipv6.conf.veth0b.disable_ipv6=0
|
||||
ip addr add 172.16.1.101/24 dev veth0b
|
||||
ip addr add 2401:db00::2/64 dev veth0b nodad
|
||||
ip link set veth0b up
|
||||
}
|
||||
|
||||
function config_cgroup {
|
||||
|
@ -34,7 +39,7 @@ function config_bpffs {
|
|||
}
|
||||
|
||||
function attach_bpf {
|
||||
./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
|
||||
$TEST /tmp/cgroupv2/foo $BPF_PROG $1
|
||||
[ $? -ne 0 ] && exit 1
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,8 @@
|
|||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
#define KBUILD_MODNAME "foo"
|
||||
#include <uapi/linux/if_ether.h>
|
||||
#include <uapi/linux/in6.h>
|
||||
#include <uapi/linux/ipv6.h>
|
||||
#include <uapi/linux/pkt_cls.h>
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include "vmlinux.h"
|
||||
#include "net_shared.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
/* copy of 'struct ethhdr' without __packed */
|
||||
|
@ -19,24 +16,13 @@ struct eth_hdr {
|
|||
unsigned short h_proto;
|
||||
};
|
||||
|
||||
#define PIN_GLOBAL_NS 2
|
||||
struct bpf_elf_map {
|
||||
__u32 type;
|
||||
__u32 size_key;
|
||||
__u32 size_value;
|
||||
__u32 max_elem;
|
||||
__u32 flags;
|
||||
__u32 id;
|
||||
__u32 pinning;
|
||||
};
|
||||
|
||||
struct bpf_elf_map SEC("maps") test_cgrp2_array_pin = {
|
||||
.type = BPF_MAP_TYPE_CGROUP_ARRAY,
|
||||
.size_key = sizeof(uint32_t),
|
||||
.size_value = sizeof(uint32_t),
|
||||
.pinning = PIN_GLOBAL_NS,
|
||||
.max_elem = 1,
|
||||
};
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
|
||||
__type(key, u32);
|
||||
__type(value, u32);
|
||||
__uint(pinning, LIBBPF_PIN_BY_NAME);
|
||||
__uint(max_entries, 1);
|
||||
} test_cgrp2_array_pin SEC(".maps");
|
||||
|
||||
SEC("filter")
|
||||
int handle_egress(struct __sk_buff *skb)
|
||||
|
@ -53,7 +39,7 @@ int handle_egress(struct __sk_buff *skb)
|
|||
if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
|
||||
return TC_ACT_OK;
|
||||
|
||||
if (eth->h_proto != htons(ETH_P_IPV6) ||
|
||||
if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
|
||||
ip6h->nexthdr != IPPROTO_ICMPV6) {
|
||||
bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
|
||||
eth->h_proto, ip6h->nexthdr);
|
|
@ -4,7 +4,7 @@
|
|||
MY_DIR=$(dirname $0)
|
||||
# Details on the bpf prog
|
||||
BPF_CGRP2_ARRAY_NAME='test_cgrp2_array_pin'
|
||||
BPF_PROG="$MY_DIR/test_cgrp2_tc_kern.o"
|
||||
BPF_PROG="$MY_DIR/test_cgrp2_tc.bpf.o"
|
||||
BPF_SECTION='filter'
|
||||
|
||||
[ -z "$TC" ] && TC='tc'
|
||||
|
@ -73,11 +73,13 @@ setup_net() {
|
|||
start)
|
||||
$IP link add $HOST_IFC type veth peer name $NS_IFC || return $?
|
||||
$IP link set dev $HOST_IFC up || return $?
|
||||
sysctl -q net.ipv6.conf.$HOST_IFC.disable_ipv6=0
|
||||
sysctl -q net.ipv6.conf.$HOST_IFC.accept_dad=0
|
||||
|
||||
$IP netns add ns || return $?
|
||||
$IP link set dev $NS_IFC netns ns || return $?
|
||||
$IP netns add $NS || return $?
|
||||
$IP link set dev $NS_IFC netns $NS || return $?
|
||||
$IP -n $NS link set dev $NS_IFC up || return $?
|
||||
$IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.disable_ipv6=0
|
||||
$IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.accept_dad=0
|
||||
$TC qdisc add dev $HOST_IFC clsact || return $?
|
||||
$TC filter add dev $HOST_IFC egress bpf da obj $BPF_PROG sec $BPF_SECTION || return $?
|
||||
|
|
|
@ -10,16 +10,8 @@
|
|||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <linux/bpf.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/in.h>
|
||||
#include <linux/in6.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/udp.h>
|
||||
#include <linux/icmpv6.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include "vmlinux.h"
|
||||
#include "net_shared.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -44,9 +36,9 @@ SEC("test_ctx")
|
|||
int do_test_ctx(struct __sk_buff *skb)
|
||||
{
|
||||
skb->cb[0] = CB_MAGIC;
|
||||
printk("len %d hash %d protocol %d\n", skb->len, skb->hash,
|
||||
printk("len %d hash %d protocol %d", skb->len, skb->hash,
|
||||
skb->protocol);
|
||||
printk("cb %d ingress_ifindex %d ifindex %d\n", skb->cb[0],
|
||||
printk("cb %d ingress_ifindex %d ifindex %d", skb->cb[0],
|
||||
skb->ingress_ifindex, skb->ifindex);
|
||||
|
||||
return BPF_OK;
|
||||
|
@ -56,9 +48,9 @@ int do_test_ctx(struct __sk_buff *skb)
|
|||
SEC("test_cb")
|
||||
int do_test_cb(struct __sk_buff *skb)
|
||||
{
|
||||
printk("cb0: %x cb1: %x cb2: %x\n", skb->cb[0], skb->cb[1],
|
||||
printk("cb0: %x cb1: %x cb2: %x", skb->cb[0], skb->cb[1],
|
||||
skb->cb[2]);
|
||||
printk("cb3: %x cb4: %x\n", skb->cb[3], skb->cb[4]);
|
||||
printk("cb3: %x cb4: %x", skb->cb[3], skb->cb[4]);
|
||||
|
||||
return BPF_OK;
|
||||
}
|
||||
|
@ -72,11 +64,11 @@ int do_test_data(struct __sk_buff *skb)
|
|||
struct iphdr *iph = data;
|
||||
|
||||
if (data + sizeof(*iph) > data_end) {
|
||||
printk("packet truncated\n");
|
||||
printk("packet truncated");
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
printk("src: %x dst: %x\n", iph->saddr, iph->daddr);
|
||||
printk("src: %x dst: %x", iph->saddr, iph->daddr);
|
||||
|
||||
return BPF_OK;
|
||||
}
|
||||
|
@ -97,7 +89,7 @@ static inline int rewrite(struct __sk_buff *skb, uint32_t old_ip,
|
|||
|
||||
ret = bpf_skb_load_bytes(skb, IP_PROTO_OFF, &proto, 1);
|
||||
if (ret < 0) {
|
||||
printk("bpf_l4_csum_replace failed: %d\n", ret);
|
||||
printk("bpf_l4_csum_replace failed: %d", ret);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
|
@ -120,14 +112,14 @@ static inline int rewrite(struct __sk_buff *skb, uint32_t old_ip,
|
|||
ret = bpf_l4_csum_replace(skb, off, old_ip, new_ip,
|
||||
flags | sizeof(new_ip));
|
||||
if (ret < 0) {
|
||||
printk("bpf_l4_csum_replace failed: %d\n");
|
||||
printk("bpf_l4_csum_replace failed: %d");
|
||||
return BPF_DROP;
|
||||
}
|
||||
}
|
||||
|
||||
ret = bpf_l3_csum_replace(skb, IP_CSUM_OFF, old_ip, new_ip, sizeof(new_ip));
|
||||
if (ret < 0) {
|
||||
printk("bpf_l3_csum_replace failed: %d\n", ret);
|
||||
printk("bpf_l3_csum_replace failed: %d", ret);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
|
@ -137,7 +129,7 @@ static inline int rewrite(struct __sk_buff *skb, uint32_t old_ip,
|
|||
ret = bpf_skb_store_bytes(skb, IP_SRC_OFF, &new_ip, sizeof(new_ip), 0);
|
||||
|
||||
if (ret < 0) {
|
||||
printk("bpf_skb_store_bytes() failed: %d\n", ret);
|
||||
printk("bpf_skb_store_bytes() failed: %d", ret);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
|
@ -153,12 +145,12 @@ int do_test_rewrite(struct __sk_buff *skb)
|
|||
|
||||
ret = bpf_skb_load_bytes(skb, IP_DST_OFF, &old_ip, 4);
|
||||
if (ret < 0) {
|
||||
printk("bpf_skb_load_bytes failed: %d\n", ret);
|
||||
printk("bpf_skb_load_bytes failed: %d", ret);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
if (old_ip == 0x2fea8c0) {
|
||||
printk("out: rewriting from %x to %x\n", old_ip, new_ip);
|
||||
printk("out: rewriting from %x to %x", old_ip, new_ip);
|
||||
return rewrite(skb, old_ip, new_ip, 1);
|
||||
}
|
||||
|
||||
|
@ -173,16 +165,16 @@ static inline int __do_push_ll_and_redirect(struct __sk_buff *skb)
|
|||
|
||||
ret = bpf_skb_change_head(skb, 14, 0);
|
||||
if (ret < 0) {
|
||||
printk("skb_change_head() failed: %d\n", ret);
|
||||
printk("skb_change_head() failed: %d", ret);
|
||||
}
|
||||
|
||||
ehdr.h_proto = __constant_htons(ETH_P_IP);
|
||||
ehdr.h_proto = bpf_htons(ETH_P_IP);
|
||||
memcpy(&ehdr.h_source, &smac, 6);
|
||||
memcpy(&ehdr.h_dest, &dmac, 6);
|
||||
|
||||
ret = bpf_skb_store_bytes(skb, 0, &ehdr, sizeof(ehdr), 0);
|
||||
if (ret < 0) {
|
||||
printk("skb_store_bytes() failed: %d\n", ret);
|
||||
printk("skb_store_bytes() failed: %d", ret);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
|
@ -202,7 +194,7 @@ int do_push_ll_and_redirect(struct __sk_buff *skb)
|
|||
|
||||
ret = __do_push_ll_and_redirect(skb);
|
||||
if (ret >= 0)
|
||||
printk("redirected to %d\n", ifindex);
|
||||
printk("redirected to %d", ifindex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -229,7 +221,7 @@ SEC("fill_garbage")
|
|||
int do_fill_garbage(struct __sk_buff *skb)
|
||||
{
|
||||
__fill_garbage(skb);
|
||||
printk("Set initial 96 bytes of header to FF\n");
|
||||
printk("Set initial 96 bytes of header to FF");
|
||||
return BPF_OK;
|
||||
}
|
||||
|
||||
|
@ -238,7 +230,7 @@ int do_fill_garbage_and_redirect(struct __sk_buff *skb)
|
|||
{
|
||||
int ifindex = DST_IFINDEX;
|
||||
__fill_garbage(skb);
|
||||
printk("redirected to %d\n", ifindex);
|
||||
printk("redirected to %d", ifindex);
|
||||
return bpf_redirect(ifindex, 0);
|
||||
}
|
||||
|
||||
|
@ -246,7 +238,7 @@ int do_fill_garbage_and_redirect(struct __sk_buff *skb)
|
|||
SEC("drop_all")
|
||||
int do_drop_all(struct __sk_buff *skb)
|
||||
{
|
||||
printk("dropping with: %d\n", BPF_DROP);
|
||||
printk("dropping with: %d", BPF_DROP);
|
||||
return BPF_DROP;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,10 @@ IPVETH3="192.168.111.2"
|
|||
|
||||
IP_LOCAL="192.168.99.1"
|
||||
|
||||
PROG_SRC="test_lwt_bpf.c"
|
||||
BPF_PROG="test_lwt_bpf.o"
|
||||
TRACE_ROOT=/sys/kernel/debug/tracing
|
||||
CONTEXT_INFO=$(cat ${TRACE_ROOT}/trace_options | grep context)
|
||||
|
||||
function lookup_mac()
|
||||
{
|
||||
|
@ -36,7 +39,7 @@ function lookup_mac()
|
|||
|
||||
function cleanup {
|
||||
set +ex
|
||||
rm test_lwt_bpf.o 2> /dev/null
|
||||
rm $BPF_PROG 2> /dev/null
|
||||
ip link del $VETH0 2> /dev/null
|
||||
ip link del $VETH1 2> /dev/null
|
||||
ip link del $VETH2 2> /dev/null
|
||||
|
@ -76,7 +79,7 @@ function install_test {
|
|||
cleanup_routes
|
||||
cp /dev/null ${TRACE_ROOT}/trace
|
||||
|
||||
OPTS="encap bpf headroom 14 $1 obj test_lwt_bpf.o section $2 $VERBOSE"
|
||||
OPTS="encap bpf headroom 14 $1 obj $BPF_PROG section $2 $VERBOSE"
|
||||
|
||||
if [ "$1" == "in" ]; then
|
||||
ip route add table local local ${IP_LOCAL}/32 $OPTS dev lo
|
||||
|
@ -96,7 +99,7 @@ function remove_prog {
|
|||
function filter_trace {
|
||||
# Add newline to allow starting EXPECT= variables on newline
|
||||
NL=$'\n'
|
||||
echo "${NL}$*" | sed -e 's/^.*: : //g'
|
||||
echo "${NL}$*" | sed -e 's/bpf_trace_printk: //g'
|
||||
}
|
||||
|
||||
function expect_fail {
|
||||
|
@ -160,11 +163,11 @@ function test_ctx_out {
|
|||
failure "test_ctx out: packets are dropped"
|
||||
}
|
||||
match_trace "$(get_trace)" "
|
||||
len 84 hash 0 protocol 0
|
||||
len 84 hash 0 protocol 8
|
||||
cb 1234 ingress_ifindex 0 ifindex 0
|
||||
len 84 hash 0 protocol 0
|
||||
len 84 hash 0 protocol 8
|
||||
cb 1234 ingress_ifindex 0 ifindex 0
|
||||
len 84 hash 0 protocol 0
|
||||
len 84 hash 0 protocol 8
|
||||
cb 1234 ingress_ifindex 0 ifindex 0" || exit 1
|
||||
remove_prog out
|
||||
}
|
||||
|
@ -367,6 +370,7 @@ setup_one_veth $NS1 $VETH0 $VETH1 $IPVETH0 $IPVETH1 $IPVETH1b
|
|||
setup_one_veth $NS2 $VETH2 $VETH3 $IPVETH2 $IPVETH3
|
||||
ip netns exec $NS1 netserver
|
||||
echo 1 > ${TRACE_ROOT}/tracing_on
|
||||
echo nocontext-info > ${TRACE_ROOT}/trace_options
|
||||
|
||||
DST_MAC=$(lookup_mac $VETH1 $NS1)
|
||||
SRC_MAC=$(lookup_mac $VETH0)
|
||||
|
@ -374,7 +378,7 @@ DST_IFINDEX=$(cat /sys/class/net/$VETH0/ifindex)
|
|||
|
||||
CLANG_OPTS="-O2 -target bpf -I ../include/"
|
||||
CLANG_OPTS+=" -DSRC_MAC=$SRC_MAC -DDST_MAC=$DST_MAC -DDST_IFINDEX=$DST_IFINDEX"
|
||||
clang $CLANG_OPTS -c test_lwt_bpf.c -o test_lwt_bpf.o
|
||||
clang $CLANG_OPTS -c $PROG_SRC -o $BPF_PROG
|
||||
|
||||
test_ctx_xmit
|
||||
test_ctx_out
|
||||
|
@ -397,4 +401,5 @@ test_netperf_redirect
|
|||
|
||||
cleanup
|
||||
echo 0 > ${TRACE_ROOT}/tracing_on
|
||||
echo $CONTEXT_INFO > ${TRACE_ROOT}/trace_options
|
||||
exit 0
|
||||
|
|
|
@ -6,16 +6,17 @@
|
|||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
#define KBUILD_MODNAME "foo"
|
||||
#include <linux/ptrace.h>
|
||||
#include "vmlinux.h"
|
||||
#include <linux/version.h>
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include <uapi/linux/in6.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
#include <bpf/bpf_core_read.h>
|
||||
|
||||
#define MAX_NR_PORTS 65536
|
||||
|
||||
#define EINVAL 22
|
||||
#define ENOENT 2
|
||||
|
||||
/* map #0 */
|
||||
struct inner_a {
|
||||
__uint(type, BPF_MAP_TYPE_ARRAY);
|
|
@ -120,7 +120,7 @@ int main(int argc, char **argv)
|
|||
struct bpf_object *obj;
|
||||
char filename[256];
|
||||
|
||||
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
||||
snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
|
||||
obj = bpf_object__open_file(filename, NULL);
|
||||
if (libbpf_get_error(obj)) {
|
||||
fprintf(stderr, "ERROR: opening BPF object file failed\n");
|
||||
|
|
|
@ -4,10 +4,8 @@
|
|||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
#include "vmlinux.h"
|
||||
#include <linux/version.h>
|
||||
#include <linux/ptrace.h>
|
||||
#include <linux/sched.h>
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
|
||||
|
@ -39,7 +37,7 @@ int prog(struct pt_regs *ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("kprobe/urandom_read")
|
||||
SEC("kprobe/fib_table_lookup")
|
||||
int prog2(struct pt_regs *ctx)
|
||||
{
|
||||
return 0;
|
|
@ -1,6 +1,6 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2018 Facebook */
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
SEC("raw_tracepoint/task_rename")
|
||||
|
@ -9,7 +9,7 @@ int prog(struct bpf_raw_tracepoint_args *ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("raw_tracepoint/urandom_read")
|
||||
SEC("raw_tracepoint/fib_table_lookup")
|
||||
int prog2(struct bpf_raw_tracepoint_args *ctx)
|
||||
{
|
||||
return 0;
|
|
@ -4,8 +4,7 @@
|
|||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*/
|
||||
#include <linux/sched.h>
|
||||
#include <uapi/linux/bpf.h>
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
/* from /sys/kernel/debug/tracing/events/task/task_rename/format */
|
||||
|
@ -22,15 +21,27 @@ int prog(struct task_rename *ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* from /sys/kernel/debug/tracing/events/random/urandom_read/format */
|
||||
struct urandom_read {
|
||||
/* from /sys/kernel/debug/tracing/events/fib/fib_table_lookup/format */
|
||||
struct fib_table_lookup {
|
||||
__u64 pad;
|
||||
int got_bits;
|
||||
int pool_left;
|
||||
int input_left;
|
||||
__u32 tb_id;
|
||||
int err;
|
||||
int oif;
|
||||
int iif;
|
||||
__u8 proto;
|
||||
__u8 tos;
|
||||
__u8 scope;
|
||||
__u8 flags;
|
||||
__u8 src[4];
|
||||
__u8 dst[4];
|
||||
__u8 gw4[4];
|
||||
__u8 gw6[16];
|
||||
__u16 sport;
|
||||
__u16 dport;
|
||||
char name[16];
|
||||
};
|
||||
SEC("tracepoint/random/urandom_read")
|
||||
int prog2(struct urandom_read *ctx)
|
||||
SEC("tracepoint/fib/fib_table_lookup")
|
||||
int prog2(struct fib_table_lookup *ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
|
@ -11,6 +11,8 @@
|
|||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <linux/bpf.h>
|
||||
|
@ -20,6 +22,8 @@
|
|||
#include <bpf/libbpf.h>
|
||||
|
||||
#define MAX_CNT 1000000
|
||||
#define DUMMY_IP "127.0.0.1"
|
||||
#define DUMMY_PORT 80
|
||||
|
||||
static struct bpf_link *links[2];
|
||||
static struct bpf_object *obj;
|
||||
|
@ -35,8 +39,8 @@ static __u64 time_get_ns(void)
|
|||
|
||||
static void test_task_rename(int cpu)
|
||||
{
|
||||
__u64 start_time;
|
||||
char buf[] = "test\n";
|
||||
__u64 start_time;
|
||||
int i, fd;
|
||||
|
||||
fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
|
||||
|
@ -57,26 +61,32 @@ static void test_task_rename(int cpu)
|
|||
close(fd);
|
||||
}
|
||||
|
||||
static void test_urandom_read(int cpu)
|
||||
static void test_fib_table_lookup(int cpu)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
char buf[] = "test\n";
|
||||
__u64 start_time;
|
||||
char buf[4];
|
||||
int i, fd;
|
||||
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (fd < 0) {
|
||||
printf("couldn't open /dev/urandom\n");
|
||||
printf("couldn't open socket\n");
|
||||
exit(1);
|
||||
}
|
||||
memset((char *)&addr, 0, sizeof(addr));
|
||||
addr.sin_addr.s_addr = inet_addr(DUMMY_IP);
|
||||
addr.sin_port = htons(DUMMY_PORT);
|
||||
addr.sin_family = AF_INET;
|
||||
start_time = time_get_ns();
|
||||
for (i = 0; i < MAX_CNT; i++) {
|
||||
if (read(fd, buf, sizeof(buf)) < 0) {
|
||||
printf("failed to read from /dev/urandom: %s\n", strerror(errno));
|
||||
if (sendto(fd, buf, strlen(buf), 0,
|
||||
(struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
printf("failed to start ping: %s\n", strerror(errno));
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
printf("urandom_read:%d: %lld events per sec\n",
|
||||
printf("fib_table_lookup:%d: %lld events per sec\n",
|
||||
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
|
||||
close(fd);
|
||||
}
|
||||
|
@ -92,7 +102,7 @@ static void loop(int cpu, int flags)
|
|||
if (flags & 1)
|
||||
test_task_rename(cpu);
|
||||
if (flags & 2)
|
||||
test_urandom_read(cpu);
|
||||
test_fib_table_lookup(cpu);
|
||||
}
|
||||
|
||||
static void run_perf_test(int tasks, int flags)
|
||||
|
@ -179,7 +189,7 @@ int main(int argc, char **argv)
|
|||
|
||||
if (test_flags & 0xC) {
|
||||
snprintf(filename, sizeof(filename),
|
||||
"%s_kprobe_kern.o", argv[0]);
|
||||
"%s_kprobe.bpf.o", argv[0]);
|
||||
|
||||
printf("w/KPROBE\n");
|
||||
err = load_progs(filename);
|
||||
|
@ -191,7 +201,7 @@ int main(int argc, char **argv)
|
|||
|
||||
if (test_flags & 0x30) {
|
||||
snprintf(filename, sizeof(filename),
|
||||
"%s_tp_kern.o", argv[0]);
|
||||
"%s_tp.bpf.o", argv[0]);
|
||||
printf("w/TRACEPOINT\n");
|
||||
err = load_progs(filename);
|
||||
if (!err)
|
||||
|
@ -202,7 +212,7 @@ int main(int argc, char **argv)
|
|||
|
||||
if (test_flags & 0xC0) {
|
||||
snprintf(filename, sizeof(filename),
|
||||
"%s_raw_tp_kern.o", argv[0]);
|
||||
"%s_raw_tp.bpf.o", argv[0]);
|
||||
printf("w/RAW_TRACEPOINT\n");
|
||||
err = load_progs(filename);
|
||||
if (!err)
|
||||
|
|
|
@ -7,17 +7,9 @@
|
|||
#include <bpf/bpf_core_read.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
#include "net_shared.h"
|
||||
#include "xdp_sample_shared.h"
|
||||
|
||||
#define ETH_ALEN 6
|
||||
#define ETH_P_802_3_MIN 0x0600
|
||||
#define ETH_P_8021Q 0x8100
|
||||
#define ETH_P_8021AD 0x88A8
|
||||
#define ETH_P_IP 0x0800
|
||||
#define ETH_P_IPV6 0x86DD
|
||||
#define ETH_P_ARP 0x0806
|
||||
#define IPPROTO_ICMPV6 58
|
||||
|
||||
#define EINVAL 22
|
||||
#define ENETDOWN 100
|
||||
#define EMSGSIZE 90
|
||||
|
@ -55,18 +47,6 @@ static __always_inline void swap_src_dst_mac(void *data)
|
|||
p[5] = dst[2];
|
||||
}
|
||||
|
||||
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define bpf_ntohs(x) __builtin_bswap16(x)
|
||||
#define bpf_htons(x) __builtin_bswap16(x)
|
||||
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define bpf_ntohs(x) (x)
|
||||
#define bpf_htons(x) (x)
|
||||
#else
|
||||
# error "Endianness detection needs to be set up for your compiler?!"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Note: including linux/compiler.h or linux/kernel.h for the macros below
|
||||
* conflicts with vmlinux.h include in BPF files, so we define them here.
|
||||
|
|
Loading…
Reference in New Issue