Kea 2.5.8
ha_service.h
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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_sync_filter.h>
15#include <query_filter.h>
17#include <asiolink/io_service.h>
18#include <asiolink/tls_socket.h>
19#include <cc/data.h>
21#include <dhcp/pkt4.h>
22#include <http/response.h>
23#include <dhcpsrv/lease.h>
25#include <hooks/parking_lots.h>
26#include <http/client.h>
27#include <util/state_model.h>
28
29#include <boost/noncopyable.hpp>
30#include <boost/shared_ptr.hpp>
31#include <functional>
32#include <map>
33#include <mutex>
34#include <vector>
35
36namespace isc {
37namespace ha {
38
39class HAService;
40
43
45typedef boost::shared_ptr<HAServiceMapper> HAServiceMapperPtr;
46
52class HAService : public boost::noncopyable, public util::StateModel {
53public:
54
57
60
63
66
69
72
75
79
82
83protected:
84
91 typedef std::function<void(const bool, const std::string&, const int)> PostRequestCallback;
92
100 typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
101
102public:
103
116 HAService(const unsigned int id,
117 const asiolink::IOServicePtr& io_service,
118 const dhcp::NetworkStatePtr& network_state,
119 const HAConfigPtr& config,
120 const HAServerType& server_type = HAServerType::DHCPv4);
121
126 virtual ~HAService();
127
130 return (server_type_);
131 }
132
133private:
134
139 unsigned int getLocalOrigin() const {
141 }
142
147 unsigned int getRemoteOrigin() const {
149 }
150
158 std::string getCSCallbacksSetName() const;
159
161 virtual void defineEvents();
162
164 virtual void verifyEvents();
165
167 virtual void defineStates();
168
181 void backupStateHandler();
182
210 void communicationRecoveryHandler();
211
240 void normalStateHandler();
241
251 void inMaintenanceStateHandler();
252
277 void partnerDownStateHandler();
278
299 void partnerInMaintenanceStateHandler();
300
308 void passiveBackupStateHandler();
309
325 void readyStateHandler();
326
343 void syncingStateHandler();
344
362 void terminatedStateHandler();
363
387 void waitingStateHandler();
388
391 int getPartnerState() const {
392 return (communication_state_->getPartnerState());
393 }
394
395protected:
396
400 void verboseTransition(const unsigned state);
401
408 int getNormalState() const;
409
410public:
411
419 bool unpause();
420
421protected:
422
427 void conditionalLogPausedState() const;
428
429public:
430
435 void serveDefaultScopes();
436
441 void serveFailoverScopes();
442
456 bool inScope(dhcp::Pkt4Ptr& query4);
457
471 bool inScope(dhcp::Pkt6Ptr& query6);
472
473private:
474
488 template<typename QueryPtrType>
489 bool inScopeInternal(QueryPtrType& query);
490
491public:
492
497 void adjustNetworkState();
498
499protected:
500
514 bool shouldPartnerDown() const;
515
534 bool shouldTerminate() const;
535
540 bool isMaintenanceCanceled() const;
541
557 bool isPartnerStateInvalid() const;
558
559public:
560
594 size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
595 const dhcp::Lease4CollectionPtr& leases,
596 const dhcp::Lease4CollectionPtr& deleted_leases,
597 const hooks::ParkingLotHandlePtr& parking_lot);
598
615 size_t asyncSendSingleLeaseUpdate(const dhcp::Pkt4Ptr& query,
616 const dhcp::Lease4Ptr& lease,
617 const hooks::ParkingLotHandlePtr& parking_lot);
618
637 size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
638 const dhcp::Lease6CollectionPtr& leases,
639 const dhcp::Lease6CollectionPtr& deleted_leases,
640 const hooks::ParkingLotHandlePtr& parking_lot);
641
642protected:
643
656 template<typename QueryPtrType>
657 void asyncSendLeaseUpdate(const QueryPtrType& query,
658 const HAConfig::PeerConfigPtr& config,
659 const data::ConstElementPtr& command,
660 const hooks::ParkingLotHandlePtr& parking_lot);
661
671 void logFailedLeaseUpdates(const dhcp::PktPtr& query,
672 const data::ConstElementPtr& args) const;
673
684 bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
685
697 bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
698
699public:
700
726
732
746
747protected:
748
750 void asyncSendHeartbeat();
751
757 void scheduleHeartbeat();
758
760 void startHeartbeat();
761
773 void asyncDisableDHCPService(http::HttpClient& http_client,
774 const std::string& server_name,
775 const unsigned int max_period,
776 PostRequestCallback post_request_action);
777
787 void asyncEnableDHCPService(http::HttpClient& http_client,
788 const std::string& server_name,
789 PostRequestCallback post_request_action);
790
793
796
812 void asyncSyncLeases();
813
864 void asyncSyncLeases(http::HttpClient& http_client,
865 const std::string& server_name,
866 const unsigned int max_period,
867 const dhcp::LeasePtr& last_lease,
868 PostSyncCallback post_sync_action,
869 const bool dhcp_disabled = false);
870
897 void asyncSyncLeasesInternal(http::HttpClient& http_client,
898 const std::string& server_name,
899 const unsigned int max_period,
900 const dhcp::LeasePtr& last_lease,
901 PostSyncCallback post_sync_action,
902 const bool dhcp_disabled);
903
904public:
905
925 data::ConstElementPtr processSynchronize(const std::string& server_name,
926 const unsigned int max_period);
927
928protected:
929
950 int synchronize(std::string& status_message, const std::string& server_name,
951 const unsigned int max_period);
952
971 void asyncSendLeaseUpdatesFromBacklog(http::HttpClient& http_client,
972 const HAConfig::PeerConfigPtr& remote_config,
973 PostRequestCallback post_request_action);
974
989
996 void asyncSendHAReset(http::HttpClient& http_client,
997 const HAConfig::PeerConfigPtr& remote_config,
998 PostRequestCallback post_request_action);
999
1012 bool sendHAReset();
1013
1014public:
1015
1021 data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
1022
1027
1049
1067
1084
1091
1092protected:
1093
1103 void asyncSyncCompleteNotify(http::HttpClient& http_client,
1104 const std::string& server_name,
1105 PostRequestCallback post_request_action);
1106
1107public:
1108
1122 data::ConstElementPtr processSyncCompleteNotify(const unsigned int origin);
1123
1132
1140
1148
1154 void stopClientAndListener();
1155
1156protected:
1157
1173 int& rcode);
1174
1184 bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd);
1185
1191 bool clientHandshakeHandler(const boost::system::error_code&) {
1192 return (true);
1193 }
1194
1211 void socketReadyHandler(int tcp_native_fd);
1212
1220 void clientCloseHandler(int tcp_native_fd);
1221
1223 unsigned int id_;
1224
1228
1231
1234
1237
1240
1244
1247
1250
1253
1270 template<typename QueryPtrType>
1271 bool leaseUpdateComplete(QueryPtrType& query,
1272 const hooks::ParkingLotHandlePtr& parking_lot);
1273
1279 template<typename QueryPtrType>
1280 void updatePendingRequest(QueryPtrType& query);
1281
1286 size_t pendingRequestSize();
1287
1297 template<typename QueryPtrType>
1298 int getPendingRequest(const QueryPtrType& query);
1299
1300private:
1319 template<typename QueryPtrType>
1320 bool leaseUpdateCompleteInternal(QueryPtrType& query,
1321 const hooks::ParkingLotHandlePtr& parking_lot);
1322
1330 template<typename QueryPtrType>
1331 void updatePendingRequestInternal(QueryPtrType& query);
1332
1343 template<typename QueryPtrType>
1344 int getPendingRequestInternal(const QueryPtrType& query);
1345
1347 std::mutex mutex_;
1348
1357 std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
1358
1359protected:
1360
1367
1376};
1377
1379typedef boost::shared_ptr<HAService> HAServicePtr;
1380
1381} // end of namespace isc::ha
1382} // end of namespace isc
1383
1384#endif
static const unsigned int HA_LOCAL_COMMAND
The network state is being altered by an HA internal command.
Definition: network_state.h:96
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:242
Holds associations between objects and HA relationships.
High availability service.
Definition: ha_service.h:52
static const int HA_MAINTENANCE_START_EVT
ha-maintenance-start command received.
Definition: ha_service.h:71
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:1047
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:1077
void stopClientAndListener()
Stop the client and(or) listener instances.
Definition: ha_service.cc:3391
int getNormalState() const
Returns normal operation state for the current configuration.
Definition: ha_service.cc:998
bool shouldQueueLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be queued.
Definition: ha_service.cc:1577
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat command.
Definition: ha_service.h:56
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:2616
bool clientConnectHandler(const boost::system::error_code &ec, int tcp_native_fd)
HttpClient connect callback handler.
Definition: ha_service.cc:3230
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:2072
bool isMaintenanceCanceled() const
Convenience method checking if the current state is a result of canceling the maintenance.
Definition: ha_service.cc:1152
data::ConstElementPtr processMaintenanceCancel()
Processes ha-maintenance-cancel command and returns a response.
Definition: ha_service.cc:2894
void checkPermissionsClientAndListener()
Check client and(or) listener current thread permissions to perform thread pool state transition.
Definition: ha_service.cc:3308
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:1387
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:927
bool sendLeaseUpdatesFromBacklog()
Attempts to send all lease updates from the backlog synchronously.
Definition: ha_service.cc:2564
config::CmdHttpListenerPtr listener_
HTTP listener instance used to receive and respond to HA commands and lease updates.
Definition: ha_service.h:1243
void clientCloseHandler(int tcp_native_fd)
HttpClient close callback handler.
Definition: ha_service.cc:3270
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:3001
bool leaseUpdateComplete(QueryPtrType &query, const hooks::ParkingLotHandlePtr &parking_lot)
Handle last pending request for this query.
Definition: ha_service.cc:1331
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:1233
data::ConstElementPtr processMaintenanceStart()
Processes ha-maintenance-start command and returns a response.
Definition: ha_service.cc:2753
unsigned int id_
Unique service id.
Definition: ha_service.h:1223
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:1236
bool sync_complete_notified_
An indicator that a partner sent ha-sync-complete-notify command.
Definition: ha_service.h:1375
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:1982
data::ConstElementPtr processSyncCompleteNotify(const unsigned int origin)
Process ha-sync-complete-notify command and returns a response.
Definition: ha_service.cc:3086
bool shouldTerminate() const
Indicates if the server should transition to the terminated state.
Definition: ha_service.cc:1136
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:2120
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:2696
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:1230
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:2340
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1883
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:1249
static const int HA_MAINTENANCE_NOTIFY_EVT
ha-maintenance-notify command received.
Definition: ha_service.h:68
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:78
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:1025
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:1014
static const int HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED
Control result returned in response to ha-maintenance-notify.
Definition: ha_service.h:81
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:1037
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:1190
size_t pendingRequestSize()
Get the number of entries in the pending request map.
Definition: ha_service.cc:3277
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchronization succeeded.
Definition: ha_service.h:65
bool sendHAReset()
Sends ha-reset command to partner synchronously.
Definition: ha_service.cc:2669
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:91
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:1227
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:2062
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:1246
void logFailedLeaseUpdates(const dhcp::PktPtr &query, const data::ConstElementPtr &args) const
Log failed lease updates.
Definition: ha_service.cc:1590
bool clientHandshakeHandler(const boost::system::error_code &)
HttpClient handshake callback handler.
Definition: ha_service.h:1191
LeaseUpdateBacklog lease_update_backlog_
Backlog of DHCP lease updates.
Definition: ha_service.h:1366
virtual ~HAService()
Destructor.
Definition: ha_service.cc:145
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:1899
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:62
static const int HA_MAINTENANCE_CANCEL_EVT
ha-maintenance-cancel command received.
Definition: ha_service.h:74
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:2486
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:1693
data::ConstElementPtr processHAReset()
Processes ha-reset command and returns a response.
Definition: ha_service.cc:1716
size_t asyncSendSingleLeaseUpdate(const dhcp::Pkt4Ptr &query, const dhcp::Lease4Ptr &lease, const hooks::ParkingLotHandlePtr &parking_lot)
Schedules an asynchronous IPv4 lease update.
Definition: ha_service.cc:1259
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:1726
bool isPartnerStateInvalid() const
Indicates if the partner's state is invalid.
Definition: ha_service.cc:1157
void startClientAndListener()
Start the client and(or) listener instances.
Definition: ha_service.cc:3337
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response, int &rcode)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:3107
void resumeClientAndListener()
Resumes client and(or) listener thread pool operations.
Definition: ha_service.cc:3372
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:2348
data::ConstElementPtr processStatusGet() const
Processes status-get command and returns a response.
Definition: ha_service.cc:1642
int getPendingRequest(const QueryPtrType &query)
Get the number of scheduled requests for a given query.
Definition: ha_service.cc:3288
LeaseSyncFilter lease_sync_filter_
Lease synchronization filter used in hub-and-spoke model.
Definition: ha_service.h:1252
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:1545
void serveFailoverScopes()
Instructs the HA service to serve failover scopes.
Definition: ha_service.cc:1042
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:2067
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:59
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:129
void socketReadyHandler(int tcp_native_fd)
IfaceMgr external socket ready callback handler.
Definition: ha_service.cc:3261
http::HttpClientPtr client_
HTTP client instance used to send HA commands and lease updates.
Definition: ha_service.h:1239
void updatePendingRequest(QueryPtrType &query)
Update pending request counter for this query.
Definition: ha_service.cc:1366
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:1109
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1890
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:2709
void pauseClientAndListener()
Pauses client and(or) listener thread pool operations.
Definition: ha_service.cc:3354
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:100
data::ConstElementPtr processMaintenanceNotify(const bool cancel)
Processes ha-maintenance-notify command and returns a response.
Definition: ha_service.cc:2717
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.
Definition: query_filter.h:61
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:982
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:25
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:31
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:292
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:42
boost::shared_ptr< HAServiceMapper > HAServiceMapperPtr
Pointer to an object mapping HAService to relationships.
Definition: ha_service.h:45
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:37
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:1379
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:81
Defines the logger used by the top-level component of kea-lfc.
This file defines the class StateModel.