From: Tomek Mrugalski Date: Fri, 19 Oct 2012 17:32:16 +0000 (+0200) Subject: [2324] AllocEngine is now part of b10-dhcpsrv lib. X-Git-Tag: trac2487_base~21^2~9^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ee7d5cf20406b4ff00a52ce8218304c6623571ea;p=thirdparty%2Fkea.git [2324] AllocEngine is now part of b10-dhcpsrv lib. --- diff --git a/src/bin/dhcp6/Makefile.am b/src/bin/dhcp6/Makefile.am index 13e43f77ec..68aadea5ad 100644 --- a/src/bin/dhcp6/Makefile.am +++ b/src/bin/dhcp6/Makefile.am @@ -47,7 +47,6 @@ pkglibexec_PROGRAMS = b10-dhcp6 b10_dhcp6_SOURCES = main.cc b10_dhcp6_SOURCES += ctrl_dhcp6_srv.cc ctrl_dhcp6_srv.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_srv.cc dhcp6_srv.h diff --git a/src/lib/dhcp/Makefile.am b/src/lib/dhcp/Makefile.am index 6585a386c3..316763527c 100644 --- a/src/lib/dhcp/Makefile.am +++ b/src/lib/dhcp/Makefile.am @@ -16,7 +16,6 @@ CLEANFILES = *.gcno *.gcda lib_LTLIBRARIES = libb10-dhcp++.la libb10-dhcpsrv.la libb10_dhcp___la_SOURCES = 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_linux.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 += subnet.cc subnet.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 += alloc_engine.cc alloc_engine.h libb10_dhcpsrv_la_CXXFLAGS = $(AM_CXXFLAGS) libb10_dhcpsrv_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES) libb10_dhcpsrv_la_LIBADD = $(top_builddir)/src/lib/asiolink/libb10-asiolink.la diff --git a/src/bin/dhcp6/alloc_engine.cc b/src/lib/dhcp/alloc_engine.cc similarity index 81% rename from src/bin/dhcp6/alloc_engine.cc rename to src/lib/dhcp/alloc_engine.cc index 0f67c14285..8964c1bf5d 100644 --- a/src/bin/dhcp6/alloc_engine.cc +++ b/src/lib/dhcp/alloc_engine.cc @@ -28,6 +28,8 @@ isc::asiolink::IOAddress AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) { uint8_t packed[V6ADDRESS_LEN]; int len; + + // First we copy the whole address as 16 bytes. if (addr.getFamily()==AF_INET) { // IPv4 memcpy(packed, addr.getAddress().to_v4().to_bytes().data(), 4); @@ -38,8 +40,6 @@ AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& len = 16; } - // First we copy the whole address as 16 bytes. - bool carry = false; for (int i = len; i >=0; --i) { packed[i]++; if (packed[i] != 0) { @@ -53,8 +53,8 @@ AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& isc::asiolink::IOAddress 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, // 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, const DuidPtr& duid, uint32_t iaid, - const IOAddress& hint) { + const IOAddress& hint, + bool fake /* = false */ ) { // That check is not necessary. We create allocator in AllocEngine // constructor if (!allocator_) { @@ -153,6 +154,12 @@ AllocEngine::allocateAddress6(const Subnet6Ptr& subnet, 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_; do { 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 // free. Let's allocate it. if (!existing) { - Lease6Ptr lease = createLease(subnet, duid, iaid, candidate); + Lease6Ptr lease = createLease(subnet, duid, iaid, candidate, fake); if (lease) { return (lease); } @@ -186,22 +193,38 @@ AllocEngine::allocateAddress6(const Subnet6Ptr& subnet, Lease6Ptr AllocEngine::createLease(const Subnet6Ptr& subnet, const DuidPtr& duid, uint32_t iaid, - const IOAddress& addr) { + const IOAddress& addr, + bool fake /*= false */ ) { Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid, subnet->getPreferred(), subnet->getValid(), 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 { - // 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()); + } } } diff --git a/src/bin/dhcp6/alloc_engine.h b/src/lib/dhcp/alloc_engine.h similarity index 95% rename from src/bin/dhcp6/alloc_engine.h rename to src/lib/dhcp/alloc_engine.h index baefe9240b..e02bcc581b 100644 --- a/src/bin/dhcp6/alloc_engine.h +++ b/src/lib/dhcp/alloc_engine.h @@ -114,7 +114,8 @@ protected: allocateAddress6(const Subnet6Ptr& subnet, const DuidPtr& duid, uint32_t iaid, - const isc::asiolink::IOAddress& hint); + const isc::asiolink::IOAddress& hint, + bool fake); /// @brief Destructor. Used during DHCPv6 service shutdown. virtual ~AllocEngine(); @@ -127,7 +128,8 @@ private: Lease6Ptr createLease(const Subnet6Ptr& subnet, const DuidPtr& duid, uint32_t iaid, - const isc::asiolink::IOAddress& addr); + const isc::asiolink::IOAddress& addr, + bool fake = false); Allocator* allocator_; diff --git a/src/lib/dhcp/lease_mgr.h b/src/lib/dhcp/lease_mgr.h index d548c09240..8ac7cc4978 100644 --- a/src/lib/dhcp/lease_mgr.h +++ b/src/lib/dhcp/lease_mgr.h @@ -12,6 +12,9 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. +#ifndef LEASE_MGR_H +#define LEASE_MGR_H + #include #include #include @@ -502,3 +505,5 @@ protected: }; // end of isc::dhcp namespace }; // end of isc namespace + +#endif // LEASE_MGR_H diff --git a/src/lib/dhcp/tests/Makefile.am b/src/lib/dhcp/tests/Makefile.am index a15d9579d2..1f15112ab5 100644 --- a/src/lib/dhcp/tests/Makefile.am +++ b/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 += pool_unittest.cc subnet_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_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS) diff --git a/src/lib/dhcp/tests/alloc_engine_unittest.cc b/src/lib/dhcp/tests/alloc_engine_unittest.cc new file mode 100644 index 0000000000..5f0a4ba556 --- /dev/null +++ b/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 +#include +#include +#include +#include +#include +#include +#include + +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