Subject: Re: [PATCH v2 bpf-next 4/6] selftests/bpf: Add IPv4 and IPv6 sockaddr test cases

From: Martin KaFai Lau <[email-masked]>
Date: Mon, 15 Apr 2024 23:07:19 -0700

On 4/12/24 9:52 AM, Jordan Rife wrote:
> This patch lays the groundwork for testing IPv4 and IPv6 sockaddr hooks
> and their interaction with both socket syscalls and kernel functions
> (e.g. kernel_connect, kernel_bind, etc.) and moves the test cases from
> the old-style bpf/test_sock_addr.c self test into the sock_addr
> prog_test.

Thanks for moving the existing sock_addr test to the test_progs.

> +static int ping_once(int ipv, const char *addr)
> +{
> +	const char *ping_cmd_prefix = "ping -";
> +
> +	if (!SYS_NOFAIL("type ping%d >/dev/null 2>&1", ipv))
> +		ping_cmd_prefix = "ping";
> +
> +	return SYS_NOFAIL("%s%d -q -c 1 -W 1 %s >/dev/null 2>&1",
> +			  ping_cmd_prefix, ipv, addr);
> +}
> +
> +static int setup_test_env(void)
> +{
> +	SYS(err, "ip link add dev %s1 type veth peer name %s2", TEST_IF_PREFIX,
> +	    TEST_IF_PREFIX);

I would like to take this chance to simplify the setup.

Does it need a veth pair? The %s2 interface is not used.

Can it be done in lo alone?

Also, all this setup (and test) has to be done in a new netns. Anything blocking 
the kfunc in patch 2 using the current task netns instead of the init_net?

> +	SYS(err, "ip link set %s1 up", TEST_IF_PREFIX);
> +	SYS(err, "ip link set %s2 up", TEST_IF_PREFIX);
> +	SYS(err, "ip -4 addr add %s/8 dev %s1", TEST_IPV4, TEST_IF_PREFIX);
> +	SYS(err, "ip -6 addr add %s/128 dev %s1", TEST_IPV6, TEST_IF_PREFIX);

Add nodad to the "ip -6 addr add...". just in case it may add unnecessary delay.

> +
> +	int i;
> +
> +	for (i = 0; i < 5; i++) {
> +		if (!ping_once(4, TEST_IPV4) && !ping_once(6, TEST_IPV6))

This interface/address ping should not be needed. Other tests under prog_tests/ 
don't need this interface/address ping also.

> +			return 0;
> +	}
> +
> +	ASSERT_FAIL("Timed out waiting for test IP to become available.");
> +err:
> +	return -1;
> +}
> +
> +static void cleanup_test_env(void)
> +{
> +	SYS_NOFAIL("ip link del %s1 2>/dev/null", TEST_IF_PREFIX);
> +	SYS_NOFAIL("ip link del %s2 2>/dev/null", TEST_IF_PREFIX);

Using lo will make this easier. Regardless, the link should go away with the netns.

> +}
> +
>   void test_sock_addr(void)
>   {
>   	int cgroup_fd = -1;
>   	void *skel;
>   
> +	if (!ASSERT_OK(setup_test_env(), "setup_test_env"))

This will probably have to be called after the test__join_cgroup() if it also 
creates a new netns.

pw-bot: cr

> +		goto cleanup;
> +
>   	cgroup_fd = test__join_cgroup("/sock_addr");
>   	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
>   		goto cleanup;
> @@ -609,4 +755,5 @@ void test_sock_addr(void)
>   cleanup:
>   	if (cgroup_fd >= 0)
>   		close(cgroup_fd);
> +	cleanup_test_env();
>   }