stream_sched.c 6.3 KB

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