From: Martin KaFai Lau <martin.lau@linux.dev>
To: Amery Hung <ameryhung@gmail.com>
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
alexei.starovoitov@gmail.com, andrii@kernel.org,
daniel@iogearbox.net, martin.lau@kernel.org,
xiyou.wangcong@gmail.com, kernel-team@meta.com
Subject: Re: [PATCH bpf-next/net v1 2/5] selftests/bpf: Test setting and creating bpf qdisc as default qdisc
Date: Thu, 1 May 2025 16:58:09 -0700 [thread overview]
Message-ID: <83c8f387-c4a9-4293-9996-fec285d34c94@linux.dev> (raw)
In-Reply-To: <20250501223025.569020-3-ameryhung@gmail.com>
On 5/1/25 3:30 PM, Amery Hung wrote:
> First, test that bpf qdisc can be set as default qdisc. Then, attach
> an mq qdisc to see if bpf qdisc can be successfully created and grafted.
>
> The test is a sequential test as net.core.default_qdisc is global.
>
> Signed-off-by: Amery Hung <ameryhung@gmail.com>
> ---
> .../selftests/bpf/prog_tests/bpf_qdisc.c | 78 +++++++++++++++++++
> 1 file changed, 78 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c b/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c
> index c9a54177c84e..c954cc2ae64f 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c
> @@ -159,6 +159,79 @@ static void test_qdisc_attach_to_non_root(void)
> bpf_qdisc_fifo__destroy(fifo_skel);
> }
>
> +static int get_default_qdisc(char *qdisc_name)
> +{
> + FILE *f;
> + int num;
> +
> + f = fopen("/proc/sys/net/core/default_qdisc", "r");
> + if (!f)
> + return -errno;
> +
> + num = fscanf(f, "%s", qdisc_name);
> + fclose(f);
> +
> + return num == 1 ? 0 : -EFAULT;
> +}
> +
> +static void test_default_qdisc_attach_to_mq(void)
> +{
> + struct bpf_qdisc_fifo *fifo_skel;
> + char default_qdisc[IFNAMSIZ];
> + struct netns_obj *netns;
> + char tc_qdisc_show[64];
> + struct bpf_link *link;
> + char *str_ret;
> + FILE *tc;
> + int err;
> +
> + fifo_skel = bpf_qdisc_fifo__open_and_load();
> + if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
> + return;
> +
> + link = bpf_map__attach_struct_ops(fifo_skel->maps.fifo);
fifo_skel->links.fifo = bpf_map__attach_struct_ops(....);
Then no need to bpf_link__destroy(link). bpf_qdisc_fifo__destroy() should do.
> + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
> + bpf_qdisc_fifo__destroy(fifo_skel);
> + return;
> + }
> +
> + err = get_default_qdisc(default_qdisc);
> + if (!ASSERT_OK(err, "read sysctl net.core.default_qdisc"))
> + goto out;
> +
> + err = write_sysctl("/proc/sys/net/core/default_qdisc", "bpf_fifo");
> + if (!ASSERT_OK(err, "write sysctl net.core.default_qdisc"))
> + goto out;
> +
> + netns = netns_new("bpf_qdisc_ns", true);
> + if (!ASSERT_OK_PTR(netns, "netns_new"))
> + goto out;
This should be 'goto out_restore_dflt_qdisc'.
I would stay with minimum number of cleanup labels if possible. Initialize the
variables that need to be cleaned up instead. There is no need to optimize each
cleanup case for a test,
e.g. "struct netns_obj netns = NULL; char default_qdisc[IFNAMSIZ] = {};..."
> +
> + SYS(out_restore_dflt_qdisc, "ip link add veth0 type veth peer veth1");
> + SYS(out_delete_netns, "tc qdisc add dev veth0 root handle 1: mq");
> +
> + tc = popen("tc qdisc show dev veth0 parent 1:1", "r");
> + if (!ASSERT_OK_PTR(tc, "tc qdisc show dev veth0 parent 1:1"))
> + goto out_delete_netns;
> +
> + str_ret = fgets(tc_qdisc_show, sizeof(tc_qdisc_show), tc);
> + if (!ASSERT_OK_PTR(str_ret, "tc qdisc show dev veth0 parent 1:1"))
> + goto out_delete_netns;
> +
> + str_ret = strstr(tc_qdisc_show, "qdisc bpf_fifo");
> + if (!ASSERT_OK_PTR(str_ret, "check if bpf_fifo is created"))
> + goto out_delete_netns;
Instead of pipe and grep, how about having the bpf_fifo_init bpf prog to set a
global variable when called and then check the "fifo_skel->bss->init_called ==
true" here?
> +
> + SYS(out_delete_netns, "tc qdisc delete dev veth0 root mq");
> +out_delete_netns:
> + netns_free(netns);
> +out_restore_dflt_qdisc:
> + write_sysctl("/proc/sys/net/core/default_qdisc", default_qdisc);
> +out:
> + bpf_link__destroy(link);
> + bpf_qdisc_fifo__destroy(fifo_skel);
> +}
> +
> void test_bpf_qdisc(void)
> {
> struct netns_obj *netns;
> @@ -178,3 +251,8 @@ void test_bpf_qdisc(void)
>
> netns_free(netns);
> }
> +
> +void serial_test_bpf_qdisc_default(void)
> +{
> + test_default_qdisc_attach_to_mq();
> +}
next prev parent reply other threads:[~2025-05-01 23:58 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-01 22:30 [PATCH bpf-next/net v1 0/5] Fix bpf qdisc bugs and cleanup Amery Hung
2025-05-01 22:30 ` [PATCH bpf-next/net v1 1/5] bpf: net_sched: Fix bpf qdisc init prologue when set as default qdisc Amery Hung
2025-05-02 9:32 ` Simon Horman
2025-05-01 22:30 ` [PATCH bpf-next/net v1 2/5] selftests/bpf: Test setting and creating bpf qdisc " Amery Hung
2025-05-01 23:58 ` Martin KaFai Lau [this message]
2025-05-02 17:52 ` Amery Hung
2025-05-02 19:27 ` Martin KaFai Lau
2025-05-02 19:33 ` Amery Hung
2025-05-01 22:30 ` [PATCH bpf-next/net v1 3/5] bpf: net_sched: Make some Qdisc_ops ops mandatory Amery Hung
2025-05-02 0:13 ` Martin KaFai Lau
2025-05-01 22:30 ` [PATCH bpf-next/net v1 4/5] selftests/bpf: selftests/bpf: Test attaching a bpf qdisc with incomplete operators Amery Hung
2025-05-01 22:30 ` [PATCH bpf-next/net v1 5/5] selftests/bpf: Cleanup bpf qdisc selftests Amery Hung
2025-05-02 0:22 ` Martin KaFai Lau
2025-05-02 17:53 ` Amery Hung
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=83c8f387-c4a9-4293-9996-fec285d34c94@linux.dev \
--to=martin.lau@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=xiyou.wangcong@gmail.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).