BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketPassFairQueue.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Fair queue using {@link PacketPassInterface}.
32 */
33  
34 #ifndef BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
35 #define BADVPN_FLOW_PACKETPASSFAIRQUEUE_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debug.h>
40 #include <misc/debugcounter.h>
41 #include <structure/SAvl.h>
42 #include <structure/LinkedList1.h>
43 #include <base/DebugObject.h>
44 #include <base/BPending.h>
45 #include <flow/PacketPassInterface.h>
46  
47 // reduce this to test time overflow handling
48 #define FAIRQUEUE_MAX_TIME UINT64_MAX
49  
50 typedef void (*PacketPassFairQueue_handler_busy) (void *user);
51  
52 struct PacketPassFairQueueFlow_s;
53  
54 #include "PacketPassFairQueue_tree.h"
55 #include <structure/SAvl_decl.h>
56  
57 typedef struct PacketPassFairQueueFlow_s {
58 struct PacketPassFairQueue_s *m;
59 PacketPassFairQueue_handler_busy handler_busy;
60 void *user;
61 PacketPassInterface input;
62 uint64_t time;
63 LinkedList1Node list_node;
64 int is_queued;
65 struct {
66 PacketPassFairQueue__TreeNode tree_node;
67 uint8_t *data;
68 int data_len;
69 } queued;
70 DebugObject d_obj;
71 } PacketPassFairQueueFlow;
72  
73 /**
74 * Fair queue using {@link PacketPassInterface}.
75 */
76 typedef struct PacketPassFairQueue_s {
77 PacketPassInterface *output;
78 BPendingGroup *pg;
79 int use_cancel;
80 int packet_weight;
81 struct PacketPassFairQueueFlow_s *sending_flow;
82 int sending_len;
83 struct PacketPassFairQueueFlow_s *previous_flow;
84 PacketPassFairQueue__Tree queued_tree;
85 LinkedList1 flows_list;
86 int freeing;
87 BPending schedule_job;
88 DebugObject d_obj;
89 DebugCounter d_ctr;
90 } PacketPassFairQueue;
91  
92 /**
93 * Initializes the queue.
94 *
95 * @param m the object
96 * @param output output interface
97 * @param pg pending group
98 * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
99 * If 1, output must support cancel functionality.
100 * @param packet_weight additional weight a packet bears. Must be >0, to keep
101 * the queue fair for zero size packets.
102 * @return 1 on success, 0 on failure (because output MTU is too large)
103 */
104 int PacketPassFairQueue_Init (PacketPassFairQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel, int packet_weight) WARN_UNUSED;
105  
106 /**
107 * Frees the queue.
108 * All flows must have been freed.
109 *
110 * @param m the object
111 */
112 void PacketPassFairQueue_Free (PacketPassFairQueue *m);
113  
114 /**
115 * Prepares for freeing the entire queue. Must be called to allow freeing
116 * the flows in the process of freeing the entire queue.
117 * After this function is called, flows and the queue must be freed
118 * before any further I/O.
119 * May be called multiple times.
120 * The queue enters freeing state.
121 *
122 * @param m the object
123 */
124 void PacketPassFairQueue_PrepareFree (PacketPassFairQueue *m);
125  
126 /**
127 * Returns the MTU of the queue.
128 *
129 * @param m the object
130 */
131 int PacketPassFairQueue_GetMTU (PacketPassFairQueue *m);
132  
133 /**
134 * Initializes a queue flow.
135 * Queue must not be in freeing state.
136 * Must not be called from queue calls to output.
137 *
138 * @param flow the object
139 * @param m queue to attach to
140 */
141 void PacketPassFairQueueFlow_Init (PacketPassFairQueueFlow *flow, PacketPassFairQueue *m);
142  
143 /**
144 * Frees a queue flow.
145 * Unless the queue is in freeing state:
146 * - The flow must not be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
147 * - Must not be called from queue calls to output.
148 *
149 * @param flow the object
150 */
151 void PacketPassFairQueueFlow_Free (PacketPassFairQueueFlow *flow);
152  
153 /**
154 * Does nothing.
155 * It must be possible to free the flow (see {@link PacketPassFairQueueFlow_Free}).
156 *
157 * @param flow the object
158 */
159 void PacketPassFairQueueFlow_AssertFree (PacketPassFairQueueFlow *flow);
160  
161 /**
162 * Determines if the flow is busy. If the flow is considered busy, it must not
163 * be freed. At any given time, at most one flow will be indicated as busy.
164 * Queue must not be in freeing state.
165 * Must not be called from queue calls to output.
166 *
167 * @param flow the object
168 * @return 0 if not busy, 1 is busy
169 */
170 int PacketPassFairQueueFlow_IsBusy (PacketPassFairQueueFlow *flow);
171  
172 /**
173 * Requests the output to stop processing the current packet as soon as possible.
174 * Cancel functionality must be enabled for the queue.
175 * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
176 * Queue must not be in freeing state.
177 *
178 * @param flow the object
179 */
180 void PacketPassFairQueueFlow_RequestCancel (PacketPassFairQueueFlow *flow);
181  
182 /**
183 * Sets up a callback to be called when the flow is no longer busy.
184 * The handler will be called as soon as the flow is no longer busy, i.e. it is not
185 * possible that this flow is no longer busy before the handler is called.
186 * The flow must be busy as indicated by {@link PacketPassFairQueueFlow_IsBusy}.
187 * Queue must not be in freeing state.
188 * Must not be called from queue calls to output.
189 *
190 * @param flow the object
191 * @param handler callback function. NULL to disable.
192 * @param user value passed to callback function. Ignored if handler is NULL.
193 */
194 void PacketPassFairQueueFlow_SetBusyHandler (PacketPassFairQueueFlow *flow, PacketPassFairQueue_handler_busy handler, void *user);
195  
196 /**
197 * Returns the input interface of the flow.
198 *
199 * @param flow the object
200 * @return input interface
201 */
202 PacketPassInterface * PacketPassFairQueueFlow_GetInput (PacketPassFairQueueFlow *flow);
203  
204 #endif