blob: 115ddb7651695cca7417cb63004a1a59c93523b8 [file] [log] [blame]
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -03001/* SCTP kernel implementation
2 * (C) Copyright Red Hat Inc. 2017
3 *
4 * This file is part of the SCTP kernel implementation
5 *
6 * These functions manipulate sctp stream queue/scheduling.
7 *
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)
12 * any later version.
13 *
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.
19 *
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/>.
23 *
24 * Please send any bug reports or fixes you make to the
25 * email addresched(es):
26 * lksctp developers <linux-sctp@vger.kernel.org>
27 *
28 * Written or modified by:
29 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
30 */
31
32#include <linux/list.h>
33#include <net/sctp/sctp.h>
34#include <net/sctp/sm.h>
35#include <net/sctp/stream_sched.h>
36
37/* First Come First Serve (a.k.a. FIFO)
38 * RFC DRAFT ndata Section 3.1
39 */
40static int sctp_sched_fcfs_set(struct sctp_stream *stream, __u16 sid,
41 __u16 value, gfp_t gfp)
42{
43 return 0;
44}
45
46static int sctp_sched_fcfs_get(struct sctp_stream *stream, __u16 sid,
47 __u16 *value)
48{
49 *value = 0;
50 return 0;
51}
52
53static int sctp_sched_fcfs_init(struct sctp_stream *stream)
54{
55 return 0;
56}
57
58static int sctp_sched_fcfs_init_sid(struct sctp_stream *stream, __u16 sid,
59 gfp_t gfp)
60{
61 return 0;
62}
63
64static void sctp_sched_fcfs_free(struct sctp_stream *stream)
65{
66}
67
68static void sctp_sched_fcfs_enqueue(struct sctp_outq *q,
69 struct sctp_datamsg *msg)
70{
71}
72
73static struct sctp_chunk *sctp_sched_fcfs_dequeue(struct sctp_outq *q)
74{
75 struct sctp_stream *stream = &q->asoc->stream;
76 struct sctp_chunk *ch = NULL;
77 struct list_head *entry;
78
79 if (list_empty(&q->out_chunk_list))
80 goto out;
81
82 if (stream->out_curr) {
83 ch = list_entry(stream->out_curr->ext->outq.next,
84 struct sctp_chunk, stream_list);
85 } else {
86 entry = q->out_chunk_list.next;
87 ch = list_entry(entry, struct sctp_chunk, list);
88 }
89
90 sctp_sched_dequeue_common(q, ch);
91
92out:
93 return ch;
94}
95
96static void sctp_sched_fcfs_dequeue_done(struct sctp_outq *q,
97 struct sctp_chunk *chunk)
98{
99}
100
101static void sctp_sched_fcfs_sched_all(struct sctp_stream *stream)
102{
103}
104
105static void sctp_sched_fcfs_unsched_all(struct sctp_stream *stream)
106{
107}
108
109static 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,
120};
121
122/* API to other parts of the stack */
123
Marcelo Ricardo Leitner637784a2017-10-03 19:20:16 -0300124extern struct sctp_sched_ops sctp_sched_prio;
125
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300126struct sctp_sched_ops *sctp_sched_ops[] = {
127 &sctp_sched_fcfs,
Marcelo Ricardo Leitner637784a2017-10-03 19:20:16 -0300128 &sctp_sched_prio,
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300129};
130
131int sctp_sched_set_sched(struct sctp_association *asoc,
132 enum sctp_sched_type sched)
133{
134 struct sctp_sched_ops *n = sctp_sched_ops[sched];
135 struct sctp_sched_ops *old = asoc->outqueue.sched;
136 struct sctp_datamsg *msg = NULL;
137 struct sctp_chunk *ch;
138 int i, ret = 0;
139
140 if (old == n)
141 return ret;
142
143 if (sched > SCTP_SS_MAX)
144 return -EINVAL;
145
146 if (old) {
147 old->free(&asoc->stream);
148
149 /* Give the next scheduler a clean slate. */
150 for (i = 0; i < asoc->stream.outcnt; i++) {
151 void *p = asoc->stream.out[i].ext;
152
153 if (!p)
154 continue;
155
156 p += offsetofend(struct sctp_stream_out_ext, outq);
157 memset(p, 0, sizeof(struct sctp_stream_out_ext) -
158 offsetofend(struct sctp_stream_out_ext, outq));
159 }
160 }
161
162 asoc->outqueue.sched = n;
163 n->init(&asoc->stream);
164 for (i = 0; i < asoc->stream.outcnt; i++) {
165 if (!asoc->stream.out[i].ext)
166 continue;
167
168 ret = n->init_sid(&asoc->stream, i, GFP_KERNEL);
169 if (ret)
170 goto err;
171 }
172
173 /* We have to requeue all chunks already queued. */
174 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
175 if (ch->msg == msg)
176 continue;
177 msg = ch->msg;
178 n->enqueue(&asoc->outqueue, msg);
179 }
180
181 return ret;
182
183err:
184 n->free(&asoc->stream);
185 asoc->outqueue.sched = &sctp_sched_fcfs; /* Always safe */
186
187 return ret;
188}
189
190int sctp_sched_get_sched(struct sctp_association *asoc)
191{
192 int i;
193
194 for (i = 0; i <= SCTP_SS_MAX; i++)
195 if (asoc->outqueue.sched == sctp_sched_ops[i])
196 return i;
197
198 return 0;
199}
200
201int sctp_sched_set_value(struct sctp_association *asoc, __u16 sid,
202 __u16 value, gfp_t gfp)
203{
204 if (sid >= asoc->stream.outcnt)
205 return -EINVAL;
206
207 if (!asoc->stream.out[sid].ext) {
208 int ret;
209
210 ret = sctp_stream_init_ext(&asoc->stream, sid);
211 if (ret)
212 return ret;
213 }
214
215 return asoc->outqueue.sched->set(&asoc->stream, sid, value, gfp);
216}
217
218int sctp_sched_get_value(struct sctp_association *asoc, __u16 sid,
219 __u16 *value)
220{
221 if (sid >= asoc->stream.outcnt)
222 return -EINVAL;
223
224 if (!asoc->stream.out[sid].ext)
225 return 0;
226
227 return asoc->outqueue.sched->get(&asoc->stream, sid, value);
228}
229
230void sctp_sched_dequeue_done(struct sctp_outq *q, struct sctp_chunk *ch)
231{
232 if (!list_is_last(&ch->frag_list, &ch->msg->chunks)) {
233 struct sctp_stream_out *sout;
234 __u16 sid;
235
236 /* datamsg is not finish, so save it as current one,
237 * in case application switch scheduler or a higher
238 * priority stream comes in.
239 */
240 sid = sctp_chunk_stream_no(ch);
241 sout = &q->asoc->stream.out[sid];
242 q->asoc->stream.out_curr = sout;
243 return;
244 }
245
246 q->asoc->stream.out_curr = NULL;
247 q->sched->dequeue_done(q, ch);
248}
249
250/* Auxiliary functions for the schedulers */
251void sctp_sched_dequeue_common(struct sctp_outq *q, struct sctp_chunk *ch)
252{
253 list_del_init(&ch->list);
254 list_del_init(&ch->stream_list);
255 q->out_qlen -= ch->skb->len;
256}
257
258int sctp_sched_init_sid(struct sctp_stream *stream, __u16 sid, gfp_t gfp)
259{
260 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
261
262 INIT_LIST_HEAD(&stream->out[sid].ext->outq);
263 return sched->init_sid(stream, sid, gfp);
264}
265
266struct sctp_sched_ops *sctp_sched_ops_from_stream(struct sctp_stream *stream)
267{
268 struct sctp_association *asoc;
269
270 asoc = container_of(stream, struct sctp_association, stream);
271
272 return asoc->outqueue.sched;
273}