stream_sched_fc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright Red Hat Inc. 2022
  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. * Xin Long <lucien.xin@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. /* Fair Capacity and Weighted Fair Queueing handling
  21. * RFC 8260 section 3.5 and 3.6
  22. */
  23. static void sctp_sched_fc_unsched_all(struct sctp_stream *stream);
  24. static int sctp_sched_wfq_set(struct sctp_stream *stream, __u16 sid,
  25. __u16 weight, gfp_t gfp)
  26. {
  27. struct sctp_stream_out_ext *soute = SCTP_SO(stream, sid)->ext;
  28. if (!weight)
  29. return -EINVAL;
  30. soute->fc_weight = weight;
  31. return 0;
  32. }
  33. static int sctp_sched_wfq_get(struct sctp_stream *stream, __u16 sid,
  34. __u16 *value)
  35. {
  36. struct sctp_stream_out_ext *soute = SCTP_SO(stream, sid)->ext;
  37. *value = soute->fc_weight;
  38. return 0;
  39. }
  40. static int sctp_sched_fc_set(struct sctp_stream *stream, __u16 sid,
  41. __u16 weight, gfp_t gfp)
  42. {
  43. return 0;
  44. }
  45. static int sctp_sched_fc_get(struct sctp_stream *stream, __u16 sid,
  46. __u16 *value)
  47. {
  48. return 0;
  49. }
  50. static int sctp_sched_fc_init(struct sctp_stream *stream)
  51. {
  52. INIT_LIST_HEAD(&stream->fc_list);
  53. return 0;
  54. }
  55. static int sctp_sched_fc_init_sid(struct sctp_stream *stream, __u16 sid,
  56. gfp_t gfp)
  57. {
  58. struct sctp_stream_out_ext *soute = SCTP_SO(stream, sid)->ext;
  59. INIT_LIST_HEAD(&soute->fc_list);
  60. soute->fc_length = 0;
  61. soute->fc_weight = 1;
  62. return 0;
  63. }
  64. static void sctp_sched_fc_free_sid(struct sctp_stream *stream, __u16 sid)
  65. {
  66. }
  67. static void sctp_sched_fc_sched(struct sctp_stream *stream,
  68. struct sctp_stream_out_ext *soute)
  69. {
  70. struct sctp_stream_out_ext *pos;
  71. if (!list_empty(&soute->fc_list))
  72. return;
  73. list_for_each_entry(pos, &stream->fc_list, fc_list)
  74. if ((__u64)pos->fc_length * soute->fc_weight >=
  75. (__u64)soute->fc_length * pos->fc_weight)
  76. break;
  77. list_add_tail(&soute->fc_list, &pos->fc_list);
  78. }
  79. static void sctp_sched_fc_enqueue(struct sctp_outq *q,
  80. struct sctp_datamsg *msg)
  81. {
  82. struct sctp_stream *stream;
  83. struct sctp_chunk *ch;
  84. __u16 sid;
  85. ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
  86. sid = sctp_chunk_stream_no(ch);
  87. stream = &q->asoc->stream;
  88. sctp_sched_fc_sched(stream, SCTP_SO(stream, sid)->ext);
  89. }
  90. static struct sctp_chunk *sctp_sched_fc_dequeue(struct sctp_outq *q)
  91. {
  92. struct sctp_stream *stream = &q->asoc->stream;
  93. struct sctp_stream_out_ext *soute;
  94. struct sctp_chunk *ch;
  95. /* Bail out quickly if queue is empty */
  96. if (list_empty(&q->out_chunk_list))
  97. return NULL;
  98. /* Find which chunk is next */
  99. if (stream->out_curr)
  100. soute = stream->out_curr->ext;
  101. else
  102. soute = list_entry(stream->fc_list.next, struct sctp_stream_out_ext, fc_list);
  103. ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
  104. sctp_sched_dequeue_common(q, ch);
  105. return ch;
  106. }
  107. static void sctp_sched_fc_dequeue_done(struct sctp_outq *q,
  108. struct sctp_chunk *ch)
  109. {
  110. struct sctp_stream *stream = &q->asoc->stream;
  111. struct sctp_stream_out_ext *soute, *pos;
  112. __u16 sid, i;
  113. sid = sctp_chunk_stream_no(ch);
  114. soute = SCTP_SO(stream, sid)->ext;
  115. /* reduce all fc_lengths by U32_MAX / 4 if the current fc_length overflows. */
  116. if (soute->fc_length > U32_MAX - ch->skb->len) {
  117. for (i = 0; i < stream->outcnt; i++) {
  118. pos = SCTP_SO(stream, i)->ext;
  119. if (!pos)
  120. continue;
  121. if (pos->fc_length <= (U32_MAX >> 2)) {
  122. pos->fc_length = 0;
  123. continue;
  124. }
  125. pos->fc_length -= (U32_MAX >> 2);
  126. }
  127. }
  128. soute->fc_length += ch->skb->len;
  129. if (list_empty(&soute->outq)) {
  130. list_del_init(&soute->fc_list);
  131. return;
  132. }
  133. pos = soute;
  134. list_for_each_entry_continue(pos, &stream->fc_list, fc_list)
  135. if ((__u64)pos->fc_length * soute->fc_weight >=
  136. (__u64)soute->fc_length * pos->fc_weight)
  137. break;
  138. list_move_tail(&soute->fc_list, &pos->fc_list);
  139. }
  140. static void sctp_sched_fc_sched_all(struct sctp_stream *stream)
  141. {
  142. struct sctp_association *asoc;
  143. struct sctp_chunk *ch;
  144. asoc = container_of(stream, struct sctp_association, stream);
  145. list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
  146. __u16 sid = sctp_chunk_stream_no(ch);
  147. if (SCTP_SO(stream, sid)->ext)
  148. sctp_sched_fc_sched(stream, SCTP_SO(stream, sid)->ext);
  149. }
  150. }
  151. static void sctp_sched_fc_unsched_all(struct sctp_stream *stream)
  152. {
  153. struct sctp_stream_out_ext *soute, *tmp;
  154. list_for_each_entry_safe(soute, tmp, &stream->fc_list, fc_list)
  155. list_del_init(&soute->fc_list);
  156. }
  157. static struct sctp_sched_ops sctp_sched_fc = {
  158. .set = sctp_sched_fc_set,
  159. .get = sctp_sched_fc_get,
  160. .init = sctp_sched_fc_init,
  161. .init_sid = sctp_sched_fc_init_sid,
  162. .free_sid = sctp_sched_fc_free_sid,
  163. .enqueue = sctp_sched_fc_enqueue,
  164. .dequeue = sctp_sched_fc_dequeue,
  165. .dequeue_done = sctp_sched_fc_dequeue_done,
  166. .sched_all = sctp_sched_fc_sched_all,
  167. .unsched_all = sctp_sched_fc_unsched_all,
  168. };
  169. void sctp_sched_ops_fc_init(void)
  170. {
  171. sctp_sched_ops_register(SCTP_SS_FC, &sctp_sched_fc);
  172. }
  173. static struct sctp_sched_ops sctp_sched_wfq = {
  174. .set = sctp_sched_wfq_set,
  175. .get = sctp_sched_wfq_get,
  176. .init = sctp_sched_fc_init,
  177. .init_sid = sctp_sched_fc_init_sid,
  178. .free_sid = sctp_sched_fc_free_sid,
  179. .enqueue = sctp_sched_fc_enqueue,
  180. .dequeue = sctp_sched_fc_dequeue,
  181. .dequeue_done = sctp_sched_fc_dequeue_done,
  182. .sched_all = sctp_sched_fc_sched_all,
  183. .unsched_all = sctp_sched_fc_unsched_all,
  184. };
  185. void sctp_sched_ops_wfq_init(void)
  186. {
  187. sctp_sched_ops_register(SCTP_SS_WFQ, &sctp_sched_wfq);
  188. }