Kea 3.3.3
ha_service.h
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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
11#include <asiolink/io_service.h>
12#include <asiolink/tls_socket.h>
13#include <cc/data.h>
15#include <dhcp/pkt4.h>
16#include <dhcpsrv/lease.h>
24#include <hooks/parking_lots.h>
25#include <http/client.h>
26#include <http/response.h>
27#include <util/state_model.h>
28
29#include <functional>
30#include <map>
31#include <mutex>
32#include <vector>
33
34#include <boost/noncopyable.hpp>
35#include <boost/shared_ptr.hpp>
36
37namespace isc {
38namespace ha {
39
40class HAService;
41
44
46typedef boost::shared_ptr<HAServiceMapper> HAServiceMapperPtr;
47
53class HAService : public boost::noncopyable, public util::StateModel {
54public:
55
58
61
64
67
70
73
76
80
83
87
88protected:
89
96 typedef std::function<void(const bool, const std::string&, const int)> PostRequestCallback;
97
105 typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
106
107public:
108
121 HAService(const unsigned int id,
122 const asiolink::IOServicePtr& io_service,
123 const dhcp::NetworkStatePtr& network_state,
124 const HAConfigPtr& config,
125 const HAServerType& server_type = HAServerType::DHCPv4);
126
131 virtual ~HAService();
132
135 return (server_type_);
136 }
137
138private:
139
144 unsigned int getLocalOrigin() const {
146 }
147
152 unsigned int getRemoteOrigin() const {
154 }
155
163 std::string getCSCallbacksSetName() const;
164
166 virtual void defineEvents();
167
169 virtual void verifyEvents();
170
172 virtual void defineStates();
173
186 void backupStateHandler();
187
215 void communicationRecoveryHandler();
216
246 void normalStateHandler();
247
257 void inMaintenanceStateHandler();
258
283 void partnerDownStateHandler();
284
305 void partnerInMaintenanceStateHandler();
306
314 void passiveBackupStateHandler();
315
331 void readyStateHandler();
332
349 void syncingStateHandler();
350
368 void terminatedStateHandler();
369
393 void waitingStateHandler();
394
397 int getPartnerState() const {
398 return (communication_state_->getPartnerState());
399 }
400
401protected:
402
406 void verboseTransition(const unsigned state);
407
414 int getNormalState() const;
415
416public:
417
425 bool unpause();
426
427protected:
428
433 void conditionalLogPausedState() const;
434
435public:
436
441 void serveDefaultScopes();
442
447 void serveFailoverScopes();
448
462 bool inScope(dhcp::Pkt4Ptr& query4);
463
477 bool inScope(dhcp::Pkt6Ptr& query6);
478
479private:
480
494 template<typename QueryPtrType>
495 bool inScopeInternal(QueryPtrType& query);
496
497public:
498
508 bool shouldReclaim(const dhcp::Lease4Ptr& lease4) const;
509
519 bool shouldReclaim(const dhcp::Lease6Ptr& lease6) const;
520
521private:
522
533 template<typename LeaseTypePtr>
534 bool shouldReclaimInternal(const LeaseTypePtr& lease) const;
535
536public:
537
542 void adjustNetworkState();
543
544protected:
545
559 bool shouldPartnerDown() const;
560
579 bool shouldTerminate() const;
580
585 bool isMaintenanceCanceled() const;
586
602 bool isPartnerStateInvalid() const;
603
604public:
605
639 size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
640 const dhcp::Lease4CollectionPtr& leases,
641 const dhcp::Lease4CollectionPtr& deleted_leases,
642 const hooks::ParkingLotHandlePtr& parking_lot);
643
660 size_t asyncSendSingleLeaseUpdate(const dhcp::Pkt4Ptr& query,
661 const dhcp::Lease4Ptr& lease,
662 const hooks::ParkingLotHandlePtr& parking_lot);
663
682 size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
683 const dhcp::Lease6CollectionPtr& leases,
684 const dhcp::Lease6CollectionPtr& deleted_leases,
685 const hooks::ParkingLotHandlePtr& parking_lot);
686
687protected:
688
701 template<typename QueryPtrType>
702 void asyncSendLeaseUpdate(const QueryPtrType& query,
703 const HAConfig::PeerConfigPtr& config,
704 const data::ConstElementPtr& command,
705 const hooks::ParkingLotHandlePtr& parking_lot);
706
716 void logFailedLeaseUpdates(const dhcp::PktPtr& query,
717 const data::ConstElementPtr& args) const;
718
729 bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
730
742 bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
743
744public:
745
771
777
791
792protected:
793
795 void asyncSendHeartbeat();
796
802 void scheduleHeartbeat();
803
805 void startHeartbeat();
806
818 void asyncDisableDHCPService(http::HttpClient& http_client,
819 const HAConfig::PeerConfigPtr& remote_config,
820 const unsigned int max_period,
821 PostRequestCallback post_request_action);
822
832 void asyncEnableDHCPService(http::HttpClient& http_client,
833 const HAConfig::PeerConfigPtr& remote_config,
834 PostRequestCallback post_request_action);
835
838
841
857 void asyncSyncLeases();
858
909 void asyncSyncLeases(http::HttpClient& http_client,
910 const HAConfig::PeerConfigPtr& remote_config,
911 const unsigned int max_period,
912 const dhcp::LeasePtr& last_lease,
913 PostSyncCallback post_sync_action,
914 const bool dhcp_disabled = false);
915
942 void asyncSyncLeasesInternal(http::HttpClient& http_client,
943 const HAConfig::PeerConfigPtr& remote_config,
944 const unsigned int max_period,
945 const dhcp::LeasePtr& last_lease,
946 PostSyncCallback post_sync_action,
947 const bool dhcp_disabled);
948
949public:
950
970 data::ConstElementPtr processSynchronize(const std::string& server_name,
971 const unsigned int max_period);
972
973protected:
974
995 int synchronize(std::string& status_message,
996 const HAConfig::PeerConfigPtr& remote_config,
997 const unsigned int max_period);
998
1017 void asyncSendLeaseUpdatesFromBacklog(http::HttpClient& http_client,
1018 const HAConfig::PeerConfigPtr& remote_config,
1019 PostRequestCallback post_request_action);
1020
1035
1042 void asyncSendHAReset(http::HttpClient& http_client,
1043 const HAConfig::PeerConfigPtr& remote_config,
1044 PostRequestCallback post_request_action);
1045
1058 bool sendHAReset();
1059
1060public:
1061
1067 data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
1068
1073
1097 const std::string& state);
1098
1116
1133
1140
1141protected:
1142
1152 void asyncSyncCompleteNotify(http::HttpClient& http_client,
1153 const HAConfig::PeerConfigPtr& remote_config,
1154 PostRequestCallback post_request_action);
1155
1156public:
1157
1171 data::ConstElementPtr processSyncCompleteNotify(const unsigned int origin_id);
1172
1181
1189
1197
1203 void stopClientAndListener();
1204
1205protected:
1206
1222 int& rcode);
1223
1233 bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd);
1234
1240 bool clientHandshakeHandler(const boost::system::error_code&) {
1241 return (true);
1242 }
1243
1260 void socketReadyHandler(int tcp_native_fd);
1261
1269 void clientCloseHandler(int tcp_native_fd);
1270
1272 unsigned int id_;
1273
1277
1280
1283
1286
1289
1293
1296
1299
1302
1319 template<typename QueryPtrType>
1320 bool leaseUpdateComplete(QueryPtrType& query,
1321 const hooks::ParkingLotHandlePtr& parking_lot);
1322
1328 template<typename QueryPtrType>
1329 void updatePendingRequest(QueryPtrType& query);
1330
1335 size_t pendingRequestSize();
1336
1346 template<typename QueryPtrType>
1347 int getPendingRequest(const QueryPtrType& query);
1348
1349private:
1368 template<typename QueryPtrType>
1369 bool leaseUpdateCompleteInternal(QueryPtrType& query,
1370 const hooks::ParkingLotHandlePtr& parking_lot);
1371
1379 template<typename QueryPtrType>
1380 void updatePendingRequestInternal(QueryPtrType& query);
1381
1392 template<typename QueryPtrType>
1393 int getPendingRequestInternal(const QueryPtrType& query);
1394
1396 std::mutex mutex_;
1397
1406 std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
1407
1408protected:
1409
1416
1425};
1426
1428typedef boost::shared_ptr<HAService> HAServicePtr;
1429
1430} // end of namespace isc::ha
1431} // end of namespace isc
1432
1433#endif
static const unsigned int HA_LOCAL_COMMAND
The network state is being altered by an HA internal command.
static const unsigned int HA_REMOTE_COMMAND
The network state is being altered by a "dhcp-disable" or "dhcp-enable" command sent by a HA partner.
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition ha_config.h:255
Holds associations between objects and HA relationships.
High availability service.
Definition ha_service.h:53
static const int HA_MAINTENANCE_START_EVT
ha-maintenance-start command received.
Definition ha_service.h:72
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
void stopClientAndListener()
Stop the client and(or) listener instances.
int getNormalState() const
Returns normal operation state for the current configuration.
bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be queued.
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat command.
Definition ha_service.h:57
void asyncSendHAReset(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Sends ha-reset command to partner asynchronously.
bool clientConnectHandler(const boost::system::error_code &ec, int tcp_native_fd)
HttpClient connect callback handler.
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
bool isMaintenanceCanceled() const
Convenience method checking if the current state is a result of canceling the maintenance.
data::ConstElementPtr processMaintenanceCancel()
Processes ha-maintenance-cancel command and returns a response.
void checkPermissionsClientAndListener()
Check client and(or) listener current thread permissions to perform thread pool state transition.
bool shouldReclaim(const dhcp::Lease4Ptr &lease4) const
Checks if the lease should be reclaimed by this server.
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.
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
bool sendLeaseUpdatesFromBacklog()
Attempts to send all lease updates from the backlog synchronously.
config::CmdHttpListenerPtr listener_
HTTP listener instance used to receive and respond to HA commands and lease updates.
void clientCloseHandler(int tcp_native_fd)
HttpClient close callback handler.
bool leaseUpdateComplete(QueryPtrType &query, const hooks::ParkingLotHandlePtr &parking_lot)
Handle last pending request for this query.
HAConfigPtr config_
Pointer to the HA hooks library configuration.
data::ConstElementPtr processMaintenanceStart()
Processes ha-maintenance-start command and returns a response.
unsigned int id_
Unique service id.
HAServerType server_type_
DHCP server type.
bool sync_complete_notified_
An indicator that a partner sent ha-sync-complete-notify command.
bool shouldTerminate() const
Indicates if the server should transition to the terminated state.
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
data::ConstElementPtr processSynchronize(const std::string &server_name, const unsigned int max_period)
Processes ha-sync command and returns a response.
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
void asyncSyncCompleteNotify(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Schedules asynchronous "ha-sync-complete-notify" command to the specified server.
QueryFilter query_filter_
Selects queries to be processed/dropped.
static const int HA_MAINTENANCE_NOTIFY_EVT
ha-maintenance-notify command received.
Definition ha_service.h:69
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:79
data::ConstElementPtr processMaintenanceNotify(const bool cancel, const std::string &state)
Processes ha-maintenance-notify command and returns a response.
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
bool unpause()
Unpauses the HA state machine with logging.
static const int HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED
Control result returned in response to ha-maintenance-notify.
Definition ha_service.h:82
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
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.
size_t pendingRequestSize()
Get the number of entries in the pending request map.
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchronization succeeded.
Definition ha_service.h:66
bool sendHAReset()
Sends ha-reset command to partner synchronously.
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:96
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
void localDisableDHCPService()
Disables local DHCP service.
CommunicationStatePtr communication_state_
Holds communication state with a peer.
void logFailedLeaseUpdates(const dhcp::PktPtr &query, const data::ConstElementPtr &args) const
Log failed lease updates.
bool clientHandshakeHandler(const boost::system::error_code &)
HttpClient handshake callback handler.
LeaseUpdateBacklog lease_update_backlog_
Backlog of DHCP lease updates.
virtual ~HAService()
Destructor.
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition ha_service.h:63
static const int HA_MAINTENANCE_CANCEL_EVT
ha-maintenance-cancel command received.
Definition ha_service.h:75
void asyncSendLeaseUpdatesFromBacklog(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Sends lease updates from backlog to partner asynchronously.
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
void asyncSyncLeasesInternal(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, 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.
data::ConstElementPtr processHAReset()
Processes ha-reset command and returns a response.
size_t asyncSendSingleLeaseUpdate(const dhcp::Pkt4Ptr &query, const dhcp::Lease4Ptr &lease, const hooks::ParkingLotHandlePtr &parking_lot)
Schedules an asynchronous IPv4 lease update.
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
bool isPartnerStateInvalid() const
Indicates if the partner's state is invalid.
void startClientAndListener()
Start the client and(or) listener instances.
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response, int &rcode)
Checks if the response is valid or contains an error.
void resumeClientAndListener()
Resumes client and(or) listener thread pool operations.
data::ConstElementPtr processStatusGet() const
Processes status-get command and returns a response.
int getPendingRequest(const QueryPtrType &query)
Get the number of scheduled requests for a given query.
LeaseSyncFilter lease_sync_filter_
Lease synchronization filter used in hub-and-spoke model.
int synchronize(std::string &status_message, const HAConfig::PeerConfigPtr &remote_config, const unsigned int max_period)
Synchronizes lease database with a partner.
bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be sent as result of leases allocation or release.
void serveFailoverScopes()
Instructs the HA service to serve failover scopes.
void localEnableDHCPService()
Enables local DHCP service.
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition ha_service.h:60
HAService(const unsigned int id, const asiolink::IOServicePtr &io_service, const dhcp::NetworkStatePtr &network_state, const HAConfigPtr &config, const HAServerType &server_type=HAServerType::DHCPv4)
Constructor.
Definition ha_service.cc:79
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition ha_service.h:134
void socketReadyHandler(int tcp_native_fd)
IfaceMgr external socket ready callback handler.
http::HttpClientPtr client_
HTTP client instance used to send HA commands and lease updates.
void updatePendingRequest(QueryPtrType &query)
Update pending request counter for this query.
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
data::ConstElementPtr processSyncCompleteNotify(const unsigned int origin_id)
Process ha-sync-complete-notify command and returns a response.
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
void asyncDisableDHCPService(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, const unsigned int max_period, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-disable" command to the specified server.
void pauseClientAndListener()
Pauses client and(or) listener thread pool operations.
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition ha_service.h:105
static const int HA_WAITING_TO_TERMINATED_ST_DELAY_MINUTES
A delay in minutes to transition from the waiting to terminated state when the partner remains in ter...
Definition ha_service.h:86
void asyncEnableDHCPService(http::HttpClient &http_client, const HAConfig::PeerConfigPtr &remote_config, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-enable" command to the specified server.
Checks if a lease fetched from the other server should be synchronized into the local lease database.
Queue holding a backlog of unsent lease updates.
DHCP query filtering class.
Implements a finite state machine.
virtual void defineEvents()
Populates the set of events.
virtual void verifyEvents()
Validates the contents of the set of events.
virtual void defineStates()
Populates the set of states.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
boost::shared_ptr< CmdHttpListener > CmdHttpListenerPtr
Defines a shared pointer to CmdHttpListener.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:1006
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition lease.h:551
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:556
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:27
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:724
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition pkt6.h:31
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:343
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
HARelationshipMapper< HAService > HAServiceMapper
Type of an object mapping HAService to relationships.
Definition ha_service.h:43
boost::shared_ptr< HAServiceMapper > HAServiceMapperPtr
Pointer to an object mapping HAService to relationships.
Definition ha_service.h:46
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition ha_config.h:39
HAServerType
Lists possible server types for which HA service is created.
boost::shared_ptr< HAService > HAServicePtr
Pointer to the HAService class.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
boost::shared_ptr< HttpClient > HttpClientPtr
Defines a pointer to an HttpClient instance.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition response.h:82
Defines the logger used by the top-level component of kea-lfc.
This file defines the class StateModel.