xref: /openbmc/linux/net/sctp/stream_sched_rr.c (revision 2da68a77)
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  */
25 static void sctp_sched_rr_unsched_all(struct sctp_stream *stream);
26 
27 static 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 
37 static 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 
51 static 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 
65 static int sctp_sched_rr_set(struct sctp_stream *stream, __u16 sid,
66 			     __u16 prio, gfp_t gfp)
67 {
68 	return 0;
69 }
70 
71 static int sctp_sched_rr_get(struct sctp_stream *stream, __u16 sid,
72 			     __u16 *value)
73 {
74 	return 0;
75 }
76 
77 static 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 
85 static 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 
93 static void sctp_sched_rr_free_sid(struct sctp_stream *stream, __u16 sid)
94 {
95 }
96 
97 static void sctp_sched_rr_free(struct sctp_stream *stream)
98 {
99 	sctp_sched_rr_unsched_all(stream);
100 }
101 
102 static void sctp_sched_rr_enqueue(struct sctp_outq *q,
103 				  struct sctp_datamsg *msg)
104 {
105 	struct sctp_stream *stream;
106 	struct sctp_chunk *ch;
107 	__u16 sid;
108 
109 	ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
110 	sid = sctp_chunk_stream_no(ch);
111 	stream = &q->asoc->stream;
112 	sctp_sched_rr_sched(stream, SCTP_SO(stream, sid)->ext);
113 }
114 
115 static struct sctp_chunk *sctp_sched_rr_dequeue(struct sctp_outq *q)
116 {
117 	struct sctp_stream *stream = &q->asoc->stream;
118 	struct sctp_stream_out_ext *soute;
119 	struct sctp_chunk *ch = NULL;
120 
121 	/* Bail out quickly if queue is empty */
122 	if (list_empty(&q->out_chunk_list))
123 		goto out;
124 
125 	/* Find which chunk is next */
126 	if (stream->out_curr)
127 		soute = stream->out_curr->ext;
128 	else
129 		soute = stream->rr_next;
130 	ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
131 
132 	sctp_sched_dequeue_common(q, ch);
133 
134 out:
135 	return ch;
136 }
137 
138 static void sctp_sched_rr_dequeue_done(struct sctp_outq *q,
139 				       struct sctp_chunk *ch)
140 {
141 	struct sctp_stream_out_ext *soute;
142 	__u16 sid;
143 
144 	/* Last chunk on that msg, move to the next stream */
145 	sid = sctp_chunk_stream_no(ch);
146 	soute = SCTP_SO(&q->asoc->stream, sid)->ext;
147 
148 	sctp_sched_rr_next_stream(&q->asoc->stream);
149 
150 	if (list_empty(&soute->outq))
151 		sctp_sched_rr_unsched(&q->asoc->stream, soute);
152 }
153 
154 static void sctp_sched_rr_sched_all(struct sctp_stream *stream)
155 {
156 	struct sctp_association *asoc;
157 	struct sctp_stream_out_ext *soute;
158 	struct sctp_chunk *ch;
159 
160 	asoc = container_of(stream, struct sctp_association, stream);
161 	list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
162 		__u16 sid;
163 
164 		sid = sctp_chunk_stream_no(ch);
165 		soute = SCTP_SO(stream, sid)->ext;
166 		if (soute)
167 			sctp_sched_rr_sched(stream, soute);
168 	}
169 }
170 
171 static void sctp_sched_rr_unsched_all(struct sctp_stream *stream)
172 {
173 	struct sctp_stream_out_ext *soute, *tmp;
174 
175 	list_for_each_entry_safe(soute, tmp, &stream->rr_list, rr_list)
176 		sctp_sched_rr_unsched(stream, soute);
177 }
178 
179 static struct sctp_sched_ops sctp_sched_rr = {
180 	.set = sctp_sched_rr_set,
181 	.get = sctp_sched_rr_get,
182 	.init = sctp_sched_rr_init,
183 	.init_sid = sctp_sched_rr_init_sid,
184 	.free_sid = sctp_sched_rr_free_sid,
185 	.free = sctp_sched_rr_free,
186 	.enqueue = sctp_sched_rr_enqueue,
187 	.dequeue = sctp_sched_rr_dequeue,
188 	.dequeue_done = sctp_sched_rr_dequeue_done,
189 	.sched_all = sctp_sched_rr_sched_all,
190 	.unsched_all = sctp_sched_rr_unsched_all,
191 };
192 
193 void sctp_sched_ops_rr_init(void)
194 {
195 	sctp_sched_ops_register(SCTP_SS_RR, &sctp_sched_rr);
196 }
197