PacketPassPriorityQueue.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @file PacketPassPriorityQueue.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. * Priority queue using {@link PacketPassInterface}.
  25. */
  26. #ifndef BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  27. #define BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  28. #include <stdint.h>
  29. #include <misc/debugcounter.h>
  30. #include <structure/BHeap.h>
  31. #include <system/DebugObject.h>
  32. #include <system/BPending.h>
  33. #include <flow/PacketPassInterface.h>
  34. typedef void (*PacketPassPriorityQueue_handler_busy) (void *user);
  35. struct PacketPassPriorityQueueFlow_s;
  36. /**
  37. * Priority queue using {@link PacketPassInterface}.
  38. */
  39. typedef struct {
  40. PacketPassInterface *output;
  41. BPendingGroup *pg;
  42. int use_cancel;
  43. struct PacketPassPriorityQueueFlow_s *sending_flow;
  44. BHeap queued_heap;
  45. int freeing;
  46. BPending schedule_job;
  47. DebugObject d_obj;
  48. DebugCounter d_ctr;
  49. } PacketPassPriorityQueue;
  50. typedef struct PacketPassPriorityQueueFlow_s {
  51. PacketPassPriorityQueue *m;
  52. int priority;
  53. PacketPassPriorityQueue_handler_busy handler_busy;
  54. void *user;
  55. PacketPassInterface input;
  56. int is_queued;
  57. struct {
  58. BHeapNode heap_node;
  59. uint8_t *data;
  60. int data_len;
  61. } queued;
  62. DebugObject d_obj;
  63. } PacketPassPriorityQueueFlow;
  64. /**
  65. * Initializes the queue.
  66. *
  67. * @param m the object
  68. * @param output output interface
  69. * @param pg pending group
  70. * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
  71. * If 1, output must support cancel functionality.
  72. */
  73. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel);
  74. /**
  75. * Frees the queue.
  76. * All flows must have been freed.
  77. *
  78. * @param m the object
  79. */
  80. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m);
  81. /**
  82. * Prepares for freeing the entire queue. Must be called to allow freeing
  83. * the flows in the process of freeing the entire queue.
  84. * After this function is called, flows and the queue must be freed
  85. * before any further I/O.
  86. * May be called multiple times.
  87. * The queue enters freeing state.
  88. *
  89. * @param m the object
  90. */
  91. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m);
  92. /**
  93. * Initializes a queue flow.
  94. * Queue must not be in freeing state.
  95. * Must not be called from queue calls to output.
  96. *
  97. * @param flow the object
  98. * @param m queue to attach to
  99. * @param priority flow priority. Lower value means higher priority.
  100. */
  101. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority);
  102. /**
  103. * Frees a queue flow.
  104. * Unless the queue is in freeing state:
  105. * - The flow must not be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  106. * - Must not be called from queue calls to output.
  107. *
  108. * @param flow the object
  109. */
  110. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow);
  111. /**
  112. * Does nothing.
  113. * It must be possible to free the flow (see {@link PacketPassPriorityQueueFlow}).
  114. *
  115. * @param flow the object
  116. */
  117. void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow);
  118. /**
  119. * Determines if the flow is busy. If the flow is considered busy, it must not
  120. * be freed. At any given time, at most one flow will be indicated as busy.
  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. * @return 0 if not busy, 1 is busy
  126. */
  127. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow);
  128. /**
  129. * Requests the output to stop processing the current packet as soon as possible.
  130. * Cancel functionality must be enabled for the queue.
  131. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  132. * Queue must not be in freeing state.
  133. *
  134. * @param flow the object
  135. */
  136. void PacketPassPriorityQueueFlow_RequestCancel (PacketPassPriorityQueueFlow *flow);
  137. /**
  138. * Sets up a callback to be called when the flow is no longer busy.
  139. * The handler will be called as soon as the flow is no longer busy, i.e. it is not
  140. * possible that this flow is no longer busy before the handler is called.
  141. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  142. * Queue must not be in freeing state.
  143. * Must not be called from queue calls to output.
  144. *
  145. * @param flow the object
  146. * @param handler callback function. NULL to disable.
  147. * @param user value passed to callback function. Ignored if handler is NULL.
  148. */
  149. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user);
  150. /**
  151. * Returns the input interface of the flow.
  152. *
  153. * @param flow the object
  154. * @return input interface
  155. */
  156. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow);
  157. #endif