Commit 51354f70 authored by John Fastabend's avatar John Fastabend Committed by Daniel Borkmann
Browse files

bpf, sockmap: Add af_unix test with both sockets in map



This adds a test where both pairs of a af_unix paired socket are put into a
BPF map. This ensures that when we tear down the af_unix pair we don't have
any issues on sockmap side with ordering and reference counting.

Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Link: https://lore.kernel.org/bpf/20231129012557.95371-3-john.fastabend@gmail.com
parent 8866730a
Loading
Loading
Loading
Loading
+40 −11
Original line number Diff line number Diff line
@@ -1337,7 +1337,8 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
}

static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
				     int sock_mapfd, int verd_mapfd, enum redir_mode mode)
				     int sock_mapfd, int nop_mapfd,
				     int verd_mapfd, enum redir_mode mode)
{
	const char *log_prefix = redir_mode_str(mode);
	unsigned int pass;
@@ -1351,6 +1352,12 @@ static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
	if (err)
		return;

	if (nop_mapfd >= 0) {
		err = add_to_sockmap(nop_mapfd, cli0, cli1);
		if (err)
			return;
	}

	n = write(cli1, "a", 1);
	if (n < 0)
		FAIL_ERRNO("%s: write", log_prefix);
@@ -1387,7 +1394,7 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd,
		goto close0;
	c1 = sfd[0], p1 = sfd[1];

	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);

	xclose(c1);
	xclose(p1);
@@ -1677,7 +1684,7 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
	if (err)
		goto close_cli0;

	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);

	xclose(c1);
	xclose(p1);
@@ -1735,7 +1742,7 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd,
	if (err)
		goto close;

	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);

	xclose(c1);
	xclose(p1);
@@ -1770,8 +1777,10 @@ static void inet_unix_skb_redir_to_connected(struct test_sockmap_listen *skel,
	xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT);
}

static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
					int verd_mapfd, enum redir_mode mode)
static void unix_inet_redir_to_connected(int family, int type,
					int sock_mapfd, int nop_mapfd,
					int verd_mapfd,
					enum redir_mode mode)
{
	int c0, c1, p0, p1;
	int sfd[2];
@@ -1785,7 +1794,8 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
		goto close_cli0;
	c1 = sfd[0], p1 = sfd[1];

	pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
	pairs_redir_to_connected(c0, p0, c1, p1,
				 sock_mapfd, nop_mapfd, verd_mapfd, mode);

	xclose(c1);
	xclose(p1);
@@ -1799,6 +1809,7 @@ static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
					    struct bpf_map *inner_map, int family)
{
	int verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
	int nop_map = bpf_map__fd(skel->maps.nop_map);
	int verdict_map = bpf_map__fd(skel->maps.verdict_map);
	int sock_map = bpf_map__fd(inner_map);
	int err;
@@ -1808,14 +1819,32 @@ static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
		return;

	skel->bss->test_ingress = false;
	unix_inet_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map,
	unix_inet_redir_to_connected(family, SOCK_DGRAM,
				     sock_map, -1, verdict_map,
				     REDIR_EGRESS);
	unix_inet_redir_to_connected(family, SOCK_STREAM, sock_map, verdict_map,
	unix_inet_redir_to_connected(family, SOCK_DGRAM,
				     sock_map, -1, verdict_map,
				     REDIR_EGRESS);

	unix_inet_redir_to_connected(family, SOCK_DGRAM,
				     sock_map, nop_map, verdict_map,
				     REDIR_EGRESS);
	unix_inet_redir_to_connected(family, SOCK_STREAM,
				     sock_map, nop_map, verdict_map,
				     REDIR_EGRESS);
	skel->bss->test_ingress = true;
	unix_inet_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map,
	unix_inet_redir_to_connected(family, SOCK_DGRAM,
				     sock_map, -1, verdict_map,
				     REDIR_INGRESS);
	unix_inet_redir_to_connected(family, SOCK_STREAM,
				     sock_map, -1, verdict_map,
				     REDIR_INGRESS);

	unix_inet_redir_to_connected(family, SOCK_DGRAM,
				     sock_map, nop_map, verdict_map,
				     REDIR_INGRESS);
	unix_inet_redir_to_connected(family, SOCK_STREAM, sock_map, verdict_map,
	unix_inet_redir_to_connected(family, SOCK_STREAM,
				     sock_map, nop_map, verdict_map,
				     REDIR_INGRESS);

	xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT);
+7 −0
Original line number Diff line number Diff line
@@ -14,6 +14,13 @@ struct {
	__type(value, __u64);
} sock_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SOCKMAP);
	__uint(max_entries, 2);
	__type(key, __u32);
	__type(value, __u64);
} nop_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SOCKHASH);
	__uint(max_entries, 2);