PacketPassFairQueue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 time_comparator (void *user, uint64_t *time1, uint64_t *time2)
  36. {
  37. return B_COMPARE(*time1, *time2);
  38. }
  39. static uint64_t get_current_time (PacketPassFairQueue *m)
  40. {
  41. if (m->sending_flow) {
  42. return m->sending_flow->time;
  43. }
  44. uint64_t time;
  45. int have = 0;
  46. BHeapNode *heap_node = BHeap_GetFirst(&m->queued_heap);
  47. if (heap_node) {
  48. PacketPassFairQueueFlow *first_flow = UPPER_OBJECT(heap_node, PacketPassFairQueueFlow, queued.heap_node);
  49. ASSERT(first_flow->is_queued)
  50. time = first_flow->time;
  51. have = 1;
  52. }
  53. if (m->previous_flow) {
  54. if (!have || m->previous_flow->time < time) {
  55. time = m->previous_flow->time;
  56. have = 1;
  57. }
  58. }
  59. return (have ? time : 0);
  60. }
  61. static void increment_sent_flow (PacketPassFairQueueFlow *flow, uint64_t amount)
  62. {
  63. PacketPassFairQueue *m = flow->m;
  64. ASSERT(amount <= FAIRQUEUE_MAX_TIME)
  65. ASSERT(!flow->is_queued)
  66. ASSERT(!m->sending_flow)
  67. // does time overflow?
  68. if (amount > FAIRQUEUE_MAX_TIME - flow->time) {
  69. // get time to subtract
  70. uint64_t subtract;
  71. BHeapNode *heap_node = BHeap_GetFirst(&m->queued_heap);
  72. if (!heap_node) {
  73. subtract = flow->time;
  74. } else {
  75. PacketPassFairQueueFlow *first_flow = UPPER_OBJECT(heap_node, PacketPassFairQueueFlow, queued.heap_node);
  76. ASSERT(first_flow->is_queued)
  77. subtract = first_flow->time;
  78. }
  79. // subtract time from all flows
  80. LinkedList2Iterator it;
  81. LinkedList2Iterator_InitForward(&it, &m->flows_list);
  82. LinkedList2Node *list_node;
  83. while (list_node = LinkedList2Iterator_Next(&it)) {
  84. PacketPassFairQueueFlow *someflow = UPPER_OBJECT(list_node, PacketPassFairQueueFlow, list_node);
  85. // don't subtract more time than there is, except for the just finished flow,
  86. // where we allow time to underflow and then overflow to the correct value after adding to it
  87. if (subtract > someflow->time && someflow != flow) {
  88. ASSERT(!someflow->is_queued)
  89. someflow->time = 0;
  90. } else {
  91. someflow->time -= subtract;
  92. }
  93. }
  94. }
  95. // add time to flow
  96. flow->time += amount;
  97. }
  98. static void schedule (PacketPassFairQueue *m)
  99. {
  100. ASSERT(!m->sending_flow)
  101. ASSERT(!m->previous_flow)
  102. ASSERT(!m->freeing)
  103. ASSERT(BHeap_GetFirst(&m->queued_heap))
  104. // get first queued flow
  105. PacketPassFairQueueFlow *qflow = UPPER_OBJECT(BHeap_GetFirst(&m->queued_heap), PacketPassFairQueueFlow, queued.heap_node);
  106. ASSERT(qflow->is_queued)
  107. // remove flow from queue
  108. BHeap_Remove(&m->queued_heap, &qflow->queued.heap_node);
  109. qflow->is_queued = 0;
  110. // schedule send
  111. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  112. m->sending_flow = qflow;
  113. m->sending_len = qflow->queued.data_len;
  114. }
  115. static void schedule_job_handler (PacketPassFairQueue *m)
  116. {
  117. ASSERT(!m->sending_flow)
  118. ASSERT(!m->freeing)
  119. DebugObject_Access(&m->d_obj);
  120. // remove previous flow
  121. m->previous_flow = NULL;
  122. if (BHeap_GetFirst(&m->queued_heap)) {
  123. schedule(m);
  124. }
  125. }
  126. static void input_handler_send (PacketPassFairQueueFlow *flow, uint8_t *data, int data_len)
  127. {
  128. PacketPassFairQueue *m = flow->m;
  129. ASSERT(flow != m->sending_flow)
  130. ASSERT(!flow->is_queued)
  131. ASSERT(!m->freeing)
  132. DebugObject_Access(&flow->d_obj);
  133. if (flow == m->previous_flow) {
  134. // remove from previous flow
  135. m->previous_flow = NULL;
  136. } else {
  137. // raise time
  138. flow->time = bmax_uint64(flow->time, get_current_time(m));
  139. }
  140. // queue flow
  141. flow->queued.data = data;
  142. flow->queued.data_len = data_len;
  143. BHeap_Insert(&m->queued_heap, &flow->queued.heap_node);
  144. flow->is_queued = 1;
  145. if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
  146. schedule(m);
  147. }
  148. }
  149. static void output_handler_done (PacketPassFairQueue *m)
  150. {
  151. ASSERT(m->sending_flow)
  152. ASSERT(!m->previous_flow)
  153. ASSERT(!BPending_IsSet(&m->schedule_job))
  154. ASSERT(!m->freeing)
  155. ASSERT(!m->sending_flow->is_queued)
  156. PacketPassFairQueueFlow *flow = m->sending_flow;
  157. // sending finished
  158. m->sending_flow = NULL;
  159. // remember this flow so the schedule job can remove its time if it didn's send
  160. m->previous_flow = flow;
  161. // update flow time by packet size
  162. increment_sent_flow(flow, (uint64_t)m->packet_weight + m->sending_len);
  163. // schedule schedule
  164. BPending_Set(&m->schedule_job);
  165. // finish flow packet
  166. PacketPassInterface_Done(&flow->input);
  167. // call busy handler if set
  168. if (flow->handler_busy) {
  169. // handler is one-shot, unset it before calling
  170. PacketPassFairQueue_handler_busy handler = flow->handler_busy;
  171. flow->handler_busy = NULL;
  172. // call handler
  173. handler(flow->user);
  174. return;
  175. }
  176. }
  177. int PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel, int packet_weight)
  178. {
  179. ASSERT(packet_weight > 0)
  180. ASSERT(use_cancel == 0 || use_cancel == 1)
  181. ASSERT(!use_cancel || PacketPassInterface_HasCancel(output))
  182. // init arguments
  183. m->output = output;
  184. m->pg = pg;
  185. m->use_cancel = use_cancel;
  186. m->packet_weight = packet_weight;
  187. // make sure that (output MTU + packet_weight <= FAIRQUEUE_MAX_TIME)
  188. if (!(
  189. (PacketPassInterface_GetMTU(output) <= FAIRQUEUE_MAX_TIME) &&
  190. (packet_weight <= FAIRQUEUE_MAX_TIME - PacketPassInterface_GetMTU(output))
  191. )) {
  192. goto fail0;
  193. }
  194. // init output
  195. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  196. // not sending
  197. m->sending_flow = NULL;
  198. // no previous flow
  199. m->previous_flow = NULL;
  200. // init queued heap
  201. BHeap_Init(&m->queued_heap, OFFSET_DIFF(PacketPassFairQueueFlow, time, queued.heap_node), (BHeap_comparator)time_comparator, NULL);
  202. // init flows list
  203. LinkedList2_Init(&m->flows_list);
  204. // not freeing
  205. m->freeing = 0;
  206. // init schedule job
  207. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  208. DebugObject_Init(&m->d_obj);
  209. DebugCounter_Init(&m->d_ctr);
  210. return 1;
  211. fail0:
  212. return 0;
  213. }
  214. void PacketPassFairQueue_Free (PacketPassFairQueue *m)
  215. {
  216. ASSERT(LinkedList2_IsEmpty(&m->flows_list))
  217. ASSERT(!BHeap_GetFirst(&m->queued_heap))
  218. ASSERT(!m->previous_flow)
  219. ASSERT(!m->sending_flow)
  220. DebugCounter_Free(&m->d_ctr);
  221. DebugObject_Free(&m->d_obj);
  222. // free schedule job
  223. BPending_Free(&m->schedule_job);
  224. }
  225. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m)
  226. {
  227. DebugObject_Access(&m->d_obj);
  228. // set freeing
  229. m->freeing = 1;
  230. }
  231. int PacketPassFairQueue_GetMTU (PacketPassFairQueue *m)
  232. {
  233. DebugObject_Access(&m->d_obj);
  234. return PacketPassInterface_GetMTU(m->output);
  235. }
  236. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m)
  237. {
  238. ASSERT(!m->freeing)
  239. DebugObject_Access(&m->d_obj);
  240. // init arguments
  241. flow->m = m;
  242. // have no canfree handler
  243. flow->handler_busy = NULL;
  244. // init input
  245. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  246. // set time
  247. flow->time = 0;
  248. // add to flows list
  249. LinkedList2_Append(&m->flows_list, &flow->list_node);
  250. // is not queued
  251. flow->is_queued = 0;
  252. DebugObject_Init(&flow->d_obj);
  253. DebugCounter_Increment(&m->d_ctr);
  254. }
  255. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow)
  256. {
  257. PacketPassFairQueue *m = flow->m;
  258. ASSERT(m->freeing || flow != m->sending_flow)
  259. DebugCounter_Decrement(&m->d_ctr);
  260. DebugObject_Free(&flow->d_obj);
  261. // remove from current flow
  262. if (flow == m->sending_flow) {
  263. m->sending_flow = NULL;
  264. }
  265. // remove from previous flow
  266. if (flow == m->previous_flow) {
  267. m->previous_flow = NULL;
  268. }
  269. // remove from queue
  270. if (flow->is_queued) {
  271. BHeap_Remove(&m->queued_heap, &flow->queued.heap_node);
  272. }
  273. // remove from flows list
  274. LinkedList2_Remove(&m->flows_list, &flow->list_node);
  275. // free input
  276. PacketPassInterface_Free(&flow->input);
  277. }
  278. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow)
  279. {
  280. PacketPassFairQueue *m = flow->m;
  281. ASSERT(m->freeing || flow != m->sending_flow)
  282. DebugObject_Access(&flow->d_obj);
  283. }
  284. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow)
  285. {
  286. PacketPassFairQueue *m = flow->m;
  287. ASSERT(!m->freeing)
  288. DebugObject_Access(&flow->d_obj);
  289. return (flow == m->sending_flow);
  290. }
  291. void PacketPassFairQueueFlow_RequestCancel (PacketPassFairQueueFlow *flow)
  292. {
  293. PacketPassFairQueue *m = flow->m;
  294. ASSERT(flow == m->sending_flow)
  295. ASSERT(m->use_cancel)
  296. ASSERT(!m->freeing)
  297. ASSERT(!BPending_IsSet(&m->schedule_job))
  298. DebugObject_Access(&flow->d_obj);
  299. // request cancel
  300. PacketPassInterface_Sender_RequestCancel(m->output);
  301. }
  302. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user)
  303. {
  304. PacketPassFairQueue *m = flow->m;
  305. ASSERT(flow == m->sending_flow)
  306. ASSERT(!m->freeing)
  307. DebugObject_Access(&flow->d_obj);
  308. // set handler
  309. flow->handler_busy = handler;
  310. flow->user = user;
  311. }
  312. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow)
  313. {
  314. DebugObject_Access(&flow->d_obj);
  315. return &flow->input;
  316. }