Skip to content

Commit 51354f7

Browse files
jrfastabborkmann
authored andcommitted
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: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Link: https://lore.kernel.org/bpf/20231129012557.95371-3-john.fastabend@gmail.com
1 parent 8866730 commit 51354f7

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

tools/testing/selftests/bpf/prog_tests/sockmap_listen.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,8 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
13371337
}
13381338

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

1355+
if (nop_mapfd >= 0) {
1356+
err = add_to_sockmap(nop_mapfd, cli0, cli1);
1357+
if (err)
1358+
return;
1359+
}
1360+
13541361
n = write(cli1, "a", 1);
13551362
if (n < 0)
13561363
FAIL_ERRNO("%s: write", log_prefix);
@@ -1387,7 +1394,7 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd,
13871394
goto close0;
13881395
c1 = sfd[0], p1 = sfd[1];
13891396

1390-
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
1397+
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);
13911398

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

1680-
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
1687+
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);
16811688

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

1738-
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
1745+
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, -1, verd_mapfd, mode);
17391746

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

1773-
static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
1774-
int verd_mapfd, enum redir_mode mode)
1780+
static void unix_inet_redir_to_connected(int family, int type,
1781+
int sock_mapfd, int nop_mapfd,
1782+
int verd_mapfd,
1783+
enum redir_mode mode)
17751784
{
17761785
int c0, c1, p0, p1;
17771786
int sfd[2];
@@ -1785,7 +1794,8 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
17851794
goto close_cli0;
17861795
c1 = sfd[0], p1 = sfd[1];
17871796

1788-
pairs_redir_to_connected(c0, p0, c1, p1, sock_mapfd, verd_mapfd, mode);
1797+
pairs_redir_to_connected(c0, p0, c1, p1,
1798+
sock_mapfd, nop_mapfd, verd_mapfd, mode);
17891799

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

18101821
skel->bss->test_ingress = false;
1811-
unix_inet_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map,
1822+
unix_inet_redir_to_connected(family, SOCK_DGRAM,
1823+
sock_map, -1, verdict_map,
18121824
REDIR_EGRESS);
1813-
unix_inet_redir_to_connected(family, SOCK_STREAM, sock_map, verdict_map,
1825+
unix_inet_redir_to_connected(family, SOCK_DGRAM,
1826+
sock_map, -1, verdict_map,
1827+
REDIR_EGRESS);
1828+
1829+
unix_inet_redir_to_connected(family, SOCK_DGRAM,
1830+
sock_map, nop_map, verdict_map,
1831+
REDIR_EGRESS);
1832+
unix_inet_redir_to_connected(family, SOCK_STREAM,
1833+
sock_map, nop_map, verdict_map,
18141834
REDIR_EGRESS);
18151835
skel->bss->test_ingress = true;
1816-
unix_inet_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map,
1836+
unix_inet_redir_to_connected(family, SOCK_DGRAM,
1837+
sock_map, -1, verdict_map,
1838+
REDIR_INGRESS);
1839+
unix_inet_redir_to_connected(family, SOCK_STREAM,
1840+
sock_map, -1, verdict_map,
1841+
REDIR_INGRESS);
1842+
1843+
unix_inet_redir_to_connected(family, SOCK_DGRAM,
1844+
sock_map, nop_map, verdict_map,
18171845
REDIR_INGRESS);
1818-
unix_inet_redir_to_connected(family, SOCK_STREAM, sock_map, verdict_map,
1846+
unix_inet_redir_to_connected(family, SOCK_STREAM,
1847+
sock_map, nop_map, verdict_map,
18191848
REDIR_INGRESS);
18201849

18211850
xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT);

tools/testing/selftests/bpf/progs/test_sockmap_listen.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ struct {
1414
__type(value, __u64);
1515
} sock_map SEC(".maps");
1616

17+
struct {
18+
__uint(type, BPF_MAP_TYPE_SOCKMAP);
19+
__uint(max_entries, 2);
20+
__type(key, __u32);
21+
__type(value, __u64);
22+
} nop_map SEC(".maps");
23+
1724
struct {
1825
__uint(type, BPF_MAP_TYPE_SOCKHASH);
1926
__uint(max_entries, 2);

0 commit comments

Comments
 (0)