1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include "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 bpf_map_def SEC("maps") socket_storage_map = {
17 	.type = BPF_MAP_TYPE_SK_STORAGE,
18 	.key_size = sizeof(int),
19 	.value_size = sizeof(struct tcp_rtt_storage),
20 	.map_flags = BPF_F_NO_PREALLOC,
21 };
22 BPF_ANNOTATE_KV_PAIR(socket_storage_map, int, struct tcp_rtt_storage);
23 
24 SEC("sockops")
25 int _sockops(struct bpf_sock_ops *ctx)
26 {
27 	struct tcp_rtt_storage *storage;
28 	struct bpf_tcp_sock *tcp_sk;
29 	int op = (int) ctx->op;
30 	struct bpf_sock *sk;
31 
32 	sk = ctx->sk;
33 	if (!sk)
34 		return 1;
35 
36 	storage = bpf_sk_storage_get(&socket_storage_map, sk, 0,
37 				     BPF_SK_STORAGE_GET_F_CREATE);
38 	if (!storage)
39 		return 1;
40 
41 	if (op == BPF_SOCK_OPS_TCP_CONNECT_CB) {
42 		bpf_sock_ops_cb_flags_set(ctx, BPF_SOCK_OPS_RTT_CB_FLAG);
43 		return 1;
44 	}
45 
46 	if (op != BPF_SOCK_OPS_RTT_CB)
47 		return 1;
48 
49 	tcp_sk = bpf_tcp_sock(sk);
50 	if (!tcp_sk)
51 		return 1;
52 
53 	storage->invoked++;
54 
55 	storage->dsack_dups = tcp_sk->dsack_dups;
56 	storage->delivered = tcp_sk->delivered;
57 	storage->delivered_ce = tcp_sk->delivered_ce;
58 	storage->icsk_retransmits = tcp_sk->icsk_retransmits;
59 
60 	return 1;
61 }
62