Browse Source

[3360] Set the lease file location for the Memfile backend.

Marcin Siodelski 11 years ago
parent
commit
88960c7130

+ 0 - 7
src/lib/dhcpsrv/dhcpsrv_messages.mes

@@ -291,13 +291,6 @@ lease from the memory file database for the specified address.
 A debug message issued when the server is attempting to update IPv6
 A debug message issued when the server is attempting to update IPv6
 lease from the memory file database for the specified address.
 lease from the memory file database for the specified address.
 
 
-% DHCPSRV_MEMFILE_WARNING using early version of memfile lease database - leases will be lost after a restart
-This warning message is issued when the 'memfile' lease database is
-opened.  The current version of memfile does not store anything
-to disk, so lease information will be lost in the event of a restart.
-Using this version of memfile in a production environment is NOT
-recommended.
-
 % DHCPSRV_MYSQL_ADD_ADDR4 adding IPv4 lease with address %1
 % DHCPSRV_MYSQL_ADD_ADDR4 adding IPv4 lease with address %1
 A debug message issued when the server is about to add an IPv4 lease
 A debug message issued when the server is about to add an IPv4 lease
 with the specified address to the MySQL backend database.
 with the specified address to the MySQL backend database.

+ 34 - 2
src/lib/dhcpsrv/memfile_lease_mgr.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -12,6 +12,7 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
+#include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/dhcpsrv_log.h>
 #include <dhcpsrv/dhcpsrv_log.h>
 #include <dhcpsrv/memfile_lease_mgr.h>
 #include <dhcpsrv/memfile_lease_mgr.h>
 #include <exceptions/exceptions.h>
 #include <exceptions/exceptions.h>
@@ -22,7 +23,9 @@ using namespace isc::dhcp;
 
 
 Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
 Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
     : LeaseMgr(parameters) {
     : LeaseMgr(parameters) {
-    LOG_WARN(dhcpsrv_logger, DHCPSRV_MEMFILE_WARNING);
+    // Get the lease files locations.
+    lease_file4_ = initLeaseFilePath(V4);
+    lease_file6_ = initLeaseFilePath(V6);
 }
 }
 
 
 Memfile_LeaseMgr::~Memfile_LeaseMgr() {
 Memfile_LeaseMgr::~Memfile_LeaseMgr() {
@@ -310,3 +313,32 @@ Memfile_LeaseMgr::rollback() {
     LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
     LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
               DHCPSRV_MEMFILE_ROLLBACK);
               DHCPSRV_MEMFILE_ROLLBACK);
 }
 }
+
+std::string
+Memfile_LeaseMgr::getDefaultLeaseFilePath(Universe u) const {
+    std::ostringstream s;
+    s << CfgMgr::instance().getDataDir() << "/kea-leases";
+    s << (u == V4 ? "4" : "6");
+    s << ".csv";
+    return (s.str());
+}
+
+bool
+Memfile_LeaseMgr::persistLeases(Universe u) const {
+    // Currently, if the lease file is empty, it means that writes to disk have
+    // been explicitly disabled by the administrator. At some point, there may
+    // be a dedicated ON/OFF flag implemented to control this.
+    return (u == V4 ? !lease_file4_.empty() : !lease_file6_.empty());
+}
+
+std::string
+Memfile_LeaseMgr::initLeaseFilePath(Universe u) {
+    std::string param_name = (u == V4 ? "leasefile4" : "leasefile6");
+    std::string lease_file;
+    try {
+        lease_file = getParameter(param_name);
+    } catch (const Exception& ex) {
+        lease_file = getDefaultLeaseFilePath(u);
+    }
+    return (lease_file);
+}

+ 62 - 2
src/lib/dhcpsrv/memfile_lease_mgr.h

