-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Generalize helpers to control background listener
Move the following routines that let us start a background listener thread and connect to a server by fd to the test_prog: * start_server - socket+bind+listen * connect_to_fd - connect to the server identified by fd These will be used in the next commit. Also, extend these helpers to support AF_INET6 and accept the family as an argument. v5: * drop pthread.h (Martin KaFai Lau) * add SO_SNDTIMEO (Martin KaFai Lau) v4: * export extra helper to start server without a thread (Martin KaFai Lau) * tcp_rtt is no longer starting background thread (Martin KaFai Lau) v2: * put helpers into network_helpers.c (Andrii Nakryiko) Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrey Ignatov <rdna@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20200508174611.228805-2-sdf@google.com
- Loading branch information
Stanislav Fomichev
authored and
Daniel Borkmann
committed
May 8, 2020
1 parent
2b6c6f0
commit 33181bb
Showing
4 changed files
with
108 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
#include <errno.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <linux/err.h> | ||
#include <linux/in.h> | ||
#include <linux/in6.h> | ||
|
||
#include "network_helpers.h" | ||
|
||
#define clean_errno() (errno == 0 ? "None" : strerror(errno)) | ||
#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \ | ||
__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) | ||
|
||
int start_server(int family, int type) | ||
{ | ||
struct sockaddr_storage addr = {}; | ||
socklen_t len; | ||
int fd; | ||
|
||
if (family == AF_INET) { | ||
struct sockaddr_in *sin = (void *)&addr; | ||
|
||
sin->sin_family = AF_INET; | ||
len = sizeof(*sin); | ||
} else { | ||
struct sockaddr_in6 *sin6 = (void *)&addr; | ||
|
||
sin6->sin6_family = AF_INET6; | ||
len = sizeof(*sin6); | ||
} | ||
|
||
fd = socket(family, type | SOCK_NONBLOCK, 0); | ||
if (fd < 0) { | ||
log_err("Failed to create server socket"); | ||
return -1; | ||
} | ||
|
||
if (bind(fd, (const struct sockaddr *)&addr, len) < 0) { | ||
log_err("Failed to bind socket"); | ||
close(fd); | ||
return -1; | ||
} | ||
|
||
if (type == SOCK_STREAM) { | ||
if (listen(fd, 1) < 0) { | ||
log_err("Failed to listed on socket"); | ||
close(fd); | ||
return -1; | ||
} | ||
} | ||
|
||
return fd; | ||
} | ||
|
||
static const struct timeval timeo_sec = { .tv_sec = 3 }; | ||
static const size_t timeo_optlen = sizeof(timeo_sec); | ||
|
||
int connect_to_fd(int family, int type, int server_fd) | ||
{ | ||
struct sockaddr_storage addr; | ||
socklen_t len = sizeof(addr); | ||
int fd; | ||
|
||
fd = socket(family, type, 0); | ||
if (fd < 0) { | ||
log_err("Failed to create client socket"); | ||
return -1; | ||
} | ||
|
||
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen)) { | ||
log_err("Failed to set SO_RCVTIMEO"); | ||
goto out; | ||
} | ||
|
||
if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { | ||
log_err("Failed to get server addr"); | ||
goto out; | ||
} | ||
|
||
if (connect(fd, (const struct sockaddr *)&addr, len) < 0) { | ||
log_err("Fail to connect to server with family %d", family); | ||
goto out; | ||
} | ||
|
||
return fd; | ||
|
||
out: | ||
close(fd); | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
#ifndef __NETWORK_HELPERS_H | ||
#define __NETWORK_HELPERS_H | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
|
||
int start_server(int family, int type); | ||
int connect_to_fd(int family, int type, int server_fd); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters