Skip to content

Commit b037f33

Browse files
author
Martin KaFai Lau
committed
Merge branch 'use network helpers, part 9'
Geliang Tang says: ==================== v3: - patch 2: - clear errno before connect_to_fd_opts. - print err logs in run_test. - set err to -1 when fd >= 0. - patch 3: - drop "int err". v2: - update patch 2 as Martin suggested. This is the 9th part of series "use network helpers" all BPF selftests wide. Patches 1-2 update network helpers interfaces suggested by Martin. Patch 3 adds a new helper connect_to_addr_str() as Martin suggested instead of adding connect_fd_to_addr_str(). Patch 4 uses this newly added helper in make_client(). Patch 5 uses make_client() in sk_lookup and drop make_socket(). ==================== Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 5183594 + 2b42d68 commit b037f33

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -277,33 +277,6 @@ int client_socket(int family, int type,
277277
return -1;
278278
}
279279

280-
static int connect_fd_to_addr(int fd,
281-
const struct sockaddr_storage *addr,
282-
socklen_t addrlen, const bool must_fail)
283-
{
284-
int ret;
285-
286-
errno = 0;
287-
ret = connect(fd, (const struct sockaddr *)addr, addrlen);
288-
if (must_fail) {
289-
if (!ret) {
290-
log_err("Unexpected success to connect to server");
291-
return -1;
292-
}
293-
if (errno != EPERM) {
294-
log_err("Unexpected error from connect to server");
295-
return -1;
296-
}
297-
} else {
298-
if (ret) {
299-
log_err("Failed to connect to server");
300-
return -1;
301-
}
302-
}
303-
304-
return 0;
305-
}
306-
307280
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
308281
const struct network_helper_opts *opts)
309282
{
@@ -318,24 +291,45 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
318291
return -1;
319292
}
320293

321-
if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail))
322-
goto error_close;
294+
if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
295+
log_err("Failed to connect to server");
296+
save_errno_close(fd);
297+
return -1;
298+
}
323299

324300
return fd;
325-
326-
error_close:
327-
save_errno_close(fd);
328-
return -1;
329301
}
330302

331-
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts)
303+
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
304+
const struct network_helper_opts *opts)
332305
{
333306
struct sockaddr_storage addr;
334307
socklen_t addrlen;
335308

336309
if (!opts)
337310
opts = &default_opts;
338311

312+
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
313+
return -1;
314+
315+
return connect_to_addr(type, &addr, addrlen, opts);
316+
}
317+
318+
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
319+
{
320+
struct sockaddr_storage addr;
321+
socklen_t addrlen, optlen;
322+
int type;
323+
324+
if (!opts)
325+
opts = &default_opts;
326+
327+
optlen = sizeof(type);
328+
if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
329+
log_err("getsockopt(SOL_TYPE)");
330+
return -1;
331+
}
332+
339333
addrlen = sizeof(addr);
340334
if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
341335
log_err("Failed to get server addr");
@@ -350,14 +344,8 @@ int connect_to_fd(int server_fd, int timeout_ms)
350344
struct network_helper_opts opts = {
351345
.timeout_ms = timeout_ms,
352346
};
353-
int type, protocol;
354347
socklen_t optlen;
355-
356-
optlen = sizeof(type);
357-
if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
358-
log_err("getsockopt(SOL_TYPE)");
359-
return -1;
360-
}
348+
int protocol;
361349

362350
optlen = sizeof(protocol);
363351
if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
@@ -366,7 +354,7 @@ int connect_to_fd(int server_fd, int timeout_ms)
366354
}
367355
opts.proto = protocol;
368356

369-
return connect_to_fd_opts(server_fd, type, &opts);
357+
return connect_to_fd_opts(server_fd, &opts);
370358
}
371359

372360
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
@@ -382,8 +370,10 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
382370
return -1;
383371
}
384372

385-
if (connect_fd_to_addr(client_fd, &addr, len, false))
373+
if (connect(client_fd, (const struct sockaddr *)&addr, len)) {
374+
log_err("Failed to connect to server");
386375
return -1;
376+
}
387377

388378
return 0;
389379
}

tools/testing/selftests/bpf/network_helpers.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ typedef __u16 __sum16;
2323

2424
struct network_helper_opts {
2525
int timeout_ms;
26-
bool must_fail;
2726
int proto;
2827
/* +ve: Passed to listen() as-is.
2928
* 0: Default when the test does not set
@@ -70,8 +69,10 @@ int client_socket(int family, int type,
7069
const struct network_helper_opts *opts);
7170
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
7271
const struct network_helper_opts *opts);
72+
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
73+
const struct network_helper_opts *opts);
7374
int connect_to_fd(int server_fd, int timeout_ms);
74-
int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts);
75+
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
7576
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
7677
int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
7778
int timeout_ms);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static bool start_test(char *addr_str,
4949
goto err;
5050

5151
/* connect to server */
52-
*cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts);
52+
*cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
5353
if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
5454
goto err;
5555

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
static int run_test(int cgroup_fd, int server_fd, bool classid)
1111
{
12-
struct network_helper_opts opts = {
13-
.must_fail = true,
14-
};
1512
struct connect4_dropper *skel;
1613
int fd, err = 0;
1714

@@ -32,11 +29,16 @@ static int run_test(int cgroup_fd, int server_fd, bool classid)
3229
goto out;
3330
}
3431

35-
fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
36-
if (fd < 0)
32+
errno = 0;
33+
fd = connect_to_fd_opts(server_fd, NULL);
34+
if (fd >= 0) {
35+
log_err("Unexpected success to connect to server");
3736
err = -1;
38-
else
3937
close(fd);
38+
} else if (errno != EPERM) {
39+
log_err("Unexpected errno from connect to server");
40+
err = -1;
41+
}
4042
out:
4143
connect4_dropper__destroy(skel);
4244
return err;
@@ -52,7 +54,7 @@ void test_cgroup_v1v2(void)
5254
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
5355
if (!ASSERT_GE(server_fd, 0, "server_fd"))
5456
return;
55-
client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
57+
client_fd = connect_to_fd_opts(server_fd, &opts);
5658
if (!ASSERT_GE(client_fd, 0, "client_fd")) {
5759
close(server_fd);
5860
return;

0 commit comments

Comments
 (0)