Kea  2.1.7-git
pgsql_lease_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2013-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 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(util::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 
538  virtual LeaseStatsQueryPtr startLeaseStatsQuery4();
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 
570  virtual LeaseStatsQueryPtr startLeaseStatsQuery6();
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 
620  bool isJsonSupported() const override;
621 
627  virtual std::string getType() const {
628  return (std::string("postgresql"));
629  }
630 
636  virtual std::string getName() const;
637 
643  virtual std::string getDescription() const;
644 
652  virtual std::pair<uint32_t, uint32_t> getVersion() const;
653 
660  virtual void commit();
661 
668  virtual void rollback();
669 
675  DELETE_LEASE4, // Delete from lease4 by address
676  DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
677  DELETE_LEASE6, // Delete from lease6 by address
678  DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
679  GET_LEASE4, // Get all IPv4 leases
680  GET_LEASE4_ADDR, // Get lease4 by address
681  GET_LEASE4_CLIENTID, // Get lease4 by client ID
682  GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
683  GET_LEASE4_HWADDR, // Get lease4 by HW address
684  GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
685  GET_LEASE4_PAGE, // Get page of leases beginning with an address
686  GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID
687  GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname
688  GET_LEASE4_EXPIRE, // Get lease4 by expiration.
689  GET_LEASE6, // Get all IPv6 leases
690  GET_LEASE6_ADDR, // Get lease6 by address
691  GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
692  GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
693  GET_LEASE6_PAGE, // Get page of leases beginning with an address
694  GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID
695  GET_LEASE6_DUID, // Get IPv6 leases by DUID
696  GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname
697  GET_LEASE6_EXPIRE, // Get lease6 by expiration.
698  INSERT_LEASE4, // Add entry to lease4 table
699  INSERT_LEASE6, // Add entry to lease6 table
700  UPDATE_LEASE4, // Update a Lease4 entry
701  UPDATE_LEASE6, // Update a Lease6 entry
702  ALL_LEASE4_STATS, // Fetches IPv4 lease statistics
703  SUBNET_LEASE4_STATS, // Fetched IPv4 lease stats for a single subnet.
704  SUBNET_RANGE_LEASE4_STATS, // Fetched IPv4 lease stats for a subnet range.
705  ALL_LEASE6_STATS, // Fetches IPv6 lease statistics
706  SUBNET_LEASE6_STATS, // Fetched IPv6 lease stats for a single subnet.
707  SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range.
708  CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits.
709  CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits.
710  NUM_STATEMENTS // Number of statements
711  };
712 
713 private:
714 
731  bool addLeaseCommon(PgSqlLeaseContextPtr& ctx,
732  StatementIndex stindex,
733  db::PsqlBindArray& bind_array);
734 
756  template <typename Exchange, typename LeaseCollection>
757  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
758  StatementIndex stindex,
759  db::PsqlBindArray& bind_array,
760  Exchange& exchange, LeaseCollection& result,
761  bool single = false) const;
762 
780  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
781  StatementIndex stindex,
782  db::PsqlBindArray& bind_array,
783  Lease4Collection& result) const {
784  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange4_, result);
785  }
786 
803  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
804  StatementIndex stindex,
805  db::PsqlBindArray& bind_array,
806  Lease6Collection& result) const {
807  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange6_, result);
808  }
809 
820  void getLease(PgSqlLeaseContextPtr& ctx,
821  StatementIndex stindex,
822  db::PsqlBindArray& bind_array,
823  Lease4Ptr& result) const;
824 
835  void getLease(PgSqlLeaseContextPtr& ctx,
836  StatementIndex stindex,
837  db::PsqlBindArray& bind_array,
838  Lease6Ptr& result) const;
839 
854  template<typename LeaseCollection>
855  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
856  const size_t max_leases,
857  StatementIndex statement_index) const;
858 
875  template <typename LeasePtr>
876  void updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
877  StatementIndex stindex,
878  db::PsqlBindArray& bind_array,
879  const LeasePtr& lease);
880 
895  uint64_t deleteLeaseCommon(StatementIndex stindex,
896  db::PsqlBindArray& bind_array);
897 
907  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
908  StatementIndex statement_index);
909 
921  std::string
922  checkLimits(isc::data::ConstElementPtr const& user_context, StatementIndex const stindex) const;
923 
935  std::string checkLimits4(isc::data::ConstElementPtr const& user_context) const override;
936 
948  std::string checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
949 
951  class PgSqlLeaseContextAlloc {
952  public:
953 
960  PgSqlLeaseContextAlloc(const PgSqlLeaseMgr& mgr);
961 
965  ~PgSqlLeaseContextAlloc();
966 
968  PgSqlLeaseContextPtr ctx_;
969 
970  private:
971 
973  const PgSqlLeaseMgr& mgr_;
974  };
975 
976 private:
977 
978  // Members
979 
982 
984  PgSqlLeaseContextPoolPtr pool_;
985 
987  std::string timer_name_;
988 };
989 
990 } // namespace dhcp
991 } // namespace isc
992 
993 #endif // PGSQL_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
PostgreSQL Lease Context.
Abstract Lease Manager.
Definition: lease_mgr.h:223
PostgreSQL Lease Manager.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
An abstract API for lease database.
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:482
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.
std::mutex mutex_
The mutex to protect pool access.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:21
std::vector< PgSqlLeaseContextPtr > pool_
The vector of available contexts.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
PostgreSQL Lease Context Pool.
boost::scoped_ptr< PgSqlLease6Exchange > exchange6_
Exchange object.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:278
db::PgSqlConnection conn_
PostgreSQL connection.
virtual std::string getType() const
Return backend type.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Common PgSql Connector Pool.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:635
Defines the logger used by the top-level component of kea-lfc.
boost::shared_ptr< PgSqlLeaseContext > PgSqlLeaseContextPtr
Type of pointers to contexts.
Type
Type of lease or pool.
Definition: lease.h:45
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
StatementIndex
Statement Tags.
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
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
boost::shared_ptr< PgSqlLeaseContextPool > PgSqlLeaseContextPoolPtr
Type of pointers to context pools.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:487
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