Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "bgp/bgp_sandesh.h"
7 : #include "bgp/bgp_show_route.h"
8 :
9 : #include <boost/assign/list_of.hpp>
10 : #include <sandesh/request_pipeline.h>
11 :
12 : #include "base/regex.h"
13 : #include "bgp/bgp_peer_internal_types.h"
14 : #include "bgp/bgp_route.h"
15 : #include "bgp/bgp_server.h"
16 : #include "bgp/bgp_table.h"
17 : #include "bgp/routing-instance/routing_instance.h"
18 :
19 : using boost::assign::list_of;
20 : using std::unique_ptr;
21 : using std::string;
22 : using std::vector;
23 :
24 0 : static bool IsLess(const ShowRoute &lhs, const ShowRoute &rhs,
25 : const BgpSandeshContext *bsc, const string &table_name) {
26 : BgpTable *table = static_cast<BgpTable *>
27 0 : (bsc->bgp_server->database()->FindTable(table_name));
28 0 : if (!table) {
29 0 : return false;
30 : }
31 0 : unique_ptr<DBEntry> lhs_entry = table->AllocEntryStr(lhs.get_prefix());
32 0 : unique_ptr<DBEntry> rhs_entry = table->AllocEntryStr(rhs.get_prefix());
33 :
34 0 : Route *lhs_route = static_cast<Route *>(lhs_entry.get());
35 0 : Route *rhs_route = static_cast<Route *>(rhs_entry.get());
36 :
37 0 : return lhs_route->IsLess(*rhs_route);
38 0 : }
39 :
40 0 : static bool IsLess(const ShowRouteTable &lhs, const ShowRouteTable &rhs,
41 : const BgpSandeshContext *bsc, const string &table_name) {
42 0 : if (lhs.get_routing_instance() < rhs.get_routing_instance()) {
43 0 : return true;
44 : }
45 0 : if (lhs.get_routing_instance() == rhs.get_routing_instance()) {
46 0 : return lhs.get_routing_table_name() < rhs.get_routing_table_name();
47 : }
48 0 : return false;
49 : }
50 :
51 : template <class T>
52 : void MergeSort(vector<T> *result, vector<const vector<T> *> *input, int limit,
53 : const BgpSandeshContext *bsc, const string &table_name);
54 :
55 0 : int MergeValues(ShowRoute *result, vector<const ShowRoute *> *input,
56 : int limit, const BgpSandeshContext *bsc) {
57 0 : assert(input->size() == 1);
58 0 : *result = *input->at(0);
59 0 : return 1;
60 : }
61 :
62 0 : int MergeValues(ShowRouteTable *result, vector<const ShowRouteTable *> *input,
63 : int limit, const BgpSandeshContext *bsc) {
64 0 : vector<const vector<ShowRoute> *> list;
65 0 : result->set_routing_instance(input->at(0)->get_routing_instance());
66 0 : result->set_routing_table_name(input->at(0)->get_routing_table_name());
67 0 : result->set_deleted(input->at(0)->get_deleted());
68 0 : result->set_deleted_at(input->at(0)->get_deleted_at());
69 0 : result->set_prefixes(input->at(0)->get_prefixes());
70 0 : result->set_primary_paths(input->at(0)->get_primary_paths());
71 0 : result->set_secondary_paths(input->at(0)->get_secondary_paths());
72 0 : result->set_infeasible_paths(input->at(0)->get_infeasible_paths());
73 0 : result->set_stale_paths(input->at(0)->get_stale_paths());
74 0 : result->set_llgr_stale_paths(input->at(0)->get_llgr_stale_paths());
75 0 : result->set_paths(input->at(0)->get_paths());
76 0 : result->set_listeners(input->at(0)->get_listeners());
77 :
78 0 : int count = 0;
79 0 : for (size_t i = 0; i < input->size(); ++i) {
80 0 : if (input->at(i)->get_routes().size())
81 0 : list.push_back(&input->at(i)->get_routes());
82 : }
83 0 : MergeSort(const_cast<vector<ShowRoute> *>(&result->get_routes()), &list,
84 : limit ? limit - count : 0, bsc,
85 0 : input->at(0)->get_routing_table_name());
86 0 : count += result->get_routes().size();
87 0 : return count;
88 0 : }
89 :
90 : //
91 : // Merge n number of vectors in result.
92 : // Input is a vector of pointers to vector.
93 : //
94 : template <class T>
95 0 : void MergeSort(vector<T> *result, vector<const vector<T> *> *input, int limit,
96 : const BgpSandeshContext *bsc, const string &table_name) {
97 0 : size_t size = input->size();
98 0 : vector<size_t> index(size, 0);
99 0 : int count = 0;
100 0 : while (limit == 0 || count < limit) {
101 0 : size_t best_index = size;
102 0 : const T *best = NULL;
103 0 : for (size_t i = 0; i < size; ++i) {
104 0 : if (index[i] == input->at(i)->size())
105 0 : continue;
106 0 : if (best == NULL ||
107 0 : IsLess(input->at(i)->at(index[i]), *best, bsc, table_name)) {
108 0 : best = &input->at(i)->at(index[i]);
109 0 : best_index = i;
110 0 : continue;
111 : }
112 : }
113 0 : if (best_index >= size)
114 0 : break;
115 0 : T table;
116 0 : vector<const T *> list;
117 0 : for (size_t j = best_index; j < size; ++j) {
118 0 : if (index[j] == input->at(j)->size())
119 0 : continue;
120 0 : if (IsLess(input->at(j)->at(index[j]), *best, bsc, table_name) ||
121 0 : IsLess(*best, input->at(j)->at(index[j]), bsc, table_name)) {
122 0 : continue;
123 : }
124 0 : list.push_back(&input->at(j)->at(index[j]));
125 0 : index[j]++;
126 : }
127 0 : count += MergeValues(&table, &list, limit ? limit - count : 0, bsc);
128 0 : result->push_back(table);
129 : }
130 0 : }
131 :
132 : char ShowRouteHandler::kIterSeparator[] = "||";
133 0 : uint32_t ShowRouteHandler::GetMaxCount(bool test_mode) {
134 0 : if (test_mode) {
135 0 : return kUnitTestMaxCount;
136 : } else {
137 0 : return kMaxCount;
138 : }
139 : }
140 :
141 0 : ShowRouteHandler::ShowRouteHandler(const ShowRouteReq *req, int inst_id) :
142 0 : req_(req), inst_id_(inst_id), prefix_expr_(req->get_prefix()) {
143 0 : }
144 :
145 : // Search for interesting prefixes in a given table for given partition
146 0 : void ShowRouteHandler::BuildShowRouteTable(BgpTable *table,
147 : vector<ShowRoute> *route_list, int count) {
148 0 : if (inst_id_ >= table->PartitionCount())
149 0 : return;
150 : DBTablePartition *partition =
151 0 : static_cast<DBTablePartition *>(table->GetTablePartition(inst_id_));
152 0 : BgpRoute *route = NULL;
153 :
154 0 : if (table->name() == req_->get_start_routing_table()) {
155 : unique_ptr<DBEntry> key =
156 0 : table->AllocEntryStr(req_->get_start_prefix());
157 0 : route = static_cast<BgpRoute *>(partition->lower_bound(key.get()));
158 0 : } else {
159 0 : route = static_cast<BgpRoute *>(partition->GetFirst());
160 : }
161 0 : for (int i = 0; route && (!count || i < count);
162 0 : route = static_cast<BgpRoute *>(partition->GetNext(route)), ++i) {
163 0 : if (!MatchPrefix(req_->get_prefix(), route,
164 0 : req_->get_longer_match(),
165 0 : req_->get_shorter_match()))
166 0 : continue;
167 0 : ShowRoute show_route;
168 0 : route->FillRouteInfo(table, &show_route, req_->get_source(),
169 0 : req_->get_protocol());
170 0 : if (!show_route.get_paths().empty())
171 0 : route_list->push_back(show_route);
172 0 : }
173 : }
174 :
175 0 : bool ShowRouteHandler::MatchPrefix(const string &expected_prefix,
176 : BgpRoute *route, bool longer_match,
177 : bool shorter_match) {
178 0 : if (expected_prefix.empty())
179 0 : return true;
180 0 : if (!longer_match && !shorter_match)
181 0 : return regex_search(route->ToString(), prefix_expr_);
182 :
183 : // Do longer match.
184 0 : if (longer_match && route->IsMoreSpecific(expected_prefix))
185 0 : return true;
186 :
187 : // Do shorter match.
188 0 : if (shorter_match && route->IsLessSpecific(expected_prefix))
189 0 : return true;
190 :
191 0 : return false;
192 : }
193 :
194 0 : bool ShowRouteHandler::match(const string &expected, const string &actual) {
195 0 : if (expected == "")
196 0 : return true;
197 0 : return expected == actual;
198 : }
199 :
200 0 : RequestPipeline::InstData *ShowRouteHandler::CreateData(int stage) {
201 0 : return static_cast<RequestPipeline::InstData *>(new ShowRouteData);
202 : }
203 :
204 0 : uint32_t ShowRouteHandler::GetMaxRouteCount(const ShowRouteReq *req) {
205 : BgpSandeshContext *bsc =
206 0 : static_cast<BgpSandeshContext *>(req->client_context());
207 : uint32_t max_count =
208 0 : ShowRouteHandler::GetMaxCount(bsc->test_mode());
209 :
210 0 : uint32_t route_count = req->get_count() + 1;
211 0 : if (!req->get_count() || (req->get_count() > max_count)) {
212 : // Set the count to our default if input is zero or too high
213 0 : route_count = max_count + 1;
214 : }
215 0 : return route_count;
216 : }
217 :
218 : // Get the information from req_iterate and fill into req
219 0 : bool ShowRouteHandler::ConvertReqIterateToReq(
220 : const ShowRouteReqIterate *req_iterate, ShowRouteReq *req) {
221 : // First, set the context from the original request since we might return
222 : // due to parsing errors.
223 0 : req->set_context(req_iterate->context());
224 :
225 : // Format of route_info:
226 : // UserRI||UserRT||UserPfx||NextRI||NextRT||NextPfx||count||longer_match||
227 : // shorter_match
228 : //
229 : // User* values were entered by the user and Next* values indicate 'where'
230 : // we need to start this iteration.
231 0 : string route_info = req_iterate->get_route_info();
232 0 : size_t sep_size = strlen(kIterSeparator);
233 :
234 0 : size_t pos1 = route_info.find(kIterSeparator);
235 0 : if (pos1 == string::npos) {
236 0 : return false;
237 : }
238 0 : string user_ri = route_info.substr(0, pos1);
239 :
240 0 : size_t pos2 = route_info.find(kIterSeparator, (pos1 + sep_size));
241 0 : if (pos2 == string::npos) {
242 0 : return false;
243 : }
244 : string user_rt = route_info.substr((pos1 + sep_size),
245 0 : pos2 - (pos1 + sep_size));
246 :
247 0 : size_t pos3 = route_info.find(kIterSeparator, (pos2 + sep_size));
248 0 : if (pos3 == string::npos) {
249 0 : return false;
250 : }
251 : string user_prefix = route_info.substr((pos2 + sep_size),
252 0 : pos3 - (pos2 + sep_size));
253 :
254 0 : size_t pos4 = route_info.find(kIterSeparator, (pos3 + sep_size));
255 0 : if (pos4 == string::npos) {
256 0 : return false;
257 : }
258 : string next_ri = route_info.substr((pos3 + sep_size),
259 0 : pos4 - (pos3 + sep_size));
260 :
261 0 : size_t pos5 = route_info.find(kIterSeparator, (pos4 + sep_size));
262 0 : if (pos5 == string::npos) {
263 0 : return false;
264 : }
265 : string next_rt = route_info.substr((pos4 + sep_size),
266 0 : pos5 - (pos4 + sep_size));
267 :
268 0 : size_t pos6 = route_info.find(kIterSeparator, (pos5 + sep_size));
269 0 : if (pos6 == string::npos) {
270 0 : return false;
271 : }
272 : string next_prefix = route_info.substr((pos5 + sep_size),
273 0 : pos6 - (pos5 + sep_size));
274 :
275 0 : size_t pos7 = route_info.find(kIterSeparator, (pos6 + sep_size));
276 0 : if (pos7 == string::npos) {
277 0 : return false;
278 : }
279 : string count_str = route_info.substr((pos6 + sep_size),
280 0 : pos7 - (pos6 + sep_size));
281 :
282 0 : size_t pos8 = route_info.find(kIterSeparator, (pos7 + sep_size));
283 0 : if (pos8 == string::npos) {
284 0 : return false;
285 : }
286 : string user_source = route_info.substr((pos7 + sep_size),
287 0 : pos8 - (pos7 + sep_size));
288 :
289 0 : size_t pos9 = route_info.find(kIterSeparator, (pos8 + sep_size));
290 0 : if (pos9 == string::npos) {
291 0 : return false;
292 : }
293 : string user_protocol = route_info.substr((pos8 + sep_size),
294 0 : pos9 - (pos8 + sep_size));
295 :
296 0 : size_t pos10 = route_info.find(kIterSeparator, (pos9 + sep_size));
297 0 : if (pos10 == string::npos) {
298 0 : return false;
299 : }
300 : string user_family = route_info.substr((pos9 + sep_size),
301 0 : pos10 - (pos9 + sep_size));
302 :
303 0 : size_t pos11 = route_info.find(kIterSeparator, (pos10 + sep_size));
304 0 : if (pos11 == string::npos) {
305 0 : return false;
306 : }
307 : string longer_match = route_info.substr((pos10 + sep_size),
308 0 : pos11 - (pos10 + sep_size));
309 :
310 0 : string shorter_match = route_info.substr(pos11 + sep_size);
311 :
312 0 : req->set_routing_instance(user_ri);
313 0 : req->set_routing_table(user_rt);
314 0 : req->set_prefix(user_prefix);
315 0 : req->set_start_routing_instance(next_ri);
316 0 : req->set_start_routing_table(next_rt);
317 0 : req->set_start_prefix(next_prefix);
318 0 : req->set_count(atoi(count_str.c_str()));
319 0 : req->set_source(user_source);
320 0 : req->set_protocol(user_protocol);
321 0 : req->set_family(user_family);
322 0 : req->set_longer_match(StringToBool(longer_match));
323 0 : req->set_shorter_match(StringToBool(shorter_match));
324 :
325 0 : return true;
326 0 : }
327 :
328 0 : bool ShowRouteHandler::CallbackS1Common(const ShowRouteReq *req, int inst_id,
329 : ShowRouteData *mydata) {
330 0 : uint32_t max_count = ShowRouteHandler::GetMaxRouteCount(req);
331 :
332 0 : ShowRouteHandler handler(req, inst_id);
333 : BgpSandeshContext *bsc =
334 0 : static_cast<BgpSandeshContext *>(req->client_context());
335 0 : RoutingInstanceMgr *rim = bsc->bgp_server->routing_instance_mgr();
336 :
337 0 : string exact_routing_table = req->get_routing_table();
338 0 : string exact_routing_instance;
339 0 : string start_routing_instance;
340 0 : if (exact_routing_table.empty()) {
341 0 : exact_routing_instance = req->get_routing_instance();
342 : } else {
343 : exact_routing_instance =
344 0 : RoutingInstance::GetVrfFromTableName(exact_routing_table);
345 : }
346 0 : if (exact_routing_instance.empty()) {
347 0 : start_routing_instance = req->get_start_routing_instance();
348 : } else {
349 0 : start_routing_instance = exact_routing_instance;
350 : }
351 :
352 : RoutingInstanceMgr::name_iterator i =
353 0 : rim->name_lower_bound(start_routing_instance);
354 0 : uint32_t count = 0;
355 0 : while (i != rim->name_end()) {
356 0 : if (!handler.match(exact_routing_instance, i->first)) {
357 0 : break;
358 : }
359 0 : RoutingInstance::RouteTableList::const_iterator j;
360 0 : if (req->get_start_routing_instance() == i->first) {
361 0 : j = i->second->GetTables().
362 0 : lower_bound(req->get_start_routing_table());
363 : } else {
364 0 : j = i->second->GetTables().begin();
365 : }
366 0 : for (; j != i->second->GetTables().end(); ++j) {
367 0 : BgpTable *table = j->second;
368 0 : if (!req->get_family().empty() && req->get_family() !=
369 0 : Address::FamilyToTableString(table->family())) {
370 0 : continue;
371 : }
372 0 : if (!handler.match(req->get_routing_table(), table->name())) {
373 0 : continue;
374 : }
375 0 : ShowRouteTable srt;
376 0 : srt.set_routing_instance(i->first);
377 0 : srt.set_routing_table_name(table->name());
378 0 : srt.set_deleted(table->IsDeleted());
379 0 : srt.set_deleted_at(
380 0 : UTCUsecToString(table->deleter()->delete_time_stamp_usecs()));
381 :
382 : // Encode routing-table stats.
383 0 : srt.set_prefixes(table->Size());
384 0 : srt.set_primary_paths(table->GetPrimaryPathCount());
385 0 : srt.set_secondary_paths(table->GetSecondaryPathCount());
386 0 : srt.set_infeasible_paths(table->GetInfeasiblePathCount());
387 0 : srt.set_stale_paths(table->GetStalePathCount());
388 0 : srt.set_llgr_stale_paths(table->GetLlgrStalePathCount());
389 0 : srt.set_paths(srt.get_primary_paths() + srt.get_secondary_paths());
390 :
391 0 : vector<ShowTableListener> listeners;
392 0 : table->FillListeners(&listeners);
393 0 : srt.set_listeners(listeners);
394 :
395 0 : vector<ShowRoute> route_list;
396 0 : handler.BuildShowRouteTable(table, &route_list,
397 0 : max_count ? max_count - count : 0);
398 0 : if (route_list.size() || table->IsDeleted()) {
399 0 : srt.set_routes(route_list);
400 0 : mydata->route_table_list.push_back(srt);
401 : }
402 0 : count += route_list.size();
403 0 : if (count >= max_count) {
404 0 : break;
405 : }
406 0 : }
407 0 : if (count >= max_count) {
408 0 : break;
409 : }
410 :
411 0 : i++;
412 : }
413 :
414 0 : return true;
415 0 : }
416 :
417 0 : bool ShowRouteHandler::CallbackS1(const Sandesh *sr,
418 : const RequestPipeline::PipeSpec ps,
419 : int stage, int instNum, RequestPipeline::InstData *data) {
420 0 : ShowRouteData *mydata = static_cast<ShowRouteData *>(data);
421 0 : int inst_id = ps.stages_[stage].instances_[instNum];
422 : const ShowRouteReq *req =
423 0 : static_cast<const ShowRouteReq *>(ps.snhRequest_.get());
424 :
425 0 : return CallbackS1Common(req, inst_id, mydata);
426 : }
427 :
428 0 : bool ShowRouteHandler::CallbackS1Iterate(const Sandesh *sr,
429 : const RequestPipeline::PipeSpec ps,
430 : int stage, int instNum, RequestPipeline::InstData *data) {
431 0 : ShowRouteData *mydata = static_cast<ShowRouteData *>(data);
432 0 : int inst_id = ps.stages_[stage].instances_[instNum];
433 : const ShowRouteReqIterate *req_iterate =
434 0 : static_cast<const ShowRouteReqIterate *>(ps.snhRequest_.get());
435 :
436 0 : ShowRouteReq *req = new ShowRouteReq;
437 0 : bool success = ConvertReqIterateToReq(req_iterate, req);
438 0 : if (success) {
439 0 : CallbackS1Common(req, inst_id, mydata);
440 : }
441 0 : req->Release();
442 0 : return true;
443 : }
444 :
445 0 : string ShowRouteHandler::SaveContextAndPopLast(const ShowRouteReq *req,
446 : vector<ShowRouteTable> *route_table_list) {
447 : // If there are no output results for the input parameters, we dont need to
448 : // save anything since there will be no subsequent iteration and there is
449 : // nothing to pop off.
450 0 : if (route_table_list->empty()) {
451 0 : return string("");
452 : }
453 :
454 : // Get the total number of routes that we have collected in this iteration
455 : // after the mergesort.
456 0 : uint32_t total_count = 0;
457 0 : for (size_t i = 0; i < route_table_list->size(); ++i) {
458 0 : total_count += route_table_list->at(i).get_routes().size();
459 : }
460 :
461 : ShowRouteTable *last_route_table =
462 0 : &route_table_list->at(route_table_list->size() - 1);
463 0 : string next_batch;
464 :
465 : // We always attempt to read one extra entry (GetMaxRouteCount() adds 1).
466 : // Case 1: (total_count > GetMaxRouteCount())
467 : // This cannot happen since the merge sort has already trimmed the list to
468 : // GetMaxRouteCount
469 :
470 : // Case 2: (total_count < GetMaxRouteCount())
471 : // There are no more entries matching the input criteria and we are done.
472 0 : if (total_count < ShowRouteHandler::GetMaxRouteCount(req)) {
473 0 : return next_batch;
474 : }
475 :
476 : // Case 3: (total_count == GetMaxRouteCount())
477 : // If total_count is equal to GetMaxRouteCount(), we have at least 1 entry
478 : // for the next round i.e. we are not done and we need to init next_batch
479 : // with the right values from the extra entry and we also need to pop off
480 : // the extra entry.
481 :
482 : int new_count;
483 0 : bool next_round = true;
484 0 : if (req->get_count()) {
485 : // This is required for the last round if the user gave a count. Even
486 : // though we have read an extra entry, we are done and we do not want
487 : // to display any 'next_batch' http links.
488 0 : new_count = req->get_count() - total_count + 1;
489 0 : if (!new_count) {
490 0 : next_round = false;
491 : }
492 : } else {
493 : // If the user did not fill in the count, keep using zero.
494 0 : new_count = req->get_count();
495 : }
496 :
497 0 : if (next_round) {
498 : ShowRoute last_route =
499 0 : last_route_table->get_routes().at(
500 0 : last_route_table->get_routes().size() - 1);
501 : next_batch =
502 0 : req->get_routing_instance() + kIterSeparator +
503 0 : req->get_routing_table() + kIterSeparator +
504 0 : req->get_prefix() + kIterSeparator +
505 0 : last_route_table->get_routing_instance() + kIterSeparator +
506 0 : last_route_table->get_routing_table_name() + kIterSeparator +
507 0 : last_route.get_prefix() + kIterSeparator +
508 0 : integerToString(new_count) + kIterSeparator +
509 0 : req->get_source() + kIterSeparator +
510 0 : req->get_protocol() + kIterSeparator +
511 0 : req->get_family() + kIterSeparator +
512 0 : BoolToString(req->get_longer_match()) + kIterSeparator +
513 0 : BoolToString(req->get_shorter_match());
514 0 : }
515 :
516 : // Pop off the last entry only after we have captured its values in
517 : // 'next_batch' above.
518 : const_cast<vector<ShowRoute> *>(
519 0 : &last_route_table->get_routes())->pop_back();
520 0 : if (last_route_table->get_routes().empty()) {
521 0 : route_table_list->pop_back();
522 : }
523 :
524 0 : return next_batch;
525 0 : }
526 :
527 0 : void ShowRouteHandler::CallbackS2Common(const ShowRouteReq *req,
528 : const RequestPipeline::PipeSpec ps,
529 : ShowRouteResp *resp) {
530 : BgpSandeshContext *bsc =
531 0 : static_cast<BgpSandeshContext *>(req->client_context());
532 0 : const RequestPipeline::StageData *sd = ps.GetStageData(0);
533 0 : vector<ShowRouteTable> route_table_list;
534 0 : vector<const vector<ShowRouteTable> *> table_lists;
535 0 : for (size_t i = 0; i < sd->size(); ++i) {
536 : const ShowRouteData &old_data =
537 0 : static_cast<const ShowRouteData &>(sd->at(i));
538 0 : if (old_data.route_table_list.size()) {
539 0 : table_lists.push_back(&old_data.route_table_list);
540 : }
541 : }
542 0 : MergeSort(&route_table_list, &table_lists,
543 0 : ShowRouteHandler::GetMaxRouteCount(req), bsc, "");
544 :
545 0 : string next_batch = SaveContextAndPopLast(req, &route_table_list);
546 0 : resp->set_next_batch(next_batch);
547 :
548 : // Save the table in the message *after* popping the last entry above.
549 0 : resp->set_tables(route_table_list);
550 0 : }
551 :
552 0 : bool ShowRouteHandler::CallbackS2(const Sandesh *sr,
553 : const RequestPipeline::PipeSpec &ps, int stage, int instNum,
554 : RequestPipeline::InstData *data) {
555 : const ShowRouteReq *req =
556 0 : static_cast<const ShowRouteReq *>(ps.snhRequest_.get());
557 :
558 0 : ShowRouteResp *resp = new ShowRouteResp;
559 0 : CallbackS2Common(req, ps, resp);
560 0 : resp->set_context(req->context());
561 0 : resp->Response();
562 0 : return true;
563 : }
564 :
565 0 : bool ShowRouteHandler::CallbackS2Iterate(const Sandesh *sr,
566 : const RequestPipeline::PipeSpec ps, int stage, int instNum,
567 : RequestPipeline::InstData *data) {
568 : const ShowRouteReqIterate *req_iterate =
569 0 : static_cast<const ShowRouteReqIterate *>(ps.snhRequest_.get());
570 :
571 0 : ShowRouteResp *resp = new ShowRouteResp;
572 0 : ShowRouteReq *req = new ShowRouteReq;
573 0 : bool success = ConvertReqIterateToReq(req_iterate, req);
574 0 : if (success) {
575 0 : CallbackS2Common(req, ps, resp);
576 : }
577 0 : resp->set_context(req->context());
578 0 : resp->Response();
579 0 : req->Release();
580 0 : return true;
581 : }
582 :
583 : // handler for 'show route'
584 0 : void ShowRouteReq::HandleRequest() const {
585 0 : BgpSandeshContext *bsc = static_cast<BgpSandeshContext *>(client_context());
586 :
587 0 : RequestPipeline::PipeSpec ps(this);
588 :
589 : // Request pipeline has 2 stages. In first stage, we spawn one task per
590 : // partition and generate the list of routes. In second stage, we look
591 : // at the generated list and merge it so that we can send it back.
592 0 : RequestPipeline::StageSpec s1, s2;
593 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
594 0 : s1.taskId_ = scheduler->GetTaskId("db::DBTable");
595 0 : s1.allocFn_ = ShowRouteHandler::CreateData;
596 :
597 0 : s1.cbFn_ = ShowRouteHandler::CallbackS1;
598 0 : for (int i = 0; i < bsc->bgp_server->database()->PartitionCount(); ++i) {
599 0 : s1.instances_.push_back(i);
600 : }
601 :
602 0 : s2.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
603 0 : s2.allocFn_ = ShowRouteHandler::CreateData;
604 0 : s2.cbFn_ = ShowRouteHandler::CallbackS2;
605 0 : s2.instances_.push_back(0);
606 :
607 0 : ps.stages_= list_of(s1)(s2)
608 0 : .convert_to_container<vector<RequestPipeline::StageSpec> >();
609 0 : RequestPipeline rp(ps);
610 0 : }
611 :
612 : // handler for 'show route' that shows batches of routes, iteratively
613 0 : void ShowRouteReqIterate::HandleRequest() const {
614 0 : BgpSandeshContext *bsc = static_cast<BgpSandeshContext *>(client_context());
615 :
616 0 : RequestPipeline::PipeSpec ps(this);
617 :
618 : // Request pipeline has 2 stages. In first stage, we spawn one task per
619 : // partition and generate the list of routes. In second stage, we look
620 : // at the generated list and merge it so that we can send it back.
621 0 : RequestPipeline::StageSpec s1, s2;
622 0 : TaskScheduler *scheduler = TaskScheduler::GetInstance();
623 :
624 0 : s1.taskId_ = scheduler->GetTaskId("db::DBTable");
625 0 : s1.allocFn_ = ShowRouteHandler::CreateData;
626 0 : s1.cbFn_ = ShowRouteHandler::CallbackS1Iterate;
627 0 : for (int i = 0; i < bsc->bgp_server->database()->PartitionCount(); ++i) {
628 0 : s1.instances_.push_back(i);
629 : }
630 :
631 0 : s2.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
632 0 : s2.allocFn_ = ShowRouteHandler::CreateData;
633 0 : s2.cbFn_ = ShowRouteHandler::CallbackS2Iterate;
634 0 : s2.instances_.push_back(0);
635 :
636 0 : ps.stages_= list_of(s1)(s2)
637 0 : .convert_to_container<vector<RequestPipeline::StageSpec> >();
638 0 : RequestPipeline rp(ps);
639 0 : }
|