stream_sched_rr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /* Priority handling
  36. * RFC DRAFT ndata section 3.2
  37. */
  38. static void sctp_sched_rr_unsched_all(struct sctp_stream *stream);
  39. static void sctp_sched_rr_next_stream(struct sctp_stream *stream)
  40. {
  41. struct list_head *pos;
  42. pos = stream->rr_next->rr_list.next;
  43. if (pos == &stream->rr_list)
  44. pos = pos->next;
  45. stream->rr_next = list_entry(pos, struct sctp_stream_out_ext, rr_list);
  46. }
  47. static void sctp_sched_rr_unsched(struct sctp_stream *stream,
  48. struct sctp_stream_out_ext *soute)
  49. {
  50. if (stream->rr_next == soute)
  51. /* Try to move to the next stream */
  52. sctp_sched_rr_next_stream(stream);
  53. list_del_init(&soute->rr_list);
  54. /* If we have no other stream queued, clear next */
  55. if (list_empty(&stream->rr_list))
  56. stream->rr_next = NULL;
  57. }
  58. static void sctp_sched_rr_sched(struct sctp_stream *stream,
  59. struct sctp_stream_out_ext *soute)
  60. {
  61. if (!list_empty(&soute->rr_list))
  62. /* Already scheduled. */
  63. return;
  64. /* Schedule the stream */
  65. list_add_tail(&soute->rr_list, &stream->rr_list);
  66. if (!stream->rr_next)
  67. stream->rr_next = soute;
  68. }
  69. static int sctp_sched_rr_set(struct sctp_stream *stream, __u16 sid,
  70. __u16 prio, gfp_t gfp)
  71. {
  72. return 0;
  73. }
  74. static int sctp_sched_rr_get(struct sctp_stream *stream, __u16 sid,
  75. __u16 *value)
  76. {
  77. return 0;
  78. }
  79. static int sctp_sched_rr_init(struct sctp_stream *stream)
  80. {
  81. INIT_LIST_HEAD(&stream->rr_list);
  82. stream->rr_next = NULL;
  83. return 0;
  84. }
  85. static int sctp_sched_rr_init_sid(struct sctp_stream *stream, __u16 sid,
  86. gfp_t gfp)
  87. {
  88. INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->rr_list);
  89. return 0;
  90. }
  91. static void sctp_sched_rr_free(struct sctp_stream *stream)
  92. {
  93. sctp_sched_rr_unsched_all(stream);
  94. }
  95. static void sctp_sched_rr_enqueue(struct sctp_outq *q,
  96. struct sctp_datamsg *msg)
  97. {
  98. struct sctp_stream *stream;
  99. struct sctp_chunk *ch;
  100. __u16 sid;
  101. ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
  102. sid = sctp_chunk_stream_no(ch);
  103. stream = &q->asoc->stream;
  104. sctp_sched_rr_sched(stream, SCTP_SO(stream, sid)->ext);
  105. }
  106. static struct sctp_chunk *sctp_sched_rr_dequeue(struct sctp_outq *q)
  107. {
  108. struct sctp_stream *stream = &q->asoc->stream;
  109. struct sctp_stream_out_ext *soute;
  110. struct sctp_chunk *ch = NULL;
  111. /* Bail out quickly if queue is empty */
  112. if (list_empty(&q->out_chunk_list))
  113. goto out;
  114. /* Find which chunk is next */
  115. if (stream->out_curr)
  116. soute = stream->out_curr->ext;
  117. else
  118. soute = stream->rr_next;
  119. ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
  120. sctp_sched_dequeue_common(q, ch);
  121. out:
  122. return ch;
  123. }
  124. static void sctp_sched_rr_dequeue_done(struct sctp_outq *q,
  125. struct sctp_chunk *ch)
  126. {
  127. struct sctp_stream_out_ext *soute;
  128. __u16 sid;
  129. /* Last chunk on that msg, move to the next stream */
  130. sid = sctp_chunk_stream_no(ch);
  131. soute = SCTP_SO(&q->asoc->stream, sid)->ext;
  132. sctp_sched_rr_next_stream(&q->asoc->stream);
  133. if (list_empty(&soute->outq))
  134. sctp_sched_rr_unsched(&q->asoc->stream, soute);
  135. }
  136. static void sctp_sched_rr_sched_all(struct sctp_stream *stream)
  137. {
  138. struct sctp_association *asoc;
  139. struct sctp_stream_out_ext *soute;
  140. struct sctp_chunk *ch;
  141. asoc = container_of(stream, struct sctp_association, stream);
  142. list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
  143. __u16 sid;
  144. sid = sctp_chunk_stream_no(ch);
  145. soute = SCTP_SO(stream, sid)->ext;
  146. if (soute)
  147. sctp_sched_rr_sched(stream, soute);
  148. }
  149. }
  150. static void sctp_sched_rr_unsched_all(struct sctp_stream *stream)
  151. {
  152. struct sctp_stream_out_ext *soute, *tmp;
  153. list_for_each_entry_safe(soute, tmp, &stream->rr_list, rr_list)
  154. sctp_sched_rr_unsched(stream, soute);
  155. }
  156. static struct sctp_sched_ops sctp_sched_rr = {
  157. .set = sctp_sched_rr_set,
  158. .get = sctp_sched_rr_get,
  159. .init = sctp_sched_rr_init,
  160. .init_sid = sctp_sched_rr_init_sid,
  161. .free = sctp_sched_rr_free,
  162. .enqueue = sctp_sched_rr_enqueue,
  163. .dequeue = sctp_sched_rr_dequeue,
  164. .dequeue_done = sctp_sched_rr_dequeue_done,
  165. .sched_all = sctp_sched_rr_sched_all,
  166. .unsched_all = sctp_sched_rr_unsched_all,
  167. };
  168. void sctp_sched_ops_rr_init(void)
  169. {
  170. sctp_sched_ops_register(SCTP_SS_RR, &sctp_sched_rr);
  171. }