| Kea 3.0.2-git

tracking_lease_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2023-2024 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#include <boost/multi_index_container.hpp>
13#include <boost/multi_index/composite_key.hpp>
14#include <boost/multi_index/indexed_by.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/multi_index/hashed_index.hpp>
17#include <boost/multi_index/ordered_index.hpp>
18#include <boost/multi_index/sequenced_index.hpp>
19#include <boost/shared_ptr.hpp>
20#include <functional>
21#include <string>
22#include <unordered_set>
23
24 namespace isc {
25 namespace dhcp {
26
76 class TrackingLeaseMgr : public LeaseMgr {
77 public:
78
83 friend class LeaseMgrFactory;
84
86 typedef enum {
87 TRACK_ADD_LEASE,
88 TRACK_UPDATE_LEASE,
89 TRACK_DELETE_LEASE
90 } CallbackType;
91
97 typedef std::function<void(LeasePtr)> CallbackFn;
98
106 typedef struct {
108 CallbackType type;
109
111 std::string owner;
112
114 SubnetID subnet_id;
115
117 Lease::Type lease_type;
118
120 CallbackFn fn;
121 } Callback;
122
123 protected:
124
131 typedef boost::multi_index_container<
132 Callback,
133 boost::multi_index::indexed_by<
134 boost::multi_index::ordered_non_unique<
135 boost::multi_index::composite_key<
136 Callback,
137 boost::multi_index::member<Callback, CallbackType, &Callback::type>,
138 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
139 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
140 >
141 >,
142 boost::multi_index::ordered_non_unique<
143 boost::multi_index::composite_key<
144 Callback,
145 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
146 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
147 >
148 >
149 >
150 > CallbackContainer;
151
153 typedef boost::shared_ptr<CallbackContainer> CallbackContainerPtr;
154
156 TrackingLeaseMgr();
157
175 bool tryLock(const LeasePtr& lease);
176
182 void unlock(const LeasePtr& lease);
183
184 public:
185
191 bool isLocked(const LeasePtr& lease);
192
193 protected:
194
203 void trackAddLease(const LeasePtr& lease);
204
213 void trackUpdateLease(const LeasePtr& lease);
214
223 void trackDeleteLease(const LeasePtr& lease);
224
225 public:
226
237 void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id,
238 Lease::Type lease_type, CallbackFn callback_fn);
239
248 void registerCallback(CallbackType type, std::string owner, Lease::Type lease_type,
249 CallbackFn callback_fn);
250
255 void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type);
256
258 void unregisterAllCallbacks();
259
266 bool hasCallbacks() const;
267
268 protected:
269
274 static std::string callbackTypeToString(CallbackType type);
275
283 void runCallbacks(CallbackType type, const LeasePtr& lease);
284
292 void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id,
293 const LeasePtr& lease);
294
296 CallbackContainerPtr callbacks_;
297
302 std::unordered_set<asiolink::IOAddress, asiolink::IOAddress::Hash> locked_leases_;
303};
304
306 typedef std::unique_ptr<TrackingLeaseMgr> TrackingLeaseMgrPtr;
307
308} // end of namespace isc::dhcp
309} // end of namespace isc
310
311#endif // TRACKING_LEASE_MGR_H
isc::dhcp::LeaseMgr::LeaseMgr
LeaseMgr()
Constructor.
Definition lease_mgr.h:252
isc::dhcp::TrackingLeaseMgr::CallbackType
CallbackType
An enumeration differentiating between lease write operations.
Definition tracking_lease_mgr.h:86
isc::dhcp::TrackingLeaseMgr::TRACK_DELETE_LEASE
@ TRACK_DELETE_LEASE
Definition tracking_lease_mgr.h:89
isc::dhcp::TrackingLeaseMgr::TRACK_ADD_LEASE
@ TRACK_ADD_LEASE
Definition tracking_lease_mgr.h:87
isc::dhcp::TrackingLeaseMgr::TRACK_UPDATE_LEASE
@ TRACK_UPDATE_LEASE
Definition tracking_lease_mgr.h:88
isc::dhcp::TrackingLeaseMgr::isLocked
bool isLocked(const LeasePtr &lease)
Checks if the lease is locked.
Definition tracking_lease_mgr.cc:40
isc::dhcp::TrackingLeaseMgr::trackAddLease
void trackAddLease(const LeasePtr &lease)
Invokes the callbacks when a new lease is added.
Definition tracking_lease_mgr.cc:45
isc::dhcp::TrackingLeaseMgr::unlock
void unlock(const LeasePtr &lease)
Attempts to unlock a lease.
Definition tracking_lease_mgr.cc:34
isc::dhcp::TrackingLeaseMgr::trackUpdateLease
void trackUpdateLease(const LeasePtr &lease)
Invokes the callbacks when a lease is updated.
Definition tracking_lease_mgr.cc:50
isc::dhcp::TrackingLeaseMgr::runCallbacks
void runCallbacks(CallbackType type, const LeasePtr &lease)
Runs registered callbacks of the particular type.
Definition tracking_lease_mgr.cc:128
isc::dhcp::TrackingLeaseMgr::trackDeleteLease
void trackDeleteLease(const LeasePtr &lease)
Invokes the callbacks when a lease is deleted.
Definition tracking_lease_mgr.cc:55
isc::dhcp::TrackingLeaseMgr::CallbackContainer
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.
Definition tracking_lease_mgr.h:150
isc::dhcp::TrackingLeaseMgr::unregisterCallbacks
void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type)
Unregisters all callbacks for a given subnet identifier.
Definition tracking_lease_mgr.cc:93
isc::dhcp::TrackingLeaseMgr::CallbackContainerPtr
boost::shared_ptr< CallbackContainer > CallbackContainerPtr
Pointer to the callback container.
Definition tracking_lease_mgr.h:153
isc::dhcp::TrackingLeaseMgr::callbackTypeToString
static std::string callbackTypeToString(CallbackType type)
Converts callback type to string for logging purposes.
Definition tracking_lease_mgr.cc:114
isc::dhcp::TrackingLeaseMgr::registerCallback
void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id, Lease::Type lease_type, CallbackFn callback_fn)
Registers a callback function for a subnet.
Definition tracking_lease_mgr.cc:60
isc::dhcp::TrackingLeaseMgr::tryLock
bool tryLock(const LeasePtr &lease)
Attempts to lock a lease.
Definition tracking_lease_mgr.cc:27
isc::dhcp::TrackingLeaseMgr::locked_leases_
std::unordered_set< asiolink::IOAddress, asiolink::IOAddress::Hash > locked_leases_
A set of locked leases.
Definition tracking_lease_mgr.h:302
isc::dhcp::TrackingLeaseMgr::TrackingLeaseMgr
TrackingLeaseMgr()
Constructor.
Definition tracking_lease_mgr.cc:22
isc::dhcp::TrackingLeaseMgr::runCallbacksForSubnetID
void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id, const LeasePtr &lease)
Runs registered callbacks of the particular type for a subnet id.
Definition tracking_lease_mgr.cc:135
isc::dhcp::TrackingLeaseMgr::callbacks_
CallbackContainerPtr callbacks_
The multi-index container holding registered callbacks.
Definition tracking_lease_mgr.h:296
isc::dhcp::TrackingLeaseMgr::unregisterAllCallbacks
void unregisterAllCallbacks()
Unregisters all callbacks.
Definition tracking_lease_mgr.cc:104
isc::dhcp::TrackingLeaseMgr::LeaseMgrFactory
friend class LeaseMgrFactory
The LeaseMgrFactory manages the LeaseMgr instances and has to be able to move installed callbacks bet...
Definition tracking_lease_mgr.h:83
isc::dhcp::TrackingLeaseMgr::CallbackFn
std::function< void(LeasePtr)> CallbackFn
Type of a callback function invoked upon a lease insertion, update or deletion.
Definition tracking_lease_mgr.h:97
isc::dhcp::TrackingLeaseMgr::hasCallbacks
bool hasCallbacks() const
Checks if any callbacks have been registered.
Definition tracking_lease_mgr.cc:109
io_address.h
lease_mgr.h
An abstract API for lease database.
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::LeasePtr
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
isc::dhcp::TrackingLeaseMgrPtr
std::unique_ptr< TrackingLeaseMgr > TrackingLeaseMgrPtr
TrackingLeaseMgr pointer.
Definition tracking_lease_mgr.h:306
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::TrackingLeaseMgr::Callback
A structure representing a registered callback.
Definition tracking_lease_mgr.h:106
isc::dhcp::TrackingLeaseMgr::Callback::fn
CallbackFn fn
Callback function.
Definition tracking_lease_mgr.h:120
isc::dhcp::TrackingLeaseMgr::Callback::owner
std::string owner
An entity owning callback registration (e.g., FLQ allocator).
Definition tracking_lease_mgr.h:111
isc::dhcp::TrackingLeaseMgr::Callback::subnet_id
SubnetID subnet_id
Subnet identifier associated with the callback.
Definition tracking_lease_mgr.h:114
isc::dhcp::TrackingLeaseMgr::Callback::lease_type
Lease::Type lease_type
Lease types for which the callback should be invoked.
Definition tracking_lease_mgr.h:117
isc::dhcp::TrackingLeaseMgr::Callback::type
CallbackType type
Callback type (i.e., lease add, update, delete).
Definition tracking_lease_mgr.h:108