PacketPassFairQueue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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->sending_flow)
  103. ASSERT(!m->previous_flow)
  104. ASSERT(!m->freeing)
  105. ASSERT(BHeap_GetFirst(&m->queued_heap))
  106. // get first queued flow
  107. PacketPassFairQueueFlow *qflow = UPPER_OBJECT(BHeap_GetFirst(&m->queued_heap), PacketPassFairQueueFlow, queued.heap_node);
  108. ASSERT(qflow->is_queued)
  109. // remove flow from queue
  110. BHeap_Remove(&m->queued_heap, &qflow->queued.heap_node);
  111. qflow->is_queued = 0;
  112. // schedule send
  113. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  114. m->sending_flow = qflow;
  115. m->sending_len = qflow->queued.data_len;
  116. }
  117. static void schedule_job_handler (PacketPassFairQueue *m)
  118. {
  119. ASSERT(!m->sending_flow)
  120. ASSERT(!m->freeing)
  121. DebugObject_Access(&m->d_obj);
  122. // remove previous flow
  123. m->previous_flow = NULL;
  124. if (BHeap_GetFirst(&m->queued_heap)) {
  125. schedule(m);
  126. }
  127. }
  128. static void input_handler_send (PacketPassFairQueueFlow *flow, uint8_t *data, int data_len)
  129. {
  130. PacketPassFairQueue *m = flow->m;
  131. ASSERT(flow != m->sending_flow)
  132. ASSERT(!flow->is_queued)
  133. ASSERT(!m->freeing)
  134. DebugObject_Access(&flow->d_obj);
  135. if (flow == m->previous_flow) {
  136. // remove from previous flow
  137. m->previous_flow = NULL;
  138. } else {
  139. // raise time
  140. flow->time = BMAX(flow->time, get_current_time(m));
  141. }
  142. // queue flow
  143. flow->queued.data = data;
  144. flow->queued.data_len = data_len;
  145. BHeap_Insert(&m->queued_heap, &flow->queued.heap_node);
  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, 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. void PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel)
  180. {
  181. ASSERT(PacketPassInterface_GetMTU(output) <= FAIRQUEUE_MAX_TIME)
  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. // init output
  189. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  190. // not sending
  191. m->sending_flow = NULL;
  192. // no previous flow
  193. m->previous_flow = NULL;
  194. // init queued heap
  195. BHeap_Init(&m->queued_heap, OFFSET_DIFF(PacketPassFairQueueFlow, time, queued.heap_node), (BHeap_comparator)time_comparator, NULL);
  196. // init flows list
  197. LinkedList2_Init(&m->flows_list);
  198. // not freeing
  199. m->freeing = 0;
  200. // init schedule job
  201. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  202. DebugObject_Init(&m->d_obj);
  203. DebugCounter_Init(&m->d_ctr);
  204. }
  205. void PacketPassFairQueue_Free (PacketPassFairQueue *m)
  206. {
  207. ASSERT(LinkedList2_IsEmpty(&m->flows_list))
  208. ASSERT(!BHeap_GetFirst(&m->queued_heap))
  209. ASSERT(!m->previous_flow)
  210. ASSERT(!m->sending_flow)
  211. DebugCounter_Free(&m->d_ctr);
  212. DebugObject_Free(&m->d_obj);
  213. // free schedule job
  214. BPending_Free(&m->schedule_job);
  215. }
  216. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m)
  217. {
  218. DebugObject_Access(&m->d_obj);
  219. // set freeing
  220. m->freeing = 1;
  221. }
  222. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m)
  223. {
  224. ASSERT(!m->freeing)
  225. DebugObject_Access(&m->d_obj);
  226. // init arguments
  227. flow->m = m;
  228. // have no canfree handler
  229. flow->handler_busy = NULL;
  230. // init input
  231. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  232. // set time
  233. flow->time = 0;
  234. // add to flows list
  235. LinkedList2_Append(&m->flows_list, &flow->list_node);
  236. // is not queued
  237. flow->is_queued = 0;
  238. DebugObject_Init(&flow->d_obj);
  239. DebugCounter_Increment(&m->d_ctr);
  240. }
  241. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow)
  242. {
  243. PacketPassFairQueue *m = flow->m;
  244. ASSERT(m->freeing || flow != m->sending_flow)
  245. DebugCounter_Decrement(&m->d_ctr);
  246. DebugObject_Free(&flow->d_obj);
  247. // remove from current flow
  248. if (flow == m->sending_flow) {
  249. m->sending_flow = NULL;
  250. }
  251. // remove from previous flow
  252. if (flow == m->previous_flow) {
  253. m->previous_flow = NULL;
  254. }
  255. // remove from queue
  256. if (flow->is_queued) {
  257. BHeap_Remove(&m->queued_heap, &flow->queued.heap_node);
  258. }
  259. // remove from flows list
  260. LinkedList2_Remove(&m->flows_list, &flow->list_node);
  261. // free input
  262. PacketPassInterface_Free(&flow->input);
  263. }
  264. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow)
  265. {
  266. PacketPassFairQueue *m = flow->m;
  267. ASSERT(m->freeing || flow != m->sending_flow)
  268. DebugObject_Access(&flow->d_obj);
  269. }
  270. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow)
  271. {
  272. PacketPassFairQueue *m = flow->m;
  273. ASSERT(!m->freeing)
  274. DebugObject_Access(&flow->d_obj);
  275. return (flow == m->sending_flow);
  276. }
  277. void PacketPassFairQueueFlow_Release (PacketPassFairQueueFlow *flow)
  278. {
  279. PacketPassFairQueue *m = flow->m;
  280. ASSERT(flow == m->sending_flow)
  281. ASSERT(m->use_cancel)
  282. ASSERT(!m->freeing)
  283. ASSERT(!BPending_IsSet(&m->schedule_job))
  284. DebugObject_Access(&flow->d_obj);
  285. // set no sending flow
  286. m->sending_flow = NULL;
  287. // schedule schedule
  288. BPending_Set(&m->schedule_job);
  289. // cancel current packet
  290. PacketPassInterface_Sender_Cancel(m->output);
  291. }
  292. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user)
  293. {
  294. PacketPassFairQueue *m = flow->m;
  295. ASSERT(flow == m->sending_flow)
  296. ASSERT(!m->freeing)
  297. DebugObject_Access(&flow->d_obj);
  298. // set handler
  299. flow->handler_busy = handler;
  300. flow->user = user;
  301. }
  302. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow)
  303. {
  304. DebugObject_Access(&flow->d_obj);
  305. return &flow->input;
  306. }