From: Tomek Mrugalski Date: Thu, 20 Aug 2015 18:51:28 +0000 (+0200) Subject: [3983] Support for 'decline-probation-period' implemented. X-Git-Tag: fd4o6_base~11^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91bfb283b0c14c52f77bf86a0a97e7ed8dcf9714;p=thirdparty%2Fkea.git [3983] Support for 'decline-probation-period' implemented. --- diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index 604f87d683..39f36521bc 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -375,7 +376,8 @@ namespace dhcp { DhcpConfigParser* parser = NULL; if ((config_id.compare("valid-lifetime") == 0) || (config_id.compare("renew-timer") == 0) || - (config_id.compare("rebind-timer") == 0)) { + (config_id.compare("rebind-timer") == 0) || + (config_id.compare("decline-probation-period") == 0) ) { parser = new Uint32Parser(config_id, globalContext()->uint32_values_); } else if (config_id.compare("interfaces-config") == 0) { @@ -411,7 +413,13 @@ namespace dhcp { return (parser); } -void commitGlobalOptions() { +/// @brief Commits global parameters +/// +/// Currently this method sets the following global parameters: +/// +/// - echo-client-id +/// - decline-probation-period +void commitGlobalParameters4() { // Although the function is modest for now, it is certain that the number // of global switches will increase over time, hence the name. @@ -423,6 +431,16 @@ void commitGlobalOptions() { } catch (...) { // Ignore errors. This flag is optional } + + // Set the probation period for decline handling. + try { + uint32_t probation_period = globalContext()->uint32_values_ + ->getOptionalParam("decline-probation-period", + DEFAULT_DECLINE_PROBATION_PERIOD); + CfgMgr::instance().getStagingCfg()->setDeclinePeriod(probation_period); + } catch (...) { + // That's not really needed. + } } isc::data::ConstElementPtr @@ -592,7 +610,7 @@ configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) { // CfgMgr::commit() function. // Apply global options - commitGlobalOptions(); + commitGlobalParameters4(); // This occurs last as if it succeeds, there is no easy way // revert it. As a result, the failure to commit a subsequent diff --git a/src/bin/dhcp4/tests/config_parser_unittest.cc b/src/bin/dhcp4/tests/config_parser_unittest.cc index 17c04482b8..91c816a7d9 100644 --- a/src/bin/dhcp4/tests/config_parser_unittest.cc +++ b/src/bin/dhcp4/tests/config_parser_unittest.cc @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "marker_file.h" @@ -3647,4 +3648,49 @@ TEST_F(Dhcp4ParserTest, hostReservationPerSubnet) { EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode()); } +/// Check that the decline-probation-period has a default value when not +/// specified. +TEST_F(Dhcp4ParserTest, declineTimerDefault) { + ConstElementPtr status; + + string config = "{ " + genIfaceConfig() + "," + + "\"subnet4\": [ ]" + "}"; + + ElementPtr json = Element::fromJSON(config); + + EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json)); + + // returned value should be 0 (success) + checkResult(status, 0); + + // The value of decline-probation-perion must be equal to the + // default value. + EXPECT_EQ(DEFAULT_DECLINE_PROBATION_PERIOD, + CfgMgr::instance().getStagingCfg()->getDeclinePeriod()); +} + +/// Check that the decline-probation-period value can be set properly. +TEST_F(Dhcp4ParserTest, declineTimer) { + ConstElementPtr status; + + string config = "{ " + genIfaceConfig() + "," + + "\"decline-probation-period\": 12345," + "\"subnet4\": [ ]" + "}"; + + ElementPtr json = Element::fromJSON(config); + + EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json)); + + // returned value should be 0 (success) + checkResult(status, 0); + + // The value of decline-probation-perion must be equal to the + // value specified. + EXPECT_EQ(12345, + CfgMgr::instance().getStagingCfg()->getDeclinePeriod()); +} + + } diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 3f3f45a60a..1474c8ca6d 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -666,7 +667,8 @@ namespace dhcp { if ((config_id.compare("preferred-lifetime") == 0) || (config_id.compare("valid-lifetime") == 0) || (config_id.compare("renew-timer") == 0) || - (config_id.compare("rebind-timer") == 0)) { + (config_id.compare("rebind-timer") == 0) || + (config_id.compare("decline-probation-period") == 0) ) { parser = new Uint32Parser(config_id, globalContext()->uint32_values_); } else if (config_id.compare("interfaces-config") == 0) { @@ -702,6 +704,24 @@ namespace dhcp { return (parser); } +/// @brief Commits global parameters +/// +/// Currently this method sets the following global parameters: +/// +/// - decline-probation-period +void commitGlobalParameters6() { + + // Set the probation period for decline handling. + try { + uint32_t probation_period = globalContext()->uint32_values_ + ->getOptionalParam("decline-probation-period", + DEFAULT_DECLINE_PROBATION_PERIOD); + CfgMgr::instance().getStagingCfg()->setDeclinePeriod(probation_period); + } catch (...) { + // That's not really needed. + } +} + isc::data::ConstElementPtr configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) { if (!config_set) { @@ -870,6 +890,9 @@ configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) { subnet_parser->commit(); } + // Commit global options + commitGlobalParameters6(); + // No need to commit interface names as this is handled by the // CfgMgr::commit() function. diff --git a/src/bin/dhcp6/tests/config_parser_unittest.cc b/src/bin/dhcp6/tests/config_parser_unittest.cc index 5296bc2d2c..6e9f738348 100644 --- a/src/bin/dhcp6/tests/config_parser_unittest.cc +++ b/src/bin/dhcp6/tests/config_parser_unittest.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -3982,4 +3983,49 @@ TEST_F(Dhcp6ParserTest, rsooBogusName) { EXPECT_TRUE(errorContainsPosition(status, "")); } +/// Check that the decline-probation-period value can be set properly. +TEST_F(Dhcp6ParserTest, declineTimerDefault) { + + ConstElementPtr status; + + string config_txt = "{ " + genIfaceConfig() + "," + "\"subnet6\": [ ] " + "}"; + ElementPtr config = Element::fromJSON(config_txt); + + EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config)); + + // returned value should be 0 (success) + checkResult(status, 0); + + // The value of decline-probation-perion must be equal to the + // default value. + EXPECT_EQ(DEFAULT_DECLINE_PROBATION_PERIOD, + CfgMgr::instance().getStagingCfg()->getDeclinePeriod()); +} + +/// Check that the decline-probation-period value can be set properly. +TEST_F(Dhcp6ParserTest, declineTimer) { + ConstElementPtr status; + + string config = "{ " + genIfaceConfig() + "," + + "\"decline-probation-period\": 12345," + "\"subnet6\": [ ]" + "}"; + + ElementPtr json = Element::fromJSON(config); + + EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json)); + + // returned value should be 0 (success) + checkResult(status, 0); + + // The value of decline-probation-perion must be equal to the + // value specified. + EXPECT_EQ(12345, + CfgMgr::instance().getStagingCfg()->getDeclinePeriod()); +} + + + }; diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index 1478e774ef..d36c3b3ad6 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -92,6 +92,7 @@ libkea_dhcpsrv_la_SOURCES += csv_lease_file6.cc csv_lease_file6.h libkea_dhcpsrv_la_SOURCES += d2_client_cfg.cc d2_client_cfg.h libkea_dhcpsrv_la_SOURCES += d2_client_mgr.cc d2_client_mgr.h libkea_dhcpsrv_la_SOURCES += daemon.cc daemon.h +libkea_dhcpsrv_la_SOURCES += defaults.h libkea_dhcpsrv_la_SOURCES += dhcpsrv_log.cc dhcpsrv_log.h libkea_dhcpsrv_la_SOURCES += host.cc host.h libkea_dhcpsrv_la_SOURCES += host_container.h diff --git a/src/lib/dhcpsrv/defaults.h b/src/lib/dhcpsrv/defaults.h new file mode 100644 index 0000000000..2addfacd74 --- /dev/null +++ b/src/lib/dhcpsrv/defaults.h @@ -0,0 +1,38 @@ +// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +/// @file defaults.h +/// +/// @brief Contains the default values of the server. + +#ifndef DEFAULTS_H +#define DEFAULTS_H + +#include + +namespace isc { +namespace dhcp { + +/// @brief Number of seconds after declined lease recovers +/// +/// This define specifies the default value for decline probation period. +/// Once a lease is declined, it will spend this amount of seconds as +/// being unavailable. This is only the default value. Specific value may +/// be defined in the configuration file. The default is 1 day. +static const uint32_t DEFAULT_DECLINE_PROBATION_PERIOD = 24*3600; + +}; +}; + +#endif diff --git a/src/lib/dhcpsrv/srv_config.cc b/src/lib/dhcpsrv/srv_config.cc index 823c6708b4..bdf4ea3763 100644 --- a/src/lib/dhcpsrv/srv_config.cc +++ b/src/lib/dhcpsrv/srv_config.cc @@ -30,14 +30,16 @@ SrvConfig::SrvConfig() : sequence_(0), cfg_iface_(new CfgIface()), cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()), cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()), - cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()) { + cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()), + decline_timer_(0) { } SrvConfig::SrvConfig(const uint32_t sequence) : sequence_(sequence), cfg_iface_(new CfgIface()), cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()), cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()), - cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()) { + cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()), + decline_timer_(0) { } std::string diff --git a/src/lib/dhcpsrv/srv_config.h b/src/lib/dhcpsrv/srv_config.h index 0f860e60b8..ba6149b627 100644 --- a/src/lib/dhcpsrv/srv_config.h +++ b/src/lib/dhcpsrv/srv_config.h @@ -376,6 +376,17 @@ public: /// @ref CfgSubnets6::removeStatistics for details. void removeStatistics(); + /// @brief Sets decline probation-period + /// @param decline_timer number of seconds after declined lease is restored + void setDeclinePeriod(uint32_t decline_timer) { + decline_timer_ = decline_timer; + } + + /// @brief + uint32_t getDeclinePeriod() const { + return (decline_timer_); + } + private: /// @brief Sequence number identifying the configuration. @@ -425,6 +436,9 @@ private: /// @brief Pointer to the control-socket information isc::data::ConstElementPtr control_socket_; + + /// @brief Decline Period time + uint32_t decline_timer_; }; /// @name Pointers to the @c SrvConfig object.