Kea 3.3.3
tracking_lease_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2023-2026 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#ifndef TRACKING_LEASE_MGR_H
8#define TRACKING_LEASE_MGR_H
9
10#include <asiolink/io_address.h>
11#include <dhcpsrv/lease_mgr.h>
12
13#include <cstdint>
14#include <functional>
15#include <string>
16#include <unordered_set>
17
18#include <boost/multi_index/composite_key.hpp>
19#include <boost/multi_index/hashed_index.hpp>
20#include <boost/multi_index/indexed_by.hpp>
21#include <boost/multi_index/member.hpp>
22#include <boost/multi_index/ordered_index.hpp>
23#include <boost/multi_index/sequenced_index.hpp>
24#include <boost/multi_index_container.hpp>
25#include <boost/shared_ptr.hpp>
26
27namespace isc {
28namespace dhcp {
29
79class TrackingLeaseMgr : public LeaseMgr {
80public:
81
86 friend class LeaseMgrFactory;
87
94
100 typedef std::function<void(LeasePtr)> CallbackFn;
101
125
126protected:
127
134 typedef boost::multi_index_container<
135 Callback,
136 boost::multi_index::indexed_by<
137 boost::multi_index::ordered_non_unique<
138 boost::multi_index::composite_key<
139 Callback,
140 boost::multi_index::member<Callback, CallbackType, &Callback::type>,
141 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
142 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
143 >
144 >,
145 boost::multi_index::ordered_non_unique<
146 boost::multi_index::composite_key<
147 Callback,
148 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
149 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
150 >
151 >
152 >
154
156 typedef boost::shared_ptr<CallbackContainer> CallbackContainerPtr;
157
160
178 bool tryLock(const LeasePtr& lease);
179
185 void unlock(const LeasePtr& lease);
186
187public:
188
194 bool isLocked(const LeasePtr& lease);
195
196protected:
197
206 void trackAddLease(const LeasePtr& lease);
207
216 void trackUpdateLease(const LeasePtr& lease);
217
226 void trackDeleteLease(const LeasePtr& lease);
227
228public:
229
240 void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id,
241 Lease::Type lease_type, CallbackFn callback_fn);
242
251 void registerCallback(CallbackType type, std::string owner, Lease::Type lease_type,
252 CallbackFn callback_fn);
253
258 void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type);
259
262
269 bool hasCallbacks() const;
270
271protected:
272
277 static std::string callbackTypeToString(CallbackType type);
278
286 void runCallbacks(CallbackType type, const LeasePtr& lease);
287
295 void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id,
296 const LeasePtr& lease);
297
300
305 std::unordered_set<asiolink::IOAddress, asiolink::IOAddress::Hash> locked_leases_;
306};
307
309typedef std::unique_ptr<TrackingLeaseMgr> TrackingLeaseMgrPtr;
310
311} // end of namespace isc::dhcp
312} // end of namespace isc
313
314#endif // TRACKING_LEASE_MGR_H
LeaseMgr()
Constructor.
Definition lease_mgr.h:280
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.
boost::shared_ptr< CallbackContainer > CallbackContainerPtr
Pointer to the callback container.
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.
CallbackType
An enumeration differentiating between lease write operations.
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.
friend class LeaseMgrFactory
The LeaseMgrFactory manages the LeaseMgr instances and has to be able to move installed callbacks bet...
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.
An abstract API for lease database.
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:27
std::unique_ptr< TrackingLeaseMgr > TrackingLeaseMgrPtr
TrackingLeaseMgr pointer.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition lease.h:48
A structure representing a registered callback.
std::string owner
An entity owning callback registration (e.g., FLQ allocator).
SubnetID subnet_id
Subnet identifier associated with the callback.
Lease::Type lease_type
Lease types for which the callback should be invoked.
CallbackType type
Callback type (i.e., lease add, update, delete).