Skip to content

Commit 5e7bc73

Browse files
jrajahalmesayboras
authored andcommitted
policy: Change policy resolver back to a shared pointer
Change policy resolver back to a shared pointer, and manage NetworkPolicyMap destruction explicitly by splitting the map to singleton and 'NetworkPolicyMapImpl' and by posting the 'NetworkPolicyMapImpl' to the main thread for deletion. NetworkPolicyMapImpl is managed via an unique_ptr, which required the removal of use of 'shared_from_this', which was possible due to simplified clean-up of the old map now that an atomic swap is used for the policy map update. This change allows long lived connections on a removed listener to keep on functioning, including receiving policy updates via the singleton policy map. Signed-off-by: Jarno Rajahalme <[email protected]>
1 parent 3860fdb commit 5e7bc73

10 files changed

+168
-154
lines changed

cilium/bpf_metadata.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,8 @@ createHostMap(Server::Configuration::ListenerFactoryContext& context) {
190190
std::shared_ptr<const Cilium::NetworkPolicyMap>
191191
createPolicyMap(Server::Configuration::FactoryContext& context, Cilium::CtMapSharedPtr& ct) {
192192
return context.serverFactoryContext().singletonManager().getTyped<const Cilium::NetworkPolicyMap>(
193-
SINGLETON_MANAGER_REGISTERED_NAME(cilium_network_policy), [&context, &ct] {
194-
auto map = std::make_shared<Cilium::NetworkPolicyMap>(context, ct);
195-
map->startSubscription();
196-
return map;
197-
});
193+
SINGLETON_MANAGER_REGISTERED_NAME(cilium_network_policy),
194+
[&context, &ct] { return std::make_shared<Cilium::NetworkPolicyMap>(context, ct); });
198195
}
199196

200197
} // namespace
@@ -530,7 +527,7 @@ Config::extractSocketMetadata(Network::ConnectionSocket& socket) {
530527
mark, ingress_source_identity, source_identity, is_ingress_, is_l7lb_, dip->port(),
531528
std::move(pod_ip), std::move(ingress_policy_name), std::move(src_address),
532529
std::move(source_addresses.ipv4_), std::move(source_addresses.ipv6_), std::move(dst_address),
533-
weak_from_this(), proxy_id_, std::move(proxylib_l7proto), sni)};
530+
shared_from_this(), proxy_id_, std::move(proxylib_l7proto), sni)};
534531
}
535532

