PacketPassFairQueue.c 11 KB

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