Commit 9a1a2cb5 authored by Martin KaFai Lau's avatar Martin KaFai Lau

Merge branch 'use network helpers, part 3'

Geliang Tang says:

====================
This patchset adds opts argument for __start_server.
====================
Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parents 06ebfd11 8405e698
...@@ -80,19 +80,19 @@ int settimeo(int fd, int timeout_ms) ...@@ -80,19 +80,19 @@ int settimeo(int fd, int timeout_ms)
#define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; }) #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })
static int __start_server(int type, int protocol, const struct sockaddr *addr, static int __start_server(int type, const struct sockaddr *addr, socklen_t addrlen,
socklen_t addrlen, int timeout_ms, bool reuseport) bool reuseport, const struct network_helper_opts *opts)
{ {
int on = 1; int on = 1;
int fd; int fd;
fd = socket(addr->sa_family, type, protocol); fd = socket(addr->sa_family, type, opts->proto);
if (fd < 0) { if (fd < 0) {
log_err("Failed to create server socket"); log_err("Failed to create server socket");
return -1; return -1;
} }
if (settimeo(fd, timeout_ms)) if (settimeo(fd, opts->timeout_ms))
goto error_close; goto error_close;
if (reuseport && if (reuseport &&
...@@ -120,35 +120,27 @@ static int __start_server(int type, int protocol, const struct sockaddr *addr, ...@@ -120,35 +120,27 @@ static int __start_server(int type, int protocol, const struct sockaddr *addr,
return -1; return -1;
} }
static int start_server_proto(int family, int type, int protocol, int start_server(int family, int type, const char *addr_str, __u16 port,
const char *addr_str, __u16 port, int timeout_ms) int timeout_ms)
{ {
struct network_helper_opts opts = {
.timeout_ms = timeout_ms,
};
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen;
if (make_sockaddr(family, addr_str, port, &addr, &addrlen)) if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
return -1; return -1;
return __start_server(type, protocol, (struct sockaddr *)&addr, return __start_server(type, (struct sockaddr *)&addr, addrlen, false, &opts);
addrlen, timeout_ms, false);
}
int start_server(int family, int type, const char *addr_str, __u16 port,
int timeout_ms)
{
return start_server_proto(family, type, 0, addr_str, port, timeout_ms);
}
int start_mptcp_server(int family, const char *addr_str, __u16 port,
int timeout_ms)
{
return start_server_proto(family, SOCK_STREAM, IPPROTO_MPTCP, addr_str,
port, timeout_ms);
} }
int *start_reuseport_server(int family, int type, const char *addr_str, int *start_reuseport_server(int family, int type, const char *addr_str,
__u16 port, int timeout_ms, unsigned int nr_listens) __u16 port, int timeout_ms, unsigned int nr_listens)
{ {
struct network_helper_opts opts = {
.timeout_ms = timeout_ms,
};
struct sockaddr_storage addr; struct sockaddr_storage addr;
unsigned int nr_fds = 0; unsigned int nr_fds = 0;
socklen_t addrlen; socklen_t addrlen;
...@@ -164,8 +156,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, ...@@ -164,8 +156,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
if (!fds) if (!fds)
return NULL; return NULL;
fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen, fds[0] = __start_server(type, (struct sockaddr *)&addr, addrlen, true, &opts);
timeout_ms, true);
if (fds[0] == -1) if (fds[0] == -1)
goto close_fds; goto close_fds;
nr_fds = 1; nr_fds = 1;
...@@ -174,8 +165,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, ...@@ -174,8 +165,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str,
goto close_fds; goto close_fds;
for (; nr_fds < nr_listens; nr_fds++) { for (; nr_fds < nr_listens; nr_fds++) {
fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr, fds[nr_fds] = __start_server(type, (struct sockaddr *)&addr, addrlen, true, &opts);
addrlen, timeout_ms, true);
if (fds[nr_fds] == -1) if (fds[nr_fds] == -1)
goto close_fds; goto close_fds;
} }
...@@ -193,8 +183,7 @@ int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t l ...@@ -193,8 +183,7 @@ int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t l
if (!opts) if (!opts)
opts = &default_opts; opts = &default_opts;
return __start_server(type, 0, (struct sockaddr *)addr, len, return __start_server(type, (struct sockaddr *)addr, len, 0, opts);
opts->timeout_ms, 0);
} }
void free_fds(int *fds, unsigned int nr_close_fds) void free_fds(int *fds, unsigned int nr_close_fds)
......
...@@ -49,8 +49,6 @@ extern struct ipv6_packet pkt_v6; ...@@ -49,8 +49,6 @@ extern struct ipv6_packet pkt_v6;
int settimeo(int fd, int timeout_ms); int settimeo(int fd, int timeout_ms);
int start_server(int family, int type, const char *addr, __u16 port, int start_server(int family, int type, const char *addr, __u16 port,
int timeout_ms); int timeout_ms);
int start_mptcp_server(int family, const char *addr, __u16 port,
int timeout_ms);
int *start_reuseport_server(int family, int type, const char *addr_str, int *start_reuseport_server(int family, int type, const char *addr_str,
__u16 port, int timeout_ms, __u16 port, int timeout_ms,
unsigned int nr_listens); unsigned int nr_listens);
......
...@@ -82,6 +82,22 @@ static void cleanup_netns(struct nstoken *nstoken) ...@@ -82,6 +82,22 @@ static void cleanup_netns(struct nstoken *nstoken)
SYS_NOFAIL("ip netns del %s", NS_TEST); SYS_NOFAIL("ip netns del %s", NS_TEST);
} }
static int start_mptcp_server(int family, const char *addr_str, __u16 port,
int timeout_ms)
{
struct network_helper_opts opts = {
.timeout_ms = timeout_ms,
.proto = IPPROTO_MPTCP,
};
struct sockaddr_storage addr;
socklen_t addrlen;
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
return -1;
return start_server_addr(SOCK_STREAM, &addr, addrlen, &opts);
}
static int verify_tsk(int map_fd, int client_fd) static int verify_tsk(int map_fd, int client_fd)
{ {
int err, cfd = client_fd; int err, cfd = client_fd;
......
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