Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* SCTP kernel implementation
3 * (C) Copyright Red Hat Inc. 2017
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 * Marcelo Ricardo Leitner <marcelo.leitner@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/* Priority handling
23 * RFC DRAFT ndata section 3.2
24 */
25static void sctp_sched_rr_unsched_all(struct sctp_stream *stream);
26
27static void sctp_sched_rr_next_stream(struct sctp_stream *stream)
28{
29 struct list_head *pos;
30
31 pos = stream->rr_next->rr_list.next;
32 if (pos == &stream->rr_list)
33 pos = pos->next;
34 stream->rr_next = list_entry(pos, struct sctp_stream_out_ext, rr_list);
35}
36
37static void sctp_sched_rr_unsched(struct sctp_stream *stream,
38 struct sctp_stream_out_ext *soute)
39{
40 if (stream->rr_next == soute)
41 /* Try to move to the next stream */
42 sctp_sched_rr_next_stream(stream);
43
44 list_del_init(&soute->rr_list);
45
46 /* If we have no other stream queued, clear next */
47 if (list_empty(&stream->rr_list))
48 stream->rr_next = NULL;
49}
50
51static void sctp_sched_rr_sched(struct sctp_stream *stream,
52 struct sctp_stream_out_ext *soute)
53{
54 if (!list_empty(&soute->rr_list))
55 /* Already scheduled. */
56 return;
57
58 /* Schedule the stream */
59 list_add_tail(&soute->rr_list, &stream->rr_list);
60
61 if (!stream->rr_next)
62 stream->rr_next = soute;
63}
64
65static int sctp_sched_rr_set(struct sctp_stream *stream, __u16 sid,
66 __u16 prio, gfp_t gfp)
67{
68 return 0;
69}
70
71static int sctp_sched_rr_get(struct sctp_stream *stream, __u16 sid,
72 __u16 *value)
73{
74 return 0;
75}
76
77static int sctp_sched_rr_init(struct sctp_stream *stream)
78{
79 INIT_LIST_HEAD(&stream->rr_list);
80 stream->rr_next = NULL;
81
82 return 0;
83}
84
85static int sctp_sched_rr_init_sid(struct sctp_stream *stream, __u16 sid,
86 gfp_t gfp)
87{
88 INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->rr_list);
89
90 return 0;
91}
92
93static void sctp_sched_rr_free_sid(struct sctp_stream *stream, __u16 sid)
94{
95}
96
97static void sctp_sched_rr_enqueue(struct sctp_outq *q,
98 struct sctp_datamsg *msg)
99{
100 struct sctp_stream *stream;
101 struct sctp_chunk *ch;
102 __u16 sid;
103
104 ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
105 sid = sctp_chunk_stream_no(ch);
106 stream = &q->asoc->stream;
107 sctp_sched_rr_sched(stream, SCTP_SO(stream, sid)->ext);
108}
109
110static struct sctp_chunk *sctp_sched_rr_dequeue(struct sctp_outq *q)
111{
112 struct sctp_stream *stream = &q->asoc->stream;
113 struct sctp_stream_out_ext *soute;
114 struct sctp_chunk *ch = NULL;
115
116 /* Bail out quickly if queue is empty */
117 if (list_empty(&q->out_chunk_list))
118 goto out;
119
120 /* Find which chunk is next */
121 if (stream->out_curr)
122 soute = stream->out_curr->ext;
123 else
124 soute = stream->rr_next;
125 ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
126
127 sctp_sched_dequeue_common(q, ch);
128
129out:
130 return ch;
131}
132
133static void sctp_sched_rr_dequeue_done(struct sctp_outq *q,
134 struct sctp_chunk *ch)
135{
136 struct sctp_stream_out_ext *soute;
137 __u16 sid;
138
139 /* Last chunk on that msg, move to the next stream */
140 sid = sctp_chunk_stream_no(ch);
141 soute = SCTP_SO(&q->asoc->stream, sid)->ext;
142
143 sctp_sched_rr_next_stream(&q->asoc->stream);
144
145 if (list_empty(&soute->outq))
146 sctp_sched_rr_unsched(&q->asoc->stream, soute);
147}
148
149static void sctp_sched_rr_sched_all(struct sctp_stream *stream)
150{
151 struct sctp_association *asoc;
152 struct sctp_stream_out_ext *soute;
153 struct sctp_chunk *ch;
154
155 asoc = container_of(stream, struct sctp_association, stream);
156 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
157 __u16 sid;
158
159 sid = sctp_chunk_stream_no(ch);
160 soute = SCTP_SO(stream, sid)->ext;
161 if (soute)
162 sctp_sched_rr_sched(stream, soute);
163 }
164}
165
166static void sctp_sched_rr_unsched_all(struct sctp_stream *stream)
167{
168 struct sctp_stream_out_ext *soute, *tmp;
169
170 list_for_each_entry_safe(soute, tmp, &stream->rr_list, rr_list)
171 sctp_sched_rr_unsched(stream, soute);
172}
173
174static struct sctp_sched_ops sctp_sched_rr = {
175 .set = sctp_sched_rr_set,
176 .get = sctp_sched_rr_get,
177 .init = sctp_sched_rr_init,
178 .init_sid = sctp_sched_rr_init_sid,
179 .free_sid = sctp_sched_rr_free_sid,
180 .enqueue = sctp_sched_rr_enqueue,
181 .dequeue = sctp_sched_rr_dequeue,
182 .dequeue_done = sctp_sched_rr_dequeue_done,
183 .sched_all = sctp_sched_rr_sched_all,
184 .unsched_all = sctp_sched_rr_unsched_all,
185};
186
187void sctp_sched_ops_rr_init(void)
188{
189 sctp_sched_ops_register(SCTP_SS_RR, &sctp_sched_rr);
190}
1/* 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/* Priority handling
38 * RFC DRAFT ndata section 3.2
39 */
40static void sctp_sched_rr_unsched_all(struct sctp_stream *stream);
41
42static void sctp_sched_rr_next_stream(struct sctp_stream *stream)
43{
44 struct list_head *pos;
45
46 pos = stream->rr_next->rr_list.next;
47 if (pos == &stream->rr_list)
48 pos = pos->next;
49 stream->rr_next = list_entry(pos, struct sctp_stream_out_ext, rr_list);
50}
51
52static void sctp_sched_rr_unsched(struct sctp_stream *stream,
53 struct sctp_stream_out_ext *soute)
54{
55 if (stream->rr_next == soute)
56 /* Try to move to the next stream */
57 sctp_sched_rr_next_stream(stream);
58
59 list_del_init(&soute->rr_list);
60
61 /* If we have no other stream queued, clear next */
62 if (list_empty(&stream->rr_list))
63 stream->rr_next = NULL;
64}
65
66static void sctp_sched_rr_sched(struct sctp_stream *stream,
67 struct sctp_stream_out_ext *soute)
68{
69 if (!list_empty(&soute->rr_list))
70 /* Already scheduled. */
71 return;
72
73 /* Schedule the stream */
74 list_add_tail(&soute->rr_list, &stream->rr_list);
75
76 if (!stream->rr_next)
77 stream->rr_next = soute;
78}
79
80static int sctp_sched_rr_set(struct sctp_stream *stream, __u16 sid,
81 __u16 prio, gfp_t gfp)
82{
83 return 0;
84}
85
86static int sctp_sched_rr_get(struct sctp_stream *stream, __u16 sid,
87 __u16 *value)
88{
89 return 0;
90}
91
92static int sctp_sched_rr_init(struct sctp_stream *stream)
93{
94 INIT_LIST_HEAD(&stream->rr_list);
95 stream->rr_next = NULL;
96
97 return 0;
98}
99
100static int sctp_sched_rr_init_sid(struct sctp_stream *stream, __u16 sid,
101 gfp_t gfp)
102{
103 INIT_LIST_HEAD(&stream->out[sid].ext->rr_list);
104
105 return 0;
106}
107
108static void sctp_sched_rr_free(struct sctp_stream *stream)
109{
110 sctp_sched_rr_unsched_all(stream);
111}
112
113static void sctp_sched_rr_enqueue(struct sctp_outq *q,
114 struct sctp_datamsg *msg)
115{
116 struct sctp_stream *stream;
117 struct sctp_chunk *ch;
118 __u16 sid;
119
120 ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
121 sid = sctp_chunk_stream_no(ch);
122 stream = &q->asoc->stream;
123 sctp_sched_rr_sched(stream, stream->out[sid].ext);
124}
125
126static struct sctp_chunk *sctp_sched_rr_dequeue(struct sctp_outq *q)
127{
128 struct sctp_stream *stream = &q->asoc->stream;
129 struct sctp_stream_out_ext *soute;
130 struct sctp_chunk *ch = NULL;
131
132 /* Bail out quickly if queue is empty */
133 if (list_empty(&q->out_chunk_list))
134 goto out;
135
136 /* Find which chunk is next */
137 if (stream->out_curr)
138 soute = stream->out_curr->ext;
139 else
140 soute = stream->rr_next;
141 ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
142
143 sctp_sched_dequeue_common(q, ch);
144
145out:
146 return ch;
147}
148
149static void sctp_sched_rr_dequeue_done(struct sctp_outq *q,
150 struct sctp_chunk *ch)
151{
152 struct sctp_stream_out_ext *soute;
153 __u16 sid;
154
155 /* Last chunk on that msg, move to the next stream */
156 sid = sctp_chunk_stream_no(ch);
157 soute = q->asoc->stream.out[sid].ext;
158
159 sctp_sched_rr_next_stream(&q->asoc->stream);
160
161 if (list_empty(&soute->outq))
162 sctp_sched_rr_unsched(&q->asoc->stream, soute);
163}
164
165static void sctp_sched_rr_sched_all(struct sctp_stream *stream)
166{
167 struct sctp_association *asoc;
168 struct sctp_stream_out_ext *soute;
169 struct sctp_chunk *ch;
170
171 asoc = container_of(stream, struct sctp_association, stream);
172 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
173 __u16 sid;
174
175 sid = sctp_chunk_stream_no(ch);
176 soute = stream->out[sid].ext;
177 if (soute)
178 sctp_sched_rr_sched(stream, soute);
179 }
180}
181
182static void sctp_sched_rr_unsched_all(struct sctp_stream *stream)
183{
184 struct sctp_stream_out_ext *soute, *tmp;
185
186 list_for_each_entry_safe(soute, tmp, &stream->rr_list, rr_list)
187 sctp_sched_rr_unsched(stream, soute);
188}
189
190static struct sctp_sched_ops sctp_sched_rr = {
191 .set = sctp_sched_rr_set,
192 .get = sctp_sched_rr_get,
193 .init = sctp_sched_rr_init,
194 .init_sid = sctp_sched_rr_init_sid,
195 .free = sctp_sched_rr_free,
196 .enqueue = sctp_sched_rr_enqueue,
197 .dequeue = sctp_sched_rr_dequeue,
198 .dequeue_done = sctp_sched_rr_dequeue_done,
199 .sched_all = sctp_sched_rr_sched_all,
200 .unsched_all = sctp_sched_rr_unsched_all,
201};
202
203void sctp_sched_ops_rr_init(void)
204{
205 sctp_sched_ops_register(SCTP_SS_RR, &sctp_sched_rr);
206}