Kea  1.9.9-git
memfile_lease_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2021 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 
92  static const int MAJOR_VERSION = 2;
93 
95  static const int MINOR_VERSION = 1;
96 
98 
99 
106  enum Universe {
107  V4,
109  };
110 
114 
115 
137 
139  virtual ~Memfile_LeaseMgr();
140 
142  static std::string getDBVersion();
143 
149  virtual bool addLease(const Lease4Ptr& lease);
150 
156  virtual bool addLease(const Lease6Ptr& lease);
157 
166  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
167 
178  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
179 
193  virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
194  SubnetID subnet_id) const;
195 
201  virtual Lease4Collection getLease4(const ClientId& client_id) const;
202 
215  virtual Lease4Ptr getLease4(const ClientId& clientid,
216  SubnetID subnet_id) const;
217 
223  virtual Lease4Collection getLeases4(SubnetID subnet_id) const;
224 
230  virtual Lease4Collection getLeases4(const std::string& hostname) const;
231 
235  virtual Lease4Collection getLeases4() const;
236 
261  virtual Lease4Collection
262  getLeases4(const asiolink::IOAddress& lower_bound_address,
263  const LeasePageSize& page_size) const;
264 
274  virtual Lease6Ptr getLease6(Lease::Type type,
275  const isc::asiolink::IOAddress& addr) const;
276 
286  const DUID& duid,
287  uint32_t iaid) const;
288 
302  const DUID& duid,
303  uint32_t iaid,
304  SubnetID subnet_id) const;
305 
311  virtual Lease6Collection getLeases6(SubnetID subnet_id) const;
312 
318  virtual Lease6Collection getLeases6(const std::string& hostname) const;
319 
323  virtual Lease6Collection getLeases6() const;
324 
328  virtual Lease6Collection getLeases6(const DUID& duid) const;
329 
354  virtual Lease6Collection
355  getLeases6(const asiolink::IOAddress& lower_bound_address,
356  const LeasePageSize& page_size) const;
357 
368  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
369  const size_t max_leases) const;
370 
381  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
382  const size_t max_leases) const;
383 
401  virtual void updateLease4(const Lease4Ptr& lease4);
402 
420  virtual void updateLease6(const Lease6Ptr& lease6);
421 
436  virtual bool deleteLease(const Lease4Ptr& lease);
437 
452  virtual bool deleteLease(const Lease6Ptr& lease);
453 
461  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
462 
470  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
471 
480  virtual size_t wipeLeases4(const SubnetID& subnet_id);
481 
490  virtual size_t wipeLeases6(const SubnetID& subnet_id);
491 
492 private:
493 
497 
503  bool addLeaseInternal(const Lease4Ptr& lease);
504 
510  bool addLeaseInternal(const Lease6Ptr& lease);
511 
517  Lease4Ptr getLease4Internal(const isc::asiolink::IOAddress& addr) const;
518 
523  void getLease4Internal(const isc::dhcp::HWAddr& hwaddr,
524  Lease4Collection& collection) const;
525 
533  Lease4Ptr getLease4Internal(const HWAddr& hwaddr,
534  SubnetID subnet_id) const;
535 
540  void getLease4Internal(const ClientId& client_id,
541  Lease4Collection& collection) const;
542 
550  Lease4Ptr getLease4Internal(const ClientId& clientid,
551  const HWAddr& hwaddr,
552  SubnetID subnet_id) const;
553 
560  Lease4Ptr getLease4Internal(const ClientId& clientid,
561  SubnetID subnet_id) const;
562 
567  void getLeases4Internal(SubnetID subnet_id,
568  Lease4Collection& collection) const;
569 
574  void getLeases4Internal(const std::string& hostname,
575  Lease4Collection& collection) const;
576 
580  void getLeases4Internal(Lease4Collection& collection) const;
581 
588  void getLeases4Internal(const asiolink::IOAddress& lower_bound_address,
589  const LeasePageSize& page_size,
590  Lease4Collection& collection) const;
591 
598  Lease6Ptr getLease6Internal(Lease::Type type,
599  const isc::asiolink::IOAddress& addr) const;
600 
608  void getLeases6Internal(Lease::Type type,
609  const DUID& duid,
610  uint32_t iaid,
611  Lease6Collection& collection) const;
612 
621  void getLeases6Internal(Lease::Type type,
622  const DUID& duid,
623  uint32_t iaid,
624  SubnetID subnet_id,
625  Lease6Collection& collection) const;
626 
631  void getLeases6Internal(SubnetID subnet_id,
632  Lease6Collection& collection) const;
633 
638  void getLeases6Internal(const std::string& hostname,
639  Lease6Collection& collection) const;
640 
644  void getLeases6Internal(Lease6Collection& collection) const;
645 
650  void getLeases6Internal(const DUID& duid,
651  Lease6Collection& collection) const;
652 
659  void getLeases6Internal(const asiolink::IOAddress& lower_bound_address,
660  const LeasePageSize& page_size,
661  Lease6Collection& collection) const;
662 
669  void getExpiredLeases4Internal(Lease4Collection& expired_leases,
670  const size_t max_leases) const;
671 
678  void getExpiredLeases6Internal(Lease6Collection& expired_leases,
679  const size_t max_leases) const;
680 
695  void updateLease4Internal(const Lease4Ptr& lease4);
696 
711  void updateLease6Internal(const Lease6Ptr& lease6);
712 
727  bool deleteLeaseInternal(const Lease4Ptr& addr);
728 
743  bool deleteLeaseInternal(const Lease6Ptr& addr);
744 
750  size_t wipeLeases4Internal(const SubnetID& subnet_id);
751 
757  size_t wipeLeases6Internal(const SubnetID& subnet_id);
759 
786  template<typename IndexType, typename LeaseType, typename StorageType,
787  typename LeaseFileType>
788  uint64_t deleteExpiredReclaimedLeases(const uint32_t secs,
789  const Universe& universe,
790  StorageType& storage,
791  LeaseFileType& lease_file) const;
792 
793 public:
794 
800  virtual std::string getType() const {
801  return (std::string("memfile"));
802  }
803 
809  virtual std::string getName() const {
810  return ("memory");
811  }
812 
818  virtual std::string getDescription() const;
819 
824  virtual std::pair<uint32_t, uint32_t> getVersion() const {
825  return (std::make_pair(MAJOR_VERSION, MINOR_VERSION));
826  }
827 
832  virtual void commit();
833 
838  virtual void rollback();
839 
841 
843 
844 
849  enum LFCFileType {
856  };
857 
876  static std::string appendSuffix(const std::string& file_name,
877  const LFCFileType& file_type);
879 
880 
884 
885 
889  std::string getDefaultLeaseFilePath(Universe u) const;
890 
897  std::string getLeaseFilePath(Universe u) const;
898 
910  bool persistLeases(Universe u) const;
911 
913 
914 private:
915 
916 
932  std::string initLeaseFilePath(Universe u);
933 
989  template<typename LeaseObjectType, typename LeaseFileType,
990  typename StorageType>
991  bool loadLeasesFromFiles(const std::string& filename,
992  boost::shared_ptr<LeaseFileType>& lease_file,
993  StorageType& storage);
994 
996  Lease4Storage storage4_;
997 
999  Lease6Storage storage6_;
1000 
1002  boost::shared_ptr<CSVLeaseFile4> lease_file4_;
1003 
1005  boost::shared_ptr<CSVLeaseFile6> lease_file6_;
1006 
1007 public:
1008 
1013 
1014 
1018  bool isLFCRunning() const;
1019 
1022  int getLFCExitStatus() const;
1024 
1033 
1042  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
1043 
1053  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
1054  const SubnetID& last_subnet_id);
1055 
1064 
1073  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
1074 
1084  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
1085  const SubnetID& last_subnet_id);
1086 
1090 
1091 
1092 protected:
1093 
1105  virtual void lfcCallback();
1107 
1109 
1110 
1111 private:
1112 
1129  void lfcSetup(bool conversion_needed = false);
1130 
1153  template<typename LeaseFileType>
1154  void lfcExecute(boost::shared_ptr<LeaseFileType>& lease_file);
1155 
1157  boost::scoped_ptr<LFCSetup> lfc_setup_;
1158 
1163  db::DatabaseConnection conn_;
1164 
1166 
1168  boost::scoped_ptr<std::mutex> mutex_;
1169 };
1170 
1171 } // namespace dhcp
1172 } // namespace isc
1173 
1174 #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 ~Memfile_LeaseMgr()
Destructor (closes file)
virtual std::string getDescription() const
Returns description of the backend.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:207
Concrete implementation of a lease database backend using flat file.
Abstract Lease Manager.
Definition: lease_mgr.h:222
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:487
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 void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv6 leases.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv4 leases.
virtual bool deleteLease(const Lease4Ptr &lease)
Deletes an IPv4 lease.
Common database connection class.
std::string getDefaultLeaseFilePath(Universe u) const
Returns default path to the lease file.
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.
static std::string getDBVersion()
Local version of getDBVersion() class method.
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 LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query.
virtual void lfcCallback()
A callback function triggering Lease File Cleanup (LFC).
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const
Returns existing IPv6 lease for a given IPv6 address.
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.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:283
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes all expired-reclaimed DHCPv4 leases.
virtual void commit()
Commit Transactions.
int getLFCExitStatus() const
Returns the status code returned by the last executed LFC process.
static const int MINOR_VERSION
Defines minor version of the memfile backend.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:640
bool isLFCRunning() const
Checks if the process performing lease file cleanup is running.
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 Lease6Collection getLeases6() const
Returns all IPv6 leases.
std::string getLeaseFilePath(Universe u) const
Returns an absolute path to the lease file.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual Lease4Collection getLeases4() const
Returns all IPv4 leases.
virtual void updateLease4(const Lease4Ptr &lease4)
Updates IPv4 lease.
Type
Type of lease or pool.
Definition: lease.h:50
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
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
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:43
virtual std::string getType() const
Return backend type.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
virtual void updateLease6(const Lease6Ptr &lease6)
Updates IPv6 lease.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:492
static std::string appendSuffix(const std::string &file_name, const LFCFileType &file_type)
Appends appropriate suffix to the file name.
bool persistLeases(Universe u) const
Specifies whether or not leases are written to disk.
virtual std::string getName() const
Returns backend name.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
virtual bool addLease(const Lease4Ptr &lease)
Adds an IPv4 lease.
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const
Returns existing IPv4 lease for specified IPv4 address.