/
loadbalancer_haproxy.cc
224 lines (199 loc) · 7.4 KB
/
loadbalancer_haproxy.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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include "loadbalancer_haproxy.h"
#include <cerrno>
#include <fstream>
#include <boost/assign/list_of.hpp>
#include "base/logging.h"
#include "agent.h"
#include "init/agent_param.h"
#include "loadbalancer_properties.h"
using namespace std;
using boost::assign::map_list_of;
LoadbalancerHaproxy::LoadbalancerHaproxy(Agent *agent)
: protocol_default_("tcp"),
balance_default_("roundrobin"), agent_(agent) {
protocol_map_ = map_list_of
("TCP", "tcp")
("HTTP", "http")
("HTTPS", "tcp");
balance_map_ = map_list_of
("ROUND_ROBIN", "roundrobin")
("LEAST_CONNECTIONS", "leastconn")
("SOURCE_IP", "source");
}
const string &LoadbalancerHaproxy::ProtocolMap(const string &proto) const {
map<string, string>::const_iterator loc = protocol_map_.find(proto);
if (loc == protocol_map_.end()) {
return protocol_default_;
}
return loc->second;
}
const string &LoadbalancerHaproxy::BalanceMap(const string &proto) const {
map<string, string>::const_iterator loc = balance_map_.find(proto);
if (loc == balance_map_.end()) {
return balance_default_;
}
return loc->second;
}
/*
* global
* daemon
* user nobody
* group nogroup
* log /dev/log local0
* log /dev/log local1 notice
* stats socket <path> mode 0666 level user
*/
void LoadbalancerHaproxy::GenerateGlobal(
ostream *out, const string &filename,
const LoadbalancerProperties &props) const {
*out << "global" << endl;
*out << string(4, ' ') << "daemon" << endl;
*out << string(4, ' ') << "user nobody" << endl;
*out << string(4, ' ') << "group nogroup" << endl;
*out << string(4, ' ') << "stats socket " << filename
<< ".sock mode 0666 level user" << endl;
*out << string(4, ' ') << "tune.ssl.default-dh-param 2048" << endl;
*out << string(4, ' ') << "ssl-default-bind-ciphers " <<
"ECDH+AESGCM:DH+AESGCM:ECDH+AES256:" <<
"DH+AES256:ECDH+AES128:DH+AES:" <<
"ECDH+3DES:DH+3DES:RSA+AESGCM:" <<
"RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS" << endl;
*out << endl;
}
void LoadbalancerHaproxy::GenerateDefaults(
ostream *out, const LoadbalancerProperties &props) const {
*out << "defaults" << endl;
*out << string(4, ' ') << "log global" << endl;
*out << string(4, ' ') << "retries 3" << endl;
*out << string(4, ' ') << "option redispatch" << endl;
*out << string(4, ' ') << "timeout connect 5000" << endl;
*out << string(4, ' ') << "timeout client 50000" << endl;
*out << string(4, ' ') << "timeout server 50000" << endl;
*out << endl;
}
void LoadbalancerHaproxy::GenerateListen(
ostream *out, const LoadbalancerProperties &props) const {
*out << "listen contrail-config-stats :5937" << endl;
*out << string(4, ' ') << "mode http" << endl;
*out << string(4, ' ') << "stats enable" << endl;
*out << string(4, ' ') << "stats uri /" << endl;
*out << string(4, ' ') << "stats auth haproxy:contrail123" << endl;
*out << endl;
}
/*
* frontend vip_id
* bind address:port
* mode [http|tcp]
* default_backend pool_id
*/
void LoadbalancerHaproxy::GenerateFrontend(
ostream *out, const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const {
*out << "frontend " << props.vip_uuid() << endl;
const autogen::VirtualIpType &vip = props.vip_properties();
*out << string(4, ' ')
<< "bind " << vip.address << ":" << vip.protocol_port;
if (vip.protocol == LB_HAPROXY_TERMINATION_PROTO) {
*out << " ssl crt " <<
agent_->params()->si_haproxy_ssl_cert_path();
}
*out << endl;
*out << string(4, ' ')
<< "mode " << ProtocolMap(vip.protocol) << endl;
*out << string(4, ' ')
<< "default_backend " << pool_id << endl;
if (vip.connection_limit >= 0) {
*out << string(4, ' ')
<< "maxconn " << vip.connection_limit << endl;
}
*out << endl;
}
/*
* backend <pool_id>
* mode <protocol>
* balance <lb_method>
* server <id> <address>:<port> weight <weight>
*/
void LoadbalancerHaproxy::GenerateBackend(
ostream *out, const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const {
const autogen::LoadbalancerPoolType &pool = props.pool_properties();
*out << "backend " << pool_id << endl;
*out << string(4, ' ')
<< "mode " << ProtocolMap(pool.protocol) << endl;
*out << string(4, ' ')
<< "balance " << BalanceMap(pool.loadbalancer_method) << endl;
int timeout = 0, max_retries = 0;
if (props.healthmonitors().size()) {
const autogen::LoadbalancerHealthmonitorType &hm =
props.healthmonitors().begin()->second;
timeout = hm.timeout * 1000; //In milliseconds
max_retries = hm.max_retries;
if (!hm.monitor_type.empty() &&
(hm.monitor_type == "HTTP" || hm.monitor_type == "HTTPS")) {
if (!hm.url_path.empty()) {
*out << string(4, ' ')
<< "option httpchk ";
if (!hm.http_method.empty()) {
*out << hm.http_method;
}
*out << " " << hm.url_path << endl;
}
if (!hm.expected_codes.empty()) {
*out << string(4, ' ')
<< "http-check expect status "
<< hm.expected_codes << endl;
}
if (hm.monitor_type == "HTTPS") {
*out << string(4, ' ')
<< "option ssl-hello-chk " << endl;
}
}
}
const autogen::VirtualIpType &vip = props.vip_properties();
if (vip.persistence_type == "HTTP_COOKIE") {
*out << string(4, ' ') << "cookie SRV insert indirect nocache"
<< endl;
}
if (vip.persistence_type == "SOURCE_IP") {
*out << string(4, ' ') << "stick-table type ip size 10k"
<< endl;
*out << string(4, ' ') << "stick on src" << endl;
}
if (vip.persistence_type == "APP_COOKIE" &&
!vip.persistence_cookie_name.empty()) {
*out << string(4, ' ') << "appsession " <<
vip.persistence_cookie_name << " len 56 timeout 3h" << endl;
}
for (LoadbalancerProperties::MemberMap::const_iterator iter =
props.members().begin();
iter != props.members().end(); ++iter) {
const autogen::LoadbalancerMemberType &member = iter->second;
*out << string(4, ' ')
<< "server " << iter->first << " " << member.address
<< ":" << member.protocol_port
<< " weight " << member.weight;
if (timeout) {
*out << " check inter " << timeout << " rise 1 fall "
<< max_retries;
}
*out << endl;
}
}
void LoadbalancerHaproxy::GenerateConfig(
const string &filename, const boost::uuids::uuid &pool_id,
const LoadbalancerProperties &props) const {
ofstream fs(filename.c_str());
if (fs.fail()) {
LOG(ERROR, "File create " << filename << ": " << strerror(errno));
}
GenerateGlobal(&fs, filename, props);
GenerateDefaults(&fs, props);
GenerateListen(&fs, props);
GenerateFrontend(&fs, pool_id, props);
GenerateBackend(&fs, pool_id, props);
fs.close();
}