From: Thomas Markwalder Date: Thu, 20 Feb 2020 20:39:15 +0000 (-0500) Subject: [#1124] Change the order of HR vs lease lookup during v4 lease allocation X-Git-Tag: Kea-1.6.3~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db7b1109cadd24da509f5e23cb1cd2aed7b43bd6;p=thirdparty%2Fkea.git [#1124] Change the order of HR vs lease lookup during v4 lease allocation src/lib/dhcpsrv/alloc_engine.cc AllocEngine::allocateUnreservedLease4() - rather than first testing for a host reservation and then looking for an existing lease, we reverse the order, thus worrying about HRs only if there is no existing lease. --- diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index 98669c405a..ab0f10aebc 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -3893,23 +3893,33 @@ AllocEngine::allocateUnreservedLease4(ClientContext4& ctx) { ctx.query_->getClasses(), client_id, ctx.requested_address_); - // If address is not reserved for another client, try to allocate it. - if (!addressReserved(candidate, ctx)) { + // Check for an existing lease for the candidate address. + Lease4Ptr exist_lease = LeaseMgrFactory::instance().getLease4(candidate); + if (!exist_lease) { + // No existing lease, is it reserved? + if (!addressReserved(candidate, ctx)) { + // Not reserved use it. + new_lease = createLease4(ctx, candidate, callout_status); + } + } else { + // An lease exists, is expired, and not reserved use it. + if (exist_lease->expired() && (!addressReserved(candidate, ctx))) { + ctx.old_lease_ = Lease4Ptr(new Lease4(*exist_lease)); + new_lease = (reuseExpiredLease4(exist_lease, ctx, callout_status)); + } + } - // The call below will return the non-NULL pointer if we - // successfully allocate this lease. This means that the - // address is not in use by another client. - new_lease = allocateOrReuseLease4(candidate, ctx, callout_status); - if (new_lease) { - return (new_lease); + // We found a lease we can use, return it. + if (new_lease) { + return (new_lease); + } - } else if (ctx.callout_handle_ && - (callout_status != CalloutHandle::NEXT_STEP_CONTINUE)) { - // Don't retry when the callout status is not continue. - subnet.reset(); - break; - } + if (ctx.callout_handle_ && (callout_status != CalloutHandle::NEXT_STEP_CONTINUE)) { + // Don't retry when the callout status is not continue. + subnet.reset(); + break; } + ++total_attempts; }