0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/list.h>
0018 #include <net/sctp/sctp.h>
0019 #include <net/sctp/sm.h>
0020 #include <net/sctp/stream_sched.h>
0021
0022
0023
0024
0025
0026 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream);
0027
0028 static struct sctp_stream_priorities *sctp_sched_prio_new_head(
0029 struct sctp_stream *stream, int prio, gfp_t gfp)
0030 {
0031 struct sctp_stream_priorities *p;
0032
0033 p = kmalloc(sizeof(*p), gfp);
0034 if (!p)
0035 return NULL;
0036
0037 INIT_LIST_HEAD(&p->prio_sched);
0038 INIT_LIST_HEAD(&p->active);
0039 p->next = NULL;
0040 p->prio = prio;
0041
0042 return p;
0043 }
0044
0045 static struct sctp_stream_priorities *sctp_sched_prio_get_head(
0046 struct sctp_stream *stream, int prio, gfp_t gfp)
0047 {
0048 struct sctp_stream_priorities *p;
0049 int i;
0050
0051
0052
0053
0054 list_for_each_entry(p, &stream->prio_list, prio_sched) {
0055 if (p->prio == prio)
0056 return p;
0057 if (p->prio > prio)
0058 break;
0059 }
0060
0061
0062 for (i = 0; i < stream->outcnt; i++) {
0063 if (!SCTP_SO(stream, i)->ext)
0064 continue;
0065
0066 p = SCTP_SO(stream, i)->ext->prio_head;
0067 if (!p)
0068
0069
0070
0071 break;
0072 if (p->prio == prio)
0073 return p;
0074 }
0075
0076
0077 return sctp_sched_prio_new_head(stream, prio, gfp);
0078 }
0079
0080 static void sctp_sched_prio_next_stream(struct sctp_stream_priorities *p)
0081 {
0082 struct list_head *pos;
0083
0084 pos = p->next->prio_list.next;
0085 if (pos == &p->active)
0086 pos = pos->next;
0087 p->next = list_entry(pos, struct sctp_stream_out_ext, prio_list);
0088 }
0089
0090 static bool sctp_sched_prio_unsched(struct sctp_stream_out_ext *soute)
0091 {
0092 bool scheduled = false;
0093
0094 if (!list_empty(&soute->prio_list)) {
0095 struct sctp_stream_priorities *prio_head = soute->prio_head;
0096
0097
0098 scheduled = true;
0099
0100 if (prio_head->next == soute)
0101
0102 sctp_sched_prio_next_stream(prio_head);
0103
0104 list_del_init(&soute->prio_list);
0105
0106
0107 if (list_empty(&prio_head->active)) {
0108 list_del_init(&prio_head->prio_sched);
0109
0110 prio_head->next = NULL;
0111 }
0112 }
0113
0114 return scheduled;
0115 }
0116
0117 static void sctp_sched_prio_sched(struct sctp_stream *stream,
0118 struct sctp_stream_out_ext *soute)
0119 {
0120 struct sctp_stream_priorities *prio, *prio_head;
0121
0122 prio_head = soute->prio_head;
0123
0124
0125 if (!list_empty(&soute->prio_list))
0126 return;
0127
0128
0129
0130
0131
0132 if (prio_head->next) {
0133 list_add(&soute->prio_list, prio_head->next->prio_list.prev);
0134 return;
0135 }
0136
0137 list_add(&soute->prio_list, &prio_head->active);
0138 prio_head->next = soute;
0139
0140 list_for_each_entry(prio, &stream->prio_list, prio_sched) {
0141 if (prio->prio > prio_head->prio) {
0142 list_add(&prio_head->prio_sched, prio->prio_sched.prev);
0143 return;
0144 }
0145 }
0146
0147 list_add_tail(&prio_head->prio_sched, &stream->prio_list);
0148 }
0149
0150 static int sctp_sched_prio_set(struct sctp_stream *stream, __u16 sid,
0151 __u16 prio, gfp_t gfp)
0152 {
0153 struct sctp_stream_out *sout = SCTP_SO(stream, sid);
0154 struct sctp_stream_out_ext *soute = sout->ext;
0155 struct sctp_stream_priorities *prio_head, *old;
0156 bool reschedule = false;
0157 int i;
0158
0159 prio_head = sctp_sched_prio_get_head(stream, prio, gfp);
0160 if (!prio_head)
0161 return -ENOMEM;
0162
0163 reschedule = sctp_sched_prio_unsched(soute);
0164 old = soute->prio_head;
0165 soute->prio_head = prio_head;
0166 if (reschedule)
0167 sctp_sched_prio_sched(stream, soute);
0168
0169 if (!old)
0170
0171 return 0;
0172
0173 for (i = 0; i < stream->outcnt; i++) {
0174 soute = SCTP_SO(stream, i)->ext;
0175 if (soute && soute->prio_head == old)
0176
0177 return 0;
0178 }
0179
0180
0181 kfree(old);
0182
0183 return 0;
0184 }
0185
0186 static int sctp_sched_prio_get(struct sctp_stream *stream, __u16 sid,
0187 __u16 *value)
0188 {
0189 *value = SCTP_SO(stream, sid)->ext->prio_head->prio;
0190 return 0;
0191 }
0192
0193 static int sctp_sched_prio_init(struct sctp_stream *stream)
0194 {
0195 INIT_LIST_HEAD(&stream->prio_list);
0196
0197 return 0;
0198 }
0199
0200 static int sctp_sched_prio_init_sid(struct sctp_stream *stream, __u16 sid,
0201 gfp_t gfp)
0202 {
0203 INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->prio_list);
0204 return sctp_sched_prio_set(stream, sid, 0, gfp);
0205 }
0206
0207 static void sctp_sched_prio_free(struct sctp_stream *stream)
0208 {
0209 struct sctp_stream_priorities *prio, *n;
0210 LIST_HEAD(list);
0211 int i;
0212
0213
0214
0215
0216
0217
0218
0219 sctp_sched_prio_unsched_all(stream);
0220 for (i = 0; i < stream->outcnt; i++) {
0221 if (!SCTP_SO(stream, i)->ext)
0222 continue;
0223 prio = SCTP_SO(stream, i)->ext->prio_head;
0224 if (prio && list_empty(&prio->prio_sched))
0225 list_add(&prio->prio_sched, &list);
0226 }
0227 list_for_each_entry_safe(prio, n, &list, prio_sched) {
0228 list_del_init(&prio->prio_sched);
0229 kfree(prio);
0230 }
0231 }
0232
0233 static void sctp_sched_prio_enqueue(struct sctp_outq *q,
0234 struct sctp_datamsg *msg)
0235 {
0236 struct sctp_stream *stream;
0237 struct sctp_chunk *ch;
0238 __u16 sid;
0239
0240 ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
0241 sid = sctp_chunk_stream_no(ch);
0242 stream = &q->asoc->stream;
0243 sctp_sched_prio_sched(stream, SCTP_SO(stream, sid)->ext);
0244 }
0245
0246 static struct sctp_chunk *sctp_sched_prio_dequeue(struct sctp_outq *q)
0247 {
0248 struct sctp_stream *stream = &q->asoc->stream;
0249 struct sctp_stream_priorities *prio;
0250 struct sctp_stream_out_ext *soute;
0251 struct sctp_chunk *ch = NULL;
0252
0253
0254 if (list_empty(&q->out_chunk_list))
0255 goto out;
0256
0257
0258
0259
0260 if (stream->out_curr) {
0261 soute = stream->out_curr->ext;
0262 } else {
0263 prio = list_entry(stream->prio_list.next,
0264 struct sctp_stream_priorities, prio_sched);
0265 soute = prio->next;
0266 }
0267 ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
0268 sctp_sched_dequeue_common(q, ch);
0269
0270 out:
0271 return ch;
0272 }
0273
0274 static void sctp_sched_prio_dequeue_done(struct sctp_outq *q,
0275 struct sctp_chunk *ch)
0276 {
0277 struct sctp_stream_priorities *prio;
0278 struct sctp_stream_out_ext *soute;
0279 __u16 sid;
0280
0281
0282
0283
0284 sid = sctp_chunk_stream_no(ch);
0285 soute = SCTP_SO(&q->asoc->stream, sid)->ext;
0286 prio = soute->prio_head;
0287
0288 sctp_sched_prio_next_stream(prio);
0289
0290 if (list_empty(&soute->outq))
0291 sctp_sched_prio_unsched(soute);
0292 }
0293
0294 static void sctp_sched_prio_sched_all(struct sctp_stream *stream)
0295 {
0296 struct sctp_association *asoc;
0297 struct sctp_stream_out *sout;
0298 struct sctp_chunk *ch;
0299
0300 asoc = container_of(stream, struct sctp_association, stream);
0301 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
0302 __u16 sid;
0303
0304 sid = sctp_chunk_stream_no(ch);
0305 sout = SCTP_SO(stream, sid);
0306 if (sout->ext)
0307 sctp_sched_prio_sched(stream, sout->ext);
0308 }
0309 }
0310
0311 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream)
0312 {
0313 struct sctp_stream_priorities *p, *tmp;
0314 struct sctp_stream_out_ext *soute, *souttmp;
0315
0316 list_for_each_entry_safe(p, tmp, &stream->prio_list, prio_sched)
0317 list_for_each_entry_safe(soute, souttmp, &p->active, prio_list)
0318 sctp_sched_prio_unsched(soute);
0319 }
0320
0321 static struct sctp_sched_ops sctp_sched_prio = {
0322 .set = sctp_sched_prio_set,
0323 .get = sctp_sched_prio_get,
0324 .init = sctp_sched_prio_init,
0325 .init_sid = sctp_sched_prio_init_sid,
0326 .free = sctp_sched_prio_free,
0327 .enqueue = sctp_sched_prio_enqueue,
0328 .dequeue = sctp_sched_prio_dequeue,
0329 .dequeue_done = sctp_sched_prio_dequeue_done,
0330 .sched_all = sctp_sched_prio_sched_all,
0331 .unsched_all = sctp_sched_prio_unsched_all,
0332 };
0333
0334 void sctp_sched_ops_prio_init(void)
0335 {
0336 sctp_sched_ops_register(SCTP_SS_PRIO, &sctp_sched_prio);
0337 }