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
470
|
/* MPTCP Scheduler module selector. Highly inspired by tcp_cong.c */
#include <linux/module.h>
#include <net/mptcp.h>
static DEFINE_SPINLOCK(mptcp_sched_list_lock);
static LIST_HEAD(mptcp_sched_list);
struct defsched_priv {
u32 last_rbuf_opti;
};
static struct defsched_priv *defsched_get_priv(const struct tcp_sock *tp)
{
return (struct defsched_priv *)&tp->mptcp->mptcp_sched[0];
}
/* If the sub-socket sk available to send the skb? */
static bool mptcp_is_available(struct sock *sk, struct sk_buff *skb,
bool zero_wnd_test)
{
struct tcp_sock *tp = tcp_sk(sk);
unsigned int mss_now;
/* Set of states for which we are allowed to send data */
if (!mptcp_sk_can_send(sk))
return false;
/* We do not send data on this subflow unless it is
* fully established, i.e. the 4th ack has been received.
*/
if (tp->mptcp->pre_established)
return false;
if (tp->pf)
return false;
if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss) {
/* If SACK is disabled, and we got a loss, TCP does not exit
* the loss-state until something above high_seq has been acked.
* (see tcp_try_undo_recovery)
*
* high_seq is the snd_nxt at the moment of the RTO. As soon
* as we have an RTO, we won't push data on the subflow.
* Thus, snd_una can never go beyond high_seq.
*/
if (!tcp_is_reno(tp))
return false;
else if (tp->snd_una != tp->high_seq)
return false;
}
if (!tp->mptcp->fully_established) {
/* Make sure that we send in-order data */
if (skb && tp->mptcp->second_packet &&
tp->mptcp->last_end_data_seq != TCP_SKB_CB(skb)->seq)
return false;
}
if (!tcp_cwnd_test(tp, skb))
return false;
mss_now = tcp_current_mss(sk);
/* Don't send on this subflow if we bypass the allowed send-window at
* the per-subflow level. Similar to tcp_snd_wnd_test, but manually
* calculated end_seq (because here at this point end_seq is still at
* the meta-level).
*/
if (skb && after(tp->write_seq + min(skb->len, mss_now), tcp_wnd_end(tp)))
return false;
return true;
}
/* Are we not allowed to reinject this skb on tp? */
static int mptcp_dont_reinject_skb(struct tcp_sock *tp, struct sk_buff *skb)
{
/* If the skb has already been enqueued in this sk, try to find
* another one.
*/
return skb &&
/* Has the skb already been enqueued into this subsocket? */
mptcp_pi_to_flag(tp->mptcp->path_index) & TCP_SKB_CB(skb)->path_mask;
}
/* This is the scheduler. This function decides on which flow to send
* a given MSS. If all subflows are found to be busy, NULL is returned
* The flow is selected based on the shortest RTT.
* If all paths have full cong windows, we simply return NULL.
*
* Additionally, this function is aware of the backup-subflows.
*/
static struct sock *get_available_subflow(struct sock *meta_sk,
struct sk_buff *skb,
bool zero_wnd_test)
{
struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
struct sock *sk, *bestsk = NULL, *lowpriosk = NULL, *backupsk = NULL;
u32 min_time_to_peer = 0xffffffff, lowprio_min_time_to_peer = 0xffffffff;
int cnt_backups = 0;
/* if there is only one subflow, bypass the scheduling function */
if (mpcb->cnt_subflows == 1) {
bestsk = (struct sock *)mpcb->connection_list;
if (!mptcp_is_available(bestsk, skb, zero_wnd_test))
bestsk = NULL;
return bestsk;
}
/* Answer data_fin on same subflow!!! */
if (meta_sk->sk_shutdown & RCV_SHUTDOWN &&
skb && mptcp_is_data_fin(skb)) {
mptcp_for_each_sk(mpcb, sk) {
if (tcp_sk(sk)->mptcp->path_index == mpcb->dfin_path_index &&
mptcp_is_available(sk, skb, zero_wnd_test))
return sk;
}
}
/* First, find the best subflow */
mptcp_for_each_sk(mpcb, sk) {
struct tcp_sock *tp = tcp_sk(sk);
if (tp->mptcp->rcv_low_prio || tp->mptcp->low_prio)
cnt_backups++;
if ((tp->mptcp->rcv_low_prio || tp->mptcp->low_prio) &&
tp->srtt < lowprio_min_time_to_peer) {
if (!mptcp_is_available(sk, skb, zero_wnd_test))
continue;
if (mptcp_dont_reinject_skb(tp, skb)) {
backupsk = sk;
continue;
}
lowprio_min_time_to_peer = tp->srtt;
lowpriosk = sk;
} else if (!(tp->mptcp->rcv_low_prio || tp->mptcp->low_prio) &&
tp->srtt < min_time_to_peer) {
if (!mptcp_is_available(sk, skb, zero_wnd_test))
continue;
if (mptcp_dont_reinject_skb(tp, skb)) {
backupsk = sk;
continue;
}
min_time_to_peer = tp->srtt;
bestsk = sk;
}
}
if (mpcb->cnt_established == cnt_backups && lowpriosk) {
sk = lowpriosk;
} else if (bestsk) {
sk = bestsk;
} else if (backupsk) {
/* It has been sent on all subflows once - let's give it a
* chance again by restarting its pathmask.
*/
if (skb)
TCP_SKB_CB(skb)->path_mask = 0;
sk = backupsk;
}
return sk;
}
static struct sk_buff *mptcp_rcv_buf_optimization(struct sock *sk, int penal)
{
struct sock *meta_sk;
struct tcp_sock *tp = tcp_sk(sk), *tp_it;
struct sk_buff *skb_head;
struct defsched_priv *dsp = defsched_get_priv(tp);
if (tp->mpcb->cnt_subflows == 1)
return NULL;
meta_sk = mptcp_meta_sk(sk);
skb_head = tcp_write_queue_head(meta_sk);
if (!skb_head || skb_head == tcp_send_head(meta_sk))
return NULL;
/* If penalization is optional (coming from mptcp_next_segment() and
* We are not send-buffer-limited we do not penalize. The retransmission
* is just an optimization to fix the idle-time due to the delay before
* we wake up the application.
*/
if (!penal && sk_stream_memory_free(meta_sk))
goto retrans;
/* Half the cwnd of the slow flow */
if (tcp_time_stamp - dsp->last_rbuf_opti >= tp->srtt >> 3) {
mptcp_for_each_tp(tp->mpcb, tp_it) {
if (tp_it != tp &&
TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
if (tp->srtt < tp_it->srtt && inet_csk((struct sock *)tp_it)->icsk_ca_state == TCP_CA_Open) {
tp_it->snd_cwnd = max(tp_it->snd_cwnd >> 1U, 1U);
if (tp_it->snd_ssthresh != TCP_INFINITE_SSTHRESH)
tp_it->snd_ssthresh = max(tp_it->snd_ssthresh >> 1U, 2U);
dsp->last_rbuf_opti = tcp_time_stamp;
}
break;
}
}
}
retrans:
/* Segment not yet injected into this path? Take it!!! */
if (!(TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp->mptcp->path_index))) {
bool do_retrans = false;
mptcp_for_each_tp(tp->mpcb, tp_it) {
if (tp_it != tp &&
TCP_SKB_CB(skb_head)->path_mask & mptcp_pi_to_flag(tp_it->mptcp->path_index)) {
if (tp_it->snd_cwnd <= 4) {
do_retrans = true;
break;
}
if (4 * tp->srtt >= tp_it->srtt) {
do_retrans = false;
break;
} else {
do_retrans = true;
}
}
}
if (do_retrans)
return skb_head;
}
return NULL;
}
/* Returns the next segment to be sent from the mptcp meta-queue.
* (chooses the reinject queue if any segment is waiting in it, otherwise,
* chooses the normal write queue).
* Sets *@reinject to 1 if the returned segment comes from the
* reinject queue. Sets it to 0 if it is the regular send-head of the meta-sk,
* and sets it to -1 if it is a meta-level retransmission to optimize the
* receive-buffer.
*/
static struct sk_buff *__mptcp_next_segment(struct sock *meta_sk, int *reinject)
{
struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
struct sk_buff *skb = NULL;
*reinject = 0;
/* If we are in fallback-mode, just take from the meta-send-queue */
if (mpcb->infinite_mapping_snd || mpcb->send_infinite_mapping)
return tcp_send_head(meta_sk);
skb = skb_peek(&mpcb->reinject_queue);
if (skb) {
*reinject = 1;
} else {
skb = tcp_send_head(meta_sk);
if (!skb && meta_sk->sk_socket &&
test_bit(SOCK_NOSPACE, &meta_sk->sk_socket->flags) &&
sk_stream_wspace(meta_sk) < sk_stream_min_wspace(meta_sk)) {
struct sock *subsk = get_available_subflow(meta_sk, NULL,
false);
if (!subsk)
return NULL;
skb = mptcp_rcv_buf_optimization(subsk, 0);
if (skb)
*reinject = -1;
}
}
return skb;
}
static struct sk_buff *mptcp_next_segment(struct sock *meta_sk,
int *reinject,
struct sock **subsk,
unsigned int *limit)
{
struct sk_buff *skb = __mptcp_next_segment(meta_sk, reinject);
unsigned int mss_now;
struct tcp_sock *subtp;
u16 gso_max_segs;
u32 max_len, max_segs, window, needed;
/* As we set it, we have to reset it as well. */
*limit = 0;
if (!skb)
return NULL;
*subsk = get_available_subflow(meta_sk, skb, false);
if (!*subsk)
return NULL;
subtp = tcp_sk(*subsk);
mss_now = tcp_current_mss(*subsk);
if (!*reinject && unlikely(!tcp_snd_wnd_test(tcp_sk(meta_sk), skb, mss_now))) {
skb = mptcp_rcv_buf_optimization(*subsk, 1);
if (skb)
*reinject = -1;
else
return NULL;
}
/* No splitting required, as we will only send one single segment */
if (skb->len <= mss_now)
return skb;
/* The following is similar to tcp_mss_split_point, but
* we do not care about nagle, because we will anyways
* use TCP_NAGLE_PUSH, which overrides this.
*
* So, we first limit according to the cwnd/gso-size and then according
* to the subflow's window.
*/
gso_max_segs = (*subsk)->sk_gso_max_segs;
if (!gso_max_segs) /* No gso supported on the subflow's NIC */
gso_max_segs = 1;
max_segs = min_t(unsigned int, tcp_cwnd_test(subtp, skb), gso_max_segs);
if (!max_segs)
return NULL;
max_len = mss_now * max_segs;
window = tcp_wnd_end(subtp) - subtp->write_seq;
needed = min(skb->len, window);
if (max_len <= skb->len)
/* Take max_win, which is actually the cwnd/gso-size */
*limit = max_len;
else
/* Or, take the window */
*limit = needed;
return skb;
}
static void defsched_init(struct sock *sk)
{
struct defsched_priv *dsp = defsched_get_priv(tcp_sk(sk));
dsp->last_rbuf_opti = tcp_time_stamp;
}
struct mptcp_sched_ops mptcp_sched_default = {
.get_subflow = get_available_subflow,
.next_segment = mptcp_next_segment,
.init = defsched_init,
.name = "default",
.owner = THIS_MODULE,
};
static struct mptcp_sched_ops *mptcp_sched_find(const char *name)
{
struct mptcp_sched_ops *e;
list_for_each_entry_rcu(e, &mptcp_sched_list, list) {
if (strcmp(e->name, name) == 0)
return e;
}
return NULL;
}
int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
{
int ret = 0;
if (!sched->get_subflow || !sched->next_segment)
return -EINVAL;
spin_lock(&mptcp_sched_list_lock);
if (mptcp_sched_find(sched->name)) {
pr_notice("%s already registered\n", sched->name);
ret = -EEXIST;
} else {
list_add_tail_rcu(&sched->list, &mptcp_sched_list);
pr_info("%s registered\n", sched->name);
}
spin_unlock(&mptcp_sched_list_lock);
return ret;
}
EXPORT_SYMBOL_GPL(mptcp_register_scheduler);
void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
{
spin_lock(&mptcp_sched_list_lock);
list_del_rcu(&sched->list);
spin_unlock(&mptcp_sched_list_lock);
}
EXPORT_SYMBOL_GPL(mptcp_unregister_scheduler);
void mptcp_get_default_scheduler(char *name)
{
struct mptcp_sched_ops *sched;
BUG_ON(list_empty(&mptcp_sched_list));
rcu_read_lock();
sched = list_entry(mptcp_sched_list.next, struct mptcp_sched_ops, list);
strncpy(name, sched->name, MPTCP_SCHED_NAME_MAX);
rcu_read_unlock();
}
int mptcp_set_default_scheduler(const char *name)
{
struct mptcp_sched_ops *sched;
int ret = -ENOENT;
spin_lock(&mptcp_sched_list_lock);
sched = mptcp_sched_find(name);
#ifdef CONFIG_MODULES
if (!sched && capable(CAP_NET_ADMIN)) {
spin_unlock(&mptcp_sched_list_lock);
request_module("mptcp_%s", name);
spin_lock(&mptcp_sched_list_lock);
sched = mptcp_sched_find(name);
}
#endif
if (sched) {
list_move(&sched->list, &mptcp_sched_list);
ret = 0;
} else {
pr_info("%s is not available\n", name);
}
spin_unlock(&mptcp_sched_list_lock);
return ret;
}
void mptcp_init_scheduler(struct mptcp_cb *mpcb)
{
struct mptcp_sched_ops *sched;
rcu_read_lock();
list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
if (try_module_get(sched->owner)) {
mpcb->sched_ops = sched;
break;
}
}
rcu_read_unlock();
}
/* Manage refcounts on socket close. */
void mptcp_cleanup_scheduler(struct mptcp_cb *mpcb)
{
module_put(mpcb->sched_ops->owner);
}
/* Set default value from kernel configuration at bootup */
static int __init mptcp_scheduler_default(void)
{
BUILD_BUG_ON(sizeof(struct defsched_priv) > MPTCP_SCHED_SIZE);
return mptcp_set_default_scheduler(CONFIG_DEFAULT_MPTCP_SCHED);
}
late_initcall(mptcp_scheduler_default);
|