stream_sched_prio.c 9.0 KB

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