Browse Source

[2324] AllocEngine is now part of b10-dhcpsrv lib.

Tomek Mrugalski 12 years ago
parent
commit
ee7d5cf204

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

@@ -47,7 +47,6 @@ pkglibexec_PROGRAMS = b10-dhcp6
 b10_dhcp6_SOURCES  = main.cc
 b10_dhcp6_SOURCES  = main.cc
 b10_dhcp6_SOURCES += ctrl_dhcp6_srv.cc ctrl_dhcp6_srv.h
 b10_dhcp6_SOURCES += ctrl_dhcp6_srv.cc ctrl_dhcp6_srv.h
 b10_dhcp6_SOURCES += config_parser.cc config_parser.h
 b10_dhcp6_SOURCES += config_parser.cc config_parser.h
-b10_dhcp6_SOURCES += alloc_engine.cc alloc_engine.h
 b10_dhcp6_SOURCES += dhcp6_log.cc dhcp6_log.h
 b10_dhcp6_SOURCES += dhcp6_log.cc dhcp6_log.h
 b10_dhcp6_SOURCES += dhcp6_srv.cc dhcp6_srv.h
 b10_dhcp6_SOURCES += dhcp6_srv.cc dhcp6_srv.h
 
 

+ 2 - 1
src/lib/dhcp/Makefile.am

@@ -16,7 +16,6 @@ CLEANFILES = *.gcno *.gcda
 lib_LTLIBRARIES = libb10-dhcp++.la libb10-dhcpsrv.la
 lib_LTLIBRARIES = libb10-dhcp++.la libb10-dhcpsrv.la
 libb10_dhcp___la_SOURCES  =
 libb10_dhcp___la_SOURCES  =
 libb10_dhcp___la_SOURCES += libdhcp++.cc libdhcp++.h
 libb10_dhcp___la_SOURCES += libdhcp++.cc libdhcp++.h
-libb10_dhcp___la_SOURCES += lease_mgr.cc lease_mgr.h
 libb10_dhcp___la_SOURCES += iface_mgr.cc iface_mgr.h
 libb10_dhcp___la_SOURCES += iface_mgr.cc iface_mgr.h
 libb10_dhcp___la_SOURCES += iface_mgr_linux.cc
 libb10_dhcp___la_SOURCES += iface_mgr_linux.cc
 libb10_dhcp___la_SOURCES += iface_mgr_bsd.cc
 libb10_dhcp___la_SOURCES += iface_mgr_bsd.cc
@@ -35,7 +34,9 @@ libb10_dhcpsrv_la_SOURCES  = cfgmgr.cc cfgmgr.h
 libb10_dhcpsrv_la_SOURCES += pool.cc pool.h
 libb10_dhcpsrv_la_SOURCES += pool.cc pool.h
 libb10_dhcpsrv_la_SOURCES += subnet.cc subnet.h
 libb10_dhcpsrv_la_SOURCES += subnet.cc subnet.h
 libb10_dhcpsrv_la_SOURCES += triplet.h
 libb10_dhcpsrv_la_SOURCES += triplet.h
+libb10_dhcpsrv_la_SOURCES += lease_mgr.cc lease_mgr.h
 libb10_dhcpsrv_la_SOURCES += addr_utilities.cc addr_utilities.h
 libb10_dhcpsrv_la_SOURCES += addr_utilities.cc addr_utilities.h
+libb10_dhcpsrv_la_SOURCES += alloc_engine.cc alloc_engine.h
 libb10_dhcpsrv_la_CXXFLAGS = $(AM_CXXFLAGS)
 libb10_dhcpsrv_la_CXXFLAGS = $(AM_CXXFLAGS)
 libb10_dhcpsrv_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libb10_dhcpsrv_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libb10_dhcpsrv_la_LIBADD   = $(top_builddir)/src/lib/asiolink/libb10-asiolink.la
 libb10_dhcpsrv_la_LIBADD   = $(top_builddir)/src/lib/asiolink/libb10-asiolink.la

+ 38 - 15
src/bin/dhcp6/alloc_engine.cc

