1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright Red Hat Inc. 2017
5 * This file is part of the SCTP kernel implementation
7 * These functions manipulate sctp stream queue/scheduling.
9 * Please send any bug reports or fixes you make to the
10 * email addresched(es):
11 * lksctp developers <linux-sctp@vger.kernel.org>
13 * Written or modified by:
14 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
17 #include <linux/list.h>
18 #include <net/sctp/sctp.h>
19 #include <net/sctp/sm.h>
20 #include <net/sctp/stream_sched.h>
23 * RFC DRAFT ndata section 3.4
26 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream);
28 static struct sctp_stream_priorities *sctp_sched_prio_head_get(struct sctp_stream_priorities *p)
34 static void sctp_sched_prio_head_put(struct sctp_stream_priorities *p)
36 if (p && --p->users == 0)
40 static struct sctp_stream_priorities *sctp_sched_prio_new_head(
41 struct sctp_stream *stream, int prio, gfp_t gfp)
43 struct sctp_stream_priorities *p;
45 p = kmalloc(sizeof(*p), gfp);
49 INIT_LIST_HEAD(&p->prio_sched);
50 INIT_LIST_HEAD(&p->active);
58 static struct sctp_stream_priorities *sctp_sched_prio_get_head(
59 struct sctp_stream *stream, int prio, gfp_t gfp)
61 struct sctp_stream_priorities *p;
64 /* Look into scheduled priorities first, as they are sorted and
65 * we can find it fast IF it's scheduled.
67 list_for_each_entry(p, &stream->prio_list, prio_sched) {
69 return sctp_sched_prio_head_get(p);
74 /* No luck. So we search on all streams now. */
75 for (i = 0; i < stream->outcnt; i++) {
76 if (!SCTP_SO(stream, i)->ext)
79 p = SCTP_SO(stream, i)->ext->prio_head;
81 /* Means all other streams won't be initialized
86 return sctp_sched_prio_head_get(p);
89 /* If not even there, allocate a new one. */
90 return sctp_sched_prio_new_head(stream, prio, gfp);
93 static void sctp_sched_prio_next_stream(struct sctp_stream_priorities *p)
95 struct list_head *pos;
97 pos = p->next->prio_list.next;
98 if (pos == &p->active)
100 p->next = list_entry(pos, struct sctp_stream_out_ext, prio_list);
103 static bool sctp_sched_prio_unsched(struct sctp_stream_out_ext *soute)
105 bool scheduled = false;
107 if (!list_empty(&soute->prio_list)) {
108 struct sctp_stream_priorities *prio_head = soute->prio_head;
113 if (prio_head->next == soute)
114 /* Try to move to the next stream */
115 sctp_sched_prio_next_stream(prio_head);
117 list_del_init(&soute->prio_list);
119 /* Also unsched the priority if this was the last stream */
120 if (list_empty(&prio_head->active)) {
121 list_del_init(&prio_head->prio_sched);
122 /* If there is no stream left, clear next */
123 prio_head->next = NULL;
130 static void sctp_sched_prio_sched(struct sctp_stream *stream,
131 struct sctp_stream_out_ext *soute)
133 struct sctp_stream_priorities *prio, *prio_head;
135 prio_head = soute->prio_head;
137 /* Nothing to do if already scheduled */
138 if (!list_empty(&soute->prio_list))
141 /* Schedule the stream. If there is a next, we schedule the new
142 * one before it, so it's the last in round robin order.
143 * If there isn't, we also have to schedule the priority.
145 if (prio_head->next) {
146 list_add(&soute->prio_list, prio_head->next->prio_list.prev);
150 list_add(&soute->prio_list, &prio_head->active);
151 prio_head->next = soute;
153 list_for_each_entry(prio, &stream->prio_list, prio_sched) {
154 if (prio->prio > prio_head->prio) {
155 list_add(&prio_head->prio_sched, prio->prio_sched.prev);
160 list_add_tail(&prio_head->prio_sched, &stream->prio_list);
163 static int sctp_sched_prio_set(struct sctp_stream *stream, __u16 sid,
164 __u16 prio, gfp_t gfp)
166 struct sctp_stream_out *sout = SCTP_SO(stream, sid);
167 struct sctp_stream_out_ext *soute = sout->ext;
168 struct sctp_stream_priorities *prio_head, *old;
169 bool reschedule = false;
171 old = soute->prio_head;
172 if (old && old->prio == prio)
175 prio_head = sctp_sched_prio_get_head(stream, prio, gfp);
179 reschedule = sctp_sched_prio_unsched(soute);
180 soute->prio_head = prio_head;
182 sctp_sched_prio_sched(stream, soute);
184 sctp_sched_prio_head_put(old);
188 static int sctp_sched_prio_get(struct sctp_stream *stream, __u16 sid,
191 *value = SCTP_SO(stream, sid)->ext->prio_head->prio;
195 static int sctp_sched_prio_init(struct sctp_stream *stream)
197 INIT_LIST_HEAD(&stream->prio_list);
202 static int sctp_sched_prio_init_sid(struct sctp_stream *stream, __u16 sid,
205 INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->prio_list);
206 return sctp_sched_prio_set(stream, sid, 0, gfp);
209 static void sctp_sched_prio_free_sid(struct sctp_stream *stream, __u16 sid)
211 sctp_sched_prio_head_put(SCTP_SO(stream, sid)->ext->prio_head);
212 SCTP_SO(stream, sid)->ext->prio_head = NULL;
215 static void sctp_sched_prio_enqueue(struct sctp_outq *q,
216 struct sctp_datamsg *msg)
218 struct sctp_stream *stream;
219 struct sctp_chunk *ch;
222 ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
223 sid = sctp_chunk_stream_no(ch);
224 stream = &q->asoc->stream;
225 sctp_sched_prio_sched(stream, SCTP_SO(stream, sid)->ext);
228 static struct sctp_chunk *sctp_sched_prio_dequeue(struct sctp_outq *q)
230 struct sctp_stream *stream = &q->asoc->stream;
231 struct sctp_stream_priorities *prio;
232 struct sctp_stream_out_ext *soute;
233 struct sctp_chunk *ch = NULL;
235 /* Bail out quickly if queue is empty */
236 if (list_empty(&q->out_chunk_list))
239 /* Find which chunk is next. It's easy, it's either the current
240 * one or the first chunk on the next active stream.
242 if (stream->out_curr) {
243 soute = stream->out_curr->ext;
245 prio = list_entry(stream->prio_list.next,
246 struct sctp_stream_priorities, prio_sched);
249 ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
250 sctp_sched_dequeue_common(q, ch);
256 static void sctp_sched_prio_dequeue_done(struct sctp_outq *q,
257 struct sctp_chunk *ch)
259 struct sctp_stream_priorities *prio;
260 struct sctp_stream_out_ext *soute;
263 /* Last chunk on that msg, move to the next stream on
266 sid = sctp_chunk_stream_no(ch);
267 soute = SCTP_SO(&q->asoc->stream, sid)->ext;
268 prio = soute->prio_head;
270 sctp_sched_prio_next_stream(prio);
272 if (list_empty(&soute->outq))
273 sctp_sched_prio_unsched(soute);
276 static void sctp_sched_prio_sched_all(struct sctp_stream *stream)
278 struct sctp_association *asoc;
279 struct sctp_stream_out *sout;
280 struct sctp_chunk *ch;
282 asoc = container_of(stream, struct sctp_association, stream);
283 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
286 sid = sctp_chunk_stream_no(ch);
287 sout = SCTP_SO(stream, sid);
289 sctp_sched_prio_sched(stream, sout->ext);
293 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream)
295 struct sctp_stream_priorities *p, *tmp;
296 struct sctp_stream_out_ext *soute, *souttmp;
298 list_for_each_entry_safe(p, tmp, &stream->prio_list, prio_sched)
299 list_for_each_entry_safe(soute, souttmp, &p->active, prio_list)
300 sctp_sched_prio_unsched(soute);
303 static struct sctp_sched_ops sctp_sched_prio = {
304 .set = sctp_sched_prio_set,
305 .get = sctp_sched_prio_get,
306 .init = sctp_sched_prio_init,
307 .init_sid = sctp_sched_prio_init_sid,
308 .free_sid = sctp_sched_prio_free_sid,
309 .enqueue = sctp_sched_prio_enqueue,
310 .dequeue = sctp_sched_prio_dequeue,
311 .dequeue_done = sctp_sched_prio_dequeue_done,
312 .sched_all = sctp_sched_prio_sched_all,
313 .unsched_all = sctp_sched_prio_unsched_all,
316 void sctp_sched_ops_prio_init(void)
318 sctp_sched_ops_register(SCTP_SS_PRIO, &sctp_sched_prio);