Kea  1.9.9-git
pgsql_lease_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2013-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 PGSQL_LEASE_MGR_H
8 #define PGSQL_LEASE_MGR_H
9 
10 #include <asiolink/io_service.h>
11 #include <dhcp/hwaddr.h>
13 #include <dhcpsrv/lease_mgr.h>
14 #include <pgsql/pgsql_connection.h>
15 #include <pgsql/pgsql_exchange.h>
16 
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/utility.hpp>
19 
20 #include <vector>
21 #include <mutex>
22 
23 namespace isc {
24 namespace dhcp {
25 
26 // Forward declaration of the Lease exchange objects. These classes are defined
27 // in the .cc file.
28 class PgSqlLease4Exchange;
29 class PgSqlLease6Exchange;
30 
41 public:
42 
49  db::IOServiceAccessorPtr io_service_accessor,
50  db::DbCallback db_reconnect_callback);
51 
56  boost::scoped_ptr<PgSqlLease4Exchange> exchange4_;
57  boost::scoped_ptr<PgSqlLease6Exchange> exchange6_;
58 
61 };
62 
64 typedef boost::shared_ptr<PgSqlLeaseContext> PgSqlLeaseContextPtr;
65 
73 public:
74 
76  std::vector<PgSqlLeaseContextPtr> pool_;
77 
79  std::mutex mutex_;
80 };
81 
83 typedef boost::shared_ptr<PgSqlLeaseContextPool> PgSqlLeaseContextPoolPtr;
84 
90 
91 class PgSqlLeaseMgr : public LeaseMgr {
92 public:
93 
114 
116  virtual ~PgSqlLeaseMgr();
117 
126  PgSqlLeaseContextPtr createContext() const;
127 
150  static bool dbReconnect(db::ReconnectCtlPtr db_reconnect_ctl);
151 
153  static std::string getDBVersion();
154 
164  virtual bool addLease(const Lease4Ptr& lease);
165 
175  virtual bool addLease(const Lease6Ptr& lease);
176 
192  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
193 
207  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
208 
222  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
223  SubnetID subnet_id) const;
224 
238  virtual Lease4Collection getLease4(const ClientId& clientid) const;
239 
252  virtual Lease4Ptr getLease4(const ClientId& clientid,
253  SubnetID subnet_id) const;
254 
260  virtual Lease4Collection getLeases4(SubnetID subnet_id) const;
261 
267  virtual Lease4Collection getLeases4(const std::string& hostname) const;
268 
272  virtual Lease4Collection getLeases4() const;
273 
298  virtual Lease4Collection
299  getLeases4(const asiolink::IOAddress& lower_bound_address,
300  const LeasePageSize& page_size) const;
301 
317  virtual Lease6Ptr getLease6(Lease::Type type,
318  const isc::asiolink::IOAddress& addr) const;
319 
337  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
338  uint32_t iaid) const;
339 
353  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
354  uint32_t iaid, SubnetID subnet_id) const;
355 
361  virtual Lease6Collection getLeases6(SubnetID subnet_id) const;
362 
368  virtual Lease6Collection getLeases6(const std::string& hostname) const;
369 
373  virtual Lease6Collection getLeases6() const;
374 
380  virtual Lease6Collection getLeases6(const DUID& duid) const;
381 
406  virtual Lease6Collection
407  getLeases6(const asiolink::IOAddress& lower_bound_address,
408  const LeasePageSize& page_size) const;
409 
420  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
421  const size_t max_leases) const;
422 
433  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
434  const size_t max_leases) const;
435 
456  virtual void updateLease4(const Lease4Ptr& lease4);
457 
478  virtual void updateLease6(const Lease6Ptr& lease6);
479 
494  virtual bool deleteLease(const Lease4Ptr& lease);
495 
510  virtual bool deleteLease(const Lease6Ptr& lease);
511 
519  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
520 
528  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
529 
539 
548  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
549 
559  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
560  const SubnetID& last_subnet_id);
561 
571 
580  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
581 
591  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
592  const SubnetID& last_subnet_id);
593 
603  virtual size_t wipeLeases4(const SubnetID& subnet_id);
604 
614  virtual size_t wipeLeases6(const SubnetID& subnet_id);
615 
621  virtual std::string getType() const {
622  return (std::string("postgresql"));
623  }
624 
630  virtual std::string getName() const;
631 
637  virtual std::string getDescription() const;
638 
646  virtual std::pair<uint32_t, uint32_t> getVersion() const;
647 
654  virtual void commit();
655 
662  virtual void rollback();
663 
669  DELETE_LEASE4, // Delete from lease4 by address
670  DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
671  DELETE_LEASE6, // Delete from lease6 by address
672  DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
673  GET_LEASE4, // Get all IPv4 leases
674  GET_LEASE4_ADDR, // Get lease4 by address
675  GET_LEASE4_CLIENTID, // Get lease4 by client ID
676  GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
677  GET_LEASE4_HWADDR, // Get lease4 by HW address
678  GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
679  GET_LEASE4_PAGE, // Get page of leases beginning with an address
680  GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID
681  GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname
682  GET_LEASE4_EXPIRE, // Get lease4 by expiration.
683  GET_LEASE6, // Get all IPv6 leases
684  GET_LEASE6_ADDR, // Get lease6 by address
685  GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
686  GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
687  GET_LEASE6_PAGE, // Get page of leases beginning with an address
688  GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID
689  GET_LEASE6_DUID, // Get IPv6 leases by DUID
690  GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname
691  GET_LEASE6_EXPIRE, // Get lease6 by expiration.
692  INSERT_LEASE4, // Add entry to lease4 table
693  INSERT_LEASE6, // Add entry to lease6 table
694  UPDATE_LEASE4, // Update a Lease4 entry
695  UPDATE_LEASE6, // Update a Lease6 entry
696  ALL_LEASE4_STATS, // Fetches IPv4 lease statistics
697  SUBNET_LEASE4_STATS, // Fetched IPv4 lease stats for a single subnet.
698  SUBNET_RANGE_LEASE4_STATS, // Fetched IPv4 lease stats for a subnet range.
699  ALL_LEASE6_STATS, // Fetches IPv6 lease statistics
700  SUBNET_LEASE6_STATS, // Fetched IPv6 lease stats for a single subnet.
701  SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range.
702  NUM_STATEMENTS // Number of statements
703  };
704 
705 private:
706 
723  bool addLeaseCommon(PgSqlLeaseContextPtr& ctx,
724  StatementIndex stindex,
725  db::PsqlBindArray& bind_array);
726 
748  template <typename Exchange, typename LeaseCollection>
749  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
750  StatementIndex stindex,
751  db::PsqlBindArray& bind_array,
752  Exchange& exchange, LeaseCollection& result,
753  bool single = false) const;
754 
772  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
773  StatementIndex stindex,
774  db::PsqlBindArray& bind_array,
775  Lease4Collection& result) const {
776  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange4_, result);
777  }
778 
795  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
796  StatementIndex stindex,
797  db::PsqlBindArray& bind_array,
798  Lease6Collection& result) const {
799  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange6_, result);
800  }
801 
812  void getLease(PgSqlLeaseContextPtr& ctx,
813  StatementIndex stindex,
814  db::PsqlBindArray& bind_array,
815  Lease4Ptr& result) const;
816 
827  void getLease(PgSqlLeaseContextPtr& ctx,
828  StatementIndex stindex,
829  db::PsqlBindArray& bind_array,
830  Lease6Ptr& result) const;
831 
846  template<typename LeaseCollection>
847  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
848  const size_t max_leases,
849  StatementIndex statement_index) const;
850 
867  template <typename LeasePtr>
868  void updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
869  StatementIndex stindex,
870  db::PsqlBindArray& bind_array,
871  const LeasePtr& lease);
872 
887  uint64_t deleteLeaseCommon(StatementIndex stindex,
888  db::PsqlBindArray& bind_array);
889 
899  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
900  StatementIndex statement_index);
901 
903  class PgSqlLeaseContextAlloc {
904  public:
905 
912  PgSqlLeaseContextAlloc(const PgSqlLeaseMgr& mgr);
913 
917  ~PgSqlLeaseContextAlloc();
918 
920  PgSqlLeaseContextPtr ctx_;
921 
922  private:
923 
925  const PgSqlLeaseMgr& mgr_;
926  };
927 
928 private:
929 
930  // Members
931 
934 
936  PgSqlLeaseContextPoolPtr pool_;
937 
939  std::string timer_name_;
940 };
941 
942 } // namespace dhcp
943 } // namespace isc
944 
945 #endif // PGSQL_LEASE_MGR_H
virtual ~PgSqlLeaseMgr()
Destructor (closes database)
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const
Returns an IPv4 lease for specified IPv4 address.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:207
PostgreSQL Lease Context.
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const
Returns existing IPv6 lease for a given IPv6 address.
Abstract Lease Manager.
Definition: lease_mgr.h:222
virtual std::string getName() const
Returns backend name.
PostgreSQL Lease Manager.
virtual Lease6Collection getLeases6() const
Returns all IPv6 leases.
An abstract API for lease database.
virtual Lease4Collection getLeases4() const
Returns all IPv4 leases.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
PgSqlLeaseContextPtr createContext() const
Create a new context.
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:487
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs)
Deletes all expired-reclaimed DHCPv6 leases.
PgSqlLeaseContext(const db::DatabaseConnection::ParameterMap &parameters, db::IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
boost::scoped_ptr< PgSqlLease4Exchange > exchange4_
The exchange objects are used for transfer of data to/from the database.
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.
std::mutex mutex_
The mutex to protect pool access.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
std::vector< PgSqlLeaseContextPtr > pool_
The vector of available contexts.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv6 leases.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
virtual std::string getDescription() const
Returns description of the backend.
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 LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
PostgreSQL Lease Context Pool.
virtual void updateLease4(const Lease4Ptr &lease4)
Updates IPv4 lease.
boost::scoped_ptr< PgSqlLease6Exchange > exchange6_
Exchange object.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:283
db::PgSqlConnection conn_
PostgreSQL connection.
static std::string getDBVersion()
Local version of getDBVersion() class method.
virtual void updateLease6(const Lease6Ptr &lease6)
Updates IPv6 lease.
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes all expired-reclaimed DHCPv4 leases.
virtual size_t wipeLeases6(const SubnetID &subnet_id)
Removed specified IPv6 leases.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query.
virtual void rollback()
Rollback Transactions.
Common PgSql Connector Pool.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:640
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.
boost::shared_ptr< PgSqlLeaseContext > PgSqlLeaseContextPtr
Type of pointers to contexts.
virtual std::string getType() const
Return backend type.
Type
Type of lease or pool.
Definition: lease.h:50
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
static bool dbReconnect(db::ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the lease DB backend manager.
StatementIndex
Statement Tags.
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
virtual bool addLease(const Lease4Ptr &lease)
Adds an IPv4 lease.
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 void commit()
Commit Transactions.
virtual bool deleteLease(const Lease4Ptr &lease)
Deletes an IPv4 lease.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
virtual size_t wipeLeases4(const SubnetID &subnet_id)
Removes specified IPv4 leases.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
boost::shared_ptr< PgSqlLeaseContextPool > PgSqlLeaseContextPoolPtr
Type of pointers to context pools.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:492
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query.
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
PgSqlLeaseMgr(const db::DatabaseConnection::ParameterMap &parameters)
Constructor.