Line data Source code
1 : /*
2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "bgp/bgp_show_handler.h"
7 :
8 : #include "base/regex.h"
9 : #include "bgp/bgp_peer_internal_types.h"
10 : #include "bgp/bgp_server.h"
11 : #include "bgp/bgp_table.h"
12 : #include "bgp/routing-instance/path_resolver.h"
13 : #include "bgp/routing-instance/routing_instance.h"
14 :
15 : using contrail::regex;
16 : using contrail::regex_match;
17 : using contrail::regex_search;
18 : using std::string;
19 : using std::vector;
20 :
21 : //
22 : // Fill in information for list of path resolvers.
23 : //
24 : // Allows regular and summary introspect to share code.
25 : //
26 0 : static bool FillPathResolverInfoList(const BgpSandeshContext *bsc,
27 : bool summary, uint32_t page_limit, uint32_t iter_limit,
28 : const string &start_instance, const string &search_string,
29 : vector<ShowPathResolver> *spr_list, string *next_instance) {
30 0 : regex search_expr(search_string);
31 0 : RoutingInstanceMgr *rim = bsc->bgp_server->routing_instance_mgr();
32 : RoutingInstanceMgr::const_name_iterator it1 =
33 0 : rim->name_clower_bound(start_instance);
34 0 : for (uint32_t iter_count = 0; it1 != rim->name_cend();
35 0 : ++it1, ++iter_count) {
36 0 : const RoutingInstance *rtinstance = it1->second;
37 0 : for (RoutingInstance::RouteTableList::const_iterator it2 =
38 0 : rtinstance->GetTables().begin();
39 0 : it2 != rtinstance->GetTables().end(); ++it2, ++iter_count) {
40 0 : const BgpTable *table = it2->second;
41 0 : if ((!regex_search(table->name(), search_expr)) &&
42 0 : (search_string != "deleted" || !table->IsDeleted())) {
43 0 : continue;
44 : }
45 0 : const PathResolver *path_resolver = table->path_resolver();
46 0 : if (!path_resolver)
47 0 : continue;
48 0 : ShowPathResolver spr;
49 0 : path_resolver->FillShowInfo(&spr, summary);
50 0 : if (spr.get_path_count() == 0 &&
51 0 : spr.get_modified_path_count() == 0 &&
52 0 : spr.get_nexthop_count() == 0 &&
53 0 : spr.get_modified_nexthop_count() == 0)
54 0 : continue;
55 0 : spr_list->push_back(spr);
56 0 : }
57 0 : if (spr_list->size() >= page_limit)
58 0 : break;
59 0 : if (iter_count >= iter_limit)
60 0 : break;
61 : }
62 :
63 : // All done if we've looked at all instances.
64 0 : if (it1 == rim->name_cend() || ++it1 == rim->name_end())
65 0 : return true;
66 :
67 : // Return true if we've reached the page limit, false if we've reached the
68 : // iteration limit.
69 0 : bool done = spr_list->size() >= page_limit;
70 0 : *next_instance = it1->second->name();
71 0 : return done;
72 0 : }
73 :
74 : //
75 : // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
76 : //
77 : template <>
78 0 : bool BgpShowHandler<ShowPathResolverReq, ShowPathResolverReqIterate,
79 : ShowPathResolverResp, ShowPathResolver>::CallbackCommon(
80 : const BgpSandeshContext *bsc, Data *data) {
81 0 : uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
82 0 : uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
83 0 : string next_instance;
84 0 : bool done = FillPathResolverInfoList(bsc, false, page_limit, iter_limit,
85 0 : data->next_entry, data->search_string, &data->show_list,
86 : &next_instance);
87 0 : if (!next_instance.empty())
88 0 : SaveContextToData(next_instance, done, data);
89 0 : return done;
90 0 : }
91 :
92 : //
93 : // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
94 : //
95 : template <>
96 0 : void BgpShowHandler<ShowPathResolverReq,
97 : ShowPathResolverReqIterate, ShowPathResolverResp,
98 : ShowPathResolver>::FillShowList(ShowPathResolverResp *resp,
99 : const vector<ShowPathResolver> &show_list) {
100 0 : resp->set_resolvers(show_list);
101 0 : }
102 :
103 : //
104 : // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
105 : //
106 : template <>
107 0 : bool BgpShowHandler<ShowPathResolverSummaryReq,
108 : ShowPathResolverSummaryReqIterate, ShowPathResolverSummaryResp,
109 : ShowPathResolver>::CallbackCommon(
110 : const BgpSandeshContext *bsc, Data *data) {
111 0 : uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
112 0 : uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
113 0 : string next_instance;
114 0 : bool done = FillPathResolverInfoList(bsc, true, page_limit, iter_limit,
115 0 : data->next_entry, data->search_string, &data->show_list,
116 : &next_instance);
117 0 : if (!next_instance.empty())
118 0 : SaveContextToData(next_instance, done, data);
119 0 : return done;
120 0 : }
121 :
122 : //
123 : // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
124 : //
125 : template <>
126 0 : void BgpShowHandler<ShowPathResolverSummaryReq,
127 : ShowPathResolverSummaryReqIterate, ShowPathResolverSummaryResp,
128 : ShowPathResolver>::FillShowList(ShowPathResolverSummaryResp *resp,
129 : const vector<ShowPathResolver> &show_list) {
130 0 : resp->set_resolvers(show_list);
131 0 : }
132 :
133 : //
134 : // Handler for ShowPathResolverReq.
135 : //
136 0 : void ShowPathResolverReq::HandleRequest() const {
137 0 : RequestPipeline::PipeSpec ps(this);
138 0 : RequestPipeline::StageSpec s1;
139 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
140 :
141 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
142 : s1.cbFn_ = boost::bind(&BgpShowHandler<
143 : ShowPathResolverReq,
144 : ShowPathResolverReqIterate,
145 : ShowPathResolverResp,
146 0 : ShowPathResolver>::Callback, _1, _2, _3, _4, _5);
147 : s1.allocFn_ = BgpShowHandler<
148 : ShowPathResolverReq,
149 : ShowPathResolverReqIterate,
150 : ShowPathResolverResp,
151 0 : ShowPathResolver>::CreateData;
152 0 : s1.instances_.push_back(0);
153 0 : ps.stages_.push_back(s1);
154 0 : RequestPipeline rp(ps);
155 0 : }
156 :
157 : //
158 : // Handler for ShowPathResolverReqIterate.
159 : //
160 0 : void ShowPathResolverReqIterate::HandleRequest() const {
161 0 : RequestPipeline::PipeSpec ps(this);
162 0 : RequestPipeline::StageSpec s1;
163 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
164 :
165 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
166 : s1.cbFn_ = boost::bind(&BgpShowHandler<
167 : ShowPathResolverReq,
168 : ShowPathResolverReqIterate,
169 : ShowPathResolverResp,
170 0 : ShowPathResolver>::CallbackIterate, _1, _2, _3, _4, _5);
171 : s1.allocFn_ = BgpShowHandler<
172 : ShowPathResolverReq,
173 : ShowPathResolverReqIterate,
174 : ShowPathResolverResp,
175 0 : ShowPathResolver>::CreateData;
176 0 : s1.instances_.push_back(0);
177 0 : ps.stages_.push_back(s1);
178 0 : RequestPipeline rp(ps);
179 0 : }
180 :
181 : //
182 : // Handler for ShowPathResolverSummaryReq.
183 : //
184 0 : void ShowPathResolverSummaryReq::HandleRequest() const {
185 0 : RequestPipeline::PipeSpec ps(this);
186 0 : RequestPipeline::StageSpec s1;
187 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
188 :
189 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
190 : s1.cbFn_ = boost::bind(&BgpShowHandler<
191 : ShowPathResolverSummaryReq,
192 : ShowPathResolverSummaryReqIterate,
193 : ShowPathResolverSummaryResp,
194 0 : ShowPathResolver>::Callback, _1, _2, _3, _4, _5);
195 : s1.allocFn_ = BgpShowHandler<
196 : ShowPathResolverSummaryReq,
197 : ShowPathResolverSummaryReqIterate,
198 : ShowPathResolverSummaryResp,
199 0 : ShowPathResolver>::CreateData;
200 0 : s1.instances_.push_back(0);
201 0 : ps.stages_.push_back(s1);
202 0 : RequestPipeline rp(ps);
203 0 : }
204 :
205 : //
206 : // Handler for ShowPathResolverSummaryReqIterate.
207 : //
208 0 : void ShowPathResolverSummaryReqIterate::HandleRequest() const {
209 0 : RequestPipeline::PipeSpec ps(this);
210 0 : RequestPipeline::StageSpec s1;
211 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
212 :
213 0 : s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
214 : s1.cbFn_ = boost::bind(&BgpShowHandler<
215 : ShowPathResolverSummaryReq,
216 : ShowPathResolverSummaryReqIterate,
217 : ShowPathResolverSummaryResp,
218 0 : ShowPathResolver>::CallbackIterate, _1, _2, _3, _4, _5);
219 : s1.allocFn_ = BgpShowHandler<
220 : ShowPathResolverSummaryReq,
221 : ShowPathResolverSummaryReqIterate,
222 : ShowPathResolverSummaryResp,
223 0 : ShowPathResolver>::CreateData;
224 0 : s1.instances_.push_back(0);
225 0 : ps.stages_.push_back(s1);
226 0 : RequestPipeline rp(ps);
227 0 : }
|