PacketPassFairQueue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * @file PacketPassFairQueue.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <misc/debug.h>
  31. #include <misc/offset.h>
  32. #include <misc/minmax.h>
  33. #include <misc/compare.h>
  34. #include <flow/PacketPassFairQueue.h>
  35. static int compare_flows (PacketPassFairQueueFlow *f1, PacketPassFairQueueFlow *f2)
  36. {
  37. int cmp = B_COMPARE(f1->time, f2->time);
  38. if (cmp) {
  39. return cmp;
  40. }
  41. return B_COMPARE((uintptr_t)f1, (uintptr_t)f2);
  42. }
  43. #include "PacketPassFairQueue_tree.h"
  44. #include <structure/SAvl_impl.h>
  45. static uint64_t get_current_time (PacketPassFairQueue *m)
  46. {
  47. if (m->sending_flow) {
  48. return m->sending_flow->time;
  49. }
  50. uint64_t time;
  51. int have = 0;
  52. PacketPassFairQueueFlow *first_flow = PacketPassFairQueue__Tree_GetFirst(&m->queued_tree, 0);
  53. if (first_flow) {
  54. ASSERT(first_flow->is_queued)
  55. time = first_flow->time;
  56. have = 1;
  57. }
  58. if (m->previous_flow) {
  59. if (!have || m->previous_flow->time < time) {
  60. time = m->previous_flow->time;
  61. have = 1;
  62. }
  63. }
  64. return (have ? time : 0);
  65. }
  66. static void increment_sent_flow (PacketPassFairQueueFlow *flow, uint64_t amount)
  67. {
  68. PacketPassFairQueue *m = flow->m;
  69. ASSERT(amount <= FAIRQUEUE_MAX_TIME)
  70. ASSERT(!flow->is_queued)
  71. ASSERT(!m->sending_flow)
  72. // does time overflow?
  73. if (amount > FAIRQUEUE_MAX_TIME - flow->time) {
  74. // get time to subtract
  75. uint64_t subtract;
  76. PacketPassFairQueueFlow *first_flow = PacketPassFairQueue__Tree_GetFirst(&m->queued_tree, 0);
  77. if (!first_flow) {
  78. subtract = flow->time;
  79. } else {
  80. ASSERT(first_flow->is_queued)
  81. subtract = first_flow->time;
  82. }
  83. // subtract time from all flows
  84. LinkedList2Iterator it;
  85. LinkedList2Iterator_InitForward(&it, &m->flows_list);
  86. LinkedList2Node *list_node;
  87. while (list_node = LinkedList2Iterator_Next(&it)) {
  88. PacketPassFairQueueFlow *someflow = UPPER_OBJECT(list_node, PacketPassFairQueueFlow, list_node);
  89. // don't subtract more time than there is, except for the just finished flow,
  90. // where we allow time to underflow and then overflow to the correct value after adding to it
  91. if (subtract > someflow->time && someflow != flow) {
  92. ASSERT(!someflow->is_queued)
  93. someflow->time = 0;
  94. } else {
  95. someflow->time -= subtract;
  96. }
  97. }
  98. }
  99. // add time to flow
  100. flow->time += amount;
  101. }
  102. static void schedule (PacketPassFairQueue *m)
  103. {
  104. ASSERT(!m->sending_flow)
  105. ASSERT(!m->previous_flow)
  106. ASSERT(!m->freeing)
  107. ASSERT(!PacketPassFairQueue__Tree_IsEmpty(&m->queued_tree))
  108. // get first queued flow
  109. PacketPassFairQueueFlow *qflow = PacketPassFairQueue__Tree_GetFirst(&m->queued_tree, 0);
  110. ASSERT(qflow->is_queued)
  111. // remove flow from queue
  112. PacketPassFairQueue__Tree_Remove(&m->queued_tree, 0, qflow);
  113. qflow->is_queued = 0;
  114. // schedule send
  115. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  116. m->sending_flow = qflow;
  117. m->sending_len = qflow->queued.data_len;
  118. }
  119. static void schedule_job_handler (PacketPassFairQueue *m)
  120. {
  121. ASSERT(!m->sending_flow)
  122. ASSERT(!m->freeing)
  123. DebugObject_Access(&m->d_obj);
  124. // remove previous flow
  125. m->previous_flow = NULL;
  126. if (!PacketPassFairQueue__Tree_IsEmpty(&m->queued_tree)) {
  127. schedule(m);
  128. }
  129. }
  130. static void input_handler_send (PacketPassFairQueueFlow *flow, uint8_t *data, int data_len)
  131. {
  132. PacketPassFairQueue *m = flow->m;
  133. ASSERT(flow != m->sending_flow)
  134. ASSERT(!flow->is_queued)
  135. ASSERT(!m->freeing)
  136. DebugObject_Access(&flow->d_obj);
  137. if (flow == m->previous_flow) {
  138. // remove from previous flow
  139. m->previous_flow = NULL;
  140. } else {
  141. // raise time
  142. flow->time = bmax_uint64(flow->time, get_current_time(m));
  143. }
  144. // queue flow
  145. flow->queued.data = data;
  146. flow->queued.data_len = data_len;
  147. int res = PacketPassFairQueue__Tree_Insert(&m->queued_tree, 0, flow, NULL);
  148. ASSERT(res)
  149. flow->is_queued = 1;
  150. if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
  151. schedule(m);
  152. }
  153. }
  154. static void output_handler_done (PacketPassFairQueue *m)
  155. {
  156. ASSERT(m->sending_flow)
  157. ASSERT(!m->previous_flow)
  158. ASSERT(!BPending_IsSet(&m->schedule_job))
  159. ASSERT(!m->freeing)
  160. ASSERT(!m->sending_flow->is_queued)
  161. PacketPassFairQueueFlow *flow = m->sending_flow;
  162. // sending finished
  163. m->sending_flow = NULL;
  164. // remember this flow so the schedule job can remove its time if it didn's send
  165. m->previous_flow = flow;
  166. // update flow time by packet size
  167. increment_sent_flow(flow, (uint64_t)m->packet_weight + m->sending_len);
  168. // schedule schedule
  169. BPending_Set(&m->schedule_job);
  170. // finish flow packet
  171. PacketPassInterface_Done(&flow->input);
  172. // call busy handler if set
  173. if (flow->handler_busy) {
  174. // handler is one-shot, unset it before calling
  175. PacketPassFairQueue_handler_busy handler = flow->handler_busy;
  176. flow->handler_busy = NULL;
  177. // call handler
  178. handler(flow->user);
  179. return;
  180. }
  181. }
  182. int PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel, int packet_weight)
  183. {
  184. ASSERT(packet_weight > 0)
  185. ASSERT(use_cancel == 0 || use_cancel == 1)
  186. ASSERT(!use_cancel || PacketPassInterface_HasCancel(output))
  187. // init arguments
  188. m->output = output;
  189. m->pg = pg;
  190. m->use_cancel = use_cancel;
  191. m->packet_weight = packet_weight;
  192. // make sure that (output MTU + packet_weight <= FAIRQUEUE_MAX_TIME)
  193. if (!(
  194. (PacketPassInterface_GetMTU(output) <= FAIRQUEUE_MAX_TIME) &&
  195. (packet_weight <= FAIRQUEUE_MAX_TIME - PacketPassInterface_GetMTU(output))
  196. )) {
  197. goto fail0;
  198. }
  199. // init output
  200. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  201. // not sending
  202. m->sending_flow = NULL;
  203. // no previous flow
  204. m->previous_flow = NULL;
  205. // init queued tree
  206. PacketPassFairQueue__Tree_Init(&m->queued_tree);
  207. // init flows list
  208. LinkedList2_Init(&m->flows_list);
  209. // not freeing
  210. m->freeing = 0;
  211. // init schedule job
  212. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  213. DebugObject_Init(&m->d_obj);
  214. DebugCounter_Init(&m->d_ctr);
  215. return 1;
  216. fail0:
  217. return 0;
  218. }
  219. void PacketPassFairQueue_Free (PacketPassFairQueue *m)
  220. {
  221. ASSERT(LinkedList2_IsEmpty(&m->flows_list))
  222. ASSERT(PacketPassFairQueue__Tree_IsEmpty(&m->queued_tree))
  223. ASSERT(!m->previous_flow)
  224. ASSERT(!m->sending_flow)
  225. DebugCounter_Free(&m->d_ctr);
  226. DebugObject_Free(&m->d_obj);
  227. // free schedule job
  228. BPending_Free(&m->schedule_job);
  229. }
  230. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m)
  231. {
  232. DebugObject_Access(&m->d_obj);
  233. // set freeing
  234. m->freeing = 1;
  235. }
  236. int PacketPassFairQueue_GetMTU (PacketPassFairQueue *m)
  237. {
  238. DebugObject_Access(&m->d_obj);
  239. return PacketPassInterface_GetMTU(m->output);
  240. }
  241. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m)
  242. {
  243. ASSERT(!m->freeing)
  244. DebugObject_Access(&m->d_obj);
  245. // init arguments
  246. flow->m = m;
  247. // have no canfree handler
  248. flow->handler_busy = NULL;
  249. // init input
  250. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  251. // set time
  252. flow->time = 0;
  253. // add to flows list
  254. LinkedList2_Append(&m->flows_list, &flow->list_node);
  255. // is not queued
  256. flow->is_queued = 0;
  257. DebugObject_Init(&flow->d_obj);
  258. DebugCounter_Increment(&m->d_ctr);
  259. }
  260. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow)
  261. {
  262. PacketPassFairQueue *m = flow->m;
  263. ASSERT(m->freeing || flow != m->sending_flow)
  264. DebugCounter_Decrement(&m->d_ctr);
  265. DebugObject_Free(&flow->d_obj);
  266. // remove from current flow
  267. if (flow == m->sending_flow) {
  268. m->sending_flow = NULL;
  269. }
  270. // remove from previous flow
  271. if (flow == m->previous_flow) {
  272. m->previous_flow = NULL;
  273. }
  274. // remove from queue
  275. if (flow->is_queued) {
  276. PacketPassFairQueue__Tree_Remove(&m->queued_tree, 0, flow);
  277. }
  278. // remove from flows list
  279. LinkedList2_Remove(&m->flows_list, &flow->list_node);
  280. // free input
  281. PacketPassInterface_Free(&flow->input);
  282. }
  283. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow)
  284. {
  285. PacketPassFairQueue *m = flow->m;
  286. ASSERT(m->freeing || flow != m->sending_flow)
  287. DebugObject_Access(&flow->d_obj);
  288. }
  289. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow)
  290. {
  291. PacketPassFairQueue *m = flow->m;
  292. ASSERT(!m->freeing)
  293. DebugObject_Access(&flow->d_obj);
  294. return (flow == m->sending_flow);
  295. }
  296. void PacketPassFairQueueFlow_RequestCancel (PacketPassFairQueueFlow *flow)
  297. {
  298. PacketPassFairQueue *m = flow->m;
  299. ASSERT(flow == m->sending_flow)
  300. ASSERT(m->use_cancel)
  301. ASSERT(!m->freeing)
  302. ASSERT(!BPending_IsSet(&m->schedule_job))
  303. DebugObject_Access(&flow->d_obj);
  304. // request cancel
  305. PacketPassInterface_Sender_RequestCancel(m->output);
  306. }
  307. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user)
  308. {
  309. PacketPassFairQueue *m = flow->m;
  310. ASSERT(flow == m->sending_flow)
  311. ASSERT(!m->freeing)
  312. DebugObject_Access(&flow->d_obj);
  313. // set handler
  314. flow->handler_busy = handler;
  315. flow->user = user;
  316. }
  317. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow)
  318. {
  319. DebugObject_Access(&flow->d_obj);
  320. return &flow->input;
  321. }