From: Marcin Siodelski Date: Tue, 27 Oct 2015 10:19:35 +0000 (+0100) Subject: [master] Merge branch 'trac3977' X-Git-Tag: fdfb_base~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5880e706cb27c19b1b70296ccd8d96e38e82027b;p=thirdparty%2Fkea.git [master] Merge branch 'trac3977' --- 5880e706cb27c19b1b70296ccd8d96e38e82027b diff --cc src/lib/dhcpsrv/alloc_engine.cc index 3d99674fc5,12d72856ce..868e4a4f69 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@@ -1706,6 -1578,200 +1578,200 @@@ AllocEngine::reclaimExpiredLeases4(cons } } + template + void + AllocEngine::reclaimExpiredLease(const LeasePtrType& lease, const bool remove_lease, + const CalloutHandlePtr& callout_handle) { + reclaimExpiredLease(lease, remove_lease ? DB_RECLAIM_REMOVE : DB_RECLAIM_UPDATE, + callout_handle); + } + + template + void + AllocEngine::reclaimExpiredLease(const LeasePtrType& lease, + const CalloutHandlePtr& callout_handle) { + // This variant of the method is used by the code which allocates or + // renews leases. It may be the case that the lease has already been + // reclaimed, so there is nothing to do. + if (!lease->stateExpiredReclaimed()) { + reclaimExpiredLease(lease, DB_RECLAIM_LEAVE_UNCHANGED, callout_handle); + } + } + + void + AllocEngine::reclaimExpiredLease(const Lease6Ptr& lease, + const DbReclaimMode& reclaim_mode, + const CalloutHandlePtr& callout_handle) { + + LOG_DEBUG(alloc_engine_logger, ALLOC_ENGINE_DBG_TRACE, + ALLOC_ENGINE_V6_LEASE_RECLAIM) + .arg(Pkt6::makeLabel(lease->duid_, lease->hwaddr_)) + .arg(lease->addr_.toText()) + .arg(static_cast(lease->prefixlen_)); + + // The skip flag indicates if the callouts have taken responsibility + // for reclaiming the lease. The callout will set this to true if + // it reclaims the lease itself. In this case the reclamation routine + // will not update DNS nor update the database. + bool skipped = false; + if (callout_handle) { + callout_handle->deleteAllArguments(); + callout_handle->setArgument("lease6", lease); + callout_handle->setArgument("remove_lease", reclaim_mode == DB_RECLAIM_REMOVE); + + HooksManager::callCallouts(Hooks.hook_index_lease6_expire_, + *callout_handle); + + skipped = callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP; + } + - /// @todo: Maybe add support for DROP stauts? ++ /// @todo: Maybe add support for DROP status? + /// Not sure if we need to support every possible status everywhere. + + if (!skipped) { + + // Generate removal name change request for D2, if required. + // This will return immediatelly if the DNS wasn't updated + // when the lease was created. + queueNCR(CHG_REMOVE, lease); + + // Let's check if the lease that just expired is in DECLINED state. + // If it is, we need to conduct couple extra steps and also force + // its removal. + bool remove_tmp = (reclaim_mode == DB_RECLAIM_REMOVE); + if (lease->state_ == Lease::STATE_DECLINED) { + // There's no point in keeping declined lease after its + // reclaimation. Declined lease doesn't have any client + // identifying information anymore. + if (reclaim_mode != DB_RECLAIM_LEAVE_UNCHANGED) { + remove_tmp = true; + } + + // Do extra steps required for declined lease reclaimation: + // - bump decline-related stats + // - log separate message + reclaimDeclined(lease); + } + + if (reclaim_mode != DB_RECLAIM_LEAVE_UNCHANGED) { + // Reclaim the lease - depending on the configuration, set the + // expired-reclaimed state or simply remove it. + LeaseMgr& lease_mgr = LeaseMgrFactory::instance(); + reclaimLeaseInDatabase(lease, remove_tmp, + boost::bind(&LeaseMgr::updateLease6, + &lease_mgr, _1)); + } + } + + // Update statistics. + + // Decrease number of assigned leases. + if (lease->type_ == Lease::TYPE_NA) { + // IA_NA + StatsMgr::instance().addValue(StatsMgr::generateName("subnet", + lease->subnet_id_, + "assigned-nas"), + int64_t(-1)); + + } else if (lease->type_ == Lease::TYPE_PD) { + // IA_PD + StatsMgr::instance().addValue(StatsMgr::generateName("subnet", + lease->subnet_id_, + "assigned-pds"), + int64_t(-1)); + + } + + // Increase total number of reclaimed leases. + StatsMgr::instance().addValue("reclaimed-leases", int64_t(1)); + + // Increase number of reclaimed leases for a subnet. + StatsMgr::instance().addValue(StatsMgr::generateName("subnet", + lease->subnet_id_, + "reclaimed-leases"), + int64_t(1)); + } + + void + AllocEngine::reclaimExpiredLease(const Lease4Ptr& lease, + const DbReclaimMode& reclaim_mode, + const CalloutHandlePtr& callout_handle) { + + LOG_DEBUG(alloc_engine_logger, ALLOC_ENGINE_DBG_TRACE, + ALLOC_ENGINE_V4_LEASE_RECLAIM) + .arg(Pkt4::makeLabel(lease->hwaddr_, lease->client_id_)) + .arg(lease->addr_.toText()); + + // The skip flag indicates if the callouts have taken responsibility + // for reclaiming the lease. The callout will set this to true if + // it reclaims the lease itself. In this case the reclamation routine + // will not update DNS nor update the database. + bool skipped = false; + if (callout_handle) { + callout_handle->deleteAllArguments(); + callout_handle->setArgument("lease4", lease); + callout_handle->setArgument("remove_lease", reclaim_mode == DB_RECLAIM_REMOVE); + + HooksManager::callCallouts(Hooks.hook_index_lease4_expire_, + *callout_handle); + + skipped = callout_handle->getStatus() == CalloutHandle::NEXT_STEP_SKIP; + } + - /// @todo: Maybe add support for DROP stauts? ++ /// @todo: Maybe add support for DROP status? + /// Not sure if we need to support every possible status everywhere. + + if (!skipped) { + + // Generate removal name change request for D2, if required. + // This will return immediatelly if the DNS wasn't updated + // when the lease was created. + queueNCR(CHG_REMOVE, lease); + + // Let's check if the lease that just expired is in DECLINED state. + // If it is, we need to conduct couple extra steps and also force + // its removal. + bool remove_tmp = (reclaim_mode == DB_RECLAIM_REMOVE); + if (lease->state_ == Lease::STATE_DECLINED) { + // There's no point in keeping declined lease after its + // reclaimation. Declined lease doesn't have any client + // identifying information anymore. + if (reclaim_mode != DB_RECLAIM_LEAVE_UNCHANGED) { + remove_tmp = true; + } + + // Do extra steps required for declined lease reclaimation: + // - bump decline-related stats + // - log separate message + reclaimDeclined(lease); + } + + if (reclaim_mode != DB_RECLAIM_LEAVE_UNCHANGED) { + // Reclaim the lease - depending on the configuration, set the + // expired-reclaimed state or simply remove it. + LeaseMgr& lease_mgr = LeaseMgrFactory::instance(); + reclaimLeaseInDatabase(lease, remove_tmp, + boost::bind(&LeaseMgr::updateLease4, + &lease_mgr, _1)); + } + } + + // Decrease number of assigned addresses. + StatsMgr::instance().addValue(StatsMgr::generateName("subnet", + lease->subnet_id_, + "assigned-addresses"), + int64_t(-1)); + + // Increase total number of reclaimed leases. + StatsMgr::instance().addValue("reclaimed-leases", int64_t(1)); + + // Increase number of reclaimed leases for a subnet. + StatsMgr::instance().addValue(StatsMgr::generateName("subnet", + lease->subnet_id_, + "reclaimed-leases"), + int64_t(1)); + } + void AllocEngine::deleteExpiredReclaimedLeases4(const uint32_t secs) { LOG_DEBUG(alloc_engine_logger, ALLOC_ENGINE_DBG_TRACE,