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>
20 #include <dhcpsrv/network_state.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 
30 namespace isc {
31 namespace ha {
32 
38 class HAService : public boost::noncopyable, public util::StateModel {
39 public:
40 
43 
46 
49 
52 
53 protected:
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 
71 public:
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 
222  void terminatedStateHandler();
223 
247  void waitingStateHandler();
248 
249 protected:
250 
254  void verboseTransition(const unsigned state);
255 
256 public:
257 
265  bool unpause();
266 
267 protected:
268 
273  void conditionalLogPausedState() const;
274 
275 public:
276 
281  void serveDefaultScopes();
282 
296  bool inScope(dhcp::Pkt4Ptr& query4);
297 
311  bool inScope(dhcp::Pkt6Ptr& query6);
312 
313 private:
314 
328  template<typename QueryPtrType>
329  bool inScopeInternal(QueryPtrType& query);
330 
331 public:
332 
337  void adjustNetworkState();
338 
339 protected:
340 
354  bool shouldPartnerDown() const;
355 
367  bool shouldTerminate() const;
368 
369 public:
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 
433 protected:
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 
465 public:
466 
491 
492 protected:
493 
495  void asyncSendHeartbeat();
496 
502  void scheduleHeartbeat();
503 
505  void startHeartbeat();
506 
518  void asyncDisableDHCPService(http::HttpClient& http_client,
519  const std::string& server_name,
520  const unsigned int max_period,
521  PostRequestCallback post_request_action);
522 
532  void asyncEnableDHCPService(http::HttpClient& http_client,
533  const std::string& server_name,
534  PostRequestCallback post_request_action);
535 
538 
540  void localEnableDHCPService();
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 
642  void asyncSyncLeasesInternal(http::HttpClient& http_client,
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 
650 public:
651 
671  data::ConstElementPtr processSynchronize(const std::string& server_name,
672  const unsigned int max_period);
673 
674 protected:
675 
689  int synchronize(std::string& status_message, const std::string& server_name,
690  const unsigned int max_period);
691 
692 public:
693 
699  data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
700 
705 
706 protected:
707 
717 
721 
724 
727 
730 
733 
736 
739 
748  std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
749 };
750 
752 typedef boost::shared_ptr<HAService> HAServicePtr;
753 
754 } // end of namespace isc::ha
755 } // end of namespace isc
756 
757 #endif
isc::dhcp::NetworkStatePtr
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
Definition: network_state.h:156
isc::ha::HAService::inScope
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:571
isc::ha::HAService::HA_SYNCING_SUCCEEDED_EVT
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchroniation succeeded.
Definition: ha_service.h:51
isc::ha::HAService::HA_SYNCING_FAILED_EVT
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:48
isc::http::HttpClient
HTTP client class.
Definition: client.h:66
isc::ha::HAService::PostRequestCallback
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
isc::ha::HAService::defineEvents
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:65
isc::ha::HAService::pending_requests_
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
isc::ha::HAService::HA_HEARTBEAT_COMPLETE_EVT
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat commannd.
Definition: ha_service.h:42
pkt4.h
isc::ha::HAService::syncingStateHandler
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:308
isc::ha::HAService::client_
http::HttpClient client_
HTTP client instance used to send lease updates.
Definition: ha_service.h:732
isc::ha::HAService::waitingStateHandler
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:410
parking_lots.h
isc::ha::HAService::verboseTransition
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:491
isc::ha::HAService::readyStateHandler
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:243
ha_config.h
isc::ha::HAService::asyncSyncLeases
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:1170
isc::ha::HAService::localDisableDHCPService
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:1160
isc::ha::HAService::localEnableDHCPService
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:1165
isc::ha::HAService::adjustNetworkState
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:601
isc::ha::HAService::synchronize
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
ha_server_type.h
isc::ha::HAService::communication_state_
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:735
response.h
isc::ha::HAService::PostSyncCallback
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:69
isc::http::HttpResponsePtr
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
io_service.h
isc::ha::HAService::shouldPartnerDown
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:630
isc::hooks::ParkingLotHandlePtr
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
isc::ha::HAService::startHeartbeat
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1016
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::ha::HAServerType::DHCPv4
@ DHCPv4
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
isc::ha::HAService::asyncSendLeaseUpdate
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
isc::ha::HAService::asyncSendLeaseUpdates
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
isc::ha::QueryFilter
DHCP query filtering class.
Definition: query_filter.h:57
isc::ha::HAService::processHeartbeat
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:902
isc::dhcp::Lease6CollectionPtr
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:608
isc::ha::HAService::scheduleHeartbeat
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1009
isc::ha::HAService::backupStateHandler
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:122
isc::ha::HAService::asyncEnableDHCPService
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
isc::ha::HAService::processSynchronize
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
isc::ha::HAService::io_service_
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:720
isc::ha::HAService
High availability service.
Definition: ha_service.h:38
isc::dhcp::LeasePtr
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
isc::ha::HAService::serveDefaultScopes
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:566
isc::ha::HAServerType
HAServerType
Lists possible server types for which HA service is created.
Definition: ha_server_type.h:14
isc::dhcp::Lease4CollectionPtr
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:458
isc::ha::HAServicePtr
boost::shared_ptr< HAService > HAServicePtr
Pointer to the HAService class.
Definition: ha_service.h:752
isc::ha::HAService::unpause
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:545
isc::ha::HAService::conditionalLogPausedState
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:555
isc::ha::HAService::verifyAsyncResponse
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:1504
isc::ha::HAService::HA_LEASE_UPDATES_COMPLETE_EVT
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:45
isc::ha::HAService::server_type_
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:729
isc::util::StateModel::SM_DERIVED_EVENT_MIN
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
isc::ha::HAService::query_filter_
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:738
isc::ha::HAService::shouldSendLeaseUpdates
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
isc::ha::HAService::getServerType
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:86
isc::ha::HAConfigPtr
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:509
isc::ha::CommunicationStatePtr
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
Definition: communication_state.h:323
isc::ha::HAConfig::PeerConfigPtr
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition: ha_config.h:159
network_state.h
data.h
isc::ha::HAService::network_state_
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:723
isc::ha::HAService::asyncSendHeartbeat
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:915
isc::ha::HAService::verifyEvents
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:75
isc::ha::HAService::processContinue
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:1496
isc::ha::HAService::processScopes
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:1483
isc::ha::HAService::HAService
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:44
query_filter.h
isc::ha::HAService::asyncDisableDHCPService
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
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::ha::HAService::partnerDownStateHandler
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:187
state_model.h
communication_state.h
isc::ha::HAService::asyncSyncLeasesInternal
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
lease.h
isc::ha::HAService::shouldTerminate
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
isc::ha::HAService::defineStates
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:85
isc::ha::HAService::normalStateHandler
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:137
isc::util::StateModel
Implements a finite state machine.
Definition: state_model.h:271
isc::dhcp::Pkt6Ptr
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
isc::ha::HAService::terminatedStateHandler
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:389
client.h
isc::ha::HAService::config_
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:726