Kea 1.9.11
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>
14#include <query_filter.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>
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
35namespace isc {
36namespace ha {
37
43class HAService : public boost::noncopyable, public util::StateModel {
44public:
45
48
51
54
57
60
63
66
69
70protected:
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
88public:
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
320
344 void waitingStateHandler();
345
348 int getPartnerState() const {
349 return (communication_state_->getPartnerState());
350 }
351
352protected:
353
357 void verboseTransition(const unsigned state);
358
365 int getNormalState() const;
366
367public:
368
376 bool unpause();
377
378protected:
379
384 void conditionalLogPausedState() const;
385
386public:
387
392 void serveDefaultScopes();
393
407 bool inScope(dhcp::Pkt4Ptr& query4);
408
422 bool inScope(dhcp::Pkt6Ptr& query6);
423
424private:
425
439 template<typename QueryPtrType>
440 bool inScopeInternal(QueryPtrType& query);
441
442public:
443
448 void adjustNetworkState();
449
450protected:
451
465 bool shouldPartnerDown() const;
466
478 bool shouldTerminate() const;
479
484 bool isMaintenanceCanceled() const;
485
501 bool isPartnerStateInvalid() const;
502
503public:
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
566protected:
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
623public:
624
650
656
670
671protected:
672
674 void asyncSendHeartbeat();
675
681 void scheduleHeartbeat();
682
684 void startHeartbeat();
685
698 const std::string& server_name,
699 const unsigned int max_period,
700 PostRequestCallback post_request_action);
701
712 const std::string& server_name,
713 PostRequestCallback post_request_action);
714
717
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
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
828public:
829
849 data::ConstElementPtr processSynchronize(const std::string& server_name,
850 const unsigned int max_period);
851
852protected:
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
934public:
935
941 data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
942
947
969
987
1004
1011
1020
1028
1036
1042 void stopClientAndListener();
1043
1044protected:
1045
1056 int& rcode);
1057
1067 bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd);
1068
1074 bool clientHandshakeHandler(const boost::system::error_code&) {
1075 return (true);
1076 }
1077
1094 void socketReadyHandler(int tcp_native_fd);
1095
1103 void clientCloseHandler(int tcp_native_fd);
1104
1108
1111
1114
1117
1120
1124
1127
1130
1147 template<typename QueryPtrType>
1148 bool leaseUpdateComplete(QueryPtrType& query,
1149 const hooks::ParkingLotHandlePtr& parking_lot);
1150
1156 template<typename QueryPtrType>
1157 void updatePendingRequest(QueryPtrType& query);
1158
1163 size_t pendingRequestSize();
1164
1174 template<typename QueryPtrType>
1175 int getPendingRequest(const QueryPtrType& query);
1176
1177private:
1196 template<typename QueryPtrType>
1197 bool leaseUpdateCompleteInternal(QueryPtrType& query,
1198 const hooks::ParkingLotHandlePtr& parking_lot);
1199
1207 template<typename QueryPtrType>
1208 void updatePendingRequestInternal(QueryPtrType& query);
1209
1220 template<typename QueryPtrType>
1221 int getPendingRequestInternal(const QueryPtrType& query);
1222
1224 std::mutex mutex_;
1225
1234 std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
1235
1236protected:
1237
1244};
1245
1247typedef boost::shared_ptr<HAService> HAServicePtr;
1248
1249} // end of namespace isc::ha
1250} // end of namespace isc
1251
1252#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:965
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:995
void stopClientAndListener()
Stop the client and(or) listener instances.
Definition: ha_service.cc:2897
int getNormalState() const
Returns normal operation state for the current configuration.
Definition: ha_service.cc:923
bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be queued.
Definition: ha_service.cc:1435
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:2329
bool clientConnectHandler(const boost::system::error_code &ec, int tcp_native_fd)
HttpClient connect callback handler.
Definition: ha_service.cc:2739
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:1882
bool isMaintenanceCanceled() const
Convenience method checking if the current state is a result of canceling the maintenance.
Definition: ha_service.cc:1067
data::ConstElementPtr processMaintenanceCancel()
Processes ha-maintenance-cancel command and returns a response.
Definition: ha_service.cc:2585
void checkPermissionsClientAndListener()
Check client and(or) listener current thread permissions to perform thread pool state transition.
Definition: ha_service.cc:2817
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
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:858
bool sendLeaseUpdatesFromBacklog()
Attempts to send all lease updates from the backlog synchronously.
Definition: ha_service.cc:2288
config::CmdHttpListenerPtr listener_
HTTP listener instance used to receive and respond to HA commands and lease updates.
Definition: ha_service.h:1123
void clientCloseHandler(int tcp_native_fd)
HttpClient close callback handler.
Definition: ha_service.cc:2779
bool leaseUpdateComplete(QueryPtrType &query, const hooks::ParkingLotHandlePtr &parking_lot)
Handle last pending request for this query.
Definition: ha_service.cc:1216
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:1113
data::ConstElementPtr processMaintenanceStart()
Processes ha-maintenance-start command and returns a response.
Definition: ha_service.cc:2455
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:1116
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 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 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
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:729
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:2398
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:1110
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
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
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1701
void passiveBackupStateHandler()
Handler for "passive-backup" state.
Definition: ha_service.cc:543
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:1129
static const int HA_MAINTENANCE_NOTIFY_EVT
ha-maintenance-notify command received.
Definition: ha_service.h:59
void inMaintenanceStateHandler()
Handler for the "in-maintenance" state.
Definition: ha_service.cc:402
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:129
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:949
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:939
static const int HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED
Control result returned in response to ha-maintenance-notify.
Definition: ha_service.h:68
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:960
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
size_t pendingRequestSize()
Get the number of entries in the pending request map.
Definition: ha_service.cc:2786
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:2379
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:116
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:1107
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:1872
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:1126
void logFailedLeaseUpdates(const dhcp::PktPtr &query, const data::ConstElementPtr &args) const
Log failed lease updates.
Definition: ha_service.cc:1448
bool clientHandshakeHandler(const boost::system::error_code &)
HttpClient handshake callback handler.
Definition: ha_service.h:1074
LeaseUpdateBacklog lease_update_backlog_
Backlog of DHCP lease updates.
Definition: ha_service.h:1243
virtual ~HAService()
Destructor.
Definition: ha_service.cc:108
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
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
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
int getPartnerState() const
Returns last known state of the partner.
Definition: ha_service.h:348
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
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:1549
data::ConstElementPtr processHAReset()
Processes ha-reset command and returns a response.
Definition: ha_service.cc:1569
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:561
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:142
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:195
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:1579
void communicationRecoveryHandler()
Handler for the "communication-recovery" state.
Definition: ha_service.cc:210
bool isPartnerStateInvalid() const
Indicates if the partner's state is invalid.
Definition: ha_service.cc:1072
void startClientAndListener()
Start the client and(or) listener instances.
Definition: ha_service.cc:2844
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response, int &rcode)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:2681
void resumeClientAndListener()
Resumes client and(or) listener thread pool operations.
Definition: ha_service.cc:2879
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
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:327
data::ConstElementPtr processStatusGet() const
Processes status-get command and returns a response.
Definition: ha_service.cc:1500
int getPendingRequest(const QueryPtrType &query)
Get the number of scheduled requests for a given query.
Definition: ha_service.cc:2797
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:750
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
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:1877
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:113
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:427
void socketReadyHandler(int tcp_native_fd)
IfaceMgr external socket ready callback handler.
Definition: ha_service.cc:2770
http::HttpClientPtr client_
HTTP client instance used to send HA commands and lease updates.
Definition: ha_service.h:1119
void updatePendingRequest(QueryPtrType &query)
Update pending request counter for this query.
Definition: ha_service.cc:1249
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:1027
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1708
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:2411
void pauseClientAndListener()
Pauses client and(or) listener thread pool operations.
Definition: ha_service.cc:2861
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:86
data::ConstElementPtr processMaintenanceNotify(const bool cancel)
Processes ha-maintenance-notify command and returns a response.
Definition: ha_service.cc:2419
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:640
void partnerInMaintenanceStateHandler()
Handler for "partner-in-maintenance" state.
Definition: ha_service.cc:504
Queue holding a backlog of unsent lease updates.
DHCP query filtering class.
Definition: query_filter.h:61
HTTP client class.
Definition: client.h:87
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:27
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:797
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:490
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
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:644
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:760
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:1247
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:375
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
Definition: client.h:337
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.