Kea 2.2.0
communication_state.h
Go to the documentation of this file.
1// Copyright (C) 2018-2022 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_COMMUNICATION_STATE_H
8#define HA_COMMUNICATION_STATE_H
9
10#include <ha_config.h>
11#include <ha_service_states.h>
13#include <asiolink/io_service.h>
14#include <cc/data.h>
15#include <dhcp/pkt.h>
16
17#include <boost/date_time/posix_time/posix_time.hpp>
18#include <boost/multi_index_container.hpp>
19#include <boost/multi_index/composite_key.hpp>
20#include <boost/multi_index/hashed_index.hpp>
21#include <boost/multi_index/indexed_by.hpp>
22#include <boost/multi_index/member.hpp>
23#include <boost/multi_index/ordered_index.hpp>
24#include <boost/scoped_ptr.hpp>
25#include <boost/shared_ptr.hpp>
26
27#include <functional>
28#include <map>
29#include <mutex>
30#include <set>
31#include <string>
32#include <utility>
33
34namespace isc {
35namespace ha {
36
83public:
84
90 const HAConfigPtr& config);
91
95 virtual ~CommunicationState();
96
100 int getPartnerState() const;
101
107 void setPartnerState(const std::string& state);
108
109private:
115 void setPartnerStateInternal(const std::string& state);
116
117public:
121 std::set<std::string> getPartnerScopes() const;
122
127
128private:
132 void setPartnerScopesInternal(data::ConstElementPtr new_scopes);
133
134public:
140 void startHeartbeat(const long interval,
141 const std::function<void()>& heartbeat_impl);
142
144 void stopHeartbeat();
145
146private:
152 void startHeartbeatInternal(const long interval = 0,
153 const std::function<void()>& heartbeat_impl = 0);
154
156 void stopHeartbeatInternal();
157
158public:
162 bool isHeartbeatRunning() const;
163
169 void poke();
170
171private:
177 void pokeInternal();
178
179public:
183 int64_t getDurationInMillisecs() const;
184
193 bool isCommunicationInterrupted() const;
194
226 virtual void analyzeMessage(const boost::shared_ptr<dhcp::Pkt>& message) = 0;
227
233 size_t getAnalyzedMessagesCount() const;
234
249 virtual bool failureDetected() const = 0;
250
259 virtual size_t getConnectingClientsCount() const = 0;
260
269 virtual size_t getUnackedClientsCount() const = 0;
270
271protected:
272
285 virtual void clearConnectingClients() = 0;
286
287public:
288
311 bool clockSkewShouldWarn();
312
313private:
336 bool clockSkewShouldWarnInternal();
337
338public:
355 bool clockSkewShouldTerminate() const;
356
357private:
374 bool clockSkewShouldTerminateInternal() const;
375
382 bool isClockSkewGreater(const long seconds) const;
383
384public:
385
397 void setPartnerTime(const std::string& time_text);
398
399private:
411 void setPartnerTimeInternal(const std::string& time_text);
412
413public:
415 std::string logFormatClockSkew() const;
416
417private:
419 std::string logFormatClockSkewInternal() const;
420
421public:
430
438 void modifyPokeTime(const long secs);
439
440private:
441
447 int64_t getDurationInMillisecsInternal() const;
448
449protected:
453 boost::posix_time::time_duration updatePokeTime();
454
455private:
461 boost::posix_time::time_duration updatePokeTimeInternal();
462
463public:
464
466 uint64_t getUnsentUpdateCount() const;
467
480
481private:
482
484 void increaseUnsentUpdateCountInternal();
485
486public:
487
497 bool hasPartnerNewUnsentUpdates() const;
498
499private:
500
505 bool hasPartnerNewUnsentUpdatesInternal() const;
506
507public:
508
513 void setPartnerUnsentUpdateCount(uint64_t unsent_update_count);
514
515private:
516
521 void setPartnerUnsentUpdateCountInternal(uint64_t unsent_update_count);
522
523protected:
526
529
532
535
537 boost::posix_time::ptime poke_time_;
538
540 std::function<void()> heartbeat_impl_;
541
546
548 std::set<std::string> partner_scopes_;
549
551 boost::posix_time::time_duration clock_skew_;
552
555 boost::posix_time::ptime last_clock_skew_warn_;
556
558 boost::posix_time::ptime my_time_at_skew_;
559
561 boost::posix_time::ptime partner_time_at_skew_;
562
565
575
582 std::pair<uint64_t, uint64_t> partner_unsent_update_count_;
583
585 const boost::scoped_ptr<std::mutex> mutex_;
586};
587
589typedef boost::shared_ptr<CommunicationState> CommunicationStatePtr;
590
591
598public:
599
605 const HAConfigPtr& config);
606
621 virtual void analyzeMessage(const boost::shared_ptr<dhcp::Pkt>& message);
622
628 virtual bool failureDetected() const;
629
638 virtual size_t getConnectingClientsCount() const;
639
648 virtual size_t getUnackedClientsCount() const;
649
650protected:
651
668 virtual void analyzeMessageInternal(const boost::shared_ptr<dhcp::Pkt>& message);
669
677 virtual bool failureDetectedInternal() const;
678
684 virtual void clearConnectingClients();
685
689 std::vector<uint8_t> hwaddr_;
690 std::vector<uint8_t> clientid_;
692 };
693
696 typedef boost::multi_index_container<
698 boost::multi_index::indexed_by<
699 // First index is a composite index which allows to find a client
700 // by the HW address/client identifier tuple.
701 boost::multi_index::hashed_unique<
702 boost::multi_index::composite_key<
704 boost::multi_index::member<ConnectingClient4, std::vector<uint8_t>,
706 boost::multi_index::member<ConnectingClient4, std::vector<uint8_t>,
708 >
709 >,
710 // Second index allows for counting all clients which are
711 // considered unacked.
712 boost::multi_index::ordered_non_unique<
713 boost::multi_index::member<ConnectingClient4, bool, &ConnectingClient4::unacked_>
714 >
715 >
717
722};
723
725typedef boost::shared_ptr<CommunicationState4> CommunicationState4Ptr;
726
733public:
734
740 const HAConfigPtr& config);
741
750 virtual void analyzeMessage(const boost::shared_ptr<dhcp::Pkt>& message);
751
757 virtual bool failureDetected() const;
758
767 virtual size_t getConnectingClientsCount() const;
768
777 virtual size_t getUnackedClientsCount() const;
778
779protected:
780
791 virtual void analyzeMessageInternal(const boost::shared_ptr<dhcp::Pkt>& message);
792
800 virtual bool failureDetectedInternal() const;
801
807 virtual void clearConnectingClients();
808
812 std::vector<uint8_t> duid_;
814 };
815
818 typedef boost::multi_index_container<
820 boost::multi_index::indexed_by<
821 // First index is for accessing connecting clients by DUID.
822 boost::multi_index::hashed_unique<
823 boost::multi_index::member<ConnectingClient6, std::vector<uint8_t>,
825 >,
826 // Second index allows for counting all clients which are
827 // considered unacked.
828 boost::multi_index::ordered_non_unique<
829 boost::multi_index::member<ConnectingClient6, bool, &ConnectingClient6::unacked_>
830 >
831 >
833
838};
839
841typedef boost::shared_ptr<CommunicationState6> CommunicationState6Ptr;
842
843} // end of namespace isc::ha
844} // end of namespace isc
845
846#endif
Holds communication state between DHCPv4 servers.
virtual size_t getUnackedClientsCount() const
Returns the current number of clients which haven't gotten a lease from the partner server.
virtual void analyzeMessageInternal(const boost::shared_ptr< dhcp::Pkt > &message)
Checks if the DHCPv4 message appears to be unanswered.
virtual size_t getConnectingClientsCount() const
Returns the current number of clients which attempted to get a lease from the partner server.
virtual void analyzeMessage(const boost::shared_ptr< dhcp::Pkt > &message)
Checks if the DHCPv4 message appears to be unanswered.
virtual bool failureDetectedInternal() const
Checks if the partner failure has been detected based on the DHCP traffic analysis.
ConnectingClients4 connecting_clients_
Holds information about the clients attempting to contact the partner server while the servers are in...
virtual bool failureDetected() const
Checks if the partner failure has been detected based on the DHCP traffic analysis.
virtual void clearConnectingClients()
Removes information about the clients the partner server should respond to while communication with t...
CommunicationState4(const asiolink::IOServicePtr &io_service, const HAConfigPtr &config)
Constructor.
boost::multi_index_container< ConnectingClient4, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::composite_key< ConnectingClient4, boost::multi_index::member< ConnectingClient4, std::vector< uint8_t >, &ConnectingClient4::hwaddr_ >, boost::multi_index::member< ConnectingClient4, std::vector< uint8_t >, &ConnectingClient4::clientid_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::member< ConnectingClient4, bool, &ConnectingClient4::unacked_ > > > > ConnectingClients4
Multi index container holding information about the clients attempting to get leases from the partner...
Holds communication state between DHCPv6 servers.
virtual void analyzeMessage(const boost::shared_ptr< dhcp::Pkt > &message)
Checks if the DHCPv6 message appears to be unanswered.
ConnectingClients6 connecting_clients_
Holds information about the clients attempting to contact the partner server while the servers are in...
CommunicationState6(const asiolink::IOServicePtr &io_service, const HAConfigPtr &config)
Constructor.
boost::multi_index_container< ConnectingClient6, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::member< ConnectingClient6, std::vector< uint8_t >, &ConnectingClient6::duid_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::member< ConnectingClient6, bool, &ConnectingClient6::unacked_ > > > > ConnectingClients6
Multi index container holding information about the clients attempting to get leases from the partner...
virtual void clearConnectingClients()
Removes information about the clients the partner server should respond to while communication with t...
virtual bool failureDetected() const
Checks if the partner failure has been detected based on the DHCP traffic analysis.
virtual size_t getUnackedClientsCount() const
Returns the current number of clients which haven't gotten a lease from the partner server.
virtual bool failureDetectedInternal() const
Checks if the partner failure has been detected based on the DHCP traffic analysis.
virtual void analyzeMessageInternal(const boost::shared_ptr< dhcp::Pkt > &message)
Checks if the DHCPv6 message appears to be unanswered.
virtual size_t getConnectingClientsCount() const
Returns the current number of clients which attempted to get a lease from the partner server.
Holds communication state between the two HA peers.
virtual size_t getConnectingClientsCount() const =0
Returns the current number of clients which attempted to get a lease from the partner server.
virtual size_t getUnackedClientsCount() const =0
Returns the current number of clients which haven't got the lease from the partner server.
virtual void clearConnectingClients()=0
Removes information about the clients the partner server should respond to while communication with t...
void startHeartbeat(const long interval, const std::function< void()> &heartbeat_impl)
Starts recurring heartbeat (public interface).
uint64_t unsent_update_count_
Total number of unsent lease updates.
bool isCommunicationInterrupted() const
Checks if communication with the partner is interrupted.
void setPartnerScopes(data::ConstElementPtr new_scopes)
Sets partner scopes.
int getPartnerState() const
Returns last known state of the partner.
bool clockSkewShouldWarn()
Issues a warning about high clock skew between the active servers if one is warranted.
std::string logFormatClockSkew() const
Returns current clock skew value in the logger friendly format.
void setPartnerUnsentUpdateCount(uint64_t unsent_update_count)
Saves new total number of unsent lease updates from the partner.
void setPartnerState(const std::string &state)
Sets partner state.
std::pair< uint64_t, uint64_t > partner_unsent_update_count_
Previous and current total number of unsent lease updates from the partner.
std::set< std::string > getPartnerScopes() const
Returns scopes served by the partner server.
virtual ~CommunicationState()
Destructor.
HAConfigPtr config_
High availability configuration.
bool isHeartbeatRunning() const
Checks if recurring heartbeat is running.
long interval_
Interval specified for the heartbeat.
void stopHeartbeat()
Stops recurring heartbeat.
void increaseUnsentUpdateCount()
Increases a total number of unsent lease updates by 1.
void setPartnerTime(const std::string &time_text)
Provide partner's notion of time so the new clock skew can be calculated.
bool hasPartnerNewUnsentUpdates() const
Checks if the partner allocated new leases for which it hasn't sent any lease updates.
asiolink::IOServicePtr io_service_
Pointer to the common IO service instance.
virtual void analyzeMessage(const boost::shared_ptr< dhcp::Pkt > &message)=0
Checks if the DHCP message appears to be unanswered.
void modifyPokeTime(const long secs)
Modifies poke time by adding seconds to it.
const boost::scoped_ptr< std::mutex > mutex_
The mutex used to protect internal state.
data::ElementPtr getReport() const
Returns the report about current communication state.
boost::posix_time::time_duration clock_skew_
Clock skew between the active servers.
size_t getAnalyzedMessagesCount() const
Returns the number of analyzed messages while being in the communications interrupted state.
size_t analyzed_messages_count_
Total number of analyzed messages to be responded by partner.
bool clockSkewShouldTerminate() const
Indicates whether the HA service should enter "terminated" state as a result of the clock skew exceed...
std::function< void()> heartbeat_impl_
Pointer to the function providing heartbeat implementation.
boost::posix_time::ptime poke_time_
Last poke time.
boost::posix_time::time_duration updatePokeTime()
Update the poke time and compute the duration.
boost::posix_time::ptime partner_time_at_skew_
Partner reported time when skew was calculated.
CommunicationState(const asiolink::IOServicePtr &io_service, const HAConfigPtr &config)
Constructor.
int partner_state_
Last known state of the partner server.
boost::posix_time::ptime last_clock_skew_warn_
Holds a time when last warning about too high clock skew was issued.
std::set< std::string > partner_scopes_
Last known set of scopes served by the partner server.
uint64_t getUnsentUpdateCount() const
Returns a total number of unsent lease updates.
boost::posix_time::ptime my_time_at_skew_
My time when skew was calculated.
int64_t getDurationInMillisecs() const
Returns duration between the poke time and current time.
virtual bool failureDetected() const =0
Checks if the partner failure has been detected based on the DHCP traffic analysis.
asiolink::IntervalTimerPtr timer_
Interval timer triggering heartbeat commands.
void poke()
Pokes the communication state.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
boost::shared_ptr< CommunicationState4 > CommunicationState4Ptr
Pointer to the CommunicationState4 object.
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:786
boost::shared_ptr< CommunicationState6 > CommunicationState6Ptr
Pointer to the CommunicationState6 object.
Defines the logger used by the top-level component of kea-lfc.
Structure holding information about the client which has send the packet being analyzed.
Structure holding information about a client which sent a packet being analyzed.