-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtask_queue.c
More file actions
executable file
·382 lines (275 loc) · 6.85 KB
/
Copy pathtask_queue.c
File metadata and controls
executable file
·382 lines (275 loc) · 6.85 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/***************************************************************************
task_queue.c - description
-------------------
begin : Tue Jul 8 2003
copyright : (C) 2003 by Luke Klein-Berndt
email : kleinb@nist.gov
***************************************************************************/
/***************************************************************************
Modified by Miguel Catalan Cid - miguel.catcid@gmail.com - Version: Mon Jan 1 2010
***************************************************************************/
#include "task_queue.h"
//spinlock_t task_queue_lock = SPIN_LOCK_UNLOCKED;
DEFINE_SPINLOCK(task_queue_lock);
task *task_q;
task *task_end;
//sub queue--control tasks
task *sub_q;
task *sub_end;
//
void queue_lock(void) {
spin_lock_bh(&task_queue_lock);
}
void queue_unlock(void) {
spin_unlock_bh(&task_queue_lock);
}
void init_task_queue() {
task_q=NULL;
task_end=NULL;
sub_q=NULL;
sub_end=NULL;
}
void cleanup_task_queue() {
task *dead_task, *tmp_task;
queue_lock();
tmp_task = task_q;
task_q=NULL;
while (tmp_task) {
dead_task = tmp_task;
tmp_task = tmp_task->next;
kfree(dead_task->data);
kfree(dead_task);
}
queue_unlock();
}
task *create_task(int type) {
task *new_task;
new_task = (task *) kmalloc(sizeof(task), GFP_ATOMIC);
if (new_task == NULL) {
printk("Not enough memory to create Event Queue Entry\n");
return NULL;
}
new_task->time = getcurrtime();
new_task->type = type;
new_task->src_ip = 0;
new_task->dst_ip = 0;
new_task->ttl = 0;
new_task->retries = 0;
new_task->data = NULL;
new_task->data_len = 0;
new_task->next = NULL;
new_task->prev = NULL;
new_task->dev = NULL;
///
new_task->prev_control = NULL;
///
new_task->tos = 0;
return new_task;
}
int is_control_task(int type){
switch(type)
{
case TASK_ETT_INFO:
case TASK_SEND_ETT:
case TASK_RECV_S_ETT:
case TASK_RECV_L_ETT: return 0;
case TASK_HELLO:
case TASK_NEIGHBOR:
case TASK_CLEANUP:
case TASK_ROUTE_CLEANUP:
case TASK_RECV_RREQ:
case TASK_RESEND_RREQ:
case TASK_RECV_RERR:
case TASK_RECV_HELLO:
case TASK_RECV_RREP:
case TASK_SEND_RREP:
case TASK_ST:
case TASK_GW_CLEANUP:
case TASK_RECV_STRREQ:
case TASK_ETT_CLEANUP:
case TASK_NEIGHBOR_2H:
case TASK_UPDATE_LOAD:
case TASK_RECV_RCVP:
#ifdef RECOVERYPATH
case TASK_RECV_RRDP:
#endif
#ifdef DTN_HELLO
case TASK_DTN_HELLO:
#endif
case TASK_GEN_RREQ:
return 1;
default:return 0;
}
return 0;
}
int queue_aodv_task(task * new_entry) {
/*lock table */
queue_lock();
//Set all the variables
new_entry->next = task_q;
new_entry->prev = NULL;
if (task_q != NULL) {
task_q->prev = new_entry;
}
if (task_end == NULL) {
task_end = new_entry;
}
task_q = new_entry;
//add the new task already
if(is_control_task(new_entry->type)){
if(sub_q==NULL && sub_end==NULL){//the sub queue is empty
sub_q = new_entry;
sub_end = new_entry;
}
else{
sub_q->prev_control = new_entry;
sub_q = new_entry;
}
}
//unlock table
queue_unlock();
//wake up the AODV thread
kick_aodv();
return 0;
}
int queue_control_task_at_front(task * new_entry) {
/*lock table */
queue_lock();
//Set all the variables
new_entry->next = NULL;
new_entry->prev = task_end;
if (task_end != NULL) {
task_end->next = new_entry;
}
if (task_q == NULL) {
task_q = new_entry;
}
task_end = new_entry;
//add the new task already
if(is_control_task(new_entry->type)){
if(sub_q==NULL && sub_end==NULL){//the sub queue is empty
sub_q = new_entry;
sub_end = new_entry;
}
else{
new_entry->prev_control = sub_end;
sub_end = new_entry;
}
}
//unlock table
queue_unlock();
//wake up the AODV thread
kick_aodv();
return 0;
}
task *get_task() {
task *tmp_task = NULL;
task *prev;
task *next;
queue_lock();
//get control task first
if(sub_end){
tmp_task = sub_end;
if(sub_end == sub_q){//one element
if( (task_end==task_q) && (task_end==sub_end) ){
task_q = NULL;
task_end = NULL;
}
else if(task_end==sub_end){//at the end of queue & subqueue
task_end = task_end->prev;
}
else if(task_q==sub_q){//at the beginning of queue &subqueue
task_q = task_q->next;
task_q->prev = NULL;
}
else{//at some where in the queue
prev = tmp_task->prev;
next = tmp_task->next;
prev->next = next;
next->prev = prev;
}
sub_q = NULL;
sub_end = NULL;
}//if sub_end == sub_q
else{
if(task_end==sub_end){//at the end of queue & subqueue
task_end = task_end->prev;
}
else{//at some where in the queue
prev = tmp_task->prev;
next = tmp_task->next;
prev->next = next;
next->prev = prev;
}
sub_end = sub_end->prev_control;
}//if-else
queue_unlock();
return tmp_task;
}//if sub_end
//if no control task ,get the end of the queue
if (task_end) {
tmp_task = task_end;
if (task_end == task_q) {
task_q = NULL;
task_end = NULL;
} else {
task_end = task_end->prev;
}
queue_unlock();
return tmp_task;
}
if (task_q != NULL) {
printk("TASK_QUEUE: Error with task queue\n");
}
queue_unlock();
return NULL;
}
int insert_task(int type, struct sk_buff *packet) {
task *new_task;
struct iphdr *ip;
int start_point = sizeof(struct udphdr) + sizeof(struct iphdr);
new_task = create_task(type);
if (!new_task) {
printk("Not enough memory to create Task\n");
return -ENOMEM;
}
ip = ip_hdr(packet);
new_task->src_ip = ip->saddr;
new_task->dst_ip = ip->daddr;
new_task->ttl = ip->ttl;
new_task->dev = packet->dev;
new_task->data_len = packet->len - start_point;
//create space for the data and copy it there
new_task->data = kmalloc(new_task->data_len, GFP_ATOMIC);
if (!new_task->data) {
kfree(new_task);
printk("Not enough memory to create Event Queue Data Entry\n");
return -ENOMEM;
}
memcpy(new_task->data, packet->data + start_point, new_task->data_len);
queue_aodv_task(new_task);
return 0;
}
int insert_task_at_front(task * new_task) {
if (!new_task) {
printk("Passed a Null task Task\n");
return -ENOMEM;
}
queue_control_task_at_front(new_task);
return 0;
}
int insert_task_from_timer(task * timer_task) {
if (!timer_task) {
printk("Passed a Null task Task\n");
return -ENOMEM;
}
//the del neigh task should be managed first
//#ifdef CaiDebug
//printk(" Task type %d\n",timer_task->type);//103
//#endif
//if(timer_task->type == TASK_NEIGHBOR)
//queue_at_head(timer_task);
//else
queue_aodv_task(timer_task);
return 1;
}