stream_sched_prio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /* Priority handling
  21. * RFC DRAFT ndata section 3.4
  22. */
  23. static void sctp_sched_prio_unsched_all(struct sctp_stream *stream);
  24. static struct sctp_stream_priorities *sctp_sched_prio_new_head(
  25. struct sctp_stream *stream, int prio, gfp_t gfp)
  26. {
  27. struct sctp_stream_priorities *p;
  28. p = kmalloc(sizeof(*p), gfp);
  29. if (!p)
  30. return NULL;
  31. INIT_LIST_HEAD(&p->prio_sched);
  32. INIT_LIST_HEAD(&p->active);
  33. p->next = NULL;
  34. p->prio = prio;
  35. return p;
  36. }
  37. static struct sctp_stream_priorities *sctp_sched_prio_get_head(
  38. struct sctp_stream *stream, int prio, gfp_t gfp)
  39. {
  40. struct sctp_stream_priorities *p;
  41. int i;
  42. /* Look into scheduled priorities first, as they are sorted and
  43. * we can find it fast IF it's scheduled.
  44. */
  45. list_for_each_entry(p, &stream->prio_list, prio_sched) {
  46. if (p->prio == prio)
  47. return p;
  48. if (p->prio > prio)
  49. break;
  50. }
  51. /* No luck. So we search on all streams now. */
  52. for (i = 0; i < stream->outcnt; i++) {
  53. if (!SCTP_SO(stream, i)->ext)
  54. continue;
  55. p = SCTP_SO(stream, i)->ext->prio_head;
  56. if (!p)
  57. /* Means all other streams won't be initialized
  58. * as well.
  59. */
  60. break;
  61. if (p->prio == prio)
  62. return p;
  63. }
  64. /* If not even there, allocate a new one. */
  65. return sctp_sched_prio_new_head(stream, prio, gfp);
  66. }
  67. static void sctp_sched_prio_next_stream(struct sctp_stream_priorities *p)
  68. {
  69. struct list_head *pos;
  70. pos = p->next->prio_list.next;
  71. if (pos == &p->active)
  72. pos = pos->next;
  73. p->next = list_entry(pos, struct sctp_stream_out_ext, prio_list);
  74. }
  75. static bool sctp_sched_prio_unsched(struct sctp_stream_out_ext *soute)
  76. {
  77. bool scheduled = false;
  78. if (!list_empty(&soute->prio_list)) {
  79. struct sctp_stream_priorities *prio_head = soute->prio_head;
  80. /* Scheduled */
  81. scheduled = true;
  82. if (prio_head->next == soute)
  83. /* Try to move to the next stream */
  84. sctp_sched_prio_next_stream(prio_head);
  85. list_del_init(&soute->prio_list);
  86. /* Also unsched the priority if this was the last stream */
  87. if (list_empty(&prio_head->active)) {
  88. list_del_init(&prio_head->prio_sched);
  89. /* If there is no stream left, clear next */
  90. prio_head->next = NULL;
  91. }
  92. }
  93. return scheduled;
  94. }
  95. static void sctp_sched_prio_sched(struct sctp_stream *stream,
  96. struct sctp_stream_out_ext *soute)
  97. {
  98. struct sctp_stream_priorities *prio, *prio_head;
  99. prio_head = soute->prio_head;
  100. /* Nothing to do if already scheduled */
  101. if (!list_empty(&soute->prio_list))
  102. return;
  103. /* Schedule the stream. If there is a next, we schedule the new
  104. * one before it, so it's the last in round robin order.
  105. * If there isn't, we also have to schedule the priority.
  106. */
  107. if (prio_head->next) {
  108. list_add(&soute->prio_list, prio_head->next->prio_list.prev);
  109. return;
  110. }
  111. list_add(&soute->prio_list, &prio_head->active);
  112. prio_head->next = soute;
  113. list_for_each_entry(prio, &stream->prio_list, prio_sched) {
  114. if (prio->prio > prio_head->prio) {
  115. list_add(&prio_head->prio_sched, prio->prio_sched.prev);
  116. return;
  117. }
  118. }
  119. list_add_tail(&prio_head->prio_sched, &stream->prio_list);
  120. }
  121. static int sctp_sched_prio_set(struct sctp_stream *stream, __u16 sid,
  122. __u16 prio, gfp_t gfp)
  123. {
  124. struct sctp_stream_out *sout = SCTP_SO(stream, sid);
  125. struct sctp_stream_out_ext *soute = sout->ext;
  126. struct sctp_stream_priorities *prio_head, *old;
  127. bool reschedule = false;
  128. int i;
  129. prio_head = sctp_sched_prio_get_head(stream, prio, gfp);
  130. if (!prio_head)
  131. return -ENOMEM;
  132. reschedule = sctp_sched_prio_unsched(soute);
  133. old = soute->prio_head;
  134. soute->prio_head = prio_head;
  135. if (reschedule)
  136. sctp_sched_prio_sched(stream, soute);
  137. if (!old)
  138. /* Happens when we set the priority for the first time */
  139. return 0;
  140. for (i = 0; i < stream->outcnt; i++) {
  141. soute = SCTP_SO(stream, i)->ext;
  142. if (soute && soute->prio_head == old)
  143. /* It's still in use, nothing else to do here. */
  144. return 0;
  145. }
  146. /* No hits, we are good to free it. */
  147. kfree(old);
  148. return 0;
  149. }
  150. static int sctp_sched_prio_get(struct sctp_stream *stream, __u16 sid,
  151. __u16 *value)
  152. {
  153. *value = SCTP_SO(stream, sid)->ext->prio_head->prio;
  154. return 0;
  155. }
  156. static int sctp_sched_prio_init(struct sctp_stream *stream)
  157. {
  158. INIT_LIST_HEAD(&stream->prio_list);
  159. return 0;
  160. }
  161. static int sctp_sched_prio_init_sid(struct sctp_stream *stream, __u16 sid,
  162. gfp_t gfp)
  163. {
  164. INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->prio_list);
  165. return sctp_sched_prio_set(stream, sid, 0, gfp);
  166. }
  167. static void sctp_sched_prio_free(struct sctp_stream *stream)
  168. {
  169. struct sctp_stream_priorities *prio, *n;
  170. LIST_HEAD(list);
  171. int i;
  172. /* As we don't keep a list of priorities, to avoid multiple
  173. * frees we have to do it in 3 steps:
  174. * 1. unsched everyone, so the lists are free to use in 2.
  175. * 2. build the list of the priorities
  176. * 3. free the list
  177. */
  178. sctp_sched_prio_unsched_all(stream);
  179. for (i = 0; i < stream->outcnt; i++) {
  180. if (!SCTP_SO(stream, i)->ext)
  181. continue;
  182. prio = SCTP_SO(stream, i)->ext->prio_head;
  183. if (prio && list_empty(&prio->prio_sched))
  184. list_add(&prio->prio_sched, &list);
  185. }
  186. list_for_each_entry_safe(prio, n, &list, prio_sched) {
  187. list_del_init(&prio->prio_sched);
  188. kfree(prio);
  189. }
  190. }
  191. static void sctp_sched_prio_enqueue(struct sctp_outq *q,
  192. struct sctp_datamsg *msg)
  193. {
  194. struct sctp_stream *stream;
  195. struct sctp_chunk *ch;
  196. __u16 sid;
  197. ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
  198. sid = sctp_chunk_stream_no(ch);
  199. stream = &q->asoc->stream;
  200. sctp_sched_prio_sched(stream, SCTP_SO(stream, sid)->ext);
  201. }
  202. static struct sctp_chunk *sctp_sched_prio_dequeue(struct sctp_outq *q)
  203. {
  204. struct sctp_stream *stream = &q->asoc->stream;
  205. struct sctp_stream_priorities *prio;
  206. struct sctp_stream_out_ext *soute;
  207. struct sctp_chunk *ch = NULL;
  208. /* Bail out quickly if queue is empty */
  209. if (list_empty(&q->out_chunk_list))
  210. goto out;
  211. /* Find which chunk is next. It's easy, it's either the current
  212. * one or the first chunk on the next active stream.
  213. */
  214. if (stream->out_curr) {
  215. soute = stream->out_curr->ext;
  216. } else {
  217. prio = list_entry(stream->prio_list.next,
  218. struct sctp_stream_priorities, prio_sched);
  219. soute = prio->next;
  220. }
  221. ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
  222. sctp_sched_dequeue_common(q, ch);
  223. out:
  224. return ch;
  225. }
  226. static void sctp_sched_prio_dequeue_done(struct sctp_outq *q,
  227. struct sctp_chunk *ch)
  228. {
  229. struct sctp_stream_priorities *prio;
  230. struct sctp_stream_out_ext *soute;
  231. __u16 sid;
  232. /* Last chunk on that msg, move to the next stream on
  233. * this priority.
  234. */
  235. sid = sctp_chunk_stream_no(ch);
  236. soute = SCTP_SO(&q->asoc->stream, sid)->ext;
  237. prio = soute->prio_head;
  238. sctp_sched_prio_next_stream(prio);
  239. if (list_empty(&soute->outq))
  240. sctp_sched_prio_unsched(soute);
  241. }
  242. static void sctp_sched_prio_sched_all(struct sctp_stream *stream)
  243. {
  244. struct sctp_association *asoc;
  245. struct sctp_stream_out *sout;
  246. struct sctp_chunk *ch;
  247. asoc = container_of(stream, struct sctp_association, stream);
  248. list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
  249. __u16 sid;
  250. sid = sctp_chunk_stream_no(ch);
  251. sout = SCTP_SO(stream, sid);
  252. if (sout->ext)
  253. sctp_sched_prio_sched(stream, sout->ext);
  254. }
  255. }
  256. static void sctp_sched_prio_unsched_all(struct sctp_stream *stream)
  257. {
  258. struct sctp_stream_priorities *p, *tmp;
  259. struct sctp_stream_out_ext *soute, *souttmp;
  260. list_for_each_entry_safe(p, tmp, &stream->prio_list, prio_sched)
  261. list_for_each_entry_safe(soute, souttmp, &p->active, prio_list)
  262. sctp_sched_prio_unsched(soute);
  263. }
  264. static struct sctp_sched_ops sctp_sched_prio = {
  265. .set = sctp_sched_prio_set,
  266. .get = sctp_sched_prio_get,
  267. .init = sctp_sched_prio_init,
  268. .init_sid = sctp_sched_prio_init_sid,
  269. .free = sctp_sched_prio_free,
  270. .enqueue = sctp_sched_prio_enqueue,
  271. .dequeue = sctp_sched_prio_dequeue,
  272. .dequeue_done = sctp_sched_prio_dequeue_done,
  273. .sched_all = sctp_sched_prio_sched_all,
  274. .unsched_all = sctp_sched_prio_unsched_all,
  275. };
  276. void sctp_sched_ops_prio_init(void)
  277. {
  278. sctp_sched_ops_register(SCTP_SS_PRIO, &sctp_sched_prio);
  279. }