-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaodv_thread.c
More file actions
executable file
·243 lines (195 loc) · 5.79 KB
/
aodv_thread.c
File metadata and controls
executable file
·243 lines (195 loc) · 5.79 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
/***************************************************************************
aodv_thread.c - description
-------------------
begin : Tue Aug 12 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 "aodv_thread.h"
#include <linux/sched.h>
//JL: Added kernel threads interface:
#include <linux/kthread.h>
//static int aodv_pid;
wait_queue_head_t aodv_wait;
static atomic_t kill_thread;
static atomic_t aodv_is_dead;
int initialized =0;
//extern u_int32_t g_aodv_gateway;
extern u_int32_t g_mesh_ip;
struct task_struct *aodv_task;
void kick_aodv() {
//We are trying to wake up AODV!!!
//AODV thread is an interupptible sleep state.... this interupts it
wake_up_interruptible(&aodv_wait);
}
void kill_aodv() {
wait_queue_head_t queue;
init_waitqueue_head(&queue);
//sets a flag letting the thread know it should die
//wait for the thread to set flag saying it is dead
//lower semaphore for the thread
atomic_set(&kill_thread, 1);
wake_up_interruptible(&aodv_wait);
interruptible_sleep_on_timeout(&queue, HZ);
//kthread_should_stop();
}
void aodv(void) {
//The queue holding all the events to be dealt with
task *tmp_task;
//Initalize the variables
init_waitqueue_head(&aodv_wait);
atomic_set(&kill_thread, 0);
atomic_set(&aodv_is_dead, 0);
//Name our thread
/*lock_kernel();
sprintk(current->comm, "aodv-mcc");
//exit_mm(current); --> equivale a: current->mm = NULL;
//we are in a kthread? aren't we?
unlock_kernel();*/
//add_wait_queue_exclusive(event_socket->sk->sleep,&(aodv_wait));
// add_wait_queue(&(aodv_wait),event_socket->sk->sleep);
//why would I ever want to stop ? :)
for (;;) {
//should the thread exit?
if (atomic_read(&kill_thread)) {
goto exit;
}
//goto sleep until we recieve an interupt
interruptible_sleep_on(&aodv_wait);
//should the thread exit?
if (atomic_read(&kill_thread)) {
goto exit;
}
//While the buffer is not empty
extern dtn_hello_ip;
while ((tmp_task = get_task()) != NULL) {
u_int32_t dst;
//takes a different action depending on what type of event is recieved
switch (tmp_task->type) {
//remove following case when DTN hell test end
case TASK_DTN_HELLO:
//inet_aton("127.127.127.127",&dst);
//extern u_int32_t dtn_hello_ip;
gen_rreq(g_mesh_ip,dtn_hello_ip,tmp_task->tos);
#ifdef CaiDebug
printk("-------DTN HELLO TASK---------\n");
#endif
//insert_timer_simple(TASK_DTN_HELLO, 300*HELLO_INTERVAL, g_mesh_ip);
//update_timer_queue();
break;
//RREP
case TASK_RECV_RREP:
recv_rrep(tmp_task);
kfree(tmp_task->data);
break;
//RERR
case TASK_RECV_RERR:
//printk("-----------\nget RERR from %s----------\n",inet_ntoa(tmp_task->src_ip));
recv_rerr(tmp_task);
kfree(tmp_task->data);
break;
case TASK_RECV_HELLO:
//printk("get HELLO from %s\n",inet_ntoa(tmp_task->src_ip));
recv_hello(tmp_task);
kfree(tmp_task->data);
break;
/****************添加接收到通路包的任务***************/
#ifdef RECOVERYPATH
case TASK_RECV_RCVP:
//printk("Receive a RCVP\n");
recv_rcvp(tmp_task);
kfree(tmp_task->data);
break;
case TASK_RECV_RRDP:
//printk("Receive a RRDP\n");
recv_rrdp(tmp_task);
kfree(tmp_task->data);
break;
#endif
//Cleanup the Route Table and Flood ID queue
case TASK_CLEANUP:
flush_aodv_route_table();
break;
case TASK_HELLO:
//printk("gen HELLO\n");
gen_hello();
break;
case TASK_ST:
gen_st_rreq();
break;
case TASK_GW_CLEANUP:
update_gw_lifetimes();
insert_timer_simple(TASK_GW_CLEANUP, ACTIVE_GWROUTE_TIMEOUT,
g_mesh_ip);
update_timer_queue();
break;
case TASK_NEIGHBOR:
//printk("get NEIGHBOR TASH,delete neigh %s\n",inet_ntoa(tmp_task->src_ip));
delete_aodv_neigh(tmp_task->src_ip);
break;
case TASK_ROUTE_CLEANUP:
flush_aodv_route_table();
break;
case TASK_SEND_ETT:
send_probe(tmp_task->dst_ip);
break;
//A small probe packet is received
case TASK_RECV_S_ETT:
recv_sprobe(tmp_task);
kfree(tmp_task->data);
break;
//A large probe packet is received
case TASK_RECV_L_ETT:
recv_lprobe(tmp_task);
kfree(tmp_task->data);
break;
case TASK_ETT_CLEANUP:
reset_ett(find_aodv_neigh(tmp_task->src_ip));
printk("Reseting ETT-Info from neighbour %s\n",
inet_ntoa(tmp_task->src_ip));
break;
case TASK_NEIGHBOR_2H:
delete_aodv_neigh_2h(tmp_task->src_ip);
break;
case TASK_RECV_RREQ:
recv_rreq(tmp_task);
kfree(tmp_task->data);
break;
case TASK_RESEND_RREQ:
resend_rreq(tmp_task);
break;
case TASK_ETT_INFO:
recv_ett_info(tmp_task);
kfree(tmp_task->data);
break;
case TASK_SEND_RREP:
gen_rrep(tmp_task->src_ip, tmp_task->dst_ip, tmp_task->tos);
break;
case TASK_RECV_STRREQ:
recv_rreq_st(tmp_task);
kfree(tmp_task->data);
break;
case TASK_UPDATE_LOAD:
update_my_load();
break;
case TASK_GEN_RREQ:
gen_rreq(tmp_task->src_ip, tmp_task->dst_ip, tmp_task->tos);
break;
default:
break;
}
kfree(tmp_task);
}
}
exit:
//Set the flag that shows you are dead
atomic_set(&aodv_is_dead, 1);
}
void startup_aodv() {
//aodv_pid = kernel_thread((void *) &aodv, NULL, 0);
aodv_task = kthread_run((void *) &aodv, NULL, "fbaodv_protocol");
initialized = 1;
}