stream_sched.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <linux/list.h>
  32. #include <net/sctp/sctp.h>
  33. #include <net/sctp/sm.h>
  34. #include <net/sctp/stream_sched.h>
  35. /* First Come First Serve (a.k.a. FIFO)
  36. * RFC DRAFT ndata Section 3.1
  37. */
  38. static int sctp_sched_fcfs_set(struct sctp_stream *stream, __u16 sid,
  39. __u16 value, gfp_t gfp)
  40. {
  41. return 0;
  42. }
  43. static int sctp_sched_fcfs_get(struct sctp_stream *stream, __u16 sid,
  44. __u16 *value)
  45. {
  46. *value = 0;
  47. return 0;
  48. }
  49. static int sctp_sched_fcfs_init(struct sctp_stream *stream)
  50. {
  51. return 0;
  52. }
  53. static int sctp_sched_fcfs_init_sid(struct sctp_stream *stream, __u16 sid,
  54. gfp_t gfp)
  55. {
  56. return 0;
  57. }
  58. static void sctp_sched_fcfs_free(struct sctp_stream *stream)
  59. {
  60. }
  61. static void sctp_sched_fcfs_enqueue(struct sctp_outq *q,
  62. struct sctp_datamsg *msg)
  63. {
  64. }
  65. static struct sctp_chunk *sctp_sched_fcfs_dequeue(struct sctp_outq *q)
  66. {
  67. struct sctp_stream *stream = &q->asoc->stream;
  68. struct sctp_chunk *ch = NULL;
  69. struct list_head *entry;
  70. if (list_empty(&q->out_chunk_list))
  71. goto out;
  72. if (stream->out_curr) {
  73. ch = list_entry(stream->out_curr->ext->outq.next,
  74. struct sctp_chunk, stream_list);
  75. } else {
  76. entry = q->out_chunk_list.next;
  77. ch = list_entry(entry, struct sctp_chunk, list);
  78. }
  79. sctp_sched_dequeue_common(q, ch);
  80. out:
  81. return ch;
  82. }
  83. static void sctp_sched_fcfs_dequeue_done(struct sctp_outq *q,
  84. struct sctp_chunk *chunk)
  85. {
  86. }
  87. static void sctp_sched_fcfs_sched_all(struct sctp_stream *stream)
  88. {
  89. }
  90. static void sctp_sched_fcfs_unsched_all(struct sctp_stream *stream)
  91. {
  92. }
  93. static struct sctp_sched_ops sctp_sched_fcfs = {
  94. .set = sctp_sched_fcfs_set,
  95. .get = sctp_sched_fcfs_get,
  96. .init = sctp_sched_fcfs_init,
  97. .init_sid = sctp_sched_fcfs_init_sid,
  98. .free = sctp_sched_fcfs_free,
  99. .enqueue = sctp_sched_fcfs_enqueue,
  100. .dequeue = sctp_sched_fcfs_dequeue,
  101. .dequeue_done = sctp_sched_fcfs_dequeue_done,
  102. .sched_all = sctp_sched_fcfs_sched_all,
  103. .unsched_all = sctp_sched_fcfs_unsched_all,
  104. };
  105. static void sctp_sched_ops_fcfs_init(void)
  106. {
  107. sctp_sched_ops_register(SCTP_SS_FCFS, &sctp_sched_fcfs);
  108. }
  109. /* API to other parts of the stack */
  110. static struct sctp_sched_ops *sctp_sched_ops[SCTP_SS_MAX + 1];
  111. void sctp_sched_ops_register(enum sctp_sched_type sched,
  112. struct sctp_sched_ops *sched_ops)
  113. {
  114. sctp_sched_ops[sched] = sched_ops;
  115. }
  116. void sctp_sched_ops_init(void)
  117. {
  118. sctp_sched_ops_fcfs_init();
  119. sctp_sched_ops_prio_init();
  120. sctp_sched_ops_rr_init();
  121. }
  122. int sctp_sched_set_sched(struct sctp_association *asoc,
  123. enum sctp_sched_type sched)
  124. {
  125. struct sctp_sched_ops *n = sctp_sched_ops[sched];
  126. struct sctp_sched_ops *old = asoc->outqueue.sched;
  127. struct sctp_datamsg *msg = NULL;
  128. struct sctp_chunk *ch;
  129. int i, ret = 0;
  130. if (old == n)
  131. return ret;
  132. if (sched > SCTP_SS_MAX)
  133. return -EINVAL;
  134. if (old) {
  135. old->free(&asoc->stream);
  136. /* Give the next scheduler a clean slate. */
  137. for (i = 0; i < asoc->stream.outcnt; i++) {
  138. void *p = SCTP_SO(&asoc->stream, i)->ext;
  139. if (!p)
  140. continue;
  141. p += offsetofend(struct sctp_stream_out_ext, outq);
  142. memset(p, 0, sizeof(struct sctp_stream_out_ext) -
  143. offsetofend(struct sctp_stream_out_ext, outq));
  144. }
  145. }
  146. asoc->outqueue.sched = n;
  147. n->init(&asoc->stream);
  148. for (i = 0; i < asoc->stream.outcnt; i++) {
  149. if (!SCTP_SO(&asoc->stream, i)->ext)
  150. continue;
  151. ret = n->init_sid(&asoc->stream, i, GFP_KERNEL);
  152. if (ret)
  153. goto err;
  154. }
  155. /* We have to requeue all chunks already queued. */
  156. list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
  157. if (ch->msg == msg)
  158. continue;
  159. msg = ch->msg;
  160. n->enqueue(&asoc->outqueue, msg);
  161. }
  162. return ret;
  163. err:
  164. n->free(&asoc->stream);
  165. asoc->outqueue.sched = &sctp_sched_fcfs; /* Always safe */
  166. return ret;
  167. }
  168. int sctp_sched_get_sched(struct sctp_association *asoc)
  169. {
  170. int i;
  171. for (i = 0; i <= SCTP_SS_MAX; i++)
  172. if (asoc->outqueue.sched == sctp_sched_ops[i])
  173. return i;
  174. return 0;
  175. }
  176. int sctp_sched_set_value(struct sctp_association *asoc, __u16 sid,
  177. __u16 value, gfp_t gfp)
  178. {
  179. if (sid >= asoc->stream.outcnt)
  180. return -EINVAL;
  181. if (!SCTP_SO(&asoc->stream, sid)->ext) {
  182. int ret;
  183. ret = sctp_stream_init_ext(&asoc->stream, sid);
  184. if (ret)
  185. return ret;
  186. }
  187. return asoc->outqueue.sched->set(&asoc->stream, sid, value, gfp);
  188. }
  189. int sctp_sched_get_value(struct sctp_association *asoc, __u16 sid,
  190. __u16 *value)
  191. {
  192. if (sid >= asoc->stream.outcnt)
  193. return -EINVAL;
  194. if (!SCTP_SO(&asoc->stream, sid)->ext)
  195. return 0;
  196. return asoc->outqueue.sched->get(&asoc->stream, sid, value);
  197. }
  198. void sctp_sched_dequeue_done(struct sctp_outq *q, struct sctp_chunk *ch)
  199. {
  200. if (!list_is_last(&ch->frag_list, &ch->msg->chunks) &&
  201. !q->asoc->intl_enable) {
  202. struct sctp_stream_out *sout;
  203. __u16 sid;
  204. /* datamsg is not finish, so save it as current one,
  205. * in case application switch scheduler or a higher
  206. * priority stream comes in.
  207. */
  208. sid = sctp_chunk_stream_no(ch);
  209. sout = SCTP_SO(&q->asoc->stream, sid);
  210. q->asoc->stream.out_curr = sout;
  211. return;
  212. }
  213. q->asoc->stream.out_curr = NULL;
  214. q->sched->dequeue_done(q, ch);
  215. }
  216. /* Auxiliary functions for the schedulers */
  217. void sctp_sched_dequeue_common(struct sctp_outq *q, struct sctp_chunk *ch)
  218. {
  219. list_del_init(&ch->list);
  220. list_del_init(&ch->stream_list);
  221. q->out_qlen -= ch->skb->len;
  222. }
  223. int sctp_sched_init_sid(struct sctp_stream *stream, __u16 sid, gfp_t gfp)
  224. {
  225. struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
  226. struct sctp_stream_out_ext *ext = SCTP_SO(stream, sid)->ext;
  227. INIT_LIST_HEAD(&ext->outq);
  228. return sched->init_sid(stream, sid, gfp);
  229. }
  230. struct sctp_sched_ops *sctp_sched_ops_from_stream(struct sctp_stream *stream)
  231. {
  232. struct sctp_association *asoc;
  233. asoc = container_of(stream, struct sctp_association, stream);
  234. return asoc->outqueue.sched;
  235. }