Kea  2.3.5-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>
14 #include <database/db_exceptions.h>
15 #include <dhcp/duid.h>
16 #include <dhcp/option.h>
17 #include <dhcp/hwaddr.h>
19 #include <dhcpsrv/lease.h>
20 #include <dhcpsrv/subnet.h>
21 
22 #include <boost/noncopyable.hpp>
23 #include <boost/shared_ptr.hpp>
24 
25 #include <fstream>
26 #include <iostream>
27 #include <map>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
39 namespace isc {
40 namespace dhcp {
41 
43 typedef std::pair<uint32_t, uint32_t> VersionPair;
44 
47 public:
48 
54  explicit LeasePageSize(const size_t page_size);
55 
56  const size_t page_size_;
57 };
58 
64 struct LeaseStatsRow {
67  subnet_id_(0), lease_type_(Lease::TYPE_NA),
68  lease_state_(Lease::STATE_DEFAULT), state_count_(0) {
69  }
70 
78  LeaseStatsRow(const SubnetID& subnet_id, const uint32_t lease_state,
79  const int64_t state_count)
80  : subnet_id_(subnet_id), lease_type_(Lease::TYPE_NA),
81  lease_state_(lease_state), state_count_(state_count) {
82  }
83 
90  LeaseStatsRow(const SubnetID& subnet_id, const Lease::Type& lease_type,
91  const uint32_t lease_state, const int64_t state_count)
92  : subnet_id_(subnet_id), lease_type_(lease_type),
93  lease_state_(lease_state), state_count_(state_count) {
94  }
95 
97  bool operator< (const LeaseStatsRow &rhs) const {
98  if (subnet_id_ < rhs.subnet_id_) {
99  return (true);
100  }
101 
102  if (subnet_id_ == rhs.subnet_id_ &&
103  lease_type_ < rhs.lease_type_) {
104  return (true);
105  }
106 
107  if (subnet_id_ == rhs.subnet_id_ &&
108  lease_type_ == rhs.lease_type_ &&
109  lease_state_ < rhs.lease_state_) {
110  return (true);
111  }
112 
113  return (false);
114  }
115 
121  uint32_t lease_state_;
123  int64_t state_count_;
124 };
125 
132 public:
134  typedef enum {
137  SUBNET_RANGE
138  } SelectMode;
139 
142  LeaseStatsQuery();
143 
150  LeaseStatsQuery(const SubnetID& subnet_id);
151 
160  LeaseStatsQuery(const SubnetID& first_subnet_id, const SubnetID& last_subnet_id);
161 
163  virtual ~LeaseStatsQuery() {};
164 
170  virtual void start() {};
171 
178  virtual bool getNextRow(LeaseStatsRow& row);
179 
182  return (first_subnet_id_);
183  };
184 
187  return (last_subnet_id_);
188  };
189 
193  SelectMode getSelectMode() const {
194  return (select_mode_);
195  };
196 
197 protected:
200 
203 
204 private:
206  SelectMode select_mode_;
207 };
208 
210 typedef boost::shared_ptr<LeaseStatsQuery> LeaseStatsQueryPtr;
211 
213 typedef boost::shared_ptr<LeaseStatsRow> LeaseStatsRowPtr;
214 
225 class LeaseMgr {
226 public:
229  LeaseMgr() : extended_info_tables_enabled_(false)
230  {}
231 
233  virtual ~LeaseMgr()
234  {}
235 
238  static std::string getDBVersion();
239 
251  virtual bool addLease(const Lease4Ptr& lease) = 0;
252 
264  virtual bool addLease(const Lease6Ptr& lease) = 0;
265 
278  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
279 
290  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
291 
302  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
303  SubnetID subnet_id) const = 0;
304 
315  virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
316 
326  virtual Lease4Ptr getLease4(const ClientId& clientid,
327  SubnetID subnet_id) const = 0;
328 
334  virtual Lease4Collection getLeases4(SubnetID subnet_id) const = 0;
335 
341  virtual Lease4Collection getLeases4(const std::string& hostname) const = 0;
342 
346  virtual Lease4Collection getLeases4() const = 0;
347 
372  virtual Lease4Collection
373  getLeases4(const asiolink::IOAddress& lower_bound_address,
374  const LeasePageSize& page_size) const = 0;
375 
386  virtual Lease6Ptr getLease6(Lease::Type type,
387  const isc::asiolink::IOAddress& addr) const = 0;
388 
401  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
402  uint32_t iaid) const = 0;
403 
415  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
416  uint32_t iaid, SubnetID subnet_id) const = 0;
417 
441  Lease6Ptr getLease6(Lease::Type type, const DUID& duid,
442  uint32_t iaid, SubnetID subnet_id) const;
443 
449  virtual Lease6Collection getLeases6(SubnetID subnet_id) const = 0;
450 
456  virtual Lease6Collection getLeases6(const std::string& hostname) const = 0;
457 
461  virtual Lease6Collection getLeases6() const = 0;
462 
467  virtual Lease6Collection getLeases6(const DUID& duid) const = 0;
468 
493  virtual Lease6Collection
494  getLeases6(const asiolink::IOAddress& lower_bound_address,
495  const LeasePageSize& page_size) const = 0;
496 
507  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
508  const size_t max_leases) const = 0;
509 
520  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
521  const size_t max_leases) const = 0;
522 
528  virtual void updateLease4(const Lease4Ptr& lease4) = 0;
529 
533  virtual void updateLease6(const Lease6Ptr& lease6) = 0;
534 
543  virtual bool deleteLease(const Lease4Ptr& lease) = 0;
544 
553  virtual bool deleteLease(const Lease6Ptr& lease) = 0;
554 
562  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) = 0;
563 
571  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) = 0;
572 
590  void recountLeaseStats4();
591 
601  virtual LeaseStatsQueryPtr startLeaseStatsQuery4();
602 
612  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
613 
625  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
626  const SubnetID& last_subnet_id);
627 
647  void recountLeaseStats6();
648 
658  virtual LeaseStatsQueryPtr startLeaseStatsQuery6();
659 
669  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
670 
682  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
683  const SubnetID& last_subnet_id);
684 
692  virtual size_t wipeLeases4(const SubnetID& subnet_id) = 0;
693 
701  virtual size_t wipeLeases6(const SubnetID& subnet_id) = 0;
702 
714  virtual std::string checkLimits4(isc::data::ConstElementPtr const& user_context) const = 0;
715 
727  virtual std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const = 0;
728 
733  virtual bool isJsonSupported() const = 0;
734 
740  virtual std::string getType() const = 0;
741 
748  virtual std::string getName() const = 0;
749 
755  virtual std::string getDescription() const = 0;
756 
771  virtual VersionPair getVersion() const = 0;
772 
777  virtual void commit() = 0;
778 
783  virtual void rollback() = 0;
784 
788  static void setIOService(const isc::asiolink::IOServicePtr& io_service) {
789  io_service_ = io_service;
790  }
791 
794  return (io_service_);
795  }
796 
797  // -- The following are memfile only, but defined in the base LeaseMgr for convenience. --
798 
806  virtual size_t getClassLeaseCount(const ClientClass& client_class,
807  const Lease::Type& ltype = Lease::TYPE_V4) const = 0;
808 
810  virtual void recountClassLeases4() = 0;
811 
813  virtual void recountClassLeases6() = 0;
814 
816  virtual void clearClassLeaseCounts() = 0;
817 
821 
836  static bool
837  upgradeLease4ExtendedInfo(const Lease4Ptr& lease,
840 
853  static bool
854  upgradeLease6ExtendedInfo(const Lease6Ptr& lease,
857 
870  virtual Lease4Collection
871  getLeases4ByRelayId(const OptionBuffer& relay_id,
872  const asiolink::IOAddress& lower_bound_address,
873  const LeasePageSize& page_size,
874  const time_t& qry_start_time = 0,
875  const time_t& qry_end_time = 0) = 0;
876 
889  virtual Lease4Collection
890  getLeases4ByRemoteId(const OptionBuffer& remote_id,
891  const asiolink::IOAddress& lower_bound_address,
892  const LeasePageSize& page_size,
893  const time_t& qry_start_time = 0,
894  const time_t& qry_end_time = 0) = 0;
895 
906  virtual Lease6Collection
907  getLeases6ByRelayId(const DUID& relay_id,
908  const asiolink::IOAddress& link_addr,
909  uint8_t link_len,
910  const asiolink::IOAddress& lower_bound_address,
911  const LeasePageSize& page_size) = 0;
912 
923  virtual Lease6Collection
924  getLeases6ByRemoteId(const OptionBuffer& remote_id,
925  const asiolink::IOAddress& link_addr,
926  uint8_t link_len,
927  const asiolink::IOAddress& lower_bound_address,
928  const LeasePageSize& page_size) = 0;
929 
939  virtual Lease6Collection
940  getLeases6ByLink(const asiolink::IOAddress& link_addr,
941  uint8_t link_len,
942  const asiolink::IOAddress& lower_bound_address,
943  const LeasePageSize& page_size) = 0;
944 
948  virtual void writeLeases4(const std::string& filename) = 0;
949 
953  virtual void writeLeases6(const std::string& filename) = 0;
954 
961  return (extended_info_tables_enabled_);
962  }
963 
970  virtual size_t buildExtendedInfoTables6(bool update, bool current) = 0;
971 
972 protected:
973 
975 
983  virtual void setExtendedInfoTablesEnabled(const bool enabled) {
984  extended_info_tables_enabled_ = enabled;
985  }
986 
993  virtual void setExtendedInfoTablesEnabled(const db::DatabaseConnection::ParameterMap& parameters);
994 
999  virtual bool addExtendedInfo6(const Lease6Ptr& lease);
1000 
1004  virtual void deleteExtendedInfo6(const isc::asiolink::IOAddress& addr) = 0;
1005 
1010  virtual void addRelayId6(const isc::asiolink::IOAddress& lease_addr,
1011  const std::vector<uint8_t>& relay_id) = 0;
1012 
1017  virtual void addRemoteId6(const isc::asiolink::IOAddress& lease_addr,
1018  const std::vector<uint8_t>& remote_id) = 0;
1019 
1020 private:
1022  static isc::asiolink::IOServicePtr io_service_;
1023 
1026  bool extended_info_tables_enabled_;
1027 };
1028 
1029 } // namespace dhcp
1030 } // namespace isc
1031 
1032 #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:43
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:210
virtual void setExtendedInfoTablesEnabled(const bool enabled)
Extended information / Bulk Lease Query shared interface.
Definition: lease_mgr.h:983
LeasePageSize(const size_t page_size)
Constructor.
Definition: lease_mgr.cc:42
Abstract Lease Manager.
Definition: lease_mgr.h:225
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:514
LeaseStatsRow()
Default constructor.
Definition: lease_mgr.h:66
Base class for fulfilling a statistical lease data query.
Definition: lease_mgr.h:131
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:793
virtual ~LeaseStatsQuery()
virtual destructor
Definition: lease_mgr.h:163
const size_t page_size_
Holds page size.
Definition: lease_mgr.h:56
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:294
SubnetID last_subnet_id_
Last subnet_id in the selection criteria when a range is given.
Definition: lease_mgr.h:202
LeaseStatsRow(const SubnetID &subnet_id, const uint32_t lease_state, const int64_t state_count)
Constructor.
Definition: lease_mgr.h:78
SelectMode
Defines the types of selection criteria supported.
Definition: lease_mgr.h:134
SubnetID subnet_id_
The subnet ID to which this data applies.
Definition: lease_mgr.h:117
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:193
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:677
bool getExtendedInfoTablesEnabled() const
Returns the setting indicating if lease extended info tables are enabled.
Definition: lease_mgr.h:960
virtual ~LeaseMgr()
Destructor.
Definition: lease_mgr.h:233
SubnetID getLastSubnetID() const
Returns the value of last subnet ID specified (or zero)
Definition: lease_mgr.h:186
boost::shared_ptr< LeaseStatsRow > LeaseStatsRowPtr
Defines a pointer to a LeaseStatsRow.
Definition: lease_mgr.h:213
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:90
ExtendedInfoSanity
Values for extended info sanity checks done for leases.
SubnetID getFirstSubnetID() const
Returns the value of first subnet ID specified (or zero)
Definition: lease_mgr.h:181
virtual void start()
Executes the query.
Definition: lease_mgr.h:170
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:121
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:195
int64_t state_count_
state_count The count of leases in the lease state
Definition: lease_mgr.h:123
static void setIOService(const isc::asiolink::IOServicePtr &io_service)
Sets IO service to be used by the Lease Manager.
Definition: lease_mgr.h:788
Contains a single row of lease statistical data.
Definition: lease_mgr.h:64
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:46
Lease::Type lease_type_
The lease_type to which the count applies.
Definition: lease_mgr.h:119
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:519
LeaseMgr()
Constructor.
Definition: lease_mgr.h:229
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:25