]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1124] Change the order of HR vs lease lookup during v4 lease allocation
authorThomas Markwalder <tmark@isc.org>
Thu, 20 Feb 2020 20:39:15 +0000 (15:39 -0500)
committerTomek Mrugalski <tomek@isc.org>
Thu, 16 Jul 2020 13:24:50 +0000 (15:24 +0200)
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.

src/lib/dhcpsrv/alloc_engine.cc

index 98669c405a918be47410d2353d155e2429544b27..ab0f10aebc44ff2f5da3f971cd2348d4bb29f1ad 100644 (file)
@@ -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;
         }