OpenSDN source code
route_aggregator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <boost/foreach.hpp>
9 
10 #include <algorithm>
11 #include <string>
12 #include <vector>
13 
14 #include "sandesh/sandesh_types.h"
15 #include "sandesh/sandesh.h"
16 #include "sandesh/sandesh_trace.h"
17 #include "base/lifetime.h"
18 #include "base/map_util.h"
19 #include "base/task_annotations.h"
20 #include "base/task_trigger.h"
21 #include "bgp/bgp_log.h"
22 #include "bgp/bgp_server.h"
26 #include "bgp/routing-instance/route_aggregate_types.h"
27 
28 using std::make_pair;
29 using std::string;
30 
31 class RouteAggregatorState : public DBState {
32 public:
34  }
35 
37  assert(!aggregating_info_);
39  aggregator_ = true;
40  }
41 
43  assert(aggregating_info_);
44  aggregating_info_ = NULL;
45  aggregator_ = false;
46  }
47 
49  assert(!contributing_info_);
51  contributor_ = true;
52  }
53 
55  assert(contributing_info_);
56  contributing_info_ = NULL;
57  contributor_ = false;
58  }
59 
61  return contributing_info_;
62  }
63 
65  return aggregating_info_;
66  }
67 
68  bool contributor() const {
69  return contributor_;
70  }
71 
72  bool aggregator() const {
73  return aggregator_;
74  }
75 
76 private:
82 };
83 
84 template <typename T>
86 public:
87  typedef typename T::TableT TableT;
88  typedef typename T::RouteT RouteT;
89  typedef typename T::PrefixT PrefixT;
90  typedef typename T::AddressT AddressT;
92  // List of more specific routes resulted in Aggregate route PER PARTITION
93  typedef std::set<BgpRoute *> RouteList;
94  typedef std::vector<RouteList> ContributingRouteList;
95 
97  NoChange = 0,
99  };
100 
101  AggregateRoute(RoutingInstance *rtinstance, AggregateRouteMgrT *manager,
103 
104  virtual ~AggregateRoute() {
105  assert(!HasContributingRoutes());
106  }
107 
110  return manager_->GetAddress(addr);
111  }
112 
113  // Compare config and return whether cfg has updated
115 
118  }
119 
121  return routing_instance_;
122  }
123 
124  BgpTable *bgp_table() const {
125  return routing_instance_->GetTable(this->GetFamily());
126  }
127 
129  return aggregate_route_;
130  }
131 
132  IpAddress nexthop() const {
133  return nexthop_;
134  }
135 
136  bool IsMoreSpecific(BgpRoute *route) const {
137  const RouteT *ip_route = static_cast<RouteT *>(route);
138  const PrefixT &ip_prefix = ip_route->GetPrefix();
139  if (ip_prefix.addr() != GetAddress(nexthop()) &&
140  ip_prefix != aggregate_route_prefix_ &&
141  ip_prefix.IsMoreSpecific(aggregate_route_prefix_)) {
142  return true;
143  }
144  return false;
145  }
146 
147  bool IsOriginVnMatch(BgpRoute *route) const;
148  bool IsBestMatch(BgpRoute *route) const;
149 
150  virtual bool Match(BgpServer *server, BgpTable *table,
151  BgpRoute *route, bool deleted);
152 
154  nexthop_ = nexthop;
156  }
157 
158  void AddAggregateRoute();
159  void UpdateAggregateRoute();
160  void RemoveAggregateRoute();
161 
162  void set_aggregate_route(BgpRoute *aggregate);
163 
164  virtual string ToString() const {
165  return (string("AggregateRoute ") +
167  }
168 
170  return &contributors_;
171  }
172 
174  return contributors_;
175  }
176 
177  bool HasContributingRoutes() const {
178  BOOST_FOREACH(RouteList per_part_contributor, contribute_route_list()) {
179  if (!per_part_contributor.empty()) {
180  return true;
181  }
182  }
183  return false;
184  }
185 
186  bool IsContributingRoute(BgpRoute *route) const {
187  uint32_t part_id = route->get_table_partition()->index();
188  return (contributors_[part_id].find(route) !=
189  contributors_[part_id].end());
190  }
191 
193  DBRequest req;
195  RouteT *ip_route = static_cast<RouteT *>(route);
196  const PrefixT &prefix = ip_route->GetPrefix();
197  req.key.reset(new typename TableT::RequestKey(prefix, NULL));
198  bgp_table()->Enqueue(&req);
199  }
200 
202  RouteAggregatorState *state = static_cast<RouteAggregatorState *>
203  (route->GetState(bgp_table(), manager_->listener_id()));
204  if (state == NULL) {
205  state = new RouteAggregatorState();
206  route->SetState(bgp_table(), manager_->listener_id(), state);
207  }
208  return state;
209  }
210 
212  uint32_t part_id = route->get_table_partition()->index();
213  contributors_[part_id].insert(route);
214  RouteAggregatorState *state = LocateRouteState(route);
217  return (contributors_[part_id].size() == 1);
218  }
219 
221  if (!state->aggregator() && !state->contributor()) {
223  delete state;
224  }
225  }
226 
228  uint32_t part_id = route->get_table_partition()->index();
229  int num_deleted = contributors_[part_id].erase(route);
230  RouteAggregatorState *state = static_cast<RouteAggregatorState *>
231  (route->GetState(bgp_table(), manager_->listener_id()));
232  if (state) {
233  state->reset_contributing_info();
234  ClearRouteState(route, state);
236  } else {
237  assert(num_deleted != 1);
238  }
239  return contributors_[part_id].empty();
240  }
241 
242  void FillShowInfo(AggregateRouteInfo *info, bool summary) const;
243 
244 private:
251 
253 };
254 
255 template <typename T>
257  AggregateRouteMgrT *manager, const PrefixT &aggregate_route,
258  IpAddress nexthop)
259  : routing_instance_(rtinstance),
260  manager_(manager),
261  aggregate_route_prefix_(aggregate_route),
262  nexthop_(nexthop),
263  aggregate_route_(NULL),
264  contributors_(ContributingRouteList(DB::PartitionCount())) {
265 }
266 
267 // Compare config and return whether cfg has updated
268 template <typename T>
270  const AggregateRouteConfig &cfg) {
271  AddressT address = this->GetAddress(cfg.aggregate);
272  PrefixT prefix(address, cfg.prefix_length);
273  assert(aggregate_route_prefix_ == prefix);
274  if (nexthop_ != cfg.nexthop) {
275  return NexthopChange;
276  }
277  return NoChange;
278 }
279 
280 template <typename T>
282  const BgpPath *path = route->BestPath();
283  const BgpAttr *attr = path->GetAttr();
284  const ExtCommunity *ext_community = attr->ext_community();
285  int vni = 0;
286  if (ext_community) {
287  BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
288  ext_community->communities()) {
289  if (!ExtCommunity::is_origin_vn(comm)) continue;
290  OriginVn origin_vn(comm);
291  vni = origin_vn.vn_index();
292  break;
293  }
294  }
295 
296  if (!vni && path->IsVrfOriginated())
297  vni = routing_instance()->virtual_network_index();
298 
299  if (vni == routing_instance()->GetOriginVnForAggregateRoute(GetFamily()))
300  return true;
301 
302  return false;
303 }
304 
305 //
306 // Calculate all aggregate prefixes to which the route can be contributing.
307 // We need to calculate the longest prefix to which this route belongs.
308 // E.g. routing instance is configured with 1/8, 1.1/16 and 1.1.1/24, 1.1.1.1/32
309 // should match 1.1.1/24. Similarly, 1.1.1/24 should be most specific to 1.1/16
310 // as so on
311 //
312 template <typename T>
314  const RouteT *ip_route = static_cast<RouteT *>(route);
315  const PrefixT &ip_prefix = ip_route->GetPrefix();
317  std::set<PrefixT> prefix_list;
318  for (it = manager_->aggregate_route_map().begin();
319  it != manager_->aggregate_route_map().end(); ++it) {
320  if (!it->second->deleted() && ip_prefix != it->first &&
321  ip_prefix.IsMoreSpecific(it->first)) {
322  prefix_list.insert(it->first);
323  }
324  }
325  // It should match atleast one prefix
326  assert(prefix_list.size());
327  //
328  // Longest prefix matches the aggregate prefix of current AggregateRoute
329  // return true to make this route as contributing route
330  // Longest prefix is the last prefix in the set
331  //
332  if (*(prefix_list.rbegin()) == aggregate_route_prefix_) return true;
333  return false;
334 }
335 
336 // Match function called from BgpConditionListener
337 // Concurrency : db::DBTable
338 template <typename T>
340  BgpRoute *route, bool deleted) {
341  CHECK_CONCURRENCY("db::DBTable");
342 
343  //
344  // Only interested routes
345  // Should satisfy following conditions
346  // 1. Origin VN should match origin VN of aggregated route
347  // 2. Route should be more specific
348  //
349  if ((!deleted && !IsOriginVnMatch(route)) || !IsMoreSpecific(route))
350  return false;
351 
352  if (!deleted) {
353  //
354  // If the route is already contributing, check whether it is still
355  // most specific aggregate prefix. Else remove the route as contributing
356  // route. As part of the notification, route will become contributing to
357  // most specific aggregate route prefix.
358  //
359  if (IsContributingRoute(route)) {
360  if (!IsBestMatch(route)) deleted = true;
361  } else if (table->IsContributingRoute(route)) {
362  //
363  // If the route is already contributing route of other aggregate
364  // prefix of this bgp-table, ignore it
365  //
366  return false;
367  }
368  }
369 
370  //
371  // Consider route only if it matches most specific aggregate prefix
372  // configured on the routing instance. e.g. if routing instance has
373  // following prefixes configured, 1/8, 1.1/16 and 1.1.1/24,
374  // 1.1.1.1/32 should match to 1.1.1/24 as most specific route.
375  //
376  if (!deleted && !IsBestMatch(route)) return false;
377 
378  BgpConditionListener *listener = server->condition_listener(GetFamily());
379  bool state_added = listener->CheckMatchState(table, route, this);
380  bool trigger_eval = false;
381  if (!deleted) {
382  if (!state_added) {
383  listener->SetMatchState(table, route, this);
384  trigger_eval = AddContributingRoute(route);
385  }
386  } else {
387  if (!state_added) {
388  // Not seen ADD ignore DELETE
389  return false;
390  }
391  trigger_eval = RemoveContributingRoute(route);
392  listener->RemoveMatchState(table, route, this);
393  }
394 
395  if (trigger_eval) manager_->EvaluateAggregateRoute(this);
396  return true;
397 }
398 
399 // AddAggregateRoute
400 template <typename T>
402  CHECK_CONCURRENCY("bgp::RouteAggregation");
403 
404  RouteT rt_key(aggregate_route_prefix());
405  DBTablePartition *partition =
406  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
407  BgpRoute *aggregate_route =
408  static_cast<BgpRoute *>(partition->Find(&rt_key));
409 
410  if (aggregate_route == NULL) {
411  aggregate_route = new RouteT(aggregate_route_prefix());
412  partition->Add(aggregate_route);
413  } else {
414  aggregate_route->ClearDelete();
415  }
416 
417  BgpPath *existing_path = aggregate_route->FindPath(BgpPath::Aggregate, 0);
418  assert(existing_path == NULL);
419 
420  BgpAttrSpec attrs;
421  BgpAttrNextHop attr_nexthop(this->GetAddress(nexthop()));
422  attrs.push_back(&attr_nexthop);
423  ExtCommunitySpec extcomm_spec;
424  OriginVn origin_vn(routing_instance()->server()->autonomous_system(),
425  routing_instance()->GetOriginVnForAggregateRoute(GetFamily()));
426  extcomm_spec.communities.push_back(origin_vn.GetExtCommunityValue());
427  attrs.push_back(&extcomm_spec);
428  BgpAttrPtr attr = routing_instance()->server()->attr_db()->Locate(attrs);
429  BgpPath *new_path = new BgpPath(BgpPath::Aggregate,
430  attr.get(), BgpPath::ResolveNexthop, 0);
431  bgp_table()->path_resolver()->StartPathResolution(aggregate_route,
432  new_path);
433  aggregate_route->InsertPath(new_path);
434  partition->Notify(aggregate_route);
435  set_aggregate_route(aggregate_route);
436 
437  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
438  "Added aggregate path " << aggregate_route_->ToString() <<
439  " in table " << partition->table()->name());
440 }
441 
442 // UpdateAggregateRoute
443 template <typename T>
445  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
446 
447  if (aggregate_route_ == NULL) return;
448 
449  DBTablePartition *partition = static_cast<DBTablePartition *>
450  (bgp_table()->GetTablePartition(aggregate_route_));
451 
452  aggregate_route_->ClearDelete();
453 
454  BgpPath *existing_path = aggregate_route_->FindPath(BgpPath::Aggregate, 0);
455  if (existing_path)
456  bgp_table()->path_resolver()->StopPathResolution(partition->index(),
457  existing_path);
458  aggregate_route_->RemovePath(BgpPath::Aggregate);
459 
460  BgpAttrSpec attrs;
461  BgpAttrNextHop attr_nexthop(this->GetAddress(nexthop()));
462  attrs.push_back(&attr_nexthop);
463  ExtCommunitySpec extcomm_spec;
464  OriginVn origin_vn(routing_instance()->server()->autonomous_system(),
465  routing_instance()->GetOriginVnForAggregateRoute(GetFamily()));
466  extcomm_spec.communities.push_back(origin_vn.GetExtCommunityValue());
467  attrs.push_back(&extcomm_spec);
468  BgpAttrPtr attr = routing_instance()->server()->attr_db()->Locate(attrs);
469  BgpPath *new_path = new BgpPath(BgpPath::Aggregate,
470  attr.get(), BgpPath::ResolveNexthop, 0);
471  bgp_table()->path_resolver()->StartPathResolution(aggregate_route_,
472  new_path);
473  aggregate_route_->InsertPath(new_path);
474 
475  partition->Notify(aggregate_route_);
476 
477  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
478  "Updated aggregate path " << aggregate_route_->ToString() <<
479  " in table " << partition->table()->name());
480 }
481 
482 // RemoveAggregateRoute
483 template <typename T>
485  CHECK_CONCURRENCY("bgp::RouteAggregation");
486  BgpRoute *aggregate_route = aggregate_route_;
487  if (!aggregate_route) return;
488 
489  DBTablePartition *partition = static_cast<DBTablePartition *>
490  (bgp_table()->GetTablePartition(aggregate_route_));
491 
492  BgpPath *existing_path =
493  aggregate_route->FindPath(BgpPath::Aggregate, 0);
494  assert(existing_path != NULL);
495 
496  bgp_table()->path_resolver()->StopPathResolution(partition->index(),
497  existing_path);
498  aggregate_route->RemovePath(BgpPath::Aggregate);
499 
500  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
501  "Removed aggregate path " << aggregate_route_->ToString() <<
502  " in table " << partition->table()->name());
503 
504  if (!aggregate_route->HasPaths()) {
505  partition->Delete(aggregate_route);
506  } else {
507  partition->Notify(aggregate_route);
508  }
509  set_aggregate_route(NULL);
510 }
511 
512 template <typename T>
514  if (aggregate) {
515  assert(aggregate_route_ == NULL);
516  RouteAggregatorState *state = LocateRouteState(aggregate);
518  } else {
519  assert(aggregate_route_ != NULL);
520  RouteAggregatorState *state = static_cast<RouteAggregatorState *>
521  (aggregate_route_->GetState(bgp_table(), manager_->listener_id()));
522  assert(state);
523  state->reset_aggregating_info();
524  ClearRouteState(aggregate_route_, state);
525  }
526  aggregate_route_ = aggregate;
527 }
528 
529 template <typename T>
530 void AggregateRoute<T>::FillShowInfo(AggregateRouteInfo *info,
531  bool summary) const {
532  BgpTable *table = bgp_table();
533  info->set_deleted(deleted());
534  info->set_prefix(aggregate_route_prefix_.ToString());
535  if (aggregate_route_) {
536  ShowRouteBrief show_route;
537  aggregate_route_->FillRouteInfo(table, &show_route);
538  info->set_aggregate_rt(show_route);
539  }
540 
541  info->set_nexthop(nexthop_.to_string());
542 
543  if (summary)
544  return;
545 
546  std::vector<string> contributor_list;
547  BOOST_FOREACH(const RouteList &list, contribute_route_list()) {
548  BOOST_FOREACH(BgpRoute *rt, list) {
549  contributor_list.push_back(rt->ToString());
550  }
551  }
552  info->set_contributors(contributor_list);
553 }
554 
555 template <typename T>
557 public:
558  explicit DeleteActor(RouteAggregator *aggregator) :
559  LifetimeActor(aggregator->routing_instance()->server()->lifetime_manager()),
560  aggregator_(aggregator) {
561  }
562  virtual ~DeleteActor() {
563  }
564 
565  virtual bool MayDelete() const {
566  return aggregator_->MayDelete();
567  }
568 
569  virtual void Destroy() {
570  aggregator_->routing_instance()->DestroyRouteAggregator(
571  aggregator_->GetFamily());
572  }
573 
574 private:
576 };
577 
578 template <typename T>
580  : rtinstance_(rtinstance),
581  condition_listener_(rtinstance_->server()->condition_listener(GetFamily())),
582  listener_id_(DBTableBase::kInvalidId),
583  update_list_trigger_(new TaskTrigger(
584  boost::bind(&RouteAggregator::ProcessUpdateList, this),
585  TaskScheduler::GetInstance()->GetTaskId("bgp::RouteAggregation"),
586  0)),
587  unregister_list_trigger_(new TaskTrigger(
588  boost::bind(&RouteAggregator::ProcessUnregisterList, this),
589  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)),
590  deleter_(new DeleteActor(this)),
591  instance_delete_ref_(this, rtinstance->deleter()) {
592 }
593 
594 template <typename T>
596  if (listener_id_ != DBTableBase::kInvalidId)
597  bgp_table()->Unregister(listener_id_);
598  listener_id_ = DBTableBase::kInvalidId;
599 }
600 
601 template <typename T>
603  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
604  const AggregateRouteConfigList &list =
605  routing_instance()->config()->aggregate_routes(GetFamily());
606  typedef AggregateRouteConfigList::const_iterator iterator_t;
607  for (iterator_t iter = list.begin(); iter != list.end(); ++iter) {
608  LocateAggregateRoutePrefix(*iter);
609  }
610 }
611 
613  const AggregateRouteConfig &rhs) {
616  return false;
617 }
618 
619 template <typename T>
621  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
622  AggregateRouteConfigList config_list =
623  routing_instance()->config()->aggregate_routes(GetFamily());
624  sort(config_list.begin(), config_list.end(), CompareAggregateRouteConfig);
625 
626  map_difference(&aggregate_route_map_,
627  config_list.begin(), config_list.end(),
628  boost::bind(&RouteAggregator<T>::CompareAggregateRoute, this, _1, _2),
629  boost::bind(&RouteAggregator<T>::AddAggregateRoute, this, _1),
630  boost::bind(&RouteAggregator<T>::DelAggregateRoute, this, _1),
631  boost::bind(&RouteAggregator<T>::UpdateAggregateRoute, this, _1, _2));
632 }
633 
634 template <typename T>
636  CHECK_CONCURRENCY("bgp::Config");
637  for (typename AggregateRouteMap::iterator it = aggregate_route_map_.begin();
638  it != aggregate_route_map_.end(); it++) {
639  RemoveAggregateRoutePrefix(it->first);
640  }
641 }
642 
643 template <>
645  return Address::INET;
646 }
647 
648 template <>
650  return Address::INET6;
651 }
652 
653 template <>
655  const {
656  assert(addr.is_v4());
657  return addr.to_v4();
658 }
659 
660 template <>
662  const {
663  assert(addr.is_v6());
664  return addr.to_v6();
665 }
666 
667 template <typename T>
669  return rtinstance_->GetTable(GetFamily());
670 }
671 
672 template <typename T>
674  // Register to the table before adding first match condition
675  listener_id_ = bgp_table()->Register(
676  boost::bind(&RouteAggregator::RouteListener, this, _1, _2),
677  "RouteAggregator");
678 }
679 
680 template <typename T>
682  if (!aggregate_route_map_.empty())
683  return false;
684  if (!update_aggregate_list_.empty())
685  return false;
686  if (!unregister_aggregate_list_.empty())
687  return false;
688  return true;
689 }
690 
691 // Cascade delete from RoutingInstance delete_ref to self.
692 template <typename T>
694  deleter_->Delete();
695 }
696 
697 // Attempt to enqueue a delete for the RouteAggregator.
698 template <typename T>
700  if (!deleter_->IsDeleted())
701  return;
702  deleter_->RetryDelete();
703 }
704 
705 template <typename T>
707  std::scoped_lock lock(mutex_);
708  update_aggregate_list_.insert(entry);
709  update_list_trigger_->Set();
710 }
711 
712 template <typename T>
714  AggregateRoutePtr entry) {
715  std::scoped_lock lock(mutex_);
716  unregister_aggregate_list_.insert(entry);
717  unregister_list_trigger_->Set();
718 }
719 
720 template <typename T>
722  RouteAggregatorState *state = static_cast<RouteAggregatorState *>
723  (route->GetState(bgp_table(), listener_id()));
724  if (state) {
725  return (state->aggregator());
726  }
727  return false;
728 }
729 
730 template <typename T>
732  RouteAggregatorState *state = static_cast<RouteAggregatorState *>
733  (route->GetState(bgp_table(), listener_id()));
734  if (state) {
735  return state->contributor();
736  }
737  return false;
738 }
739 
740 template <typename T>
741 bool RouteAggregator<T>::FillAggregateRouteInfo(AggregateRouteEntriesInfo *info,
742  bool summary) const {
743  if (aggregate_route_map().empty())
744  return false;
745 
746  info->set_name(rtinstance_->GetVirtualNetworkName());
747  info->set_instance_name(rtinstance_->name());
748  vector<AggregateRouteInfo> aggregate_route_list =
749  vector<AggregateRouteInfo>();
750  for (typename AggregateRouteMap::const_iterator it =
751  aggregate_route_map_.begin(); it != aggregate_route_map_.end(); it++) {
752  AggregateRouteT *aggregate =
753  static_cast<AggregateRouteT *>(it->second.get());
754  AggregateRouteInfo aggregate_info;
755  aggregate->FillShowInfo(&aggregate_info, summary);
756  aggregate_route_list.push_back(aggregate_info);
757  }
758  info->set_aggregate_route_list(aggregate_route_list);
759  return true;
760 }
761 
762 template <typename T>
764  typename AggregateRouteMap::iterator loc,
765  AggregateRouteConfigList::iterator it) {
766  AddressT address = this->GetAddress(it->aggregate);
767  PrefixT prefix(address, it->prefix_length);
768  KEY_COMPARE(loc->first, prefix);
769  return 0;
770 }
771 
772 template <typename T>
774  AggregateRouteConfigList::iterator it) {
775  LocateAggregateRoutePrefix(*it);
776 }
777 
778 template <typename T>
780  typename AggregateRouteMap::iterator loc) {
781  RemoveAggregateRoutePrefix(loc->first);
782 }
783 
784 template <typename T>
786  typename AggregateRouteMap::iterator loc,
787  AggregateRouteConfigList::iterator it) {
788  LocateAggregateRoutePrefix(*it);
789 }
790 
791 template <typename T>
793  &cfg) {
794  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
795  AddressT address = this->GetAddress(cfg.aggregate);
796  PrefixT prefix(address, cfg.prefix_length);
797 
798  // Verify whether the entry already exists
799  typename AggregateRouteMap::iterator it = aggregate_route_map_.find(prefix);
800  if (it != aggregate_route_map_.end()) {
801  // Wait for the delete complete cb
802  if (it->second->deleted()) return;
803 
804  AggregateRouteT *match =
805  static_cast<AggregateRouteT *>(it->second.get());
806  // Check whether the config has got updated
807  typename AggregateRouteT::CompareResult change =
808  match->CompareConfig(cfg);
809  // No change..
810  if (change == AggregateRouteT::NoChange) return;
811 
812  if (change == AggregateRouteT::NexthopChange)
813  match->UpdateNexthop(cfg.nexthop);
814  return;
815  }
816 
817  AggregateRouteT *match =
818  new AggregateRouteT(routing_instance(), this, prefix, cfg.nexthop);
819  AggregateRoutePtr aggregate_route_match = AggregateRoutePtr(match);
820  aggregate_route_map_.insert(make_pair(prefix, aggregate_route_match));
821 
822  condition_listener_->AddMatchCondition(match->bgp_table(),
823  aggregate_route_match.get(), BgpConditionListener::RequestDoneCb());
824  return;
825 }
826 
827 template <typename T>
829  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
830  typename AggregateRouteMap::iterator it =
831  aggregate_route_map_.find(aggregate);
832  if (it == aggregate_route_map_.end()) return;
833  if (it->second->deleted()) return;
834 
836  boost::bind(&RouteAggregator::StopAggregateRouteDone, this, _1, _2);
837 
838  AggregateRouteT *match = static_cast<AggregateRouteT *>(it->second.get());
839  condition_listener_->RemoveMatchCondition(match->bgp_table(),
840  match, callback);
841 }
842 
843 template <typename T>
845  ConditionMatch *info) {
846  CHECK_CONCURRENCY("db::Walker");
847  UnregisterAndResolveRouteAggregate(info);
848  return;
849 }
850 
851 template <typename T>
853  CHECK_CONCURRENCY("bgp::Config");
854 
855  for (AggregateRouteProcessList::iterator
856  it = unregister_aggregate_list_.begin();
857  it != unregister_aggregate_list_.end(); ++it) {
858  AggregateRouteT *aggregate = static_cast<AggregateRouteT *>(it->get());
859  aggregate_route_map_.erase(aggregate->aggregate_route_prefix());
860  condition_listener_->UnregisterMatchCondition(aggregate->bgp_table(),
861  aggregate);
862  }
863 
864  unregister_aggregate_list_.clear();
865 
866  if (!routing_instance()->deleted() && routing_instance()->config())
867  ProcessAggregateRouteConfig();
868 
869  if (MayDelete()) RetryDelete();
870  return true;
871 }
872 
873 template <typename T>
875  CHECK_CONCURRENCY("bgp::RouteAggregation");
876 
877  for (AggregateRouteProcessList::iterator
878  it = update_aggregate_list_.begin();
879  it != update_aggregate_list_.end(); ++it) {
880  AggregateRouteT *aggregate = static_cast<AggregateRouteT *>(it->get());
881  if (aggregate->aggregate_route()) {
882  if (!aggregate->HasContributingRoutes())
883  aggregate->RemoveAggregateRoute();
884  } else {
885  if (aggregate->HasContributingRoutes())
886  aggregate->AddAggregateRoute();
887  }
888  }
889 
890  update_aggregate_list_.clear();
891 
892  if (MayDelete()) RetryDelete();
893  return true;
894 }
895 
896 // Need this to store the aggregate info in aggregated route as DBState
897 template <typename T>
899  DBEntryBase *entry) {
900  return true;
901 }
902 
903 // Enable/Disable task triggers
904 template <typename T>
906  update_list_trigger_->set_disable();
907 }
908 
909 template <typename T>
911  update_list_trigger_->set_enable();
912 }
913 
914 template <typename T>
916  return update_aggregate_list_.size();
917 }
918 
919 template <typename T>
921  unregister_list_trigger_->set_disable();
922 }
923 
924 template <typename T>
926  unregister_list_trigger_->set_enable();
927 }
928 
929 template <typename T>
931  return unregister_aggregate_list_.size();
932 }
933 
934 // Explicit instantiation of RouteAggregator for INET and INET6.
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:16
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
std::vector< BgpAttribute * > BgpAttrSpec
Definition: bgp_attr.h:823
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
#define BGP_LOG_FLAG_TRACE
Definition: bgp_log.h:44
#define BGP_LOG_STR(obj, level, flags, arg)
Definition: bgp_log.h:90
Family
Definition: address.h:25
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
T::PrefixT PrefixT
PrefixT aggregate_route_prefix_
void UpdateNexthop(IpAddress nexthop)
void set_aggregate_route(BgpRoute *aggregate)
void NotifyContributingRoute(BgpRoute *route)
void ClearRouteState(BgpRoute *route, RouteAggregatorState *state)
bool HasContributingRoutes() const
CompareResult CompareConfig(const AggregateRouteConfig &cfg)
bool AddContributingRoute(BgpRoute *route)
IpAddress nexthop() const
bool IsOriginVnMatch(BgpRoute *route) const
AddressT GetAddress(IpAddress addr) const
DISALLOW_COPY_AND_ASSIGN(AggregateRoute)
std::set< BgpRoute * > RouteList
AggregateRoute(RoutingInstance *rtinstance, AggregateRouteMgrT *manager, const PrefixT &aggregate_route, IpAddress nexthop)
BgpTable * bgp_table() const
void FillShowInfo(AggregateRouteInfo *info, bool summary) const
virtual string ToString() const
bool IsMoreSpecific(BgpRoute *route) const
ContributingRouteList * contribute_route_list()
RouteAggregator< T > AggregateRouteMgrT
T::AddressT AddressT
bool IsBestMatch(BgpRoute *route) const
Address::Family GetFamily() const
BgpRoute * aggregate_route() const
std::vector< RouteList > ContributingRouteList
const ContributingRouteList & contribute_route_list() const
RouteAggregatorState * LocateRouteState(BgpRoute *route)
ContributingRouteList contributors_
RoutingInstance * routing_instance() const
bool IsContributingRoute(BgpRoute *route) const
const PrefixT & aggregate_route_prefix() const
virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route, bool deleted)
RoutingInstance * routing_instance_
virtual ~AggregateRoute()
AggregateRouteMgrT * manager_
BgpRoute * aggregate_route_
bool RemoveContributingRoute(BgpRoute *route)
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:918
bool CheckMatchState(BgpTable *table, BgpRoute *route, ConditionMatch *obj)
boost::function< void(BgpTable *, ConditionMatch *)> RequestDoneCb
void RemoveMatchState(BgpTable *table, BgpRoute *route, ConditionMatch *obj)
void SetMatchState(BgpTable *table, BgpRoute *route, ConditionMatch *obj, ConditionMatchState *state=NULL)
bool IsVrfOriginated() const
Definition: bgp_path.h:69
@ Aggregate
Definition: bgp_path.h:43
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
@ ResolveNexthop
Definition: bgp_path.h:28
const BgpPath * FindPath(BgpPath::PathSource src) const
Definition: bgp_route.cc:147
bool RemovePath(BgpPath::PathSource src, const IPeer *peer=NULL, uint32_t path_id=0)
Definition: bgp_route.cc:264
const BgpPath * BestPath() const
Definition: bgp_route.cc:48
void InsertPath(BgpPath *path)
Definition: bgp_route.cc:62
bool HasPaths() const
Definition: bgp_route.h:29
BgpConditionListener * condition_listener(Address::Family family)
Definition: bgp_server.h:119
bool IsContributingRoute(const BgpRoute *route) const
Definition: bgp_table.cc:1152
virtual std::string ToString() const =0
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:74
void ClearDelete()
Definition: db_entry.h:48
DBTablePartBase * get_table_partition() const
Definition: db_entry.cc:116
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:23
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
static const int kInvalidId
Definition: db_table.h:65
const std::string & name() const
Definition: db_table.h:111
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
Definition: db.h:25
std::vector< uint64_t > communities
Definition: community.h:146
boost::array< uint8_t, 8 > ExtCommunityValue
Definition: community.h:155
const ExtCommunityList & communities() const
Definition: community.h:183
static bool is_origin_vn(const ExtCommunityValue &val)
Definition: community.h:196
int vn_index() const
Definition: origin_vn.cc:123
const uint64_t GetExtCommunityValue() const
Definition: origin_vn.h:38
DISALLOW_COPY_AND_ASSIGN(RouteAggregatorState)
AggregateRoutePtr aggregating_info_
AggregateRoutePtr contributing_info_
AggregateRoutePtr contributing_info()
void set_contributing_info(AggregateRoutePtr aggregator)
void set_aggregating_info(AggregateRoutePtr aggregator)
AggregateRoutePtr aggregating_info()
DeleteActor(RouteAggregator *aggregator)
virtual bool MayDelete() const
virtual bool IsContributingRoute(const BgpRoute *route) const
void RemoveAggregateRoutePrefix(const PrefixT &static_route)
BgpInstanceConfig::AggregateRouteList AggregateRouteConfigList
virtual size_t GetUpdateAggregateListSize() const
T::AddressT AddressT
virtual bool FillAggregateRouteInfo(AggregateRouteEntriesInfo *info, bool summary) const
void UpdateAggregateRoute(typename AggregateRouteMap::iterator loc, AggregateRouteConfigList::iterator it)
virtual void Initialize()
virtual void EnableRouteAggregateUpdate()
void EvaluateAggregateRoute(AggregateRoutePtr entry)
virtual void DisableUnregResolveTask()
RoutingInstance * routing_instance()
Address::Family GetFamily() const
void UnregisterAndResolveRouteAggregate(AggregateRoutePtr entry)
virtual bool IsAggregateRoute(const BgpRoute *route) const
int CompareAggregateRoute(typename AggregateRouteMap::iterator loc, AggregateRouteConfigList::iterator it)
RouteAggregator(RoutingInstance *instance)
virtual void FlushAggregateRouteConfig()
virtual void DisableRouteAggregateUpdate()
void DelAggregateRoute(typename AggregateRouteMap::iterator loc)
virtual void UpdateAggregateRouteConfig()
void StopAggregateRouteDone(BgpTable *table, ConditionMatch *info)
virtual size_t GetUnregResolveListSize() const
DBTableBase::ListenerId listener_id() const
bool RouteListener(DBTablePartBase *root, DBEntryBase *entry)
virtual void EnableUnregResolveTask()
AddressT GetAddress(IpAddress addr) const
bool MayDelete() const
BgpTable * bgp_table() const
const AggregateRouteMap & aggregate_route_map() const
virtual void ProcessAggregateRouteConfig()
void AddAggregateRoute(AggregateRouteConfigList::iterator it)
void LocateAggregateRoutePrefix(const AggregateRouteConfig &cfg)
BgpTable * GetTable(Address::Family fmly)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void map_difference(ForwardIterator __first1, ForwardIterator __last1, ForwardIterator __first2, ForwardIterator __last2, AddFunctor __add_fn, DelFunctor __del_fn, EqFunctor __eq_fn)
Definition: map_util.h:21
bool CompareAggregateRouteConfig(const AggregateRouteConfig &lhs, const AggregateRouteConfig &rhs)
ConditionMatchPtr AggregateRoutePtr
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_NOTIFY
Definition: db_table.h:41
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
#define CHECK_CONCURRENCY(...)
#define KEY_COMPARE(x, y)
Definition: util.h:71
#define BOOL_KEY_COMPARE(x, y)
Definition: util.h:65