Kea  2.5.3
tracking_lease_mgr.h
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 #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 {
91 
98  typedef std::function<void(LeasePtr, bool)> CallbackFn;
99 
107  typedef struct {
111  std::string owner;
118  } Callback;
119 
120 protected:
121 
128  typedef boost::multi_index_container<
129  Callback,
130  boost::multi_index::indexed_by<
131  boost::multi_index::ordered_non_unique<
132  boost::multi_index::composite_key<
133  Callback,
134  boost::multi_index::member<Callback, CallbackType, &Callback::type>,
135  boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
136  boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
137  >
138  >,
139  boost::multi_index::ordered_non_unique<
140  boost::multi_index::composite_key<
141  Callback,
142  boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
143  boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
144  >
145  >
146  >
148 
150  typedef boost::shared_ptr<CallbackContainer> CallbackContainerPtr;
151 
154 
172  bool tryLock(const LeasePtr& lease);
173 
179  void unlock(const LeasePtr& lease);
180 
181 public:
182 
188  bool isLocked(const LeasePtr& lease);
189 
190 protected:
191 
202  void trackAddLease(const LeasePtr& lease, bool mt_safe);
203 
214  void trackUpdateLease(const LeasePtr& lease, bool mt_safe);
215 
226  void trackDeleteLease(const LeasePtr& lease, bool mt_safe);
227 
228 public:
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 
261  void unregisterAllCallbacks();
262 
269  bool hasCallbacks() const;
270 
271 protected:
272 
277  static std::string callbackTypeToString(CallbackType type);
278 
288  void runCallbacks(CallbackType type, const LeasePtr& lease, bool mt_safe);
289 
299  void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id,
300  const LeasePtr& lease, bool mt_safe);
301 
304 
309  std::unordered_set<asiolink::IOAddress, asiolink::IOAddress::Hash> locked_leases_;
310 };
311 
312 } // end of namespace isc::dhcp
313 } // end of namespace isc
314 
315 #endif // TRACKING_LEASE_MGR_H
Lease Manager Factory.
Abstract Lease Manager.
Definition: lease_mgr.h:248
Introduces callbacks into the LeaseMgr.
CallbackType
An enumeration differentiating between lease write operations.
void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id, const LeasePtr &lease, bool mt_safe)
Runs registered callbacks of the particular type for a subnet id.
bool isLocked(const LeasePtr &lease)
Checks if the lease is locked.
void trackUpdateLease(const LeasePtr &lease, bool mt_safe)
Invokes the callbacks when a lease is updated.
void unlock(const LeasePtr &lease)
Attempts to unlock a lease.
void trackAddLease(const LeasePtr &lease, bool mt_safe)
Invokes the callbacks when a new lease is added.
void runCallbacks(CallbackType type, const LeasePtr &lease, bool mt_safe)
Runs registered callbacks of the particular type.
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 trackDeleteLease(const LeasePtr &lease, bool mt_safe)
Invokes the callbacks when a lease is deleted.
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.
CallbackContainerPtr callbacks_
The multi-index container holding registered callbacks.
void unregisterAllCallbacks()
Unregisters all callbacks.
std::function< void(LeasePtr, bool)> 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:22
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition: lease.h:46
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).