Kea  2.1.7-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);
166 
176  virtual bool addLease(const Lease6Ptr& lease);
177 
196  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
197 
214  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
215 
232  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
233  SubnetID subnet_id) const;
234 
251  virtual Lease4Collection getLease4(const ClientId& clientid) const;
252 
268  virtual Lease4Ptr getLease4(const ClientId& clientid,
269  SubnetID subnet_id) const;
270 
276  virtual Lease4Collection getLeases4(SubnetID subnet_id) const;
277 
283  virtual Lease4Collection getLeases4(const std::string& hostname) const;
284 
288  virtual Lease4Collection getLeases4() const;
289 
314  virtual Lease4Collection
315  getLeases4(const asiolink::IOAddress& lower_bound_address,
316  const LeasePageSize& page_size) const;
317 
336  virtual Lease6Ptr getLease6(Lease::Type type,
337  const isc::asiolink::IOAddress& addr) const;
338 
359  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
360  uint32_t iaid) const;
361 
378  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
379  uint32_t iaid, SubnetID subnet_id) const;
380 
386  virtual Lease6Collection getLeases6(SubnetID subnet_id) const;
387 
393  virtual Lease6Collection getLeases6(const std::string& hostname) const;
394 
398  virtual Lease6Collection getLeases6() const;
399 
405  virtual Lease6Collection getLeases6(const DUID& duid) const;
406 
431  virtual Lease6Collection
432  getLeases6(const asiolink::IOAddress& lower_bound_address,
433  const LeasePageSize& page_size) const;
434 
445  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
446  const size_t max_leases) const;
447 
458  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
459  const size_t max_leases) const;
460 
481  virtual void updateLease4(const Lease4Ptr& lease4);
482 
503  virtual void updateLease6(const Lease6Ptr& lease6);
504 
519  virtual bool deleteLease(const Lease4Ptr& lease);
520 
535  virtual bool deleteLease(const Lease6Ptr& lease);
536 
544  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
545 
553  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
554 
563  virtual LeaseStatsQueryPtr startLeaseStatsQuery4();
564 
573  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
574 
584  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
585  const SubnetID& last_subnet_id);
586 
595  virtual LeaseStatsQueryPtr startLeaseStatsQuery6();
596 
605  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
606 
616  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
617  const SubnetID& last_subnet_id);
618 
628  virtual size_t wipeLeases4(const SubnetID& subnet_id);
629 
639  virtual size_t wipeLeases6(const SubnetID& subnet_id);
640 
645  bool isJsonSupported() const override;
646 
652  virtual std::string getType() const {
653  return (std::string("mysql"));
654  }
655 
661  virtual std::string getName() const;
662 
668  virtual std::string getDescription() const;
669 
677  virtual std::pair<uint32_t, uint32_t> getVersion() const;
678 
685  virtual void commit();
686 
693  virtual void rollback();
694 
700  DELETE_LEASE4, // Delete from lease4 by address
701  DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
702  DELETE_LEASE6, // Delete from lease6 by address
703  DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
704  GET_LEASE4, // Get all IPv4 leases
705  GET_LEASE4_ADDR, // Get lease4 by address
706  GET_LEASE4_CLIENTID, // Get lease4 by client ID
707  GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
708  GET_LEASE4_HWADDR, // Get lease4 by HW address
709  GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
710  GET_LEASE4_PAGE, // Get page of leases beginning with an address
711  GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID
712  GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname
713  GET_LEASE4_EXPIRE, // Get lease4 by expiration.
714  GET_LEASE6, // Get all IPv6 leases
715  GET_LEASE6_ADDR, // Get lease6 by address
716  GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
717  GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
718  GET_LEASE6_PAGE, // Get page of leases beginning with an address
719  GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID
720  GET_LEASE6_DUID, // Get IPv6 leases by DUID
721  GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname
722  GET_LEASE6_EXPIRE, // Get lease6 by expiration.
723  INSERT_LEASE4, // Add entry to lease4 table
724  INSERT_LEASE6, // Add entry to lease6 table
725  UPDATE_LEASE4, // Update a Lease4 entry
726  UPDATE_LEASE6, // Update a Lease6 entry
727  ALL_LEASE4_STATS, // Fetches IPv4 lease statistics
728  SUBNET_LEASE4_STATS, // Fetched IPv4 lease stats for a single subnet.
729  SUBNET_RANGE_LEASE4_STATS, // Fetched IPv4 lease stats for a subnet range.
730  ALL_LEASE6_STATS, // Fetches IPv6 lease statistics
731  SUBNET_LEASE6_STATS, // Fetched IPv6 lease stats for a single subnet.
732  SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range.
733  CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits.
734  CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits.
735  IS_JSON_SUPPORTED, // Checks if JSON support is enabled in the database.
736  NUM_STATEMENTS // Number of statements
737  };
738 
739 private:
740 
757  bool addLeaseCommon(MySqlLeaseContextPtr& ctx,
758  StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
759 
781  template <typename Exchange, typename LeaseCollection>
782  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
783  StatementIndex stindex,
784  MYSQL_BIND* bind,
785  Exchange& exchange, LeaseCollection& result,
786  bool single = false) const;
787 
805  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
806  StatementIndex stindex,
807  MYSQL_BIND* bind,
808  Lease4Collection& result) const {
809  getLeaseCollection(ctx, stindex, bind, ctx->exchange4_, result);
810  }
811 
828  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
829  StatementIndex stindex,
830  MYSQL_BIND* bind,
831  Lease6Collection& result) const {
832  getLeaseCollection(ctx, stindex, bind, ctx->exchange6_, result);
833  }
834 
845  void getLease(MySqlLeaseContextPtr& ctx,
846  StatementIndex stindex,
847  MYSQL_BIND* bind,
848  Lease4Ptr& result) const;
849 
860  void getLease(MySqlLeaseContextPtr& ctx,
861  StatementIndex stindex,
862  MYSQL_BIND* bind,
863  Lease6Ptr& result) const;
864 
879  template<typename LeaseCollection>
880  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
881  const size_t max_leases,
882  StatementIndex statement_index) const;
883 
901  template <typename LeasePtr>
902  void updateLeaseCommon(MySqlLeaseContextPtr& ctx,
903  StatementIndex stindex,
904  MYSQL_BIND* bind,
905  const LeasePtr& lease);
906 
922  uint64_t deleteLeaseCommon(StatementIndex stindex,
923  MYSQL_BIND* bind);
924 
934  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
935  StatementIndex statement_index);
936 
948  std::string
949  checkLimits(isc::data::ConstElementPtr const& user_context, StatementIndex const stindex) const;
950 
962  std::string checkLimits4(isc::data::ConstElementPtr const& user_context) const override;
963 
975  std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
976 
988  void checkError(MySqlLeaseContextPtr& ctx,
989  int status, StatementIndex index,
990  const char* what) const;
991 
993  class MySqlLeaseContextAlloc {
994  public:
995 
1002  MySqlLeaseContextAlloc(const MySqlLeaseMgr& mgr);
1003 
1007  ~MySqlLeaseContextAlloc();
1008 
1010  MySqlLeaseContextPtr ctx_;
1011 
1012  private:
1013 
1015  const MySqlLeaseMgr& mgr_;
1016  };
1017 
1018 private:
1019 
1020  // Members
1021 
1024 
1026  MySqlLeaseContextPoolPtr pool_;
1027 
1029  std::string timer_name_;
1030 };
1031 
1032 } // namespace dhcp
1033 } // namespace isc
1034 
1035 #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:208
std::vector< MySqlLeaseContextPtr > pool_
The vector of available contexts.
Abstract Lease Manager.
Definition: lease_mgr.h:223
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:482
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:21
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
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:278
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:635
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:45
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:44
virtual std::string getType() const
Return backend type.
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:487
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:24
Common MySQL Connector Pool.