Kea 3.1.1
ping_context_store.cc
Go to the documentation of this file.
1// Copyright (C) 2023-2025 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
11
12using namespace std;
13using namespace isc;
14using namespace isc::asiolink;
15using namespace isc::dhcp;
16using namespace isc::hooks;
17using namespace isc::util;
18using namespace std::chrono;
19
20namespace isc {
21namespace ping_check {
22
25 uint32_t min_echos, uint32_t reply_timeout,
26 ParkingLotHandlePtr& parking_lot) {
27
28 MultiThreadingLock lock(*mutex_);
29 PingContextPtr context;
30 try {
31 context.reset(new PingContext(lease, query, min_echos, reply_timeout, parking_lot));
32 } catch (const std::exception& ex) {
33 isc_throw(BadValue, "PingContextStore::addContext failed: " << ex.what());
34 }
35
36 context->beginWaitingToSend();
37 auto ret = pings_.insert(context);
38 if (ret.second == false) {
39 isc_throw(DuplicateContext, "PingContextStore::addContex: context already exists for: "
40 << lease->addr_);
41 }
42
43 return (context);
44}
45
46void
48 MultiThreadingLock lock(*mutex_);
49 auto& index = pings_.get<AddressIndexTag>();
50 auto context_iter = index.find(context->getTarget());
51 if (context_iter == index.end()) {
52 isc_throw(InvalidOperation, "PingContextStore::updateContext failed for address: "
53 << context->getTarget() << ", not in store");
54 }
55
56 // Use replace() to re-index contexts.
57 index.replace(context_iter, PingContextPtr(new PingContext(*context)));
58}
59
60void
62 MultiThreadingLock lock(*mutex_);
63 auto& index = pings_.get<AddressIndexTag>();
64 auto context_iter = index.find(context->getTarget());
65 if (context_iter == index.end()) {
66 // Not there, just return.
67 return;
68 }
69
70 // Remove the context from the store.
71 pings_.erase(context_iter);
72}
73
76 MultiThreadingLock lock(*mutex_);
77 auto const& index = pings_.get<AddressIndexTag>();
78 auto context_iter = index.find(address);
79 return (context_iter == index.end() ? PingContextPtr()
80 : PingContextPtr(new PingContext(**context_iter)));
81}
82
85 MultiThreadingLock lock(*mutex_);
86 auto const& index = pings_.get<QueryIndexTag>();
87 auto context_iter = index.find(query);
88 return (context_iter == index.end() ? PingContextPtr()
89 : PingContextPtr(new PingContext(**context_iter)));
90}
91
94 MultiThreadingLock lock(*mutex_);
95 auto const& index = pings_.get<NextToSendIndexTag>();
96 auto context_iter = index.lower_bound(boost::make_tuple(true, PingContext::MIN_TIME()));
97 return (context_iter == index.end() ? PingContextPtr()
98 : PingContextPtr(new PingContext(**context_iter)));
99}
100
103 MultiThreadingLock lock(*mutex_);
104 auto const& index = pings_.get<ExpirationIndexTag>();
105 auto context_iter = index.lower_bound(boost::make_tuple(true, PingContext::now() + milliseconds(1)));
106 return (context_iter == index.end() ? PingContextPtr()
107 : PingContextPtr(new PingContext(**context_iter)));
108}
109
112 MultiThreadingLock lock(*mutex_);
113 auto const& index = pings_.get<ExpirationIndexTag>();
114 auto lower_limit = index.lower_bound(boost::make_tuple(true, PingContext::MIN_TIME()));
115 auto upper_limit = index.upper_bound(boost::make_tuple(true, since));
116
118 for (auto context_iter = lower_limit; context_iter != upper_limit; ++context_iter) {
119 PingContextPtr context(new PingContext(**context_iter));
120 collection->push_back(context);
121 }
122
123 return (collection);
124}
125
128 MultiThreadingLock lock(*mutex_);
129 auto const& index = pings_.get<AddressIndexTag>();
131 for (auto const& context_iter : index) {
132 collection->push_back(PingContextPtr(new PingContext(*context_iter)));
133 }
134
135 return (collection);
136}
137
139 MultiThreadingLock lock(*mutex_);
140 pings_.clear();
141}
142
143} // end of namespace ping_check
144} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
Exception thrown when an attempt was made to add a duplicate context.
void clear()
Removes all contexts from the store.
PingContextPtr getContextByQuery(isc::dhcp::Pkt4Ptr &query)
Fetches the context with a given query packet.
PingContextPtr getContextByAddress(const isc::asiolink::IOAddress &address)
Fetches the context with a given target address.
PingContextPtr addContext(isc::dhcp::Lease4Ptr &lease, isc::dhcp::Pkt4Ptr &query, uint32_t min_echos, uint32_t reply_timeout, isc::hooks::ParkingLotHandlePtr &parking_lot=PingContext::EMPTY_LOT())
Creates a new PingContext and adds it to the store.
PingContextCollectionPtr getAll()
Fetches all of the contexts (in order by target)
PingContextCollectionPtr getExpiredSince(const TimeStamp &since=PingContext::now())
Fetches the contexts in WAITING_FOR_REPLY that expired since a given time.
void deleteContext(const PingContextPtr &context)
Removes the context from the store.
PingContextPtr getNextToSend()
Fetches the context in WAITING_TO_SEND with the oldest send wait start time.
PingContextPtr getExpiresNext()
Fetches the context in WAITING_FOR_REPLY with the oldest expiration time that has not already passed ...
void updateContext(const PingContextPtr &context)
Updates a context in the store.
Embodies the life cycle of a ping check test for a single address for a single DHCPDISCOVER.
static const TimeStamp & MIN_TIME()
Fetches the minimum timestamp.
static TimeStamp now()
Fetches the current timestamp (UTC/milliseconds precision)
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
boost::shared_ptr< PingContextCollection > PingContextCollectionPtr
Type for a pointer to a collection of PingContextPtrs.
std::vector< PingContextPtr > PingContextCollection
Type for a collection of PingContextPtrs.
std::chrono::time_point< std::chrono::system_clock > TimeStamp
Specifies the type for time stamps.
boost::shared_ptr< PingContext > PingContextPtr
Defines a shared pointer to a PingContext.
Defines the logger used by the top-level component of kea-lfc.
Tag for index by target address.
Tag for index by expiration time.
Tag for index by send wait start time.
Tag for index by the query packet.
RAII lock object to protect the code in the same scope with a mutex.