Kea  2.1.7-git
memfile_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 MEMFILE_LEASE_MGR_H
8 #define MEMFILE_LEASE_MGR_H
9 
11 #include <asiolink/process_spawn.h>
13 #include <dhcp/hwaddr.h>
17 #include <dhcpsrv/lease_mgr.h>
18 
19 #include <boost/scoped_ptr.hpp>
20 #include <boost/shared_ptr.hpp>
21 
22 #include <mutex>
23 
24 namespace isc {
25 namespace dhcp {
26 
27 class LFCSetup;
28 
79 class Memfile_LeaseMgr : public LeaseMgr {
80 public:
81 
91  static const int MAJOR_VERSION_V4 = 2;
92 
94  static const int MINOR_VERSION_V4 = 1;
96 
108  static const int MAJOR_VERSION_V6 = 4;
109 
111  static const int MINOR_VERSION_V6 = 0;
113 
114 
121  enum Universe {
122  V4,
124  };
125 
129 
130 
152 
154  virtual ~Memfile_LeaseMgr();
155 
157  static std::string getDBVersion(Universe const& u);
158 
164  virtual bool addLease(const Lease4Ptr& lease);
165 
171  virtual bool addLease(const Lease6Ptr& lease);
172 
181  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
182 
193  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
194 
208  virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
209  SubnetID subnet_id) const;
210 
216  virtual Lease4Collection getLease4(const ClientId& client_id) const;
217 
230  virtual Lease4Ptr getLease4(const ClientId& clientid,
231  SubnetID subnet_id) const;
232 
238  virtual Lease4Collection getLeases4(SubnetID subnet_id) const;
239 
245  virtual Lease4Collection getLeases4(const std::string& hostname) const;
246 
250  virtual Lease4Collection getLeases4() const;
251 
276  virtual Lease4Collection
277  getLeases4(const asiolink::IOAddress& lower_bound_address,
278  const LeasePageSize& page_size) const;
279 
289  virtual Lease6Ptr getLease6(Lease::Type type,
290  const isc::asiolink::IOAddress& addr) const;
291 
301  const DUID& duid,
302  uint32_t iaid) const;
303 
317  const DUID& duid,
318  uint32_t iaid,
319  SubnetID subnet_id) const;
320 
326  virtual Lease6Collection getLeases6(SubnetID subnet_id) const;
327 
333  virtual Lease6Collection getLeases6(const std::string& hostname) const;
334 
338  virtual Lease6Collection getLeases6() const;
339 
343  virtual Lease6Collection getLeases6(const DUID& duid) const;
344 
369  virtual Lease6Collection
370  getLeases6(const asiolink::IOAddress& lower_bound_address,
371  const LeasePageSize& page_size) const;
372 
383  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
384  const size_t max_leases) const;
385 
396  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
397  const size_t max_leases) const;
398 
416  virtual void updateLease4(const Lease4Ptr& lease4);
417 
435  virtual void updateLease6(const Lease6Ptr& lease6);
436 
451  virtual bool deleteLease(const Lease4Ptr& lease);
452 
467  virtual bool deleteLease(const Lease6Ptr& lease);
468 
476  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
477 
485  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
486 
495  virtual size_t wipeLeases4(const SubnetID& subnet_id);
496 
505  virtual size_t wipeLeases6(const SubnetID& subnet_id);
506 
518  std::string checkLimits4(isc::data::ConstElementPtr const& user_context) const override;
519 
531  std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
532 
537  bool isJsonSupported() const override;
538 
539 private:
540 
544 
550  bool addLeaseInternal(const Lease4Ptr& lease);
551 
557  bool addLeaseInternal(const Lease6Ptr& lease);
558 
564  Lease4Ptr getLease4Internal(const isc::asiolink::IOAddress& addr) const;
565 
570  void getLease4Internal(const isc::dhcp::HWAddr& hwaddr,
571  Lease4Collection& collection) const;
572 
580  Lease4Ptr getLease4Internal(const HWAddr& hwaddr,
581  SubnetID subnet_id) const;
582 
587  void getLease4Internal(const ClientId& client_id,
588  Lease4Collection& collection) const;
589 
597  Lease4Ptr getLease4Internal(const ClientId& clientid,
598  const HWAddr& hwaddr,
599  SubnetID subnet_id) const;
600 
607  Lease4Ptr getLease4Internal(const ClientId& clientid,
608  SubnetID subnet_id) const;
609 
614  void getLeases4Internal(SubnetID subnet_id,
615  Lease4Collection& collection) const;
616 
621  void getLeases4Internal(const std::string& hostname,
622  Lease4Collection& collection) const;
623 
627  void getLeases4Internal(Lease4Collection& collection) const;
628 
635  void getLeases4Internal(const asiolink::IOAddress& lower_bound_address,
636  const LeasePageSize& page_size,
637  Lease4Collection& collection) const;
638 
645  Lease6Ptr getLease6Internal(Lease::Type type,
646  const isc::asiolink::IOAddress& addr) const;
647 
655  void getLeases6Internal(Lease::Type type,
656  const DUID& duid,
657  uint32_t iaid,
658  Lease6Collection& collection) const;
659 
668  void getLeases6Internal(Lease::Type type,
669  const DUID& duid,
670  uint32_t iaid,
671  SubnetID subnet_id,
672  Lease6Collection& collection) const;
673 
678  void getLeases6Internal(SubnetID subnet_id,
679  Lease6Collection& collection) const;
680 
685  void getLeases6Internal(const std::string& hostname,
686  Lease6Collection& collection) const;
687 
691  void getLeases6Internal(Lease6Collection& collection) const;
692 
697  void getLeases6Internal(const DUID& duid,
698  Lease6Collection& collection) const;
699 
706  void getLeases6Internal(const asiolink::IOAddress& lower_bound_address,
707  const LeasePageSize& page_size,
708  Lease6Collection& collection) const;
709 
716  void getExpiredLeases4Internal(Lease4Collection& expired_leases,
717  const size_t max_leases) const;
718 
725  void getExpiredLeases6Internal(Lease6Collection& expired_leases,
726  const size_t max_leases) const;
727 
742  void updateLease4Internal(const Lease4Ptr& lease4);
743 
758  void updateLease6Internal(const Lease6Ptr& lease6);
759 
774  bool deleteLeaseInternal(const Lease4Ptr& addr);
775 
790  bool deleteLeaseInternal(const Lease6Ptr& addr);
791 
797  size_t wipeLeases4Internal(const SubnetID& subnet_id);
798 
804  size_t wipeLeases6Internal(const SubnetID& subnet_id);
806 
833  template<typename IndexType, typename LeaseType, typename StorageType,
834  typename LeaseFileType>
835  uint64_t deleteExpiredReclaimedLeases(const uint32_t secs,
836  const Universe& universe,
837  StorageType& storage,
838  LeaseFileType& lease_file) const;
839 
840 public:
841 
847  virtual std::string getType() const {
848  return (std::string("memfile"));
849  }
850 
856  virtual std::string getName() const {
857  return ("memory");
858  }
859 
865  virtual std::string getDescription() const;
866 
871  virtual std::pair<uint32_t, uint32_t> getVersion() const;
872 
877  virtual void commit();
878 
883  virtual void rollback();
884 
886 
888 
889 
894  enum LFCFileType {
901  };
902 
921  static std::string appendSuffix(const std::string& file_name,
922  const LFCFileType& file_type);
924 
925 
929 
930 
934  std::string getDefaultLeaseFilePath(Universe u) const;
935 
942  std::string getLeaseFilePath(Universe u) const;
943 
955  bool persistLeases(Universe u) const;
956 
958 
959 private:
960 
961 
977  std::string initLeaseFilePath(Universe u);
978 
1034  template<typename LeaseObjectType, typename LeaseFileType,
1035  typename StorageType>
1036  bool loadLeasesFromFiles(const std::string& filename,
1037  boost::shared_ptr<LeaseFileType>& lease_file,
1038  StorageType& storage);
1039 
1041  Lease4Storage storage4_;
1042 
1044  Lease6Storage storage6_;
1045 
1047  boost::shared_ptr<CSVLeaseFile4> lease_file4_;
1048 
1050  boost::shared_ptr<CSVLeaseFile6> lease_file6_;
1051 
1052 public:
1053 
1058 
1059 
1063  bool isLFCRunning() const;
1064 
1067  int getLFCExitStatus() const;
1069 
1078 
1087  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
1088 
1098  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
1099  const SubnetID& last_subnet_id);
1100 
1109 
1118  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
1119 
1129  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
1130  const SubnetID& last_subnet_id);
1131 
1135 
1136 
1137 protected:
1138 
1150  virtual void lfcCallback();
1152 
1154 
1155 
1156 private:
1157 
1174  void lfcSetup(bool conversion_needed = false);
1175 
1198  template<typename LeaseFileType>
1199  void lfcExecute(boost::shared_ptr<LeaseFileType>& lease_file);
1200 
1202  boost::scoped_ptr<LFCSetup> lfc_setup_;
1203 
1208  db::DatabaseConnection conn_;
1209 
1211 
1213  boost::scoped_ptr<std::mutex> mutex_;
1214 };
1215 
1216 } // namespace dhcp
1217 } // namespace isc
1218 
1219 #endif // MEMFILE_LEASE_MGR_H
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
virtual void rollback()
Rollback Transactions.
virtual std::string getName() const
Returns backend name.
virtual ~Memfile_LeaseMgr()
Destructor (closes file)
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:208
Concrete implementation of a lease database backend using flat file.
virtual std::string getDescription() const
Returns description of the backend.
Abstract Lease Manager.
Definition: lease_mgr.h:223
std::string checkLimits4(isc::data::ConstElementPtr const &user_context) const override
Checks if the IPv4 lease limits set in the given user context are exceeded.
An abstract API for lease database.
virtual size_t wipeLeases6(const SubnetID &subnet_id)
Removed specified IPv6 leases.
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:482
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
Universe
Specifies universe (V4, V6)
virtual bool deleteLease(const Lease4Ptr &lease)
Deletes an IPv4 lease.
Common database connection class.
Memfile_LeaseMgr(const db::DatabaseConnection::ParameterMap &parameters)
The sole lease manager constructor.
boost::multi_index_container< Lease4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< AddressIndexTag >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HWAddressSubnetIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease, const std::vector< uint8_t > &, &Lease::getHWAddrVector >, boost::multi_index::member< Lease, SubnetID, &Lease::subnet_id_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ClientIdSubnetIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease4, const std::vector< uint8_t > &, &Lease4::getClientIdVector >, boost::multi_index::member< Lease, uint32_t, &Lease::subnet_id_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ExpirationIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease, bool, &Lease::stateExpiredReclaimed >, boost::multi_index::const_mem_fun< Lease, int64_t, &Lease::getExpirationTime > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetIdIndexTag >, boost::multi_index::member< Lease, isc::dhcp::SubnetID, &Lease::subnet_id_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HostnameIndexTag >, boost::multi_index::member< Lease, std::string, &Lease::hostname_ > > >> Lease4Storage
A multi index container holding DHCPv4 leases.
virtual Lease6Collection getLeases6() const
Returns all IPv6 leases.
LFCFileType
Types of the lease files used by the Lease File Cleanup.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const
Returns existing IPv6 lease for a given IPv6 address.
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query.
static std::string getDBVersion()
Class method to return extended version info This class method must be redeclared and redefined in de...
Definition: lease_mgr.cc:354
virtual void lfcCallback()
A callback function triggering Lease File Cleanup (LFC).
boost::multi_index_container< Lease6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< AddressIndexTag >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< DuidIaidTypeIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::const_mem_fun< Lease6, const std::vector< uint8_t > &, &Lease6::getDuidVector >, boost::multi_index::member< Lease6, uint32_t, &Lease6::iaid_ >, boost::multi_index::member< Lease6, Lease::Type, &Lease6::type_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ExpirationIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::const_mem_fun< Lease, bool, &Lease::stateExpiredReclaimed >, boost::multi_index::const_mem_fun< Lease, int64_t, &Lease::getExpirationTime > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetIdIndexTag >, boost::multi_index::member< Lease, isc::dhcp::SubnetID, &Lease::subnet_id_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< DuidIndexTag >, boost::multi_index::const_mem_fun< Lease6, const std::vector< uint8_t > &, &Lease6::getDuidVector > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HostnameIndexTag >, boost::multi_index::member< Lease, std::string, &Lease::hostname_ > > >> Lease6Storage
A multi index container holding DHCPv6 leases.
bool isLFCRunning() const
Checks if the process performing lease file cleanup is running.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:278
static const int MINOR_VERSION_V4
the minor version of the v4 memfile backend
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes all expired-reclaimed DHCPv4 leases.
virtual void commit()
Commit Transactions.
std::string checkLimits6(isc::data::ConstElementPtr const &user_context) const override
Checks if the IPv6 lease limits set in the given user context are exceeded.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv6 leases.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
virtual std::string getType() const
Return backend type.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:635
bool isJsonSupported() const override
Checks if JSON support is enabled in the database.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
virtual size_t wipeLeases4(const SubnetID &subnet_id)
Removes specified IPv4 leases.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query.
std::string getLeaseFilePath(Universe u) const
Returns an absolute path to the lease file.
Defines the logger used by the top-level component of kea-lfc.
bool persistLeases(Universe u) const
Specifies whether or not leases are written to disk.
virtual void updateLease4(const Lease4Ptr &lease4)
Updates IPv4 lease.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv4 leases.
std::string getDefaultLeaseFilePath(Universe u) const
Returns default path to the lease file.
Type
Type of lease or pool.
Definition: lease.h:45
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
static const int MAJOR_VERSION_V4
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs)
Deletes all expired-reclaimed DHCPv6 leases.
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:44
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
int getLFCExitStatus() const
Returns the status code returned by the last executed LFC process.
virtual void updateLease6(const Lease6Ptr &lease6)
Updates IPv6 lease.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:487
static const int MINOR_VERSION_V6
the minor version of the v6 memfile backend
static std::string appendSuffix(const std::string &file_name, const LFCFileType &file_type)
Appends appropriate suffix to the file name.
virtual Lease4Collection getLeases4() const
Returns all IPv4 leases.
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const
Returns existing IPv4 lease for specified IPv4 address.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:24
virtual bool addLease(const Lease4Ptr &lease)
Adds an IPv4 lease.