Kea 1.5.0
ha_service.h
Go to the documentation of this file.
1// Copyright (C) 2018 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 <query_filter.h>
14#include <asiolink/io_service.h>
15#include <cc/data.h>
16#include <dhcp/pkt4.h>
17#include <http/response.h>
18#include <dhcp/pkt4.h>
19#include <dhcpsrv/lease.h>
21#include <hooks/parking_lots.h>
22#include <http/client.h>
23#include <util/state_model.h>
24#include <boost/noncopyable.hpp>
25#include <boost/shared_ptr.hpp>
26#include <functional>
27#include <map>
28#include <vector>
29
30namespace isc {
31namespace ha {
32
38class HAService : public boost::noncopyable, public util::StateModel {
39public:
40
43
46
49
52
53protected:
54
60 typedef std::function<void(const bool, const std::string&)> PostRequestCallback;
61
69 typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
70
71public:
72
80 HAService(const asiolink::IOServicePtr& io_service,
81 const dhcp::NetworkStatePtr& network_state,
82 const HAConfigPtr& config,
83 const HAServerType& server_type = HAServerType::DHCPv4);
84
87 return (server_type_);
88 }
89
91 virtual void defineEvents();
92
94 virtual void verifyEvents();
95
97 virtual void defineStates();
98
111 void backupStateHandler();
112
142 void normalStateHandler();
143
169
185 void readyStateHandler();
186
203 void syncingStateHandler();
204
223
247 void waitingStateHandler();
248
249protected:
250
254 void verboseTransition(const unsigned state);
255
256public:
257
265 bool unpause();
266
267protected:
268
273 void conditionalLogPausedState() const;
274
275public:
276
281 void serveDefaultScopes();
282
296 bool inScope(dhcp::Pkt4Ptr& query4);
297
311 bool inScope(dhcp::Pkt6Ptr& query6);
312
313private:
314
328 template<typename QueryPtrType>
329 bool inScopeInternal(QueryPtrType& query);
330
331public:
332
337 void adjustNetworkState();
338
339protected:
340
354 bool shouldPartnerDown() const;
355
367 bool shouldTerminate() const;
368
369public:
370
404 size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
405 const dhcp::Lease4CollectionPtr& leases,
406 const dhcp::Lease4CollectionPtr& deleted_leases,
407 const hooks::ParkingLotHandlePtr& parking_lot);
408
409
428 size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
429 const dhcp::Lease6CollectionPtr& leases,
430 const dhcp::Lease6CollectionPtr& deleted_leases,
431 const hooks::ParkingLotHandlePtr& parking_lot);
432
433protected:
434
447 template<typename QueryPtrType>
448 void asyncSendLeaseUpdate(const QueryPtrType& query,
449 const HAConfig::PeerConfigPtr& config,
450 const data::ConstElementPtr& command,
451 const hooks::ParkingLotHandlePtr& parking_lot);
452
463 bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
464
465public:
466
491
492protected:
493
495 void asyncSendHeartbeat();
496
502 void scheduleHeartbeat();
503
505 void startHeartbeat();
506
519 const std::string& server_name,
520 const unsigned int max_period,
521 PostRequestCallback post_request_action);
522
533 const std::string& server_name,
534 PostRequestCallback post_request_action);
535
538
541
557 void asyncSyncLeases();
558
609 void asyncSyncLeases(http::HttpClient& http_client,
610 const std::string& server_name,
611 const unsigned int max_period,
612 const dhcp::LeasePtr& last_lease,
613 PostSyncCallback post_sync_action,
614 const bool dhcp_disabled = false);
615
643 const std::string& server_name,
644 const unsigned int max_period,
645 const dhcp::LeasePtr& last_lease,
646 PostSyncCallback post_sync_action,
647 const bool dhcp_disabled);
648
649
650public:
651
671 data::ConstElementPtr processSynchronize(const std::string& server_name,
672 const unsigned int max_period);
673
674protected:
675
689 int synchronize(std::string& status_message, const std::string& server_name,
690 const unsigned int max_period);
691
692public:
693
699 data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
700
705
706protected:
707
717
721
724
727
730
733
736
739
748 std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
749};
750
752typedef boost::shared_ptr<HAService> HAServicePtr;
753
754} // end of namespace isc::ha
755} // end of namespace isc
756
757#endif
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition: ha_config.h:159
High availability service.
Definition: ha_service.h:38
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:571
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:601
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat commannd.
Definition: ha_service.h:42
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:1170
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:753
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:491
http::HttpClient client_
HTTP client instance used to send lease updates.
Definition: ha_service.h:732
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:1504
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:726
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:729
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:1094
bool shouldTerminate() const
Indicates if the server should transition to the terminated state as a result of high clock skew.
Definition: ha_service.cc:657
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:1217
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:389
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:1496
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:1483
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:723
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:1395
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1009
std::map< boost::shared_ptr< dhcp::Pkt >, int > pending_requests_
Map holding a number of scheduled requests for a given packet.
Definition: ha_service.h:748
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:738
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:75
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:555
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:545
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:566
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:672
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchroniation succeeded.
Definition: ha_service.h:51
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:65
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:720
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:1160
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:735
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:1025
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:48
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:60
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:902
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:243
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:85
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:122
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:915
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:1403
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:137
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:410
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:871
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:1165
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:45
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:86
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:187
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:630
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1016
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:69
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:308
DHCP query filtering class.
Definition: query_filter.h:57
HTTP client class.
Definition: client.h:66
Implements a finite state machine.
Definition: state_model.h:271
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:458
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:29
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:608
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:31
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:509
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:752
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
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-dhcp-ddns.
This file defines the class StateModel.