8 #include <boost/assign/list_of.hpp>
9 #include <boost/foreach.hpp>
26 #include "bgp/routing-instance/static_route_types.h"
30 using boost::assign::list_of;
31 using boost::system::error_code;
114 for (Route::PathList::iterator it =
146 return (
string(
"StaticRoute ") +
nexthop_.to_string());
161 return (
nexthop_ == ip_route->GetPrefix().addr());
180 template <
typename T>
184 : routing_instance_(rtinstance),
187 nexthop_(config.nexthop),
188 nexthop_route_(NULL),
189 unregistered_(false) {
197 template <
typename T>
202 if (prefix_ != prefix) {
205 if (nexthop_ != config.
nexthop) {
206 return NexthopChange;
209 return AttributeChange;
211 for (vector<string>::const_iterator it = config.
route_targets.begin();
215 if (rtarget_list_.find(rtarget) == rtarget_list_.end()) {
216 return AttributeChange;
219 if (community_ != GetCommunity(config)) {
220 return AttributeChange;
226 template <
typename T>
233 info->set_prefix(prefix_.ToString());
234 info->set_static_rt(path ?
true :
false);
235 info->set_nexthop(nexthop_.to_string());
236 if (nexthop_route_) {
237 ShowRouteBrief show_route;
238 nexthop_route_->FillRouteInfo(table, &show_route);
239 info->set_nexthop_rt(show_route);
242 vector<string> community_list;
243 BOOST_FOREACH(uint32_t value, community_->communities()) {
246 info->set_community_list(community_list);
248 vector<string> route_target_list;
249 for (RouteTargetList::const_iterator it = rtarget_list_.begin();
250 it != rtarget_list_.end(); ++it) {
251 route_target_list.push_back(it->ToString());
253 info->set_route_target_list(route_target_list);
258 info->set_secondary_tables(
265 template <
typename T>
271 if (is_nexthop_route(route) && !unregistered()) {
310 manager_->EnqueueStaticRouteReq(req);
320 template <
typename T>
324 for (vector<string>::const_iterator it = config.
communities.begin();
331 CommunityDB *comm_db = routing_instance()->server()->comm_db();
332 return comm_db->
Locate(comm_spec);
344 template <
typename T>
347 for (RouteTargetList::const_iterator it = rtarget_list().begin();
348 it != rtarget_list().end(); it++) {
349 export_list.push_back(it->GetExtCommunity());
352 BgpServer *server = routing_instance()->server();
357 int vn_index = routing_instance()->virtual_network_index();
358 if (export_list.empty() && vn_index) {
360 if (asn >
AS2_MAX && vn_index > 0xffff) {
373 return new_ext_community;
376 template <
typename T>
378 rtarget_list_.clear();
379 for (vector<string>::const_iterator it = config.
route_targets.begin();
385 rtarget_list_.insert(rtarget);
387 community_ = GetCommunity(config);
391 template <
typename T>
399 if (!static_route || static_route->
IsDeleted())
return;
401 for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
402 it != NexthopPathIds()->end(); it++) {
405 "Removed Static route path " << static_route->
ToString() <<
407 " in table " << bgp_table()->name());
411 partition->
Delete(static_route);
413 partition->
Notify(static_route);
418 template <
typename T>
426 if (static_route == NULL)
return;
431 for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
432 it != NexthopPathIds()->end(); it++) {
436 "Update attributes of StaticRoute path "
437 << static_route->
ToString() <<
" path_id "
439 << bgp_table()->name());
444 existing_path->
GetAttr(), ptr);
458 partition->
Notify(static_route);
462 template <
typename T>
472 if (static_route == NULL) {
473 static_route =
new RouteT(prefix_);
474 partition->
Add(static_route);
480 for (Route::PathList::iterator it = nexthop_route()->GetPathList().begin();
481 it != nexthop_route()->GetPathList().end(); it++) {
482 BgpPath *nexthop_route_path =
static_cast<BgpPath *
>(it.operator->());
488 if (nexthop_route()->BestPath()->PathCompare(*nexthop_route_path,
true))
495 if (nexthop_route()->DuplicateForwardingPath(nexthop_route_path))
501 nexthop_route_path->
GetAttr(), ptr);
521 vpn_route->GetPrefix().route_distinguisher());
530 if (existing_path != NULL) {
531 if ((new_attr.get() != existing_path->
GetAttr()) ||
549 partition->
Notify(static_route);
552 "Added Static Route path " << static_route->
ToString() <<
554 " in table " << bgp_table()->name());
557 if (!old_path_ids)
return;
559 for (NexthopPathIdList::iterator it = old_path_ids->begin();
560 it != old_path_ids->end(); it++) {
561 if (NexthopPathIds()->find(*it) != NexthopPathIds()->end())
564 partition->
Notify(static_route);
567 "Removed StaticRoute path " << static_route->
ToString() <<
569 " in table " << bgp_table()->name());
573 template <
typename T>
581 partition->
Notify(static_route);
584 template <
typename T>
598 template <
typename T>
600 : rtinstance_(rtinstance),
601 listener_(rtinstance_->server()->condition_listener(GetFamily())),
604 TaskScheduler::GetInstance()->GetTaskId(
"bgp::Config"), 0)) {
627 assert(addr.is_v4());
633 assert(addr.is_v6());
637 template <
typename T>
639 static_route_queue_->Enqueue(req);
642 template <
typename T>
652 (listener_->GetMatchState(table, route, info));
655 switch (req->
type_) {
692 listener_->RemoveMatchState(table, route, info);
695 UnregisterAndResolveStaticRoute(info);
704 template <
typename T>
708 for (StaticRouteProcessList::iterator
709 it = unregister_static_route_list_.begin();
710 it != unregister_static_route_list_.end(); ++it) {
712 listener_->UnregisterMatchCondition(info->
bgp_table(), info);
716 unregister_static_route_list_.clear();
718 if (static_route_map_.empty()) {
719 rtinstance_->server()->RemoveStaticRouteMgr(
this);
722 if (!routing_instance()->deleted() &&
723 routing_instance()->config()) {
724 ProcessStaticRouteConfig();
729 template <
typename T>
731 std::scoped_lock lock(mutex_);
732 unregister_static_route_list_.insert(entry);
733 unregister_list_trigger_->Set();
736 template <
typename T>
745 typename StaticRouteMap::iterator it = static_route_map_.find(route_key);
746 if (it != static_route_map_.end()) {
748 if (it->second->deleted())
return;
757 assert(change != StaticRouteT::PrefixChange);
760 if (change == StaticRouteT::NoChange)
764 if (change == StaticRouteT::AttributeChange) {
774 listener_->RemoveMatchCondition(
775 match->
bgp_table(), it->second.get(), callback);
780 new StaticRouteT(routing_instance(),
this, prefix, config);
783 if (static_route_map_.empty())
784 rtinstance_->server()->InsertStaticRouteMgr(
this);
785 static_route_map_.insert(make_pair(route_key, static_route_match));
787 listener_->AddMatchCondition(match->
bgp_table(), static_route_match.get(),
791 template <
typename T>
798 UnregisterAndResolveStaticRoute(match);
803 template <
typename T>
806 typename StaticRouteMap::iterator it = static_route_map_.find(static_route);
807 if (it == static_route_map_.end())
return;
809 if (it->second->deleted())
return;
815 listener_->RemoveMatchCondition(match->
bgp_table(), match, callback);
818 template <
typename T>
821 if (routing_instance()->deleted() || !routing_instance()->config())
return;
823 routing_instance()->config()->static_routes(GetFamily());
824 typedef BgpInstanceConfig::StaticRouteList::const_iterator iterator_t;
825 for (iterator_t iter = list.begin(); iter != list.end(); ++iter) {
826 LocateStaticRoutePrefix(*iter);
830 template <
typename T>
832 if (static_route_queue_)
833 delete static_route_queue_;
836 template <
typename T>
840 routing_instance()->config()->static_routes(GetFamily());
843 config_list.begin(), config_list.end(),
850 template <
typename T>
853 for (
typename StaticRouteMap::iterator it = static_route_map_.begin();
854 it != static_route_map_.end(); it++) {
855 RemoveStaticRoutePrefix(it->first);
859 template <
typename T>
861 typename StaticRouteMap::iterator loc,
862 StaticRouteConfigList::iterator it) {
863 AddressT address = this->GetAddress(it->address);
864 PrefixT prefix(address, it->prefix_length);
865 RouteKey route_key(prefix, it->nexthop);
870 template <
typename T>
872 LocateStaticRoutePrefix(*it);
875 template <
typename T>
877 RemoveStaticRoutePrefix(loc->first);
880 template <
typename T>
882 StaticRouteConfigList::iterator it) {
883 LocateStaticRoutePrefix(*it);
886 template <
typename T>
889 for (
typename StaticRouteMap::iterator it = static_route_map_.begin();
890 it != static_route_map_.end(); ++it) {
897 template <
typename T>
900 for (
typename StaticRouteMap::iterator it = static_route_map_.begin();
901 it != static_route_map_.end(); ++it) {
908 template <
typename T>
910 unregister_list_trigger_->set_disable();
913 template <
typename T>
915 unregister_list_trigger_->set_enable();
918 template <
typename T>
921 return static_route_map_.size();
924 template <
typename T>
928 for (
typename StaticRouteMap::const_iterator it = static_route_map_.begin();
929 it != static_route_map_.end(); ++it) {
940 if (static_route_map_.empty())
943 info->set_ri_name(ri->
name());
945 static_route_map_.begin(); it != static_route_map_.end(); ++it) {
948 StaticRouteInfo static_info;
950 info->static_route_list.push_back(static_info);
boost::asio::ip::address_v6 Ip6Address
boost::asio::ip::address IpAddress
boost::asio::ip::address_v4 Ip4Address
boost::intrusive_ptr< const AsPath > AsPathPtr
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
#define BGP_LOG_FLAG_TRACE
#define BGP_LOG_STR(obj, level, flags, arg)
static Family VpnFamilyFromFamily(Family family)
BgpAttrPtr ReplaceSubProtocolAndLocate(const BgpAttr *attr, const std::string &sbp)
BgpAttrPtr ReplaceAsPathAndLocate(const BgpAttr *attr, AsPathPtr aspath)
BgpAttrPtr ReplaceExtCommunityAndLocate(const BgpAttr *attr, ExtCommunityPtr extcomm)
BgpAttrPtr ReplaceSourceRdAndLocate(const BgpAttr *attr, const RouteDistinguisher &source_rd)
BgpAttrPtr ReplaceCommunityAndLocate(const BgpAttr *attr, CommunityPtr community)
const IpAddress & nexthop() const
const ExtCommunity * ext_community() const
boost::function< void(BgpTable *, ConditionMatch *)> RequestDoneCb
void SetMatchState(BgpTable *table, BgpRoute *route, ConditionMatch *obj, ConditionMatchState *state=NULL)
ConditionMatchState * GetMatchState(BgpTable *table, BgpRoute *route, ConditionMatch *obj)
std::set< StaticRouteConfig > StaticRouteList
TypePtr Locate(Type *attr)
uint32_t GetLabel() const
uint32_t GetFlags() const
static std::string PathIdString(uint32_t path_id)
virtual bool IsReplicated() const
const BgpAttr * GetAttr() const
int PathCompare(const BgpPath &rhs, bool allow_ecmp) 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)
const BgpRoute * src_rt() const
const BgpTable * src_table() const
ExtCommunityDB * extcomm_db()
BgpConditionListener * condition_listener(Address::Family family)
as_t autonomous_system() const
RoutePathReplicator * replicator(Address::Family family)
RoutingInstance * routing_instance()
uint32_t num_matchstate() const
virtual std::string ToString() const =0
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
const bytes_type & GetExtCommunity() const
std::vector< std::string > GetReplicatedTableNameList(const BgpTable *table, const BgpRoute *route, const BgpPath *path) const
static RouteTarget FromString(const std::string &str, boost::system::error_code *error=NULL)
const PathList & GetPathList() const
const std::string & name() const
BgpTable * GetTable(Address::Family fmly)
bool IsMasterRoutingInstance() const
void StopStaticRouteDone(BgpTable *table, ConditionMatch *info)
virtual uint32_t GetRouteCount() const
int CompareStaticRoute(typename StaticRouteMap::iterator loc, StaticRouteConfigList::iterator it)
virtual void UpdateStaticRouteConfig()
virtual void DisableUnregisterTrigger()
virtual void ProcessStaticRouteConfig()
virtual bool FillStaticRouteInfo(RoutingInstance *rtinstance, StaticRouteEntriesInfo *info) const
Address::Family GetFamily() const
void EnqueueStaticRouteReq(StaticRouteRequest *req)
BgpInstanceConfig::StaticRouteList StaticRouteConfigList
virtual uint32_t GetDownRouteCount() const
virtual void EnableUnregisterTrigger()
virtual void FlushStaticRouteConfig()
void DelStaticRoute(typename StaticRouteMap::iterator loc)
void LocateStaticRoutePrefix(const StaticRouteConfig &config)
bool ProcessUnregisterList()
AddressT GetAddress(IpAddress addr) const
virtual void UpdateAllRoutes()
void AddStaticRoute(StaticRouteConfigList::iterator it)
virtual void NotifyAllRoutes()
StaticRouteMgr(RoutingInstance *instance)
void UpdateStaticRoute(typename StaticRouteMap::iterator loc, StaticRouteConfigList::iterator it)
void RemoveStaticRoutePrefix(const RouteKey &static_route)
bool StaticRouteEventCallback(StaticRouteRequest *req)
WorkQueue< StaticRouteRequest * > * static_route_queue_
static int static_route_task_id_
void UnregisterAndResolveStaticRoute(StaticRoutePtr entry)
DISALLOW_COPY_AND_ASSIGN(StaticRouteState)
StaticRouteState(StaticRoutePtr info)
virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route, bool deleted)
DISALLOW_COPY_AND_ASSIGN(StaticRoute)
set< uint32_t > NexthopPathIdList
RoutingInstance * routing_instance_
virtual string ToString() const
BgpRoute * nexthop_route() const
RoutingInstance * routing_instance() const
CompareResult CompareConfig(const StaticRouteConfig &config)
ExtCommunityPtr UpdateExtCommunity(const BgpAttr *attr) const
Address::Family GetFamily() const
set< RouteTarget > RouteTargetList
BgpRoute * nexthop_route_
const IpAddress & nexthop() const
bool unregistered() const
const RouteTargetList & rtarget_list() const
StaticRouteMgr< T > StaticRouteMgrT
AddressT GetAddress(IpAddress addr) const
StaticRoute(RoutingInstance *rtinstance, StaticRouteMgrT *manager, const PrefixT &static_route, const StaticRouteConfig &config)
const PrefixT & prefix() const
NexthopPathIdList * NexthopPathIds()
void set_nexthop_route(BgpRoute *nexthop_route)
void FillShowInfo(StaticRouteInfo *info) const
void AddStaticRoute(NexthopPathIdList *list)
StaticRouteMgrT * manager_
NexthopPathIdList nexthop_path_ids_
RouteTargetList rtarget_list_
bool is_nexthop_route(BgpRoute *route)
void UpdateAttributes(const StaticRouteConfig &config)
BgpTable * bgp_table() const
CommunityPtr GetCommunity(const StaticRouteConfig &config)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
int GetTaskId(const std::string &name)
static TaskScheduler * GetInstance()
void map_difference(ForwardIterator __first1, ForwardIterator __last1, ForwardIterator __first2, ForwardIterator __last2, AddFunctor __add_fn, DelFunctor __del_fn, EqFunctor __eq_fn)
static ExtCommunityPtr UpdateExtCommunity(BgpServer *server, const RoutingInstance *rtinstance, const ExtCommunity *ext_community, const ExtCommunity::ExtCommunityList &export_list)
const string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol)
ConditionMatchPtr StaticRoutePtr
std::vector< std::string > route_targets
std::vector< std::string > communities
#define CHECK_CONCURRENCY(...)
#define KEY_COMPARE(x, y)