stream_sched_rr.c 4.7 KB

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