OSDN Git Service

Merge pull request #36314 (suigintoh/ultramonkey-l7-v3/fix_signum_cast into master).
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / module / protocol / protocol_module_ip.h
1 /*
2  * @file  protocol_module_ip.h
3  * @brief protocol module ip header file.
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  *register_replication_area_unlock
23  **********************************************************************/
24
25 #include <boost/thread/mutex.hpp>
26 #include <boost/lexical_cast.hpp>
27 #include "ip_protocol_module_base.h"
28 #include "ip_session_data_processor.h"
29 #include "ip_replication_data_processor.h"
30
31 #ifndef PROTOCOL_MODULE_IP_H
32 #define PROTOCOL_MODULE_IP_H
33
34 #define MAX_FORWARD_FOR_SIZE 36
35 #define MAX_OPTION_SIZE 128
36 #define MAX_IP_MODULE_BUFFER_SIZE (8190 + MAX_BUFFER_SIZE)
37 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
38 #define GOLDEN_RATIO_PRIME 0x9e370001UL
39
40 namespace l7vs
41 {
42
43 class protocol_module_ip : public ip_protocol_module_base
44 {
45 public:
46         enum DATA_STATE_TAG {
47                 HTTP_START = 0,
48                 HTTP_HEADER,
49                 HTTP_BODY,
50                 UNKNOWN
51         };
52
53         typedef std::deque<std::pair<char *, size_t> > buffer_deque;
54         struct session_thread_data_ip {
55                 boost::thread::id thread_id;
56                 boost::thread::id pair_thread_id;
57                 int thread_division;
58                 int end_flag;
59                 int accept_end_flag;
60                 int sorry_flag;
61                 int switch_flag;
62                 size_t data_buffer_size;
63                 size_t data_length;
64                 size_t data_offset;
65                 size_t current_message_rest_size;
66                 char *data_buffer;
67                 boost::array<char, MAX_FORWARD_FOR_SIZE> forwarded_for_buffer;
68                 int ip_hash;
69                 boost::asio::ip::tcp::endpoint client_endpoint;
70                 DATA_STATE_TAG data_state;
71                 EVENT_TAG last_status;
72                 buffer_deque buffer_sequence;
73         };
74
75         typedef boost::shared_ptr<session_thread_data_ip> thread_data_ptr;
76         typedef std::map<boost::thread::id, thread_data_ptr>::iterator session_thread_data_map_it;
77 protected:
78         int forwarded_for;
79         int reschedule;
80         int timeout;
81         int realserver_connect_failed_max_count;
82         boost:: array<char, MAX_OPTION_SIZE> sorry_uri ;
83         std::map<boost::thread::id, thread_data_ptr> session_thread_data_map;
84         boost::mutex session_data_mutex;
85         boost::mutex session_thread_data_map_mutex;
86
87         ip_replication_data_processor *replication_data_processor;
88         ip_session_data_processor *ip_data_processor;
89
90         unsigned int l7vs_ip_service_calc_hash(const boost::asio::ip::tcp::endpoint &cl_endpoint) {
91                 unsigned int hash = 0;
92                 if (cl_endpoint.address().is_v4()) {
93                         hash = cl_endpoint.address().to_v4().to_ulong() * GOLDEN_RATIO_PRIME;
94                 } else {
95                         boost::asio::ip::address_v6::bytes_type v6_bytes = cl_endpoint.address().to_v6().to_bytes();
96                         const boost::asio::ip::address_v4::bytes_type v4_bytes = {{v6_bytes[12], v6_bytes[13], v6_bytes[14], v6_bytes[15]}};
97                         boost::asio::ip::address_v4 v4_address(v4_bytes);
98                         hash = v4_address.to_ulong() * GOLDEN_RATIO_PRIME;
99
100                 }
101                 return hash >> (32 - HASH_TABLE_BITS);
102         }
103         class rs_list_scoped_lock
104         {
105         protected:
106                 boost::function< void(void) >    rs_list_unlock;
107         public:
108                 rs_list_scoped_lock(boost::function< void(void) >    inlist_lock,
109                 boost::function< void(void) >    inlist_unlock) {
110                         inlist_lock();
111                         rs_list_unlock = inlist_unlock;
112                 }
113                 ~rs_list_scoped_lock() {
114                         rs_list_unlock();
115                 }
116         };
117
118 public:
119         static const std::string MODULE_NAME;
120
121         static const int THREAD_DIVISION_UP_STREAM;
122         static const int THREAD_DIVISION_DOWN_STREAM;
123
124         static const int END_FLAG_OFF;
125         static const int END_FLAG_ON;
126
127         static const int ACCEPT_END_FLAG_OFF;
128         static const int ACCEPT_END_FLAG_ON;
129
130         static const int SORRY_FLAG_ON;
131         static const int SORRY_FLAG_OFF;
132
133         static const int SWITCH_FLAG_OFF;
134         static const int SWITCH_FLAG_ON;
135
136         static const int FORWARDED_FOR_OFF;
137         static const int FORWARDED_FOR_ON;
138
139         static const int COLLECT_STATS_OFF;
140         static const int COLLECT_STATS_ON;
141 public:
142         protocol_module_ip();
143         ~protocol_module_ip();
144         bool is_tcp();
145         bool is_udp();
146         void replication_interrupt();
147         void initialize(rs_list_itr_func_type    inlist_begin,
148         rs_list_itr_func_type    inlist_end,
149         rs_list_itr_next_func_type    inlist_next,
150         boost::function< void(void) >    inlist_lock,
151         boost::function< void(void) >    inlist_unlock);
152         void finalize();
153         bool is_use_sorry();
154         check_message_result check_parameter(const std::vector<std::string>& args);
155         check_message_result set_parameter(const std::vector<std::string>& args);
156         check_message_result add_parameter(const std::vector<std::string>& args);
157         void get_option_info(std::string &option);
158         void handle_rslist_update();
159         void register_schedule(tcp_schedule_func_type inschedule);
160         void register_schedule(udp_schedule_func_type inschedule);
161         EVENT_TAG handle_session_initialize(const boost::thread::id up_thread_id, const boost::thread::id down_thread_id, const boost::asio::ip::tcp::endpoint &client_endpoint_tcp, const boost::asio::ip::udp::endpoint &client_endpoint_udp);
162         EVENT_TAG handle_session_finalize(const boost::thread::id up_thread_id, const boost::thread::id down_thread_id);
163         EVENT_TAG handle_accept(const boost::thread::id thread_id);
164         EVENT_TAG handle_client_recv(const boost::thread::id thread_id, const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen);
165         EVENT_TAG handle_realserver_select(const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint &rs_endpoint);
166         EVENT_TAG handle_realserver_select(const boost::thread::id thread_id, boost::asio::ip::udp::endpoint &rs_endpoint, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
167         EVENT_TAG handle_realserver_connect(const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
168         EVENT_TAG handle_realserver_connection_fail(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint);
169         EVENT_TAG handle_realserver_send(const boost::thread::id thread_id);
170         EVENT_TAG handle_sorryserver_select(const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint &sorry_endpoint);
171         EVENT_TAG handle_sorryserver_connect(const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
172         EVENT_TAG handle_sorryserver_connection_fail(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint);
173         EVENT_TAG handle_sorryserver_send(const boost::thread::id thread_id);
174         EVENT_TAG handle_realserver_recv(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint, const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen);
175         EVENT_TAG handle_realserver_recv(const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint &rs_endpoint, const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen);
176         EVENT_TAG handle_sorryserver_recv(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint, const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen);
177         EVENT_TAG handle_response_send_inform(const boost::thread::id thread_id);
178         EVENT_TAG handle_client_connection_check(const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
179         EVENT_TAG handle_client_select(const boost::thread::id thread_id, boost::asio::ip::udp::endpoint &cl_endpoint, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
180         EVENT_TAG handle_client_send(const boost::thread::id thread_id);
181         EVENT_TAG handle_client_disconnect(const boost::thread::id thread_id);
182         EVENT_TAG handle_sorry_enable(const boost::thread::id thread_id);
183         EVENT_TAG handle_sorry_disable(const boost::thread::id thread_id);
184         EVENT_TAG handle_realserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint);
185         EVENT_TAG handle_sorryserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint);
186         EVENT_TAG handle_realserver_close(const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint &rs_endpoint);
187         bool get_data_from_recvbuffer(thread_data_ptr data_ptr, const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen);
188         bool put_data_into_sendbuffer(thread_data_ptr data_ptr, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen);
189         bool create_x_forwarded_for(const std::string &client_endpoint,  const char *buffer,
190         const size_t buffer_len, size_t &x_forwarded_for_insert_pos,
191         std::string &x_forwarded_for_context);
192
193 };
194
195 }//namespace l7vs
196
197
198
199
200
201 #endif  //PROTOCOL_MODULE_IP_H
202