@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -38,6 +38,17 @@ namespace dhcp {
 class Memfile_LeaseMgr : public LeaseMgr {
 class Memfile_LeaseMgr : public LeaseMgr {
 public:
 public:
 
 
+    /// @brief Specifies universe (V4, V6)
+    ///
+    /// This enumeration is used by various functions in Memfile Lease Manager,
+    /// to identify the lease type referred to. In particular, it is used by
+    /// functions operating on the lease files to distinguish between lease
+    /// files for DHCPv4 and DHCPv6.
+    enum Universe {
+        V4,
+        V6
+    };
+
     /// @brief The sole lease manager constructor
     /// @brief The sole lease manager constructor
     ///
     ///
     /// dbconfig is a generic way of passing parameters. Parameters
     /// dbconfig is a generic way of passing parameters. Parameters
@@ -245,8 +256,51 @@ public:
     /// support transactions, this is a no-op.
     /// support transactions, this is a no-op.
     virtual void rollback();
     virtual void rollback();
 
 
+    /// @brief Returns default path to the lease file.
+    ///
+    /// @param u Universe (V4 or V6).
+    std::string getDefaultLeaseFilePath(Universe u) const;
+
+    /// @brief Returns an absolute path to the lease file.
+    ///
+    /// @param u Universe (V4 or V6).
+    std::string getLeaseFilePath(Universe u) const {
+        return (u == V4 ? lease_file4_ : lease_file6_);
+    }
+
+    /// @brief Specifies whether or not leases are written to disk.
+    ///
+    /// It is possible that leases for DHCPv4 are written to disk whereas leases
+    /// for DHCPv6 are not; or vice versa. The argument of the method specifies
+    /// the type of lease in that respect.
+    ///
+    /// @param u Universe (V4 or V6).
+    ///
+    /// @return true if leases are written to lease file; if false is
+    /// returned, leases will be held in memory and will be lost upon
+    /// server shut down.
+    bool persistLeases(Universe u) const;
+
 protected:
 protected:
 
 
+    /// @brief Initialize the location of the lease file.
+    ///
+    /// This method uses the parameters passed as a map to the constructor to
+    /// initialize the location of the lease file. If the lease file is not
+    /// specified, the method will use the default location for the universe
+    /// (v4 or v6) selected. If the location is specified in the map as empty
+    /// it will set the empty location, which implies that leases belonging to
+    /// the specified universe will not be written to disk.
+    ///
+    /// @param u Universe (v4 or v6)
+    /// @param parameters Map holding parameters of the Lease Manager, passed to
+    /// the constructor.
+    ///
+    /// @return The location of the lease file that should be assigned to the
+    /// lease_file4_ or lease_file6_, depending on the universe specified as an
+    /// argument to this function.
+    std::string initLeaseFilePath(Universe u);
+
     // This is a multi-index container, which holds elements that can
     // This is a multi-index container, which holds elements that can
     // be accessed using different search indexes.
     // be accessed using different search indexes.
     typedef boost::multi_index_container<
     typedef boost::multi_index_container<
@@ -283,7 +337,7 @@ protected:
     // be accessed using different search indexes.
     // be accessed using different search indexes.
     typedef boost::multi_index_container<
     typedef boost::multi_index_container<
         // It holds pointers to Lease4 objects.
         // It holds pointers to Lease4 objects.
-        Lease4Ptr, 
+        Lease4Ptr,
         // Specification of search indexes starts here.
         // Specification of search indexes starts here.
         boost::multi_index::indexed_by<
         boost::multi_index::indexed_by<
             // Specification of the first index starts here.
             // Specification of the first index starts here.
@@ -354,6 +408,12 @@ protected:
 
 
     /// @brief stores IPv6 leases
     /// @brief stores IPv6 leases
     Lease6Storage storage6_;
     Lease6Storage storage6_;
+
+    /// @brief Holds the absolute path to the lease file for DHCPv4.
+    std::string lease_file4_;
+
+    /// @brief Holds the absolute path to the lease file for DHCPv6.
+    std::string lease_file6_;
 };
 };
 
 
 }; // end of isc::dhcp namespace
 }; // end of isc::dhcp namespace

+ 1 - 0
src/lib/dhcpsrv/tests/Makefile.am

@@ -3,6 +3,7 @@ SUBDIRS = .
 AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
 AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/dhcp/tests\"
 AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/dhcp/tests\"
+AM_CPPFLAGS += -DDHCP_DATA_DIR=\"$(abs_top_builddir)/src/lib/dhcp/tests\"
 AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\"
 AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\"
 
 
 AM_CXXFLAGS = $(B10_CXXFLAGS)
 AM_CXXFLAGS = $(B10_CXXFLAGS)

