GRPC Core  18.0.0
xds_http_filters.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_CORE_EXT_XDS_XDS_HTTP_FILTERS_H
18 #define GRPC_CORE_EXT_XDS_XDS_HTTP_FILTERS_H
19 
21 
22 #include <memory>
23 #include <set>
24 #include <string>
25 
26 #include "absl/status/statusor.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/string_view.h"
30 #include "upb/def.h"
31 
32 #include <grpc/grpc.h>
33 
35 #include "src/core/lib/json/json.h"
36 
37 namespace grpc_core {
38 
39 extern const char* kXdsHttpRouterFilterConfigName;
40 
42  public:
43  struct FilterConfig {
44  absl::string_view config_proto_type_name;
46 
47  bool operator==(const FilterConfig& other) const {
49  config == other.config;
50  }
51  std::string ToString() const {
52  return absl::StrCat("{config_proto_type_name=", config_proto_type_name,
53  " config=", config.Dump(), "}");
54  }
55  };
56 
57  // Service config data for the filter, returned by GenerateServiceConfig().
59  // The top-level field name in the method config.
60  // Filter implementations should use their primary config proto type
61  // name for this.
62  // The value of this field in the method config will be a JSON array,
63  // which will be populated with the elements returned by each filter
64  // instance.
66  // The element to add to the JSON array.
67  std::string element;
68  };
69 
70  virtual ~XdsHttpFilterImpl() = default;
71 
72  // Loads the proto message into the upb symtab.
73  virtual void PopulateSymtab(upb_symtab* symtab) const = 0;
74 
75  // Generates a Config from the xDS filter config proto.
76  // Used for the top-level config in the HCM HTTP filter list.
77  virtual absl::StatusOr<FilterConfig> GenerateFilterConfig(
78  upb_strview serialized_filter_config, upb_arena* arena) const = 0;
79 
80  // Generates a Config from the xDS filter config proto.
81  // Used for the typed_per_filter_config override in VirtualHost and Route.
82  virtual absl::StatusOr<FilterConfig> GenerateFilterConfigOverride(
83  upb_strview serialized_filter_config, upb_arena* arena) const = 0;
84 
85  // C-core channel filter implementation.
86  virtual const grpc_channel_filter* channel_filter() const = 0;
87 
88  // Modifies channel args that may affect service config parsing (not
89  // visible to the channel as a whole).
90  // Takes ownership of args. Caller takes ownership of return value.
92  return args;
93  }
94 
95  // Function to convert the Configs into a JSON string to be added to the
96  // per-method part of the service config.
97  // The hcm_filter_config comes from the HttpConnectionManager config.
98  // The filter_config_override comes from the first of the ClusterWeight,
99  // Route, or VirtualHost entries that it is found in, or null if
100  // there is no override in any of those locations.
101  virtual absl::StatusOr<ServiceConfigJsonEntry> GenerateServiceConfig(
102  const FilterConfig& hcm_filter_config,
103  const FilterConfig* filter_config_override) const = 0;
104 
105  // Returns true if the filter is supported on clients; false otherwise
106  virtual bool IsSupportedOnClients() const = 0;
107 
108  // Returns true if the filter is supported on servers; false otherwise
109  virtual bool IsSupportedOnServers() const = 0;
110 };
111 
113  public:
114  static void RegisterFilter(
115  std::unique_ptr<XdsHttpFilterImpl> filter,
116  const std::set<absl::string_view>& config_proto_type_names);
117 
118  static const XdsHttpFilterImpl* GetFilterForType(
119  absl::string_view proto_type_name);
120 
121  static void PopulateSymtab(upb_symtab* symtab);
122 
123  // Global init and shutdown.
124  static void Init();
125  static void Shutdown();
126 };
127 
128 } // namespace grpc_core
129 
130 #endif /* GRPC_CORE_EXT_XDS_XDS_HTTP_FILTERS_H */
Definition: json.h:38
std::string Dump(int indent=0) const
Definition: json_writer.cc:334
Definition: xds_http_filters.h:41
virtual ~XdsHttpFilterImpl()=default
virtual bool IsSupportedOnServers() const =0
virtual const grpc_channel_filter * channel_filter() const =0
virtual absl::StatusOr< ServiceConfigJsonEntry > GenerateServiceConfig(const FilterConfig &hcm_filter_config, const FilterConfig *filter_config_override) const =0
virtual absl::StatusOr< FilterConfig > GenerateFilterConfigOverride(upb_strview serialized_filter_config, upb_arena *arena) const =0
virtual grpc_channel_args * ModifyChannelArgs(grpc_channel_args *args) const
Definition: xds_http_filters.h:91
virtual void PopulateSymtab(upb_symtab *symtab) const =0
virtual absl::StatusOr< FilterConfig > GenerateFilterConfig(upb_strview serialized_filter_config, upb_arena *arena) const =0
virtual bool IsSupportedOnClients() const =0
Definition: xds_http_filters.h:112
static void RegisterFilter(std::unique_ptr< XdsHttpFilterImpl > filter, const std::set< absl::string_view > &config_proto_type_names)
Definition: xds_http_filters.cc:78
static const XdsHttpFilterImpl * GetFilterForType(absl::string_view proto_type_name)
Definition: xds_http_filters.cc:87
static void Init()
Definition: xds_http_filters.cc:100
static void Shutdown()
Definition: xds_http_filters.cc:109
static void PopulateSymtab(upb_symtab *symtab)
Definition: xds_http_filters.cc:94
Round Robin Policy.
Definition: backend_metric.cc:26
const char * kXdsHttpRouterFilterConfigName
Definition: xds_http_filters.cc:27
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: channel_stack.h:107
Definition: xds_http_filters.h:43
Json config
Definition: xds_http_filters.h:45
std::string ToString() const
Definition: xds_http_filters.h:51
bool operator==(const FilterConfig &other) const
Definition: xds_http_filters.h:47
absl::string_view config_proto_type_name
Definition: xds_http_filters.h:44
Definition: xds_http_filters.h:58
std::string element
Definition: xds_http_filters.h:67
std::string service_config_field_name
Definition: xds_http_filters.h:65
upb_arena * arena
Definition: xds_api.cc:909
upb_symtab * symtab
Definition: xds_api.cc:908