2019-03-23 07:40:19 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/if_ether.h>
|
selftests/bpf: add bpf-gcc support
Now that binutils and gcc support for BPF is upstream, make use of it in
BPF selftests using alu32-like approach. Share as much as possible of
CFLAGS calculation with clang.
Fixes only obvious issues, leaving more complex ones for later:
- Use gcc-provided bpf-helpers.h instead of manually defining the
helpers, change bpf_helpers.h include guard to avoid conflict.
- Include <linux/stddef.h> for __always_inline.
- Add $(OUTPUT)/../usr/include to include path in order to use local
kernel headers instead of system kernel headers when building with O=.
In order to activate the bpf-gcc support, one needs to configure
binutils and gcc with --target=bpf and make them available in $PATH. In
particular, gcc must be installed as `bpf-gcc`, which is the default.
Right now with binutils 25a2915e8dba and gcc r275589 only a handful of
tests work:
# ./test_progs_bpf_gcc
# Summary: 7/39 PASSED, 1 SKIPPED, 98 FAILED
The reason for those failures are as follows:
- Build errors:
- `error: too many function arguments for eBPF` for __always_inline
functions read_str_var and read_map_var - must be inlining issue,
and for process_l3_headers_v6, which relies on optimizing away
function arguments.
- `error: indirect call in function, which are not supported by eBPF`
where there are no obvious indirect calls in the source calls, e.g.
in __encap_ipip_none.
- `error: field 'lock' has incomplete type` for fields of `struct
bpf_spin_lock` type - bpf_spin_lock is re#defined by bpf-helpers.h,
so its usage is sensitive to order of #includes.
- `error: eBPF stack limit exceeded` in sysctl_tcp_mem.
- Load errors:
- Missing object files due to above build errors.
- `libbpf: failed to create map (name: 'test_ver.bss')`.
- `libbpf: object file doesn't contain bpf program`.
- `libbpf: Program '.text' contains unrecognized relo data pointing to
section 0`.
- `libbpf: BTF is required, but is missing or corrupted` - no BTF
support in gcc yet.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Jose E. Marchesi <jose.marchesi@oracle.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-09-13 00:05:43 +08:00
|
|
|
#include <linux/stddef.h>
|
2019-03-23 07:40:19 +08:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/pkt_cls.h>
|
|
|
|
#include <linux/tcp.h>
|
2020-01-20 21:06:45 +08:00
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
#include <bpf/bpf_endian.h>
|
2019-03-23 07:40:19 +08:00
|
|
|
|
|
|
|
/* the maximum delay we are willing to add (drop packets beyond that) */
|
|
|
|
#define TIME_HORIZON_NS (2000 * 1000 * 1000)
|
|
|
|
#define NS_PER_SEC 1000000000
|
|
|
|
#define ECN_HORIZON_NS 5000000
|
|
|
|
#define THROTTLE_RATE_BPS (5 * 1000 * 1000)
|
|
|
|
|
|
|
|
/* flow_key => last_tstamp timestamp used */
|
2022-01-20 14:05:27 +08:00
|
|
|
struct {
|
|
|
|
__uint(type, BPF_MAP_TYPE_HASH);
|
|
|
|
__type(key, uint32_t);
|
|
|
|
__type(value, uint64_t);
|
|
|
|
__uint(max_entries, 1);
|
|
|
|
} flow_map SEC(".maps");
|
2019-03-23 07:40:19 +08:00
|
|
|
|
|
|
|
static inline int throttle_flow(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
int key = 0;
|
|
|
|
uint64_t *last_tstamp = bpf_map_lookup_elem(&flow_map, &key);
|
|
|
|
uint64_t delay_ns = ((uint64_t)skb->len) * NS_PER_SEC /
|
|
|
|
THROTTLE_RATE_BPS;
|
|
|
|
uint64_t now = bpf_ktime_get_ns();
|
|
|
|
uint64_t tstamp, next_tstamp = 0;
|
|
|
|
|
|
|
|
if (last_tstamp)
|
|
|
|
next_tstamp = *last_tstamp + delay_ns;
|
|
|
|
|
|
|
|
tstamp = skb->tstamp;
|
|
|
|
if (tstamp < now)
|
|
|
|
tstamp = now;
|
|
|
|
|
|
|
|
/* should we throttle? */
|
|
|
|
if (next_tstamp <= tstamp) {
|
|
|
|
if (bpf_map_update_elem(&flow_map, &key, &tstamp, BPF_ANY))
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
return TC_ACT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do not queue past the time horizon */
|
|
|
|
if (next_tstamp - now >= TIME_HORIZON_NS)
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
|
|
|
|
/* set ecn bit, if needed */
|
|
|
|
if (next_tstamp - now >= ECN_HORIZON_NS)
|
|
|
|
bpf_skb_ecn_set_ce(skb);
|
|
|
|
|
|
|
|
if (bpf_map_update_elem(&flow_map, &key, &next_tstamp, BPF_EXIST))
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
skb->tstamp = next_tstamp;
|
|
|
|
|
|
|
|
return TC_ACT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int handle_tcp(struct __sk_buff *skb, struct tcphdr *tcp)
|
|
|
|
{
|
|
|
|
void *data_end = (void *)(long)skb->data_end;
|
|
|
|
|
|
|
|
/* drop malformed packets */
|
|
|
|
if ((void *)(tcp + 1) > data_end)
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
|
|
|
|
if (tcp->dest == bpf_htons(9000))
|
|
|
|
return throttle_flow(skb);
|
|
|
|
|
|
|
|
return TC_ACT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int handle_ipv4(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
void *data_end = (void *)(long)skb->data_end;
|
|
|
|
void *data = (void *)(long)skb->data;
|
|
|
|
struct iphdr *iph;
|
|
|
|
uint32_t ihl;
|
|
|
|
|
|
|
|
/* drop malformed packets */
|
|
|
|
if (data + sizeof(struct ethhdr) > data_end)
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
iph = (struct iphdr *)(data + sizeof(struct ethhdr));
|
|
|
|
if ((void *)(iph + 1) > data_end)
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
ihl = iph->ihl * 4;
|
|
|
|
if (((void *)iph) + ihl > data_end)
|
|
|
|
return TC_ACT_SHOT;
|
|
|
|
|
|
|
|
if (iph->protocol == IPPROTO_TCP)
|
|
|
|
return handle_tcp(skb, (struct tcphdr *)(((void *)iph) + ihl));
|
|
|
|
|
|
|
|
return TC_ACT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("cls_test") int tc_prog(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
if (skb->protocol == bpf_htons(ETH_P_IP))
|
|
|
|
return handle_ipv4(skb);
|
|
|
|
|
|
|
|
return TC_ACT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char __license[] SEC("license") = "GPL";
|