-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a filter for applying Android socket tags (#2423)
Add a filter for applying Android socket tags Testing: Functionality verified as part of the run of the experimentation example app. Also filed #2494 to track improvements. Docs Changes: Updated docs/root/api/starting_envoy.rst Release Notes: Added Signed-off-by: Ryan Hamilton <[email protected]> Co-authored-by: Rafał Augustyniak <[email protected]>
- Loading branch information
1 parent
e3ade7e
commit 66ecf9f
Showing
35 changed files
with
868 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
load( | ||
"@envoy//bazel:envoy_build_system.bzl", | ||
"envoy_cc_extension", | ||
"envoy_extension_package", | ||
"envoy_proto_library", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_extension_package() | ||
|
||
envoy_proto_library( | ||
name = "filter", | ||
srcs = ["filter.proto"], | ||
) | ||
|
||
envoy_cc_extension( | ||
name = "socket_tag_filter_lib", | ||
srcs = ["filter.cc"], | ||
hdrs = ["filter.h"], | ||
repository = "@envoy", | ||
deps = [ | ||
":filter_cc_proto", | ||
"//library/common/http:internal_headers_lib", | ||
"//library/common/network:socket_tag_socket_option_lib", | ||
"//library/common/types:c_types_lib", | ||
"@envoy//envoy/http:codes_interface", | ||
"@envoy//envoy/http:filter_interface", | ||
"@envoy//source/extensions/filters/http/common:pass_through_filter_lib", | ||
], | ||
) | ||
|
||
envoy_cc_extension( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
repository = "@envoy", | ||
deps = [ | ||
":socket_tag_filter_lib", | ||
"@envoy//source/extensions/filters/http/common:factory_base_lib", | ||
], | ||
) |
33 changes: 33 additions & 0 deletions
33
library/common/extensions/filters/http/socket_tag/config.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "library/common/extensions/filters/http/socket_tag/config.h" | ||
|
||
#include <stdint.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
#include "envoy/network/listen_socket.h" | ||
|
||
#include "library/common/extensions/filters/http/socket_tag/filter.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace SocketTag { | ||
|
||
Http::FilterFactoryCb SocketTagFilterFactory::createFilterFactoryFromProtoTyped( | ||
const envoymobile::extensions::filters::http::socket_tag::SocketTag& /*proto_config*/, | ||
const std::string&, Server::Configuration::FactoryContext& /*context*/) { | ||
|
||
return [](Http::FilterChainFactoryCallbacks& callbacks) -> void { | ||
callbacks.addStreamFilter(std::make_shared<SocketTagFilter>()); | ||
}; | ||
} | ||
|
||
/** | ||
* Static registration for the SocketTag filter. @see NamedHttpFilterConfigFactory. | ||
*/ | ||
REGISTER_FACTORY(SocketTagFilterFactory, Server::Configuration::NamedHttpFilterConfigFactory); | ||
|
||
} // namespace SocketTag | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
32 changes: 32 additions & 0 deletions
32
library/common/extensions/filters/http/socket_tag/config.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <string> | ||
|
||
#include "source/extensions/filters/http/common/factory_base.h" | ||
|
||
#include "library/common/extensions/filters/http/socket_tag/filter.pb.h" | ||
#include "library/common/extensions/filters/http/socket_tag/filter.pb.validate.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace SocketTag { | ||
|
||
/** | ||
* Config registration for the socket tag filter. @see NamedHttpFilterConfigFactory. | ||
*/ | ||
class SocketTagFilterFactory | ||
: public Common::FactoryBase<envoymobile::extensions::filters::http::socket_tag::SocketTag> { | ||
public: | ||
SocketTagFilterFactory() : FactoryBase("socket_tag") {} | ||
|
||
private: | ||
::Envoy::Http::FilterFactoryCb createFilterFactoryFromProtoTyped( | ||
const envoymobile::extensions::filters::http::socket_tag::SocketTag& config, | ||
const std::string& stats_prefix, Server::Configuration::FactoryContext& context) override; | ||
}; | ||
|
||
DECLARE_FACTORY(SocketTagFilterFactory); | ||
|
||
} // namespace SocketTag | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
44 changes: 44 additions & 0 deletions
44
library/common/extensions/filters/http/socket_tag/filter.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "library/common/extensions/filters/http/socket_tag/filter.h" | ||
|
||
#include "envoy/server/filter_config.h" | ||
|
||
#include "library/common/network/socket_tag_socket_option_impl.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace SocketTag { | ||
|
||
Http::FilterHeadersStatus SocketTagFilter::decodeHeaders(Http::RequestHeaderMap& request_headers, | ||
bool) { | ||
static auto socket_tag_header = Http::LowerCaseString("x-envoy-mobile-socket-tag"); | ||
Http::RequestHeaderMap::GetResult header = request_headers.get(socket_tag_header); | ||
if (header.empty()) { | ||
return Http::FilterHeadersStatus::Continue; | ||
} | ||
|
||
// The x-envoy-mobile-socket-tag header must contain a pair of number separated by a comma, e.g.: | ||
// x-envoy-mobile-socket-tag: 123,456 | ||
// The first number contains the UID and the second contains the traffic stats tag. | ||
std::string tag_string(header[0]->value().getStringView()); | ||
std::pair<std::string, std::string> data = absl::StrSplit(tag_string, ','); | ||
uid_t uid; | ||
uint32_t traffic_stats_tag; | ||
if (!absl::SimpleAtoi(data.first, &uid) || !absl::SimpleAtoi(data.second, &traffic_stats_tag)) { | ||
decoder_callbacks_->sendLocalReply( | ||
Http::Code::BadRequest, | ||
absl::StrCat("Invalid x-envoy-mobile-socket-tag header: ", tag_string), nullptr, | ||
absl::nullopt, ""); | ||
return Http::FilterHeadersStatus::StopIteration; | ||
} | ||
auto options = std::make_shared<Network::Socket::Options>(); | ||
options->push_back(std::make_shared<Network::SocketTagSocketOptionImpl>(uid, traffic_stats_tag)); | ||
decoder_callbacks_->addUpstreamSocketOptions(options); | ||
request_headers.remove(socket_tag_header); | ||
return Http::FilterHeadersStatus::Continue; | ||
} | ||
|
||
} // namespace SocketTag | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
30 changes: 30 additions & 0 deletions
30
library/common/extensions/filters/http/socket_tag/filter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "envoy/http/filter.h" | ||
|
||
#include "source/common/common/logger.h" | ||
#include "source/extensions/filters/http/common/pass_through_filter.h" | ||
|
||
#include "library/common/extensions/filters/http/socket_tag/filter.pb.h" | ||
#include "library/common/types/c_types.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace HttpFilters { | ||
namespace SocketTag { | ||
|
||
/** | ||
* Filter to set upstream socket tags based on a request header. | ||
* See: https://source.android.com/devices/tech/datausage/tags-explained | ||
*/ | ||
class SocketTagFilter final : public Http::PassThroughFilter, | ||
public Logger::Loggable<Logger::Id::filter> { | ||
public: | ||
// Http::PassThroughDecoderFilter | ||
Http::FilterHeadersStatus decodeHeaders(Http::RequestHeaderMap& request_headers, bool) override; | ||
}; | ||
|
||
} // namespace SocketTag | ||
} // namespace HttpFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
13 changes: 13 additions & 0 deletions
13
library/common/extensions/filters/http/socket_tag/filter.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto3"; | ||
|
||
package envoymobile.extensions.filters.http.socket_tag; | ||
|
||
// Configuration for the socket tagging filer. This filter uses data from the | ||
// x-envoy-mobile-socket-tag request header to apply an Android Socket Tag to the upstream | ||
// socket. | ||
// See: https://source.android.com/devices/tech/datausage/tags-explained | ||
// See: https://developer.android.com/reference/android/net/TrafficStats#setThreadStatsTag(int) | ||
// See: https://developer.android.com/reference/android/net/TrafficStats#setThreadStatsUid(int) | ||
// See: https://developer.android.com/reference/android/net/TrafficStats#tagSocket(java.net.Socket) | ||
message SocketTag { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.