-
Notifications
You must be signed in to change notification settings - Fork 2
/
send-buf.c
376 lines (294 loc) · 7.48 KB
/
send-buf.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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* Copyright (C) Uppsala University
*
* This file is distributed under the terms of the GNU general Public
* License (GPL), see the file LICENSE
*
* Author: Erik Nordström, <[email protected]>
*/
#ifdef __KERNEL__
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
#include <linux/config.h>
#endif
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <net/sock.h>
#include <linux/icmp.h>
#include <net/icmp.h>
#endif
#ifdef NS2
#include "ns-agent.h"
#endif
#include "tbl.h"
#include "send-buf.h"
#include "debug.h"
#include "link-cache.h"
#include "dsr-srt.h"
#include "timer.h"
#ifdef __KERNEL__
#define SEND_BUF_PROC_FS_NAME "send_buf"
TBL(send_buf, SEND_BUF_MAX_LEN);
static DSRUUTimer send_buf_timer;
static int send_buf_print(struct tbl *t, char *buffer);
#endif
struct send_buf_entry {
list_t l;
struct dsr_pkt *dp;
struct timeval qtime;
xmit_fct_t okfn;
};
static inline int crit_addr(void *pos, void *addr)
{
struct in_addr *a = (struct in_addr *)addr;
struct send_buf_entry *e = (struct send_buf_entry *)pos;
if (e->dp->dst.s_addr == a->s_addr)
return 1;
return 0;
}
static inline int crit_garbage(void *pos, void *n)
{
struct timeval *now = (struct timeval *)n;
struct send_buf_entry *e = (struct send_buf_entry *)pos;
if (timeval_diff(now, &e->qtime) >=
(int)ConfValToUsecs(SendBufferTimeout)) {
if (e->dp)
dsr_pkt_free(e->dp);
return 1;
}
return 0;
}
void NSCLASS send_buf_set_max_len(unsigned int max_len)
{
send_buf.max_len = max_len;
}
void NSCLASS send_buf_timeout(unsigned long data)
{
struct send_buf_entry *e;
int pkts;
struct timeval expires, now;
/* char buf[2048]; */
gettime(&now);
/* send_buf_print(&send_buf, buf); */
/* LOG_DBG("\n%s\n", buf); */
pkts = tbl_for_each_del(&send_buf, &now, crit_garbage);
LOG_DBG("%d packets garbage collected\n", pkts);
read_lock_bh(&send_buf.lock);
/* Get first packet in maintenance buffer */
e = (struct send_buf_entry *)__tbl_find(&send_buf, NULL, crit_none);
if (!e) {
LOG_DBG("No packet to set timeout for\n");
read_unlock_bh(&send_buf.lock);
return;
}
expires = e->qtime;
timeval_add_usecs(&expires, ConfValToUsecs(SendBufferTimeout));
LOG_DBG("now=%s qtime=%s exp=%s\n",
print_timeval(&now),
print_timeval(&e->qtime),
print_timeval(&expires));
read_unlock_bh(&send_buf.lock);
set_timer(&send_buf_timer, &expires);
}
static struct send_buf_entry *send_buf_entry_create(struct dsr_pkt *dp,
xmit_fct_t okfn)
{
struct send_buf_entry *e;
e = (struct send_buf_entry *)kmalloc(sizeof(*e), GFP_ATOMIC);
if (!e)
return NULL;
e->dp = dp;
e->okfn = okfn;
gettime(&e->qtime);
return e;
}
int NSCLASS send_buf_enqueue_packet(struct dsr_pkt *dp, xmit_fct_t okfn)
{
struct send_buf_entry *e;
struct timeval expires;
int res, empty = 0;
e = send_buf_entry_create(dp, okfn);
if (!e)
return -ENOMEM;
LOG_DBG("enqueing packet to %s\n", print_ip(dp->dst));
write_lock_bh(&send_buf.lock);
if (tbl_empty(&send_buf))
empty = 1;
res = __tbl_add_tail(&send_buf, &e->l);
if (res < 0) {
struct send_buf_entry *f;
LOG_DBG("buffer full, removing first\n");
f = (struct send_buf_entry *)__tbl_detach_first(&send_buf);
if (f) {
dsr_pkt_free(f->dp);
kfree(f);
}
res = tbl_add_tail(&send_buf, &e->l);
if (res < 0) {
LOG_DBG("Could not buffer packet\n");
kfree(e);
write_unlock_bh(&send_buf.lock);
return -ENOSPC;
}
}
write_unlock_bh(&send_buf.lock);
if (empty) {
gettime(&expires);
timeval_add_usecs(&expires, ConfValToUsecs(SendBufferTimeout));
set_timer(&send_buf_timer, &expires);
}
return res;
}
int NSCLASS send_buf_set_verdict(int verdict, struct in_addr dst)
{
struct send_buf_entry *e;
int pkts = 0;
write_lock_bh(&send_buf.lock);
switch (verdict) {
case SEND_BUF_DROP:
while ((e =
(struct send_buf_entry *)__tbl_find_detach(&send_buf,
&dst,
crit_addr))) {
/* Only send one ICMP message */
#ifdef __KERNEL__
if (pkts == 0)
icmp_send(e->dp->skb, ICMP_DEST_UNREACH,
ICMP_HOST_UNREACH, 0);
#endif
dsr_pkt_free(e->dp);
kfree(e);
pkts++;
}
LOG_DBG("Dropped %d queued pkts for %s\n", pkts, print_ip(dst));
break;
case SEND_BUF_SEND:
while ((e =
(struct send_buf_entry *)__tbl_find_detach(&send_buf,
&dst,
crit_addr))) {
LOG_DBG("Send packet\n");
/* Get source route */
e->dp->srt = dsr_rtc_find(e->dp->src, e->dp->dst);
if (e->dp->srt) {
if (dsr_srt_add(e->dp) < 0) {
LOG_DBG("Could not add source route\n");
dsr_pkt_free(e->dp);
} else
/* Send packet */
#ifdef NS2
(this->*e->okfn) (e->dp);
#else
e->okfn(e->dp);
#endif
} else {
LOG_DBG("No source route found for %s!\n",
print_ip(dst));
dsr_pkt_free(e->dp);
}
pkts++;
kfree(e);
}
LOG_DBG("Sent %d queued packets to %s\n", pkts, print_ip(dst));
/* if (pkts == 0) */
/* LOG_DBG("No packets for dest %s\n", print_ip(dst)); */
break;
}
write_unlock_bh(&send_buf.lock);
return pkts;
}
static inline int send_buf_flush(struct tbl *t)
{
struct send_buf_entry *e;
int pkts = 0;
/* Flush send buffer */
write_lock_bh(&t->lock);
while ((e = (struct send_buf_entry *)
__tbl_find_detach(t, NULL, crit_none))) {
dsr_pkt_free(e->dp);
kfree(e);
pkts++;
}
write_unlock_bh(&t->lock);
return pkts;
}
#ifdef __KERNEL__
static int send_buf_print(struct tbl *t, char *buffer)
{
list_t *p;
int len;
struct timeval now;
gettime(&now);
len = sprintf(buffer, "# %-15s %-8s\n", "IPAddr", "Age (s)");
read_lock_bh(&t->lock);
list_for_each(p, &t->head) {
struct send_buf_entry *e = (struct send_buf_entry *)p;
if (e && e->dp)
len += sprintf(buffer + len, " %-15s %-8lu\n",
print_ip(e->dp->dst),
timeval_diff(&now, &e->qtime) / 1000000);
}
len += sprintf(buffer + len,
"\nQueue length : %u\n"
"Queue max. length : %u\n", t->len, t->max_len);
read_unlock_bh(&t->lock);
return len;
}
static int
send_buf_get_info(char *buffer, char **start, off_t offset,
int length, int *eof, void *data)
{
int len;
len = send_buf_print(&send_buf, buffer);
*start = buffer + offset;
len -= offset;
if (len > length)
len = length;
else if (len < 0)
len = 0;
return len;
}
#endif /* __KERNEL__ */
int __init NSCLASS send_buf_init(void)
{
#ifdef __KERNEL__
struct proc_dir_entry *proc;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23))
#define proc_net init_net.proc_net
#endif
proc = create_proc_read_entry(SEND_BUF_PROC_FS_NAME, 0,
proc_net, send_buf_get_info, NULL);
if (!proc) {
printk(KERN_ERR "send_buf: failed to create proc entry\n");
return -1;
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
proc->owner = THIS_MODULE;
#endif
#endif
INIT_TBL(&send_buf, SEND_BUF_MAX_LEN);
init_timer(&send_buf_timer);
send_buf_timer.function = &NSCLASS send_buf_timeout;
return 1;
}
void __exit NSCLASS send_buf_cleanup(void)
{
int pkts;
#ifdef KERNEL26
synchronize_net();
#endif
if (timer_pending(&send_buf_timer))
del_timer_sync(&send_buf_timer);
pkts = send_buf_flush(&send_buf);
LOG_DBG("Flushed %d packets\n", pkts);
#ifdef __KERNEL__
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
proc_net_remove(SEND_BUF_PROC_FS_NAME);
#else
proc_net_remove(&init_net, SEND_BUF_PROC_FS_NAME);
#endif
#endif
}