Kea  2.3.5-git
mysql_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 MYSQL_LEASE_MGR_H
8 #define MYSQL_LEASE_MGR_H
9 
10 #include <asiolink/io_service.h>
11 #include <dhcp/hwaddr.h>
13 #include <dhcpsrv/lease_mgr.h>
14 #include <mysql/mysql_connection.h>
15 
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/utility.hpp>
18 #include <mysql.h>
19 
20 #include <time.h>
21 #include <vector>
22 #include <mutex>
23 
24 namespace isc {
25 namespace dhcp {
26 
27 // Forward declaration of the Lease exchange objects. These classes are defined
28 // in the .cc file.
29 class MySqlLease4Exchange;
30 class MySqlLease6Exchange;
31 
42 public:
43 
50  db::IOServiceAccessorPtr io_service_accessor,
51  db::DbCallback db_reconnect_callback);
52 
57  boost::scoped_ptr<MySqlLease4Exchange> exchange4_;
58  boost::scoped_ptr<MySqlLease6Exchange> exchange6_;
59 
62 };
63 
65 typedef boost::shared_ptr<MySqlLeaseContext> MySqlLeaseContextPtr;
66 
74 public:
75 
77  std::vector<MySqlLeaseContextPtr> pool_;
78 
80  std::mutex mutex_;
81 };
82 
84 typedef boost::shared_ptr<MySqlLeaseContextPool> MySqlLeaseContextPoolPtr;
85 
91 
92 class MySqlLeaseMgr : public LeaseMgr {
93 public:
94 
115 
117  virtual ~MySqlLeaseMgr();
118 
127  MySqlLeaseContextPtr createContext() const;
128 
151  static bool dbReconnect(util::ReconnectCtlPtr db_reconnect_ctl);
152 
154  static std::string getDBVersion();
155 
165  virtual bool addLease(const Lease4Ptr& lease) override;
166 
176  virtual bool addLease(const Lease6Ptr& lease) override;
177 
196  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const override;
197 
214  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const override;
215 
232  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
233  SubnetID subnet_id) const override;
234 
251  virtual Lease4Collection getLease4(const ClientId& clientid) const override;
252 
268  virtual Lease4Ptr getLease4(const ClientId& clientid,
269  SubnetID subnet_id) const override;
270 
276  virtual Lease4Collection getLeases4(SubnetID subnet_id) const override;
277 
283  virtual Lease4Collection getLeases4(const std::string& hostname) const override;
284 
288  virtual Lease4Collection getLeases4() const override;
289 
314  virtual Lease4Collection
315  getLeases4(const asiolink::IOAddress& lower_bound_address,
316  const LeasePageSize& page_size) const override;
317 
336  virtual Lease6Ptr getLease6(Lease::Type type,
337  const isc::asiolink::IOAddress& addr) const override;
338 
359  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
360  uint32_t iaid) const override;
361 
378  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
379  uint32_t iaid, SubnetID subnet_id) const override;
380 
386  virtual Lease6Collection getLeases6(SubnetID subnet_id) const override;
387 
393  virtual Lease6Collection getLeases6(const std::string& hostname) const override;
394 
398  virtual Lease6Collection getLeases6() const override;
399 
405  virtual Lease6Collection getLeases6(const DUID& duid) const override;
406 
431  virtual Lease6Collection
432  getLeases6(const asiolink::IOAddress& lower_bound_address,
433  const LeasePageSize& page_size) const override;
434 
445  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
446  const size_t max_leases) const override;
447 
458  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
459  const size_t max_leases) const override;
460 
481  virtual void updateLease4(const Lease4Ptr& lease4) override;
482 
503  virtual void updateLease6(const Lease6Ptr& lease6) override;
504 
519  virtual bool deleteLease(const Lease4Ptr& lease) override;
520 
535  virtual bool deleteLease(const Lease6Ptr& lease) override;
536 
544  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) override;
545 
553  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) override;
554 
563  virtual LeaseStatsQueryPtr startLeaseStatsQuery4() override;
564 
573  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id) override;
574 
584  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
585  const SubnetID& last_subnet_id) override;
586 
595  virtual LeaseStatsQueryPtr startLeaseStatsQuery6() override;
596 
605  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id) override;
606 
616  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
617  const SubnetID& last_subnet_id) override;
618 
628  virtual size_t wipeLeases4(const SubnetID& subnet_id) override;
629 
639  virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
640 
646  virtual std::string getType() const override {
647  return (std::string("mysql"));
648  }
649 
655  virtual std::string getName() const override;
656 
662  virtual std::string getDescription() const override;
663 
671  virtual std::pair<uint32_t, uint32_t> getVersion() const override;
672 
679  virtual void commit() override;
680 
687  virtual void rollback() override;
688 
694  DELETE_LEASE4, // Delete from lease4 by address
695  DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
696  DELETE_LEASE6, // Delete from lease6 by address
697  DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
698  GET_LEASE4, // Get all IPv4 leases
699  GET_LEASE4_ADDR, // Get lease4 by address
700  GET_LEASE4_CLIENTID, // Get lease4 by client ID
701  GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
702  GET_LEASE4_HWADDR, // Get lease4 by HW address
703  GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
704  GET_LEASE4_PAGE, // Get page of leases beginning with an address
705  GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID
706  GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname
707  GET_LEASE4_EXPIRE, // Get lease4 by expiration.
708  GET_LEASE6, // Get all IPv6 leases
709  GET_LEASE6_ADDR, // Get lease6 by address
710  GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
711  GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
712  GET_LEASE6_PAGE, // Get page of leases beginning with an address
713  GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID
714  GET_LEASE6_DUID, // Get IPv6 leases by DUID
715  GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname
716  GET_LEASE6_EXPIRE, // Get lease6 by expiration.
717  INSERT_LEASE4, // Add entry to lease4 table
718  INSERT_LEASE6, // Add entry to lease6 table
719  UPDATE_LEASE4, // Update a Lease4 entry
720  UPDATE_LEASE6, // Update a Lease6 entry
721  ALL_LEASE4_STATS, // Fetches IPv4 lease statistics
722  SUBNET_LEASE4_STATS, // Fetched IPv4 lease stats for a single subnet.
723  SUBNET_RANGE_LEASE4_STATS, // Fetched IPv4 lease stats for a subnet range.
724  ALL_LEASE6_STATS, // Fetches IPv6 lease statistics
725  SUBNET_LEASE6_STATS, // Fetched IPv6 lease stats for a single subnet.
726  SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range.
727  CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits.
728  CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits.
729  IS_JSON_SUPPORTED, // Checks if JSON support is enabled in the database.
730  GET_LEASE4_COUNT_BY_CLASS, // Fetches the IPv4 lease count for a given class.
731  GET_LEASE6_COUNT_BY_CLASS, // Fetches the IPv6 lease count for given class and lease type.
732  NUM_STATEMENTS // Number of statements
733  };
734 
735 private:
736 
753  bool addLeaseCommon(MySqlLeaseContextPtr& ctx,
754  StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
755 
777  template <typename Exchange, typename LeaseCollection>
778  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
779  StatementIndex stindex,
780  MYSQL_BIND* bind,
781  Exchange& exchange, LeaseCollection& result,
782  bool single = false) const;
783 
801  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
802  StatementIndex stindex,
803  MYSQL_BIND* bind,
804  Lease4Collection& result) const {
805  getLeaseCollection(ctx, stindex, bind, ctx->exchange4_, result);
806  }
807 
824  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
825  StatementIndex stindex,
826  MYSQL_BIND* bind,
827  Lease6Collection& result) const {
828  getLeaseCollection(ctx, stindex, bind, ctx->exchange6_, result);
829  }
830 
841  void getLease(MySqlLeaseContextPtr& ctx,
842  StatementIndex stindex,
843  MYSQL_BIND* bind,
844  Lease4Ptr& result) const;
845 
856  void getLease(MySqlLeaseContextPtr& ctx,
857  StatementIndex stindex,
858  MYSQL_BIND* bind,
859  Lease6Ptr& result) const;
860 
875  template<typename LeaseCollection>
876  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
877  const size_t max_leases,
878  StatementIndex statement_index) const;
879 
897  template <typename LeasePtr>
898  void updateLeaseCommon(MySqlLeaseContextPtr& ctx,
899  StatementIndex stindex,
900  MYSQL_BIND* bind,
901  const LeasePtr& lease);
902 
918  uint64_t deleteLeaseCommon(StatementIndex stindex,
919  MYSQL_BIND* bind);
920 
930  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
931  StatementIndex statement_index);
932 
944  std::string
945  checkLimits(isc::data::ConstElementPtr const& user_context, StatementIndex const stindex) const;
946 
958  virtual std::string
959  checkLimits4(isc::data::ConstElementPtr const& user_context) const override;
960 
972  virtual std::string
973  checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
974 
979  virtual bool isJsonSupported() const override;
980 
988  virtual size_t getClassLeaseCount(const ClientClass& client_class,
989  const Lease::Type& ltype = Lease::TYPE_V4) const override;
990 
992  virtual void recountClassLeases4() override;
993 
995  virtual void recountClassLeases6() override;
996 
998  virtual void clearClassLeaseCounts() override;
999 
1001  virtual void writeLeases4(const std::string& /*filename*/) override;
1002 
1004  virtual void writeLeases6(const std::string& /*filename*/) override;
1005 
1017  void checkError(MySqlLeaseContextPtr& ctx,
1018  int status, StatementIndex index,
1019  const char* what) const;
1020 
1024 
1037  virtual Lease4Collection
1038  getLeases4ByRelayId(const OptionBuffer& relay_id,
1039  const asiolink::IOAddress& lower_bound_address,
1040  const LeasePageSize& page_size,
1041  const time_t& qry_start_time = 0,
1042  const time_t& qry_end_time = 0) override;
1043 
1056  virtual Lease4Collection
1057  getLeases4ByRemoteId(const OptionBuffer& remote_id,
1058  const asiolink::IOAddress& lower_bound_address,
1059  const LeasePageSize& page_size,
1060  const time_t& qry_start_time = 0,
1061  const time_t& qry_end_time = 0) override;
1062 
1073  virtual Lease6Collection
1074  getLeases6ByRelayId(const DUID& relay_id,
1075  const asiolink::IOAddress& link_addr,
1076  uint8_t link_len,
1077  const asiolink::IOAddress& lower_bound_address,
1078  const LeasePageSize& page_size) override;
1079 
1090  virtual Lease6Collection
1091  getLeases6ByRemoteId(const OptionBuffer& remote_id,
1092  const asiolink::IOAddress& link_addr,
1093  uint8_t link_len,
1094  const asiolink::IOAddress& lower_bound_address,
1095  const LeasePageSize& page_size) override;
1096 
1106  virtual Lease6Collection
1107  getLeases6ByLink(const asiolink::IOAddress& link_addr,
1108  uint8_t link_len,
1109  const asiolink::IOAddress& lower_bound_address,
1110  const LeasePageSize& page_size) override;
1111 
1118  virtual size_t buildExtendedInfoTables6(bool update, bool current) override;
1119 
1121  class MySqlLeaseContextAlloc {
1122  public:
1123 
1130  MySqlLeaseContextAlloc(const MySqlLeaseMgr& mgr);
1131 
1135  ~MySqlLeaseContextAlloc();
1136 
1138  MySqlLeaseContextPtr ctx_;
1139 
1140  private:
1141 
1143  const MySqlLeaseMgr& mgr_;
1144  };
1145 
1146 protected:
1147 
1149 
1155  virtual void setExtendedInfoTablesEnabled(const bool enabled) override {
1156  if (enabled) {
1158  "extended info tables are not yet supported by mysql");
1159  }
1160  }
1161 
1168  virtual void setExtendedInfoTablesEnabled(const db::DatabaseConnection::ParameterMap& parameters) override;
1169 
1173  virtual void deleteExtendedInfo6(const isc::asiolink::IOAddress& addr) override;
1174 
1179  virtual void addRelayId6(const isc::asiolink::IOAddress& lease_addr,
1180  const std::vector<uint8_t>& relay_id) override;
1181 
1186  virtual void addRemoteId6(const isc::asiolink::IOAddress& lease_addr,
1187  const std::vector<uint8_t>& remote_id) override;
1188 
1189 private:
1190 
1191  // Members
1192 
1195 
1197  MySqlLeaseContextPoolPtr pool_;
1198 
1200  std::string timer_name_;
1201 };
1202 
1203 } // namespace dhcp
1204 } // namespace isc
1205 
1206 #endif // MYSQL_LEASE_MGR_H
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:210
A generic exception that is thrown when a function is not implemented.
std::vector< MySqlLeaseContextPtr > pool_
The vector of available contexts.
Abstract Lease Manager.
Definition: lease_mgr.h:225
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
MySQL Lease Context Pool.
An abstract API for lease database.
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:514
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:22
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
MySQL Lease Context.
StatementIndex
Statement Tags.
boost::shared_ptr< MySqlLeaseContext > MySqlLeaseContextPtr
Type of pointers to contexts.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:294
virtual void setExtendedInfoTablesEnabled(const bool enabled) override
Extended information / Bulk Lease Query shared interface.
IPv4 lease.
Definition: lease.h:50
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
virtual std::string getType() const override
Return backend type.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:677
MySQL Lease Manager.
boost::scoped_ptr< MySqlLease6Exchange > exchange6_
Exchange object.
Defines the logger used by the top-level component of kea-lfc.
db::MySqlConnection conn_
MySQL connection.
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
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:46
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
boost::shared_ptr< MySqlLeaseContextPool > MySqlLeaseContextPoolPtr
Type of pointers to context pools.
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
boost::scoped_ptr< MySqlLease4Exchange > exchange4_
The exchange objects are used for transfer of data to/from the database.
MySqlLeaseContext(const db::DatabaseConnection::ParameterMap &parameters, db::IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
std::mutex mutex_
The mutex to protect pool access.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:25
Common MySQL Connector Pool.