PacketPassPriorityQueue.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @file PacketPassPriorityQueue.h
  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. * @section DESCRIPTION
  23. *
  24. * Priority queue using {@link PacketPassInterface}.
  25. */
  26. #ifndef BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  27. #define BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  28. #include <stdint.h>
  29. #include <misc/dead.h>
  30. #include <misc/debugin.h>
  31. #include <system/DebugObject.h>
  32. #include <misc/debugcounter.h>
  33. #include <structure/BHeap.h>
  34. #include <system/BPending.h>
  35. #include <flow/PacketPassInterface.h>
  36. typedef void (*PacketPassPriorityQueue_handler_busy) (void *user);
  37. struct PacketPassPriorityQueueFlow_s;
  38. /**
  39. * Priority queue using {@link PacketPassInterface}.
  40. */
  41. typedef struct {
  42. dead_t dead;
  43. PacketPassInterface *output;
  44. struct PacketPassPriorityQueueFlow_s *sending_flow;
  45. int sending_len;
  46. BHeap queued_heap;
  47. int freeing;
  48. int use_cancel;
  49. BPending continue_job;
  50. DebugCounter d_ctr;
  51. DebugIn in_output;
  52. DebugObject d_obj;
  53. } PacketPassPriorityQueue;
  54. typedef struct PacketPassPriorityQueueFlow_s {
  55. dead_t dead;
  56. PacketPassPriorityQueue *m;
  57. PacketPassPriorityQueue_handler_busy handler_busy;
  58. void *user;
  59. PacketPassInterface input;
  60. int priority;
  61. int is_queued;
  62. struct {
  63. BHeapNode heap_node;
  64. uint8_t *data;
  65. int data_len;
  66. } queued;
  67. DebugObject d_obj;
  68. } PacketPassPriorityQueueFlow;
  69. /**
  70. * Initializes the queue.
  71. *
  72. * @param m the object
  73. * @param output output interface
  74. * @param pg pending group
  75. */
  76. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg);
  77. /**
  78. * Frees the queue.
  79. * All flows must have been freed.
  80. *
  81. * @param m the object
  82. */
  83. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m);
  84. /**
  85. * Enables cancel functionality.
  86. * This allows freeing flows even if they're busy by releasing them.
  87. * Output must support {@link PacketPassInterface} cancel functionality.
  88. * May only be called once.
  89. */
  90. void PacketPassPriorityQueue_EnableCancel (PacketPassPriorityQueue *m);
  91. /**
  92. * Prepares for freeing the entire queue. Must be called to allow freeing
  93. * the flows in the process of freeing the entire queue.
  94. * After this function is called, flows and the queue must be freed
  95. * before any further I/O.
  96. * May be called multiple times.
  97. * The queue enters freeing state.
  98. *
  99. * @param m the object
  100. */
  101. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m);
  102. /**
  103. * Initializes a queue flow.
  104. * Queue must not be in freeing state.
  105. * Must not be called from queue calls to output.
  106. *
  107. * @param flow the object
  108. * @param m queue to attach to
  109. * @param priority flow priority. Lower value means higher priority.
  110. */
  111. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority);
  112. /**
  113. * Frees a queue flow.
  114. * Unless the queue is in freeing state:
  115. * - The flow must not be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  116. * - Must not be called from queue calls to output.
  117. *
  118. * @param flow the object
  119. */
  120. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow);
  121. /**
  122. * Determines if the flow is busy. If the flow is considered busy, it must not
  123. * be freed.
  124. * Queue must not be in freeing state.
  125. * Must not be called from queue calls to output.
  126. *
  127. * @param flow the object
  128. * @return 0 if not busy, 1 is busy
  129. */
  130. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow);
  131. /**
  132. * Cancels the packet that is currently being sent to output in order
  133. * to allow freeing the flow.
  134. * Cancel functionality must be enabled for the queue.
  135. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  136. * Queue must not be in freeing state.
  137. * Must not be called from queue calls to output.
  138. * Will call Cancel on output. Will not invoke any input I/O.
  139. * After this, {@link PacketPassPriorityQueueFlow_IsBusy} will report the flow as not busy.
  140. * The flow's input's Done will never be called (the flow will become inoperable).
  141. *
  142. * @param flow the object
  143. */
  144. void PacketPassPriorityQueueFlow_Release (PacketPassPriorityQueueFlow *flow);
  145. /**
  146. * Sets up a callback to be called when the flow is no longer busy.
  147. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  148. * Queue must not be in freeing state.
  149. * Must not be called from queue calls to output.
  150. *
  151. * @param flow the object
  152. * @param handler callback function. NULL to disable.
  153. * @param user value passed to callback function. Ignored if handler is NULL.
  154. */
  155. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user);
  156. /**
  157. * Returns the input interface of the flow.
  158. *
  159. * @param flow the object
  160. * @return input interface
  161. */
  162. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow);
  163. #endif