1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <linux/btf.h>
40#include <linux/btf_ids.h>
41#include <linux/module.h>
42#include <linux/mm.h>
43#include <net/tcp.h>
44#include <linux/inet_diag.h>
45#include "tcp_dctcp.h"
46
47#define DCTCP_MAX_ALPHA 1024U
48
49struct dctcp {
50 u32 old_delivered;
51 u32 old_delivered_ce;
52 u32 prior_rcv_nxt;
53 u32 dctcp_alpha;
54 u32 next_seq;
55 u32 ce_state;
56 u32 loss_cwnd;
57};
58
59static unsigned int dctcp_shift_g __read_mostly = 4;
60module_param(dctcp_shift_g, uint, 0644);
61MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha");
62
63static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA;
64module_param(dctcp_alpha_on_init, uint, 0644);
65MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value");
66
67static struct tcp_congestion_ops dctcp_reno;
68
69static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca)
70{
71 ca->next_seq = tp->snd_nxt;
72
73 ca->old_delivered = tp->delivered;
74 ca->old_delivered_ce = tp->delivered_ce;
75}
76
77static void dctcp_init(struct sock *sk)
78{
79 const struct tcp_sock *tp = tcp_sk(sk);
80
81 if ((tp->ecn_flags & TCP_ECN_OK) ||
82 (sk->sk_state == TCP_LISTEN ||
83 sk->sk_state == TCP_CLOSE)) {
84 struct dctcp *ca = inet_csk_ca(sk);
85
86 ca->prior_rcv_nxt = tp->rcv_nxt;
87
88 ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
89
90 ca->loss_cwnd = 0;
91 ca->ce_state = 0;
92
93 dctcp_reset(tp, ca);
94 return;
95 }
96
97
98
99
100 inet_csk(sk)->icsk_ca_ops = &dctcp_reno;
101 INET_ECN_dontxmit(sk);
102}
103
104static u32 dctcp_ssthresh(struct sock *sk)
105{
106 struct dctcp *ca = inet_csk_ca(sk);
107 struct tcp_sock *tp = tcp_sk(sk);
108
109 ca->loss_cwnd = tp->snd_cwnd;
110 return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->dctcp_alpha) >> 11U), 2U);
111}
112
113static void dctcp_update_alpha(struct sock *sk, u32 flags)
114{
115 const struct tcp_sock *tp = tcp_sk(sk);
116 struct dctcp *ca = inet_csk_ca(sk);
117
118
119 if (!before(tp->snd_una, ca->next_seq)) {
120 u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce;
121 u32 alpha = ca->dctcp_alpha;
122
123
124
125 alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
126 if (delivered_ce) {
127 u32 delivered = tp->delivered - ca->old_delivered;
128
129
130
131
132 delivered_ce <<= (10 - dctcp_shift_g);
133 delivered_ce /= max(1U, delivered);
134
135 alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA);
136 }
137
138
139
140
141 WRITE_ONCE(ca->dctcp_alpha, alpha);
142 dctcp_reset(tp, ca);
143 }
144}
145
146static void dctcp_react_to_loss(struct sock *sk)
147{
148 struct dctcp *ca = inet_csk_ca(sk);
149 struct tcp_sock *tp = tcp_sk(sk);
150
151 ca->loss_cwnd = tp->snd_cwnd;
152 tp->snd_ssthresh = max(tp->snd_cwnd >> 1U, 2U);
153}
154
155static void dctcp_state(struct sock *sk, u8 new_state)
156{
157 if (new_state == TCP_CA_Recovery &&
158 new_state != inet_csk(sk)->icsk_ca_state)
159 dctcp_react_to_loss(sk);
160
161
162
163}
164
165static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
166{
167 struct dctcp *ca = inet_csk_ca(sk);
168
169 switch (ev) {
170 case CA_EVENT_ECN_IS_CE:
171 case CA_EVENT_ECN_NO_CE:
172 dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state);
173 break;
174 case CA_EVENT_LOSS:
175 dctcp_react_to_loss(sk);
176 break;
177 default:
178
179 break;
180 }
181}
182
183static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr,
184 union tcp_cc_info *info)
185{
186 const struct dctcp *ca = inet_csk_ca(sk);
187 const struct tcp_sock *tp = tcp_sk(sk);
188
189
190
191
192 if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) ||
193 ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
194 memset(&info->dctcp, 0, sizeof(info->dctcp));
195 if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) {
196 info->dctcp.dctcp_enabled = 1;
197 info->dctcp.dctcp_ce_state = (u16) ca->ce_state;
198 info->dctcp.dctcp_alpha = ca->dctcp_alpha;
199 info->dctcp.dctcp_ab_ecn = tp->mss_cache *
200 (tp->delivered_ce - ca->old_delivered_ce);
201 info->dctcp.dctcp_ab_tot = tp->mss_cache *
202 (tp->delivered - ca->old_delivered);
203 }
204
205 *attr = INET_DIAG_DCTCPINFO;
206 return sizeof(info->dctcp);
207 }
208 return 0;
209}
210
211static u32 dctcp_cwnd_undo(struct sock *sk)
212{
213 const struct dctcp *ca = inet_csk_ca(sk);
214
215 return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
216}
217
218static struct tcp_congestion_ops dctcp __read_mostly = {
219 .init = dctcp_init,
220 .in_ack_event = dctcp_update_alpha,
221 .cwnd_event = dctcp_cwnd_event,
222 .ssthresh = dctcp_ssthresh,
223 .cong_avoid = tcp_reno_cong_avoid,
224 .undo_cwnd = dctcp_cwnd_undo,
225 .set_state = dctcp_state,
226 .get_info = dctcp_get_info,
227 .flags = TCP_CONG_NEEDS_ECN,
228 .owner = THIS_MODULE,
229 .name = "dctcp",
230};
231
232static struct tcp_congestion_ops dctcp_reno __read_mostly = {
233 .ssthresh = tcp_reno_ssthresh,
234 .cong_avoid = tcp_reno_cong_avoid,
235 .undo_cwnd = tcp_reno_undo_cwnd,
236 .get_info = dctcp_get_info,
237 .owner = THIS_MODULE,
238 .name = "dctcp-reno",
239};
240
241BTF_SET_START(tcp_dctcp_kfunc_ids)
242#ifdef CONFIG_X86
243#ifdef CONFIG_DYNAMIC_FTRACE
244BTF_ID(func, dctcp_init)
245BTF_ID(func, dctcp_update_alpha)
246BTF_ID(func, dctcp_cwnd_event)
247BTF_ID(func, dctcp_ssthresh)
248BTF_ID(func, dctcp_cwnd_undo)
249BTF_ID(func, dctcp_state)
250#endif
251#endif
252BTF_SET_END(tcp_dctcp_kfunc_ids)
253
254static DEFINE_KFUNC_BTF_ID_SET(&tcp_dctcp_kfunc_ids, tcp_dctcp_kfunc_btf_set);
255
256static int __init dctcp_register(void)
257{
258 int ret;
259
260 BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
261 ret = tcp_register_congestion_control(&dctcp);
262 if (ret)
263 return ret;
264 register_kfunc_btf_id_set(&bpf_tcp_ca_kfunc_list, &tcp_dctcp_kfunc_btf_set);
265 return 0;
266}
267
268static void __exit dctcp_unregister(void)
269{
270 unregister_kfunc_btf_id_set(&bpf_tcp_ca_kfunc_list, &tcp_dctcp_kfunc_btf_set);
271 tcp_unregister_congestion_control(&dctcp);
272}
273
274module_init(dctcp_register);
275module_exit(dctcp_unregister);
276
277MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
278MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
279MODULE_AUTHOR("Glenn Judd <glenn.judd@morganstanley.com>");
280
281MODULE_LICENSE("GPL v2");
282MODULE_DESCRIPTION("DataCenter TCP (DCTCP)");
283