1b5587398SStanislav Fomichev // SPDX-License-Identifier: GPL-2.0 2b5587398SStanislav Fomichev #include <linux/bpf.h> 3*3e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h> 4b5587398SStanislav Fomichev 5b5587398SStanislav Fomichev char _license[] SEC("license") = "GPL"; 6b5587398SStanislav Fomichev 7b5587398SStanislav Fomichev struct tcp_rtt_storage { 8b5587398SStanislav Fomichev __u32 invoked; 9b5587398SStanislav Fomichev __u32 dsack_dups; 10b5587398SStanislav Fomichev __u32 delivered; 11b5587398SStanislav Fomichev __u32 delivered_ce; 12b5587398SStanislav Fomichev __u32 icsk_retransmits; 13b5587398SStanislav Fomichev }; 14b5587398SStanislav Fomichev 1536b5d471SAndrii Nakryiko struct { 1636b5d471SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_SK_STORAGE); 1736b5d471SAndrii Nakryiko __uint(map_flags, BPF_F_NO_PREALLOC); 1836b5d471SAndrii Nakryiko __type(key, int); 1936b5d471SAndrii Nakryiko __type(value, struct tcp_rtt_storage); 2036b5d471SAndrii Nakryiko } socket_storage_map SEC(".maps"); 21b5587398SStanislav Fomichev 22b5587398SStanislav Fomichev SEC("sockops") _sockops(struct bpf_sock_ops * ctx)23b5587398SStanislav Fomichevint _sockops(struct bpf_sock_ops *ctx) 24b5587398SStanislav Fomichev { 25b5587398SStanislav Fomichev struct tcp_rtt_storage *storage; 26b5587398SStanislav Fomichev struct bpf_tcp_sock *tcp_sk; 27b5587398SStanislav Fomichev int op = (int) ctx->op; 28b5587398SStanislav Fomichev struct bpf_sock *sk; 29b5587398SStanislav Fomichev 30b5587398SStanislav Fomichev sk = ctx->sk; 31b5587398SStanislav Fomichev if (!sk) 32b5587398SStanislav Fomichev return 1; 33b5587398SStanislav Fomichev 34b5587398SStanislav Fomichev storage = bpf_sk_storage_get(&socket_storage_map, sk, 0, 35b5587398SStanislav Fomichev BPF_SK_STORAGE_GET_F_CREATE); 36b5587398SStanislav Fomichev if (!storage) 37b5587398SStanislav Fomichev return 1; 38b5587398SStanislav Fomichev 39b5587398SStanislav Fomichev if (op == BPF_SOCK_OPS_TCP_CONNECT_CB) { 40b5587398SStanislav Fomichev bpf_sock_ops_cb_flags_set(ctx, BPF_SOCK_OPS_RTT_CB_FLAG); 41b5587398SStanislav Fomichev return 1; 42b5587398SStanislav Fomichev } 43b5587398SStanislav Fomichev 44b5587398SStanislav Fomichev if (op != BPF_SOCK_OPS_RTT_CB) 45b5587398SStanislav Fomichev return 1; 46b5587398SStanislav Fomichev 47b5587398SStanislav Fomichev tcp_sk = bpf_tcp_sock(sk); 48b5587398SStanislav Fomichev if (!tcp_sk) 49b5587398SStanislav Fomichev return 1; 50b5587398SStanislav Fomichev 51b5587398SStanislav Fomichev storage->invoked++; 52b5587398SStanislav Fomichev 53b5587398SStanislav Fomichev storage->dsack_dups = tcp_sk->dsack_dups; 54b5587398SStanislav Fomichev storage->delivered = tcp_sk->delivered; 55b5587398SStanislav Fomichev storage->delivered_ce = tcp_sk->delivered_ce; 56b5587398SStanislav Fomichev storage->icsk_retransmits = tcp_sk->icsk_retransmits; 57b5587398SStanislav Fomichev 58b5587398SStanislav Fomichev return 1; 59b5587398SStanislav Fomichev } 60