PacketPassFairQueue.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file PacketPassFairQueue.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. * Fair queue using {@link PacketPassInterface}.
  25. */
  26. #ifndef BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
  27. #define BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
  28. #include <stdint.h>
  29. #include <misc/debugcounter.h>
  30. #include <structure/BHeap.h>
  31. #include <structure/LinkedList2.h>
  32. #include <system/DebugObject.h>
  33. #include <system/BPending.h>
  34. #include <flow/PacketPassInterface.h>
  35. // reduce this to test time overflow handling
  36. #define FAIRQUEUE_MAX_TIME UINT64_MAX
  37. typedef void (*PacketPassFairQueue_handler_busy) (void *user);
  38. struct PacketPassFairQueueFlow_s;
  39. /**
  40. * Fair queue using {@link PacketPassInterface}.
  41. */
  42. typedef struct {
  43. PacketPassInterface *output;
  44. BPendingGroup *pg;
  45. int use_cancel;
  46. int packet_weight;
  47. struct PacketPassFairQueueFlow_s *sending_flow;
  48. int sending_len;
  49. struct PacketPassFairQueueFlow_s *previous_flow;
  50. BHeap queued_heap;
  51. LinkedList2 flows_list;
  52. int freeing;
  53. BPending schedule_job;
  54. DebugObject d_obj;
  55. DebugCounter d_ctr;
  56. } PacketPassFairQueue;
  57. typedef struct PacketPassFairQueueFlow_s {
  58. PacketPassFairQueue *m;
  59. PacketPassFairQueue_handler_busy handler_busy;
  60. void *user;
  61. PacketPassInterface input;
  62. uint64_t time;
  63. LinkedList2Node list_node;
  64. int is_queued;
  65. struct {
  66. BHeapNode heap_node;
  67. uint8_t *data;
  68. int data_len;
  69. } queued;
  70. DebugObject d_obj;
  71. } PacketPassFairQueueFlow;
  72. /**
  73. * Initializes the queue.
  74. * (output MTU + packet_weight <= FAIRQUEUE_MAX_TIME) must hold.
  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. * @param packet_weight additional weight a packet bears. Must be >0, to keep
  82. * the queue fair for zero size packets.
  83. */
  84. void PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel, int packet_weight);
  85. /**
  86. * Frees the queue.
  87. * All flows must have been freed.
  88. *
  89. * @param m the object
  90. */
  91. void PacketPassFairQueue_Free (PacketPassFairQueue *m);
  92. /**
  93. * Prepares for freeing the entire queue. Must be called to allow freeing
  94. * the flows in the process of freeing the entire queue.
  95. * After this function is called, flows and the queue must be freed
  96. * before any further I/O.
  97. * May be called multiple times.
  98. * The queue enters freeing state.
  99. *
  100. * @param m the object
  101. */
  102. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m);
  103. /**
  104. * Initializes a queue flow.
  105. * Queue must not be in freeing state.
  106. * Must not be called from queue calls to output.
  107. *
  108. * @param flow the object
  109. * @param m queue to attach to
  110. */
  111. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m);
  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 PacketPassFairQueueFlow_IsBusy}.
  116. * - Must not be called from queue calls to output.
  117. *
  118. * @param flow the object
  119. */
  120. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow);
  121. /**
  122. * Does nothing.
  123. * It must be possible to free the flow (see {@link PacketPassFairQueueFlow_Free}).
  124. *
  125. * @param flow the object
  126. */
  127. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow);
  128. /**
  129. * Determines if the flow is busy. If the flow is considered busy, it must not
  130. * be freed. At any given time, at most one flow will be indicated as busy.
  131. * Queue must not be in freeing state.
  132. * Must not be called from queue calls to output.
  133. *
  134. * @param flow the object
  135. * @return 0 if not busy, 1 is busy
  136. */
  137. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow);
  138. /**
  139. * Cancels the packet that is currently being sent to output in order
  140. * to allow freeing the flow.
  141. * Cancel functionality must be enabled for the queue.
  142. * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  143. * Queue must not be in freeing state.
  144. * Must not be called from queue calls to output.
  145. * Will call Cancel on output. Will not invoke any input I/O.
  146. * After this, {@link PacketPassFairQueueFlow_IsBusy} will report the flow as not busy.
  147. * The flow's input's Done will never be called (the flow will become inoperable).
  148. *
  149. * @param flow the object
  150. */
  151. void PacketPassFairQueueFlow_Release (PacketPassFairQueueFlow *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 PacketPassFairQueueFlow_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 PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_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 * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow);
  172. #endif