536533
Network::FilterStatus Instance::onAccept(Network::ListenerFilterCallbacks& cb) {

cilium/bpf_metadata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct SocketMetadata : public Logger::Loggable<Logger::Id::filter> {
3939
Network::Address::InstanceConstSharedPtr source_address_ipv4,
4040
Network::Address::InstanceConstSharedPtr source_address_ipv6,
4141
Network::Address::InstanceConstSharedPtr original_dest_address,
42-
const std::weak_ptr<PolicyResolver>& policy_resolver, uint32_t proxy_id,
42+
const PolicyResolverSharedPtr& policy_resolver, uint32_t proxy_id,
4343
std::string&& proxylib_l7_proto, absl::string_view sni)
4444
: ingress_source_identity_(ingress_source_identity), source_identity_(source_identity),
4545
ingress_(ingress), is_l7lb_(l7lb), port_(port), pod_ip_(std::move(pod_ip)),
@@ -118,7 +118,7 @@ struct SocketMetadata : public Logger::Loggable<Logger::Id::filter> {
118118
uint32_t proxy_id_;
119119
std::string proxylib_l7_proto_;
120120
std::string sni_;
121-
std::weak_ptr<PolicyResolver> policy_resolver_;
121+
const PolicyResolverSharedPtr policy_resolver_;
122122

123123
uint32_t mark_;
124124

cilium/filter_state_cilium_policy.cc

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@ bool CiliumPolicyFilterState::enforceNetworkPolicy(const Network::Connection& co
2626
/* OUT */ bool& use_proxy_lib,
2727
/* OUT */ std::string& l7_proto,
2828
/* INOUT */ AccessLog::Entry& log_entry) const {
29-
const auto resolver = policy_resolver_.lock();
3029
use_proxy_lib = false;
3130
l7_proto = "";
3231

33-
if (!resolver) {
34-
// No policy resolver
35-
ENVOY_CONN_LOG(debug, "No policy resolver", conn);
36-
return false;
37-
}
38-
3932
// enforce pod policy first, if any
4033
if (pod_ip_.length() > 0) {
41-
const auto& policy = resolver->getPolicy(pod_ip_);
34+
const auto& policy = policy_resolver_->getPolicy(pod_ip_);
4235
auto remote_id = ingress_ ? source_identity_ : destination_identity;
4336
auto port = ingress_ ? port_ : destination_port;
4437

@@ -57,7 +50,7 @@ bool CiliumPolicyFilterState::enforceNetworkPolicy(const Network::Connection& co
5750
// enforce Ingress policy 2nd, if any
5851
if (ingress_policy_name_.length() > 0) {
5952
log_entry.entry_.set_policy_name(ingress_policy_name_);
60-
const auto& policy = resolver->getPolicy(ingress_policy_name_);
53+
const auto& policy = policy_resolver_->getPolicy(ingress_policy_name_);
6154

6255
// Enforce ingress policy for Ingress, on the original destination port
6356
if (ingress_source_identity_ != 0) {
@@ -93,14 +86,6 @@ bool CiliumPolicyFilterState::enforceHTTPPolicy(const Network::Connection& conn,
9386
uint16_t destination_port,
9487
/* INOUT */ Http::RequestHeaderMap& headers,
9588
/* INOUT */ AccessLog::Entry& log_entry) const {
96-
const auto resolver = policy_resolver_.lock();
97-
98-
if (!resolver) {
99-
// No policy resolver
100-
ENVOY_CONN_LOG(debug, "No policy resolver", conn);
101-
return false;
102-
}
103-
10489
// enforce pod policy first, if any.
10590
// - ingress enforcement in downstream
10691
// - egress enforcement in upstream
@@ -109,7 +94,7 @@ bool CiliumPolicyFilterState::enforceHTTPPolicy(const Network::Connection& conn,
10994
// - is_l7lb_: ingress_ == is_downstream
11095
// - !is_l7lb_: is_downstream
11196
if (pod_ip_.length() > 0 && (is_l7lb_ ? is_downstream == ingress_ : is_downstream)) {
112-
const auto& policy = resolver->getPolicy(pod_ip_);
97+
const auto& policy = policy_resolver_->getPolicy(pod_ip_);
11398
auto remote_id = ingress_ ? source_identity_ : destination_identity;
11499
auto port = ingress_ ? port_ : destination_port;
115100
if (!policy.allowed(ingress_, proxy_id_, remote_id, port, headers, log_entry)) {
@@ -122,7 +107,7 @@ bool CiliumPolicyFilterState::enforceHTTPPolicy(const Network::Connection& conn,
122107
// enforce Ingress policy 2nd, if any, always on the upstream
123108
if (!is_downstream && ingress_policy_name_.length() > 0) {
124109
log_entry.entry_.set_policy_name(ingress_policy_name_);
125-
const auto& policy = resolver->getPolicy(ingress_policy_name_);
110+
const auto& policy = policy_resolver_->getPolicy(ingress_policy_name_);
126111

127112
// Enforce ingress policy for Ingress, on the original destination port
128113
if (ingress_source_identity_ != 0) {

cilium/filter_state_cilium_policy.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "absl/strings/string_view.h"
1717
#include "cilium/accesslog.h"
1818
#include "cilium/network_policy.h"
19-
#include "cilium/policy_id.h"
2019

2120
namespace Envoy {
2221
namespace Cilium {
@@ -28,6 +27,7 @@ class PolicyResolver {
2827
virtual uint32_t resolvePolicyId(const Network::Address::Ip*) const PURE;
2928
virtual const PolicyInstance& getPolicy(const std::string&) const PURE;
3029
};
30+
using PolicyResolverSharedPtr = std::shared_ptr<PolicyResolver>;
3131

3232
// FilterState that holds relevant connection & policy information that can be retrieved
3333
// by the Cilium network- and HTTP policy filters via filter state.
@@ -37,7 +37,7 @@ class CiliumPolicyFilterState : public StreamInfo::FilterState::Object,
3737
CiliumPolicyFilterState(uint32_t ingress_source_identity, uint32_t source_identity, bool ingress,
3838
bool l7lb, uint16_t port, std::string&& pod_ip,
3939
std::string&& ingress_policy_name,
40-
const std::weak_ptr<PolicyResolver>& policy_resolver, uint32_t proxy_id,
40+
const PolicyResolverSharedPtr& policy_resolver, uint32_t proxy_id,
4141
absl::string_view sni)
4242
: ingress_source_identity_(ingress_source_identity), source_identity_(source_identity),
4343
ingress_(ingress), is_l7lb_(l7lb), port_(port), pod_ip_(std::move(pod_ip)),
@@ -51,20 +51,10 @@ class CiliumPolicyFilterState : public StreamInfo::FilterState::Object,
5151
}
5252

5353
uint32_t resolvePolicyId(const Network::Address::Ip* ip) const {
54-
const auto resolver = policy_resolver_.lock();
55-
if (resolver) {
56-
return resolver->resolvePolicyId(ip);
57-
}
58-
return Cilium::ID::WORLD; // default to WORLD policy ID if resolver is no longer available
54+
return policy_resolver_->resolvePolicyId(ip);
5955
}
6056

61-
const PolicyInstance& getPolicy() const {
62-
const auto resolver = policy_resolver_.lock();
63-
if (resolver) {
64-
return resolver->getPolicy(pod_ip_);
65-
}
66-
return NetworkPolicyMap::getDenyAllPolicy();
67-
}
57+
const PolicyInstance& getPolicy() const { return policy_resolver_->getPolicy(pod_ip_); }
6858

6959
bool enforceNetworkPolicy(const Network::Connection& conn, uint32_t destination_identity,
7060
uint16_t destination_port, const absl::string_view sni,
@@ -95,7 +85,7 @@ class CiliumPolicyFilterState : public StreamInfo::FilterState::Object,
9585
std::string sni_;
9686

9787
private:
98-
const std::weak_ptr<PolicyResolver> policy_resolver_;
88+
const PolicyResolverSharedPtr policy_resolver_;
9989
};
10090
} // namespace Cilium
10191
} // namespace Envoy

0 commit comments

Comments
 (0)