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