8 #include <boost/foreach.hpp>
14 #include "sandesh/sandesh_types.h"
15 #include "sandesh/sandesh.h"
16 #include "sandesh/sandesh_trace.h"
26 #include "bgp/routing-instance/route_aggregate_types.h"
138 const PrefixT &ip_prefix = ip_route->GetPrefix();
165 return (
string(
"AggregateRoute ") +
179 if (!per_part_contributor.empty()) {
196 const PrefixT &prefix = ip_route->GetPrefix();
197 req.
key.reset(
new typename TableT::RequestKey(prefix, NULL));
237 assert(num_deleted != 1);
242 void FillShowInfo(AggregateRouteInfo *info,
bool summary)
const;
255 template <
typename T>
259 : routing_instance_(rtinstance),
261 aggregate_route_prefix_(aggregate_route),
263 aggregate_route_(NULL),
268 template <
typename T>
273 assert(aggregate_route_prefix_ == prefix);
275 return NexthopChange;
280 template <
typename T>
297 vni = routing_instance()->virtual_network_index();
299 if (vni == routing_instance()->GetOriginVnForAggregateRoute(GetFamily()))
312 template <
typename T>
315 const PrefixT &ip_prefix = ip_route->GetPrefix();
317 std::set<PrefixT> prefix_list;
320 if (!it->second->deleted() && ip_prefix != it->first &&
321 ip_prefix.IsMoreSpecific(it->first)) {
322 prefix_list.insert(it->first);
326 assert(prefix_list.size());
332 if (*(prefix_list.rbegin()) == aggregate_route_prefix_)
return true;
338 template <
typename T>
349 if ((!deleted && !IsOriginVnMatch(route)) || !IsMoreSpecific(route))
359 if (IsContributingRoute(route)) {
360 if (!IsBestMatch(route)) deleted =
true;
376 if (!deleted && !IsBestMatch(route))
return false;
380 bool trigger_eval =
false;
384 trigger_eval = AddContributingRoute(route);
391 trigger_eval = RemoveContributingRoute(route);
395 if (trigger_eval) manager_->EvaluateAggregateRoute(
this);
400 template <
typename T>
404 RouteT rt_key(aggregate_route_prefix());
410 if (aggregate_route == NULL) {
411 aggregate_route =
new RouteT(aggregate_route_prefix());
412 partition->
Add(aggregate_route);
418 assert(existing_path == NULL);
422 attrs.push_back(&attr_nexthop);
424 OriginVn origin_vn(routing_instance()->server()->autonomous_system(),
425 routing_instance()->GetOriginVnForAggregateRoute(GetFamily()));
427 attrs.push_back(&extcomm_spec);
428 BgpAttrPtr attr = routing_instance()->server()->attr_db()->Locate(attrs);
431 bgp_table()->path_resolver()->StartPathResolution(aggregate_route,
434 partition->
Notify(aggregate_route);
435 set_aggregate_route(aggregate_route);
438 "Added aggregate path " << aggregate_route_->ToString() <<
439 " in table " << partition->
table()->
name());
443 template <
typename T>
447 if (aggregate_route_ == NULL)
return;
450 (bgp_table()->GetTablePartition(aggregate_route_));
452 aggregate_route_->ClearDelete();
456 bgp_table()->path_resolver()->StopPathResolution(partition->
index(),
462 attrs.push_back(&attr_nexthop);
464 OriginVn origin_vn(routing_instance()->server()->autonomous_system(),
465 routing_instance()->GetOriginVnForAggregateRoute(GetFamily()));
467 attrs.push_back(&extcomm_spec);
468 BgpAttrPtr attr = routing_instance()->server()->attr_db()->Locate(attrs);
471 bgp_table()->path_resolver()->StartPathResolution(aggregate_route_,
473 aggregate_route_->InsertPath(new_path);
475 partition->
Notify(aggregate_route_);
478 "Updated aggregate path " << aggregate_route_->ToString() <<
479 " in table " << partition->
table()->
name());
483 template <
typename T>
486 BgpRoute *aggregate_route = aggregate_route_;
487 if (!aggregate_route)
return;
490 (bgp_table()->GetTablePartition(aggregate_route_));
494 assert(existing_path != NULL);
496 bgp_table()->path_resolver()->StopPathResolution(partition->
index(),
501 "Removed aggregate path " << aggregate_route_->ToString() <<
502 " in table " << partition->
table()->
name());
505 partition->
Delete(aggregate_route);
507 partition->
Notify(aggregate_route);
509 set_aggregate_route(NULL);
512 template <
typename T>
515 assert(aggregate_route_ == NULL);
519 assert(aggregate_route_ != NULL);
521 (aggregate_route_->GetState(bgp_table(), manager_->listener_id()));
524 ClearRouteState(aggregate_route_, state);
526 aggregate_route_ = aggregate;
529 template <
typename T>
531 bool summary)
const {
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);
541 info->set_nexthop(nexthop_.to_string());
546 std::vector<string> contributor_list;
547 BOOST_FOREACH(
const RouteList &list, contribute_route_list()) {
549 contributor_list.push_back(rt->
ToString());
552 info->set_contributors(contributor_list);
555 template <
typename T>
560 aggregator_(aggregator) {
566 return aggregator_->MayDelete();
570 aggregator_->routing_instance()->DestroyRouteAggregator(
571 aggregator_->GetFamily());
578 template <
typename T>
580 : rtinstance_(rtinstance),
581 condition_listener_(rtinstance_->server()->condition_listener(GetFamily())),
585 TaskScheduler::GetInstance()->GetTaskId(
"bgp::RouteAggregation"),
591 instance_delete_ref_(this, rtinstance->
deleter()) {
594 template <
typename T>
597 bgp_table()->Unregister(listener_id_);
601 template <
typename T>
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);
619 template <
typename T>
623 routing_instance()->config()->aggregate_routes(GetFamily());
627 config_list.begin(), config_list.end(),
634 template <
typename T>
637 for (
typename AggregateRouteMap::iterator it = aggregate_route_map_.begin();
638 it != aggregate_route_map_.end(); it++) {
639 RemoveAggregateRoutePrefix(it->first);
656 assert(addr.is_v4());
663 assert(addr.is_v6());
667 template <
typename T>
669 return rtinstance_->GetTable(GetFamily());
672 template <
typename T>
675 listener_id_ = bgp_table()->Register(
680 template <
typename T>
682 if (!aggregate_route_map_.empty())
684 if (!update_aggregate_list_.empty())
686 if (!unregister_aggregate_list_.empty())
692 template <
typename T>
698 template <
typename T>
700 if (!deleter_->IsDeleted())
702 deleter_->RetryDelete();
705 template <
typename T>
707 std::scoped_lock lock(mutex_);
708 update_aggregate_list_.insert(entry);
709 update_list_trigger_->Set();
712 template <
typename T>
715 std::scoped_lock lock(mutex_);
716 unregister_aggregate_list_.insert(entry);
717 unregister_list_trigger_->Set();
720 template <
typename T>
723 (route->
GetState(bgp_table(), listener_id()));
730 template <
typename T>
733 (route->
GetState(bgp_table(), listener_id()));
740 template <
typename T>
742 bool summary)
const {
743 if (aggregate_route_map().empty())
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++) {
754 AggregateRouteInfo aggregate_info;
756 aggregate_route_list.push_back(aggregate_info);
758 info->set_aggregate_route_list(aggregate_route_list);
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);
772 template <
typename T>
774 AggregateRouteConfigList::iterator it) {
775 LocateAggregateRoutePrefix(*it);
778 template <
typename T>
780 typename AggregateRouteMap::iterator loc) {
781 RemoveAggregateRoutePrefix(loc->first);
784 template <
typename T>
786 typename AggregateRouteMap::iterator loc,
787 AggregateRouteConfigList::iterator it) {
788 LocateAggregateRoutePrefix(*it);
791 template <
typename T>
799 typename AggregateRouteMap::iterator it = aggregate_route_map_.find(prefix);
800 if (it != aggregate_route_map_.end()) {
802 if (it->second->deleted())
return;
810 if (change == AggregateRouteT::NoChange)
return;
812 if (change == AggregateRouteT::NexthopChange)
820 aggregate_route_map_.insert(make_pair(prefix, aggregate_route_match));
822 condition_listener_->AddMatchCondition(match->
bgp_table(),
827 template <
typename T>
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;
839 condition_listener_->RemoveMatchCondition(match->
bgp_table(),
843 template <
typename T>
847 UnregisterAndResolveRouteAggregate(info);
851 template <
typename T>
855 for (AggregateRouteProcessList::iterator
856 it = unregister_aggregate_list_.begin();
857 it != unregister_aggregate_list_.end(); ++it) {
860 condition_listener_->UnregisterMatchCondition(aggregate->
bgp_table(),
864 unregister_aggregate_list_.clear();
866 if (!routing_instance()->deleted() && routing_instance()->config())
867 ProcessAggregateRouteConfig();
869 if (MayDelete()) RetryDelete();
873 template <
typename T>
877 for (AggregateRouteProcessList::iterator
878 it = update_aggregate_list_.begin();
879 it != update_aggregate_list_.end(); ++it) {
890 update_aggregate_list_.clear();
892 if (MayDelete()) RetryDelete();
897 template <
typename T>
904 template <
typename T>
906 update_list_trigger_->set_disable();
909 template <
typename T>
911 update_list_trigger_->set_enable();
914 template <
typename T>
916 return update_aggregate_list_.size();
919 template <
typename T>
921 unregister_list_trigger_->set_disable();
924 template <
typename T>
926 unregister_list_trigger_->set_enable();
929 template <
typename T>
931 return unregister_aggregate_list_.size();
boost::asio::ip::address_v6 Ip6Address
boost::asio::ip::address IpAddress
boost::asio::ip::address_v4 Ip4Address
std::vector< BgpAttribute * > BgpAttrSpec
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
#define BGP_LOG_FLAG_TRACE
#define BGP_LOG_STR(obj, level, flags, arg)
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
void UpdateAggregateRoute()
void RemoveAggregateRoute()
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
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
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
const BgpAttr * GetAttr() const
const BgpPath * FindPath(BgpPath::PathSource src) const
bool RemovePath(BgpPath::PathSource src, const IPeer *peer=NULL, uint32_t path_id=0)
const BgpPath * BestPath() const
void InsertPath(BgpPath *path)
BgpConditionListener * condition_listener(Address::Family family)
bool IsContributingRoute(const BgpRoute *route) const
virtual std::string ToString() const =0
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
void ClearState(DBTableBase *tbl_base, ListenerId listener)
DBTablePartBase * get_table_partition() const
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
bool Enqueue(DBRequest *req)
static const int kInvalidId
const std::string & name() const
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
const uint64_t GetExtCommunityValue() const
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)
void reset_aggregating_info()
void reset_contributing_info()
AggregateRoutePtr aggregating_info()
DeleteActor(RouteAggregator *aggregator)
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
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
bool ProcessUnregisterList()
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
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 ...
void map_difference(ForwardIterator __first1, ForwardIterator __last1, ForwardIterator __first2, ForwardIterator __last2, AddFunctor __add_fn, DelFunctor __del_fn, EqFunctor __eq_fn)
bool CompareAggregateRouteConfig(const AggregateRouteConfig &lhs, const AggregateRouteConfig &rhs)
ConditionMatchPtr AggregateRoutePtr
std::unique_ptr< DBRequestKey > key
#define CHECK_CONCURRENCY(...)
#define KEY_COMPARE(x, y)
#define BOOL_KEY_COMPARE(x, y)