PacketPassPriorityQueue.h 6.5 KB

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