From: Benjamin Coddington A classifier for RPC service transports that looks the peer address up in an LPM trie keyed by {family, address}, the reference for the sunrpc svc_classifier struct_ops, and tests of the attach model: a second attach in the same network namespace fails with -EBUSY, a link update replaces the classifier, a detached instance can be attached again, a classifier attached inside a namespace does not occupy the initial one, and a namespace that exits leaves its link to detach into nothing. Skipped where the struct_ops is not registered. Signed-off-by: Benjamin Coddington --- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/svc_classifier.c | 104 ++++++++++++++++++ .../selftests/bpf/progs/bpf_svc_classifier.c | 80 ++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/svc_classifier.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_svc_classifier.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index ea7044f30adc..2cb7080d7da2 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -114,6 +114,7 @@ CONFIG_IP_NF_IPTABLES=y CONFIG_IP6_NF_IPTABLES=y CONFIG_IP6_NF_FILTER=y CONFIG_NF_NAT=y +CONFIG_NFS_FS=y CONFIG_PACKET=y CONFIG_RC_CORE=y CONFIG_SAMPLES=y @@ -133,6 +134,7 @@ CONFIG_TCP_CONG_BBR=y CONFIG_INFINIBAND=y CONFIG_SMC=y CONFIG_SMC_HS_CTRL_BPF=y +CONFIG_SUNRPC_BPF_CLASSIFY=y CONFIG_DIBS=y CONFIG_DIBS_LO=y CONFIG_PM_WAKELOCKS=y diff --git a/tools/testing/selftests/bpf/prog_tests/svc_classifier.c b/tools/testing/selftests/bpf/prog_tests/svc_classifier.c new file mode 100644 index 000000000000..887af29b21c7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/svc_classifier.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The svc_classifier struct_ops: one per network namespace, bound to + * the attaching task's namespace; replace by link update; detach by + * link close; a namespace that exits leaves its link empty. + */ +#include +#include "bpf_svc_classifier.skel.h" + +static struct bpf_svc_classifier *load(void) +{ + struct bpf_svc_classifier *skel; + + skel = bpf_svc_classifier__open_and_load(); + if (!skel && (errno == ENOENT || errno == EINVAL || errno == EOPNOTSUPP)) { + test__skip(); + return NULL; + } + ASSERT_OK_PTR(skel, "bpf_svc_classifier__open_and_load"); + return skel; +} + +static void test_attach(void) +{ + struct bpf_svc_classifier *one, *two; + struct bpf_link *link, *busy; + int err; + + one = load(); + if (!one) + return; + two = load(); + if (!two) + goto out_one; + + link = bpf_map__attach_struct_ops(one->maps.prefix); + if (!ASSERT_OK_PTR(link, "attach")) + goto out_two; + + busy = bpf_map__attach_struct_ops(two->maps.prefix); + ASSERT_ERR_PTR(busy, "second attach"); + ASSERT_EQ(errno, EBUSY, "second attach errno"); + + err = bpf_link__update_map(link, two->maps.prefix); + ASSERT_OK(err, "replace"); + + /* the replaced instance can be attached again once released */ + bpf_link__destroy(link); + link = bpf_map__attach_struct_ops(one->maps.prefix); + ASSERT_OK_PTR(link, "attach after detach"); + bpf_link__destroy(link); +out_two: + bpf_svc_classifier__destroy(two); +out_one: + bpf_svc_classifier__destroy(one); +} + +static void test_netns(void) +{ + struct bpf_svc_classifier *inner, *outer; + struct netns_obj *ns; + struct bpf_link *link, *link2; + + inner = load(); + if (!inner) + return; + outer = load(); + if (!outer) + goto out_inner; + + ns = netns_new("svc_classifier_ns", true); + if (!ASSERT_OK_PTR(ns, "netns_new")) + goto out_outer; + + /* attached inside the namespace */ + link = bpf_map__attach_struct_ops(inner->maps.prefix); + if (!ASSERT_OK_PTR(link, "attach in netns")) + goto out_ns; + + /* the initial namespace is free */ + netns_free(ns); + ns = NULL; + link2 = bpf_map__attach_struct_ops(outer->maps.prefix); + ASSERT_OK_PTR(link2, "attach in init_net"); + bpf_link__destroy(link2); + + /* the link of the dead namespace detaches into nothing */ + bpf_link__destroy(link); +out_ns: + if (ns) + netns_free(ns); +out_outer: + bpf_svc_classifier__destroy(outer); +out_inner: + bpf_svc_classifier__destroy(inner); +} + +void test_svc_classifier(void) +{ + if (test__start_subtest("attach")) + test_attach(); + if (test__start_subtest("netns")) + test_netns(); +} diff --git a/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c b/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c new file mode 100644 index 000000000000..bae9d115ee74 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A svc_classifier for RPC service transports: the peer address is looked + * up in an LPM trie keyed by {family, address}; a hit returns the class + * word stored there, a miss returns 0, the anonymous client. A key of + * prefixlen 0 is the default for every family, 8 the default for one. + */ +#include +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +#define AF_INET 2 +#define AF_INET6 10 + +#define SVC_CLASSIFIER_NAME_MAX 16 + +struct svc_xprt___local { + struct __kernel_sockaddr_storage xpt_remote; +} __attribute__((preserve_access_index)); + +struct svc_classifier___local { + char name[SVC_CLASSIFIER_NAME_MAX]; + __u32 (*classify)(const struct svc_xprt___local *xprt); +}; + +struct prefix_key { + __u32 prefixlen; /* 8 (the family byte) + address bits */ + __u8 family; + __u8 addr[16]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_LPM_TRIE); + __type(key, struct prefix_key); + __type(value, __u32); + __uint(max_entries, 1024); + __uint(map_flags, BPF_F_NO_PREALLOC); +} prefixes SEC(".maps"); + +__u64 classified; + +SEC("struct_ops/classify") +__u32 BPF_PROG(prefix_classify, const struct svc_xprt___local *xprt) +{ + struct __kernel_sockaddr_storage ss; + struct prefix_key key = {}; + __u32 *class; + + if (bpf_core_read(&ss, sizeof(ss), &xprt->xpt_remote)) + return 0; + + key.family = ss.ss_family; + switch (ss.ss_family) { + case AF_INET: + /* struct sockaddr_in: port at 2, address at 4 */ + __builtin_memcpy(key.addr, &ss.__data[2], 4); + key.prefixlen = 8 + 32; + break; + case AF_INET6: + /* struct sockaddr_in6: port at 2, flowinfo at 4, address at 8 */ + __builtin_memcpy(key.addr, &ss.__data[6], 16); + key.prefixlen = 8 + 128; + break; + default: + return 0; + } + + __sync_fetch_and_add(&classified, 1); + class = bpf_map_lookup_elem(&prefixes, &key); + return class ? *class : 0; +} + +SEC(".struct_ops.link") +struct svc_classifier___local prefix = { + .name = "prefix", + .classify = (void *)prefix_classify, +}; -- 2.53.0