-
Notifications
You must be signed in to change notification settings - Fork 3
/
lineariplookup.cc
248 lines (223 loc) · 6.06 KB
/
lineariplookup.cc
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
// -*- c-basic-offset: 4 -*-
/*
* lineariplookup.{cc,hh} -- element looks up next-hop address in linear
* routing table
* Robert Morris, Eddie Kohler
*
* Copyright (c) 1999-2000 Massachusetts Institute of Technology
* Copyright (c) 2002 International Computer Science Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Click LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Click LICENSE file; the license in that file is
* legally binding.
*/
#include <click/config.h>
#include "lineariplookup.hh"
#include <click/ipaddress.hh>
#include <click/straccum.hh>
#include <click/error.hh>
CLICK_DECLS
LinearIPLookup::LinearIPLookup()
: _zero_route(-1)
{
}
LinearIPLookup::~LinearIPLookup()
{
}
int
LinearIPLookup::initialize(ErrorHandler *)
{
_last_addr = IPAddress();
#ifdef IP_RT_CACHE2
_last_addr2 = _last_addr;
#endif
return 0;
}
bool
LinearIPLookup::check() const
{
bool ok = true;
//click_chatter("%s\n", ((LinearIPLookup*)this)->dump_routes().c_str());
// 'next' pointers are correct
for (int i = 0; i < _t.size(); i++) {
if (!_t[i].real())
continue;
for (int j = i + 1; j < _t[i].extra && j < _t.size(); j++)
if (_t[i].contains(_t[j]) && _t[j].real()) {
click_chatter("%s: bad next pointers: routes %s, %s", declaration().c_str(), _t[i].unparse_addr().c_str(), _t[j].unparse_addr().c_str());
ok = false;
}
#if 0
// This invariant actually does not hold.
int j = _t[i].extra;
if (j < _t.size())
if (!_t[i].contains(_t[j]) && _t[j].real()) {
click_chatter("%s: bad next pointers': routes %s, %s", declaration().c_str(), _t[i].unparse_addr().c_str(), _t[j].unparse_addr().c_str());
ok = false;
}
#endif
}
// no duplicate routes
for (int i = 0; i < _t.size(); i++)
for (int j = i + 1; j < _t.size(); j++)
if (_t[i].addr == _t[j].addr && _t[i].mask == _t[j].mask && _t[i].real() && _t[j].real()) {
click_chatter("%s: duplicate routes for %s", declaration().c_str(), _t[i].unparse_addr().c_str());
ok = false;
}
// caches point to the right place
if (_last_addr && lookup_entry(_last_addr) != _last_entry) {
click_chatter("%s: bad cache entry for %s", declaration().c_str(), _last_addr.unparse().c_str());
ok = false;
}
#ifdef IP_RT_CACHE2
if (_last_addr2 && lookup_entry(_last_addr2) != _last_entry2) {
click_chatter("%s: bad cache entry for %s", declaration().c_str(), _last_addr2.unparse().c_str());
ok = false;
}
#endif
return ok;
}
int
LinearIPLookup::add_route(const IPRoute &r, bool allow_replace, IPRoute* replaced, ErrorHandler *)
{
// overwrite any existing route
int found = _t.size();
for (int i = 0; i < _t.size(); i++)
if (!_t[i].real()) {
if (found == _t.size())
found = i;
} else if (_t[i].addr == r.addr && _t[i].mask == r.mask) {
if (replaced)
*replaced = _t[i];
if (!allow_replace)
return -EEXIST;
_t[i].gw = r.gw;
_t[i].port = r.port;
check();
return 0;
}
// maybe make a new slot
if (found == _t.size())
_t.push_back(r);
else
_t[found] = r;
// patch up next pointers
_t[found].extra = 0x7FFFFFFF;
for (int i = found - 1; i >= 0; i--)
if (_t[i].contains(r) && _t[i].extra > found)
_t[i].extra = found;
for (int i = found + 1; i < _t.size(); i++)
if (r.contains(_t[i]) && _t[i].real()) {
_t[found].extra = i;
break;
}
// remember zero route
if (!r.addr && r.mask.addr() == 0xFFFFFFFFU)
_zero_route = found;
// get rid of caches
_last_addr = IPAddress();
#ifdef IP_RT_CACHE2
_last_addr2 = IPAddress();
#endif
check();
return 0;
}
int
LinearIPLookup::remove_route(const IPRoute& route, IPRoute* old_route, ErrorHandler *errh)
{
for (int i = 0; i < _t.size(); i++)
if (route.match(_t[i])) {
if (old_route)
*old_route = _t[i];
_t[i].kill();
// need to handle zero routes, bummer
if (i == _zero_route)
_zero_route = -1;
else if (i < _zero_route) {
IPRoute zero(_t[_zero_route]);
_t[_zero_route].kill();
int r = add_route(zero, false, 0, errh);
assert(r >= 0);
}
// get rid of caches
_last_addr = IPAddress();
#ifdef IP_RT_CACHE2
_last_addr2 = IPAddress();
#endif
check();
return 0;
}
return -ENOENT;
}
int
LinearIPLookup::lookup_entry(IPAddress a) const
{
for (int i = 0; i < _t.size(); i++)
if (_t[i].contains(a)) {
int found = i;
for (int j = _t[i].extra; j < _t.size(); j++)
if (_t[j].contains(a) && _t[j].mask_as_specific(_t[found].mask))
found = j;
return found;
}
return -1;
}
int
LinearIPLookup::lookup_route(IPAddress a, IPAddress &gw) const
{
int ei = lookup_entry(a);
if (ei >= 0) {
gw = _t[ei].gw;
return _t[ei].port;
} else
return -1;
}
String
LinearIPLookup::dump_routes()
{
StringAccum sa;
for (int i = 0; i < _t.size(); i++)
if (_t[i].real())
_t[i].unparse(sa, true) << '\n';
return sa.take_string();
}
void
LinearIPLookup::push(int, Packet *p)
{
#define EXCHANGE(a,b,t) { t = a; a = b; b = t; }
IPAddress a = p->dst_ip_anno();
int ei = -1;
if (a && a == _last_addr)
ei = _last_entry;
#ifdef IP_RT_CACHE2
else if (a && a == _last_addr2)
ei = _last_entry2;
#endif
else if ((ei = lookup_entry(a)) >= 0) {
#ifdef IP_RT_CACHE2
_last_addr2 = _last_addr;
_last_entry2 = _last_entry;
#endif
_last_addr = a;
_last_entry = ei;
} else {
static int complained = 0;
if (++complained <= 5)
click_chatter("LinearIPLookup: no route for %s", a.unparse().c_str());
p->kill();
return;
}
const IPRoute &e = _t[ei];
if (e.gw)
p->set_dst_ip_anno(e.gw);
output(e.port).push(p);
}
CLICK_ENDDECLS
ELEMENT_REQUIRES(IPRouteTable)
EXPORT_ELEMENT(LinearIPLookup)