Kea  2.3.1-git
lease_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2022 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 LEASE_MGR_H
8 #define LEASE_MGR_H
9 
10 #include <asiolink/io_address.h>
11 #include <asiolink/io_service.h>
12 #include <cc/data.h>
13 #include <database/db_exceptions.h>
14 #include <dhcp/duid.h>
15 #include <dhcp/option.h>
16 #include <dhcp/hwaddr.h>
17 #include <dhcpsrv/lease.h>
18 #include <dhcpsrv/subnet.h>
19 
20 #include <boost/noncopyable.hpp>
21 #include <boost/shared_ptr.hpp>
22 
23 #include <fstream>
24 #include <iostream>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
37 namespace isc {
38 namespace dhcp {
39 
41 typedef std::pair<uint32_t, uint32_t> VersionPair;
42 
45 public:
46 
52  explicit LeasePageSize(const size_t page_size);
53 
54  const size_t page_size_;
55 };
56 
62 struct LeaseStatsRow {
65  subnet_id_(0), lease_type_(Lease::TYPE_NA),
66  lease_state_(Lease::STATE_DEFAULT), state_count_(0) {
67  }
68 
76  LeaseStatsRow(const SubnetID& subnet_id, const uint32_t lease_state,
77  const int64_t state_count)
78  : subnet_id_(subnet_id), lease_type_(Lease::TYPE_NA),
79  lease_state_(lease_state), state_count_(state_count) {
80  }
81 
88  LeaseStatsRow(const SubnetID& subnet_id, const Lease::Type& lease_type,
89  const uint32_t lease_state, const int64_t state_count)
90  : subnet_id_(subnet_id), lease_type_(lease_type),
91  lease_state_(lease_state), state_count_(state_count) {
92  }
93 
95  bool operator< (const LeaseStatsRow &rhs) const {
96  if (subnet_id_ < rhs.subnet_id_) {
97  return (true);
98  }
99 
100  if (subnet_id_ == rhs.subnet_id_ &&
101  lease_type_ < rhs.lease_type_) {
102  return (true);
103  }
104 
105  if (subnet_id_ == rhs.subnet_id_ &&
106  lease_type_ == rhs.lease_type_ &&
107  lease_state_ < rhs.lease_state_) {
108  return (true);
109  }
110 
111  return (false);
112  }
113 
119  uint32_t lease_state_;
121  int64_t state_count_;
122 };
123 
130 public:
132  typedef enum {
135  SUBNET_RANGE
136  } SelectMode;
137 
140  LeaseStatsQuery();
141 
148  LeaseStatsQuery(const SubnetID& subnet_id);
149 
158  LeaseStatsQuery(const SubnetID& first_subnet_id, const SubnetID& last_subnet_id);
159 
161  virtual ~LeaseStatsQuery() {};
162 
168  virtual void start() {};
169 
176  virtual bool getNextRow(LeaseStatsRow& row);
177 
180  return (first_subnet_id_);
181  };
182 
185  return (last_subnet_id_);
186  };
187 
191  SelectMode getSelectMode() const {
192  return (select_mode_);
193  };
194 
195 protected:
198 
201 
202 private:
204  SelectMode select_mode_;
205 };
206 
208 typedef boost::shared_ptr<LeaseStatsQuery> LeaseStatsQueryPtr;
209 
211 typedef boost::shared_ptr<LeaseStatsRow> LeaseStatsRowPtr;
212 
223 class LeaseMgr {
224 public:
228  {}
229 
231  virtual ~LeaseMgr()
232  {}
233 
236  static std::string getDBVersion();
237 
249  virtual bool addLease(const Lease4Ptr& lease) = 0;
250 
262  virtual bool addLease(const Lease6Ptr& lease) = 0;
263 
276  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
277 
288  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
289 
300  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
301  SubnetID subnet_id) const = 0;
302 
313  virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
314 
324  virtual Lease4Ptr getLease4(const ClientId& clientid,
325  SubnetID subnet_id) const = 0;
326 
332  virtual Lease4Collection getLeases4(SubnetID subnet_id) const = 0;
333 
339  virtual Lease4Collection getLeases4(const std::string& hostname) const = 0;
340 
344  virtual Lease4Collection getLeases4() const = 0;
345 
370  virtual Lease4Collection
371  getLeases4(const asiolink::IOAddress& lower_bound_address,
372  const LeasePageSize& page_size) const = 0;
373 
384  virtual Lease6Ptr getLease6(Lease::Type type,
385  const isc::asiolink::IOAddress& addr) const = 0;
386 
399  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
400  uint32_t iaid) const = 0;
401 
413  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
414  uint32_t iaid, SubnetID subnet_id) const = 0;
415 
439  Lease6Ptr getLease6(Lease::Type type, const DUID& duid,
440  uint32_t iaid, SubnetID subnet_id) const;
441 
447  virtual Lease6Collection getLeases6(SubnetID subnet_id) const = 0;
448 
454  virtual Lease6Collection getLeases6(const std::string& hostname) const = 0;
455 
459  virtual Lease6Collection getLeases6() const = 0;
460 
465  virtual Lease6Collection getLeases6(const DUID& duid) const = 0;
466 
491  virtual Lease6Collection
492  getLeases6(const asiolink::IOAddress& lower_bound_address,
493  const LeasePageSize& page_size) const = 0;
494 
505  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
506  const size_t max_leases) const = 0;
507 
518  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
519  const size_t max_leases) const = 0;
520 
526  virtual void updateLease4(const Lease4Ptr& lease4) = 0;
527 
531  virtual void updateLease6(const Lease6Ptr& lease6) = 0;
532 
541  virtual bool deleteLease(const Lease4Ptr& lease) = 0;
542 
551  virtual bool deleteLease(const Lease6Ptr& lease) = 0;
552 
560  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) = 0;
561 
569  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) = 0;
570 
588  void recountLeaseStats4();
589 
599  virtual LeaseStatsQueryPtr startLeaseStatsQuery4();
600 
610  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
611 
623  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
624  const SubnetID& last_subnet_id);
625 
645  void recountLeaseStats6();
646 
656  virtual LeaseStatsQueryPtr startLeaseStatsQuery6();
657 
667  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
668 
680  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
681  const SubnetID& last_subnet_id);
682 
690  virtual size_t wipeLeases4(const SubnetID& subnet_id) = 0;
691 
699  virtual size_t wipeLeases6(const SubnetID& subnet_id) = 0;
700 
712  virtual std::string checkLimits4(isc::data::ConstElementPtr const& user_context) const = 0;
713 
725  virtual std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const = 0;
726 
731  virtual bool isJsonSupported() const = 0;
732 
738  virtual std::string getType() const = 0;
739 
746  virtual std::string getName() const = 0;
747 
753  virtual std::string getDescription() const = 0;
754 
769  virtual VersionPair getVersion() const = 0;
770 
775  virtual void commit() = 0;
776 
781  virtual void rollback() = 0;
782 
786  static void setIOService(const isc::asiolink::IOServicePtr& io_service) {
787  io_service_ = io_service;
788  }
789 
792  return (io_service_);
793  }
794 
795  // -- The following are memfile only, but defined in the base LeaseMgr for convenience. --
796 
804  virtual size_t getClassLeaseCount(const ClientClass& client_class,
805  const Lease::Type& ltype = Lease::TYPE_V4) const = 0;
806 
808  virtual void recountClassLeases4() = 0;
809 
811  virtual void recountClassLeases6() = 0;
812 
814  virtual void clearClassLeaseCounts() = 0;
815 
819 
832  virtual Lease4Collection
833  getLeases4ByRelayId(const OptionBuffer& relay_id,
834  const asiolink::IOAddress& lower_bound_address,
835  const LeasePageSize& page_size,
836  const time_t& qry_start_time = 0,
837  const time_t& qry_end_time = 0) = 0;
838 
851  virtual Lease4Collection
852  getLeases4ByRemoteId(const OptionBuffer& remote_id,
853  const asiolink::IOAddress& lower_bound_address,
854  const LeasePageSize& page_size,
855  const time_t& qry_start_time = 0,
856  const time_t& qry_end_time = 0) = 0;
857 
867  virtual Lease6Collection
868  getLeases6ByRelayId(const DUID& relay_id,
869  const asiolink::IOAddress& link_addr,
870  const asiolink::IOAddress& lower_bound_address,
871  const LeasePageSize& page_size) = 0;
872 
882  virtual Lease6Collection
883  getLeases6ByRemoteId(const OptionBuffer& remote_id,
884  const asiolink::IOAddress& link_addr,
885  const asiolink::IOAddress& lower_bound_address,
886  const LeasePageSize& page_size) = 0;
887 
896  virtual Lease6Collection
897  getLeases6ByLink(const asiolink::IOAddress& link_addr,
898  const asiolink::IOAddress& lower_bound_address,
899  const LeasePageSize& page_size) = 0;
900 
904  virtual void writeLeases4(const std::string& filename) = 0;
905 
909  virtual void writeLeases6(const std::string& filename) = 0;
910 
911 private:
913  static isc::asiolink::IOServicePtr io_service_;
914 };
915 
916 } // namespace dhcp
917 } // namespace isc
918 
919 #endif // LEASE_MGR_H
bool operator<(Element const &a, Element const &b)
Definition: data.cc:219
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
Definition: lease_mgr.h:41
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:208
LeasePageSize(const size_t page_size)
Constructor.
Definition: lease_mgr.cc:36
Abstract Lease Manager.
Definition: lease_mgr.h:223
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:498
LeaseStatsRow()
Default constructor.
Definition: lease_mgr.h:64
Base class for fulfilling a statistical lease data query.
Definition: lease_mgr.h:129
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
Definition: lease_mgr.h:791
virtual ~LeaseStatsQuery()
virtual destructor
Definition: lease_mgr.h:161
const size_t page_size_
Holds page size.
Definition: lease_mgr.h:54
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:284
SubnetID last_subnet_id_
Last subnet_id in the selection criteria when a range is given.
Definition: lease_mgr.h:200
LeaseStatsRow(const SubnetID &subnet_id, const uint32_t lease_state, const int64_t state_count)
Constructor.
Definition: lease_mgr.h:76
SelectMode
Defines the types of selection criteria supported.
Definition: lease_mgr.h:132
SubnetID subnet_id_
The subnet ID to which this data applies.
Definition: lease_mgr.h:115
IPv4 lease.
Definition: lease.h:50
SelectMode getSelectMode() const
Returns the selection criteria mode The value returned is based upon the constructor variant used and...
Definition: lease_mgr.h:191
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:661
virtual ~LeaseMgr()
Destructor.
Definition: lease_mgr.h:231
SubnetID getLastSubnetID() const
Returns the value of last subnet ID specified (or zero)
Definition: lease_mgr.h:184
boost::shared_ptr< LeaseStatsRow > LeaseStatsRowPtr
Defines a pointer to a LeaseStatsRow.
Definition: lease_mgr.h:211
Defines the logger used by the top-level component of kea-lfc.
LeaseStatsRow(const SubnetID &subnet_id, const Lease::Type &lease_type, const uint32_t lease_state, const int64_t state_count)
Constructor.
Definition: lease_mgr.h:88
SubnetID getFirstSubnetID() const
Returns the value of first subnet ID specified (or zero)
Definition: lease_mgr.h:179
virtual void start()
Executes the query.
Definition: lease_mgr.h:168
a common structure for IPv4 and IPv6 leases
Definition: lease.h:31
uint32_t lease_state_
The lease_state to which the count applies.
Definition: lease_mgr.h:119
Type
Type of lease or pool.
Definition: lease.h:46
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
SubnetID first_subnet_id_
First (or only) subnet_id in the selection criteria.
Definition: lease_mgr.h:193
int64_t state_count_
state_count The count of leases in the lease state
Definition: lease_mgr.h:121
static void setIOService(const isc::asiolink::IOServicePtr &io_service)
Sets IO service to be used by the Lease Manager.
Definition: lease_mgr.h:786
Contains a single row of lease statistical data.
Definition: lease_mgr.h:62
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:44
Lease::Type lease_type_
The lease_type to which the count applies.
Definition: lease_mgr.h:117
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:503
LeaseMgr()
Constructor.
Definition: lease_mgr.h:227
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:24