Kea  1.9.9-git
mysql_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 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(db::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 
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 
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 
646  virtual std::string getType() const {
647  return (std::string("mysql"));
648  }
649 
655  virtual std::string getName() const;
656 
662  virtual std::string getDescription() const;
663 
671  virtual std::pair<uint32_t, uint32_t> getVersion() const;
672 
679  virtual void commit();
680 
687  virtual void rollback();
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  NUM_STATEMENTS // Number of statements
728  };
729 
730 private:
731 
748  bool addLeaseCommon(MySqlLeaseContextPtr& ctx,
749  StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
750 
772  template <typename Exchange, typename LeaseCollection>
773  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
774  StatementIndex stindex,
775  MYSQL_BIND* bind,
776  Exchange& exchange, LeaseCollection& result,
777  bool single = false) const;
778 
796  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
797  StatementIndex stindex,
798  MYSQL_BIND* bind,
799  Lease4Collection& result) const {
800  getLeaseCollection(ctx, stindex, bind, ctx->exchange4_, result);
801  }
802 
819  void getLeaseCollection(MySqlLeaseContextPtr& ctx,
820  StatementIndex stindex,
821  MYSQL_BIND* bind,
822  Lease6Collection& result) const {
823  getLeaseCollection(ctx, stindex, bind, ctx->exchange6_, result);
824  }
825 
836  void getLease(MySqlLeaseContextPtr& ctx,
837  StatementIndex stindex,
838  MYSQL_BIND* bind,
839  Lease4Ptr& result) const;
840 
851  void getLease(MySqlLeaseContextPtr& ctx,
852  StatementIndex stindex,
853  MYSQL_BIND* bind,
854  Lease6Ptr& result) const;
855 
870  template<typename LeaseCollection>
871  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
872  const size_t max_leases,
873  StatementIndex statement_index) const;
874 
892  template <typename LeasePtr>
893  void updateLeaseCommon(MySqlLeaseContextPtr& ctx,
894  StatementIndex stindex,
895  MYSQL_BIND* bind,
896  const LeasePtr& lease);
897 
913  uint64_t deleteLeaseCommon(StatementIndex stindex,
914  MYSQL_BIND* bind);
915 
925  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
926  StatementIndex statement_index);
927 
939  void checkError(MySqlLeaseContextPtr& ctx,
940  int status, StatementIndex index,
941  const char* what) const;
942 
944  class MySqlLeaseContextAlloc {
945  public:
946 
953  MySqlLeaseContextAlloc(const MySqlLeaseMgr& mgr);
954 
958  ~MySqlLeaseContextAlloc();
959 
961  MySqlLeaseContextPtr ctx_;
962 
963  private:
964 
966  const MySqlLeaseMgr& mgr_;
967  };
968 
969 private:
970 
971  // Members
972 
975 
977  MySqlLeaseContextPoolPtr pool_;
978 
980  std::string timer_name_;
981 };
982 
983 } // namespace dhcp
984 } // namespace isc
985 
986 #endif // MYSQL_LEASE_MGR_H
static bool dbReconnect(db::ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the lease DB backend manager.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:207
std::vector< MySqlLeaseContextPtr > pool_
The vector of available contexts.
static std::string getDBVersion()
Local version of getDBVersion() class method.
Abstract Lease Manager.
Definition: lease_mgr.h:222
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs)
Deletes all expired-reclaimed DHCPv6 leases.
virtual std::string getName() const
Returns backend name.
MySQL Lease Context Pool.
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 void updateLease6(const Lease6Ptr &lease6)
Updates IPv6 lease.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query.
virtual size_t wipeLeases4(const SubnetID &subnet_id)
Removes specified IPv4 leases.
virtual std::string getDescription() const
Returns description of the backend.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const
Returns existing IPv6 lease for a given IPv6 address.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes all expired-reclaimed DHCPv4 leases.
virtual Lease4Collection getLeases4() const
Returns all IPv4 leases.
virtual void rollback()
Rollback Transactions.
virtual bool addLease(const Lease4Ptr &lease)
Adds an IPv4 lease.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
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:283
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
virtual ~MySqlLeaseMgr()
Destructor (closes database)
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:640
virtual void updateLease4(const Lease4Ptr &lease4)
Updates IPv4 lease.
MySQL Lease Manager.
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.
virtual bool deleteLease(const Lease4Ptr &lease)
Deletes an IPv4 lease.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
virtual std::string getType() const
Return backend type.
boost::scoped_ptr< MySqlLease6Exchange > exchange6_
Exchange object.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv4 leases.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
db::MySqlConnection conn_
MySQL connection.
virtual Lease6Collection getLeases6() const
Returns all IPv6 leases.
Type
Type of lease or pool.
Definition: lease.h:50
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
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 Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const
Returns an IPv4 lease for specified IPv4 address.
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
MySqlLeaseContextPtr createContext() const
Create a new context.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv6 leases.
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query.
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:43
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< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:492
virtual void commit()
Commit Transactions.
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.
MySqlLeaseMgr(const db::DatabaseConnection::ParameterMap &parameters)
Constructor.
std::mutex mutex_
The mutex to protect pool access.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
Common MySQL Connector Pool.