Commit dfff86f8 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

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]: commit ac5a72ea ("bpf: Use dedicated bpf_trace_printk event instead of trace_printk()")
[3]: commit ceb5dea5 ("samples: bpf: Remove bpf_load loader completely")
[4]: commit 14c17463 ("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: default avatarAlexei Starovoitov <ast@kernel.org>
parents 81bbbb69 e04946f5
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -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
+8 −21
Original line number Diff line number Diff line
@@ -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)
{
+2 −2
Original line number Diff line number Diff line
@@ -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
+32 −0
Original line number Diff line number Diff line
// 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
+11 −13
Original line number Diff line number Diff line
#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;

Loading