Kea  2.3.5-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) override;
165 
175  virtual bool addLease(const Lease6Ptr& lease) override;
176 
192  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const override;
193 
207  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const override;
208 
222  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
223  SubnetID subnet_id) const override;
224 
238  virtual Lease4Collection getLease4(const ClientId& clientid) const override;
239 
252  virtual Lease4Ptr getLease4(const ClientId& clientid,
253  SubnetID subnet_id) const override;
254 
260  virtual Lease4Collection getLeases4(SubnetID subnet_id) const override;
261 
267  virtual Lease4Collection getLeases4(const std::string& hostname) const override;
268 
272  virtual Lease4Collection getLeases4() const override;
273 
298  virtual Lease4Collection
299  getLeases4(const asiolink::IOAddress& lower_bound_address,
300  const LeasePageSize& page_size) const override;
301 
317  virtual Lease6Ptr getLease6(Lease::Type type,
318  const isc::asiolink::IOAddress& addr) const override;
319 
337  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
338  uint32_t iaid) const override;
339 
353  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
354  uint32_t iaid, SubnetID subnet_id) const override;
355 
361  virtual Lease6Collection getLeases6(SubnetID subnet_id) const override;
362 
368  virtual Lease6Collection getLeases6(const std::string& hostname) const override;
369 
373  virtual Lease6Collection getLeases6() const override;
374 
380  virtual Lease6Collection getLeases6(const DUID& duid) const override;
381 
406  virtual Lease6Collection
407  getLeases6(const asiolink::IOAddress& lower_bound_address,
408  const LeasePageSize& page_size) const override;
409 
420  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
421  const size_t max_leases) const override;
422 
433  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
434  const size_t max_leases) const override;
435 
456  virtual void updateLease4(const Lease4Ptr& lease4) override;
457 
478  virtual void updateLease6(const Lease6Ptr& lease6) override;
479 
494  virtual bool deleteLease(const Lease4Ptr& lease) override;
495 
510  virtual bool deleteLease(const Lease6Ptr& lease) override;
511 
519  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) override;
520 
528  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) override;
529 
538  virtual LeaseStatsQueryPtr startLeaseStatsQuery4() override;
539 
548  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id) override;
549 
559  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
560  const SubnetID& last_subnet_id) override;
561 
570  virtual LeaseStatsQueryPtr startLeaseStatsQuery6() override;
571 
580  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id) override;
581 
591  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
592  const SubnetID& last_subnet_id) override;
593 
603  virtual size_t wipeLeases4(const SubnetID& subnet_id) override;
604 
614  virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
615 
621  virtual std::string getType() const override {
622  return (std::string("postgresql"));
623  }
624 
630  virtual std::string getName() const override;
631 
637  virtual std::string getDescription() const override;
638 
646  virtual std::pair<uint32_t, uint32_t> getVersion() const override;
647 
654  virtual void commit() override;
655 
662  virtual void rollback() override;
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  CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits.
703  CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits.
704  IS_JSON_SUPPORTED, // Checks if JSON support is enabled in the database.
705  GET_LEASE4_COUNT_BY_CLASS, // Fetches the IPv4 lease count for a given class.
706  GET_LEASE6_COUNT_BY_CLASS, // Fetches the IPv6 lease count for given class and lease type.
707  NUM_STATEMENTS // Number of statements
708  };
709 
710 private:
711 
728  bool addLeaseCommon(PgSqlLeaseContextPtr& ctx,
729  StatementIndex stindex,
730  db::PsqlBindArray& bind_array);
731 
753  template <typename Exchange, typename LeaseCollection>
754  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
755  StatementIndex stindex,
756  db::PsqlBindArray& bind_array,
757  Exchange& exchange, LeaseCollection& result,
758  bool single = false) const;
759 
777  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
778  StatementIndex stindex,
779  db::PsqlBindArray& bind_array,
780  Lease4Collection& result) const {
781  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange4_, result);
782  }
783 
800  void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
801  StatementIndex stindex,
802  db::PsqlBindArray& bind_array,
803  Lease6Collection& result) const {
804  getLeaseCollection(ctx, stindex, bind_array, ctx->exchange6_, result);
805  }
806 
817  void getLease(PgSqlLeaseContextPtr& ctx,
818  StatementIndex stindex,
819  db::PsqlBindArray& bind_array,
820  Lease4Ptr& result) const;
821 
832  void getLease(PgSqlLeaseContextPtr& ctx,
833  StatementIndex stindex,
834  db::PsqlBindArray& bind_array,
835  Lease6Ptr& result) const;
836 
851  template<typename LeaseCollection>
852  void getExpiredLeasesCommon(LeaseCollection& expired_leases,
853  const size_t max_leases,
854  StatementIndex statement_index) const;
855 
872  template <typename LeasePtr>
873  void updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
874  StatementIndex stindex,
875  db::PsqlBindArray& bind_array,
876  const LeasePtr& lease);
877 
892  uint64_t deleteLeaseCommon(StatementIndex stindex,
893  db::PsqlBindArray& bind_array);
894 
904  uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
905  StatementIndex statement_index);
906 
918  std::string
919  checkLimits(isc::data::ConstElementPtr const& user_context, StatementIndex const stindex) const;
920 
932  virtual std::string
933  checkLimits4(isc::data::ConstElementPtr const& user_context) const override;
934 
946  virtual std::string
947  checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
948 
953  virtual bool isJsonSupported() const override;
954 
962  virtual size_t getClassLeaseCount(const ClientClass& client_class,
963  const Lease::Type& ltype = Lease::TYPE_V4) const override;
964 
966  virtual void recountClassLeases4() override;
967 
969  virtual void recountClassLeases6() override;
970 
972  virtual void clearClassLeaseCounts() override;
973 
977 
990  virtual Lease4Collection
991  getLeases4ByRelayId(const OptionBuffer& relay_id,
992  const asiolink::IOAddress& lower_bound_address,
993  const LeasePageSize& page_size,
994  const time_t& qry_start_time = 0,
995  const time_t& qry_end_time = 0) override;
996 
1009  virtual Lease4Collection
1010  getLeases4ByRemoteId(const OptionBuffer& remote_id,
1011  const asiolink::IOAddress& lower_bound_address,
1012  const LeasePageSize& page_size,
1013  const time_t& qry_start_time = 0,
1014  const time_t& qry_end_time = 0) override;
1015 
1026  virtual Lease6Collection
1027  getLeases6ByRelayId(const DUID& relay_id,
1028  const asiolink::IOAddress& link_addr,
1029  uint8_t link_len,
1030  const asiolink::IOAddress& lower_bound_address,
1031  const LeasePageSize& page_size) override;
1032 
1043  virtual Lease6Collection
1044  getLeases6ByRemoteId(const OptionBuffer& remote_id,
1045  const asiolink::IOAddress& link_addr,
1046  uint8_t link_len,
1047  const asiolink::IOAddress& lower_bound_address,
1048  const LeasePageSize& page_size) override;
1049 
1059  virtual Lease6Collection
1060  getLeases6ByLink(const asiolink::IOAddress& link_addr,
1061  uint8_t link_len,
1062  const asiolink::IOAddress& lower_bound_address,
1063  const LeasePageSize& page_size) override;
1064 
1071  virtual size_t buildExtendedInfoTables6(bool update, bool current) override;
1072 
1074  virtual void writeLeases4(const std::string& /*filename*/) override;
1075 
1077  virtual void writeLeases6(const std::string& /*filename*/) override;
1078 
1080  class PgSqlLeaseContextAlloc {
1081  public:
1082 
1089  PgSqlLeaseContextAlloc(const PgSqlLeaseMgr& mgr);
1090 
1094  ~PgSqlLeaseContextAlloc();
1095 
1097  PgSqlLeaseContextPtr ctx_;
1098 
1099  private:
1100 
1102  const PgSqlLeaseMgr& mgr_;
1103  };
1104 
1105 protected:
1106 
1108 
1114  virtual void setExtendedInfoTablesEnabled(const bool enabled) override {
1115  if (enabled) {
1117  "extended info tables are not yet supported by postgresql");
1118  }
1119  }
1120 
1127  virtual void setExtendedInfoTablesEnabled(const db::DatabaseConnection::ParameterMap& parameters) override;
1128 
1132  virtual void deleteExtendedInfo6(const isc::asiolink::IOAddress& addr) override;
1133 
1138  virtual void addRelayId6(const isc::asiolink::IOAddress& lease_addr,
1139  const std::vector<uint8_t>& relay_id) override;
1140 
1145  virtual void addRemoteId6(const isc::asiolink::IOAddress& lease_addr,
1146  const std::vector<uint8_t>& remote_id) override;
1147 
1148 private:
1149 
1150  // Members
1151 
1154 
1156  PgSqlLeaseContextPoolPtr pool_;
1157 
1159  std::string timer_name_;
1160 };
1161 
1162 } // namespace dhcp
1163 } // namespace isc
1164 
1165 #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:210
PostgreSQL Lease Context.
A generic exception that is thrown when a function is not implemented.
virtual void setExtendedInfoTablesEnabled(const bool enabled) override
Extended information / Bulk Lease Query shared interface.
Abstract Lease Manager.
Definition: lease_mgr.h:225
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:514
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:22
std::vector< PgSqlLeaseContextPtr > pool_
The vector of available contexts.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
virtual std::string getType() const override
Return backend type.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
PostgreSQL Lease Context Pool.
boost::scoped_ptr< PgSqlLease6Exchange > exchange6_
Exchange object.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:294
db::PgSqlConnection conn_
PostgreSQL connection.
IPv4 lease.
Definition: lease.h:50
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:677
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:46
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:46
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
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:519
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