PacketPassFairQueue.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <system/DebugObject.h>
  30. #include <system/BPending.h>
  31. #include <misc/debugcounter.h>
  32. #include <structure/BHeap.h>
  33. #include <structure/LinkedList2.h>
  34. #include <flow/PacketPassInterface.h>
  35. typedef void (*PacketPassFairQueue_handler_busy) (void *user);
  36. struct PacketPassFairQueueFlow_s;
  37. /**
  38. * Fair queue using {@link PacketPassInterface}.
  39. */
  40. typedef struct {
  41. PacketPassInterface *output;
  42. struct PacketPassFairQueueFlow_s *sending_flow;
  43. int sending_len;
  44. struct PacketPassFairQueueFlow_s *previous_flow;
  45. BHeap queued_heap;
  46. LinkedList2 queued_list;
  47. int freeing;
  48. int use_cancel;
  49. BPending schedule_job;
  50. BPendingGroup *pg;
  51. DebugCounter d_ctr;
  52. DebugObject d_obj;
  53. } PacketPassFairQueue;
  54. typedef struct PacketPassFairQueueFlow_s {
  55. PacketPassFairQueue *m;
  56. PacketPassFairQueue_handler_busy handler_busy;
  57. void *user;
  58. PacketPassInterface input;
  59. uint64_t time;
  60. int is_queued;
  61. struct {
  62. BHeapNode heap_node;
  63. LinkedList2Node list_node;
  64. uint8_t *data;
  65. int data_len;
  66. } queued;
  67. DebugObject d_obj;
  68. } PacketPassFairQueueFlow;
  69. /**
  70. * Initializes the queue.
  71. *
  72. * @param m the object
  73. * @param output output interface
  74. * @param pg pending group
  75. * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
  76. * If 1, output must support cancel functionality.
  77. */
  78. void PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel);
  79. /**
  80. * Frees the queue.
  81. * All flows must have been freed.
  82. *
  83. * @param m the object
  84. */
  85. void PacketPassFairQueue_Free (PacketPassFairQueue *m);
  86. /**
  87. * Prepares for freeing the entire queue. Must be called to allow freeing
  88. * the flows in the process of freeing the entire queue.
  89. * After this function is called, flows and the queue must be freed
  90. * before any further I/O.
  91. * May be called multiple times.
  92. * The queue enters freeing state.
  93. *
  94. * @param m the object
  95. */
  96. void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m);
  97. /**
  98. * Initializes a queue flow.
  99. * Queue must not be in freeing state.
  100. * Must not be called from queue calls to output.
  101. *
  102. * @param flow the object
  103. * @param m queue to attach to
  104. */
  105. void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m);
  106. /**
  107. * Frees a queue flow.
  108. * Unless the queue is in freeing state:
  109. * - The flow must not be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  110. * - Must not be called from queue calls to output.
  111. *
  112. * @param flow the object
  113. */
  114. void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow);
  115. /**
  116. * Does nothing.
  117. * It must be possible to free the flow (see {@link PacketPassFairQueueFlow_Free}).
  118. *
  119. * @param flow the object
  120. */
  121. void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow);
  122. /**
  123. * Determines if the flow is busy. If the flow is considered busy, it must not
  124. * be freed.
  125. * Queue must not be in freeing state.
  126. * Must not be called from queue calls to output.
  127. *
  128. * @param flow the object
  129. * @return 0 if not busy, 1 is busy
  130. */
  131. int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow);
  132. /**
  133. * Cancels the packet that is currently being sent to output in order
  134. * to allow freeing the flow.
  135. * Cancel functionality must be enabled for the queue.
  136. * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  137. * Queue must not be in freeing state.
  138. * Must not be called from queue calls to output.
  139. * Will call Cancel on output. Will not invoke any input I/O.
  140. * After this, {@link PacketPassFairQueueFlow_IsBusy} will report the flow as not busy.
  141. * The flow's input's Done will never be called (the flow will become inoperable).
  142. *
  143. * @param flow the object
  144. */
  145. void PacketPassFairQueueFlow_Release (PacketPassFairQueueFlow *flow);
  146. /**
  147. * Sets up a callback to be called when the flow is no longer busy.
  148. * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
  149. * Queue must not be in freeing state.
  150. * Must not be called from queue calls to output.
  151. *
  152. * @param flow the object
  153. * @param handler callback function. NULL to disable.
  154. * @param user value passed to callback function. Ignored if handler is NULL.
  155. */
  156. void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user);
  157. /**
  158. * Returns the input interface of the flow.
  159. *
  160. * @param flow the object
  161. * @return input interface
  162. */
  163. PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow);
  164. #endif