Skip to content

Commit 2a3af99

Browse files
committed
selftests/bpf: Return socket cookies from sock_iter_batch progs
Extend the iter_udp_soreuse and iter_tcp_soreuse programs to write the cookie of the current socket, so that we can track the identity of the sockets that the iterator has seen so far. Update the existing do_test function to account for this change to the iterator program output. At the same time, teach both programs to work with AF_INET as well. Signed-off-by: Jordan Rife <[email protected]>
1 parent 513feff commit 2a3af99

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99

1010
static const int nr_soreuse = 4;
1111

12+
struct iter_out {
13+
int idx;
14+
__u64 cookie;
15+
} __packed;
16+
1217
static void do_test(int sock_type, bool onebyone)
1318
{
1419
int err, i, nread, to_read, total_read, iter_fd = -1;
15-
int first_idx, second_idx, indices[nr_soreuse];
20+
struct iter_out outputs[nr_soreuse];
1621
struct bpf_link *link = NULL;
1722
struct sock_iter_batch *skel;
23+
int first_idx, second_idx;
1824
int *fds[2] = {};
1925

2026
skel = sock_iter_batch__open();
@@ -34,6 +40,7 @@ static void do_test(int sock_type, bool onebyone)
3440
goto done;
3541
skel->rodata->ports[i] = ntohs(local_port);
3642
}
43+
skel->rodata->sf = AF_INET6;
3744

3845
err = sock_iter_batch__load(skel);
3946
if (!ASSERT_OK(err, "sock_iter_batch__load"))
@@ -55,38 +62,38 @@ static void do_test(int sock_type, bool onebyone)
5562
* from a bucket and leave one socket out from
5663
* that bucket on purpose.
5764
*/
58-
to_read = (nr_soreuse - 1) * sizeof(*indices);
65+
to_read = (nr_soreuse - 1) * sizeof(*outputs);
5966
total_read = 0;
6067
first_idx = -1;
6168
do {
62-
nread = read(iter_fd, indices, onebyone ? sizeof(*indices) : to_read);
63-
if (nread <= 0 || nread % sizeof(*indices))
69+
nread = read(iter_fd, outputs, onebyone ? sizeof(*outputs) : to_read);
70+
if (nread <= 0 || nread % sizeof(*outputs))
6471
break;
6572
total_read += nread;
6673

6774
if (first_idx == -1)
68-
first_idx = indices[0];
69-
for (i = 0; i < nread / sizeof(*indices); i++)
70-
ASSERT_EQ(indices[i], first_idx, "first_idx");
75+
first_idx = outputs[0].idx;
76+
for (i = 0; i < nread / sizeof(*outputs); i++)
77+
ASSERT_EQ(outputs[i].idx, first_idx, "first_idx");
7178
} while (total_read < to_read);
72-
ASSERT_EQ(nread, onebyone ? sizeof(*indices) : to_read, "nread");
79+
ASSERT_EQ(nread, onebyone ? sizeof(*outputs) : to_read, "nread");
7380
ASSERT_EQ(total_read, to_read, "total_read");
7481

7582
free_fds(fds[first_idx], nr_soreuse);
7683
fds[first_idx] = NULL;
7784

