PacketPassFairQueue.h 5.8 KB

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