stream_sched.c 6.3 KB

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