1 /* SCTP kernel implementation
2 * (C) Copyright Red Hat Inc. 2017
4 * This file is part of the SCTP kernel implementation
6 * These functions manipulate sctp stream queue/scheduling.
8 * This SCTP implementation is free software;
9 * you can redistribute it and/or modify it under the terms of
10 * the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This SCTP implementation is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * ************************
17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 * See the GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GNU CC; see the file COPYING. If not, see
22 * <http://www.gnu.org/licenses/>.
24 * Please send any bug reports or fixes you make to the
25 * email addresched(es):
28 * Written or modified by:
32 #include <linux/list.h>
33 #include <net/sctp/sctp.h>
34 #include <net/sctp/sm.h>
35 #include <net/sctp/stream_sched.h>
37 /* First Come First Serve (a.k.a. FIFO)
38 * RFC DRAFT ndata Section 3.1
40 static int sctp_sched_fcfs_set(struct sctp_stream *stream, __u16 sid,
41 __u16 value, gfp_t gfp)
46 static int sctp_sched_fcfs_get(struct sctp_stream *stream, __u16 sid,
53 static int sctp_sched_fcfs_init(struct sctp_stream *stream)
58 static int sctp_sched_fcfs_init_sid(struct sctp_stream *stream, __u16 sid,
64 static void sctp_sched_fcfs_free(struct sctp_stream *stream)
68 static void sctp_sched_fcfs_enqueue(struct sctp_outq *q,
69 struct sctp_datamsg *msg)
73 static struct sctp_chunk *sctp_sched_fcfs_dequeue(struct sctp_outq *q)
75 struct sctp_stream *stream = &q->asoc->stream;
76 struct sctp_chunk *ch = NULL;
77 struct list_head *entry;
79 if (list_empty(&q->out_chunk_list))
82 if (stream->out_curr) {
83 ch = list_entry(stream->out_curr->ext->outq.next,
84 struct sctp_chunk, stream_list);
86 entry = q->out_chunk_list.next;
87 ch = list_entry(entry, struct sctp_chunk, list);
90 sctp_sched_dequeue_common(q, ch);
96 static void sctp_sched_fcfs_dequeue_done(struct sctp_outq *q,
97 struct sctp_chunk *chunk)
101 static void sctp_sched_fcfs_sched_all(struct sctp_stream *stream)
105 static void sctp_sched_fcfs_unsched_all(struct sctp_stream *stream)
109 static struct sctp_sched_ops sctp_sched_fcfs = {
110 .set = sctp_sched_fcfs_set,
111 .get = sctp_sched_fcfs_get,
112 .init = sctp_sched_fcfs_init,
113 .init_sid = sctp_sched_fcfs_init_sid,
114 .free = sctp_sched_fcfs_free,
115 .enqueue = sctp_sched_fcfs_enqueue,
116 .dequeue = sctp_sched_fcfs_dequeue,
117 .dequeue_done = sctp_sched_fcfs_dequeue_done,
118 .sched_all = sctp_sched_fcfs_sched_all,
119 .unsched_all = sctp_sched_fcfs_unsched_all,
122 static void sctp_sched_ops_fcfs_init(void)
124 sctp_sched_ops_register(SCTP_SS_FCFS, &sctp_sched_fcfs);
127 /* API to other parts of the stack */
129 static struct sctp_sched_ops *sctp_sched_ops[SCTP_SS_MAX + 1];
131 void sctp_sched_ops_register(enum sctp_sched_type sched,
132 struct sctp_sched_ops *sched_ops)
134 sctp_sched_ops[sched] = sched_ops;
137 void sctp_sched_ops_init(void)
139 sctp_sched_ops_fcfs_init();
140 sctp_sched_ops_prio_init();
141 sctp_sched_ops_rr_init();
144 int sctp_sched_set_sched(struct sctp_association *asoc,
145 enum sctp_sched_type sched)
147 struct sctp_sched_ops *n = sctp_sched_ops[sched];
148 struct sctp_sched_ops *old = asoc->outqueue.sched;
149 struct sctp_datamsg *msg = NULL;
150 struct sctp_chunk *ch;
156 if (sched > SCTP_SS_MAX)
160 old->free(&asoc->stream);
162 /* Give the next scheduler a clean slate. */
163 for (i = 0; i < asoc->stream.outcnt; i++) {
164 void *p = SCTP_SO(&asoc->stream, i)->ext;
169 p += offsetofend(struct sctp_stream_out_ext, outq);
170 memset(p, 0, sizeof(struct sctp_stream_out_ext) -
171 offsetofend(struct sctp_stream_out_ext, outq));
175 asoc->outqueue.sched = n;
176 n->init(&asoc->stream);
177 for (i = 0; i < asoc->stream.outcnt; i++) {
178 if (!SCTP_SO(&asoc->stream, i)->ext)
181 ret = n->init_sid(&asoc->stream, i, GFP_KERNEL);
186 /* We have to requeue all chunks already queued. */
187 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
191 n->enqueue(&asoc->outqueue, msg);
197 n->free(&asoc->stream);
198 asoc->outqueue.sched = &sctp_sched_fcfs; /* Always safe */
203 int sctp_sched_get_sched(struct sctp_association *asoc)
207 for (i = 0; i <= SCTP_SS_MAX; i++)
208 if (asoc->outqueue.sched == sctp_sched_ops[i])
214 int sctp_sched_set_value(struct sctp_association *asoc, __u16 sid,
215 __u16 value, gfp_t gfp)
217 if (sid >= asoc->stream.outcnt)
220 if (!SCTP_SO(&asoc->stream, sid)->ext) {
223 ret = sctp_stream_init_ext(&asoc->stream, sid);
228 return asoc->outqueue.sched->set(&asoc->stream, sid, value, gfp);
231 int sctp_sched_get_value(struct sctp_association *asoc, __u16 sid,
234 if (sid >= asoc->stream.outcnt)
237 if (!SCTP_SO(&asoc->stream, sid)->ext)
240 return asoc->outqueue.sched->get(&asoc->stream, sid, value);
243 void sctp_sched_dequeue_done(struct sctp_outq *q, struct sctp_chunk *ch)
245 if (!list_is_last(&ch->frag_list, &ch->msg->chunks) &&
246 !q->asoc->intl_enable) {
247 struct sctp_stream_out *sout;
250 /* datamsg is not finish, so save it as current one,
251 * in case application switch scheduler or a higher
252 * priority stream comes in.
254 sid = sctp_chunk_stream_no(ch);
255 sout = SCTP_SO(&q->asoc->stream, sid);
256 q->asoc->stream.out_curr = sout;
260 q->asoc->stream.out_curr = NULL;
261 q->sched->dequeue_done(q, ch);
264 /* Auxiliary functions for the schedulers */
265 void sctp_sched_dequeue_common(struct sctp_outq *q, struct sctp_chunk *ch)
267 list_del_init(&ch->list);
268 list_del_init(&ch->stream_list);
269 q->out_qlen -= ch->skb->len;
272 int sctp_sched_init_sid(struct sctp_stream *stream, __u16 sid, gfp_t gfp)
274 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
275 struct sctp_stream_out_ext *ext = SCTP_SO(stream, sid)->ext;
277 INIT_LIST_HEAD(&ext->outq);
278 return sched->init_sid(stream, sid, gfp);
281 struct sctp_sched_ops *sctp_sched_ops_from_stream(struct sctp_stream *stream)
283 struct sctp_association *asoc;
285 asoc = container_of(stream, struct sctp_association, stream);
287 return asoc->outqueue.sched;