Kea  2.5.3
ha_service.h
Go to the documentation of this file.
1 // Copyright (C) 2018-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 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 
70 
73 
74 protected:
75 
82  typedef std::function<void(const bool, const std::string&, const int)> PostRequestCallback;
83 
91  typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
92 
93 public:
94 
106  HAService(const asiolink::IOServicePtr& io_service,
107  const dhcp::NetworkStatePtr& network_state,
108  const HAConfigPtr& config,
109  const HAServerType& server_type = HAServerType::DHCPv4);
110 
115  virtual ~HAService();
116 
119  return (server_type_);
120  }
121 
123  virtual void defineEvents();
124 
126  virtual void verifyEvents();
127 
129  virtual void defineStates();
130 
143  void backupStateHandler();
144 
173 
202  void normalStateHandler();
203 
214 
240 
262 
271 
287  void readyStateHandler();
288 
305  void syncingStateHandler();
306 
324  void terminatedStateHandler();
325 
349  void waitingStateHandler();
350 
353  int getPartnerState() const {
354  return (communication_state_->getPartnerState());
355  }
356 
357 protected:
358 
362  void verboseTransition(const unsigned state);
363 
370  int getNormalState() const;
371 
372 public:
373 
381  bool unpause();
382 
383 protected:
384 
389  void conditionalLogPausedState() const;
390 
391 public:
392 
397  void serveDefaultScopes();
398 
412  bool inScope(dhcp::Pkt4Ptr& query4);
413 
427  bool inScope(dhcp::Pkt6Ptr& query6);
428 
429 private:
430 
444  template<typename QueryPtrType>
445  bool inScopeInternal(QueryPtrType& query);
446 
447 public:
448 
453  void adjustNetworkState();
454 
455 protected:
456 
470  bool shouldPartnerDown() const;
471 
490  bool shouldTerminate() const;
491 
496  bool isMaintenanceCanceled() const;
497 
513  bool isPartnerStateInvalid() const;
514 
515 public:
516 
550  size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
551  const dhcp::Lease4CollectionPtr& leases,
552  const dhcp::Lease4CollectionPtr& deleted_leases,
553  const hooks::ParkingLotHandlePtr& parking_lot);
554 
573  size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
574  const dhcp::Lease6CollectionPtr& leases,
575  const dhcp::Lease6CollectionPtr& deleted_leases,
576  const hooks::ParkingLotHandlePtr& parking_lot);
577 
578 protected:
579 
592  template<typename QueryPtrType>
593  void asyncSendLeaseUpdate(const QueryPtrType& query,
594  const HAConfig::PeerConfigPtr& config,
595  const data::ConstElementPtr& command,
596  const hooks::ParkingLotHandlePtr& parking_lot);
597 
607  void logFailedLeaseUpdates(const dhcp::PktPtr& query,
608  const data::ConstElementPtr& args) const;
609 
620  bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
621 
633  bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
634 
635 public:
636 
662 
668 
682 
683 protected:
684 
686  void asyncSendHeartbeat();
687 
693  void scheduleHeartbeat();
694 
696  void startHeartbeat();
697 
709  void asyncDisableDHCPService(http::HttpClient& http_client,
710  const std::string& server_name,
711  const unsigned int max_period,
712  PostRequestCallback post_request_action);
713 
723  void asyncEnableDHCPService(http::HttpClient& http_client,
724  const std::string& server_name,
725  PostRequestCallback post_request_action);
726 
729 
731  void localEnableDHCPService();
732 
748  void asyncSyncLeases();
749 
800  void asyncSyncLeases(http::HttpClient& http_client,
801  const std::string& server_name,
802  const unsigned int max_period,
803  const dhcp::LeasePtr& last_lease,
804  PostSyncCallback post_sync_action,
805  const bool dhcp_disabled = false);
806 
833  void asyncSyncLeasesInternal(http::HttpClient& http_client,
834  const std::string& server_name,
835  const unsigned int max_period,
836  const dhcp::LeasePtr& last_lease,
837  PostSyncCallback post_sync_action,
838  const bool dhcp_disabled);
839 
840 public:
841 
861  data::ConstElementPtr processSynchronize(const std::string& server_name,
862  const unsigned int max_period);
863 
864 protected:
865 
886  int synchronize(std::string& status_message, const std::string& server_name,
887  const unsigned int max_period);
888 
908  const HAConfig::PeerConfigPtr& remote_config,
909  PostRequestCallback post_request_action);
910 
925 
932  void asyncSendHAReset(http::HttpClient& http_client,
933  const HAConfig::PeerConfigPtr& remote_config,
934  PostRequestCallback post_request_action);
935 
948  bool sendHAReset();
949 
950 public:
951 
957  data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
958 
963 
985 
1003 
1020 
1027 
1028 protected:
1029 
1039  void asyncSyncCompleteNotify(http::HttpClient& http_client,
1040  const std::string& server_name,
1041  PostRequestCallback post_request_action);
1042 
1043 public:
1044 
1057 
1065  void startClientAndListener();
1066 
1073  void pauseClientAndListener();
1074 
1081  void resumeClientAndListener();
1082 
1088  void stopClientAndListener();
1089 
1090 protected:
1091 
1107  int& rcode);
1108 
1118  bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd);
1119 
1125  bool clientHandshakeHandler(const boost::system::error_code&) {
1126  return (true);
1127  }
1128 
1145  void socketReadyHandler(int tcp_native_fd);
1146 
1154  void clientCloseHandler(int tcp_native_fd);
1155 
1159 
1162 
1165 
1168 
1171 
1175 
1178 
1181 
1198  template<typename QueryPtrType>
1199  bool leaseUpdateComplete(QueryPtrType& query,
1200  const hooks::ParkingLotHandlePtr& parking_lot);
1201 
1207  template<typename QueryPtrType>
1208  void updatePendingRequest(QueryPtrType& query);
1209 
1214  size_t pendingRequestSize();
1215 
1225  template<typename QueryPtrType>
1226  int getPendingRequest(const QueryPtrType& query);
1227 
1228 private:
1247  template<typename QueryPtrType>
1248  bool leaseUpdateCompleteInternal(QueryPtrType& query,
1249  const hooks::ParkingLotHandlePtr& parking_lot);
1250 
1258  template<typename QueryPtrType>
1259  void updatePendingRequestInternal(QueryPtrType& query);
1260 
1271  template<typename QueryPtrType>
1272  int getPendingRequestInternal(const QueryPtrType& query);
1273 
1275  std::mutex mutex_;
1276 
1285  std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
1286 
1287 protected:
1288 
1295 
1304 };
1305 
1307 typedef boost::shared_ptr<HAService> HAServicePtr;
1308 
1309 } // end of namespace isc::ha
1310 } // end of namespace isc
1311 
1312 #endif
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition: ha_config.h:229
High availability service.
Definition: ha_service.h:43
static const int HA_MAINTENANCE_START_EVT
ha-maintenance-start command received.
Definition: ha_service.h:62
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:1020
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:1050
void stopClientAndListener()
Stop the client and(or) listener instances.
Definition: ha_service.cc:3229
int getNormalState() const
Returns normal operation state for the current configuration.
Definition: ha_service.cc:978
bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be queued.
Definition: ha_service.cc:1529
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat command.
Definition: ha_service.h:47
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:2504
bool clientConnectHandler(const boost::system::error_code &ec, int tcp_native_fd)
HttpClient connect callback handler.
Definition: ha_service.cc:3071
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:2012
bool isMaintenanceCanceled() const
Convenience method checking if the current state is a result of canceling the maintenance.
Definition: ha_service.cc:1125
data::ConstElementPtr processMaintenanceCancel()
Processes ha-maintenance-cancel command and returns a response.
Definition: ha_service.cc:2760
void checkPermissionsClientAndListener()
Check client and(or) listener current thread permissions to perform thread pool state transition.
Definition: ha_service.cc:3149
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:1344
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:913
bool sendLeaseUpdatesFromBacklog()
Attempts to send all lease updates from the backlog synchronously.
Definition: ha_service.cc:2463
config::CmdHttpListenerPtr listener_
HTTP listener instance used to receive and respond to HA commands and lease updates.
Definition: ha_service.h:1174
void clientCloseHandler(int tcp_native_fd)
HttpClient close callback handler.
Definition: ha_service.cc:3111
void asyncSyncCompleteNotify(http::HttpClient &http_client, const std::string &server_name, PostRequestCallback post_request_action)
Schedules asynchronous "ha-sync-complete-notify" command to the specified server.
Definition: ha_service.cc:2856
bool leaseUpdateComplete(QueryPtrType &query, const hooks::ParkingLotHandlePtr &parking_lot)
Handle last pending request for this query.
Definition: ha_service.cc:1290
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:1164
data::ConstElementPtr processMaintenanceStart()
Processes ha-maintenance-start command and returns a response.
Definition: ha_service.cc:2630
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:1167
bool sync_complete_notified_
An indicator that a partner sent ha-sync-complete-notify command.
Definition: ha_service.h:1303
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:1925
bool shouldTerminate() const
Indicates if the server should transition to the terminated state.
Definition: ha_service.cc:1109
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:2059
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:782
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:2573
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:1161
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:74
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:2255
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1829
void passiveBackupStateHandler()
Handler for "passive-backup" state.
Definition: ha_service.cc:594
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:1180
static const int HA_MAINTENANCE_NOTIFY_EVT
ha-maintenance-notify command received.
Definition: ha_service.h:59
static const int HA_SYNCED_PARTNER_UNAVAILABLE_EVT
The heartbeat command failed after receiving ha-sync-complete-notify command from the partner.
Definition: ha_service.h:69
void inMaintenanceStateHandler()
Handler for the "in-maintenance" state.
Definition: ha_service.cc:439
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:165
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:1004
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:994
static const int HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED
Control result returned in response to ha-maintenance-notify.
Definition: ha_service.h:72
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:1015
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:1160
size_t pendingRequestSize()
Get the number of entries in the pending request map.
Definition: ha_service.cc:3118
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchronization succeeded.
Definition: ha_service.h:56
bool sendHAReset()
Sends ha-reset command to partner synchronously.
Definition: ha_service.cc:2554
std::function< void(const bool, const std::string &, const int)> PostRequestCallback
Callback invoked when request was sent and a response received or an error occurred.
Definition: ha_service.h:82
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:151
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:1158
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:2002
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:1177
void logFailedLeaseUpdates(const dhcp::PktPtr &query, const data::ConstElementPtr &args) const
Log failed lease updates.
Definition: ha_service.cc:1542
bool clientHandshakeHandler(const boost::system::error_code &)
HttpClient handshake callback handler.
Definition: ha_service.h:1125
LeaseUpdateBacklog lease_update_backlog_
Backlog of DHCP lease updates.
Definition: ha_service.h:1294
data::ConstElementPtr processSyncCompleteNotify()
Process ha-sync-complete-notify command and returns a response.
Definition: ha_service.cc:2937
virtual ~HAService()
Destructor.
Definition: ha_service.cc:143
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:1845
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:53
static const int HA_MAINTENANCE_CANCEL_EVT
ha-maintenance-cancel command received.
Definition: ha_service.h:65
int getPartnerState() const
Returns last known state of the partner.
Definition: ha_service.h:353
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:2387
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:1643
data::ConstElementPtr processHAReset()
Processes ha-reset command and returns a response.
Definition: ha_service.cc:1666
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:612
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:179
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:232
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:1676
void communicationRecoveryHandler()
Handler for the "communication-recovery" state.
Definition: ha_service.cc:247
bool isPartnerStateInvalid() const
Indicates if the partner's state is invalid.
Definition: ha_service.cc:1130
void startClientAndListener()
Start the client and(or) listener instances.
Definition: ha_service.cc:3176
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response, int &rcode)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:2948
void resumeClientAndListener()
Resumes client and(or) listener thread pool operations.
Definition: ha_service.cc:3211
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:2263
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:364
data::ConstElementPtr processStatusGet() const
Processes status-get command and returns a response.
Definition: ha_service.cc:1594
int getPendingRequest(const QueryPtrType &query)
Get the number of scheduled requests for a given query.
Definition: ha_service.cc:3129
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:804
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:1497
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:2007
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:50
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:118
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:464
void socketReadyHandler(int tcp_native_fd)
IfaceMgr external socket ready callback handler.
Definition: ha_service.cc:3102
http::HttpClientPtr client_
HTTP client instance used to send HA commands and lease updates.
Definition: ha_service.h:1170
void updatePendingRequest(QueryPtrType &query)
Update pending request counter for this query.
Definition: ha_service.cc:1323
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:1082
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1836
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:2586
void pauseClientAndListener()
Pauses client and(or) listener thread pool operations.
Definition: ha_service.cc:3193
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:91
data::ConstElementPtr processMaintenanceNotify(const bool cancel)
Processes ha-maintenance-notify command and returns a response.
Definition: ha_service.cc:2594
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:692
void partnerInMaintenanceStateHandler()
Handler for "partner-in-maintenance" state.
Definition: ha_service.cc:555
Queue holding a backlog of unsent lease updates.
DHCP query filtering class.
Definition: query_filter.h:61
HTTP client class.
Definition: client.h:86
Implements a finite state machine.
Definition: state_model.h:274
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:304
boost::shared_ptr< CmdHttpListener > CmdHttpListenerPtr
Defines a shared pointer to CmdHttpListener.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:864
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:500
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:555
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:22
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:673
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:820
HAServerType
Lists possible server types for which HA service is created.
boost::shared_ptr< HAService > HAServicePtr
Pointer to the HAService class.
Definition: ha_service.h:1307
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:381
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:339
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
Defines the logger used by the top-level component of kea-lfc.
This file defines the class StateModel.