PacketPassFairQueue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @file PacketPassFairQueue.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <misc/debug.h>
  24. #include <misc/offset.h>
  25. #include <misc/minmax.h>
  26. #include <flow/PacketPassFairQueue.h>
  27. // reduce this to test time overflow handling
  28. #define FAIRQUEUE_MAX_TIME UINT64_MAX
  29. static int time_comparator (void *user, uint64_t *time1, uint64_t *time2)
  30. {
  31. if (*time1 < *time2) {
  32. return -1;
  33. }
  34. if (*time1 > *time2) {
  35. return 1;
  36. }
  37. return 0;
  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, int iamount)
  62. {
  63. ASSERT(iamount >= 0)
  64. ASSERT(iamount <= FAIRQUEUE_MAX_TIME)
  65. ASSERT(!flow->is_queued)
  66. ASSERT(!flow->m->sending_flow)
  67. PacketPassFairQueue *m = flow->m;
  68. uint64_t amount = iamount;
  69. // does time overflow?
  70. if (amount > FAIRQUEUE_MAX_TIME - flow->time) {
  71. // get time to subtract
  72. uint64_t subtract;
  73. BHeapNode *heap_node = BHeap_GetFirst(&m->queued_heap);
  74. if (!heap_node) {
  75. subtract = flow->time;
  76. } else {
  77. PacketPassFairQueueFlow *first_flow = UPPER_OBJECT(heap_node, PacketPassFairQueueFlow, queued.heap_node);
  78. ASSERT(first_flow->is_queued)
  79. subtract = first_flow->time;
  80. }
  81. // subtract time from all flows
  82. LinkedList2Iterator it;
  83. LinkedList2Iterator_InitForward(&it, &m->flows_list);
  84. LinkedList2Node *list_node;
  85. while (list_node = LinkedList2Iterator_Next(&it)) {
  86. PacketPassFairQueueFlow *someflow = UPPER_OBJECT(list_node, PacketPassFairQueueFlow, list_node);
  87. // don't subtract more time than there is, except for the just finished flow,
  88. // where we allow time to underflow and then overflow to the correct value after adding to it
  89. if (subtract > someflow->time && someflow != flow) {
  90. ASSERT(!someflow->is_queued)
  91. someflow->time = 0;
  92. } else {
  93. someflow->time -= subtract;
  94. }
  95. }
  96. }
  97. // add time to flow
  98. flow->time += amount;
  99. }
  100. static void schedule (PacketPassFairQueue *m)
  101. {
  102. ASSERT(!m->freeing)
  103. ASSERT(!m->sending_flow)
  104. ASSERT(!m->previous_flow)
  105. ASSERT(BHeap_GetFirst(&m->queued_heap))
  106. // get first queued flow
  107. BHeapNode *heap_node = BHeap_GetFirst(&m->queued_heap);
  108. PacketPassFairQueueFlow *qflow = UPPER_OBJECT(heap_node, PacketPassFairQueueFlow, queued.heap_node);
  109. ASSERT(qflow->is_queued)
  110. // remove flow from queue
  111. BHeap_Remove(&m->queued_heap, &qflow->queued.heap_node);
  112. qflow->is_queued = 0;
  113. // schedule send
  114. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  115. m->sending_flow = qflow;
  116. m->sending_len = qflow->queued.data_len;
  117. }
  118. static void schedule_job_handler (PacketPassFairQueue *m)
  119. {
  120. ASSERT(!m->freeing)
  121. ASSERT(!m->sending_flow)
  122. DebugObject_Access(&m->d_obj);
  123. // remove previous flow
  124. m->previous_flow = NULL;
  125. // schedule next
  126. if (BHeap_GetFirst(&m->queued_heap)) {
  127. schedule(m);
  128. }
  129. }
  130. static void input_handler_send (PacketPassFairQueueFlow *flow, uint8_t *data, int data_len)
  131. {
  132. ASSERT(!flow->m->freeing)
  133. ASSERT(flow != flow->m->sending_flow)
  134. ASSERT(!flow->is_queued)
  135. DebugObject_Access(&flow->d_obj);
  136. PacketPassFairQueue *m = flow->m;
  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(flow->time, get_current_time(m));
  143. }
  144. // queue flow
  145. flow->queued.data = data;
  146. flow->queued.data_len = data_len;
  147. BHeap_Insert(&m->queued_heap, &flow->queued.heap_node);
  148. flow->is_queued = 1;
  149. if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
  150. schedule(m);
  151. }
  152. }
  153. static void output_handler_done (PacketPassFairQueue *m)
  154. {
  155. ASSERT(!m->freeing)
  156. ASSERT(m->sending_flow)
  157. ASSERT(!m->previous_flow)
  158. ASSERT(!m->sending_flow->is_queued)
  159. ASSERT(!BPending_IsSet(&m->schedule_job))
  160. PacketPassFairQueueFlow *flow = m->sending_flow;
  161. // sending finished
  162. m->sending_flow = NULL;
  163. // remember this flow so the schedule job can remove its time if it didn's send
  164. m->previous_flow = flow;
  165. // update flow time by packet size
  166. increment_sent_flow(flow, m->sending_len);
  167. // schedule schedule
  168. BPending_Set(&m->schedule_job);
  169. // finish flow packet
  170. PacketPassInterface_Done(&flow->input);
  171. // call busy handler if set
  172. if (flow->handler_busy) {
  173. // handler is one-shot, unset it before calling
  174. PacketPassFairQueue_handler_busy handler = flow->handler_busy;
  175. flow->handler_busy = NULL;
  176. // call handler
  177. handler(flow->user);
  178. return;
  179. }
  180. }
  181. void PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel)
  182. {
  183. ASSERT(use_cancel == 0 || use_cancel == 1)
  184. if (use_cancel) {
  185. ASSERT(PacketPassInterface_HasCancel(output))
  186. }
  187. // init arguments
  188. m->output = output;
  189. m->pg = pg;
  190. // init output
  191. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  192. // not sending
  193. m->sending_flow = NULL;
  194. // no previous flow
  195. m->previous_flow = NULL;
  196. // init queued heap
  197. BHeap_Init(&m->queued_heap, OFFSET_DIFF(PacketPassFairQueueFlow, time, queued.heap_node), (BHeap_comparator)time_comparator, NULL);
  198. // init flows list
  199. LinkedList2_Init(&m->flows_list);
  200. // not freeing
  201. m->freeing = 0;
  202. // set if using cancel
  203. m->use_cancel = use_cancel;
  204. // init schedule job
  205. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  206. DebugCounter_Init(&m->d_ctr);
  207. DebugObject_Init(&m->d_obj);
  208. }
  209. void PacketPassFairQueue_Free (PacketPassFairQueue *m)
  210. {
  211. ASSERT(LinkedList2_IsEmpty(&m->flows_list))
  212. ASSERT(!BHeap_GetFirst(&m->queued_heap))
  213. ASSERT(!m->previous_flow)
  214. ASSERT(!m->sending_flow)
  215. DebugCounter_Free(&m->d_ctr);
  216. DebugObject_Free(&m->d_obj);
  217. // free schedule job
  218. BPending_Free(&m->schedule_job);
  219. }
  220. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m)
  221. {
  222. DebugObject_Access(&m->d_obj);
  223. m->freeing = 1;
  224. }
  225. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m)
  226. {
  227. ASSERT(!m->freeing)
  228. DebugObject_Access(&m->d_obj);
  229. // init arguments
  230. flow->m = m;
  231. // have no canfree handler
  232. flow->handler_busy = NULL;
  233. // init input
  234. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  235. // set time
  236. flow->time = 0;
  237. // add to flows list
  238. LinkedList2_Append(&m->flows_list, &flow->list_node);
  239. // is not queued
  240. flow->is_queued = 0;
  241. DebugCounter_Increment(&m->d_ctr);
  242. DebugObject_Init(&flow->d_obj);
  243. }
  244. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow)
  245. {
  246. if (!flow->m->freeing) {
  247. ASSERT(flow != flow->m->sending_flow)
  248. }
  249. DebugCounter_Decrement(&flow->m->d_ctr);
  250. DebugObject_Free(&flow->d_obj);
  251. PacketPassFairQueue *m = flow->m;
  252. // remove from current flow
  253. if (flow == m->sending_flow) {
  254. m->sending_flow = NULL;
  255. }
  256. // remove from previous flow
  257. if (flow == m->previous_flow) {
  258. m->previous_flow = NULL;
  259. }
  260. // remove from queue
  261. if (flow->is_queued) {
  262. BHeap_Remove(&m->queued_heap, &flow->queued.heap_node);
  263. }
  264. // remove from flows list
  265. LinkedList2_Remove(&m->flows_list, &flow->list_node);
  266. // free input
  267. PacketPassInterface_Free(&flow->input);
  268. }
  269. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow)
  270. {
  271. if (!flow->m->freeing) {
  272. ASSERT(flow != flow->m->sending_flow)
  273. }
  274. DebugObject_Access(&flow->d_obj);
  275. }
  276. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow)
  277. {
  278. ASSERT(!flow->m->freeing)
  279. DebugObject_Access(&flow->d_obj);
  280. return (flow == flow->m->sending_flow);
  281. }
  282. void PacketPassFairQueueFlow_Release (PacketPassFairQueueFlow *flow)
  283. {
  284. ASSERT(flow->m->use_cancel)
  285. ASSERT(flow == flow->m->sending_flow)
  286. ASSERT(!flow->m->freeing)
  287. ASSERT(!BPending_IsSet(&flow->m->schedule_job))
  288. DebugObject_Access(&flow->d_obj);
  289. PacketPassFairQueue *m = flow->m;
  290. // cancel current packet
  291. PacketPassInterface_Sender_Cancel(m->output);
  292. // set no sending flow
  293. m->sending_flow = NULL;
  294. // schedule schedule
  295. BPending_Set(&m->schedule_job);
  296. }
  297. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user)
  298. {
  299. ASSERT(flow == flow->m->sending_flow)
  300. ASSERT(!flow->m->freeing)
  301. DebugObject_Access(&flow->d_obj);
  302. flow->handler_busy = handler;
  303. flow->user = user;
  304. }
  305. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow)
  306. {
  307. DebugObject_Access(&flow->d_obj);
  308. return &flow->input;
  309. }