3 * @brief l7vsd control application.
5 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6 * Copyright (C) 2009 NTT COMWARE Corporation.
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.
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.
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
23 **********************************************************************/
28 #include <boost/function.hpp>
29 #include <boost/asio.hpp>
30 #include <boost/regex.hpp>
32 #include "error_code.h"
33 #include "l7vs_command.h"
35 #ifndef L7VS_MODULE_PATH
36 #define L7VS_MODULE_PATH "./"
39 #ifndef COMMAND_BUFFER_SIZE
40 #define COMMAND_BUFFER_SIZE (65535)
43 #ifndef L7VS_CONFIG_SOCK_PATH
44 #define L7VS_CONFIG_SOCK_PATH "/var/run/l7vs"
46 #define L7VS_CONFIG_SOCKNAME L7VS_CONFIG_SOCK_PATH "/l7vs"
48 #define L7VSADM_DEFAULT_SCHEDULER "rr" //!< Default scheduler name
49 #define L7VSADM_DEFAULT_WAIT_INTERVAL (1) //!< Default wait interval
50 #define L7VSADM_DEFAULT_WAIT_COUNT (10) //!< Default wait count
51 #define L7VSADM_MAX_WAIT (60) //!< Max wait value
52 #define L7VS_MODNAME_LEN (16) //!< Module name length
53 #define L7VS_FILENAME_LEN (256) //!< File name length
57 //! endpoint string parse function
58 //! @param[in] endpoint string
59 //! @param[out] error_code
62 typename T::endpoint string_to_endpoint( std::string& str, error_code& err ){
63 std::string::size_type pos = str.rfind( ":" );
64 std::string hostname = str.substr( 0, pos );
65 std::string portname = str.substr( pos+1, str.length() );
66 if( 0 == hostname.length() ){
67 err.setter( 1, "hostname is not specified:" );
68 return typename T::endpoint();
70 boost::regex re("\\d+");
71 if( boost::regex_match( portname.c_str(), re ) ){
73 boost::lexical_cast<unsigned short>(portname);
75 catch( boost::bad_lexical_cast& ){
76 err.setter( 1, "invalid port number:" );
77 return typename T::endpoint();
80 boost::asio::io_service io_service;
81 typename T::resolver resolver(io_service);
82 typename T::resolver::query query( hostname, portname );
83 typename T::resolver::iterator end;
84 boost::system::error_code ec;
85 typename T::resolver::iterator itr = resolver.resolve( query, ec );
87 std::stringstream buf;
88 buf << "invalid endpoint:" << ec.message() << ":";
89 err.setter( 1, buf.str() );
90 return typename T::endpoint();
92 if( itr == end ) return typename T::endpoint();
96 //! check endpoint function
97 //! @param[in] endpoint
98 //! @param[out] error_code
100 void check_endpoint( typename T::endpoint& ep, error_code& err ){
101 if( ep.address().is_v4() ){
102 if( ep.address().to_v4() == boost::asio::ip::address_v4::any() ){
103 err.setter( 1, "invalid address (INADDR_ANY):" );
108 if( ep.address().to_v6() == boost::asio::ip::address_v6::any() ){
109 err.setter( 1, "invalid address (INADDR_ANY):" );
113 if( ep.port() == 0 ){
114 err.setter( 1, "invalid port number (0):" );
119 //! endpoint to string function
120 //! @param[in] endpoint
121 //! @param[in] return numeric expression or not
122 //! @return endpoint string
124 std::string endpoint_to_string( typename T::endpoint ep, bool numeric_flag ){
125 std::stringstream buf;
127 boost::asio::io_service io_service;
128 typename T::resolver resolver(io_service);
129 boost::system::error_code ec;
130 typename T::resolver::iterator itr = resolver.resolve( ep, ec );
132 buf << itr->host_name() << ":" << itr->service_name();
136 buf << ep.address().to_string() << ":" << ep.port();
143 //! @brief l7vsadm execute file lock class
150 file_lock( const std::string& path, error_code& err )
153 fd = open( path.c_str(), O_RDONLY );
155 std::stringstream buf;
156 buf << boost::format( "L7vsadm execute file open error. file:%s" ) % path;
157 err.setter( true, buf.str() );
165 flock( fd, LOCK_UN );
173 //! try lock function
174 //! @return lock succeed(true) / lock failed(false)
176 lock = flock( fd, LOCK_EX | LOCK_NB );
178 // l7vsadm file lock OK.
186 // command parse functions.
188 //! list command parse function
189 bool parse_list_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
190 //! virtualservice command parse function
191 bool parse_vs_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
192 //! realserver_command parse function
193 bool parse_rs_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
194 //! replication command parse function
195 bool parse_replication_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
196 //! log command parse function
197 bool parse_log_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
198 //! snmpagent command parse function
199 bool parse_snmp_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
200 //! parameter parse function
201 bool parse_parameter_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
202 //! help command parse func
203 bool parse_help_func( l7vsadm_request::COMMAND_CODE_TAG, int, char*[] );
206 // option parse functions.
209 // list option functions.
210 //! list numeric flag check.
211 bool parse_opt_list_numeric_func( int&, int, char*[] );
213 // virtualservice option functions.
214 //! target option check
215 bool parse_opt_vs_target_func( int&, int, char*[] );
216 //! module option check
217 bool parse_opt_vs_module_func( int&, int, char*[] );
218 //! scheduler option check.
219 bool parse_opt_vs_scheduler_func( int&, int, char*[] );
221 bool parse_opt_vs_upper_func( int&, int, char*[] );
222 //! bypass(SorryServer) option check
223 bool parse_opt_vs_bypass_func( int&, int, char*[] );
224 //! virtualservice option flag function
225 bool parse_opt_vs_flag_func( int&, int, char*[] );
226 //! virtualservice option qosupstream function
227 bool parse_opt_vs_qosup_func( int&, int, char*[] );
228 //! virtualservice option qosdownstream functipn
229 bool parse_opt_vs_qosdown_func( int&, int, char*[] );
230 //! virtualservice option udp func.
231 bool parse_opt_vs_udp_func( int&, int, char*[] );
232 //! virtualservice option ssl function
233 bool parse_opt_vs_ssl_func( int&, int, char*[] );
234 // realserver option function
235 //! realserver weight set
236 bool parse_opt_rs_weight_func( int&, int, char*[] );
237 //! realserver target set
238 bool parse_opt_rs_realserver_func( int&, int, char*[] );
239 // replication option function
240 //! replication switch function
241 bool parse_opt_replication_switch_func( int&, int, char*[] );
242 //! replication start function
243 bool parse_opt_replication_start_func( int&, int, char*[] );
244 //! replication stop function
245 bool parse_opt_replication_stop_func( int&, int, char*[] );
246 //! replication force function
247 bool parse_opt_replication_force_func( int&, int, char*[] );
248 //! replication dump function
249 bool parse_opt_replication_dump_func( int&, int, char*[] );
250 // log option function
251 //! log category set function
252 bool parse_opt_log_category_func( int&, int, char*[] );
253 //! log level set function
254 bool parse_opt_log_level_func( int&, int, char*[] );
255 // snmp option function
256 //! snmp log category set function
257 bool parse_opt_snmp_log_category_func( int&, int, char*[] );
258 //! snmp log level set function
259 bool parse_opt_snmp_log_level_func( int&, int, char*[] );
260 // parameter option function
261 //! parameter reload component parsing
262 bool parse_opt_parameter_reload_func( int&, int, char*[] );
264 //! disp_list function
266 //! disp_list_key function
267 void disp_list_key();
268 //! disp_list_verbose function
269 void disp_list_verbose();
271 //! Get l7vsadm parameter data
272 void set_parameter();
274 //! argument dump for debug
275 std::string argument_debug_dump( int, char*[] );
277 //! command parse function object.type.
278 typedef boost::function< bool ( int, char*[] ) >
280 //! command string - parse function object map type.
281 typedef std::map< std::string, parse_cmd_func_type >
283 //! command function map dictionary.
284 parse_cmd_map_type command_dic;
286 //! option parse function object type.
287 typedef boost::function< bool ( int&, int, char*[] ) >
289 //! option string - parse function object map type
290 typedef std::map< std::string, parse_opt_func_type >
292 //! list option function map dictionary.
293 parse_opt_map_type list_option_dic;
294 //! virtualservice option function map dictionary
295 parse_opt_map_type vs_option_dic;
296 //! realserver option function map dictionary
297 parse_opt_map_type rs_option_dic;
298 //! replication option function map dictionary
299 parse_opt_map_type replication_option_dic;
300 //! replication switch option function map dictionary
301 parse_opt_map_type replication_switch_option_dic;
302 //! log option function map dictionary
303 parse_opt_map_type log_option_dic;
304 //! snmp option_function map dictionary
305 parse_opt_map_type snmp_option_dic;
306 //! parameter option function map dictionary
307 parse_opt_map_type parameter_option_dic;
309 //! log category string -> log category enum convert map type.
310 typedef std::map< std::string, LOG_CATEGORY_TAG > string_logcategory_map_type;
311 //! log category string to log category enum dictionary.
312 string_logcategory_map_type string_logcategory_dic;
313 //! snmp log category string to snmp log category enum dictionary.
314 string_logcategory_map_type string_snmp_logcategory_dic;
316 //! log category enum -> log category string convert map type.
317 typedef std::map< LOG_CATEGORY_TAG, std::string > logcategory_string_map_type;
318 //! log category enum to log category string dictionary.
319 logcategory_string_map_type logcategory_string_dic;
320 //! snmp log category enum to snmp log category string dictionary.
321 logcategory_string_map_type snmp_logcategory_string_dic;
323 //! log level string -> log level enum convert map type
324 typedef std::map< std::string, LOG_LEVEL_TAG > string_loglevel_map_type;
325 //! log level string to log level enum dictionary
326 string_loglevel_map_type string_loglevel_dic;
328 //! log level enum convert map -> log level string type
329 typedef std::map< LOG_LEVEL_TAG, std::string > loglevel_string_map_type;
330 //! log level enum to log level string dictionary
331 loglevel_string_map_type loglevel_string_dic;
333 //! parameter category string -> parameter category enum convert map type
334 typedef std::map< std::string, PARAMETER_COMPONENT_TAG > string_parameter_map_type;
335 string_parameter_map_type string_parameter_dic;
336 //! COMMAND_RESPONSE_CODE -> message convert map type
337 typedef std::map< l7vsd_response::COMMAND_RESPONSE_CODE, std::string > response_error_message_map_type;
338 response_error_message_map_type response_error_message_dic;
340 //! disp result function object type.
341 typedef boost::function< void () >
342 disp_result_func_type;
343 //! command - disp result function object map type
344 typedef std::map< l7vsadm_request::COMMAND_CODE_TAG, disp_result_func_type >
345 disp_result_map_type;
346 //! disp result function map dictionary.
347 disp_result_map_type disp_result_dic;
349 //! replication mode enum -> replication mode string convert map type
350 typedef std::map< replication::REPLICATION_MODE_TAG, std::string > replication_mode_string_map_type;
351 //! replication mode enum to replication mode string dictionary
352 replication_mode_string_map_type replication_mode_string_dic;
358 // l7vsd request data
359 l7vsadm_request request; //!< send_request
360 l7vsd_response response; //!< recv_response
363 // l7vsadm using datas.
364 bool numeric_flag; //!< numeric flag
365 boost::asio::io_service io_service; //!< io_service
367 error_code l7vsadm_err;
369 //! Interval of l7vsadm command conflict check.
370 int command_wait_interval;
371 //! Number of times of l7vsadm command conflict check.
372 int command_wait_count;
373 //! Interval of connected check to l7vsd.
374 int connect_wait_interval;
375 //! Number of times of connected check to l7vsd.
376 int connect_wait_count;
383 bool execute( int, char*[] );