stream_sched_prio.c 8.6 KB

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