PacketPassFairQueue.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @file PacketPassFairQueue.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. * Fair queue using {@link PacketPassInterface}.
  32. */
  33. #ifndef BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
  34. #define BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. #include <misc/debugcounter.h>
  38. #include <structure/BHeap.h>
  39. #include <structure/LinkedList2.h>
  40. #include <base/DebugObject.h>
  41. #include <base/BPending.h>
  42. #include <flow/PacketPassInterface.h>
  43. // reduce this to test time overflow handling
  44. #define FAIRQUEUE_MAX_TIME UINT64_MAX
  45. typedef void (*PacketPassFairQueue_handler_busy) (void *user);
  46. struct PacketPassFairQueueFlow_s;
  47. /**
  48. * Fair queue using {@link PacketPassInterface}.
  49. */
  50. typedef struct {
  51. PacketPassInterface *output;
  52. BPendingGroup *pg;
  53. int use_cancel;
  54. int packet_weight;
  55. struct PacketPassFairQueueFlow_s *sending_flow;
  56. int sending_len;
  57. struct PacketPassFairQueueFlow_s *previous_flow;
  58. BHeap queued_heap;
  59. LinkedList2 flows_list;
  60. int freeing;
  61. BPending schedule_job;
  62. DebugObject d_obj;
  63. DebugCounter d_ctr;
  64. } PacketPassFairQueue;
  65. typedef struct PacketPassFairQueueFlow_s {
  66. PacketPassFairQueue *m;
  67. PacketPassFairQueue_handler_busy handler_busy;
  68. void *user;
  69. PacketPassInterface input;
  70. uint64_t time;
  71. LinkedList2Node list_node;
  72. int is_queued;
  73. struct {
  74. BHeapNode heap_node;
  75. uint8_t *data;
  76. int data_len;
  77. } queued;
  78. DebugObject d_obj;
  79. } PacketPassFairQueueFlow;
  80. /**
  81. * Initializes the queue.
  82. *
  83. * @param m the object
  84. * @param output output interface
  85. * @param pg pending group
  86. * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
  87. * If 1, output must support cancel functionality.
  88. * @param packet_weight additional weight a packet bears. Must be >0, to keep
  89. * the queue fair for zero size packets.
  90. * @return 1 on success, 0 on failure (because output MTU is too large)
  91. */
  92. int PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel, int packet_weight) WARN_UNUSED;
  93. /**
  94. * Frees the queue.
  95. * All flows must have been freed.
  96. *
  97. * @param m the object
  98. */
  99. void PacketPassFairQueue_Free (PacketPassFairQueue *m);
  100. /**
  101. * Prepares for freeing the entire queue. Must be called to allow freeing
  102. * the flows in the process of freeing the entire queue.
  103. * After this function is called, flows and the queue must be freed
  104. * before any further I/O.
  105. * May be called multiple times.
  106. * The queue enters freeing state.
  107. *
  108. * @param m the object
  109. */
  110. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m);
  111. /**
  112. * Returns the MTU of the queue.
  113. *
  114. * @param m the object
  115. */
  116. int PacketPassFairQueue_GetMTU (PacketPassFairQueue *m);
  117. /**
  118. * Initializes a queue flow.
  119. * Queue must not be in freeing state.
  120. * Must not be called from queue calls to output.
  121. *
  122. * @param flow the object
  123. * @param m queue to attach to
  124. */
  125. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m);
  126. /**
  127. * Frees a queue flow.
  128. * Unless the queue is in freeing state:
  129. * - The flow must not be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  130. * - Must not be called from queue calls to output.
  131. *
  132. * @param flow the object
  133. */
  134. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow);
  135. /**
  136. * Does nothing.
  137. * It must be possible to free the flow (see {@link PacketPassFairQueueFlow_Free}).
  138. *
  139. * @param flow the object
  140. */
  141. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow);
  142. /**
  143. * Determines if the flow is busy. If the flow is considered busy, it must not
  144. * be freed. At any given time, at most one flow will be indicated as busy.
  145. * Queue must not be in freeing state.
  146. * Must not be called from queue calls to output.
  147. *
  148. * @param flow the object
  149. * @return 0 if not busy, 1 is busy
  150. */
  151. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow);
  152. /**
  153. * Requests the output to stop processing the current packet as soon as possible.
  154. * Cancel functionality must be enabled for the queue.
  155. * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  156. * Queue must not be in freeing state.
  157. *
  158. * @param flow the object
  159. */
  160. void PacketPassFairQueueFlow_RequestCancel (PacketPassFairQueueFlow *flow);
  161. /**
  162. * Sets up a callback to be called when the flow is no longer busy.
  163. * The handler will be called as soon as the flow is no longer busy, i.e. it is not
  164. * possible that this flow is no longer busy before the handler is called.
  165. * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  166. * Queue must not be in freeing state.
  167. * Must not be called from queue calls to output.
  168. *
  169. * @param flow the object
  170. * @param handler callback function. NULL to disable.
  171. * @param user value passed to callback function. Ignored if handler is NULL.
  172. */
  173. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user);
  174. /**
  175. * Returns the input interface of the flow.
  176. *
  177. * @param flow the object
  178. * @return input interface
  179. */
  180. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow);
  181. #endif