PacketPassFairQueue.h 5.4 KB

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