Skip to content

Commit

Permalink
Merge branch 'bpf-next/net' into for-next
Browse files Browse the repository at this point in the history
Signed-off-by: Martin KaFai Lau <[email protected]>
  • Loading branch information
Martin KaFai Lau committed Aug 20, 2024
2 parents c3d80ea + db16377 commit 8ccd382
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 170 deletions.
28 changes: 9 additions & 19 deletions tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ static void test_sockmap_progs_query(enum bpf_attach_type attach_type)
#define MAX_EVENTS 10
static void test_sockmap_skb_verdict_shutdown(void)
{
int n, err, map, verdict, c1 = -1, p1 = -1;
struct epoll_event ev, events[MAX_EVENTS];
int n, err, map, verdict, s, c1 = -1, p1 = -1;
struct test_sockmap_pass_prog *skel;
int epollfd;
int zero = 0;
int epollfd;
char b;

skel = test_sockmap_pass_prog__open_and_load();
Expand All @@ -469,10 +469,7 @@ static void test_sockmap_skb_verdict_shutdown(void)
if (!ASSERT_OK(err, "bpf_prog_attach"))
goto out;

s = socket_loopback(AF_INET, SOCK_STREAM);
if (s < 0)
goto out;
err = create_pair(s, AF_INET, SOCK_STREAM, &c1, &p1);
err = create_pair(AF_INET, SOCK_STREAM, &c1, &p1);
if (err < 0)
goto out;

Expand Down Expand Up @@ -506,8 +503,8 @@ static void test_sockmap_skb_verdict_shutdown(void)

static void test_sockmap_skb_verdict_fionread(bool pass_prog)
{
int err, map, verdict, c0 = -1, c1 = -1, p0 = -1, p1 = -1;
int expected, zero = 0, sent, recvd, avail;
int err, map, verdict, s, c0 = -1, c1 = -1, p0 = -1, p1 = -1;
struct test_sockmap_pass_prog *pass = NULL;
struct test_sockmap_drop_prog *drop = NULL;
char buf[256] = "0123456789";
Expand All @@ -534,11 +531,8 @@ static void test_sockmap_skb_verdict_fionread(bool pass_prog)
if (!ASSERT_OK(err, "bpf_prog_attach"))
goto out;

s = socket_loopback(AF_INET, SOCK_STREAM);
if (!ASSERT_GT(s, -1, "socket_loopback(s)"))
goto out;
err = create_socket_pairs(s, AF_INET, SOCK_STREAM, &c0, &c1, &p0, &p1);
if (!ASSERT_OK(err, "create_socket_pairs(s)"))
err = create_socket_pairs(AF_INET, SOCK_STREAM, &c0, &c1, &p0, &p1);
if (!ASSERT_OK(err, "create_socket_pairs()"))
goto out;

err = bpf_map_update_elem(map, &zero, &c1, BPF_NOEXIST);
Expand Down Expand Up @@ -570,16 +564,12 @@ static void test_sockmap_skb_verdict_fionread(bool pass_prog)

static void test_sockmap_skb_verdict_peek_helper(int map)
{
int err, s, c1, p1, zero = 0, sent, recvd, avail;
int err, c1, p1, zero = 0, sent, recvd, avail;
char snd[256] = "0123456789";
char rcv[256] = "0";

s = socket_loopback(AF_INET, SOCK_STREAM);
if (!ASSERT_GT(s, -1, "socket_loopback(s)"))
return;

err = create_pair(s, AF_INET, SOCK_STREAM, &c1, &p1);
if (!ASSERT_OK(err, "create_pairs(s)"))
err = create_pair(AF_INET, SOCK_STREAM, &c1, &p1);
if (!ASSERT_OK(err, "create_pair()"))
return;

err = bpf_map_update_elem(map, &zero, &c1, BPF_NOEXIST);
Expand Down
149 changes: 101 additions & 48 deletions tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <linux/vm_sockets.h>

/* include/linux/net.h */
#define SOCK_TYPE_MASK 0xf

#define IO_TIMEOUT_SEC 30
#define MAX_STRERR_LEN 256
#define MAX_TEST_NAME 80
Expand All @@ -14,6 +17,17 @@

#define __always_unused __attribute__((__unused__))

/* include/linux/cleanup.h */
#define __get_and_null(p, nullvalue) \
({ \
__auto_type __ptr = &(p); \
__auto_type __val = *__ptr; \
*__ptr = nullvalue; \
__val; \
})

#define take_fd(fd) __get_and_null(fd, -EBADF)

#define _FAIL(errnum, fmt...) \
({ \
error_at_line(0, (errnum), __func__, __LINE__, fmt); \
Expand Down Expand Up @@ -179,6 +193,14 @@
__ret; \
})

static inline void close_fd(int *fd)
{
if (*fd >= 0)
xclose(*fd);
}

#define __close_fd __attribute__((cleanup(close_fd)))

static inline int poll_connect(int fd, unsigned int timeout_sec)
{
struct timeval timeout = { .tv_sec = timeout_sec };
Expand Down Expand Up @@ -312,54 +334,6 @@ static inline int add_to_sockmap(int sock_mapfd, int fd1, int fd2)
return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
}

static inline int create_pair(int s, int family, int sotype, int *c, int *p)
{
struct sockaddr_storage addr;
socklen_t len;
int err = 0;

len = sizeof(addr);
err = xgetsockname(s, sockaddr(&addr), &len);
if (err)
return err;

*c = xsocket(family, sotype, 0);
if (*c < 0)
return errno;
err = xconnect(*c, sockaddr(&addr), len);
if (err) {
err = errno;
goto close_cli0;
}

*p = xaccept_nonblock(s, NULL, NULL);
if (*p < 0) {
err = errno;
goto close_cli0;
}
return err;
close_cli0:
close(*c);
return err;
}

static inline int create_socket_pairs(int s, int family, int sotype,
int *c0, int *c1, int *p0, int *p1)
{
int err;

err = create_pair(s, family, sotype, c0, p0);
if (err)
return err;

err = create_pair(s, family, sotype, c1, p1);
if (err) {
close(*c0);
close(*p0);
}
return err;
}

static inline int enable_reuseport(int s, int progfd)
{
int err, one = 1;
Expand Down Expand Up @@ -412,5 +386,84 @@ static inline int socket_loopback(int family, int sotype)
return socket_loopback_reuseport(family, sotype, -1);
}

static inline int create_pair(int family, int sotype, int *p0, int *p1)
{
__close_fd int s, c = -1, p = -1;
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
int err;

s = socket_loopback(family, sotype);
if (s < 0)
return s;

err = xgetsockname(s, sockaddr(&addr), &len);
if (err)
return err;

c = xsocket(family, sotype, 0);
if (c < 0)
return c;

err = connect(c, sockaddr(&addr), len);
if (err) {
if (errno != EINPROGRESS) {
FAIL_ERRNO("connect");
return err;
}

err = poll_connect(c, IO_TIMEOUT_SEC);
if (err) {
FAIL_ERRNO("poll_connect");
return err;
}
}

switch (sotype & SOCK_TYPE_MASK) {
case SOCK_DGRAM:
err = xgetsockname(c, sockaddr(&addr), &len);
if (err)
return err;

err = xconnect(s, sockaddr(&addr), len);
if (err)
return err;

*p0 = take_fd(s);
break;
case SOCK_STREAM:
case SOCK_SEQPACKET:
p = xaccept_nonblock(s, NULL, NULL);
if (p < 0)
return p;

*p0 = take_fd(p);
break;
default:
FAIL("Unsupported socket type %#x", sotype);
return -EOPNOTSUPP;
}

*p1 = take_fd(c);
return 0;
}

static inline int create_socket_pairs(int family, int sotype, int *c0, int *c1,
int *p0, int *p1)
{
int err;

err = create_pair(family, sotype, c0, p0);
if (err)
return err;

err = create_pair(family, sotype, c1, p1);
if (err) {
close(*c0);
close(*p0);
}

return err;
}

#endif // __SOCKMAP_HELPERS__
Loading

0 comments on commit 8ccd382

Please sign in to comment.