1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_TCP_HELPERS_H
3 #define __BPF_TCP_HELPERS_H
6 #include <linux/types.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_core_read.h>
9 #include <bpf/bpf_tracing.h>
11 #define BPF_STRUCT_OPS(name, args...) \
12 SEC("struct_ops/"#name) \
19 #define tcp_jiffies32 ((__u32)bpf_jiffies64())
22 unsigned char skc_state;
24 } __attribute__((preserve_access_index));
33 struct sock_common __sk_common;
34 #define sk_state __sk_common.skc_state
35 unsigned long sk_pacing_rate;
36 __u32 sk_pacing_status; /* see enum sk_pacing */
37 } __attribute__((preserve_access_index));
41 } __attribute__((preserve_access_index));
43 struct inet_connection_sock {
44 struct inet_sock icsk_inet;
51 __u64 icsk_ca_priv[104 / sizeof(__u64)];
52 } __attribute__((preserve_access_index));
55 struct sock_common __req_common;
56 } __attribute__((preserve_access_index));
59 struct inet_connection_sock inet_conn;
72 __u8 syn_data:1, /* SYN includes data */
73 syn_fastopen:1, /* SYN includes Fast Open option */
74 syn_fastopen_exp:1,/* SYN includes Fast Open exp. option */
75 syn_fastopen_ch:1, /* Active TFO re-enabling probe */
76 syn_data_acked:1,/* data in SYN is acked by SYN-ACK */
77 save_syn:1, /* Save headers of SYN packet */
78 is_cwnd_limited:1,/* forward progress limited by snd_cwnd? */
79 syn_smc:1; /* SYN includes SMC */
80 __u32 max_packets_out;
83 __u64 tcp_mstamp; /* most recent packet received/sent */
85 } __attribute__((preserve_access_index));
87 static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
89 return (struct inet_connection_sock *)sk;
92 static __always_inline void *inet_csk_ca(const struct sock *sk)
94 return (void *)inet_csk(sk)->icsk_ca_priv;
97 static __always_inline struct tcp_sock *tcp_sk(const struct sock *sk)
99 return (struct tcp_sock *)sk;
102 static __always_inline bool before(__u32 seq1, __u32 seq2)
104 return (__s32)(seq1-seq2) < 0;
106 #define after(seq2, seq1) before(seq1, seq2)
109 #define TCP_ECN_QUEUE_CWR 2
110 #define TCP_ECN_DEMAND_CWR 4
111 #define TCP_ECN_SEEN 8
113 enum inet_csk_ack_state_t {
117 ICSK_ACK_PUSHED2 = 8,
118 ICSK_ACK_NOW = 16 /* Send the next ACK immediately (once) */
122 CA_EVENT_TX_START = 0,
123 CA_EVENT_CWND_RESTART = 1,
124 CA_EVENT_COMPLETE_CWR = 2,
126 CA_EVENT_ECN_NO_CE = 4,
127 CA_EVENT_ECN_IS_CE = 5,
134 } __attribute__((preserve_access_index));
137 __u64 prior_mstamp; /* starting timestamp for interval */
138 __u32 prior_delivered; /* tp->delivered at "prior_mstamp" */
139 __s32 delivered; /* number of packets delivered over interval */
140 long interval_us; /* time for tp->delivered to incr "delivered" */
141 __u32 snd_interval_us; /* snd interval for delivered packets */
142 __u32 rcv_interval_us; /* rcv interval for delivered packets */
143 long rtt_us; /* RTT of last (S)ACKed packet (or -1) */
144 int losses; /* number of packets marked lost upon ACK */
145 __u32 acked_sacked; /* number of packets newly (S)ACKed upon ACK */
146 __u32 prior_in_flight; /* in flight before this ACK */
147 bool is_app_limited; /* is sample from packet with bubble in pipe? */
148 bool is_retrans; /* is sample from retransmission? */
149 bool is_ack_delayed; /* is this (likely) a delayed ACK? */
150 } __attribute__((preserve_access_index));
152 #define TCP_CA_NAME_MAX 16
153 #define TCP_CONG_NEEDS_ECN 0x2
155 struct tcp_congestion_ops {
156 char name[TCP_CA_NAME_MAX];
159 /* initialize private data (optional) */
160 void (*init)(struct sock *sk);
161 /* cleanup private data (optional) */
162 void (*release)(struct sock *sk);
164 /* return slow start threshold (required) */
165 __u32 (*ssthresh)(struct sock *sk);
166 /* do new cwnd calculation (required) */
167 void (*cong_avoid)(struct sock *sk, __u32 ack, __u32 acked);
168 /* call before changing ca_state (optional) */
169 void (*set_state)(struct sock *sk, __u8 new_state);
170 /* call when cwnd event occurs (optional) */
171 void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
172 /* call when ack arrives (optional) */
173 void (*in_ack_event)(struct sock *sk, __u32 flags);
174 /* new value of cwnd after loss (required) */
175 __u32 (*undo_cwnd)(struct sock *sk);
176 /* hook for packet ack accounting (optional) */
177 void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
178 /* override sysctl_tcp_min_tso_segs */
179 __u32 (*min_tso_segs)(struct sock *sk);
180 /* returns the multiplier used in tcp_sndbuf_expand (optional) */
181 __u32 (*sndbuf_expand)(struct sock *sk);
182 /* call when packets are delivered to update cwnd and pacing rate,
183 * after all the ca_state processing. (optional)
185 void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
189 #define min(a, b) ((a) < (b) ? (a) : (b))
190 #define max(a, b) ((a) > (b) ? (a) : (b))
191 #define min_not_zero(x, y) ({ \
192 typeof(x) __x = (x); \
193 typeof(y) __y = (y); \
194 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
196 static __always_inline bool tcp_in_slow_start(const struct tcp_sock *tp)
198 return tp->snd_cwnd < tp->snd_ssthresh;
201 static __always_inline bool tcp_is_cwnd_limited(const struct sock *sk)
203 const struct tcp_sock *tp = tcp_sk(sk);
205 /* If in slow start, ensure cwnd grows to twice what was ACKed. */
206 if (tcp_in_slow_start(tp))
207 return tp->snd_cwnd < 2 * tp->max_packets_out;
209 return !!BPF_CORE_READ_BITFIELD(tp, is_cwnd_limited);
212 static __always_inline bool tcp_cc_eq(const char *a, const char *b)
216 for (i = 0; i < TCP_CA_NAME_MAX; i++) {
226 extern __u32 tcp_slow_start(struct tcp_sock *tp, __u32 acked) __ksym;
227 extern void tcp_cong_avoid_ai(struct tcp_sock *tp, __u32 w, __u32 acked) __ksym;
230 struct inet_connection_sock sk;
233 } __attribute__((preserve_access_index));