From: Yonghong Song <yhs@fb.com>
To: Rumen Telbizov <rumen.telbizov@menlosecurity.com>, <bpf@vger.kernel.org>
Cc: <dsahern@gmail.com>
Subject: Re: [PATCH 3/3] selftests: Add selftests for fwmark support in bpf_fib_lookup
Date: Tue, 29 Jun 2021 16:08:54 -0700 [thread overview]
Message-ID: <19658dd5-d872-1c9e-475a-120d0674ad7a@fb.com> (raw)
In-Reply-To: <20210629185537.78008-4-rumen.telbizov@menlosecurity.com>
On 6/29/21 11:55 AM, Rumen Telbizov wrote:
> Add selftests for ensuring:
> * IPv4 route match according to ip rule fwmark
> * IPv6 route match according to ip rule fwmark
>
> Signed-off-by: Rumen Telbizov <rumen.telbizov@menlosecurity.com>
> ---
> tools/testing/selftests/bpf/Makefile | 1 +
> .../selftests/bpf/progs/test_bpf_fib_lookup.c | 135 ++++++++++++++
> .../selftests/bpf/test_bpf_fib_lookup.sh | 166 ++++++++++++++++++
> 3 files changed, 302 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/progs/test_bpf_fib_lookup.c
> create mode 100755 tools/testing/selftests/bpf/test_bpf_fib_lookup.sh
It will be great if you can incorperate the test into test_progs.
You can see selftests/bpf/prog_tests/sk_assign.c as an example.
>
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index 511259c2c6c5..afbac539e20d 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -73,6 +73,7 @@ TEST_PROGS := test_kmod.sh \
> test_bpftool_build.sh \
> test_bpftool.sh \
> test_bpftool_metadata.sh \
> + test_bpf_fib_lookup.sh \
> test_doc_build.sh \
> test_xsk.sh
>
> diff --git a/tools/testing/selftests/bpf/progs/test_bpf_fib_lookup.c b/tools/testing/selftests/bpf/progs/test_bpf_fib_lookup.c
> new file mode 100644
> index 000000000000..e4bbfb01ab86
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_bpf_fib_lookup.c
> @@ -0,0 +1,135 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * @author: Rumen Telbizov <telbizov@gmail.com> <rumen.telbizov@menlosecurity.com>
> + * @created: Wed Jun 23 17:33:19 UTC 2021
> + *
> + * @description:
> + * Perform tests against bpf_fib_lookup()
> + * Communicates the results back via the trace buffer for the calling script
> + * to parse - /sys/kernel/debug/tracing/trace
> + *
> + */
> +
> +#include <arpa/inet.h>
> +#include <linux/bpf.h>
> +#include <linux/pkt_cls.h>
> +#include <linux/ip.h>
> +#include <linux/ipv6.h>
> +#include <linux/if_ether.h>
> +#include <bpf/bpf_helpers.h>
> +
> +#define BPF_TRACE(fmt, ...) \
> +({ \
> + static const char ____fmt[] = fmt; \
> + bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \
> +})
> +
> +SEC("test_egress_ipv4_fwmark")
> +int __test_egress_ipv4_fwmark(struct __sk_buff *skb)
> +{
> + void *data = (void *)(long)skb->data;
> + void *data_end = (void *)(long)skb->data_end;
> + struct bpf_fib_lookup fib;
> + struct ethhdr *eth = data;
> + struct iphdr *ip = data + sizeof(*eth);
> +
> + if (data + sizeof(*eth) > data_end)
> + return TC_ACT_OK;
> +
> + if (eth->h_proto != htons(ETH_P_IP))
> + return TC_ACT_OK;
> +
> + if (data + sizeof(*eth) + sizeof(*ip) > data_end)
> + return TC_ACT_OK;
> +
> + if (ip->protocol != IPPROTO_ICMP)
> + return TC_ACT_OK;
> +
> + if (htonl(ip->daddr) != 0x01020304)
> + return TC_ACT_OK;
> +
> + __builtin_memset(&fib, 0x0, sizeof(fib));
> +
> + fib.family = AF_INET;
> + fib.l4_protocol = ip->protocol;
> + fib.tot_len = htons(ip->tot_len);
> + fib.ifindex = skb->ifindex;
> + fib.tos = ip->tos;
> + fib.ipv4_src = ip->saddr;
> + fib.ipv4_dst = ip->daddr;
> + fib.mark = skb->mark;
> +
> + if (bpf_fib_lookup(skb, &fib, sizeof(fib), 0) < 0)
> + return TC_ACT_OK;
> +
> + BPF_TRACE("<test_bpf_fib_lookup: test_egress_ipv4_fwmark> fib.ipv4_dst: <%x> mark: <%d>",
> + htonl(fib.ipv4_dst), skb->mark);
If you use test_progs framework, you don't need BPF_TRACE any more.
You can assign these values to global variables and compare them
in user space C program.
> + return TC_ACT_OK;
> +}
> +
> +SEC("test_egress_ipv6_fwmark")
> +int __test_egress_ipv6_fwmark(struct __sk_buff *skb)
> +{
> + void *data = (void *)(long)skb->data;
> + void *data_end = (void *)(long)skb->data_end;
> + struct in6_addr *src, *dst;
> + struct bpf_fib_lookup fib;
> + struct ethhdr *eth = data;
> + struct ipv6hdr *ip = data + sizeof(*eth);
> +
> + if (data + sizeof(*eth) > data_end)
> + return TC_ACT_OK;
> +
> + if (eth->h_proto != htons(ETH_P_IPV6))
> + return TC_ACT_OK;
> +
> + if (data + sizeof(*eth) + sizeof(*ip) > data_end)
> + return TC_ACT_OK;
> +
> + if (ip->nexthdr != IPPROTO_ICMPV6)
> + return TC_ACT_OK;
> +
> + /* 2000::2000 */
> + if (!(ntohs(ip->daddr.s6_addr16[0]) == 0x2000 &&
> + ntohs(ip->daddr.s6_addr16[1]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[2]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[3]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[4]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[5]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[6]) == 0x0000 &&
> + ntohs(ip->daddr.s6_addr16[7]) == 0x2000))
> + return TC_ACT_OK;
> +
> + __builtin_memset(&fib, 0x0, sizeof(fib));
> +
> + fib.family = AF_INET6;
> + fib.flowinfo = 0;
> + fib.l4_protocol = ip->nexthdr;
> + fib.tot_len = ntohs(ip->payload_len);
> + fib.ifindex = skb->ifindex;
> + fib.mark = skb->mark;
> +
> + src = (struct in6_addr *)fib.ipv6_src;
> + dst = (struct in6_addr *)fib.ipv6_dst;
> + *src = ip->saddr;
> + *dst = ip->daddr;
> +
> + if (bpf_fib_lookup(skb, &fib, sizeof(fib), 0) < 0)
> + return TC_ACT_OK;
> +
> + BPF_TRACE("<test_bpf_fib_lookup - egress_IPv6> fib.ipv6_dst<0-2>: <%04x:%04x:%04x>",
> + ntohs(dst->s6_addr16[0]), ntohs(dst->s6_addr16[1]),
> + ntohs(dst->s6_addr16[2])
> + );
> + BPF_TRACE("<test_bpf_fib_lookup - egress_IPv6> fib.ipv6_dst<3-5>: <%04x:%04x:%04x>",
> + ntohs(dst->s6_addr16[3]), ntohs(dst->s6_addr16[4]),
> + ntohs(dst->s6_addr16[5])
> + );
> + BPF_TRACE("<test_bpf_fib_lookup - egress_IPv6> fib.ipv6_dst<6-7>: <%04x:%04x> mark: <%d>",
> + ntohs(dst->s6_addr16[6]), ntohs(dst->s6_addr16[7]), skb->mark
> + );
> +
> + return TC_ACT_OK;
> +}
> +
> +char __license[] SEC("license") = "GPL";
> diff --git a/tools/testing/selftests/bpf/test_bpf_fib_lookup.sh b/tools/testing/selftests/bpf/test_bpf_fib_lookup.sh
> new file mode 100755
> index 000000000000..4b8cc984b486
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/test_bpf_fib_lookup.sh
> @@ -0,0 +1,166 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# @author: Rumen Telbizov <telbizov@gmail.com> <rumen.telbizov@menlosecurity.com>
> +# @created: Wed Jun 23 17:33:19 UTC 2021
> +# @description:
> +# Test coverage for bpf_fib_lookup():
> +# * IPv4 route match according to ip rule fwmark
> +# * IPv6 route match according to ip rule fwmark
> +#
> +
[...]
next prev parent reply other threads:[~2021-06-29 23:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-29 18:55 [PATCH 0/3] Add support for fwmark to bpf_fib_lookup Rumen Telbizov
2021-06-29 18:55 ` [PATCH 1/3] bpf: Add support for mark with bpf_fib_lookup Rumen Telbizov
2021-06-30 5:35 ` Greg KH
2021-06-30 14:26 ` David Ahern
2021-06-29 18:55 ` [PATCH 2/3] tools: Update bpf header Rumen Telbizov
2021-06-29 18:55 ` [PATCH 3/3] selftests: Add selftests for fwmark support in bpf_fib_lookup Rumen Telbizov
2021-06-29 23:08 ` Yonghong Song [this message]
-- strict thread matches above, loose matches on Subject: below --
2021-06-29 17:37 Rumen Telbizov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=19658dd5-d872-1c9e-475a-120d0674ad7a@fb.com \
--to=yhs@fb.com \
--cc=bpf@vger.kernel.org \
--cc=dsahern@gmail.com \
--cc=rumen.telbizov@menlosecurity.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).