Skip to content

Commit

Permalink
udp: Extend hash policies with key based hashing
Browse files Browse the repository at this point in the history
Co-authored-by: DongRyeol Cha <[email protected]>
Signed-off-by: Kornel David <[email protected]>
  • Loading branch information
davidkornel and DongRyeol Cha committed Apr 14, 2021
1 parent 5b1977f commit 78f4b2c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/envoy/extensions/filters/udp/udp_proxy/v3/udp_proxy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ message UdpProxyConfig {

// The source IP will be used to compute the hash used by hash-based load balancing algorithms.
bool source_ip = 1 [(validate.rules).bool = {const: true}];

// A given key will be used to compute the hash used by hash-based load balancing algorithms.
string key = 2 [(validate.rules).string = {min_len: 1}];
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions source/extensions/filters/udp/udp_proxy/hash_policy_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,34 @@ class SourceIpHashMethod : public HashPolicyImpl::HashMethod {
}
};

class KeyHashMethod : public HashPolicyImpl::HashMethod {
public:
KeyHashMethod(const std::string& key) : Key(key) {}
const std::string& Key;

absl::optional<uint64_t>
evaluate(const Network::Address::Instance& downstream_addr) const override {
(void)downstream_addr;
// ASSERT(!Key.empty());
if (!Key.empty()) {
uint64_t hash = HashUtil::xxHash64(Key);
return hash;
}

return absl::nullopt;
}
};

HashPolicyImpl::HashPolicyImpl(
const absl::Span<const UdpProxyConfig::HashPolicy* const>& hash_policies) {
ASSERT(hash_policies.size() == 1);
switch (hash_policies[0]->policy_specifier_case()) {
case UdpProxyConfig::HashPolicy::PolicySpecifierCase::kSourceIp:
hash_impl_ = std::make_unique<SourceIpHashMethod>();
break;
case UdpProxyConfig::HashPolicy::PolicySpecifierCase::kKey:
hash_impl_ = std::make_unique<KeyHashMethod>(hash_policies[0]->key());
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
Expand Down

0 comments on commit 78f4b2c

Please sign in to comment.