summaryrefslogtreecommitdiff
path: root/src/lbdata.hh
blob: aaaa896683843c788d7f25738f734e27bd1b0ab0 (plain)
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
/*
 * Module: lbdata.hh
 *
 * 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.
 */
#ifndef __LBDATA_HH__
#define __LBDATA_HH__

#include <assert.h>
#include <map>
#include <set>
#include <vector>
#include <string>

using namespace std;

class LBHealth;
class LBTest;

/**
 *
 *
 **/
class LBRule {
 public:
  typedef map<string, int> InterfaceDistColl;
  typedef map<string, int>::iterator InterfaceDistIter;

  typedef enum {ALL,ICMP,UDP,TCP} Protocol;
  typedef enum {K_SECOND,K_MINUTE,K_HOUR} LimitPeriod;

  LBRule() :
    _proto("all"),
    _exclude(false),
    _failover(false),
    _enable_source_based_routing(false),
    _limit(false),
    _limit_burst("5"),
    _limit_rate("1"),
    _limit_mode(false),
    _limit_period(K_HOUR)
  {}

 public:
  string _proto;
  string _s_addr;
  string _s_port;
  string _s_port_ipt;

  string _d_addr;
  string _d_port;
  string _d_port_ipt;

  bool _exclude;
  bool _failover;

  bool _enable_source_based_routing;

  bool _limit;
  string _limit_burst;
  string _limit_rate;
  bool _limit_mode; //true above, false below
  LimitPeriod _limit_period;

  string _in_iface;
  InterfaceDistColl _iface_dist_coll;
};


/**
 *
 *
 **/
class LBHealthHistory {
public:
  LBHealthHistory(int buffer_size);

  //push in the ping response for this...
  int 
  push(int rtt);

  int
  get_last_resp();

public:
  //results of health testing
  unsigned long _last_success;
  unsigned long _last_failure;

  unsigned long _failure_count;

  static int _buffer_size;
  vector<int> _resp_data;
  int _index;
};

/**
 *
 *
 **/
class LBHealth {
public:
  typedef map<int,LBTest*> TestColl;
  typedef map<int,LBTest*>::iterator TestIter;
  typedef map<int,LBTest*>::const_iterator TestConstIter;

public:
  LBHealth() :
    _success_ct(0),
    _failure_ct(0),
    _hresults(10),
    _is_active(true),
    _state_changed(true),
    _last_time_state_changed(0),
    _interface_index(0),
    _timeout(5),
    _time_start(0),
    _new_test(false)
      {}

  LBHealth(int interface_index, string &interface) :
    _success_ct(0),
    _failure_ct(0),
    _hresults(10),
    _is_active(true),
    _state_changed(true),
    _last_time_state_changed(0),
    _interface(interface),
    _interface_index(interface_index),
    _timeout(5),			      
    _time_start(0),
    _new_test(false)
      {}

  void put(int rtt);

  bool
  state_changed() const {return _state_changed;}

  unsigned long
  last_success() const {return _hresults._last_success;}

  unsigned long
  last_failure() const {return _hresults._last_failure;}

  unsigned long
  failure_count() const {return _failure_ct;}

  //test interfaces
  void
  start_new_test_cycle();

  void
  start_new_test();

  void
  send_test();

  int
  recv_test();

public: //variables
  int _success_ct;
  int _failure_ct;
  string _nexthop;
  string _dhcp_nexthop;
  LBHealthHistory _hresults;
  bool _is_active;
  bool _state_changed;
  unsigned long _last_time_state_changed;
  string _interface;
  int _interface_index;
  string _address;

  TestColl _test_coll;
private: //variables
  TestIter _test_iter;
  bool _test_success;
  unsigned long _timeout;
  unsigned long _time_start;
  bool _new_test;
};

/**
 *
 *
 **/
class LBData {
 public:
  typedef map<int,LBRule> LBRuleColl;
  typedef map<int,LBRule>::iterator LBRuleIter;
  typedef map<int,LBRule>::const_iterator LBRuleConstIter;
  typedef map<string,LBHealth> InterfaceHealthColl;
  typedef map<string,LBHealth>::iterator InterfaceHealthIter;
  typedef map<string,LBHealth>::const_iterator InterfaceHealthConstIter;

  LBData() : _disable_source_nat(false),_enable_local_traffic(false),_flush_conntrack(false),_sticky_inbound_connections(false) {}

  bool
  error() {return false;}

  bool
  is_active(const string &iface);

  map<string,string>
  state_changed();

  void
  reset_state_changed();

  void
  update_dhcp_nexthop();

  void
  dump();

 public:
  string _filename;

  LBRuleColl _lb_rule_coll;
  InterfaceHealthColl _iface_health_coll;

  bool _disable_source_nat;
  bool _enable_local_traffic;
  bool _flush_conntrack;
  bool _sticky_inbound_connections;
  string _hook;
  string _post_hook;
};

#endif //__LBDATA_HH__