Commit 0626bc2f authored by Cong Wang's avatar Cong Wang Committed by Alexei Starovoitov

selftests/bpf: Factor out add_to_sockmap()

Factor out a common helper add_to_sockmap() which adds two
sockets into a sockmap.
Signed-off-by: default avatarCong Wang <cong.wang@bytedance.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210704190252.11866-10-xiyou.wangcong@gmail.com
parent d950625c
...@@ -921,6 +921,23 @@ static const char *redir_mode_str(enum redir_mode mode) ...@@ -921,6 +921,23 @@ static const char *redir_mode_str(enum redir_mode mode)
} }
} }
static int add_to_sockmap(int sock_mapfd, int fd1, int fd2)
{
u64 value;
u32 key;
int err;
key = 0;
value = fd1;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err)
return err;
key = 1;
value = fd2;
return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
}
static void redir_to_connected(int family, int sotype, int sock_mapfd, static void redir_to_connected(int family, int sotype, int sock_mapfd,
int verd_mapfd, enum redir_mode mode) int verd_mapfd, enum redir_mode mode)
{ {
...@@ -930,7 +947,6 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, ...@@ -930,7 +947,6 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd,
unsigned int pass; unsigned int pass;
socklen_t len; socklen_t len;
int err, n; int err, n;
u64 value;
u32 key; u32 key;
char b; char b;
...@@ -967,15 +983,7 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, ...@@ -967,15 +983,7 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd,
if (p1 < 0) if (p1 < 0)
goto close_cli1; goto close_cli1;
key = 0; err = add_to_sockmap(sock_mapfd, p0, p1);
value = p0;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err)
goto close_peer1;
key = 1;
value = p1;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err) if (err)
goto close_peer1; goto close_peer1;
...@@ -1063,7 +1071,6 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd, ...@@ -1063,7 +1071,6 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd,
int s, c, p, err, n; int s, c, p, err, n;
unsigned int drop; unsigned int drop;
socklen_t len; socklen_t len;
u64 value;
u32 key; u32 key;
zero_verdict_count(verd_mapfd); zero_verdict_count(verd_mapfd);
...@@ -1088,15 +1095,7 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd, ...@@ -1088,15 +1095,7 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd,
if (p < 0) if (p < 0)
goto close_cli; goto close_cli;
key = 0; err = add_to_sockmap(sock_mapfd, s, p);
value = s;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err)
goto close_peer;
key = 1;
value = p;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err) if (err)
goto close_peer; goto close_peer;
...@@ -1348,7 +1347,6 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map, ...@@ -1348,7 +1347,6 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map,
int s1, s2, c, err; int s1, s2, c, err;
unsigned int drop; unsigned int drop;
socklen_t len; socklen_t len;
u64 value;
u32 key; u32 key;
zero_verdict_count(verd_map); zero_verdict_count(verd_map);
...@@ -1362,16 +1360,10 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map, ...@@ -1362,16 +1360,10 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map,
if (s2 < 0) if (s2 < 0)
goto close_srv1; goto close_srv1;
key = 0; err = add_to_sockmap(sock_map, s1, s2);
value = s1;
err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST);
if (err) if (err)
goto close_srv2; goto close_srv2;
key = 1;
value = s2;
err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST);
/* Connect to s2, reuseport BPF selects s1 via sock_map[0] */ /* Connect to s2, reuseport BPF selects s1 via sock_map[0] */
len = sizeof(addr); len = sizeof(addr);
err = xgetsockname(s2, sockaddr(&addr), &len); err = xgetsockname(s2, sockaddr(&addr), &len);
...@@ -1655,7 +1647,6 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, ...@@ -1655,7 +1647,6 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
unsigned int pass; unsigned int pass;
int retries = 100; int retries = 100;
int err, n; int err, n;
u64 value;
u32 key; u32 key;
char b; char b;
...@@ -1668,15 +1659,7 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, ...@@ -1668,15 +1659,7 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
if (err) if (err)
goto close_cli0; goto close_cli0;
key = 0; err = add_to_sockmap(sock_mapfd, p0, p1);
value = p0;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err)
goto close_cli1;
key = 1;
value = p1;
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
if (err) if (err)
goto close_cli1; goto close_cli1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment