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
|
/*
* Module: lbdata.cc
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include <stdio.h>
#include <sys/time.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include <iostream>
#include "rl_str_proc.hh"
#include "lbtest.hh"
#include "lbdata.hh"
int LBHealthHistory::_buffer_size = 10;
/**
*
*
**/
void
LBHealth::put(int rtt)
{
int activity_ct = _hresults.push(rtt);
if (rtt == -1) {
if (activity_ct >= _failure_ct) {
if (_is_active == true) {
_state_changed = true;
}
_is_active = false;
}
}
else {
if (activity_ct >= _success_ct) {
if (_is_active == false) {
_state_changed = true;
}
_is_active = true;
}
}
}
/**
*
*
**/
void
LBHealth::start_new_test_cycle()
{
//let's first clear out all the old results
TestIter t = _test_coll.begin();
while (t != _test_coll.end()) {
t->second->_state = LBTest::K_NONE;
++t;
}
_test_iter = _test_coll.begin();
// _test_iter->second->start();
}
/**
*
*
**/
void
LBHealth::start_new_test()
{
if (_test_iter == _test_coll.end()) {
return;
}
_test_iter->second->start();
}
/**
*
*
**/
void
LBHealth::send_test()
{
if (_test_iter == _test_coll.end()) {
return; //means we are done
}
_test_iter->second->send(*this);
}
/**
*
*
**/
int
LBHealth::recv_test()
{
if (_test_iter == _test_coll.end()) {
put(-1);
return 0; //means stop iteration, no more tests...
}
int rtt = _test_iter->second->recv(*this);
++_test_iter;
if (rtt >= 0) {
put(rtt);
return 0; //means success, testing is done
}
return -1; //means testing is not done for this interface
}
/**
*
*
**/
LBHealthHistory::LBHealthHistory(int buffer_size) :
_last_success(0),
_last_failure(0),
_failure_count(0),
_index(0)
{
_resp_data.resize(10);
for (int i = 0; i < _buffer_size; ++i) {
_resp_data[i] = 0;
}
}
/**
*
*
**/
int
LBHealthHistory::get_last_resp()
{
if (_resp_data.size() == 0) {
return -1;
}
return (_resp_data[0]);
}
/**
*
*
**/
int
LBHealthHistory::push(int rtt)
{
struct timeval tv;
gettimeofday(&tv,NULL);
if (rtt == -1) {
_last_failure = tv.tv_sec;
++_failure_count;
}
else {
_failure_count = 0;
_last_success = tv.tv_sec;
}
_resp_data[_index % _buffer_size] = rtt;
++_index;
//compute count of sequence of same responses
int ct = 0;
int start_index = (_index - 1) % _buffer_size;
for (int i = 0; i < _buffer_size; ++i) {
int index = start_index - i;
if (index < 0) {
//handle wrap around of index here
index = _buffer_size - index;
}
if (_resp_data[index] == -1 && rtt == -1) {
++ct;
}
else if (_resp_data[index] != -1 && rtt != -1) {
++ct;
}
else {
break;
}
}
return ct;
}
/**
*
*
**/
bool
LBData::is_active(const string &iface)
{
InterfaceHealthIter iter = _iface_health_coll.find(iface);
if (iter != _iface_health_coll.end()) {
return iter->second._is_active;
}
return false;
}
/**
*
*
**/
void
LBData::dump()
{
cout << "health:" << endl;
LBData::InterfaceHealthIter h_iter = _iface_health_coll.begin();
while (h_iter != _iface_health_coll.end()) {
cout << " interface: " << h_iter->first << endl;
cout << " nexthop: " << h_iter->second._nexthop << endl;
cout << " success ct: " << h_iter->second._success_ct << endl;
cout << " failure ct: " << h_iter->second._failure_ct << endl;
LBHealth::TestIter t_iter = h_iter->second._test_coll.begin();
while (t_iter != h_iter->second._test_coll.end()) {
cout << " test: " << t_iter->first << endl;
string foo = t_iter->second->dump();
cout << " " << foo << endl;
++t_iter;
}
++h_iter;
}
cout << endl << "wan:" << endl;
LBData::LBRuleIter r_iter = _lb_rule_coll.begin();
while (r_iter != _lb_rule_coll.end()) {
cout << " rule: " << r_iter->first << endl;
cout << " " << r_iter->second._proto << endl;
cout << " " << r_iter->second._s_addr << endl;
cout << " " << r_iter->second._s_port << endl;
cout << " " << r_iter->second._d_addr << endl;
cout << " " << r_iter->second._d_port << endl;
if (r_iter->second._limit) {
cout << " limit:" << endl;
cout << " burst: " << r_iter->second._limit_burst << endl;
cout << " rate: " << r_iter->second._limit_burst << endl;
if (r_iter->second._limit_mode) {
cout << " thresh: above" << endl;
}
else {
cout << " thresh: below" << endl;
}
if (r_iter->second._limit_period == LBRule::K_SECOND) {
cout << " period: second" << endl;
}
else if (r_iter->second._limit_period == LBRule::K_MINUTE) {
cout << " period: minute" << endl;
}
else if (r_iter->second._limit_period == LBRule::K_HOUR) {
cout << " period: hour" << endl;
}
}
LBRule::InterfaceDistIter ri_iter = r_iter->second._iface_dist_coll.begin();
while (ri_iter != r_iter->second._iface_dist_coll.end()) {
cout << " interface: " << ri_iter->first << endl;
cout << " weight: " << ri_iter->second << endl;
++ri_iter;
}
++r_iter;
}
cout << "end dump" << endl;
}
/**
*
*
**/
map<string,string>
LBData::state_changed()
{
map<string,string> coll;
LBData::InterfaceHealthIter h_iter = _iface_health_coll.begin();
while (h_iter != _iface_health_coll.end()) {
if (h_iter->second.state_changed()) {
string tmp = (h_iter->second._is_active ? string("ACTIVE") : string("FAILED"));
struct timeval tv;
gettimeofday(&tv,NULL);
h_iter->second._last_time_state_changed = (unsigned long)tv.tv_sec;
syslog(LOG_WARNING, "Interface %s has changed state to %s",h_iter->first.c_str(),tmp.c_str());
coll.insert(pair<string,string>(h_iter->first,tmp));
}
++h_iter;
}
return coll;
}
/**
*
*
**/
void
LBData::reset_state_changed()
{
LBData::InterfaceHealthIter h_iter = _iface_health_coll.begin();
while (h_iter != _iface_health_coll.end()) {
h_iter->second._state_changed = false;
++h_iter;
}
}
/**
*
*
**/
void
LBData::update_dhcp_nexthop()
{
/**
* currently only reads the nexthop as maintained by the dhcp client
**/
LBData::InterfaceHealthIter h_iter = _iface_health_coll.begin();
while (h_iter != _iface_health_coll.end()) {
if (h_iter->second._nexthop == "dhcp") {
string file("/var/lib/dhcp/dhclient_"+h_iter->first+".lease");
FILE *fp = fopen(file.c_str(),"r");
if (fp) {
char str[1025];
while (fgets(str, 1024, fp)) {
StrProc tokens(str, "=");
if (tokens.get(0) == "new_routers") {
string tmp = tokens.get(1);
//need to watch out for the case where there are mult routers...
StrProc tokens2(tmp," ");
tmp = tokens2.get(0);
long len = tmp.length()-2;
h_iter->second._dhcp_nexthop = tmp.substr(1,len);
break;
}
}
fclose(fp);
}
else {
//check if this is a ppp interface
string pppfile("/var/run/load-balance/ppp/"+h_iter->first);
FILE *fp = fopen(pppfile.c_str(),"r");
if (fp) {
char str[1025];
if (fgets(str, 1024, fp)) {
h_iter->second._dhcp_nexthop = string(str);
}
fclose(fp);
}
}
}
++h_iter;
}
}
|