stream_sched.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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(struct sctp_stream *stream)
  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 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 = sctp_sched_fcfs_free,
  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 struct sctp_sched_ops *sctp_sched_ops[SCTP_SS_MAX + 1];
  96. void sctp_sched_ops_register(enum sctp_sched_type sched,
  97. 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. }
  107. int sctp_sched_set_sched(struct sctp_association *asoc,
  108. enum sctp_sched_type sched)
  109. {
  110. struct sctp_sched_ops *n = sctp_sched_ops[sched];
  111. struct sctp_sched_ops *old = asoc->outqueue.sched;
  112. struct sctp_datamsg *msg = NULL;
  113. struct sctp_chunk *ch;
  114. int i, ret = 0;
  115. if (old == n)
  116. return ret;
  117. if (sched > SCTP_SS_MAX)
  118. return -EINVAL;
  119. if (old) {
  120. old->free(&asoc->stream);
  121. /* Give the next scheduler a clean slate. */
  122. for (i = 0; i < asoc->stream.outcnt; i++) {
  123. void *p = SCTP_SO(&asoc->stream, i)->ext;
  124. if (!p)
  125. continue;
  126. p += offsetofend(struct sctp_stream_out_ext, outq);
  127. memset(p, 0, sizeof(struct sctp_stream_out_ext) -
  128. offsetofend(struct sctp_stream_out_ext, outq));
  129. }
  130. }
  131. asoc->outqueue.sched = n;
  132. n->init(&asoc->stream);
  133. for (i = 0; i < asoc->stream.outcnt; i++) {
  134. if (!SCTP_SO(&asoc->stream, i)->ext)
  135. continue;
  136. ret = n->init_sid(&asoc->stream, i, GFP_KERNEL);
  137. if (ret)
  138. goto err;
  139. }
  140. /* We have to requeue all chunks already queued. */
  141. list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
  142. if (ch->msg == msg)
  143. continue;
  144. msg = ch->msg;
  145. n->enqueue(&asoc->outqueue, msg);
  146. }
  147. return ret;
  148. err:
  149. n->free(&asoc->stream);
  150. asoc->outqueue.sched = &sctp_sched_fcfs; /* Always safe */
  151. return ret;
  152. }
  153. int sctp_sched_get_sched(struct sctp_association *asoc)
  154. {
  155. int i;
  156. for (i = 0; i <= SCTP_SS_MAX; i++)
  157. if (asoc->outqueue.sched == sctp_sched_ops[i])
  158. return i;
  159. return 0;
  160. }
  161. int sctp_sched_set_value(struct sctp_association *asoc, __u16 sid,
  162. __u16 value, gfp_t gfp)
  163. {
  164. if (sid >= asoc->stream.outcnt)
  165. return -EINVAL;
  166. if (!SCTP_SO(&asoc->stream, sid)->ext) {
  167. int ret;
  168. ret = sctp_stream_init_ext(&asoc->stream, sid);
  169. if (ret)
  170. return ret;
  171. }
  172. return asoc->outqueue.sched->set(&asoc->stream, sid, value, gfp);
  173. }
  174. int sctp_sched_get_value(struct sctp_association *asoc, __u16 sid,
  175. __u16 *value)
  176. {
  177. if (sid >= asoc->stream.outcnt)
  178. return -EINVAL;
  179. if (!SCTP_SO(&asoc->stream, sid)->ext)
  180. return 0;
  181. return asoc->outqueue.sched->get(&asoc->stream, sid, value);
  182. }
  183. void sctp_sched_dequeue_done(struct sctp_outq *q, struct sctp_chunk *ch)
  184. {
  185. if (!list_is_last(&ch->frag_list, &ch->msg->chunks) &&
  186. !q->asoc->peer.intl_capable) {
  187. struct sctp_stream_out *sout;
  188. __u16 sid;
  189. /* datamsg is not finish, so save it as current one,
  190. * in case application switch scheduler or a higher
  191. * priority stream comes in.
  192. */
  193. sid = sctp_chunk_stream_no(ch);
  194. sout = SCTP_SO(&q->asoc->stream, sid);
  195. q->asoc->stream.out_curr = sout;
  196. return;
  197. }
  198. q->asoc->stream.out_curr = NULL;
  199. q->sched->dequeue_done(q, ch);
  200. }
  201. /* Auxiliary functions for the schedulers */
  202. void sctp_sched_dequeue_common(struct sctp_outq *q, struct sctp_chunk *ch)
  203. {
  204. list_del_init(&ch->list);
  205. list_del_init(&ch->stream_list);
  206. q->out_qlen -= ch->skb->len;
  207. }
  208. int sctp_sched_init_sid(struct sctp_stream *stream, __u16 sid, gfp_t gfp)
  209. {
  210. struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
  211. struct sctp_stream_out_ext *ext = SCTP_SO(stream, sid)->ext;
  212. INIT_LIST_HEAD(&ext->outq);
  213. return sched->init_sid(stream, sid, gfp);
  214. }
  215. struct sctp_sched_ops *sctp_sched_ops_from_stream(struct sctp_stream *stream)
  216. {
  217. struct sctp_association *asoc;
  218. asoc = container_of(stream, struct sctp_association, stream);
  219. return asoc->outqueue.sched;
  220. }