Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/udp/udp_proxy/hash_policy_impl.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/extensions/filters/udp/udp_proxy/hash_policy_impl.h"
2
3
#include "envoy/common/exception.h"
4
5
#include "source/common/common/assert.h"
6
#include "source/common/common/macros.h"
7
8
namespace Envoy {
9
namespace Extensions {
10
namespace UdpFilters {
11
namespace UdpProxy {
12
13
class SourceIpHashMethod : public HashPolicyImpl::HashMethod {
14
public:
15
  absl::optional<uint64_t>
16
0
  evaluate(const Network::Address::Instance& downstream_addr) const override {
17
0
    if (downstream_addr.ip()) {
18
0
      ASSERT(!downstream_addr.ip()->addressAsString().empty());
19
0
      return HashUtil::xxHash64(downstream_addr.ip()->addressAsString());
20
0
    }
21
22
0
    return absl::nullopt;
23
0
  }
24
};
25
26
class KeyHashMethod : public HashPolicyImpl::HashMethod {
27
public:
28
0
  explicit KeyHashMethod(const std::string& key) : hash_{HashUtil::xxHash64(key)} {
29
0
    ASSERT(!key.empty());
30
0
  }
31
32
  absl::optional<uint64_t>
33
0
  evaluate(const Network::Address::Instance& downstream_addr) const override {
34
0
    UNREFERENCED_PARAMETER(downstream_addr);
35
0
    return hash_;
36
0
  }
37
38
private:
39
  const uint64_t hash_;
40
};
41
42
HashPolicyImpl::HashPolicyImpl(
43
0
    const absl::Span<const UdpProxyConfig::HashPolicy* const>& hash_policies) {
44
0
  ASSERT(hash_policies.size() == 1);
45
0
  switch (hash_policies[0]->policy_specifier_case()) {
46
0
  case UdpProxyConfig::HashPolicy::PolicySpecifierCase::kSourceIp:
47
0
    hash_impl_ = std::make_unique<SourceIpHashMethod>();
48
0
    break;
49
0
  case UdpProxyConfig::HashPolicy::PolicySpecifierCase::kKey:
50
0
    hash_impl_ = std::make_unique<KeyHashMethod>(hash_policies[0]->key());
51
0
    break;
52
0
  case UdpProxyConfig::HashPolicy::PolicySpecifierCase::POLICY_SPECIFIER_NOT_SET:
53
0
    PANIC_DUE_TO_CORRUPT_ENUM;
54
0
  }
55
0
}
56
57
absl::optional<uint64_t>
58
0
HashPolicyImpl::generateHash(const Network::Address::Instance& downstream_addr) const {
59
0
  return hash_impl_->evaluate(downstream_addr);
60
0
}
61
62
} // namespace UdpProxy
63
} // namespace UdpFilters
64
} // namespace Extensions
65
} // namespace Envoy