Kea 1.5.0
alloc_engine.h
Go to the documentation of this file.
1// Copyright (C) 2012-2018 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 ALLOC_ENGINE_H
8#define ALLOC_ENGINE_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/classify.h>
12#include <dhcp/duid.h>
13#include <dhcp/hwaddr.h>
14#include <dhcp/pkt4.h>
15#include <dhcp/pkt6.h>
16#include <dhcp/option6_ia.h>
17#include <dhcpsrv/host.h>
18#include <dhcpsrv/subnet.h>
19#include <dhcpsrv/lease_mgr.h>
21
22#include <boost/function.hpp>
23#include <boost/shared_ptr.hpp>
24#include <boost/noncopyable.hpp>
25
26#include <list>
27#include <map>
28#include <set>
29#include <utility>
30
31namespace isc {
32namespace dhcp {
33
37public:
38
44 AllocFailed(const char* file, size_t line, const char* what)
45 : isc::Exception(file, line, what) {}
46};
47
56class AllocEngine : public boost::noncopyable {
57protected:
58
63 class Allocator {
64 public:
65
91 pickAddress(const SubnetPtr& subnet,
92 const ClientClasses& client_classes,
93 const DuidPtr& duid,
94 const isc::asiolink::IOAddress& hint) = 0;
95
101 :pool_type_(pool_type) {
102 }
103
105 virtual ~Allocator() {
106 }
107 protected:
108
111 };
112
114 typedef boost::shared_ptr<Allocator> AllocatorPtr;
115
123 public:
124
130
139 pickAddress(const SubnetPtr& subnet,
140 const ClientClasses& client_classes,
141 const DuidPtr& duid,
142 const isc::asiolink::IOAddress& hint);
143 protected:
144
157 const uint8_t prefix_len);
158
170 bool prefix, const uint8_t prefix_len);
171
172 };
173
177 class HashedAllocator : public Allocator {
178 public:
179
183
194 pickAddress(const SubnetPtr& subnet,
195 const ClientClasses& client_classes,
196 const DuidPtr& duid,
197 const isc::asiolink::IOAddress& hint);
198 };
199
203 class RandomAllocator : public Allocator {
204 public:
205
209
220 pickAddress(const SubnetPtr& subnet,
221 const ClientClasses& client_classes,
222 const DuidPtr& duid,
223 const isc::asiolink::IOAddress& hint);
224 };
225
226public:
227
229 typedef enum {
230 ALLOC_ITERATIVE, // iterative - one address after another
231 ALLOC_HASHED, // hashed - client's DUID/client-id is hashed
232 ALLOC_RANDOM // random - an address is randomly selected
234
246 AllocEngine(AllocType engine_type, uint64_t attempts, bool ipv6 = true);
247
249 virtual ~AllocEngine() { }
250
257
258private:
259
264 std::map<Lease::Type, AllocatorPtr> allocators_;
265
267 uint64_t attempts_;
268
269 // hook name indexes (used in hooks callouts)
270 int hook_index_lease4_select_;
271 int hook_index_lease6_select_;
272
273public:
274
280 typedef std::pair<isc::asiolink::IOAddress, uint8_t> ResourceType;
281
283 typedef std::vector<ResourceType> HintContainer;
284
286 typedef std::set<ResourceType> ResourceContainer;
287
289 typedef std::pair<Host::IdentifierType, std::vector<uint8_t> > IdentifierPair;
290
292 typedef std::list<IdentifierPair> IdentifierList;
293
316 struct ClientContext6 : public boost::noncopyable {
317
319
320
325
332
335
340
343
346
350
356 std::map<SubnetID, ConstHostPtr> hosts_;
357
362
367
372 std::string hostname_;
373
376
379
382
384
386 struct IAContext {
387
390 uint32_t iaid_;
391
394
400
409
417
421
425 IAContext();
426
431 void addHint(const asiolink::IOAddress& prefix,
432 const uint8_t prefix_len = 128);
433 };
434
436 std::vector<IAContext> ias_;
437
442 void addAllocatedResource(const asiolink::IOAddress& prefix,
443 const uint8_t prefix_len = 128);
444
449 bool isAllocated(const asiolink::IOAddress& prefix,
450 const uint8_t prefix_len = 128) const;
451
458 const std::vector<uint8_t>& identifier) {
459 host_identifiers_.push_back(IdentifierPair(id_type, identifier));
460 }
461
468 if (ias_.empty()) {
470 }
471 return (ias_.back());
472 }
473
479 ias_.push_back(IAContext());
480 };
481
486
495 ConstHostPtr globalHost() const;
496
502 bool hasGlobalReservation(const IPv6Resrv& resv) const;
503
506
528 ClientContext6(const Subnet6Ptr& subnet, const DuidPtr& duid,
529 const bool fwd_dns, const bool rev_dns,
530 const std::string& hostname, const bool fake_allocation,
531 const Pkt6Ptr& query,
532 const hooks::CalloutHandlePtr& callout_handle =
534 };
535
618 allocateLeases6(ClientContext6& ctx);
619
640 Lease6Collection renewLeases6(ClientContext6& ctx);
641
690 void reclaimExpiredLeases6(const size_t max_leases, const uint16_t timeout,
691 const bool remove_lease,
692 const uint16_t max_unwarned_cycles = 0);
693
699 void deleteExpiredReclaimedLeases6(const uint32_t secs);
700
749 void reclaimExpiredLeases4(const size_t max_leases, const uint16_t timeout,
750 const bool remove_lease,
751 const uint16_t max_unwarned_cycles = 0);
752
758 void deleteExpiredReclaimedLeases4(const uint32_t secs);
759
760
767 static void findReservation(ClientContext6& ctx);
768
778 static ConstHostPtr findGlobalReservation(ClientContext6& ctx);
779
784 static IPv6Resrv makeIPv6Resrv(const Lease6& lease) {
785 if (lease.type_ == Lease::TYPE_NA) {
786 return (IPv6Resrv(IPv6Resrv::TYPE_NA, lease.addr_,
787 (lease.prefixlen_ ? lease.prefixlen_ : 128)));
788 }
789
790 return (IPv6Resrv(IPv6Resrv::TYPE_PD, lease.addr_, lease.prefixlen_));
791 }
792
793private:
794
829 Lease6Ptr createLease6(ClientContext6& ctx,
830 const isc::asiolink::IOAddress& addr,
831 const uint8_t prefix_len,
833
843 Lease6Collection allocateUnreservedLeases6(ClientContext6& ctx);
844
858 void
859 allocateReservedLeases6(ClientContext6& ctx, Lease6Collection& existing_leases);
860
874 bool
875 allocateGlobalReservedLeases6(ClientContext6& ctx, Lease6Collection& existing_leases);
876
885 void
886 removeNonmatchingReservedLeases6(ClientContext6& ctx,
887 Lease6Collection& existing_leases);
888
896 void
897 removeNonmatchingReservedNoHostLeases6(ClientContext6& ctx,
898 Lease6Collection& existing_leases);
899
913 void
914 removeNonreservedLeases6(ClientContext6& ctx,
915 Lease6Collection& existing_leases);
916
949 reuseExpiredLease(Lease6Ptr& expired,
950 ClientContext6& ctx,
951 uint8_t prefix_len,
953
972 Lease6Collection updateLeaseData(ClientContext6& ctx,
973 const Lease6Collection& leases);
974
979 static bool
980 removeLeases(Lease6Collection& container,
981 const asiolink::IOAddress& addr);
982
995 void extendLease6(ClientContext6& ctx, Lease6Ptr lease);
996
1004 enum DbReclaimMode {
1005 DB_RECLAIM_REMOVE,
1006 DB_RECLAIM_UPDATE,
1007 DB_RECLAIM_LEAVE_UNCHANGED
1008 };
1009
1021 template<typename LeasePtrType>
1022 void reclaimExpiredLease(const LeasePtrType& lease,
1023 const bool remove_lease,
1024 const hooks::CalloutHandlePtr& callout_handle);
1025
1036 template<typename LeasePtrType>
1037 void reclaimExpiredLease(const LeasePtrType& lease,
1038 const hooks::CalloutHandlePtr& callout_handle);
1039
1050 void reclaimExpiredLease(const Lease6Ptr& lease,
1051 const DbReclaimMode& reclaim_mode,
1052 const hooks::CalloutHandlePtr& callout_handle);
1053
1064 void reclaimExpiredLease(const Lease4Ptr& lease,
1065 const DbReclaimMode& reclaim_mode,
1066 const hooks::CalloutHandlePtr& callout_handle);
1067
1086 template<typename LeasePtrType>
1087 void reclaimLeaseInDatabase(const LeasePtrType& lease,
1088 const bool remove_lease,
1089 const boost::function<void (const LeasePtrType&)>&
1090 lease_update_fun) const;
1091
1103 bool reclaimDeclined(const Lease4Ptr& lease);
1104
1116 bool reclaimDeclined(const Lease6Ptr& lease);
1117
1118public:
1119
1137 struct ClientContext4 : public boost::noncopyable {
1140
1143
1146
1152
1155
1158
1163 std::string hostname_;
1164
1167
1174
1177
1180
1186 std::map<SubnetID, ConstHostPtr> hosts_;
1187
1194
1200
1204
1211 const std::vector<uint8_t>& identifier) {
1212 host_identifiers_.push_back(IdentifierPair(id_type, identifier));
1213 }
1214
1218 ConstHostPtr currentHost() const;
1219
1222
1237 ClientContext4(const Subnet4Ptr& subnet, const ClientIdPtr& clientid,
1238 const HWAddrPtr& hwaddr,
1239 const asiolink::IOAddress& requested_addr,
1240 const bool fwd_dns_update, const bool rev_dns_update,
1241 const std::string& hostname, const bool fake_allocation);
1242
1243 };
1244
1246 typedef boost::shared_ptr<ClientContext4> ClientContext4Ptr;
1247
1354
1363 static void findReservation(ClientContext4& ctx);
1364
1375
1376private:
1377
1408 Lease4Ptr discoverLease4(ClientContext4& ctx);
1409
1447 Lease4Ptr requestLease4(ClientContext4& ctx);
1448
1477 Lease4Ptr createLease4(const ClientContext4& ctx,
1478 const isc::asiolink::IOAddress& addr,
1480
1494 Lease4Ptr renewLease4(const Lease4Ptr& lease, ClientContext4& ctx);
1495
1511 Lease4Ptr
1512 reuseExpiredLease4(Lease4Ptr& expired, ClientContext4& ctx,
1514
1530 Lease4Ptr
1531 allocateOrReuseLease4(const asiolink::IOAddress& address,
1532 ClientContext4& ctx,
1534
1553 Lease4Ptr allocateUnreservedLease4(ClientContext4& ctx);
1554
1575 void updateLease4Information(const Lease4Ptr& lease,
1576 ClientContext4& ctx) const;
1577
1597 bool conditionalExtendLifetime(Lease& lease) const;
1598
1599private:
1600
1603 uint16_t incomplete_v4_reclamations_;
1604
1607 uint16_t incomplete_v6_reclamations_;
1608};
1609
1611typedef boost::shared_ptr<AllocEngine> AllocEnginePtr;
1612
1613}; // namespace isc::dhcp
1614}; // namespace isc
1615
1616#endif // ALLOC_ENGINE_H
Defines elements for storing the names of client classes.
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
base class for all address/prefix allocation algorithms
Definition: alloc_engine.h:63
virtual ~Allocator()
virtual destructor
Definition: alloc_engine.h:105
Lease::Type pool_type_
defines pool type allocation
Definition: alloc_engine.h:110
Allocator(Lease::Type pool_type)
Default constructor.
Definition: alloc_engine.h:100
virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr &subnet, const ClientClasses &client_classes, const DuidPtr &duid, const isc::asiolink::IOAddress &hint)=0
picks one address out of available pools in a given subnet
Address/prefix allocator that gets an address based on a hash.
Definition: alloc_engine.h:177
virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr &subnet, const ClientClasses &client_classes, const DuidPtr &duid, const isc::asiolink::IOAddress &hint)
returns an address based on hash calculated from client's DUID.
Address/prefix allocator that iterates over all addresses.
Definition: alloc_engine.h:122
static isc::asiolink::IOAddress increasePrefix(const isc::asiolink::IOAddress &prefix, const uint8_t prefix_len)
Returns the next prefix.
Definition: alloc_engine.cc:92
static isc::asiolink::IOAddress increaseAddress(const isc::asiolink::IOAddress &address, bool prefix, const uint8_t prefix_len)
Returns the next address or prefix.
virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr &subnet, const ClientClasses &client_classes, const DuidPtr &duid, const isc::asiolink::IOAddress &hint)
returns the next address from pools in a subnet
Random allocator that picks address randomly.
Definition: alloc_engine.h:203
virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr &subnet, const ClientClasses &client_classes, const DuidPtr &duid, const isc::asiolink::IOAddress &hint)
returns a random address from pool of specified subnet
DHCPv4 and DHCPv6 allocation engine.
Definition: alloc_engine.h:56
boost::shared_ptr< ClientContext4 > ClientContext4Ptr
Pointer to the ClientContext4.
static IPv6Resrv makeIPv6Resrv(const Lease6 &lease)
Creates an IPv6Resrv instance from a Lease6.
Definition: alloc_engine.h:784
std::set< ResourceType > ResourceContainer
Container holding allocated prefixes or addresses.
Definition: alloc_engine.h:286
AllocType
specifies allocation type
Definition: alloc_engine.h:229
std::pair< isc::asiolink::IOAddress, uint8_t > ResourceType
Defines a single hint (an address + prefix-length).
Definition: alloc_engine.h:280
static ConstHostPtr findGlobalReservation(ClientContext6 &ctx)
Attempts to find the host reservation for the client.
std::pair< Host::IdentifierType, std::vector< uint8_t > > IdentifierPair
A tuple holding host identifier type and value.
Definition: alloc_engine.h:289
virtual ~AllocEngine()
Destructor.
Definition: alloc_engine.h:249
static void findReservation(ClientContext6 &ctx)
boost::shared_ptr< Allocator > AllocatorPtr
defines a pointer to allocator
Definition: alloc_engine.h:114
void deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes reclaimed leases expired more than specified amount of time ago.
void reclaimExpiredLeases6(const size_t max_leases, const uint16_t timeout, const bool remove_lease, const uint16_t max_unwarned_cycles=0)
Reclaims expired IPv6 leases.
std::vector< ResourceType > HintContainer
Container for client's hints.
Definition: alloc_engine.h:283
std::list< IdentifierPair > IdentifierList
Map holding values to be used as host identifiers.
Definition: alloc_engine.h:292
void reclaimExpiredLeases4(const size_t max_leases, const uint16_t timeout, const bool remove_lease, const uint16_t max_unwarned_cycles=0)
Reclaims expired IPv4 leases.
Lease4Ptr allocateLease4(ClientContext4 &ctx)
Returns IPv4 lease.
void deleteExpiredReclaimedLeases6(const uint32_t secs)
Deletes reclaimed leases expired more than specified amount of time ago.
Lease6Collection allocateLeases6(ClientContext6 &ctx)
Allocates IPv6 leases for a given IA container.
Lease6Collection renewLeases6(ClientContext6 &ctx)
Renews existing DHCPv6 leases for a given IA.
AllocatorPtr getAllocator(Lease::Type type)
Returns allocator for a given pool type.
An exception that is thrown when allocation module fails (e.g.
Definition: alloc_engine.h:36
AllocFailed(const char *file, size_t line, const char *what)
constructor
Definition: alloc_engine.h:44
Container for storing client class names.
Definition: classify.h:43
IdentifierType
Type of the host identifier.
Definition: host.h:252
IPv6 reservation for a host.
Definition: host.h:106
CalloutNextStep
Specifies allowed next steps.
An abstract API for lease database.
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition: subnet.h:455
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:464
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:21
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:463
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:604
boost::shared_ptr< AllocEngine > AllocEnginePtr
A pointer to the AllocEngine object.
boost::shared_ptr< Option6IA > Option6IAPtr
A pointer to the Option6IA object.
Definition: option6_ia.h:20
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:629
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:105
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:728
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:31
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:248
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Context information for the DHCPv4 lease allocation.
ClientIdPtr clientid_
Client identifier from the DHCP message.
ConstHostPtr currentHost() const
Returns host for currently selected subnet.
Pkt4Ptr query_
A pointer to the client's message.
Subnet4Ptr subnet_
Subnet selected for the client by the server.
Lease4Ptr new_lease_
A pointer to a newly allocated lease.
std::map< SubnetID, ConstHostPtr > hosts_
Holds a map of hosts belonging to the client within different subnets.
bool rev_dns_update_
Perform reverse DNS update.
bool fake_allocation_
Indicates if this is a real or fake allocation.
hooks::CalloutHandlePtr callout_handle_
Callout handle associated with the client's message.
Lease4Ptr old_lease_
A pointer to an old lease that the client had before update.
bool fwd_dns_update_
Perform forward DNS update.
asiolink::IOAddress requested_address_
An address that the client desires.
Lease4Ptr conflicting_lease_
A pointer to the object representing a lease in conflict.
void addHostIdentifier(const Host::IdentifierType &id_type, const std::vector< uint8_t > &identifier)
Convenience function adding host identifier into host_identifiers_ list.
IdentifierList host_identifiers_
A list holding host identifiers extracted from a message received by the server.
HWAddrPtr hwaddr_
HW address from the DHCP message.
Parameters pertaining to individual IAs.
Definition: alloc_engine.h:386
Lease6Collection old_leases_
A pointer to any old leases that the client had before update but are no longer valid after the updat...
Definition: alloc_engine.h:408
Option6IAPtr ia_rsp_
A pointer to the IA_NA/IA_PD option to be sent in response.
Definition: alloc_engine.h:420
Lease::Type type_
Lease type (IA or PD)
Definition: alloc_engine.h:393
Lease6Collection changed_leases_
A pointer to any leases that have changed FQDN information.
Definition: alloc_engine.h:416
uint32_t iaid_
iaid IAID field from IA_NA or IA_PD that is being processed
Definition: alloc_engine.h:390
void addHint(const asiolink::IOAddress &prefix, const uint8_t prefix_len=128)
Convenience method adding new hint.
Context information for the DHCPv6 leases allocation.
Definition: alloc_engine.h:316
IAContext & currentIA()
Returns IA specific context for the currently processed IA.
Definition: alloc_engine.h:467
std::vector< IAContext > ias_
Container holding IA specific contexts.
Definition: alloc_engine.h:436
void addHostIdentifier(const Host::IdentifierType &id_type, const std::vector< uint8_t > &identifier)
Convenience function adding host identifier into host_identifiers_ list.
Definition: alloc_engine.h:457
bool fake_allocation_
Indicates if this is a real or fake allocation.
Definition: alloc_engine.h:331
ConstHostPtr currentHost() const
Returns host from the most preferred subnet.
DuidPtr duid_
Client identifier.
Definition: alloc_engine.h:342
void addAllocatedResource(const asiolink::IOAddress &prefix, const uint8_t prefix_len=128)
Convenience method adding allocated prefix or address.
Lease6Collection new_leases_
A collection of newly allocated leases.
Definition: alloc_engine.h:381
HWAddrPtr hwaddr_
Hardware/MAC address (if available, may be NULL)
Definition: alloc_engine.h:345
hooks::CalloutHandlePtr callout_handle_
Callout handle associated with the client's message.
Definition: alloc_engine.h:375
bool isAllocated(const asiolink::IOAddress &prefix, const uint8_t prefix_len=128) const
Checks if specified address or prefix was allocated.
Subnet6Ptr subnet_
Subnet selected for the client by the server.
Definition: alloc_engine.h:334
Subnet6Ptr host_subnet_
Subnet from which host reservations should be retrieved.
Definition: alloc_engine.h:339
bool hasGlobalReservation(const IPv6Resrv &resv) const
Determines if a global reservation exists.
ResourceContainer allocated_resources_
Holds addresses and prefixes allocated for all IAs.
Definition: alloc_engine.h:378
bool rev_dns_update_
A boolean value which indicates that server takes responsibility for the reverse DNS Update for this ...
Definition: alloc_engine.h:366
ConstHostPtr globalHost() const
Returns global host reservation if there is one.
Pkt6Ptr query_
A pointer to the client's message.
Definition: alloc_engine.h:324
void createIAContext()
Creates new IA context.
Definition: alloc_engine.h:478
IdentifierList host_identifiers_
A list holding host identifiers extracted from a message received by the server.
Definition: alloc_engine.h:349
std::map< SubnetID, ConstHostPtr > hosts_
Holds a map of hosts belonging to the client within different subnets.
Definition: alloc_engine.h:356
bool fwd_dns_update_
A boolean value which indicates that server takes responsibility for the forward DNS Update for this ...
Definition: alloc_engine.h:361
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:471
Lease::Type type_
Lease type.
Definition: lease.h:476
uint8_t prefixlen_
IPv6 prefix length.
Definition: lease.h:481
a common structure for IPv4 and IPv6 leases
Definition: lease.h:35
Type
Type of lease or pool.
Definition: lease.h:38
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition: lease.h:39
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition: lease.h:102