7885
/* Read the "whole" second bucket */
79-
to_read = nr_soreuse * sizeof(*indices);
86+
to_read = nr_soreuse * sizeof(*outputs);
8087
total_read = 0;
8188
second_idx = !first_idx;
8289
do {
83-
nread = read(iter_fd, indices, onebyone ? sizeof(*indices) : to_read);
84-
if (nread <= 0 || nread % sizeof(*indices))
90+
nread = read(iter_fd, outputs, onebyone ? sizeof(*outputs) : to_read);
91+
if (nread <= 0 || nread % sizeof(*outputs))
8592
break;
8693
total_read += nread;
8794

88-
for (i = 0; i < nread / sizeof(*indices); i++)
89-
ASSERT_EQ(indices[i], second_idx, "second_idx");
95+
for (i = 0; i < nread / sizeof(*outputs); i++)
96+
ASSERT_EQ(outputs[i].idx, second_idx, "second_idx");
9097
} while (total_read <= to_read);
9198
ASSERT_EQ(nread, 0, "nread");
9299
/* Both so_reuseport ports should be in different buckets, so

tools/testing/selftests/bpf/progs/bpf_tracing_net.h

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
#define sk_refcnt __sk_common.skc_refcnt
129129
#define sk_state __sk_common.skc_state
130130
#define sk_net __sk_common.skc_net
131+
#define sk_rcv_saddr __sk_common.skc_rcv_saddr
131132
#define sk_v6_daddr __sk_common.skc_v6_daddr
132133
#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
133134
#define sk_flags __sk_common.skc_flags

tools/testing/selftests/bpf/progs/sock_iter_batch.c

+20-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ static bool ipv6_addr_loopback(const struct in6_addr *a)
1717
a->s6_addr32[2] | (a->s6_addr32[3] ^ bpf_htonl(1))) == 0;
1818
}
1919

20+
static bool ipv4_addr_loopback(__be32 a)
21+
{
22+
return a == bpf_ntohl(0x7f000001);
23+
}
24+
25+
volatile const unsigned int sf;
2026
volatile const __u16 ports[2];
2127
unsigned int bucket[2];
2228

@@ -26,16 +32,20 @@ int iter_tcp_soreuse(struct bpf_iter__tcp *ctx)
2632
struct sock *sk = (struct sock *)ctx->sk_common;
2733
struct inet_hashinfo *hinfo;
2834
unsigned int hash;
35+
__u64 sock_cookie;
2936
struct net *net;
3037
int idx;
3138

3239
if (!sk)
3340
return 0;
3441

42+
sock_cookie = bpf_get_socket_cookie(sk);
3543
sk = bpf_core_cast(sk, struct sock);
36-
if (sk->sk_family != AF_INET6 ||
44+
if (sk->sk_family != sf ||
3745
sk->sk_state != TCP_LISTEN ||
38-
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
46+
sk->sk_family == AF_INET6 ?
47+
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr) :
48+
!ipv4_addr_loopback(sk->sk_rcv_saddr))
3949
return 0;
4050

4151
if (sk->sk_num == ports[0])
@@ -52,6 +62,7 @@ int iter_tcp_soreuse(struct bpf_iter__tcp *ctx)
5262
hinfo = net->ipv4.tcp_death_row.hashinfo;
5363
bucket[idx] = hash & hinfo->lhash2_mask;
5464
bpf_seq_write(ctx->meta->seq, &idx, sizeof(idx));
65+
bpf_seq_write(ctx->meta->seq, &sock_cookie, sizeof(sock_cookie));
5566

5667
return 0;
5768
}
@@ -63,14 +74,18 @@ int iter_udp_soreuse(struct bpf_iter__udp *ctx)
6374
{
6475
struct sock *sk = (struct sock *)ctx->udp_sk;
6576
struct udp_table *udptable;
77+
__u64 sock_cookie;
6678
int idx;
6779

6880
if (!sk)
6981
return 0;
7082

83+
sock_cookie = bpf_get_socket_cookie(sk);
7184
sk = bpf_core_cast(sk, struct sock);
72-
if (sk->sk_family != AF_INET6 ||
73-
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
85+
if (sk->sk_family != sf ||
86+
sk->sk_family == AF_INET6 ?
87+
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr) :
88+
!ipv4_addr_loopback(sk->sk_rcv_saddr))
7489
return 0;
7590

7691
if (sk->sk_num == ports[0])
@@ -84,6 +99,7 @@ int iter_udp_soreuse(struct bpf_iter__udp *ctx)
8499
udptable = sk->sk_net.net->ipv4.udp_table;
85100
bucket[idx] = udp_sk(sk)->udp_portaddr_hash & udptable->mask;
86101
bpf_seq_write(ctx->meta->seq, &idx, sizeof(idx));
102+
bpf_seq_write(ctx->meta->seq, &sock_cookie, sizeof(sock_cookie));
87103

88104
return 0;
89105
}

0 commit comments

Comments
 (0)