PacketPassPriorityQueue.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * @file PacketPassPriorityQueue.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 <flow/PacketPassPriorityQueue.h>
  26. static int call_send (PacketPassPriorityQueue *m, uint8_t *data, int data_len)
  27. {
  28. DebugIn_GoIn(&m->in_output);
  29. DEAD_ENTER(m->dead)
  30. int res = PacketPassInterface_Sender_Send(m->output, data, data_len);
  31. if (DEAD_LEAVE(m->dead)) {
  32. return -1;
  33. }
  34. DebugIn_GoOut(&m->in_output);
  35. ASSERT(!m->freeing)
  36. ASSERT(res == 0 || res == 1)
  37. return res;
  38. }
  39. static int call_cancel (PacketPassPriorityQueue *m)
  40. {
  41. DebugIn_GoIn(&m->in_output);
  42. DEAD_ENTER(m->dead)
  43. PacketPassInterface_Sender_Cancel(m->output);
  44. if (DEAD_LEAVE(m->dead)) {
  45. return -1;
  46. }
  47. DebugIn_GoOut(&m->in_output);
  48. ASSERT(!m->freeing)
  49. return 0;
  50. }
  51. static int call_done (PacketPassPriorityQueue *m, PacketPassPriorityQueueFlow *flow)
  52. {
  53. DEAD_ENTER(m->dead)
  54. PacketPassInterface_Done(&flow->input);
  55. if (DEAD_LEAVE(m->dead)) {
  56. return -1;
  57. }
  58. ASSERT(!m->freeing)
  59. return 0;
  60. }
  61. static void process_queue (PacketPassPriorityQueue *m)
  62. {
  63. ASSERT(!m->freeing)
  64. ASSERT(!m->sending_flow)
  65. do {
  66. // get first queued flow
  67. BHeapNode *heap_node = BHeap_GetFirst(&m->queued_heap);
  68. if (!heap_node) {
  69. return;
  70. }
  71. PacketPassPriorityQueueFlow *qflow = UPPER_OBJECT(heap_node, PacketPassPriorityQueueFlow, queued.heap_node);
  72. ASSERT(qflow->is_queued)
  73. // remove flow from queue
  74. BHeap_Remove(&m->queued_heap, &qflow->queued.heap_node);
  75. qflow->is_queued = 0;
  76. // try to send the packet
  77. int res = call_send(m, qflow->queued.data, qflow->queued.data_len);
  78. if (res < 0) {
  79. return;
  80. }
  81. if (res == 0) {
  82. // sending in progress
  83. m->sending_flow = qflow;
  84. m->sending_len = qflow->queued.data_len;
  85. return;
  86. }
  87. // notify sender
  88. if (call_done(m, qflow) < 0) {
  89. return;
  90. }
  91. } while (!m->sending_flow);
  92. }
  93. static int int_comparator (void *user, int *prio1, int *prio2)
  94. {
  95. if (*prio1 < *prio2) {
  96. return -1;
  97. }
  98. if (*prio1 > *prio2) {
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. static int input_handler_send (PacketPassPriorityQueueFlow *flow, uint8_t *data, int data_len)
  104. {
  105. ASSERT(!flow->m->freeing)
  106. ASSERT(flow != flow->m->sending_flow)
  107. ASSERT(!flow->is_queued)
  108. DebugIn_AmOut(&flow->m->in_output);
  109. PacketPassPriorityQueue *m = flow->m;
  110. // if nothing is being sent and queue is empty, send immediately without queueing
  111. if (!m->sending_flow && !BHeap_GetFirst(&m->queued_heap)) {
  112. int res = call_send(m, data, data_len);
  113. if (res < 0) {
  114. return -1;
  115. }
  116. if (res == 0) {
  117. // output busy, continue in output_handler_done
  118. m->sending_flow = flow;
  119. m->sending_len = data_len;
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. // add flow to queue
  125. flow->queued.data = data;
  126. flow->queued.data_len = data_len;
  127. BHeap_Insert(&m->queued_heap, &flow->queued.heap_node);
  128. flow->is_queued = 1;
  129. return 0;
  130. }
  131. static void output_handler_done (PacketPassPriorityQueue *m)
  132. {
  133. ASSERT(!m->freeing)
  134. ASSERT(m->sending_flow)
  135. ASSERT(!m->sending_flow->is_queued)
  136. DebugIn_AmOut(&m->in_output);
  137. PacketPassPriorityQueueFlow *flow = m->sending_flow;
  138. // sending finished
  139. m->sending_flow = NULL;
  140. // call busy handler if set
  141. if (flow->handler_busy) {
  142. // handler is one-shot, unset it before calling
  143. PacketPassPriorityQueue_handler_busy handler = flow->handler_busy;
  144. flow->handler_busy = NULL;
  145. // call handler
  146. DEAD_ENTER_N(m, m->dead)
  147. DEAD_ENTER_N(flow, flow->dead)
  148. handler(flow->user);
  149. DEAD_LEAVE_N(m, m->dead);
  150. DEAD_LEAVE_N(flow, flow->dead);
  151. if (DEAD_KILLED_N(m)) {
  152. return;
  153. }
  154. if (DEAD_KILLED_N(flow)) {
  155. flow = NULL;
  156. }
  157. ASSERT(!m->freeing)
  158. }
  159. // report completion to sender
  160. if (flow) {
  161. if (call_done(m, flow) < 0) {
  162. return;
  163. }
  164. }
  165. // process queued flows
  166. if (!m->sending_flow) {
  167. process_queue(m);
  168. return;
  169. }
  170. }
  171. static void job_handler (PacketPassPriorityQueue *m)
  172. {
  173. ASSERT(!m->freeing)
  174. if (!m->sending_flow) {
  175. process_queue(m);
  176. return;
  177. }
  178. }
  179. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg)
  180. {
  181. // init arguments
  182. m->output = output;
  183. // init dead var
  184. DEAD_INIT(m->dead);
  185. // init output
  186. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  187. // not sending
  188. m->sending_flow = NULL;
  189. // init queued heap
  190. BHeap_Init(&m->queued_heap, OFFSET_DIFF(PacketPassPriorityQueueFlow, priority, queued.heap_node), (BHeap_comparator)int_comparator, NULL);
  191. // not freeing
  192. m->freeing = 0;
  193. // not using cancel
  194. m->use_cancel = 0;
  195. // init continue job
  196. BPending_Init(&m->continue_job, pg, (BPending_handler)job_handler, m);
  197. // init debug counter
  198. DebugCounter_Init(&m->d_ctr);
  199. // init debug in output
  200. DebugIn_Init(&m->in_output);
  201. // init debug object
  202. DebugObject_Init(&m->d_obj);
  203. }
  204. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m)
  205. {
  206. ASSERT(!BHeap_GetFirst(&m->queued_heap))
  207. ASSERT(!m->sending_flow)
  208. DebugCounter_Free(&m->d_ctr);
  209. DebugObject_Free(&m->d_obj);
  210. // free continue job
  211. BPending_Free(&m->continue_job);
  212. // free dead var
  213. DEAD_KILL(m->dead);
  214. }
  215. void PacketPassPriorityQueue_EnableCancel (PacketPassPriorityQueue *m)
  216. {
  217. ASSERT(!m->use_cancel)
  218. ASSERT(PacketPassInterface_HasCancel(m->output))
  219. // using cancel
  220. m->use_cancel = 1;
  221. }
  222. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m)
  223. {
  224. m->freeing = 1;
  225. }
  226. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority)
  227. {
  228. ASSERT(!m->freeing)
  229. DebugIn_AmOut(&m->in_output);
  230. // init arguments
  231. flow->m = m;
  232. flow->priority = priority;
  233. // init dead var
  234. DEAD_INIT(flow->dead);
  235. // have no canfree handler
  236. flow->handler_busy = NULL;
  237. // init input
  238. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow);
  239. // is not queued
  240. flow->is_queued = 0;
  241. // increment debug counter
  242. DebugCounter_Increment(&m->d_ctr);
  243. // init debug object
  244. DebugObject_Init(&flow->d_obj);
  245. }
  246. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow)
  247. {
  248. if (!flow->m->freeing) {
  249. ASSERT(flow != flow->m->sending_flow)
  250. DebugIn_AmOut(&flow->m->in_output);
  251. }
  252. DebugCounter_Decrement(&flow->m->d_ctr);
  253. DebugObject_Free(&flow->d_obj);
  254. PacketPassPriorityQueue *m = flow->m;
  255. // remove current flow
  256. if (flow == flow->m->sending_flow) {
  257. flow->m->sending_flow = NULL;
  258. }
  259. // remove from queue
  260. if (flow->is_queued) {
  261. BHeap_Remove(&m->queued_heap, &flow->queued.heap_node);
  262. }
  263. // free input
  264. PacketPassInterface_Free(&flow->input);
  265. // free dead var
  266. DEAD_KILL(flow->dead);
  267. }
  268. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow)
  269. {
  270. ASSERT(!flow->m->freeing)
  271. DebugIn_AmOut(&flow->m->in_output);
  272. return (flow == flow->m->sending_flow);
  273. }
  274. void PacketPassPriorityQueueFlow_Release (PacketPassPriorityQueueFlow *flow)
  275. {
  276. ASSERT(flow->m->use_cancel)
  277. ASSERT(flow == flow->m->sending_flow)
  278. ASSERT(!flow->m->freeing)
  279. DebugIn_AmOut(&flow->m->in_output);
  280. PacketPassPriorityQueue *m = flow->m;
  281. // cancel current packet
  282. if (call_cancel(m) < 0) {
  283. return;
  284. }
  285. // set no sending flow
  286. m->sending_flow = NULL;
  287. // set continue job
  288. BPending_Set(&m->continue_job);
  289. }
  290. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user)
  291. {
  292. ASSERT(flow == flow->m->sending_flow)
  293. ASSERT(!flow->m->freeing)
  294. DebugIn_AmOut(&flow->m->in_output);
  295. flow->handler_busy = handler;
  296. flow->user = user;
  297. }
  298. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow)
  299. {
  300. return &flow->input;
  301. }