PacketPassFairQueue.h 6.8 KB

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