1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
4 #include <netinet/in.h>
12 #include <bpf/libbpf.h>
13 #include <linux/compiler.h>
15 #include "network_helpers.h"
16 #include "cgroup_helpers.h"
17 #include "test_progs.h"
18 #include "bpf_rlimit.h"
19 #include "test_sock_fields.skel.h"
21 enum bpf_linum_array_idx
{
24 __NR_BPF_LINUM_ARRAY_IDX
,
27 struct bpf_spinlock_cnt
{
28 struct bpf_spin_lock lock
;
32 #define PARENT_CGROUP "/test-bpf-sock-fields"
33 #define CHILD_CGROUP "/test-bpf-sock-fields/child"
34 #define DATA "Hello BPF!"
35 #define DATA_LEN sizeof(DATA)
37 static struct sockaddr_in6 srv_sa6
, cli_sa6
;
38 static int sk_pkt_out_cnt10_fd
;
39 static struct test_sock_fields
*skel
;
40 static int sk_pkt_out_cnt_fd
;
41 static __u64 parent_cg_id
;
42 static __u64 child_cg_id
;
43 static int linum_map_fd
;
44 static __u32 duration
;
46 static __u32 egress_linum_idx
= EGRESS_LINUM_IDX
;
47 static __u32 ingress_linum_idx
= INGRESS_LINUM_IDX
;
49 static void print_sk(const struct bpf_sock
*sk
, const char *prefix
)
51 char src_ip4
[24], dst_ip4
[24];
52 char src_ip6
[64], dst_ip6
[64];
54 inet_ntop(AF_INET
, &sk
->src_ip4
, src_ip4
, sizeof(src_ip4
));
55 inet_ntop(AF_INET6
, &sk
->src_ip6
, src_ip6
, sizeof(src_ip6
));
56 inet_ntop(AF_INET
, &sk
->dst_ip4
, dst_ip4
, sizeof(dst_ip4
));
57 inet_ntop(AF_INET6
, &sk
->dst_ip6
, dst_ip6
, sizeof(dst_ip6
));
59 printf("%s: state:%u bound_dev_if:%u family:%u type:%u protocol:%u mark:%u priority:%u "
60 "src_ip4:%x(%s) src_ip6:%x:%x:%x:%x(%s) src_port:%u "
61 "dst_ip4:%x(%s) dst_ip6:%x:%x:%x:%x(%s) dst_port:%u\n",
63 sk
->state
, sk
->bound_dev_if
, sk
->family
, sk
->type
, sk
->protocol
,
64 sk
->mark
, sk
->priority
,
66 sk
->src_ip6
[0], sk
->src_ip6
[1], sk
->src_ip6
[2], sk
->src_ip6
[3],
67 src_ip6
, sk
->src_port
,
69 sk
->dst_ip6
[0], sk
->dst_ip6
[1], sk
->dst_ip6
[2], sk
->dst_ip6
[3],
70 dst_ip6
, ntohs(sk
->dst_port
));
73 static void print_tp(const struct bpf_tcp_sock
*tp
, const char *prefix
)
75 printf("%s: snd_cwnd:%u srtt_us:%u rtt_min:%u snd_ssthresh:%u rcv_nxt:%u "
76 "snd_nxt:%u snd:una:%u mss_cache:%u ecn_flags:%u "
77 "rate_delivered:%u rate_interval_us:%u packets_out:%u "
78 "retrans_out:%u total_retrans:%u segs_in:%u data_segs_in:%u "
79 "segs_out:%u data_segs_out:%u lost_out:%u sacked_out:%u "
80 "bytes_received:%llu bytes_acked:%llu\n",
82 tp
->snd_cwnd
, tp
->srtt_us
, tp
->rtt_min
, tp
->snd_ssthresh
,
83 tp
->rcv_nxt
, tp
->snd_nxt
, tp
->snd_una
, tp
->mss_cache
,
84 tp
->ecn_flags
, tp
->rate_delivered
, tp
->rate_interval_us
,
85 tp
->packets_out
, tp
->retrans_out
, tp
->total_retrans
,
86 tp
->segs_in
, tp
->data_segs_in
, tp
->segs_out
,
87 tp
->data_segs_out
, tp
->lost_out
, tp
->sacked_out
,
88 tp
->bytes_received
, tp
->bytes_acked
);
91 static void check_result(void)
93 struct bpf_tcp_sock srv_tp
, cli_tp
, listen_tp
;
94 struct bpf_sock srv_sk
, cli_sk
, listen_sk
;
95 __u32 ingress_linum
, egress_linum
;
98 err
= bpf_map_lookup_elem(linum_map_fd
, &egress_linum_idx
,
100 CHECK(err
== -1, "bpf_map_lookup_elem(linum_map_fd)",
101 "err:%d errno:%d\n", err
, errno
);
103 err
= bpf_map_lookup_elem(linum_map_fd
, &ingress_linum_idx
,
105 CHECK(err
== -1, "bpf_map_lookup_elem(linum_map_fd)",
106 "err:%d errno:%d\n", err
, errno
);
108 memcpy(&srv_sk
, &skel
->bss
->srv_sk
, sizeof(srv_sk
));
109 memcpy(&srv_tp
, &skel
->bss
->srv_tp
, sizeof(srv_tp
));
110 memcpy(&cli_sk
, &skel
->bss
->cli_sk
, sizeof(cli_sk
));
111 memcpy(&cli_tp
, &skel
->bss
->cli_tp
, sizeof(cli_tp
));
112 memcpy(&listen_sk
, &skel
->bss
->listen_sk
, sizeof(listen_sk
));
113 memcpy(&listen_tp
, &skel
->bss
->listen_tp
, sizeof(listen_tp
));
115 print_sk(&listen_sk
, "listen_sk");
116 print_sk(&srv_sk
, "srv_sk");
117 print_sk(&cli_sk
, "cli_sk");
118 print_tp(&listen_tp
, "listen_tp");
119 print_tp(&srv_tp
, "srv_tp");
120 print_tp(&cli_tp
, "cli_tp");
122 CHECK(listen_sk
.state
!= 10 ||
123 listen_sk
.family
!= AF_INET6
||
124 listen_sk
.protocol
!= IPPROTO_TCP
||
125 memcmp(listen_sk
.src_ip6
, &in6addr_loopback
,
126 sizeof(listen_sk
.src_ip6
)) ||
127 listen_sk
.dst_ip6
[0] || listen_sk
.dst_ip6
[1] ||
128 listen_sk
.dst_ip6
[2] || listen_sk
.dst_ip6
[3] ||
129 listen_sk
.src_port
!= ntohs(srv_sa6
.sin6_port
) ||
132 "Unexpected. Check listen_sk output. ingress_linum:%u\n",
135 CHECK(srv_sk
.state
== 10 ||
137 srv_sk
.family
!= AF_INET6
||
138 srv_sk
.protocol
!= IPPROTO_TCP
||
139 memcmp(srv_sk
.src_ip6
, &in6addr_loopback
,
140 sizeof(srv_sk
.src_ip6
)) ||
141 memcmp(srv_sk
.dst_ip6
, &in6addr_loopback
,
142 sizeof(srv_sk
.dst_ip6
)) ||
143 srv_sk
.src_port
!= ntohs(srv_sa6
.sin6_port
) ||
144 srv_sk
.dst_port
!= cli_sa6
.sin6_port
,
145 "srv_sk", "Unexpected. Check srv_sk output. egress_linum:%u\n",
148 CHECK(!skel
->bss
->lsndtime
, "srv_tp", "Unexpected lsndtime:0\n");
150 CHECK(cli_sk
.state
== 10 ||
152 cli_sk
.family
!= AF_INET6
||
153 cli_sk
.protocol
!= IPPROTO_TCP
||
154 memcmp(cli_sk
.src_ip6
, &in6addr_loopback
,
155 sizeof(cli_sk
.src_ip6
)) ||
156 memcmp(cli_sk
.dst_ip6
, &in6addr_loopback
,
157 sizeof(cli_sk
.dst_ip6
)) ||
158 cli_sk
.src_port
!= ntohs(cli_sa6
.sin6_port
) ||
159 cli_sk
.dst_port
!= srv_sa6
.sin6_port
,
160 "cli_sk", "Unexpected. Check cli_sk output. egress_linum:%u\n",
163 CHECK(listen_tp
.data_segs_out
||
164 listen_tp
.data_segs_in
||
165 listen_tp
.total_retrans
||
166 listen_tp
.bytes_acked
,
168 "Unexpected. Check listen_tp output. ingress_linum:%u\n",
171 CHECK(srv_tp
.data_segs_out
!= 2 ||
172 srv_tp
.data_segs_in
||
173 srv_tp
.snd_cwnd
!= 10 ||
174 srv_tp
.total_retrans
||
175 srv_tp
.bytes_acked
< 2 * DATA_LEN
,
176 "srv_tp", "Unexpected. Check srv_tp output. egress_linum:%u\n",
179 CHECK(cli_tp
.data_segs_out
||
180 cli_tp
.data_segs_in
!= 2 ||
181 cli_tp
.snd_cwnd
!= 10 ||
182 cli_tp
.total_retrans
||
183 cli_tp
.bytes_received
< 2 * DATA_LEN
,
184 "cli_tp", "Unexpected. Check cli_tp output. egress_linum:%u\n",
187 CHECK(skel
->bss
->parent_cg_id
!= parent_cg_id
,
188 "parent_cg_id", "%zu != %zu\n",
189 (size_t)skel
->bss
->parent_cg_id
, (size_t)parent_cg_id
);
191 CHECK(skel
->bss
->child_cg_id
!= child_cg_id
,
192 "child_cg_id", "%zu != %zu\n",
193 (size_t)skel
->bss
->child_cg_id
, (size_t)child_cg_id
);
196 static void check_sk_pkt_out_cnt(int accept_fd
, int cli_fd
)
198 struct bpf_spinlock_cnt pkt_out_cnt
= {}, pkt_out_cnt10
= {};
201 pkt_out_cnt
.cnt
= ~0;
202 pkt_out_cnt10
.cnt
= ~0;
203 err
= bpf_map_lookup_elem(sk_pkt_out_cnt_fd
, &accept_fd
, &pkt_out_cnt
);
205 err
= bpf_map_lookup_elem(sk_pkt_out_cnt10_fd
, &accept_fd
,
208 /* The bpf prog only counts for fullsock and
209 * passive connection did not become fullsock until 3WHS
210 * had been finished, so the bpf prog only counted two data
213 CHECK(err
|| pkt_out_cnt
.cnt
< 0xeB9F + 2 ||
214 pkt_out_cnt10
.cnt
< 0xeB9F + 20,
215 "bpf_map_lookup_elem(sk_pkt_out_cnt, &accept_fd)",
216 "err:%d errno:%d pkt_out_cnt:%u pkt_out_cnt10:%u\n",
217 err
, errno
, pkt_out_cnt
.cnt
, pkt_out_cnt10
.cnt
);
219 pkt_out_cnt
.cnt
= ~0;
220 pkt_out_cnt10
.cnt
= ~0;
221 err
= bpf_map_lookup_elem(sk_pkt_out_cnt_fd
, &cli_fd
, &pkt_out_cnt
);
223 err
= bpf_map_lookup_elem(sk_pkt_out_cnt10_fd
, &cli_fd
,
225 /* Active connection is fullsock from the beginning.
226 * 1 SYN and 1 ACK during 3WHS
227 * 2 Acks on data packet.
229 * The bpf_prog initialized it to 0xeB9F.
231 CHECK(err
|| pkt_out_cnt
.cnt
< 0xeB9F + 4 ||
232 pkt_out_cnt10
.cnt
< 0xeB9F + 40,
233 "bpf_map_lookup_elem(sk_pkt_out_cnt, &cli_fd)",
234 "err:%d errno:%d pkt_out_cnt:%u pkt_out_cnt10:%u\n",
235 err
, errno
, pkt_out_cnt
.cnt
, pkt_out_cnt10
.cnt
);
238 static int init_sk_storage(int sk_fd
, __u32 pkt_out_cnt
)
240 struct bpf_spinlock_cnt scnt
= {};
243 scnt
.cnt
= pkt_out_cnt
;
244 err
= bpf_map_update_elem(sk_pkt_out_cnt_fd
, &sk_fd
, &scnt
,
246 if (CHECK(err
, "bpf_map_update_elem(sk_pkt_out_cnt_fd)",
247 "err:%d errno:%d\n", err
, errno
))
250 err
= bpf_map_update_elem(sk_pkt_out_cnt10_fd
, &sk_fd
, &scnt
,
252 if (CHECK(err
, "bpf_map_update_elem(sk_pkt_out_cnt10_fd)",
253 "err:%d errno:%d\n", err
, errno
))
259 static void test(void)
261 int listen_fd
= -1, cli_fd
= -1, accept_fd
= -1, err
, i
;
262 socklen_t addrlen
= sizeof(struct sockaddr_in6
);
265 /* Prepare listen_fd */
266 listen_fd
= start_server(AF_INET6
, SOCK_STREAM
, "::1", 0, 0);
267 /* start_server() has logged the error details */
268 if (CHECK_FAIL(listen_fd
== -1))
271 err
= getsockname(listen_fd
, (struct sockaddr
*)&srv_sa6
, &addrlen
);
272 if (CHECK(err
, "getsockname(listen_fd)", "err:%d errno:%d\n", err
,
275 memcpy(&skel
->bss
->srv_sa6
, &srv_sa6
, sizeof(srv_sa6
));
277 cli_fd
= connect_to_fd(listen_fd
, 0);
278 if (CHECK_FAIL(cli_fd
== -1))
281 err
= getsockname(cli_fd
, (struct sockaddr
*)&cli_sa6
, &addrlen
);
282 if (CHECK(err
, "getsockname(cli_fd)", "err:%d errno:%d\n",
286 accept_fd
= accept(listen_fd
, NULL
, NULL
);
287 if (CHECK(accept_fd
== -1, "accept(listen_fd)",
288 "accept_fd:%d errno:%d\n",
292 if (init_sk_storage(accept_fd
, 0xeB9F))
295 for (i
= 0; i
< 2; i
++) {
296 /* Send some data from accept_fd to cli_fd.
297 * MSG_EOR to stop kernel from coalescing two pkts.
299 err
= send(accept_fd
, DATA
, DATA_LEN
, MSG_EOR
);
300 if (CHECK(err
!= DATA_LEN
, "send(accept_fd)",
301 "err:%d errno:%d\n", err
, errno
))
304 err
= recv(cli_fd
, buf
, DATA_LEN
, 0);
305 if (CHECK(err
!= DATA_LEN
, "recv(cli_fd)", "err:%d errno:%d\n",
310 shutdown(cli_fd
, SHUT_WR
);
311 err
= recv(accept_fd
, buf
, 1, 0);
312 if (CHECK(err
, "recv(accept_fd) for fin", "err:%d errno:%d\n",
315 shutdown(accept_fd
, SHUT_WR
);
316 err
= recv(cli_fd
, buf
, 1, 0);
317 if (CHECK(err
, "recv(cli_fd) for fin", "err:%d errno:%d\n",
320 check_sk_pkt_out_cnt(accept_fd
, cli_fd
);
332 void test_sock_fields(void)
334 struct bpf_link
*egress_link
= NULL
, *ingress_link
= NULL
;
335 int parent_cg_fd
= -1, child_cg_fd
= -1;
337 /* Create a cgroup, get fd, and join it */
338 parent_cg_fd
= test__join_cgroup(PARENT_CGROUP
);
339 if (CHECK_FAIL(parent_cg_fd
< 0))
341 parent_cg_id
= get_cgroup_id(PARENT_CGROUP
);
342 if (CHECK_FAIL(!parent_cg_id
))
345 child_cg_fd
= test__join_cgroup(CHILD_CGROUP
);
346 if (CHECK_FAIL(child_cg_fd
< 0))
348 child_cg_id
= get_cgroup_id(CHILD_CGROUP
);
349 if (CHECK_FAIL(!child_cg_id
))
352 skel
= test_sock_fields__open_and_load();
353 if (CHECK(!skel
, "test_sock_fields__open_and_load", "failed\n"))
356 egress_link
= bpf_program__attach_cgroup(skel
->progs
.egress_read_sock_fields
,
358 if (CHECK(IS_ERR(egress_link
), "attach_cgroup(egress)", "err:%ld\n",
359 PTR_ERR(egress_link
)))
362 ingress_link
= bpf_program__attach_cgroup(skel
->progs
.ingress_read_sock_fields
,
364 if (CHECK(IS_ERR(ingress_link
), "attach_cgroup(ingress)", "err:%ld\n",
365 PTR_ERR(ingress_link
)))
368 linum_map_fd
= bpf_map__fd(skel
->maps
.linum_map
);
369 sk_pkt_out_cnt_fd
= bpf_map__fd(skel
->maps
.sk_pkt_out_cnt
);
370 sk_pkt_out_cnt10_fd
= bpf_map__fd(skel
->maps
.sk_pkt_out_cnt10
);
375 bpf_link__destroy(egress_link
);
376 bpf_link__destroy(ingress_link
);
377 test_sock_fields__destroy(skel
);
378 if (child_cg_fd
!= -1)
380 if (parent_cg_fd
!= -1)