• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/server/xds_channel_stack_modifier.h"
20 
21 #include <grpc/support/port_platform.h>
22 
23 #include <algorithm>
24 #include <initializer_list>
25 #include <string>
26 
27 #include "src/core/config/core_configuration.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/channel/channel_stack.h"
30 #include "src/core/lib/surface/channel_init.h"
31 #include "src/core/lib/surface/channel_stack_type.h"
32 #include "src/core/util/useful.h"
33 
34 namespace grpc_core {
35 namespace {
36 
XdsChannelStackModifierArgCopy(void * p)37 void* XdsChannelStackModifierArgCopy(void* p) {
38   XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
39   return arg->Ref().release();
40 }
41 
XdsChannelStackModifierArgDestroy(void * p)42 void XdsChannelStackModifierArgDestroy(void* p) {
43   XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
44   arg->Unref();
45 }
46 
XdsChannelStackModifierArgCmp(void * p,void * q)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 
ModifyChannelStack(ChannelStackBuilder & builder)60 void XdsChannelStackModifier::ModifyChannelStack(ChannelStackBuilder& builder) {
61   // Insert the filters after predicate filters if present.
62   auto insert_before = builder.mutable_stack()->end();
63   for (auto it = builder.mutable_stack()->begin();
64        it != builder.mutable_stack()->end(); ++it) {
65     for (absl::string_view predicate_name : {"server", "census_server"}) {
66       if (predicate_name == (*it)->name.name()) insert_before = it + 1;
67     }
68   }
69   for (const grpc_channel_filter* filter : filters_) {
70     insert_before = builder.mutable_stack()->insert(insert_before, filter);
71     ++insert_before;
72   }
73 }
74 
MakeChannelArg() const75 grpc_arg XdsChannelStackModifier::MakeChannelArg() const {
76   return grpc_channel_arg_pointer_create(
77       const_cast<char*>(kXdsChannelStackModifierChannelArgName),
78       const_cast<XdsChannelStackModifier*>(this), &kChannelArgVtable);
79 }
80 
ChannelArgName()81 absl::string_view XdsChannelStackModifier::ChannelArgName() {
82   return kXdsChannelStackModifierChannelArgName;
83 }
84 
85 RefCountedPtr<XdsChannelStackModifier>
GetFromChannelArgs(const grpc_channel_args & args)86 XdsChannelStackModifier::GetFromChannelArgs(const grpc_channel_args& args) {
87   XdsChannelStackModifier* config_selector_provider =
88       grpc_channel_args_find_pointer<XdsChannelStackModifier>(
89           &args, kXdsChannelStackModifierChannelArgName);
90   return config_selector_provider != nullptr ? config_selector_provider->Ref()
91                                              : nullptr;
92 }
93 
RegisterXdsChannelStackModifier(CoreConfiguration::Builder * builder)94 void RegisterXdsChannelStackModifier(CoreConfiguration::Builder* builder) {
95   builder->channel_init()->RegisterPostProcessor(
96       GRPC_SERVER_CHANNEL,
97       ChannelInit::PostProcessorSlot::kXdsChannelStackModifier,
98       [](ChannelStackBuilder& builder) {
99         auto channel_stack_modifier =
100             builder.channel_args().GetObjectRef<XdsChannelStackModifier>();
101         if (channel_stack_modifier != nullptr) {
102           return channel_stack_modifier->ModifyChannelStack(builder);
103         }
104       });
105 }
106 
107 }  // namespace grpc_core
108