Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sctp: add fair capacity stream scheduler

As it says in rfc8260#section-3.5 about the fair capacity scheduler:

A fair capacity distribution between the streams is used. This
scheduler considers the lengths of the messages of each stream and
schedules them in a specific way to maintain an equal capacity for
all streams. The details are implementation dependent. interleaving
user messages allows for a better realization of the fair capacity
usage.

This patch adds Fair Capacity Scheduler based on the foundations added
by commit 5bbbbe32a431 ("sctp: introduce stream scheduler foundations"):

A fc_list and a fc_length are added into struct sctp_stream_out_ext and
a fc_list is added into struct sctp_stream. In .enqueue, when there are
chunks enqueued into a stream, this stream will be linked into stream->
fc_list by its fc_list ordered by its fc_length. In .dequeue, it always
picks up the 1st skb from stream->fc_list. In .dequeue_done, fc_length
is increased by chunk's len and update its location in stream->fc_list
according to the its new fc_length.

Note that when the new fc_length overflows in .dequeue_done, instead of
resetting all fc_lengths to 0, we only reduced them by U32_MAX / 4 to
avoid a moment of imbalance in the scheduling, as Marcelo suggested.

Signed-off-by: Xin Long <lucien.xin@gmail.com>
Acked-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Xin Long and committed by
Paolo Abeni
4821a076 46ca833c

