Parcourir la source

[master] Merge branch 'trac3983' (Decline parameters in v4 and v6)

Tomek Mrugalski il y a 9 ans
Parent
commit
6b10d119c8

+ 2 - 1
src/Makefile.am

@@ -3,4 +3,5 @@ SUBDIRS = lib bin hooks
 EXTRA_DIST = \
 	cppcheck-suppress.lst		\
 	valgrind-suppressions		\
-	valgrind-suppressions.revisit
+	valgrind-suppressions.revisit   \
+        defaults.h

+ 1 - 0
src/bin/dhcp4/Makefile.am

@@ -2,6 +2,7 @@ SUBDIRS = . tests
 
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin -I$(top_builddir)/src/bin
+AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 if HAVE_MYSQL
 AM_CPPFLAGS += $(MYSQL_CPPFLAGS)

+ 22 - 4
src/bin/dhcp4/json_config_parser.cc

@@ -30,6 +30,7 @@
 #include <config/command_mgr.h>
 #include <util/encode/hex.h>
 #include <util/strutil.h>
+#include <defaults.h>
 
 #include <boost/foreach.hpp>
 #include <boost/lexical_cast.hpp>
@@ -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 Sets global parameters in staging configuration
+///
+/// Currently this method sets the following global parameters:
+///
+/// - echo-client-id
+/// - decline-probation-period
+void setGlobalParameters4() {
     // 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
@@ -591,8 +609,8 @@ configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) {
             // No need to commit interface names as this is handled by the
             // CfgMgr::commit() function.
 
-            // Apply global options
-            commitGlobalOptions();
+            // Apply global options in the staging config.
+            setGlobalParameters4();
 
             // This occurs last as if it succeeds, there is no easy way
             // revert it.  As a result, the failure to commit a subsequent

+ 1 - 0
src/bin/dhcp4/tests/Makefile.am

@@ -17,6 +17,7 @@ check-local:
 	done
 
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += -I$(top_builddir)/src/bin # for generated spec_config.h header
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin
 AM_CPPFLAGS += -I$(top_builddir)/src/lib/cc

+ 65 - 0
src/bin/dhcp4/tests/config_parser_unittest.cc

@@ -33,6 +33,7 @@
 #include <dhcpsrv/cfg_subnets4.h>
 #include <dhcpsrv/testutils/config_result_check.h>
 #include <hooks/hooks_manager.h>
+#include <defaults.h>
 
 #include "marker_file.h"
 #include "test_libraries.h"
@@ -3647,4 +3648,68 @@ 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());
+}
+
+/// Check that an incorrect decline-probation-period value will be caught.
+TEST_F(Dhcp4ParserTest, declineTimerError) {
+    ConstElementPtr status;
+
+    string config = "{ " + genIfaceConfig() + "," +
+        "\"decline-probation-period\": \"soon\","
+        "\"subnet4\": [ ]"
+        "}";
+
+    ElementPtr json = Element::fromJSON(config);
+
+    EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
+
+    // returned value should be 1 (error)
+    checkResult(status, 1);
+
+    // Check that the error contains error position.
+    EXPECT_TRUE(errorContainsPosition(status, "<string>"));
+}
+
 }

+ 1 - 0
src/bin/dhcp6/Makefile.am

@@ -3,6 +3,7 @@ SUBDIRS = . tests
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin -I$(top_builddir)/src/bin
 AM_CPPFLAGS += -I$(top_srcdir)/src/lib/cc -I$(top_builddir)/src/lib/cc
+AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 if HAVE_MYSQL
 AM_CPPFLAGS += $(MYSQL_CPPFLAGS)

+ 24 - 1
src/bin/dhcp6/json_config_parser.cc

@@ -36,6 +36,7 @@
 #include <log/logger_support.h>
 #include <util/encode/hex.h>
 #include <util/strutil.h>
+#include <defaults.h>
 
 #include <boost/algorithm/string.hpp>
 #include <boost/foreach.hpp>