@@ -28,6 +28,8 @@ isc::asiolink::IOAddress
 AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) {
 AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) {
     uint8_t packed[V6ADDRESS_LEN];
     uint8_t packed[V6ADDRESS_LEN];
     int len;
     int len;
+
+    // First we copy the whole address as 16 bytes.
     if (addr.getFamily()==AF_INET) {
     if (addr.getFamily()==AF_INET) {
         // IPv4
         // IPv4
         memcpy(packed, addr.getAddress().to_v4().to_bytes().data(), 4);
         memcpy(packed, addr.getAddress().to_v4().to_bytes().data(), 4);
@@ -38,8 +40,6 @@ AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress&
         len = 16;
         len = 16;
     }
     }
 
 
-    // First we copy the whole address as 16 bytes.
-    bool carry = false;
     for (int i = len; i >=0; --i) {
     for (int i = len; i >=0; --i) {
         packed[i]++;
         packed[i]++;
         if (packed[i] != 0) {
         if (packed[i] != 0) {
@@ -53,8 +53,8 @@ AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress&
 
 
 isc::asiolink::IOAddress
 isc::asiolink::IOAddress
 AllocEngine::IterativeAllocator::pickAddress(const Subnet6Ptr& subnet,
 AllocEngine::IterativeAllocator::pickAddress(const Subnet6Ptr& subnet,
-                                             const DuidPtr& duid,
-                                             const IOAddress& hint) {
+                                             const DuidPtr&,
+                                             const IOAddress&) {
 
 
     // Let's get the last allocated address. It is usually be set correctly,
     // Let's get the last allocated address. It is usually be set correctly,
     // but there are times when it won't be (like after removing a pool or
     // but there are times when it won't be (like after removing a pool or
@@ -138,7 +138,8 @@ Lease6Ptr
 AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
 AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
                               const DuidPtr& duid,
                               const DuidPtr& duid,
                               uint32_t iaid,
                               uint32_t iaid,
-                              const IOAddress& hint) {
+                              const IOAddress& hint,
+                              bool fake /* = false */ ) {
     // That check is not necessary. We create allocator in AllocEngine
     // That check is not necessary. We create allocator in AllocEngine
     // constructor
     // constructor
     if (!allocator_) {
     if (!allocator_) {
@@ -153,6 +154,12 @@ AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
         return (existing);
         return (existing);
     }
     }
 
 
+    // check if the hint is available
+    existing = LeaseMgr::instance().getLease6(hint);
+    if (!existing) {
+        // the hint is good, let's create a lease for it
+    }
+
     unsigned int i = attempts_;
     unsigned int i = attempts_;
     do {
     do {
         IOAddress candidate = allocator_->pickAddress(subnet, duid, hint);
         IOAddress candidate = allocator_->pickAddress(subnet, duid, hint);
@@ -164,7 +171,7 @@ AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
         // there's no existing lease for selected candidate, so it is
         // there's no existing lease for selected candidate, so it is
         // free. Let's allocate it.
         // free. Let's allocate it.
         if (!existing) {
         if (!existing) {
-            Lease6Ptr lease = createLease(subnet, duid, iaid, candidate);
+            Lease6Ptr lease = createLease(subnet, duid, iaid, candidate, fake);
             if (lease) {
             if (lease) {
                 return (lease);
                 return (lease);
             }
             }
@@ -186,22 +193,38 @@ AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
 Lease6Ptr AllocEngine::createLease(const Subnet6Ptr& subnet,
 Lease6Ptr AllocEngine::createLease(const Subnet6Ptr& subnet,
                                    const DuidPtr& duid,
                                    const DuidPtr& duid,
                                    uint32_t iaid,
                                    uint32_t iaid,
-                                   const IOAddress& addr) {
+                                   const IOAddress& addr,
+                                   bool fake /*= false */ ) {
 
 
     Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid,
     Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid,
                                subnet->getPreferred(), subnet->getValid(),
                                subnet->getPreferred(), subnet->getValid(),
                                subnet->getT1(), subnet->getT2(), subnet->getID()));
                                subnet->getT1(), subnet->getT2(), subnet->getID()));
 
 
-    bool status = LeaseMgr::instance().addLease(lease);
+    if (!fake) {
+        // That is a real (REQUEST) allocation
+        bool status = LeaseMgr::instance().addLease(lease);
 
 
-    if (status) {
-        return (lease);
+        if (status) {
+
+            return (lease);
+        } else {
+            // One of many failures with LeaseMgr (e.g. lost connection to the
+            // database, database failed etc.). One notable case for that
+            // is that we are working in multi-process mode and we lost a race
+            // (some other process got that address first)
+            return (Lease6Ptr());
+        }
     } else {
     } else {
-        // One of many failures with LeaseMgr (e.g. lost connection to the
-        // database, database failed etc.). One notable case for that
-        // is that we are working in multi-process mode and we lost a race
-        // (some other process got that address first)
-        return (Lease6Ptr());
+        // That is only fake (SOLICIT without rapid-commit) allocation
+
+        // It is for advertise only. We should not insert the lease into LeaseMgr,
+        // but rather check that we could have inserted it.
+        Lease6Ptr existing = LeaseMgr::instance().getLease6(addr);
+        if (!existing) {
+            return (lease);
+        } else {
+            return (Lease6Ptr());
+        }
     }
     }
 }
 }
 
 

+ 4 - 2
src/bin/dhcp6/alloc_engine.h

@@ -114,7 +114,8 @@ protected:
     allocateAddress6(const Subnet6Ptr& subnet,
     allocateAddress6(const Subnet6Ptr& subnet,
                      const DuidPtr& duid,
                      const DuidPtr& duid,
                      uint32_t iaid,
                      uint32_t iaid,
-                     const isc::asiolink::IOAddress& hint);
+                     const isc::asiolink::IOAddress& hint,
+                     bool fake);
 
 
     /// @brief Destructor. Used during DHCPv6 service shutdown.
     /// @brief Destructor. Used during DHCPv6 service shutdown.
     virtual ~AllocEngine();
     virtual ~AllocEngine();
@@ -127,7 +128,8 @@ private:
     Lease6Ptr createLease(const Subnet6Ptr& subnet,
     Lease6Ptr createLease(const Subnet6Ptr& subnet,
                           const DuidPtr& duid,
                           const DuidPtr& duid,
                           uint32_t iaid,
                           uint32_t iaid,
-                          const isc::asiolink::IOAddress& addr);
+                          const isc::asiolink::IOAddress& addr,
+                          bool fake = false);
 
 
     Allocator* allocator_;
     Allocator* allocator_;
 
 

+ 5 - 0
src/lib/dhcp/lease_mgr.h

@@ -12,6 +12,9 @@
 // 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.
 
 
+#ifndef LEASE_MGR_H
+#define LEASE_MGR_H
+
 #include <string>
 #include <string>
 #include <fstream>
 #include <fstream>
 #include <vector>
 #include <vector>
@@ -502,3 +505,5 @@ protected:
 }; // end of isc::dhcp namespace
 }; // end of isc::dhcp namespace
 
 
 }; // end of isc namespace
 }; // end of isc namespace
+
+#endif // LEASE_MGR_H

+ 2 - 0
src/lib/dhcp/tests/Makefile.am

@@ -46,6 +46,8 @@ libdhcpsrv_unittests_SOURCES  = run_unittests.cc
 libdhcpsrv_unittests_SOURCES += cfgmgr_unittest.cc triplet_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfgmgr_unittest.cc triplet_unittest.cc
 libdhcpsrv_unittests_SOURCES += pool_unittest.cc subnet_unittest.cc
 libdhcpsrv_unittests_SOURCES += pool_unittest.cc subnet_unittest.cc
 libdhcpsrv_unittests_SOURCES += addr_utilities_unittest.cc
 libdhcpsrv_unittests_SOURCES += addr_utilities_unittest.cc
+libdhcpsrv_unittests_SOURCES += alloc_engine_unittest.cc
+
 
 
 libdhcpsrv_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES)
 libdhcpsrv_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES)
 libdhcpsrv_unittests_LDFLAGS  = $(AM_LDFLAGS)  $(GTEST_LDFLAGS)
 libdhcpsrv_unittests_LDFLAGS  = $(AM_LDFLAGS)  $(GTEST_LDFLAGS)

+ 53 - 0
src/lib/dhcp/tests/alloc_engine_unittest.cc

@@ -0,0 +1,53 @@
+// Copyright (C) 2011-2012 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.
+
+#include <config.h>
+#include <iostream>
+#include <sstream>
+#include <gtest/gtest.h>
+#include <asiolink/io_address.h>
+#include <dhcp/lease_mgr.h>
+#include <dhcp/duid.h>
+#include <dhcp/alloc_engine.h>
+
+using namespace std;
+using namespace isc;
+using namespace isc::asiolink;
+using namespace isc::dhcp;
+
+
+namespace {
+// empty class for now, but may be extended once Addr6 becomes bigger
+class AllocEngineTest : public ::testing::Test {
+public:
+    AllocEngineTest() {
+    }
+};
+
+// This test checks if the Allocation Engine can be instantiated and that it
+// parses parameters string properly.
+TEST_F(AllocEngineTest, constructor) {
+
+    AllocEngine* x = NULL;
+
+    // Hashed and random allocators are not supported yet
+    ASSERT_THROW(x = new AllocEngine(AllocEngine::ALLOC_HASHED, 5), BadValue);
+    ASSERT_THROW(x = new AllocEngine(AllocEngine::ALLOC_RANDOM, 5), BadValue);
+
+    ASSERT_NO_THROW(x = new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
+
+    delete x;
+}
+
+}; // end of anonymous namespace