Kea 2.5.5
tracking_lease_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2023 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <dhcpsrv/dhcpsrv_log.h>
13#include <boost/tuple/tuple.hpp>
14
15using namespace isc::asiolink;
16using namespace isc::util;
17
18namespace isc {
19namespace dhcp {
20
22 : LeaseMgr(), callbacks_(new TrackingLeaseMgr::CallbackContainer()) {
23}
24
25bool
27 // Try inserting a lease. If such a lease already exists in the set, return false.
28 auto result = locked_leases_.insert(lease->addr_);
29 return (result.second);
30}
31
32void
34 // Remove the locked lease from the set.
35 locked_leases_.erase(lease->addr_);
36}
37
38bool
40 return (locked_leases_.find(lease->addr_) != locked_leases_.end());
41}
42
43void
46}
47
48void
51}
52
53void
56}
57
58void
60 std::string owner,
61 SubnetID subnet_id,
62 Lease::Type lease_type,
63 TrackingLeaseMgr::CallbackFn callback_fn) {
64 // The first index filters the callbacks by type and subnet_id.
65 auto& idx = callbacks_->get<0>();
66 auto range = idx.equal_range(boost::make_tuple(type, subnet_id, lease_type));
67 if (range.first != range.second) {
68 // Make sure that the callback for this owner does not exist.
69 if (std::find_if(range.first, range.second,
70 [&owner] (const Callback& cb) -> bool {
71 return (cb.owner == owner);
72 }) != range.second) {
73 isc_throw(InvalidOperation, "the callback owned by the " << owner
74 << ", for subnet ID " << subnet_id
75 << ", and lease type " << Lease::typeToText(lease_type)
76 << " has already been registered in the lease manager");
77 }
78 }
79 TrackingLeaseMgr::Callback callback{type, owner, subnet_id, lease_type, callback_fn};
80 callbacks_->insert(callback);
81}
82
83void
85 std::string owner,
86 Lease::Type lease_type,
87 TrackingLeaseMgr::CallbackFn callback_fn) {
88 registerCallback(type, owner, SUBNET_ID_GLOBAL, lease_type, callback_fn);
89}
90
91void
93 // The second index filters the callbacks by the subnet identifier and
94 // the lease type.
95 auto& idx = callbacks_->get<1>();
96 auto range = idx.equal_range(boost::make_tuple(subnet_id, lease_type));
97 if (range.first != range.second) {
98 idx.erase(range.first, range.second);
99 }
100}
101
102void
104 callbacks_->clear();
105}
106
107bool
109 return (!callbacks_->empty());
110}
111
112std::string
114 switch (type) {
116 return ("add_lease");
118 return ("update_lease");
120 return ("delete_lease");
121 default:
122 return ("unknown");
123 }
124}
125
126void
128 const LeasePtr& lease) {
129 runCallbacksForSubnetID(type, SUBNET_ID_GLOBAL, lease);
130 runCallbacksForSubnetID(type, lease->subnet_id_, lease);
131}
132
133void
135 const LeasePtr& lease) {
136 // The first index filters by callback type and subnet_id.
137 auto& idx = callbacks_->get<0>();
138 auto cbs = idx.equal_range(boost::make_tuple(type, subnet_id, lease->getType()));
139 if (cbs.first == cbs.second) {
140 return;
141 }
142 for (auto it = cbs.first; it != cbs.second; ++it) {
143 auto cb = *it;
144 try {
145 cb.fn(lease);
146 } catch (const std::exception& ex) {
148 .arg(callbackTypeToString(type))
149 .arg(subnet_id)
150 .arg(lease->addr_.toText())
151 .arg(ex.what());
152 } catch (...) {
154 .arg(callbackTypeToString(type))
155 .arg(subnet_id)
156 .arg(lease->addr_.toText());
157 }
158 }
159}
160
161} // end of namespace isc::dhcp
162} // end of namespace isc
A generic exception that is thrown if a function is called in a prohibited way.
Abstract Lease Manager.
Definition: lease_mgr.h:248
Introduces callbacks into the LeaseMgr.
CallbackType
An enumeration differentiating between lease write operations.
bool isLocked(const LeasePtr &lease)
Checks if the lease is locked.
void trackAddLease(const LeasePtr &lease)
Invokes the callbacks when a new lease is added.
void unlock(const LeasePtr &lease)
Attempts to unlock a lease.
void trackUpdateLease(const LeasePtr &lease)
Invokes the callbacks when a lease is updated.
void runCallbacks(CallbackType type, const LeasePtr &lease)
Runs registered callbacks of the particular type.
void trackDeleteLease(const LeasePtr &lease)
Invokes the callbacks when a lease is deleted.
boost::multi_index_container< Callback, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::composite_key< Callback, boost::multi_index::member< Callback, CallbackType, &Callback::type >, boost::multi_index::member< Callback, SubnetID, &Callback::subnet_id >, boost::multi_index::member< Callback, Lease::Type, &Callback::lease_type > > >, boost::multi_index::ordered_non_unique< boost::multi_index::composite_key< Callback, boost::multi_index::member< Callback, SubnetID, &Callback::subnet_id >, boost::multi_index::member< Callback, Lease::Type, &Callback::lease_type > > > > > CallbackContainer
A multi-index container holding registered callbacks.
void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type)
Unregisters all callbacks for a given subnet identifier.
static std::string callbackTypeToString(CallbackType type)
Converts callback type to string for logging purposes.
void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id, Lease::Type lease_type, CallbackFn callback_fn)
Registers a callback function for a subnet.
bool tryLock(const LeasePtr &lease)
Attempts to lock a lease.
std::unordered_set< asiolink::IOAddress, asiolink::IOAddress::Hash > locked_leases_
A set of locked leases.
void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id, const LeasePtr &lease)
Runs registered callbacks of the particular type for a subnet id.
CallbackContainerPtr callbacks_
The multi-index container holding registered callbacks.
void unregisterAllCallbacks()
Unregisters all callbacks.
std::function< void(LeasePtr)> CallbackFn
Type of a callback function invoked upon a lease insertion, update or deletion.
bool hasCallbacks() const
Checks if any callbacks have been registered.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_LEASE_MGR_CALLBACK_UNKNOWN_EXCEPTION
const isc::log::MessageID DHCPSRV_LEASE_MGR_CALLBACK_EXCEPTION
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:25
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:25
Definition: edns.h:19
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition: lease.h:46
static std::string typeToText(Type type)
returns text representation of a lease type
Definition: lease.cc:54
A structure representing a registered callback.