forked from ccp-project/ccp-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_ccp.c
469 lines (403 loc) · 12.3 KB
/
tcp_ccp.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "tcp_ccp.h"
#include "libccp/ccp.h"
#if __KERNEL_VERSION_MINOR__ <= 16 && __KERNEL_VERSION_MINOR__ >= 13
#define COMPAT_MODE
#elif __KERNEL_VERSION_MINOR__ >= 19
#define RATESAMPLE_MODE
#endif
#define IPC_NETLINK 0
#define IPC_CHARDEV 1
#if __IPC__ == IPC_NETLINK
#include "ccp_nl.h"
#elif __IPC__ == IPC_CHARDEV
#include "ccpkp/ccpkp.h"
#endif
#include <linux/module.h>
#include <linux/time64.h>
#include <linux/timekeeping.h>
#include <net/tcp.h>
#define CCP_FRAC_DENOM 10
#define CCP_EWMA_RECENCY 6
void ccp_set_pacing_rate(struct sock *sk, uint32_t rate) {
sk->sk_pacing_rate = rate;
}
static int rate_sample_valid(const struct rate_sample *rs) {
int ret = 0;
if (rs->delivered <= 0)
ret |= 1;
if (rs->interval_us <= 0)
ret |= 1 << 1;
if (rs->rtt_us <= 0)
ret |= 1 << 2;
return ret;
}
static inline void get_sock_from_ccp(
struct sock **sk,
struct ccp_connection *conn
) {
*sk = (struct sock*) ccp_get_impl(conn);
}
static void do_set_cwnd(
struct ccp_datapath *dp,
struct ccp_connection *conn,
uint32_t cwnd
) {
struct sock *sk;
struct tcp_sock *tp;
get_sock_from_ccp(&sk, conn);
tp = tcp_sk(sk);
// translate cwnd value back into packets
cwnd /= tp->mss_cache;
tp->snd_cwnd = cwnd;
}
static void do_set_rate_abs(
struct ccp_datapath *dp,
struct ccp_connection *conn,
uint32_t rate
) {
struct sock *sk;
get_sock_from_ccp(&sk, conn);
ccp_set_pacing_rate(sk, rate);
}
static void do_set_rate_rel(
struct ccp_datapath *dp,
struct ccp_connection *conn,
uint32_t factor
) {
struct sock *sk;
uint64_t newrate;
get_sock_from_ccp(&sk, conn);
// factor is * 100
newrate = sk->sk_pacing_rate * factor;
do_div(newrate, 100);
ccp_set_pacing_rate(sk, newrate);
}
struct timespec64 tzero;
static u64 ccp_now(void) {
struct timespec64 now, diff;
getnstimeofday64(&now);
diff = timespec64_sub(now, tzero);
return timespec64_to_ns(&diff);
}
static u64 ccp_since(u64 then) {
struct timespec64 now, then_ts, diff;
getnstimeofday64(&now);
then_ts = tzero;
timespec64_add_ns(&then_ts, then);
diff = timespec64_sub(now, then_ts);
return timespec64_to_ns(&diff) / NSEC_PER_USEC;
}
static u64 ccp_after(u64 us) {
struct timespec64 now;
getnstimeofday64(&now);
now = timespec64_sub(now, tzero);
timespec64_add_ns(&now, us * NSEC_PER_USEC);
return timespec64_to_ns(&now);
}
// in dctcp code, in ack event used for ecn information per packet
void tcp_ccp_in_ack_event(struct sock *sk, u32 flags) {
// according to tcp_input, in_ack_event is called before cong_control, so mmt.ack has old ack value
const struct tcp_sock *tp = tcp_sk(sk);
struct ccp *ca = inet_csk_ca(sk);
struct ccp_primitives *mmt;
u32 acked_bytes;
#ifdef COMPAT_MODE
int i=0;
struct sk_buff *skb = tcp_write_queue_head(sk);
struct tcp_skb_cb *scb;
#endif
if (ca->dp == NULL) {
pr_info("ccp: ccp_connection not initialized");
return;
}
#ifdef COMPAT_MODE
for (i=0; i < MAX_SKB_STORED; i++) {
ca->skb_array[i].first_tx_mstamp = 0;
ca->skb_array[i].interval_us = 0;
}
for (i=0; i < MAX_SKB_STORED; i++) {
if (skb) {
scb = TCP_SKB_CB(skb);
ca->skb_array[i].first_tx_mstamp = skb->skb_mstamp;
ca->skb_array[i].interval_us = tcp_stamp_us_delta(skb->skb_mstamp, scb->tx.first_tx_mstamp);
skb = skb->next;
}
}
#endif
mmt = &ca->dp->prims;
acked_bytes = tp->snd_una - ca->last_snd_una;
ca->last_snd_una = tp->snd_una;
if (acked_bytes) {
if (flags & CA_ACK_ECE) {
mmt->ecn_bytes = (u64)acked_bytes;
mmt->ecn_packets = (u64)acked_bytes / tp->mss_cache;
} else {
mmt->ecn_bytes = 0;
mmt->ecn_packets = 0;
}
}
}
EXPORT_SYMBOL_GPL(tcp_ccp_in_ack_event);
/* load the primitive registers of the rate sample - convert all to u64
* raw values, not averaged
*/
int load_primitives(struct sock *sk, const struct rate_sample *rs) {
struct tcp_sock *tp = tcp_sk(sk);
struct ccp *ca = inet_csk_ca(sk);
struct ccp_primitives *mmt = &ca->dp->prims;
#ifdef COMPAT_MODE
int i=0;
#endif
u64 rin = 0; // send bandwidth in bytes per second
u64 rout = 0; // recv bandwidth in bytes per second
u64 ack_us = 0;
u64 snd_us = 0;
int measured_valid_rate = rate_sample_valid(rs);
if ( measured_valid_rate != 0 ) {
return -1;
}
#ifdef COMPAT_MODE
// receive rate
ack_us = tcp_stamp_us_delta(tp->tcp_mstamp, rs->prior_mstamp);
// send rate
for (i=0; i < MAX_SKB_STORED; i++) {
if (ca->skb_array[i].first_tx_mstamp == tp->first_tx_mstamp) {
snd_us = ca->skb_array[i].interval_us;
break;
}
}
#endif
#ifdef RATESAMPLE_MODE
ack_us = rs->rcv_interval_us;
snd_us = rs->snd_interval_us;
#endif
if (ack_us != 0 && snd_us != 0) {
rin = rout = (u64)rs->delivered * MTU * S_TO_US;
do_div(rin, snd_us);
do_div(rout, ack_us);
}
mmt->bytes_acked = tp->bytes_acked - ca->last_bytes_acked;
ca->last_bytes_acked = tp->bytes_acked;
mmt->packets_misordered = tp->sacked_out - ca->last_sacked_out;
if (tp->sacked_out < ca->last_sacked_out) {
mmt->packets_misordered = 0;
} else {
mmt->packets_misordered = tp->sacked_out - ca->last_sacked_out;
}
ca->last_sacked_out = tp->sacked_out;
mmt->packets_acked = rs->acked_sacked - mmt->packets_misordered;
mmt->bytes_misordered = mmt->packets_misordered * tp->mss_cache;
mmt->lost_pkts_sample = rs->losses;
mmt->rtt_sample_us = rs->rtt_us;
if ( rin != 0 ) {
mmt->rate_outgoing = rin;
}
if ( rout != 0 ) {
mmt->rate_incoming = rout;
}
mmt->bytes_in_flight = tcp_packets_in_flight(tp) * tp->mss_cache;
mmt->packets_in_flight = tcp_packets_in_flight(tp);
if (tp->snd_cwnd <= 0) {
return -1;
}
mmt->snd_cwnd = tp->snd_cwnd * tp->mss_cache;
if (unlikely(tp->snd_una > tp->write_seq)) {
mmt->bytes_pending = ((u32) ~0U) - (tp->snd_una - tp->write_seq);
} else {
mmt->bytes_pending = (tp->write_seq - tp->snd_una);
}
return 0;
}
void tcp_ccp_cong_control(struct sock *sk, const struct rate_sample *rs) {
// aggregate measurement
// state = fold(state, rs)
int ok;
struct ccp *ca = inet_csk_ca(sk);
struct ccp_connection *dp = ca->dp;
#if __IPC__ == IPC_CHARDEV
ccpkp_try_read();
#endif
if (dp != NULL) {
// load primitive registers
ok = load_primitives(sk, rs);
if (ok < 0) {
return;
}
ccp_invoke(dp);
ca->dp->prims.was_timeout = false;
} else {
pr_info("ccp: ccp_connection not initialized");
}
}
EXPORT_SYMBOL_GPL(tcp_ccp_cong_control);
/* Slow start threshold is half the congestion window (min 2) */
u32 tcp_ccp_ssthresh(struct sock *sk) {
const struct tcp_sock *tp = tcp_sk(sk);
return max(tp->snd_cwnd >> 1U, 2U);
}
EXPORT_SYMBOL_GPL(tcp_ccp_ssthresh);
u32 tcp_ccp_undo_cwnd(struct sock *sk) {
const struct tcp_sock *tp = tcp_sk(sk);
return max(tp->snd_cwnd, tp->snd_ssthresh << 1);
}
EXPORT_SYMBOL_GPL(tcp_ccp_undo_cwnd);
void tcp_ccp_pkts_acked(struct sock *sk, const struct ack_sample *sample) {
struct ccp *cpl;
s32 sampleRTT;
cpl = inet_csk_ca(sk);
sampleRTT = sample->rtt_us;
//printk(KERN_INFO "pkt sample rtt %d us\n", sampleRTT);
}
EXPORT_SYMBOL_GPL(tcp_ccp_pkts_acked);
/*
* Detect drops.
*
* TCP_CA_Loss -> a timeout happened
* TCP_CA_Recovery -> an isolated loss (3x dupack) happened.
* TCP_CA_CWR -> got an ECN
*/
void tcp_ccp_set_state(struct sock *sk, u8 new_state) {
struct ccp *cpl = inet_csk_ca(sk);
switch (new_state) {
case TCP_CA_Loss:
printk(KERN_INFO "entered TCP_CA_Loss (timeout drop)\n");
if (cpl->dp != NULL) {
cpl->dp->prims.was_timeout = true;
}
ccp_invoke(cpl->dp);
return;
case TCP_CA_Recovery:
printk(KERN_INFO "entered TCP_CA_Recovery (dupack drop)\n");
break;
case TCP_CA_CWR:
printk(KERN_INFO "entered TCP_CA_CWR (ecn drop)\n");
break;
default:
printk(KERN_INFO "entered TCP normal state\n");
break;
}
if (cpl->dp != NULL) {
cpl->dp->prims.was_timeout = false;
}
}
EXPORT_SYMBOL_GPL(tcp_ccp_set_state);
void tcp_ccp_init(struct sock *sk) {
struct ccp *cpl;
struct tcp_sock *tp = tcp_sk(sk);
struct ccp_datapath_info dp = {
.init_cwnd = tp->snd_cwnd * tp->mss_cache,
.mss = tp->mss_cache,
.src_ip = tp->inet_conn.icsk_inet.inet_rcv_saddr,
.src_port = tp->inet_conn.icsk_inet.inet_num,
.dst_ip = tp->inet_conn.icsk_inet.inet_saddr,
.dst_port = tp->inet_conn.icsk_inet.inet_dport,
.congAlg = "reno",
};
pr_info("ccp: new flow\n");
cpl = inet_csk_ca(sk);
cpl->last_snd_una = tp->snd_una;
cpl->last_bytes_acked = tp->bytes_acked;
cpl->last_sacked_out = tp->sacked_out;
cpl->skb_array = (struct skb_info*)__MALLOC__(MAX_SKB_STORED * sizeof(struct skb_info));
if (!(cpl->skb_array)) {
pr_info("ccp: could not allocate skb array\n");
}
memset(cpl->skb_array, 0, MAX_SKB_STORED * sizeof(struct skb_info));
cpl->dp = ccp_connection_start((void *) sk, &dp);
if (cpl->dp == NULL) {
pr_info("ccp: start connection failed\n");
} else {
pr_info("ccp: starting connection %d", cpl->dp->index);
}
// if no ecn support
if (!(tp->ecn_flags & TCP_ECN_OK)) {
INET_ECN_dontxmit(sk);
}
cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
}
EXPORT_SYMBOL_GPL(tcp_ccp_init);
void tcp_ccp_release(struct sock *sk) {
struct ccp *cpl = inet_csk_ca(sk);
if (cpl->dp != NULL) {
pr_info("ccp: freeing connection %d", cpl->dp->index);
ccp_connection_free(cpl->dp->index);
} else {
pr_info("ccp: already freed");
}
if (cpl->skb_array != NULL) {
kfree(cpl->skb_array);
cpl->skb_array = NULL;
}
}
EXPORT_SYMBOL_GPL(tcp_ccp_release);
struct tcp_congestion_ops tcp_ccp_congestion_ops = {
.flags = TCP_CONG_NEEDS_ECN,
.in_ack_event = tcp_ccp_in_ack_event,
.name = "ccp",
.owner = THIS_MODULE,
.init = tcp_ccp_init,
.release = tcp_ccp_release,
.ssthresh = tcp_ccp_ssthresh,
//.cong_avoid = tcp_ccp_cong_avoid,
.cong_control = tcp_ccp_cong_control,
.undo_cwnd = tcp_ccp_undo_cwnd,
.set_state = tcp_ccp_set_state,
.pkts_acked = tcp_ccp_pkts_acked
};
static int __init tcp_ccp_register(void) {
int ok;
struct ccp_datapath dp = {
.set_cwnd = &do_set_cwnd,
.set_rate_abs = &do_set_rate_abs,
.set_rate_rel = &do_set_rate_rel,
.now = &ccp_now,
.since_usecs = &ccp_since,
.after_usecs = &ccp_after
};
getnstimeofday64(&tzero);
#ifdef COMPAT_MODE
pr_info("[ccp] Compatibility mode: 4.13 <= kernel version <= 4.16\n");
#endif
#ifdef RATESAMPLE_MODE
pr_info("[ccp] Rate-sample mode: 4.19 <= kernel version\n");
#endif
#if __IPC__ == IPC_NETLINK
ok = ccp_nl_sk(&ccp_read_msg);
if (ok < 0) {
return -1;
}
dp.send_msg = &nl_sendmsg;
printk(KERN_INFO "[ccp] ipc = netlink\n");
#elif __IPC__ == IPC_CHARDEV
ok = ccpkp_init(&ccp_read_msg);
if (ok < 0) {
return -1;
}
dp.send_msg = &ccpkp_sendmsg;
printk(KERN_INFO "[ccp] ipc = chardev\n");
#else
printk(KERN_WARNING "[ccp] ipc = %s unknown\n", __IPC__);
return -1;
#endif
ok = ccp_init(&dp);
if (ok < 0) {
return -1;
}
printk(KERN_INFO "[ccp] init: size %lu\n", sizeof(struct ccp));
return tcp_register_congestion_control(&tcp_ccp_congestion_ops);
}
static void __exit tcp_ccp_unregister(void) {
printk(KERN_INFO "[ccp] exit\n");
ccp_free();
tcp_unregister_congestion_control(&tcp_ccp_congestion_ops);
#if __IPC__ == IPC_NETLINK
free_ccp_nl_sk();
#elif __IPC__ == IPC_CHARDEV
ccpkp_cleanup();
#endif
}
module_init(tcp_ccp_register);
module_exit(tcp_ccp_unregister);
MODULE_AUTHOR("Akshay Narayan <[email protected]>");
MODULE_DESCRIPTION("Kernel datapath for a congestion control plane");
MODULE_LICENSE("GPL");