OSDN Git Service

ae24c88c83acc11052b9ec01281a6ec9f7a80d60
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / virtualservice_element.h
1 /*!
2  * @file  virtualservice_element.h
3  * @brief use l7vscommand virtualservice_data prototype
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2009  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24
25 #ifndef VIRTUALSERVICE_ELEMENT_H
26 #define VIRTUALSERVICE_ELEMENT_H
27
28 #include <vector>
29 #include <boost/foreach.hpp>
30 #include <boost/serialization/string.hpp>
31 #include <boost/serialization/vector.hpp>
32 #include <boost/serialization/list.hpp>
33 #include <boost/serialization/map.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <boost/format.hpp>
36 #include "realserver_element.h"
37 #include "endpoint.h"
38
39 namespace l7vs
40 {
41
42 // virtual service element includes.
43 class virtualservice_element
44 {
45 public:
46         typedef std::pair<std::string, std::string> access_log_rotate_arguments_pair_type;
47         typedef std::map<std::string, std::string> access_log_rotate_arguments_map_type;
48         enum SORRYSERVER_FWDMODE_TAG {
49                 FWD_NONE = 0,
50                 FWD_MASQ,
51                 FWD_TPROXY,
52         };
53
54         bool udpmode;
55         boost::asio::ip::tcp::endpoint tcp_accept_endpoint;
56         boost::asio::ip::udp::endpoint udp_recv_endpoint;
57         std::vector<realserver_element> realserver_vector;
58         std::string protocol_module_name;
59         std::vector<std::string> protocol_args;
60         std::string schedule_module_name;
61
62         long long sorry_maxconnection;
63         boost::asio::ip::tcp::endpoint sorry_endpoint;
64         int sorry_flag;
65         SORRYSERVER_FWDMODE_TAG sorry_fwdmode;
66
67         unsigned long long qos_upstream;
68         unsigned long long qos_downstream;
69         unsigned long long throughput_upstream;
70         unsigned long long throughput_downstream;
71
72         std::string ssl_file_name;
73
74         int access_log_flag;
75         std::string access_log_file_name;
76         access_log_rotate_arguments_map_type access_log_rotate_arguments;
77         std::string access_log_rotate_key_info;
78         std::string access_log_rotate_verbose_info;
79
80         std::string protocol_module_for_indication_options;
81
82         int socket_option_tcp_defer_accept;
83         int socket_option_tcp_nodelay;
84         int socket_option_tcp_cork;
85         int socket_option_tcp_quickack;
86         std::string socket_option_string;
87
88         unsigned long long    http_total_count;
89         unsigned long long    http_get_count;
90         unsigned long long    http_post_count;
91
92         // constructor
93         virtualservice_element()
94                 :  udpmode(false),
95                    sorry_maxconnection(0LL),
96                    sorry_flag(0),
97                    sorry_fwdmode(FWD_NONE),
98                    qos_upstream(0ULL),
99                    qos_downstream(0ULL),
100                    throughput_upstream(0ULL),
101                    throughput_downstream(0ULL),
102                    access_log_flag(0),
103                    socket_option_tcp_defer_accept(0),
104                    socket_option_tcp_nodelay(0),
105                    socket_option_tcp_cork(0),
106                    socket_option_tcp_quickack(0),
107                    http_total_count(0ULL),
108                    http_get_count(0ULL),
109                    http_post_count(0ULL) {}
110
111         virtualservice_element(const virtualservice_element &in)
112                 :  udpmode(in.udpmode),
113                    tcp_accept_endpoint(in.tcp_accept_endpoint),
114                    udp_recv_endpoint(in.udp_recv_endpoint),
115                    protocol_module_name(in.protocol_module_name),
116                    schedule_module_name(in.schedule_module_name),
117                    sorry_maxconnection(in.sorry_maxconnection),
118                    sorry_endpoint(in.sorry_endpoint),
119                    sorry_flag(in.sorry_flag),
120                    sorry_fwdmode(in.sorry_fwdmode),
121                    qos_upstream(in.qos_upstream),
122                    qos_downstream(in.qos_downstream),
123                    throughput_upstream(in.throughput_upstream),
124                    throughput_downstream(in.throughput_downstream),
125                    ssl_file_name(in.ssl_file_name),
126                    access_log_flag(in.access_log_flag),
127                    access_log_file_name(in.access_log_file_name),
128                    access_log_rotate_key_info(in.access_log_rotate_key_info),
129                    access_log_rotate_verbose_info(in.access_log_rotate_verbose_info),
130                    protocol_module_for_indication_options(in.protocol_module_for_indication_options),
131                    socket_option_tcp_defer_accept(in.socket_option_tcp_defer_accept),
132                    socket_option_tcp_nodelay(in.socket_option_tcp_nodelay),
133                    socket_option_tcp_cork(in.socket_option_tcp_cork),
134                    socket_option_tcp_quickack(in.socket_option_tcp_quickack),
135                    socket_option_string(in.socket_option_string),
136                    http_total_count(in.http_total_count),
137                    http_get_count(in.http_get_count),
138                    http_post_count(in.http_post_count) {
139                 protocol_args.clear();
140                 BOOST_FOREACH(std::string str, in.protocol_args) {
141                         protocol_args.push_back(str);
142                 }
143                 realserver_vector.clear();
144                 BOOST_FOREACH(realserver_element elem, in.realserver_vector) {
145                         realserver_vector.push_back(elem);
146                 }
147                 access_log_rotate_arguments.clear();
148                 BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, in.access_log_rotate_arguments) {
149                         access_log_rotate_arguments.insert(pair);
150                 }
151         }
152
153         virtualservice_element &operator=(const virtualservice_element &in) {
154                 udpmode = in.udpmode;
155                 tcp_accept_endpoint = in.tcp_accept_endpoint;
156                 udp_recv_endpoint = in.udp_recv_endpoint;
157                 protocol_module_name = in.protocol_module_name;
158                 schedule_module_name = in.schedule_module_name;
159                 sorry_maxconnection = in.sorry_maxconnection;
160                 sorry_endpoint = in.sorry_endpoint;
161                 sorry_flag = in.sorry_flag;
162                 sorry_fwdmode = in.sorry_fwdmode;
163                 qos_upstream = in.qos_upstream;
164                 qos_downstream = in.qos_downstream;
165                 throughput_upstream = in.throughput_upstream;
166                 throughput_downstream = in.throughput_downstream;
167                 access_log_flag = in.access_log_flag;
168                 ssl_file_name = in.ssl_file_name;
169                 access_log_file_name = in.access_log_file_name;
170                 protocol_module_for_indication_options = in.protocol_module_for_indication_options;
171                 access_log_rotate_key_info = in.access_log_rotate_key_info;
172                 access_log_rotate_verbose_info = in.access_log_rotate_verbose_info;
173                 socket_option_tcp_defer_accept = in.socket_option_tcp_defer_accept;
174                 socket_option_tcp_nodelay = in.socket_option_tcp_nodelay;
175                 socket_option_tcp_cork = in.socket_option_tcp_cork;
176                 socket_option_tcp_quickack = in.socket_option_tcp_quickack;
177                 socket_option_string = in.socket_option_string;
178                 http_total_count = in.http_total_count;
179                 http_get_count = in.http_get_count;
180                 http_post_count = in.http_post_count;
181                 protocol_args.clear();
182                 BOOST_FOREACH(std::string str, in.protocol_args) {
183                         protocol_args.push_back(str);
184                 }
185                 realserver_vector.clear();
186                 BOOST_FOREACH(realserver_element elem, in.realserver_vector) {
187                         realserver_vector.push_back(elem);
188                 }
189                 access_log_rotate_arguments.clear();
190                 BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, in.access_log_rotate_arguments) {
191                         access_log_rotate_arguments.insert(pair);
192                 }
193                 return *this;
194         }
195
196         friend bool operator==(const virtualservice_element &elem1, const virtualservice_element &elem2) {
197                 if (elem1.udpmode == elem2.udpmode &&
198                     elem1.tcp_accept_endpoint == elem2.tcp_accept_endpoint &&
199                     elem1.udp_recv_endpoint == elem2.udp_recv_endpoint &&
200                     elem1.protocol_module_name == elem2.protocol_module_name &&
201                     elem1.sorry_maxconnection == elem2.sorry_maxconnection &&
202                     elem1.sorry_flag == elem2.sorry_flag &&
203                     elem1.sorry_fwdmode == elem2.sorry_fwdmode &&
204                     elem1.qos_upstream == elem2.qos_upstream &&
205                     elem1.qos_downstream == elem2.qos_downstream &&
206                     elem1.throughput_upstream == elem2.throughput_upstream &&
207                     elem1.throughput_downstream == elem2.throughput_downstream &&
208                     elem1.access_log_flag == elem2.access_log_flag &&
209                     elem1.ssl_file_name == elem2.ssl_file_name &&
210                     elem1.access_log_file_name == elem2.access_log_file_name &&
211                     elem1.protocol_module_for_indication_options == elem2.protocol_module_for_indication_options &&
212                     elem1.access_log_rotate_key_info == elem2.access_log_rotate_key_info &&
213                     elem1.access_log_rotate_verbose_info == elem2.access_log_rotate_verbose_info &&
214                     elem1.socket_option_tcp_defer_accept == elem2.socket_option_tcp_defer_accept &&
215                     elem1.socket_option_tcp_nodelay == elem2.socket_option_tcp_nodelay &&
216                     elem1.socket_option_tcp_cork == elem2.socket_option_tcp_cork &&
217                     elem1.socket_option_tcp_quickack == elem2.socket_option_tcp_quickack &&
218                     elem1.socket_option_string == elem2.socket_option_string &&
219                     elem1.http_total_count == elem2.http_total_count &&
220                     elem1.http_get_count == elem2.http_get_count &&
221                     elem1.http_post_count == elem2.http_post_count) {
222
223                         if (elem1.realserver_vector.size() != elem2.realserver_vector.size()) {
224                                 return false;
225                         }
226                         for (unsigned int i = 0; i < elem1.realserver_vector.size(); ++i) {
227                                 if (elem1.realserver_vector[i] != elem2.realserver_vector[i]) {
228                                         return false;
229                                 }
230                         }
231                         if (elem1.protocol_args.size() != elem2.protocol_args.size()) {
232                                 return false;
233                         }
234                         for (unsigned int i = 0; i < elem1.protocol_args.size(); ++i) {
235                                 if (elem1.protocol_args[i] != elem2.protocol_args[i]) {
236                                         return false;
237                                 }
238                         }
239                         if (elem1.access_log_rotate_arguments.size() != elem2.access_log_rotate_arguments.size()) {
240                                 return false;
241                         }
242                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem1.access_log_rotate_arguments) {
243                                 access_log_rotate_arguments_map_type::const_iterator it = elem2.access_log_rotate_arguments.find(pair.first);
244                                 if (elem2.access_log_rotate_arguments.end() == it) {
245                                         return false;
246                                 }
247                                 if (it->second != pair.second) {
248                                         return false;
249                                 }
250                         }
251                         return true;
252                 }
253                 return false;
254         }
255
256         friend bool operator!=(const virtualservice_element &elem1, const virtualservice_element &elem2) {
257                 if (elem1.udpmode == elem2.udpmode &&
258                     elem1.tcp_accept_endpoint == elem2.tcp_accept_endpoint &&
259                     elem1.udp_recv_endpoint == elem2.udp_recv_endpoint &&
260                     elem1.protocol_module_name == elem2.protocol_module_name &&
261                     elem1.sorry_maxconnection == elem2.sorry_maxconnection &&
262                     elem1.sorry_flag == elem2.sorry_flag &&
263                     elem1.sorry_fwdmode == elem2.sorry_fwdmode &&
264                     elem1.qos_upstream == elem2.qos_upstream &&
265                     elem1.qos_downstream == elem2.qos_downstream &&
266                     elem1.throughput_upstream == elem2.throughput_upstream &&
267                     elem1.throughput_downstream == elem2.throughput_downstream &&
268                     elem1.access_log_flag == elem2.access_log_flag &&
269                     elem1.ssl_file_name == elem2.ssl_file_name &&
270                     elem1.access_log_file_name == elem2.access_log_file_name &&
271                     elem1.protocol_module_for_indication_options == elem2.protocol_module_for_indication_options &&
272                     elem1.access_log_rotate_key_info == elem2.access_log_rotate_key_info &&
273                     elem1.access_log_rotate_verbose_info == elem2.access_log_rotate_verbose_info &&
274                     elem1.socket_option_tcp_defer_accept == elem2.socket_option_tcp_defer_accept &&
275                     elem1.socket_option_tcp_nodelay == elem2.socket_option_tcp_nodelay &&
276                     elem1.socket_option_tcp_cork == elem2.socket_option_tcp_cork &&
277                     elem1.socket_option_tcp_quickack == elem2.socket_option_tcp_quickack &&
278                     elem1.socket_option_string == elem2.socket_option_string &&
279                     elem1.http_total_count == elem2.http_total_count &&
280                     elem1.http_get_count == elem2.http_get_count &&
281                     elem1.http_post_count == elem2.http_post_count) {
282
283                         if (elem1.realserver_vector.size() != elem2.realserver_vector.size()) {
284                                 return true;
285                         }
286                         for (unsigned int i = 0; i < elem1.realserver_vector.size(); ++i) {
287                                 if (elem1.realserver_vector[i] != elem2.realserver_vector[i]) {
288                                         return true;
289                                 }
290                         }
291                         if (elem1.protocol_args.size() != elem2.protocol_args.size()) {
292                                 return true;
293                         }
294                         for (unsigned int i = 0; i < elem1.protocol_args.size(); ++i) {
295                                 if (elem1.protocol_args[i] != elem2.protocol_args[i]) {
296                                         return true;
297                                 }
298                         }
299                         if (elem1.access_log_rotate_arguments.size() != elem2.access_log_rotate_arguments.size()) {
300                                 return true;
301                         }
302                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem1.access_log_rotate_arguments) {
303                                 access_log_rotate_arguments_map_type::const_iterator it = elem2.access_log_rotate_arguments.find(pair.first);
304                                 if (elem2.access_log_rotate_arguments.end() == it) {
305                                         return true;
306                                 }
307                                 if (it->second != pair.second) {
308                                         return true;
309                                 }
310                         }
311                         return false;
312                 }
313                 return true;
314         }
315
316         friend bool operator<(const virtualservice_element &elem1, const virtualservice_element &elem2) {
317                 if (!elem1.udpmode && !elem2.udpmode) {
318                         return elem1.tcp_accept_endpoint < elem2.tcp_accept_endpoint;
319                 } else if (elem1.udpmode && elem2.udpmode) {
320                         return elem1.udp_recv_endpoint < elem2.udp_recv_endpoint;
321                 }
322                 return false;
323         }
324
325         template <typename Elem, typename Traits>
326         friend std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& os, const virtualservice_element &elem) {
327                 os << "virtualservice_element={";
328                 os << boost::format("udpmode=%s, "
329                                     "tcp_accept_endpoint=%s, "
330                                     "udp_recv_endpoint=%s, ")
331                    % elem.udpmode
332                    % elem.tcp_accept_endpoint
333                    % elem.udp_recv_endpoint;
334
335                 {
336                         unsigned int i = 0;
337                         BOOST_FOREACH(realserver_element rs_elem, elem.realserver_vector) {
338                                 os << boost::format("realserver_vector[%d]=") % i;
339                                 os << rs_elem;
340                                 os << ", ";
341                                 ++i;
342                         }
343                 }
344
345                 os << boost::format("protocol_module_name=%s, "
346                                     "schedule_module_name=%s, ")
347                    % elem.protocol_module_name
348                    % elem.schedule_module_name;
349
350                 std::stringstream access_log_rotate_arguments_stream;
351                 {
352                         unsigned int i = 0;
353                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem.access_log_rotate_arguments) {
354                                 access_log_rotate_arguments_stream << boost::format("access_log_rotate_arguments[%d]=") % i;
355                                 access_log_rotate_arguments_stream << boost::format("{key=%s, value=%s}") % pair.first % pair.second;
356                                 access_log_rotate_arguments_stream << ", ";
357                                 ++i;
358                         }
359                 }
360
361                 std::string args = boost::algorithm::join(elem.protocol_args, " ");
362                 os << boost::format("protocol_args=%s, "
363                                     "sorry_maxconnection=%d, "
364                                     "sorry_endpoint=%s, "
365                                     "sorry_flag=%d, "
366                                     "sorry_fwdmode=%d, "
367                                     "qos_upstream=%d, "
368                                     "qos_downstream=%d, "
369                                     "throughput_upstream=%d, "
370                                     "throughput_downstream=%d, "
371                                     "access_log_flag=%d, "
372                                     "ssl_file_name=%s, "
373                                     "access_log_file_name=%s, "
374                                     "access_log_rotate_arguments=%s, "
375                                     "protocol_module_for_indication_options=%s, "
376                                     "access_log_rotate_key_info=%s, "
377                                     "access_log_rotate_verbose_info=%s, "
378                                     "socket_option_tcp_defer_accept=%d, "
379                                     "socket_option_tcp_nodelay=%d, "
380                                     "socket_option_tcp_cork=%d, "
381                                     "socket_option_tcp_quickack=%d, "
382                                     "socket_option_string=%s; "
383                                     "http_total_count=%d; "
384                                     "http_get_count=%d; "
385                                     "http_post_count=%d; }")
386                    % args
387                    % elem.sorry_maxconnection
388                    % elem.sorry_endpoint
389                    % elem.sorry_flag
390                    % elem.sorry_fwdmode
391                    % elem.qos_upstream
392                    % elem.qos_downstream
393                    % elem.throughput_upstream
394                    % elem.throughput_downstream
395                    % elem.access_log_flag
396                    % elem.ssl_file_name
397                    % elem.access_log_file_name
398                    % access_log_rotate_arguments_stream.str()
399                    % elem.protocol_module_for_indication_options
400                    % elem.access_log_rotate_key_info
401                    % elem.access_log_rotate_verbose_info
402                    % elem.socket_option_tcp_defer_accept
403                    % elem.socket_option_tcp_nodelay
404                    % elem.socket_option_tcp_cork
405                    % elem.socket_option_tcp_quickack
406                    % elem.socket_option_string
407                    % elem.http_total_count
408                    % elem.http_get_count
409                    % elem.http_post_count;
410
411                 return os;
412         }
413
414         const std::string get_fwdmode_str() {
415                 return sorry_fwdmode == FWD_MASQ   ? "Masq"
416                        : sorry_fwdmode == FWD_TPROXY ? "Tproxy"
417                        : "Unknown";
418         }
419
420 private:
421         friend class boost::serialization::access; //! friend boost serializable class
422         //! serializable
423         //! @brief using boost serializable. class serializable function.
424         //! @param[in] archive
425         //! @param[in] version
426         template <class Archive>
427         void serialize(Archive &ar, const unsigned int version) {
428                 ar &udpmode;
429                 ar &tcp_accept_endpoint;
430                 ar &udp_recv_endpoint;
431                 ar &realserver_vector;
432                 ar &protocol_module_name;
433                 ar &schedule_module_name;
434                 ar &protocol_args;
435                 ar &sorry_maxconnection;
436                 ar &sorry_endpoint;
437                 ar &sorry_flag;
438                 ar &sorry_fwdmode;
439                 ar &qos_upstream;
440                 ar &qos_downstream;
441                 ar &throughput_upstream;
442                 ar &throughput_downstream;
443                 ar &access_log_flag;
444                 ar &ssl_file_name;
445                 ar &access_log_file_name;
446                 ar &access_log_rotate_arguments;
447                 ar &protocol_module_for_indication_options;
448                 ar &access_log_rotate_key_info;
449                 ar &access_log_rotate_verbose_info;
450                 ar &socket_option_tcp_defer_accept;
451                 ar &socket_option_tcp_nodelay;
452                 ar &socket_option_tcp_cork;
453                 ar &socket_option_tcp_quickack;
454                 ar &socket_option_string;
455                 ar &http_total_count;
456                 ar &http_get_count;
457                 ar &http_post_count;
458         }
459 };
460
461 } //namespace l7vsd
462 #endif //L7VS_VIRTUALSERVICE_ELEMENT_H