forked from XTLS/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketPassPriorityQueue.c
283 lines (222 loc) · 8.16 KB
/
PacketPassPriorityQueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @file PacketPassPriorityQueue.c
* @author Ambroz Bizjak <[email protected]>
*
* @section LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <misc/debug.h>
#include <misc/offset.h>
#include <misc/compare.h>
#include <flow/PacketPassPriorityQueue.h>
static int compare_flows (PacketPassPriorityQueueFlow *f1, PacketPassPriorityQueueFlow *f2)
{
int cmp = B_COMPARE(f1->priority, f2->priority);
if (cmp) {
return cmp;
}
return B_COMPARE((uintptr_t)f1, (uintptr_t)f2);
}
#include "PacketPassPriorityQueue_tree.h"
#include <structure/SAvl_impl.h>
static void schedule (PacketPassPriorityQueue *m)
{
ASSERT(!m->sending_flow)
ASSERT(!m->freeing)
ASSERT(!PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree))
// get first queued flow
PacketPassPriorityQueueFlow *qflow = PacketPassPriorityQueue__Tree_GetFirst(&m->queued_tree, 0);
ASSERT(qflow->is_queued)
// remove flow from queue
PacketPassPriorityQueue__Tree_Remove(&m->queued_tree, 0, qflow);
qflow->is_queued = 0;
// schedule send
PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
m->sending_flow = qflow;
}
static void schedule_job_handler (PacketPassPriorityQueue *m)
{
ASSERT(!m->sending_flow)
ASSERT(!m->freeing)
DebugObject_Access(&m->d_obj);
if (!PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree)) {
schedule(m);
}
}
static void input_handler_send (PacketPassPriorityQueueFlow *flow, uint8_t *data, int data_len)
{
PacketPassPriorityQueue *m = flow->m;
ASSERT(flow != m->sending_flow)
ASSERT(!flow->is_queued)
ASSERT(!m->freeing)
DebugObject_Access(&flow->d_obj);
// queue flow
flow->queued.data = data;
flow->queued.data_len = data_len;
int res = PacketPassPriorityQueue__Tree_Insert(&m->queued_tree, 0, flow, NULL);
ASSERT_EXECUTE(res)
flow->is_queued = 1;
if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
schedule(m);
}
}
static void output_handler_done (PacketPassPriorityQueue *m)
{
ASSERT(m->sending_flow)
ASSERT(!BPending_IsSet(&m->schedule_job))
ASSERT(!m->freeing)
ASSERT(!m->sending_flow->is_queued)
PacketPassPriorityQueueFlow *flow = m->sending_flow;
// sending finished
m->sending_flow = NULL;
// schedule schedule
BPending_Set(&m->schedule_job);
// finish flow packet
PacketPassInterface_Done(&flow->input);
// call busy handler if set
if (flow->handler_busy) {
// handler is one-shot, unset it before calling
PacketPassPriorityQueue_handler_busy handler = flow->handler_busy;
flow->handler_busy = NULL;
// call handler
handler(flow->user);
return;
}
}
void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel)
{
ASSERT(use_cancel == 0 || use_cancel == 1)
ASSERT(!use_cancel || PacketPassInterface_HasCancel(output))
// init arguments
m->output = output;
m->pg = pg;
m->use_cancel = use_cancel;
// init output
PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
// not sending
m->sending_flow = NULL;
// init queued tree
PacketPassPriorityQueue__Tree_Init(&m->queued_tree);
// not freeing
m->freeing = 0;
// init schedule job
BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
DebugObject_Init(&m->d_obj);
DebugCounter_Init(&m->d_ctr);
}
void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m)
{
ASSERT(PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree))
ASSERT(!m->sending_flow)
DebugCounter_Free(&m->d_ctr);
DebugObject_Free(&m->d_obj);
// free schedule job
BPending_Free(&m->schedule_job);
}
void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m)
{
DebugObject_Access(&m->d_obj);
// set freeing
m->freeing = 1;
}
int PacketPassPriorityQueue_GetMTU (PacketPassPriorityQueue *m)
{
DebugObject_Access(&m->d_obj);
return PacketPassInterface_GetMTU(m->output);
}
void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority)
{
ASSERT(!m->freeing)
DebugObject_Access(&m->d_obj);
// init arguments
flow->m = m;
flow->priority = priority;
// have no canfree handler
flow->handler_busy = NULL;
// init input
PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
// is not queued
flow->is_queued = 0;
DebugObject_Init(&flow->d_obj);
DebugCounter_Increment(&m->d_ctr);
}
void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow)
{
PacketPassPriorityQueue *m = flow->m;
ASSERT(m->freeing || flow != m->sending_flow)
DebugCounter_Decrement(&m->d_ctr);
DebugObject_Free(&flow->d_obj);
// remove from current flow
if (flow == m->sending_flow) {
m->sending_flow = NULL;
}
// remove from queue
if (flow->is_queued) {
PacketPassPriorityQueue__Tree_Remove(&m->queued_tree, 0, flow);
}
// free input
PacketPassInterface_Free(&flow->input);
}
void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow)
{
PacketPassPriorityQueue *m = flow->m;
B_USE(m)
ASSERT(m->freeing || flow != m->sending_flow)
DebugObject_Access(&flow->d_obj);
}
int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow)
{
PacketPassPriorityQueue *m = flow->m;
ASSERT(!m->freeing)
DebugObject_Access(&flow->d_obj);
return (flow == m->sending_flow);
}
void PacketPassPriorityQueueFlow_RequestCancel (PacketPassPriorityQueueFlow *flow)
{
PacketPassPriorityQueue *m = flow->m;
ASSERT(flow == m->sending_flow)
ASSERT(m->use_cancel)
ASSERT(!m->freeing)
ASSERT(!BPending_IsSet(&m->schedule_job))
DebugObject_Access(&flow->d_obj);
// request cancel
PacketPassInterface_Sender_RequestCancel(m->output);
}
void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user)
{
PacketPassPriorityQueue *m = flow->m;
B_USE(m)
ASSERT(flow == m->sending_flow)
ASSERT(!m->freeing)
DebugObject_Access(&flow->d_obj);
// set handler
flow->handler_busy = handler;
flow->user = user;
}
PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow)
{
DebugObject_Access(&flow->d_obj);
return &flow->input;
}