xref: /linux/tools/testing/selftests/bpf/progs/tcp_rtt.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <bpf/bpf_helpers.h>
4 
5 char _license[] SEC("license") = "GPL";
6 __u32 _version SEC("version") = 1;
7 
8 struct tcp_rtt_storage {
9 	__u32 invoked;
10 	__u32 dsack_dups;
11 	__u32 delivered;
12 	__u32 delivered_ce;
13 	__u32 icsk_retransmits;
14 };
15 
16 struct {
17 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
18 	__uint(map_flags, BPF_F_NO_PREALLOC);
19 	__type(key, int);
20 	__type(value, struct tcp_rtt_storage);
21 } socket_storage_map SEC(".maps");
22 
23 SEC("sockops")
24 int _sockops(struct bpf_sock_ops *ctx)
25 {
26 	struct tcp_rtt_storage *storage;
27 	struct bpf_tcp_sock *tcp_sk;
28 	int op = (int) ctx->op;
29 	struct bpf_sock *sk;
30 
31 	sk = ctx->sk;
32 	if (!sk)
33 		return 1;
34 
35 	storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
36 				     BPF_SK_STORAGE_GET_F_CREATE);
37 	if (!storage)
38 		return 1;
39 
40 	if (op == BPF_SOCK_OPS_TCP_CONNECT_CB) {
41 		bpf_sock_ops_cb_flags_set(ctx, BPF_SOCK_OPS_RTT_CB_FLAG);
42 		return 1;
43 	}
44 
45 	if (op != BPF_SOCK_OPS_RTT_CB)
46 		return 1;
47 
48 	tcp_sk = bpf_tcp_sock(sk);
49 	if (!tcp_sk)
50 		return 1;
51 
52 	storage->invoked++;
53 
54 	storage->dsack_dups = tcp_sk->dsack_dups;
55 	storage->delivered = tcp_sk->delivered;
56 	storage->delivered_ce = tcp_sk->delivered_ce;
57 	storage->icsk_retransmits = tcp_sk->icsk_retransmits;
58 
59 	return 1;
60 }
61