Kea  1.9.9-git
ha_service.h
Go to the documentation of this file.
1 // Copyright (C) 2018-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 HA_SERVICE_H
8 #define HA_SERVICE_H
9 
10 #include <communication_state.h>
11 #include <ha_config.h>
12 #include <ha_server_type.h>
13 #include <lease_update_backlog.h>
14 #include <query_filter.h>
15 #include <asiolink/asio_wrapper.h>
16 #include <asiolink/io_service.h>
17 #include <asiolink/tls_socket.h>
18 #include <cc/data.h>
20 #include <dhcp/pkt4.h>
21 #include <http/response.h>
22 #include <dhcpsrv/lease.h>
23 #include <dhcpsrv/network_state.h>
24 #include <hooks/parking_lots.h>
25 #include <http/client.h>
26 #include <util/state_model.h>
27 
28 #include <boost/noncopyable.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <functional>
31 #include <map>
32 #include <mutex>
33 #include <vector>
34 
35 namespace isc {
36 namespace ha {
37 
43 class HAService : public boost::noncopyable, public util::StateModel {
44 public:
45 
48 
51 
54 
57 
60 
63 
66 
69 
70 protected:
71 
77  typedef std::function<void(const bool, const std::string&)> PostRequestCallback;
78 
86  typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
87 
88 public:
89 
101  HAService(const asiolink::IOServicePtr& io_service,
102  const dhcp::NetworkStatePtr& network_state,
103  const HAConfigPtr& config,
104  const HAServerType& server_type = HAServerType::DHCPv4);
105 
110  virtual ~HAService();
111 
114  return (server_type_);
115  }
116 
118  virtual void defineEvents();
119 
121  virtual void verifyEvents();
122 
124  virtual void defineStates();
125 
138  void backupStateHandler();
139 
168 
197  void normalStateHandler();
198 
209 
235 
257 
266 
282  void readyStateHandler();
283 
300  void syncingStateHandler();
301 
319  void terminatedStateHandler();
320 
344  void waitingStateHandler();
345 
348  int getPartnerState() const {
349  return (communication_state_->getPartnerState());
350  }
351 
352 protected:
353 
357  void verboseTransition(const unsigned state);
358 
365  int getNormalState() const;
366 
367 public:
368 
376  bool unpause();
377 
378 protected:
379 
384  void conditionalLogPausedState() const;
385 
386 public:
387 
392  void serveDefaultScopes();
393 
407  bool inScope(dhcp::Pkt4Ptr& query4);
408 
422  bool inScope(dhcp::Pkt6Ptr& query6);
423 
424 private:
425 
439  template<typename QueryPtrType>
440  bool inScopeInternal(QueryPtrType& query);
441 
442 public:
443 
448  void adjustNetworkState();
449 
450 protected:
451 
465  bool shouldPartnerDown() const;
466 
478  bool shouldTerminate() const;
479 
484  bool isMaintenanceCanceled() const;
485 
501  bool isPartnerStateInvalid() const;
502 
503 public:
504 
538  size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
539  const dhcp::Lease4CollectionPtr& leases,
540  const dhcp::Lease4CollectionPtr& deleted_leases,
541  const hooks::ParkingLotHandlePtr& parking_lot);
542 
561  size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
562  const dhcp::Lease6CollectionPtr& leases,
563  const dhcp::Lease6CollectionPtr& deleted_leases,
564  const hooks::ParkingLotHandlePtr& parking_lot);
565 
566 protected:
567 
580  template<typename QueryPtrType>
581  void asyncSendLeaseUpdate(const QueryPtrType& query,
582  const HAConfig::PeerConfigPtr& config,
583  const data::ConstElementPtr& command,
584  const hooks::ParkingLotHandlePtr& parking_lot);
585 
595  void logFailedLeaseUpdates(const dhcp::PktPtr& query,
596  const data::ConstElementPtr& args) const;
597 
608  bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
609 
621  bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
622 
623 public:
624 
650 
656 
670 
671 protected:
672 
674  void asyncSendHeartbeat();
675 
681  void scheduleHeartbeat();
682 
684  void startHeartbeat();
685 
697  void asyncDisableDHCPService(http::HttpClient& http_client,
698  const std::string& server_name,
699  const unsigned int max_period,
700  PostRequestCallback post_request_action);
701 
711  void asyncEnableDHCPService(http::HttpClient& http_client,
712  const std::string& server_name,
713  PostRequestCallback post_request_action);
714 
717 
719  void localEnableDHCPService();
720 
736  void asyncSyncLeases();
737 
788  void asyncSyncLeases(http::HttpClient& http_client,
789  const std::string& server_name,
790  const unsigned int max_period,
791  const dhcp::LeasePtr& last_lease,
792  PostSyncCallback post_sync_action,
793  const bool dhcp_disabled = false);
794 
821  void asyncSyncLeasesInternal(http::HttpClient& http_client,
822  const std::string& server_name,
823  const unsigned int max_period,
824  const dhcp::LeasePtr& last_lease,
825  PostSyncCallback post_sync_action,
826  const bool dhcp_disabled);
827 
828 public:
829 
849  data::ConstElementPtr processSynchronize(const std::string& server_name,
850  const unsigned int max_period);
851 
852 protected:
853 
870  int synchronize(std::string& status_message, const std::string& server_name,
871  const unsigned int max_period);
872 
892  const HAConfig::PeerConfigPtr& remote_config,
893  PostRequestCallback post_request_action);
894 
909 
916  void asyncSendHAReset(http::HttpClient& http_client,
917  const HAConfig::PeerConfigPtr& remote_config,
918  PostRequestCallback post_request_action);
919 
932  bool sendHAReset();
933 
934 public:
935 
941  data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
942 
947 
969 
987 
1004 
1012  void startClientAndListener();
1013 
1020  void pauseClientAndListener();
1021 
1028  void resumeClientAndListener();
1029 
1035  void stopClientAndListener();
1036 
1037 protected:
1038 
1049  int& rcode);
1050 
1060  bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd);
1061 
1067  bool clientHandshakeHandler(const boost::system::error_code&) {
1068  return (true);
1069  }
1070 
1087  void socketReadyHandler(int tcp_native_fd);
1088 
1096  void clientCloseHandler(int tcp_native_fd);
1097 
1101 
1104 
1107 
1110 
1113 
1117 
1120 
1123 
1140  template<typename QueryPtrType>
1141  bool leaseUpdateComplete(QueryPtrType& query,
1142  const hooks::ParkingLotHandlePtr& parking_lot);
1143 
1149  template<typename QueryPtrType>
1150  void updatePendingRequest(QueryPtrType& query);
1151 
1156  size_t pendingRequestSize();
1157 
1167  template<typename QueryPtrType>
1168  int getPendingRequest(const QueryPtrType& query);
1169 
1170 private:
1189  template<typename QueryPtrType>
1190  bool leaseUpdateCompleteInternal(QueryPtrType& query,
1191  const hooks::ParkingLotHandlePtr& parking_lot);
1192 
1200  template<typename QueryPtrType>
1201  void updatePendingRequestInternal(QueryPtrType& query);
1202 
1213  template<typename QueryPtrType>
1214  int getPendingRequestInternal(const QueryPtrType& query);
1215 
1217  std::mutex mutex_;
1218 
1227  std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
1228 
1229 protected:
1230 
1237 };
1238 
1240 typedef boost::shared_ptr<HAService> HAServicePtr;
1241 
1242 } // end of namespace isc::ha
1243 } // end of namespace isc
1244 
1245 #endif
config::CmdHttpListenerPtr listener_
HTTP listener instance used to receive and respond to HA commands and lease updates.
Definition: ha_service.h:1116
static const int HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED
Control result returned in response to ha-maintenance-notify.
Definition: ha_service.h:68
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:142
data::ConstElementPtr processStatusGet() const
Processes status-get command and returns a response.
Definition: ha_service.cc:1500
Implements a finite state machine.
Definition: state_model.h:274
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:960
data::ConstElementPtr processHAReset()
Processes ha-reset command and returns a response.
Definition: ha_service.cc:1569
void pauseClientAndListener()
Pauses client and(or) listener thread pool operations.
Definition: ha_service.cc:2833
bool leaseUpdateComplete(QueryPtrType &query, const hooks::ParkingLotHandlePtr &parking_lot)
Handle last pending request for this query.
Definition: ha_service.cc:1216
static const int HA_MAINTENANCE_CANCEL_EVT
ha-maintenance-cancel command received.
Definition: ha_service.h:65
int getNormalState() const
Returns normal operation state for the current configuration.
Definition: ha_service.cc:923
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:561
size_t pendingRequestSize()
Get the number of entries in the pending request map.
Definition: ha_service.cc:2786
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:2398
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1701
void asyncEnableDHCPService(http::HttpClient &http_client, const std::string &server_name, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-enable" command to the specified server.
Definition: ha_service.cc:1796
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:939
void passiveBackupStateHandler()
Handler for "passive-backup" state.
Definition: ha_service.cc:543
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:1882
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:1549
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:1122
boost::shared_ptr< CmdHttpListener > CmdHttpListenerPtr
Defines a shared pointer to CmdHttpListener.
LeaseUpdateBacklog lease_update_backlog_
Backlog of DHCP lease updates.
Definition: ha_service.h:1236
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:1119
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:129
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchronization succeeded.
Definition: ha_service.h:56
bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be queued.
Definition: ha_service.cc:1435
bool clientConnectHandler(const boost::system::error_code &ec, int tcp_native_fd)
HttpClient connect callback handler.
Definition: ha_service.cc:2739
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:1109
static const int HA_MAINTENANCE_NOTIFY_EVT
ha-maintenance-notify command received.
Definition: ha_service.h:59
data::ConstElementPtr processMaintenanceCancel()
Processes ha-maintenance-cancel command and returns a response.
Definition: ha_service.cc:2585
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:750
void updatePendingRequest(QueryPtrType &query)
Update pending request counter for this query.
Definition: ha_service.cc:1249
void partnerInMaintenanceStateHandler()
Handler for "partner-in-maintenance" state.
Definition: ha_service.cc:504
HAServerType
Lists possible server types for which HA service is created.
virtual ~HAService()
Destructor.
Definition: ha_service.cc:108
bool isMaintenanceCanceled() const
Convenience method checking if the current state is a result of canceling the maintenance.
Definition: ha_service.cc:1067
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:1103
static const int HA_MAINTENANCE_START_EVT
ha-maintenance-start command received.
Definition: ha_service.h:62
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:1100
bool isPartnerStateInvalid() const
Indicates if the partner's state is invalid.
Definition: ha_service.cc:1072
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:2411
DHCP query filtering class.
Definition: query_filter.h:61
void resumeClientAndListener()
Resumes client and(or) listener thread pool operations.
Definition: ha_service.cc:2851
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat command.
Definition: ha_service.h:47
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:50
bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be sent as result of leases allocation or release.
Definition: ha_service.cc:1403
HAService(const asiolink::IOServicePtr &io_service, const dhcp::NetworkStatePtr &network_state, const HAConfigPtr &config, const HAServerType &server_type=HAServerType::DHCPv4)
Constructor.
Definition: ha_service.cc:53
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:427
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:640
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:1579
boost::shared_ptr< HAService > HAServicePtr
Pointer to the HAService class.
Definition: ha_service.h:1240
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:490
void inMaintenanceStateHandler()
Handler for the "in-maintenance" state.
Definition: ha_service.cc:402
void logFailedLeaseUpdates(const dhcp::PktPtr &query, const data::ConstElementPtr &args) const
Log failed lease updates.
Definition: ha_service.cc:1448
void asyncSendLeaseUpdate(const QueryPtrType &query, const HAConfig::PeerConfigPtr &config, const data::ConstElementPtr &command, const hooks::ParkingLotHandlePtr &parking_lot)
Asynchronously sends lease update to the peer.
Definition: ha_service.cc:1270
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
data::ConstElementPtr processMaintenanceNotify(const bool cancel)
Processes ha-maintenance-notify command and returns a response.
Definition: ha_service.cc:2419
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:965
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:729
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:304
void stopClientAndListener()
Stop the client and(or) listener instances.
Definition: ha_service.cc:2869
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:1872
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:797
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:995
void asyncSyncLeasesInternal(http::HttpClient &http_client, const std::string &server_name, const unsigned int max_period, const dhcp::LeasePtr &last_lease, PostSyncCallback post_sync_action, const bool dhcp_disabled)
Implements fetching one page of leases during synchronization.
Definition: ha_service.cc:1929
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:86
HTTP client class.
Definition: client.h:87
void communicationRecoveryHandler()
Handler for the "communication-recovery" state.
Definition: ha_service.cc:210
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:1027
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:949
data::ConstElementPtr processMaintenanceStart()
Processes ha-maintenance-start command and returns a response.
Definition: ha_service.cc:2455
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:1106
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1708
void startClientAndListener()
Start the client and(or) listener instances.
Definition: ha_service.cc:2817
http::HttpClientPtr client_
HTTP client instance used to send HA commands and lease updates.
Definition: ha_service.h:1112
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:327
High availability service.
Definition: ha_service.h:43
bool sendLeaseUpdatesFromBacklog()
Attempts to send all lease updates from the backlog synchronously.
Definition: ha_service.cc:2288
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:1877
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:113
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:116
std::function< void(const bool, const std::string &)> PostRequestCallback
Callback invoked when request was sent and a response received or an error occurred.
Definition: ha_service.h:77
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:644
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:195
int getPendingRequest(const QueryPtrType &query)
Get the number of scheduled requests for a given query.
Definition: ha_service.cc:2797
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:53
size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr &query, const dhcp::Lease4CollectionPtr &leases, const dhcp::Lease4CollectionPtr &deleted_leases, const hooks::ParkingLotHandlePtr &parking_lot)
Schedules asynchronous IPv4 leases updates.
Definition: ha_service.cc:1102
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:375
void asyncSendHAReset(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Sends ha-reset command to partner asynchronously.
Definition: ha_service.cc:2329
void socketReadyHandler(int tcp_native_fd)
IfaceMgr external socket ready callback handler.
Definition: ha_service.cc:2770
This file defines the class StateModel.
data::ConstElementPtr processSynchronize(const std::string &server_name, const unsigned int max_period)
Processes ha-sync command and returns a response.
Definition: ha_service.cc:2125
int getPartnerState() const
Returns last known state of the partner.
Definition: ha_service.h:348
Queue holding a backlog of unsent lease updates.
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response, int &rcode)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:2681
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
bool shouldTerminate() const
Indicates if the server should transition to the terminated state as a result of high clock skew...
Definition: ha_service.cc:1054
void clientCloseHandler(int tcp_native_fd)
HttpClient close callback handler.
Definition: ha_service.cc:2779
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:858
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:760
bool sendHAReset()
Sends ha-reset command to partner synchronously.
Definition: ha_service.cc:2379
int synchronize(std::string &status_message, const std::string &server_name, const unsigned int max_period)
Synchronizes lease database with a partner.
Definition: ha_service.cc:2133
bool clientHandshakeHandler(const boost::system::error_code &)
HttpClient handshake callback handler.
Definition: ha_service.h:1067
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:330
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition: ha_config.h:229
void asyncDisableDHCPService(http::HttpClient &http_client, const std::string &server_name, const unsigned int max_period, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-disable" command to the specified server.
Definition: ha_service.cc:1717
void asyncSendLeaseUpdatesFromBacklog(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Sends lease updates from backlog to partner asynchronously.
Definition: ha_service.cc:2212