@@ -668,7 +669,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) {
@@ -704,6 +706,24 @@ namespace dhcp {
     return (parser);
 }
 
+/// @brief Sets global parameters in the staging configuration
+///
+/// Currently this method sets the following global parameters:
+///
+/// - decline-probation-period
+void setGlobalParameters6() {
+
+    // 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) {
@@ -872,6 +892,9 @@ configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) {
                 subnet_parser->commit();
             }
 
+            // Apply global options in the staging config.
+            setGlobalParameters6();
+
             // No need to commit interface names as this is handled by the
             // CfgMgr::commit() function.
 

+ 1 - 0
src/bin/dhcp6/tests/Makefile.am

@@ -20,6 +20,7 @@ check-local:
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_builddir)/src/bin # for generated spec_config.h header
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin
+AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += -DTOP_BUILDDIR="\"$(top_builddir)\""
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/bin/dhcp6/tests\"

+ 65 - 0
src/bin/dhcp6/tests/config_parser_unittest.cc

@@ -31,6 +31,7 @@
 #include <dhcpsrv/subnet_selector.h>
 #include <dhcpsrv/testutils/config_result_check.h>
 #include <hooks/hooks_manager.h>
+#include <defaults.h>
 
 #include "test_data_files_config.h"
 #include "test_libraries.h"
@@ -3982,4 +3983,68 @@ TEST_F(Dhcp6ParserTest, rsooBogusName) {
     EXPECT_TRUE(errorContainsPosition(status, "<string>"));
 }
 
+/// 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());
+}
+
+/// Check that an incorrect decline-probation-period value will be caught.
+TEST_F(Dhcp6ParserTest, declineTimerError) {
+    ConstElementPtr status;
+
+    string config = "{ " + genIfaceConfig() + "," +
+        "\"decline-probation-period\": \"soon\","
+        "\"subnet6\": [ ]"
+        "}";
+
+    ElementPtr json = Element::fromJSON(config);
+
+    EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
+
+    // returned value should be 1 (error)
+    checkResult(status, 1);
+
+    // Check that the error contains error position.
+    EXPECT_TRUE(errorContainsPosition(status, "<string>"));
+}
+
+
 };

+ 41 - 0
src/defaults.h

@@ -0,0 +1,41 @@
+// 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 for various parameters.
+///
+/// While the content of this file is currently small, it is envisaged that it
+/// will grow over time.
+
+#ifndef DEFAULTS_H
+#define DEFAULTS_H
+
+#include <stdint.h>
+
+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

+ 1 - 0
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

+ 4 - 2
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

+ 24 - 0
src/lib/dhcpsrv/srv_config.h

@@ -376,6 +376,24 @@ public:
     /// @ref CfgSubnets6::removeStatistics for details.
     void removeStatistics();
 
+    /// @brief Sets decline probation-period
+    ///
+    /// Probation-period is the timer, expressed, in seconds, that specifies how
+    /// long a lease is unavailable after reported as declined.
+    ///
+    /// @param decline_timer number of seconds after declined lease is restored
+    void setDeclinePeriod(const uint32_t decline_timer) {
+        decline_timer_ = decline_timer;
+    }
+
+    /// @brief Returns probation-period
+    ///
+    /// See @ref setDeclinePeriod for brief discussion.
+    /// @return value of probation-period, expressed in seconds
+    uint32_t getDeclinePeriod() const {
+        return (decline_timer_);
+    }
+
 private:
 
     /// @brief Sequence number identifying the configuration.
@@ -425,6 +443,12 @@ private:
 
     /// @brief Pointer to the control-socket information
     isc::data::ConstElementPtr control_socket_;
+
+    /// @brief Decline Period time
+    ///
+    /// This timer specifies decline probation period, the time after a declined
+    /// lease is recovered back to available state. Expressed in seconds.
+    uint32_t decline_timer_;
 };
 
 /// @name Pointers to the @c SrvConfig object.