Debian Codegraph @

src/core/ext/xds/xds_channel_stack_modifier.cc

raw
1 //
2 //
3 // Copyright 2021 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/xds/xds_channel_stack_modifier.h"
22
23 #include <limits.h>
24 #include <string.h>
25
26 #include <algorithm>
27
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/channel/channel_stack.h"
30 #include "src/core/lib/config/core_configuration.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/surface/channel_stack_type.h"
33
34 namespace grpc_core {
35 namespace {
36
37 void* XdsChannelStackModifierArgCopy(void* p) {
38 XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
39 return arg->Ref().release();
40 }
41
42 void XdsChannelStackModifierArgDestroy(void* p) {
43 XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
44 arg->Unref();
45 }
46
47 int XdsChannelStackModifierArgCmp(void* p, void* q) {
48 return QsortCompare(p, q);
49 }
50
51 const grpc_arg_pointer_vtable kChannelArgVtable = {
52 XdsChannelStackModifierArgCopy, XdsChannelStackModifierArgDestroy,
53 XdsChannelStackModifierArgCmp};
54
55 const char* kXdsChannelStackModifierChannelArgName =
56 "grpc.internal.xds_channel_stack_modifier";
57
58 } // namespace
59
60 bool XdsChannelStackModifier::ModifyChannelStack(ChannelStackBuilder* builder) {
61 // Insert the filters after the census filter if present.
62 auto it = builder->mutable_stack()->begin();
63 while (it != builder->mutable_stack()->end()) {
64 const char* filter_name_at_it = (*it)->name;
65 if (strcmp("census_server", filter_name_at_it) == 0 ||
66 strcmp("opencensus_server", filter_name_at_it) == 0) {
67 break;
68 }
69 ++it;
70 }
71 if (it == builder->mutable_stack()->end()) {
72 // No census filter found. Reset iterator to the beginning. This will result
73 // in prepending the list of xDS HTTP filters to the current stack. Note
74 // that this stage is run before the stage that adds the top server filter,
75 // resulting in these filters being finally placed after the `server`
76 // filter.
77 it = builder->mutable_stack()->begin();
78 } else {
79 ++it;
80 }
81 for (const grpc_channel_filter* filter : filters_) {
82 it = builder->mutable_stack()->insert(it, filter);
83 ++it;
84 }
85 return true;
86 }
87
88 grpc_arg XdsChannelStackModifier::MakeChannelArg() const {
89 return grpc_channel_arg_pointer_create(
90 const_cast<char*>(kXdsChannelStackModifierChannelArgName),
91 const_cast<XdsChannelStackModifier*>(this), &kChannelArgVtable);
92 }
93
94 absl::string_view XdsChannelStackModifier::ChannelArgName() {
95 return kXdsChannelStackModifierChannelArgName;
96 }
97
98 RefCountedPtr<XdsChannelStackModifier>
99 XdsChannelStackModifier::GetFromChannelArgs(const grpc_channel_args& args) {
100 XdsChannelStackModifier* config_selector_provider =
101 grpc_channel_args_find_pointer<XdsChannelStackModifier>(
102 &args, kXdsChannelStackModifierChannelArgName);
103 return config_selector_provider != nullptr ? config_selector_provider->Ref()
104 : nullptr;
105 }
106
107 void RegisterXdsChannelStackModifier(CoreConfiguration::Builder* builder) {
108 builder->channel_init()->RegisterStage(
109 GRPC_SERVER_CHANNEL, INT_MAX, [](ChannelStackBuilder* builder) {
110 auto channel_stack_modifier =
111 builder->channel_args().GetObjectRef<XdsChannelStackModifier>();
112 if (channel_stack_modifier != nullptr) {
113 return channel_stack_modifier->ModifyChannelStack(builder);
114 }
115 return true;
116 });
117 }
118
119 } // namespace grpc_core