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