diff options
author | Daniel T. Lee <danieltimlee@gmail.com> | 2023-01-15 16:16:13 +0900 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2023-01-15 13:32:45 -0800 |
commit | e04946f54cd99fa1bd92e22f4540720d76d88058 (patch) | |
tree | 059e4cedf8d2015617268672584b31c5db88c6ff /samples/bpf/test_cgrp2_tc.bpf.c | |
parent | e8acf8f47a5d58a00fbfa0f3592bbaaff557cec3 (diff) | |
download | linux-e04946f54cd99fa1bd92e22f4540720d76d88058.tar.gz linux-e04946f54cd99fa1bd92e22f4540720d76d88058.tar.bz2 linux-e04946f54cd99fa1bd92e22f4540720d76d88058.zip |
samples/bpf: change _kern suffix to .bpf with BPF test programs
This commit changes the _kern suffix to .bpf with the BPF test programs.
With this modification, test programs will inherit the benefit of the
new CLANG-BPF compile target.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-11-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'samples/bpf/test_cgrp2_tc.bpf.c')
-rw-r--r-- | samples/bpf/test_cgrp2_tc.bpf.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/samples/bpf/test_cgrp2_tc.bpf.c b/samples/bpf/test_cgrp2_tc.bpf.c new file mode 100644 index 000000000000..c7d2291d676f --- /dev/null +++ b/samples/bpf/test_cgrp2_tc.bpf.c @@ -0,0 +1,56 @@ +/* Copyright (c) 2016 Facebook + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ +#define KBUILD_MODNAME "foo" +#include "vmlinux.h" +#include "net_shared.h" +#include <bpf/bpf_helpers.h> + +/* copy of 'struct ethhdr' without __packed */ +struct eth_hdr { + unsigned char h_dest[ETH_ALEN]; + unsigned char h_source[ETH_ALEN]; + unsigned short h_proto; +}; + +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) +{ + void *data = (void *)(long)skb->data; + struct eth_hdr *eth = data; + struct ipv6hdr *ip6h = data + sizeof(*eth); + void *data_end = (void *)(long)skb->data_end; + char dont_care_msg[] = "dont care %04x %d\n"; + char pass_msg[] = "pass\n"; + char reject_msg[] = "reject\n"; + + /* single length check */ + if (data + sizeof(*eth) + sizeof(*ip6h) > data_end) + return TC_ACT_OK; + + 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); + return TC_ACT_OK; + } else if (bpf_skb_under_cgroup(skb, &test_cgrp2_array_pin, 0) != 1) { + bpf_trace_printk(pass_msg, sizeof(pass_msg)); + return TC_ACT_OK; + } else { + bpf_trace_printk(reject_msg, sizeof(reject_msg)); + return TC_ACT_SHOT; + } +} + +char _license[] SEC("license") = "GPL"; |