PacketPassPriorityQueue.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @file PacketPassPriorityQueue.h
  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. * @section DESCRIPTION
  30. *
  31. * Priority queue using {@link PacketPassInterface}.
  32. */
  33. #ifndef BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  34. #define BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  35. #include <stdint.h>
  36. #include <misc/debugcounter.h>
  37. #include <structure/BHeap.h>
  38. #include <base/DebugObject.h>
  39. #include <base/BPending.h>
  40. #include <flow/PacketPassInterface.h>
  41. typedef void (*PacketPassPriorityQueue_handler_busy) (void *user);
  42. struct PacketPassPriorityQueueFlow_s;
  43. /**
  44. * Priority queue using {@link PacketPassInterface}.
  45. */
  46. typedef struct {
  47. PacketPassInterface *output;
  48. BPendingGroup *pg;
  49. int use_cancel;
  50. struct PacketPassPriorityQueueFlow_s *sending_flow;
  51. BHeap queued_heap;
  52. int freeing;
  53. BPending schedule_job;
  54. DebugObject d_obj;
  55. DebugCounter d_ctr;
  56. } PacketPassPriorityQueue;
  57. typedef struct PacketPassPriorityQueueFlow_s {
  58. PacketPassPriorityQueue *m;
  59. int priority;
  60. PacketPassPriorityQueue_handler_busy handler_busy;
  61. void *user;
  62. PacketPassInterface input;
  63. int is_queued;
  64. struct {
  65. BHeapNode heap_node;
  66. uint8_t *data;
  67. int data_len;
  68. } queued;
  69. DebugObject d_obj;
  70. } PacketPassPriorityQueueFlow;
  71. /**
  72. * Initializes the queue.
  73. *
  74. * @param m the object
  75. * @param output output interface
  76. * @param pg pending group
  77. * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
  78. * If 1, output must support cancel functionality.
  79. */
  80. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel);
  81. /**
  82. * Frees the queue.
  83. * All flows must have been freed.
  84. *
  85. * @param m the object
  86. */
  87. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m);
  88. /**
  89. * Prepares for freeing the entire queue. Must be called to allow freeing
  90. * the flows in the process of freeing the entire queue.
  91. * After this function is called, flows and the queue must be freed
  92. * before any further I/O.
  93. * May be called multiple times.
  94. * The queue enters freeing state.
  95. *
  96. * @param m the object
  97. */
  98. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m);
  99. /**
  100. * Returns the MTU of the queue.
  101. *
  102. * @param m the object
  103. */
  104. int PacketPassPriorityQueue_GetMTU (PacketPassPriorityQueue *m);
  105. /**
  106. * Initializes a queue flow.
  107. * Queue must not be in freeing state.
  108. * Must not be called from queue calls to output.
  109. *
  110. * @param flow the object
  111. * @param m queue to attach to
  112. * @param priority flow priority. Lower value means higher priority.
  113. */
  114. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority);
  115. /**
  116. * Frees a queue flow.
  117. * Unless the queue is in freeing state:
  118. * - The flow must not be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  119. * - Must not be called from queue calls to output.
  120. *
  121. * @param flow the object
  122. */
  123. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow);
  124. /**
  125. * Does nothing.
  126. * It must be possible to free the flow (see {@link PacketPassPriorityQueueFlow}).
  127. *
  128. * @param flow the object
  129. */
  130. void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow);
  131. /**
  132. * Determines if the flow is busy. If the flow is considered busy, it must not
  133. * be freed. At any given time, at most one flow will be indicated as busy.
  134. * Queue must not be in freeing state.
  135. * Must not be called from queue calls to output.
  136. *
  137. * @param flow the object
  138. * @return 0 if not busy, 1 is busy
  139. */
  140. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow);
  141. /**
  142. * Requests the output to stop processing the current packet as soon as possible.
  143. * Cancel functionality must be enabled for the queue.
  144. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  145. * Queue must not be in freeing state.
  146. *
  147. * @param flow the object
  148. */
  149. void PacketPassPriorityQueueFlow_RequestCancel (PacketPassPriorityQueueFlow *flow);
  150. /**
  151. * Sets up a callback to be called when the flow is no longer busy.
  152. * The handler will be called as soon as the flow is no longer busy, i.e. it is not
  153. * possible that this flow is no longer busy before the handler is called.
  154. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  155. * Queue must not be in freeing state.
  156. * Must not be called from queue calls to output.
  157. *
  158. * @param flow the object
  159. * @param handler callback function. NULL to disable.
  160. * @param user value passed to callback function. Ignored if handler is NULL.
  161. */
  162. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user);
  163. /**
  164. * Returns the input interface of the flow.
  165. *
  166. * @param flow the object
  167. * @return input interface
  168. */
  169. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow);
  170. #endif