+ 76 - 0
src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc

@@ -16,6 +16,7 @@
 
 
 #include <asiolink/io_address.h>
 #include <asiolink/io_address.h>
 #include <dhcp/duid.h>
 #include <dhcp/duid.h>
+#include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/lease_mgr.h>
 #include <dhcpsrv/lease_mgr.h>
 #include <dhcpsrv/memfile_lease_mgr.h>
 #include <dhcpsrv/memfile_lease_mgr.h>
 #include <dhcpsrv/tests/test_utils.h>
 #include <dhcpsrv/tests/test_utils.h>
@@ -56,6 +57,18 @@ public:
         lmptr_ = 0;
         lmptr_ = 0;
     }
     }
 
 
+    /// @brief Return path to the lease file used by unit tests.
+    ///
+    /// @param filename Name of the lease file appended to the path to the
+    /// directory where test data is held.
+    ///
+    /// @return Full path to the lease file.
+    std::string getLeaseFilePath(const std::string& filename) const {
+        std::ostringstream s;
+        s << TEST_DATA_BUILDDIR << "/" << filename;
+        return (s.str());
+    }
+
 };
 };
 
 
 // This test checks if the LeaseMgr can be instantiated and that it
 // This test checks if the LeaseMgr can be instantiated and that it
@@ -74,6 +87,69 @@ TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
     EXPECT_EQ(std::string("memory"),  lmptr_->getName());
     EXPECT_EQ(std::string("memory"),  lmptr_->getName());
 }
 }
 
 
+// Checks if the path to the lease files is initialized correctly.
+TEST_F(MemfileLeaseMgrTest, getLeaseFilePath) {
+    LeaseMgr::ParameterMap pmap;
+    boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
+
+    std::ostringstream s4;
+    s4 << CfgMgr::instance().getDataDir() << "/" << "kea-leases4.csv";
+    std::ostringstream s6;
+    s6 << CfgMgr::instance().getDataDir() << "/" << "kea-leases6.csv";
+    EXPECT_EQ(s4.str(),
+              lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V4));
+    EXPECT_EQ(s6.str(),
+              lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6));
+
+
+    EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V4),
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
+
+    EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6),
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
+
+    pmap["leasefile4"] = getLeaseFilePath("leasefile4.csv");
+    lease_mgr.reset(new Memfile_LeaseMgr(pmap));
+    EXPECT_EQ(pmap["leasefile4"],
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
+    EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6),
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
+
+    pmap["leasefile6"] = getLeaseFilePath("kea-leases6.csv");
+    lease_mgr.reset(new Memfile_LeaseMgr(pmap));
+    EXPECT_EQ(pmap["leasefile4"],
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
+    EXPECT_EQ(pmap["leasefile6"],
+              lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
+}
+
+// Check if the persitLeases correctly checks that leases should not be written
+// to disk when lease file is set to empty value.
+TEST_F(MemfileLeaseMgrTest, persistLeases) {
+    LeaseMgr::ParameterMap pmap;
+    boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
+    // If the leasefile4 and leasefile6 are not specified, the default
+    // file names will be used. The leases will be written to these files.
+    EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
+    EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
+
+    // Specify the names of the lease files. Leases will be written.
+    pmap["leasefile4"] = "leases4.csv";
+    pmap["leasefile6"] = "leases6.csv";
+    lease_mgr.reset(new Memfile_LeaseMgr(pmap));
+    EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
+    EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
+
+    // Specify empty names of the lease files. This should disable writes
+    // of leases to disk.
+    pmap["leasefile4"] = "";
+    pmap["leasefile6"] = "";
+    lease_mgr.reset(new Memfile_LeaseMgr(pmap));
+    EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
+    EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
+}
+
+
 // Checks that adding/getting/deleting a Lease6 object works.
 // Checks that adding/getting/deleting a Lease6 object works.
 TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
 TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
     testAddGetDelete6(true); // true - check T1,T2 values
     testAddGetDelete6(true); // true - check T1,T2 values