forked from wujingbang/aodv-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_queue.c
executable file
·173 lines (132 loc) · 3.49 KB
/
task_queue.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
/***************************************************************************
task_queue.c - description
-------------------
begin : Tue Jul 8 2003
copyright : (C) 2003 by Luke Klein-Berndt
email : [email protected]
***************************************************************************/
/***************************************************************************
Modified by Miguel Catalan Cid - [email protected] - 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;
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;
}
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->tos = 0;
return new_task;
}
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;
//unlock table
queue_unlock();
//wake up the AODV thread
kick_aodv();
return 0;
}
task *get_task() {
task *tmp_task = NULL;
queue_lock();
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_from_timer(task * timer_task) {
if (!timer_task) {
printk("Passed a Null task Task\n");
return -ENOMEM;
}
queue_aodv_task(timer_task);
return 1;
}