Kea 2.7.5
tracking_lease_mgr.cc
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#include <config.h>
8
10#include <dhcpsrv/dhcpsrv_log.h>
13#include <boost/foreach.hpp>
14#include <boost/tuple/tuple.hpp>
15
16using namespace isc::asiolink;
17using namespace isc::util;
18
19namespace isc {
20namespace dhcp {
21
25
26bool
28 // Try inserting a lease. If such a lease already exists in the set, return false.
29 auto result = locked_leases_.insert(lease->addr_);
30 return (result.second);
31}
32
33void
35 // Remove the locked lease from the set.
36 locked_leases_.erase(lease->addr_);
37}
38
39bool
41 return (locked_leases_.find(lease->addr_) != locked_leases_.end());
42}
43
44void
48
49void
53
54void
58
59void
61 std::string owner,
62 SubnetID subnet_id,
63 Lease::Type lease_type,
64 TrackingLeaseMgr::CallbackFn callback_fn) {
65 // The first index filters the callbacks by type and subnet_id.
66 auto& idx = callbacks_->get<0>();
67 auto range = idx.equal_range(boost::make_tuple(type, subnet_id, lease_type));
68 if (range.first != range.second) {
69 // Make sure that the callback for this owner does not exist.
70 if (std::find_if(range.first, range.second,
71 [&owner] (const Callback& cb) -> bool {
72 return (cb.owner == owner);
73 }) != range.second) {
74 isc_throw(InvalidOperation, "the callback owned by the " << owner
75 << ", for subnet ID " << subnet_id
76 << ", and lease type " << Lease::typeToText(lease_type)
77 << " has already been registered in the lease manager");
78 }
79 }
80 TrackingLeaseMgr::Callback callback{type, owner, subnet_id, lease_type, callback_fn};
81 callbacks_->insert(callback);
82}
83
84void
86 std::string owner,
87 Lease::Type lease_type,
88 TrackingLeaseMgr::CallbackFn callback_fn) {
89 registerCallback(type, owner, SUBNET_ID_GLOBAL, lease_type, callback_fn);
90}
91
92void
94 // The second index filters the callbacks by the subnet identifier and
95 // the lease type.
96 auto& idx = callbacks_->get<1>();
97 auto range = idx.equal_range(boost::make_tuple(subnet_id, lease_type));
98 if (range.first != range.second) {
99 idx.erase(range.first, range.second);
100 }
101}
102
103void
107
108bool
110 return (!callbacks_->empty());
111}
112
113std::string
115 switch (type) {
117 return ("add_lease");
119 return ("update_lease");
121 return ("delete_lease");
122 default:
123 return ("unknown");
124 }
125}
126
127void
129 const LeasePtr& lease) {
130 runCallbacksForSubnetID(type, SUBNET_ID_GLOBAL, lease);
131 runCallbacksForSubnetID(type, lease->subnet_id_, lease);
132}
133
134void
136 const LeasePtr& lease) {
137 // The first index filters by callback type and subnet_id.
138 auto& idx = callbacks_->get<0>();
139 auto cbs = idx.equal_range(boost::make_tuple(type, subnet_id, lease->getType()));
140 if (cbs.first == cbs.second) {
141 return;
142 }
143 BOOST_FOREACH(auto const& cb, cbs) {
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
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:55
A structure representing a registered callback.