31std::array<uint8_t, 256> loadb_mx_tbl = { {
32 251, 175, 119, 215, 81, 14, 79, 191, 103, 49, 181, 143, 186, 157, 0,
33 232, 31, 32, 55, 60, 152, 58, 17, 237, 174, 70, 160, 144, 220, 90, 57,
34 223, 59, 3, 18, 140, 111, 166, 203, 196, 134, 243, 124, 95, 222, 179,
35 197, 65, 180, 48, 36, 15, 107, 46, 233, 130, 165, 30, 123, 161, 209, 23,
36 97, 16, 40, 91, 219, 61, 100, 10, 210, 109, 250, 127, 22, 138, 29, 108,
37 244, 67, 207, 9, 178, 204, 74, 98, 126, 249, 167, 116, 34, 77, 193,
38 200, 121, 5, 20, 113, 71, 35, 128, 13, 182, 94, 25, 226, 227, 199, 75,
39 27, 41, 245, 230, 224, 43, 225, 177, 26, 155, 150, 212, 142, 218, 115,
40 241, 73, 88, 105, 39, 114, 62, 255, 192, 201, 145, 214, 168, 158, 221,
41 148, 154, 122, 12, 84, 82, 163, 44, 139, 228, 236, 205, 242, 217, 11,
42 187, 146, 159, 64, 86, 239, 195, 42, 106, 198, 118, 112, 184, 172, 87,
43 2, 173, 117, 176, 229, 247, 253, 137, 185, 99, 164, 102, 147, 45, 66,
44 231, 52, 141, 211, 194, 206, 246, 238, 56, 110, 78, 248, 63, 240, 189,
45 93, 92, 51, 53, 183, 19, 171, 72, 50, 33, 104, 101, 69, 8, 252, 83, 120,
46 76, 135, 85, 54, 202, 125, 188, 213, 96, 235, 136, 208, 162, 129, 190,
47 132, 156, 38, 47, 1, 7, 254, 24, 4, 216, 131, 89, 21, 28, 133, 37, 153,
48 149, 80, 170, 68, 6, 169, 234, 151 }
52std::array<bool, DHCP_TYPES_EOF> v4_ha_types = {
75std::array<bool, DHCPV6_TYPES_EOF> v6_ha_types = {
132 std::vector<HAConfig::PeerConfigPtr> backup_peers;
136 for (
auto const& peer_pair : peers_map) {
137 auto peer = peer_pair.second;
156 backup_peers.push_back(peer);
161 if (!backup_peers.empty()) {
162 peers_.insert(peers_.end(), backup_peers.begin(), backup_peers.end());
176 std::lock_guard<std::mutex> lock(*
mutex_);
177 serveScopeInternal(scope_name);
179 serveScopeInternal(scope_name);
184QueryFilter::serveScopeInternal(
const std::string& scope_name) {
192 std::lock_guard<std::mutex> lock(*
mutex_);
193 serveScopeOnlyInternal(scope_name);
195 serveScopeOnlyInternal(scope_name);
200QueryFilter::serveScopeOnlyInternal(
const std::string& scope_name) {
202 serveNoScopesInternal();
203 serveScopeInternal(scope_name);
209 std::lock_guard<std::mutex> lock(*
mutex_);
210 serveScopesInternal(scopes);
212 serveScopesInternal(scopes);
217QueryFilter::serveScopesInternal(
const std::vector<std::string>& scopes) {
222 serveNoScopesInternal();
223 for (
size_t i = 0; i < scopes.size(); ++i) {
224 serveScopeInternal(scopes[i]);
238 std::lock_guard<std::mutex> lock(*
mutex_);
239 serveDefaultScopesInternal();
241 serveDefaultScopesInternal();
246QueryFilter::serveDefaultScopesInternal() {
252 serveNoScopesInternal();
258 serveScopeInternal(my_config->getName());
265 std::lock_guard<std::mutex> lock(*
mutex_);
266 serveFailoverScopesInternal();
268 serveFailoverScopesInternal();
273QueryFilter::serveFailoverScopesInternal() {
275 serveNoScopesInternal();
279 for (
auto const& peer :
peers_) {
288 serveScopeInternal(peer->getName());
296 std::lock_guard<std::mutex> lock(*
mutex_);
297 serveNoScopesInternal();
299 serveNoScopesInternal();
304QueryFilter::serveNoScopesInternal() {
308 for (
auto const& peer :
peers_) {
309 scopes_[peer->getName()] =
false;
316 std::lock_guard<std::mutex> lock(*
mutex_);
317 return (amServingScopeInternal(scope_name));
319 return (amServingScopeInternal(scope_name));
324QueryFilter::amServingScopeInternal(
const std::string& scope_name)
const {
325 auto scope =
scopes_.find(scope_name);
326 return ((scope ==
scopes_.end()) || (scope->second));
332 std::lock_guard<std::mutex> lock(*
mutex_);
333 return (getServedScopesInternal());
335 return (getServedScopesInternal());
340QueryFilter::getServedScopesInternal()
const {
341 std::set<std::string> scope_set;
342 for (
auto const& scope :
scopes_) {
344 scope_set.insert(scope.first);
352 auto msg_type = query4->getType();
353 return (msg_type < v4_ha_types.size() && v4_ha_types[msg_type]);
358 auto msg_type = query->getType();
359 return (msg_type < v6_ha_types.size() && v6_ha_types[msg_type]);
366 std::lock_guard<std::mutex> lock(*
mutex_);
367 return (queryInScopeInternal(query4, scope_class));
369 return (queryInScopeInternal(query4, scope_class));
376 std::lock_guard<std::mutex> lock(*
mutex_);
377 return (queryInScopeInternal(query6, scope_class));
379 return (queryInScopeInternal(query6, scope_class));
383template<
typename QueryPtrType>
385QueryFilter::queryInScopeInternal(
const QueryPtrType& query,
386 std::string& scope_class)
const {
393 auto scope =
peers_[0]->getName();
398 int candidate_server = 0;
406 if (candidate_server < 0) {
411 auto scope =
peers_[candidate_server]->getName();
413 return ((candidate_server >= 0) && amServingScopeInternal(scope));
419 std::lock_guard<std::mutex> lock(*
mutex_);
420 return (leaseInScopeInternal(lease4));
422 return (leaseInScopeInternal(lease4));
429 std::lock_guard<std::mutex> lock(*
mutex_);
430 return (leaseInScopeInternal(lease6));
432 return (leaseInScopeInternal(lease6));
436template<
typename LeasePtrType>
438QueryFilter::leaseInScopeInternal(
const LeasePtrType& lease)
const {
443 int candidate_server = 0;
451 if (candidate_server < 0) {
456 auto scope =
peers_[candidate_server]->getName();
457 return ((candidate_server >= 0) && amServingScopeInternal(scope));
466 if (opt_client_id && !opt_client_id->getData().empty()) {
467 auto const& client_id_key = opt_client_id->getData();
473 if (hwaddr && !hwaddr->hwaddr_.empty()) {
474 lb_hash =
loadBalanceHash(&hwaddr->hwaddr_[0], hwaddr->hwaddr_.size());
479 std::stringstream xid;
480 xid <<
"0x" << std::hex << query4->getTransid() << std::dec;
482 .arg(
config_->getThisServerName())
498 if (opt_duid && !opt_duid->getData().empty()) {
499 auto const& duid_key = opt_duid->getData();
504 std::stringstream xid;
505 xid <<
"0x" << std::hex << query6->getTransid() << std::dec;
507 .arg(
config_->getThisServerName())
522 if (lease4->client_id_ && !lease4->client_id_->getClientId().empty()) {
523 auto const& client_id_key = lease4->client_id_->getClientId();
529 if (hwaddr && !hwaddr->hwaddr_.empty()) {
530 lb_hash =
loadBalanceHash(&hwaddr->hwaddr_[0], hwaddr->hwaddr_.size());
536 .arg(
config_->getThisServerName())
551 auto duid = lease6->duid_;
552 if (duid && !duid->getDuid().empty()) {
553 auto const& duid_key = duid->getDuid();
559 .arg(
config_->getThisServerName())
571 uint8_t hash =
static_cast<uint8_t
>(key_len);
573 for (
size_t i = key_len; i > 0;) {
574 hash = loadb_mx_tbl[hash ^ key[--i]];
584 static_cast<void>(
config_->getPeerConfig(scope_name));
588 <<
"' while enabling/disabling HA scopes");
594 return (std::string(
"HA_") + scope_name);
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Role
Server's role in the High Availability setup.
std::map< std::string, PeerConfigPtr > PeerConfigMap
Map of the servers' configurations.
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
void serveScopes(const std::vector< std::string > &scopes)
Enables selected scopes.
bool inScope(const dhcp::Pkt4Ptr &query4, std::string &scope_class) const
Checks if this server should process the DHCPv4 query.
bool amServingScope(const std::string &scope_name) const
Checks if this server instance is configured to process traffic belonging to a particular scope.
std::string makeScopeClass(const std::string &scope_name) const
Returns scope class name for the specified scope name.
boost::scoped_ptr< std::mutex > mutex_
Mutex to protect the internal state.
void serveFailoverScopes()
Enable scopes required in failover case.
uint8_t loadBalanceHash(const uint8_t *key, const size_t key_len) const
Compute load balancing hash.
void validateScopeName(const std::string &scope_name) const
Checks if the scope name matches a name of any of the configured servers.
int active_servers_
Number of the active servers in the given HA mode.
HAConfigPtr config_
Pointer to the HA configuration.
void serveScopeOnly(const std::string &scope_name)
Enable scope and disable all other scopes.
int loadBalance(const dhcp::Pkt4Ptr &query4) const
Performs load balancing of the DHCPv4 queries.
static bool isHaType(const dhcp::Pkt4Ptr &query4)
Determines if a DHCPv4 query is a message type HA should process.
void serveDefaultScopes()
Serve default scopes for the given HA mode.
void serveNoScopes()
Disables all scopes.
std::set< std::string > getServedScopes() const
Returns served scopes.
void serveScope(const std::string &scope_name)
Enable scope.
std::vector< HAConfig::PeerConfigPtr > peers_
Vector of HA peers configurations.
std::map< std::string, bool > scopes_
Holds mapping of the scope names to the flag which indicates if the scopes are enabled or disabled.
QueryFilter(const HAConfigPtr &config)
Constructor.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
@ DHO_DHCP_CLIENT_IDENTIFIER
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
boost::shared_ptr< Option > OptionPtr
const isc::log::MessageID HA_LOAD_BALANCING_LEASE_IDENTIFIER_MISSING
isc::log::Logger ha_logger("ha-hooks")
const isc::log::MessageID HA_LOAD_BALANCING_LEASE_DUID_MISSING
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
const isc::log::MessageID HA_LOAD_BALANCING_DUID_MISSING
const isc::log::MessageID HA_LOAD_BALANCING_IDENTIFIER_MISSING
const int DBGLVL_TRACE_BASIC
Trace basic operations.
Defines the logger used by the top-level component of kea-lfc.