OSDN Git Service

spell check by aspell
[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         {
140                 protocol_args.clear();
141                 BOOST_FOREACH(std::string str, in.protocol_args) {
142                         protocol_args.push_back(str);
143                 }
144                 realserver_vector.clear();
145                 BOOST_FOREACH(realserver_element elem, in.realserver_vector) {
146                         realserver_vector.push_back(elem);
147                 }
148                 access_log_rotate_arguments.clear();
149                 BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, in.access_log_rotate_arguments) {
150                         access_log_rotate_arguments.insert(pair);
151                 }
152         }
153
154         virtualservice_element &operator=(const virtualservice_element &in)
155         {
156                 udpmode = in.udpmode;
157                 tcp_accept_endpoint = in.tcp_accept_endpoint;
158                 udp_recv_endpoint = in.udp_recv_endpoint;
159                 protocol_module_name = in.protocol_module_name;
160                 schedule_module_name = in.schedule_module_name;
161                 sorry_maxconnection = in.sorry_maxconnection;
162                 sorry_endpoint = in.sorry_endpoint;
163                 sorry_flag = in.sorry_flag;
164                 sorry_fwdmode = in.sorry_fwdmode;
165                 qos_upstream = in.qos_upstream;
166                 qos_downstream = in.qos_downstream;
167                 throughput_upstream = in.throughput_upstream;
168                 throughput_downstream = in.throughput_downstream;
169                 access_log_flag = in.access_log_flag;
170                 ssl_file_name = in.ssl_file_name;
171                 access_log_file_name = in.access_log_file_name;
172                 protocol_module_for_indication_options = in.protocol_module_for_indication_options;
173                 access_log_rotate_key_info = in.access_log_rotate_key_info;
174                 access_log_rotate_verbose_info = in.access_log_rotate_verbose_info;
175                 socket_option_tcp_defer_accept = in.socket_option_tcp_defer_accept;
176                 socket_option_tcp_nodelay = in.socket_option_tcp_nodelay;
177                 socket_option_tcp_cork = in.socket_option_tcp_cork;
178                 socket_option_tcp_quickack = in.socket_option_tcp_quickack;
179                 socket_option_string = in.socket_option_string;
180                 http_total_count = in.http_total_count;
181                 http_get_count = in.http_get_count;
182                 http_post_count = in.http_post_count;
183                 protocol_args.clear();
184                 BOOST_FOREACH(std::string str, in.protocol_args) {
185                         protocol_args.push_back(str);
186                 }
187                 realserver_vector.clear();
188                 BOOST_FOREACH(realserver_element elem, in.realserver_vector) {
189                         realserver_vector.push_back(elem);
190                 }
191                 access_log_rotate_arguments.clear();
192                 BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, in.access_log_rotate_arguments) {
193                         access_log_rotate_arguments.insert(pair);
194                 }
195                 return *this;
196         }
197
198         friend bool operator==(const virtualservice_element &elem1, const virtualservice_element &elem2)
199         {
200                 if (elem1.udpmode == elem2.udpmode &&
201                     elem1.tcp_accept_endpoint == elem2.tcp_accept_endpoint &&
202                     elem1.udp_recv_endpoint == elem2.udp_recv_endpoint &&
203                     elem1.protocol_module_name == elem2.protocol_module_name &&
204                     elem1.sorry_maxconnection == elem2.sorry_maxconnection &&
205                     elem1.sorry_flag == elem2.sorry_flag &&
206                     elem1.sorry_fwdmode == elem2.sorry_fwdmode &&
207                     elem1.qos_upstream == elem2.qos_upstream &&
208                     elem1.qos_downstream == elem2.qos_downstream &&
209                     elem1.throughput_upstream == elem2.throughput_upstream &&
210                     elem1.throughput_downstream == elem2.throughput_downstream &&
211                     elem1.access_log_flag == elem2.access_log_flag &&
212                     elem1.ssl_file_name == elem2.ssl_file_name &&
213                     elem1.access_log_file_name == elem2.access_log_file_name &&
214                     elem1.protocol_module_for_indication_options == elem2.protocol_module_for_indication_options &&
215                     elem1.access_log_rotate_key_info == elem2.access_log_rotate_key_info &&
216                     elem1.access_log_rotate_verbose_info == elem2.access_log_rotate_verbose_info &&
217                     elem1.socket_option_tcp_defer_accept == elem2.socket_option_tcp_defer_accept &&
218                     elem1.socket_option_tcp_nodelay == elem2.socket_option_tcp_nodelay &&
219                     elem1.socket_option_tcp_cork == elem2.socket_option_tcp_cork &&
220                     elem1.socket_option_tcp_quickack == elem2.socket_option_tcp_quickack &&
221                     elem1.socket_option_string == elem2.socket_option_string &&
222                     elem1.http_total_count == elem2.http_total_count &&
223                     elem1.http_get_count == elem2.http_get_count &&
224                     elem1.http_post_count == elem2.http_post_count) {
225
226                         if (elem1.realserver_vector.size() != elem2.realserver_vector.size()) {
227                                 return false;
228                         }
229                         for (unsigned int i = 0; i < elem1.realserver_vector.size(); ++i) {
230                                 if (elem1.realserver_vector[i] != elem2.realserver_vector[i]) {
231                                         return false;
232                                 }
233                         }
234                         if (elem1.protocol_args.size() != elem2.protocol_args.size()) {
235                                 return false;
236                         }
237                         for (unsigned int i = 0; i < elem1.protocol_args.size(); ++i) {
238                                 if (elem1.protocol_args[i] != elem2.protocol_args[i]) {
239                                         return false;
240                                 }
241                         }
242                         if (elem1.access_log_rotate_arguments.size() != elem2.access_log_rotate_arguments.size()) {
243                                 return false;
244                         }
245                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem1.access_log_rotate_arguments) {
246                                 access_log_rotate_arguments_map_type::const_iterator it = elem2.access_log_rotate_arguments.find(pair.first);
247                                 if (elem2.access_log_rotate_arguments.end() == it) {
248                                         return false;
249                                 }
250                                 if (it->second != pair.second) {
251                                         return false;
252                                 }
253                         }
254                         return true;
255                 }
256                 return false;
257         }
258
259         friend bool operator!=(const virtualservice_element &elem1, const virtualservice_element &elem2)
260         {
261                 if (elem1.udpmode == elem2.udpmode &&
262                     elem1.tcp_accept_endpoint == elem2.tcp_accept_endpoint &&
263                     elem1.udp_recv_endpoint == elem2.udp_recv_endpoint &&
264                     elem1.protocol_module_name == elem2.protocol_module_name &&
265                     elem1.sorry_maxconnection == elem2.sorry_maxconnection &&
266                     elem1.sorry_flag == elem2.sorry_flag &&
267                     elem1.sorry_fwdmode == elem2.sorry_fwdmode &&
268                     elem1.qos_upstream == elem2.qos_upstream &&
269                     elem1.qos_downstream == elem2.qos_downstream &&
270                     elem1.throughput_upstream == elem2.throughput_upstream &&
271                     elem1.throughput_downstream == elem2.throughput_downstream &&
272                     elem1.access_log_flag == elem2.access_log_flag &&
273                     elem1.ssl_file_name == elem2.ssl_file_name &&
274                     elem1.access_log_file_name == elem2.access_log_file_name &&
275                     elem1.protocol_module_for_indication_options == elem2.protocol_module_for_indication_options &&
276                     elem1.access_log_rotate_key_info == elem2.access_log_rotate_key_info &&
277                     elem1.access_log_rotate_verbose_info == elem2.access_log_rotate_verbose_info &&
278                     elem1.socket_option_tcp_defer_accept == elem2.socket_option_tcp_defer_accept &&
279                     elem1.socket_option_tcp_nodelay == elem2.socket_option_tcp_nodelay &&
280                     elem1.socket_option_tcp_cork == elem2.socket_option_tcp_cork &&
281                     elem1.socket_option_tcp_quickack == elem2.socket_option_tcp_quickack &&
282                     elem1.socket_option_string == elem2.socket_option_string &&
283                     elem1.http_total_count == elem2.http_total_count &&
284                     elem1.http_get_count == elem2.http_get_count &&
285                     elem1.http_post_count == elem2.http_post_count ) {
286
287                         if (elem1.realserver_vector.size() != elem2.realserver_vector.size()) {
288                                 return true;
289                         }
290                         for (unsigned int i = 0; i < elem1.realserver_vector.size(); ++i) {
291                                 if (elem1.realserver_vector[i] != elem2.realserver_vector[i]) {
292                                         return true;
293                                 }
294                         }
295                         if (elem1.protocol_args.size() != elem2.protocol_args.size()) {
296                                 return true;
297                         }
298                         for (unsigned int i = 0; i < elem1.protocol_args.size(); ++i) {
299                                 if (elem1.protocol_args[i] != elem2.protocol_args[i]) {
300                                         return true;
301                                 }
302                         }
303                         if (elem1.access_log_rotate_arguments.size() != elem2.access_log_rotate_arguments.size()) {
304                                 return true;
305                         }
306                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem1.access_log_rotate_arguments) {
307                                 access_log_rotate_arguments_map_type::const_iterator it = elem2.access_log_rotate_arguments.find(pair.first);
308                                 if (elem2.access_log_rotate_arguments.end() == it) {
309                                         return true;
310                                 }
311                                 if (it->second != pair.second) {
312                                         return true;
313                                 }
314                         }
315                         return false;
316                 }
317                 return true;
318         }
319
320         friend bool operator<(const virtualservice_element &elem1, const virtualservice_element &elem2)
321         {
322                 if (!elem1.udpmode && !elem2.udpmode) {
323                         return elem1.tcp_accept_endpoint < elem2.tcp_accept_endpoint;
324                 } else if (elem1.udpmode && elem2.udpmode) {
325                         return elem1.udp_recv_endpoint < elem2.udp_recv_endpoint;
326                 }
327                 return false;
328         }
329
330         template <typename Elem, typename Traits>
331         friend std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& os, const virtualservice_element &elem)
332         {
333                 os << "virtualservice_element={";
334                 os << boost::format("udpmode=%s, "
335                                     "tcp_accept_endpoint=%s, "
336                                     "udp_recv_endpoint=%s, ")
337                    % elem.udpmode
338                    % elem.tcp_accept_endpoint
339                    % elem.udp_recv_endpoint;
340
341                 {
342                         unsigned int i = 0;
343                         BOOST_FOREACH(realserver_element rs_elem, elem.realserver_vector) {
344                                 os << boost::format("realserver_vector[%d]=") % i;
345                                 os << rs_elem;
346                                 os << ", ";
347                                 ++i;
348                         }
349                 }
350
351                 os << boost::format("protocol_module_name=%s, "
352                                     "schedule_module_name=%s, ")
353                    % elem.protocol_module_name
354                    % elem.schedule_module_name;
355
356                 std::stringstream access_log_rotate_arguments_stream;
357                 {
358                         unsigned int i = 0;
359                         BOOST_FOREACH(access_log_rotate_arguments_pair_type pair, elem.access_log_rotate_arguments) {
360                                 access_log_rotate_arguments_stream << boost::format("access_log_rotate_arguments[%d]=") % i;
361                                 access_log_rotate_arguments_stream << boost::format("{key=%s, value=%s}") % pair.first % pair.second;
362                                 access_log_rotate_arguments_stream << ", ";
363                                 ++i;
364                         }
365                 }
366
367                 std::string args = boost::algorithm::join(elem.protocol_args, " ");
368                 os << boost::format("protocol_args=%s, "
369                                     "sorry_maxconnection=%d, "
370                                     "sorry_endpoint=%s, "
371                                     "sorry_flag=%d, "
372                                     "sorry_fwdmode=%d, "
373                                     "qos_upstream=%d, "
374                                     "qos_downstream=%d, "
375                                     "throughput_upstream=%d, "
376                                     "throughput_downstream=%d, "
377                                     "access_log_flag=%d, "
378                                     "ssl_file_name=%s, "
379                                     "access_log_file_name=%s, "
380                                     "access_log_rotate_arguments=%s, "
381                                     "protocol_module_for_indication_options=%s, "
382                                     "access_log_rotate_key_info=%s, "
383                                     "access_log_rotate_verbose_info=%s, "
384                                     "socket_option_tcp_defer_accept=%d, "
385                                     "socket_option_tcp_nodelay=%d, "
386                                     "socket_option_tcp_cork=%d, "
387                                     "socket_option_tcp_quickack=%d, "
388                                     "socket_option_string=%s; "
389                                     "http_total_count=%d; "
390                                     "http_get_count=%d; "
391                                     "http_post_count=%d; }")
392                    % args
393                    % elem.sorry_maxconnection
394                    % elem.sorry_endpoint
395                    % elem.sorry_flag
396                    % elem.sorry_fwdmode
397                    % elem.qos_upstream
398                    % elem.qos_downstream
399                    % elem.throughput_upstream
400                    % elem.throughput_downstream
401                    % elem.access_log_flag
402                    % elem.ssl_file_name
403                    % elem.access_log_file_name
404                    % access_log_rotate_arguments_stream.str()
405                    % elem.protocol_module_for_indication_options
406                    % elem.access_log_rotate_key_info
407                    % elem.access_log_rotate_verbose_info
408                    % elem.socket_option_tcp_defer_accept
409                    % elem.socket_option_tcp_nodelay
410                    % elem.socket_option_tcp_cork
411                    % elem.socket_option_tcp_quickack
412                    % elem.socket_option_string
413                    % elem.http_total_count
414                    % elem.http_get_count
415                    % elem.http_post_count;
416
417                 return os;
418         }
419
420         const std::string get_fwdmode_str() {
421                 return sorry_fwdmode == FWD_MASQ   ? "Masq"
422                      : sorry_fwdmode == FWD_TPROXY ? "Tproxy"
423                      : "Unknown";
424         }
425
426 private:
427         friend class boost::serialization::access; //! friend boost serializable class
428         //! serializable
429         //! @brief using boost serializable. class serializable function.
430         //! @param[in] archive
431         //! @param[in] version
432         template <class Archive>
433         void serialize(Archive &ar, const unsigned int version)
434         {
435                 ar &udpmode;
436                 ar &tcp_accept_endpoint;
437                 ar &udp_recv_endpoint;
438                 ar &realserver_vector;
439                 ar &protocol_module_name;
440                 ar &schedule_module_name;
441                 ar &protocol_args;
442                 ar &sorry_maxconnection;
443                 ar &sorry_endpoint;
444                 ar &sorry_flag;
445                 ar &sorry_fwdmode;
446                 ar &qos_upstream;
447                 ar &qos_downstream;
448                 ar &throughput_upstream;
449                 ar &throughput_downstream;
450                 ar &access_log_flag;
451                 ar &ssl_file_name;
452                 ar &access_log_file_name;
453                 ar &access_log_rotate_arguments;
454                 ar &protocol_module_for_indication_options;
455                 ar &access_log_rotate_key_info;
456                 ar &access_log_rotate_verbose_info;
457                 ar &socket_option_tcp_defer_accept;
458                 ar &socket_option_tcp_nodelay;
459                 ar &socket_option_tcp_cork;
460                 ar &socket_option_tcp_quickack;
461                 ar &socket_option_string;
462                 ar &http_total_count;
463                 ar &http_get_count;
464                 ar &http_post_count;
465         }
466 };
467
468 } //namespace l7vsd
469 #endif //L7VS_VIRTUALSERVICE_ELEMENT_H