+196 -2
+1
include/net/sctp/stream_sched.h
··· 58 58 struct sctp_sched_ops *sched_ops); 59 59 void sctp_sched_ops_prio_init(void); 60 60 void sctp_sched_ops_rr_init(void); 61 + void sctp_sched_ops_fc_init(void); 61 62 62 63 #endif /* __sctp_stream_sched_h__ */
+7
include/net/sctp/structs.h
··· 1429 1429 struct { 1430 1430 struct list_head rr_list; 1431 1431 }; 1432 + struct { 1433 + struct list_head fc_list; 1434 + __u32 fc_length; 1435 + }; 1432 1436 }; 1433 1437 }; 1434 1438 ··· 1478 1474 struct list_head rr_list; 1479 1475 /* The next stream in line */ 1480 1476 struct sctp_stream_out_ext *rr_next; 1477 + }; 1478 + struct { 1479 + struct list_head fc_list; 1481 1480 }; 1482 1481 }; 1483 1482 struct sctp_stream_interleave *si;
+2 -1
include/uapi/linux/sctp.h
··· 1211 1211 SCTP_SS_DEFAULT = SCTP_SS_FCFS, 1212 1212 SCTP_SS_PRIO, 1213 1213 SCTP_SS_RR, 1214 - SCTP_SS_MAX = SCTP_SS_RR 1214 + SCTP_SS_FC, 1215 + SCTP_SS_MAX = SCTP_SS_FC 1215 1216 }; 1216 1217 1217 1218 /* Probe Interval socket option */
+2 -1
net/sctp/Makefile
··· 13 13 tsnmap.o bind_addr.o socket.o primitive.o \ 14 14 output.o input.o debug.o stream.o auth.o \ 15 15 offload.o stream_sched.o stream_sched_prio.o \ 16 - stream_sched_rr.o stream_interleave.o 16 + stream_sched_rr.o stream_sched_fc.o \ 17 + stream_interleave.o 17 18 18 19 sctp_diag-y := diag.o 19 20
+1
net/sctp/stream_sched.c
··· 124 124 sctp_sched_ops_fcfs_init(); 125 125 sctp_sched_ops_prio_init(); 126 126 sctp_sched_ops_rr_init(); 127 + sctp_sched_ops_fc_init(); 127 128 } 128 129 129 130 static void sctp_sched_free_sched(struct sctp_stream *stream)
+183
net/sctp/stream_sched_fc.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* SCTP kernel implementation 3 + * (C) Copyright Red Hat Inc. 2022 4 + * 5 + * This file is part of the SCTP kernel implementation 6 + * 7 + * These functions manipulate sctp stream queue/scheduling. 8 + * 9 + * Please send any bug reports or fixes you make to the 10 + * email addresched(es): 11 + * lksctp developers <linux-sctp@vger.kernel.org> 12 + * 13 + * Written or modified by: 14 + * Xin Long <lucien.xin@gmail.com> 15 + */ 16 + 17 + #include <linux/list.h> 18 + #include <net/sctp/sctp.h> 19 + #include <net/sctp/sm.h> 20 + #include <net/sctp/stream_sched.h> 21 + 22 + /* Fair Capacity handling 23 + * RFC 8260 section 3.5 24 + */ 25 + static void sctp_sched_fc_unsched_all(struct sctp_stream *stream); 26 + 27 + static int sctp_sched_fc_set(struct sctp_stream *stream, __u16 sid, 28 + __u16 weight, gfp_t gfp) 29 + { 30 + return 0; 31 + } 32 + 33 + static int sctp_sched_fc_get(struct sctp_stream *stream, __u16 sid, 34 + __u16 *value) 35 + { 36 + return 0; 37 + } 38 + 39 + static int sctp_sched_fc_init(struct sctp_stream *stream) 40 + { 41 + INIT_LIST_HEAD(&stream->fc_list); 42 + 43 + return 0; 44 + } 45 + 46 + static int sctp_sched_fc_init_sid(struct sctp_stream *stream, __u16 sid, 47 + gfp_t gfp) 48 + { 49 + struct sctp_stream_out_ext *soute = SCTP_SO(stream, sid)->ext; 50 + 51 + INIT_LIST_HEAD(&soute->fc_list); 52 + soute->fc_length = 0; 53 + 54 + return 0; 55 + } 56 + 57 + static void sctp_sched_fc_free_sid(struct sctp_stream *stream, __u16 sid) 58 + { 59 + } 60 + 61 + static void sctp_sched_fc_sched(struct sctp_stream *stream, 62 + struct sctp_stream_out_ext *soute) 63 + { 64 + struct sctp_stream_out_ext *pos; 65 + 66 + if (!list_empty(&soute->fc_list)) 67 + return; 68 + 69 + list_for_each_entry(pos, &stream->fc_list, fc_list) 70 + if (pos->fc_length >= soute->fc_length) 71 + break; 72 + list_add_tail(&soute->fc_list, &pos->fc_list); 73 + } 74 + 75 + static void sctp_sched_fc_enqueue(struct sctp_outq *q, 76 + struct sctp_datamsg *msg) 77 + { 78 + struct sctp_stream *stream; 79 + struct sctp_chunk *ch; 80 + __u16 sid; 81 + 82 + ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list); 83 + sid = sctp_chunk_stream_no(ch); 84 + stream = &q->asoc->stream; 85 + sctp_sched_fc_sched(stream, SCTP_SO(stream, sid)->ext); 86 + } 87 + 88 + static struct sctp_chunk *sctp_sched_fc_dequeue(struct sctp_outq *q) 89 + { 90 + struct sctp_stream *stream = &q->asoc->stream; 91 + struct sctp_stream_out_ext *soute; 92 + struct sctp_chunk *ch; 93 + 94 + /* Bail out quickly if queue is empty */ 95 + if (list_empty(&q->out_chunk_list)) 96 + return NULL; 97 + 98 + /* Find which chunk is next */ 99 + if (stream->out_curr) 100 + soute = stream->out_curr->ext; 101 + else 102 + soute = list_entry(stream->fc_list.next, struct sctp_stream_out_ext, fc_list); 103 + ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list); 104 + 105 + sctp_sched_dequeue_common(q, ch); 106 + return ch; 107 + } 108 + 109 + static void sctp_sched_fc_dequeue_done(struct sctp_outq *q, 110 + struct sctp_chunk *ch) 111 + { 112 + struct sctp_stream *stream = &q->asoc->stream; 113 + struct sctp_stream_out_ext *soute, *pos; 114 + __u16 sid, i; 115 + 116 + sid = sctp_chunk_stream_no(ch); 117 + soute = SCTP_SO(stream, sid)->ext; 118 + /* reduce all fc_lengths by U32_MAX / 4 if the current fc_length overflows. */ 119 + if (soute->fc_length > U32_MAX - ch->skb->len) { 120 + for (i = 0; i < stream->outcnt; i++) { 121 + pos = SCTP_SO(stream, i)->ext; 122 + if (!pos) 123 + continue; 124 + if (pos->fc_length <= (U32_MAX >> 2)) { 125 + pos->fc_length = 0; 126 + continue; 127 + } 128 + pos->fc_length -= (U32_MAX >> 2); 129 + } 130 + } 131 + soute->fc_length += ch->skb->len; 132 + 133 + if (list_empty(&soute->outq)) { 134 + list_del_init(&soute->fc_list); 135 + return; 136 + } 137 + 138 + pos = soute; 139 + list_for_each_entry_continue(pos, &stream->fc_list, fc_list) 140 + if (pos->fc_length >= soute->fc_length) 141 + break; 142 + list_move_tail(&soute->fc_list, &pos->fc_list); 143 + } 144 + 145 + static void sctp_sched_fc_sched_all(struct sctp_stream *stream) 146 + { 147 + struct sctp_association *asoc; 148 + struct sctp_chunk *ch; 149 + 150 + asoc = container_of(stream, struct sctp_association, stream); 151 + list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) { 152 + __u16 sid = sctp_chunk_stream_no(ch); 153 + 154 + if (SCTP_SO(stream, sid)->ext) 155 + sctp_sched_fc_sched(stream, SCTP_SO(stream, sid)->ext); 156 + } 157 + } 158 + 159 + static void sctp_sched_fc_unsched_all(struct sctp_stream *stream) 160 + { 161 + struct sctp_stream_out_ext *soute, *tmp; 162 + 163 + list_for_each_entry_safe(soute, tmp, &stream->fc_list, fc_list) 164 + list_del_init(&soute->fc_list); 165 + } 166 + 167 + static struct sctp_sched_ops sctp_sched_fc = { 168 + .set = sctp_sched_fc_set, 169 + .get = sctp_sched_fc_get, 170 + .init = sctp_sched_fc_init, 171 + .init_sid = sctp_sched_fc_init_sid, 172 + .free_sid = sctp_sched_fc_free_sid, 173 + .enqueue = sctp_sched_fc_enqueue, 174 + .dequeue = sctp_sched_fc_dequeue, 175 + .dequeue_done = sctp_sched_fc_dequeue_done, 176 + .sched_all = sctp_sched_fc_sched_all, 177 + .unsched_all = sctp_sched_fc_unsched_all, 178 + }; 179 + 180 + void sctp_sched_ops_fc_init(void) 181 + { 182 + sctp_sched_ops_register(SCTP_SS_FC, &sctp_sched_fc); 183 + }