OpenSDN source code
static_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <boost/assign/list_of.hpp>
9 #include <boost/foreach.hpp>
10 
11 #include <algorithm>
12 #include <string>
13 #include <vector>
14 
15 #include "base/map_util.h"
16 #include "base/task_annotations.h"
17 #include "base/task_trigger.h"
18 #include "bgp/bgp_config.h"
19 #include "bgp/bgp_log.h"
20 #include "bgp/bgp_server.h"
26 #include "bgp/routing-instance/static_route_types.h"
28 #include "net/community_type.h"
29 
30 using boost::assign::list_of;
31 using boost::system::error_code;
32 using std::make_pair;
33 using std::set;
34 using std::string;
35 using std::vector;
36 
38 public:
40  }
42  return info_;
43  }
44 
45 private:
48 };
49 
50 template <typename T>
51 class StaticRoute : public ConditionMatch {
52 public:
53  typedef typename T::RouteT RouteT;
54  typedef typename T::VpnRouteT VpnRouteT;
55  typedef typename T::PrefixT PrefixT;
56  typedef typename T::AddressT AddressT;
58 
59  // List of Route targets
60  typedef set<RouteTarget> RouteTargetList;
61 
62  // List of path ids for the Nexthop
63  typedef set<uint32_t> NexthopPathIdList;
64 
66  NoChange = 0,
69  AttributeChange = 3
70  };
71 
72  StaticRoute(RoutingInstance *rtinstance, StaticRouteMgrT *manager,
73  const PrefixT &static_route, const StaticRouteConfig &config);
76  return manager_->GetAddress(addr);
77  }
78 
80  void FillShowInfo(StaticRouteInfo *info) const;
81 
82  const PrefixT &prefix() const {
83  return prefix_;
84  }
85 
86  const IpAddress &nexthop() const {
87  return nexthop_;
88  }
89 
91  return routing_instance_;
92  }
93 
94  BgpTable *bgp_table() const {
95  return routing_instance_->GetTable(this->GetFamily());
96  }
97 
99  return nexthop_route_;
100  }
101 
103 
106 
107  if (!nexthop_route_) {
108  nexthop_path_ids_.clear();
109  return;
110  }
111 
112  assert(nexthop_path_ids_.empty());
113 
114  for (Route::PathList::iterator it =
115  nexthop_route->GetPathList().begin();
116  it != nexthop_route->GetPathList().end(); it++) {
117  BgpPath *path = static_cast<BgpPath *>(it.operator->());
118 
119  // Infeasible paths are not considered
120  if (!path->IsFeasible()) break;
121 
122  // take snapshot of all ECMP paths
123  if (nexthop_route_->BestPath()->PathCompare(*path, true)) break;
124 
125  // Use the nexthop attribute of the nexthop path as the path id.
126  uint32_t path_id = path->GetAttr()->nexthop().to_v4().to_ulong();
127  nexthop_path_ids_.insert(path_id);
128  }
129  }
130 
131  const RouteTargetList &rtarget_list() const {
132  return rtarget_list_;
133  }
134 
135  void UpdateAttributes(const StaticRouteConfig &config);
136  void AddStaticRoute(NexthopPathIdList *list);
137  void UpdateStaticRoute();
138  void RemoveStaticRoute();
139  void NotifyRoute();
140  bool IsPending() const;
141 
142  virtual bool Match(BgpServer *server, BgpTable *table,
143  BgpRoute *route, bool deleted);
144 
145  virtual string ToString() const {
146  return (string("StaticRoute ") + nexthop_.to_string());
147  }
148 
150  unregistered_ = true;
151  }
152 
153  bool unregistered() const {
154  return unregistered_;
155  }
156 
157 private:
158  // Helper function to match
159  bool is_nexthop_route(BgpRoute *route) {
160  RouteT *ip_route = dynamic_cast<RouteT *>(route);
161  return (nexthop_ == ip_route->GetPrefix().addr());
162  }
163 
165  ExtCommunityPtr UpdateExtCommunity(const BgpAttr *attr) const;
166 
176 
178 };
179 
180 template <typename T>
182  StaticRouteMgrT *manager, const PrefixT &prefix,
183  const StaticRouteConfig &config)
184  : routing_instance_(rtinstance),
185  manager_(manager),
186  prefix_(prefix),
187  nexthop_(config.nexthop),
188  nexthop_route_(NULL),
189  unregistered_(false) {
190  UpdateAttributes(config);
191 }
192 
193 //
194 // Compare the given config against the current state of the StaticRoute.
195 // Return the appropriate value from CompareResult.
196 //
197 template <typename T>
199  const StaticRouteConfig &config) {
200  AddressT address = this->GetAddress(config.address);
201  PrefixT prefix(address, config.prefix_length);
202  if (prefix_ != prefix) {
203  return PrefixChange;
204  }
205  if (nexthop_ != config.nexthop) {
206  return NexthopChange;
207  }
208  if (rtarget_list_.size() != config.route_targets.size()) {
209  return AttributeChange;
210  }
211  for (vector<string>::const_iterator it = config.route_targets.begin();
212  it != config.route_targets.end(); ++it) {
213  error_code ec;
214  RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
215  if (rtarget_list_.find(rtarget) == rtarget_list_.end()) {
216  return AttributeChange;
217  }
218  }
219  if (community_ != GetCommunity(config)) {
220  return AttributeChange;
221  }
222 
223  return NoChange;
224 }
225 
226 template <typename T>
227 void StaticRoute<T>::FillShowInfo(StaticRouteInfo *info) const {
228  BgpTable *table = bgp_table();
229  RouteT rt_key(prefix_);
230  const BgpRoute *route = static_cast<const BgpRoute *>(table->Find(&rt_key));
231  const BgpPath *path = route ? route->FindPath(BgpPath::StaticRoute) : NULL;
232 
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);
240  }
241 
242  vector<string> community_list;
243  BOOST_FOREACH(uint32_t value, community_->communities()) {
244  community_list.push_back(CommunityType::CommunityToString(value));
245  }
246  info->set_community_list(community_list);
247 
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());
252  }
253  info->set_route_target_list(route_target_list);
254 
255  if (path) {
256  const RoutePathReplicator *replicator = table->server()->replicator(
257  Address::VpnFamilyFromFamily(GetFamily()));
258  info->set_secondary_tables(
259  replicator->GetReplicatedTableNameList(table, route, path));
260  }
261 }
262 
263 // Match function called from BgpConditionListener
264 // Concurrency : db::DBTable
265 template <typename T>
267  BgpRoute *route, bool deleted) {
268  CHECK_CONCURRENCY("db::DBTable");
270 
271  if (is_nexthop_route(route) && !unregistered()) {
272  if (deleted)
274  else
276  } else {
277  return false;
278  }
279 
280  BgpConditionListener *listener = server->condition_listener(GetFamily());
281  StaticRouteState *state = static_cast<StaticRouteState *>
282  (listener->GetMatchState(table, route, this));
283  if (!deleted) {
284  // MatchState is added to the Route to ensure that DBEntry is not
285  // deleted before the module processes the WorkQueue request.
286  if (!state) {
287  state = new StaticRouteState(StaticRoutePtr(this));
288  listener->SetMatchState(table, route, this, state);
289  }
290  } else {
291  // MatchState is set on all the Routes that matches the conditions
292  // Retrieve to check and ignore delete of unseen Add Match
293  if (state == NULL) {
294  // Not seen ADD ignore DELETE
295  return false;
296  }
297  }
298 
299  // The MatchState reference is taken to ensure that the route is not
300  // deleted when request is still present in the queue
301  // This is to handle the case where MatchState already exists and
302  // deleted entry gets reused or reused entry gets deleted.
303  state->IncrementRefCnt();
304 
305  // Post the Match result to StaticRoute processing task to take Action
306  // Nexthop route found in NAT instance ==> Add Static Route
307  // and stitch the Path Attribute from nexthop route
308  StaticRouteRequest *req =
309  new StaticRouteRequest(type, table, route, StaticRoutePtr(this));
310  manager_->EnqueueStaticRouteReq(req);
311 
312  return true;
313 }
314 
315 //
316 // Build a Community for the given StaticRouteConfig.
317 // Always add the AcceptOwnNexthop community in addition to the configured
318 // list.
319 //
320 template <typename T>
322  CommunitySpec comm_spec;
323  comm_spec.communities.push_back(CommunityType::AcceptOwnNexthop);
324  for (vector<string>::const_iterator it = config.communities.begin();
325  it != config.communities.end(); ++it) {
326  uint32_t value = CommunityType::CommunityFromString(*it);
327  if (!value)
328  continue;
329  comm_spec.communities.push_back(value);
330  }
331  CommunityDB *comm_db = routing_instance()->server()->comm_db();
332  return comm_db->Locate(comm_spec);
333 }
334 
335 //
336 // Build an updated ExtCommunity for the static route.
337 //
338 // Replace any RouteTargets with the list of RouteTargets for the StaticRoute.
339 // If the StaticRoute has an empty RouteTarget list, then we infer that this
340 // is not a snat use case and add the OriginVn as well. We don't want to add
341 // OriginVn in snat scenario because the route has to be imported into many
342 // VRFs and we want to set the OriginVn differently for each imported route.
343 //
344 template <typename T>
346  ExtCommunity::ExtCommunityList export_list;
347  for (RouteTargetList::const_iterator it = rtarget_list().begin();
348  it != rtarget_list().end(); it++) {
349  export_list.push_back(it->GetExtCommunity());
350  }
351 
352  BgpServer *server = routing_instance()->server();
353  ExtCommunityDB *extcomm_db = server->extcomm_db();
354  ExtCommunityPtr new_ext_community = extcomm_db->ReplaceRTargetAndLocate(
355  attr->ext_community(), export_list);
356 
357  int vn_index = routing_instance()->virtual_network_index();
358  if (export_list.empty() && vn_index) {
359  as_t asn = server->autonomous_system();
360  if (asn > AS2_MAX && vn_index > 0xffff) {
361  OriginVn origin_vn(asn, AS_TRANS);
362  new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
363  new_ext_community.get(), origin_vn.GetExtCommunity());
364  OriginVn origin_vn2(AS_TRANS, vn_index);
365  new_ext_community = extcomm_db->AppendAndLocate(
366  new_ext_community.get(), origin_vn2.GetExtCommunity());
367  } else {
368  OriginVn origin_vn(server->autonomous_system(), vn_index);
369  new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
370  new_ext_community.get(), origin_vn.GetExtCommunity());
371  }
372  }
373  return new_ext_community;
374 }
375 
376 template <typename T>
378  rtarget_list_.clear();
379  for (vector<string>::const_iterator it = config.route_targets.begin();
380  it != config.route_targets.end(); ++it) {
381  error_code ec;
382  RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
383  if (ec.failed())
384  continue;
385  rtarget_list_.insert(rtarget);
386  }
387  community_ = GetCommunity(config);
388 }
389 
390 // RemoveStaticRoute
391 template <typename T>
393  CHECK_CONCURRENCY("bgp::StaticRoute");
394  RouteT rt_key(prefix_);
395  DBTablePartition *partition =
396  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
397  BgpRoute *static_route =
398  static_cast<BgpRoute *>(partition->Find(&rt_key));
399  if (!static_route || static_route->IsDeleted()) return;
400 
401  for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
402  it != NexthopPathIds()->end(); it++) {
403  static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
404  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
405  "Removed Static route path " << static_route->ToString() <<
406  " path_id " << BgpPath::PathIdString(*it) <<
407  " in table " << bgp_table()->name());
408  }
409 
410  if (!static_route->HasPaths()) {
411  partition->Delete(static_route);
412  } else {
413  partition->Notify(static_route);
414  }
415 }
416 
417 // UpdateStaticRoute
418 template <typename T>
420  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
421  RouteT rt_key(prefix_);
422  DBTablePartition *partition =
423  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
424  BgpRoute *static_route =
425  static_cast<BgpRoute *>(partition->Find(&rt_key));
426  if (static_route == NULL) return;
427 
428  static_route->ClearDelete();
429 
430  BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
431  for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
432  it != NexthopPathIds()->end(); it++) {
433  BgpPath *existing_path =
434  static_route->FindPath(BgpPath::StaticRoute, NULL, *it);
435  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
436  "Update attributes of StaticRoute path "
437  << static_route->ToString() << " path_id "
438  << BgpPath::PathIdString(*it) << " in table "
439  << bgp_table()->name());
440 
441  // Add the RouteTarget and OrignVn in the ExtCommunity attribute.
442  ExtCommunityPtr ptr = UpdateExtCommunity(existing_path->GetAttr());
443  BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
444  existing_path->GetAttr(), ptr);
445 
446  // Use pre-calculated community from the StaticRoute.
447  new_attr =
448  attr_db->ReplaceCommunityAndLocate(new_attr.get(), community_);
449 
450  BgpPath *new_path =
451  new BgpPath(*it, BgpPath::StaticRoute, new_attr.get(),
452  existing_path->GetFlags(), existing_path->GetLabel());
453 
454  static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
455 
456  static_route->InsertPath(new_path);
457  }
458  partition->Notify(static_route);
459 }
460 
461 // AddStaticRoute
462 template <typename T>
464  CHECK_CONCURRENCY("bgp::StaticRoute");
465 
466  RouteT rt_key(prefix_);
467  DBTablePartition *partition =
468  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
469  BgpRoute *static_route =
470  static_cast<BgpRoute *>(partition->Find(&rt_key));
471 
472  if (static_route == NULL) {
473  static_route = new RouteT(prefix_);
474  partition->Add(static_route);
475  } else {
476  static_route->ClearDelete();
477  }
478 
479  BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
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->());
483 
484  // Infeasible paths are not considered
485  if (!nexthop_route_path->IsFeasible()) break;
486 
487  // take snapshot of all ECMP paths
488  if (nexthop_route()->BestPath()->PathCompare(*nexthop_route_path, true))
489  break;
490 
491  // Skip paths with duplicate forwarding information. This ensures
492  // that we generate only one path with any given next hop and label
493  // when there are multiple nexthop paths from the original source
494  // received via different peers e.g. directly via XMPP and via BGP.
495  if (nexthop_route()->DuplicateForwardingPath(nexthop_route_path))
496  continue;
497 
498  // Add the route target in the ExtCommunity attribute.
499  ExtCommunityPtr ptr = UpdateExtCommunity(nexthop_route_path->GetAttr());
500  BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
501  nexthop_route_path->GetAttr(), ptr);
502 
503  // Use pre-calculated community from the static route.
504  new_attr =
505  attr_db->ReplaceCommunityAndLocate(new_attr.get(), community_);
506 
507  // Strip aspath. This is required when the nexthop route is learnt
508  // via BGP.
509  new_attr = attr_db->ReplaceAsPathAndLocate(new_attr.get(), AsPathPtr());
510 
511  // Replace the source rd if the nexthop path is a secondary path
512  // of a primary path in the l3vpn table. Use the RD of the primary.
513  if (nexthop_route_path->IsReplicated()) {
514  const BgpSecondaryPath *spath =
515  static_cast<const BgpSecondaryPath *>(nexthop_route_path);
516  const RoutingInstance *ri = spath->src_table()->routing_instance();
517  if (ri->IsMasterRoutingInstance()) {
518  const VpnRouteT *vpn_route =
519  static_cast<const VpnRouteT *>(spath->src_rt());
520  new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(),
521  vpn_route->GetPrefix().route_distinguisher());
522  }
523  }
524 
525  // Check whether we already have a path with the associated path id.
526  uint32_t path_id =
527  nexthop_route_path->GetAttr()->nexthop().to_v4().to_ulong();
528  BgpPath *existing_path = static_route->FindPath(BgpPath::StaticRoute,
529  NULL, path_id);
530  if (existing_path != NULL) {
531  if ((new_attr.get() != existing_path->GetAttr()) ||
532  (nexthop_route_path->GetFlags() != existing_path->GetFlags()) ||
533  (nexthop_route_path->GetLabel() != existing_path->GetLabel())) {
534  // Update Attributes and notify (if needed)
535  static_route->RemovePath(BgpPath::StaticRoute, NULL, path_id);
536  } else {
537  continue;
538  }
539  }
540 
541  // Populate SubProtocol for StaticRoute.
542  new_attr = attr_db->ReplaceSubProtocolAndLocate(new_attr.get(),
544 
545  BgpPath *new_path =
546  new BgpPath(path_id, BgpPath::StaticRoute, new_attr.get(),
547  nexthop_route_path->GetFlags(), nexthop_route_path->GetLabel());
548  static_route->InsertPath(new_path);
549  partition->Notify(static_route);
550 
551  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
552  "Added Static Route path " << static_route->ToString() <<
553  " path_id " << BgpPath::PathIdString(path_id) <<
554  " in table " << bgp_table()->name());
555  }
556 
557  if (!old_path_ids) return;
558 
559  for (NexthopPathIdList::iterator it = old_path_ids->begin();
560  it != old_path_ids->end(); it++) {
561  if (NexthopPathIds()->find(*it) != NexthopPathIds()->end())
562  continue;
563  static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
564  partition->Notify(static_route);
565 
566  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
567  "Removed StaticRoute path " << static_route->ToString() <<
568  " path_id " << BgpPath::PathIdString(*it) <<
569  " in table " << bgp_table()->name());
570  }
571 }
572 
573 template <typename T>
575  RouteT rt_key(prefix_);
576  DBTablePartition *partition =
577  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
578  BgpRoute *static_route = static_cast<BgpRoute *>(partition->Find(&rt_key));
579  if (!static_route)
580  return;
581  partition->Notify(static_route);
582 }
583 
584 template <typename T>
586  RouteT rt_key(prefix_);
587  DBTablePartition *partition =
588  static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
589  const BgpRoute *route = static_cast<BgpRoute *>(partition->Find(&rt_key));
590  return (!route || !route->FindPath(BgpPath::StaticRoute));
591 }
592 
593 template <>
595 template <>
597 
598 template <typename T>
600  : rtinstance_(rtinstance),
601  listener_(rtinstance_->server()->condition_listener(GetFamily())),
602  unregister_list_trigger_(new TaskTrigger(
603  boost::bind(&StaticRouteMgr::ProcessUnregisterList, this),
604  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)) {
605  if (static_route_task_id_ == -1) {
607  static_route_task_id_ = scheduler->GetTaskId("bgp::StaticRoute");
608  }
609 
612  boost::bind(&StaticRouteMgr::StaticRouteEventCallback, this, _1));
613 }
614 
615 template <>
617  return Address::INET;
618 }
619 
620 template <>
622  return Address::INET6;
623 }
624 
625 template <>
627  assert(addr.is_v4());
628  return addr.to_v4();
629 }
630 
631 template <>
633  assert(addr.is_v6());
634  return addr.to_v6();
635 }
636 
637 template <typename T>
639  static_route_queue_->Enqueue(req);
640 }
641 
642 template <typename T>
644  CHECK_CONCURRENCY("bgp::StaticRoute");
645  BgpTable *table = req->table_;
646  BgpRoute *route = req->rt_;
647  StaticRouteT *info = static_cast<StaticRouteT *>(req->info_.get());
648 
649  StaticRouteState *state = NULL;
650  if (route) {
651  state = static_cast<StaticRouteState *>
652  (listener_->GetMatchState(table, route, info));
653  }
654 
655  switch (req->type_) {
657  assert(state);
658  state->reset_deleted();
659  if (route->IsDeleted() || !route->BestPath() ||
660  !route->BestPath()->IsFeasible()) {
661  break;
662  }
663 
664  // Store the old path list
665  typename StaticRouteT::NexthopPathIdList path_ids;
666  path_ids.swap(*(info->NexthopPathIds()));
667 
668  // Populate the Nexthop PathID
669  info->set_nexthop_route(route);
670 
671  info->AddStaticRoute(&path_ids);
672  break;
673  }
675  assert(state);
676  info->RemoveStaticRoute();
677  if (info->deleted() || route->IsDeleted()) {
678  state->set_deleted();
679  }
680  info->set_nexthop_route(NULL);
681  break;
682  }
683  default: {
684  assert(0);
685  break;
686  }
687  }
688 
689  if (state) {
690  state->DecrementRefCnt();
691  if (state->refcnt() == 0 && state->deleted()) {
692  listener_->RemoveMatchState(table, route, info);
693  delete state;
694  if (!info->num_matchstate() && info->unregistered()) {
695  UnregisterAndResolveStaticRoute(info);
696  }
697  }
698  }
699 
700  delete req;
701  return true;
702 }
703 
704 template <typename T>
706  CHECK_CONCURRENCY("bgp::Config");
707 
708  for (StaticRouteProcessList::iterator
709  it = unregister_static_route_list_.begin();
710  it != unregister_static_route_list_.end(); ++it) {
711  StaticRouteT *info = static_cast<StaticRouteT *>(it->get());
712  listener_->UnregisterMatchCondition(info->bgp_table(), info);
713  static_route_map_.erase(RouteKey(info->prefix(), info->nexthop()));
714  }
715 
716  unregister_static_route_list_.clear();
717 
718  if (static_route_map_.empty()) {
719  rtinstance_->server()->RemoveStaticRouteMgr(this);
720  }
721 
722  if (!routing_instance()->deleted() &&
723  routing_instance()->config()) {
724  ProcessStaticRouteConfig();
725  }
726  return true;
727 }
728 
729 template <typename T>
731  std::scoped_lock lock(mutex_);
732  unregister_static_route_list_.insert(entry);
733  unregister_list_trigger_->Set();
734 }
735 
736 template <typename T>
738  const StaticRouteConfig &config) {
739  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
740  AddressT address = this->GetAddress(config.address);
741  PrefixT prefix(address, config.prefix_length);
742  RouteKey route_key(prefix, config.nexthop);
743 
744  // Verify whether the entry already exists
745  typename StaticRouteMap::iterator it = static_route_map_.find(route_key);
746  if (it != static_route_map_.end()) {
747  // Wait for the delete complete cb
748  if (it->second->deleted()) return;
749 
750  StaticRouteT *match =
751  static_cast<StaticRouteT *>(it->second.get());
752  // Check whether the config has got updated
753  typename StaticRouteT::CompareResult change =
754  match->CompareConfig(config);
755 
756  // StaticRoutePrefix is the key, it can't change.
757  assert(change != StaticRouteT::PrefixChange);
758 
759  // Skip if there's no change.
760  if (change == StaticRouteT::NoChange)
761  return;
762 
763  // Update the attributes and any existing BgpPaths.
764  if (change == StaticRouteT::AttributeChange) {
765  match->UpdateAttributes(config);
766  match->UpdateStaticRoute();
767  return;
768  }
769 
770  // If the nexthop changes, remove the static route, if already added.
771  // To do this, remove match condition and wait for remove completion.
773  boost::bind(&StaticRouteMgr::StopStaticRouteDone, this, _1, _2);
774  listener_->RemoveMatchCondition(
775  match->bgp_table(), it->second.get(), callback);
776  return;
777  }
778 
779  StaticRouteT *match =
780  new StaticRouteT(routing_instance(), this, prefix, config);
781  StaticRoutePtr static_route_match = StaticRoutePtr(match);
782 
783  if (static_route_map_.empty())
784  rtinstance_->server()->InsertStaticRouteMgr(this);
785  static_route_map_.insert(make_pair(route_key, static_route_match));
786 
787  listener_->AddMatchCondition(match->bgp_table(), static_route_match.get(),
789 }
790 
791 template <typename T>
793  ConditionMatch *info) {
794  CHECK_CONCURRENCY("db::Walker");
795  StaticRoute<T> *match = static_cast<StaticRoute<T> *>(info);
796  match->set_unregistered();
797  if (!match->num_matchstate() && match->unregistered()) {
798  UnregisterAndResolveStaticRoute(match);
799  }
800  return;
801 }
802 
803 template <typename T>
805  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
806  typename StaticRouteMap::iterator it = static_route_map_.find(static_route);
807  if (it == static_route_map_.end()) return;
808 
809  if (it->second->deleted()) return;
810 
812  boost::bind(&StaticRouteMgr::StopStaticRouteDone, this, _1, _2);
813 
814  StaticRouteT *match = static_cast<StaticRouteT *>(it->second.get());
815  listener_->RemoveMatchCondition(match->bgp_table(), match, callback);
816 }
817 
818 template <typename T>
820  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
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);
827  }
828 }
829 
830 template <typename T>
832  if (static_route_queue_)
833  delete static_route_queue_;
834 }
835 
836 template <typename T>
838  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
839  StaticRouteConfigList config_list =
840  routing_instance()->config()->static_routes(GetFamily());
841 
842  map_difference(&static_route_map_,
843  config_list.begin(), config_list.end(),
844  boost::bind(&StaticRouteMgr<T>::CompareStaticRoute, this, _1, _2),
845  boost::bind(&StaticRouteMgr<T>::AddStaticRoute, this, _1),
846  boost::bind(&StaticRouteMgr<T>::DelStaticRoute, this, _1),
847  boost::bind(&StaticRouteMgr<T>::UpdateStaticRoute, this, _1, _2));
848 }
849 
850 template <typename T>
852  CHECK_CONCURRENCY("bgp::Config");
853  for (typename StaticRouteMap::iterator it = static_route_map_.begin();
854  it != static_route_map_.end(); it++) {
855  RemoveStaticRoutePrefix(it->first);
856  }
857 }
858 
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);
866  KEY_COMPARE(loc->first, route_key);
867  return 0;
868 }
869 
870 template <typename T>
871 void StaticRouteMgr<T>::AddStaticRoute(StaticRouteConfigList::iterator it) {
872  LocateStaticRoutePrefix(*it);
873 }
874 
875 template <typename T>
876 void StaticRouteMgr<T>::DelStaticRoute(typename StaticRouteMap::iterator loc) {
877  RemoveStaticRoutePrefix(loc->first);
878 }
879 
880 template <typename T>
881 void StaticRouteMgr<T>::UpdateStaticRoute(typename StaticRouteMap::iterator loc,
882  StaticRouteConfigList::iterator it) {
883  LocateStaticRoutePrefix(*it);
884 }
885 
886 template <typename T>
888  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
889  for (typename StaticRouteMap::iterator it = static_route_map_.begin();
890  it != static_route_map_.end(); ++it) {
891  StaticRouteT *static_route =
892  static_cast<StaticRouteT *>(it->second.get());
893  static_route->NotifyRoute();
894  }
895 }
896 
897 template <typename T>
899  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
900  for (typename StaticRouteMap::iterator it = static_route_map_.begin();
901  it != static_route_map_.end(); ++it) {
902  StaticRouteT *static_route =
903  static_cast<StaticRouteT *>(it->second.get());
904  static_route->UpdateStaticRoute();
905  }
906 }
907 
908 template <typename T>
910  unregister_list_trigger_->set_disable();
911 }
912 
913 template <typename T>
915  unregister_list_trigger_->set_enable();
916 }
917 
918 template <typename T>
920  CHECK_CONCURRENCY("bgp::ShowCommand");
921  return static_route_map_.size();
922 }
923 
924 template <typename T>
926  CHECK_CONCURRENCY("bgp::ShowCommand");
927  uint32_t count = 0;
928  for (typename StaticRouteMap::const_iterator it = static_route_map_.begin();
929  it != static_route_map_.end(); ++it) {
930  const StaticRouteT *static_route =
931  static_cast<const StaticRouteT *>(it->second.get());
932  if (static_route->IsPending())
933  count++;
934  }
935  return count;
936 }
937 
938 template <typename T> bool StaticRouteMgr<T>::FillStaticRouteInfo(
939  RoutingInstance *ri, StaticRouteEntriesInfo *info) const {
940  if (static_route_map_.empty())
941  return false;
942 
943  info->set_ri_name(ri->name());
945  static_route_map_.begin(); it != static_route_map_.end(); ++it) {
946  StaticRoute<T> *match =
947  static_cast<StaticRoute<T> *>(it->second.get());
948  StaticRouteInfo static_info;
949  match->FillShowInfo(&static_info);
950  info->static_route_list.push_back(static_info);
951  }
952  return true;
953 }
954 
955 // Explicit instantiation of StaticRouteMgr for INET and INET6.
956 template class StaticRouteMgr<StaticRouteInet>;
957 template class StaticRouteMgr<StaticRouteInet6>;
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
boost::intrusive_ptr< const AsPath > AsPathPtr
Definition: bgp_aspath.h:166
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
#define AS_TRANS
Definition: bgp_common.h:24
#define AS2_MAX
Definition: bgp_common.h:25
uint32_t as_t
Definition: bgp_common.h:22
#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
static Family VpnFamilyFromFamily(Family family)
Definition: address.cc:72
BgpAttrPtr ReplaceSubProtocolAndLocate(const BgpAttr *attr, const std::string &sbp)
Definition: bgp_attr.cc:1441
BgpAttrPtr ReplaceAsPathAndLocate(const BgpAttr *attr, AsPathPtr aspath)
Definition: bgp_attr.cc:1343
BgpServer * server()
Definition: bgp_attr.h:1041
BgpAttrPtr ReplaceExtCommunityAndLocate(const BgpAttr *attr, ExtCommunityPtr extcomm)
Definition: bgp_attr.cc:1367
BgpAttrPtr ReplaceSourceRdAndLocate(const BgpAttr *attr, const RouteDistinguisher &source_rd)
Definition: bgp_attr.cc:1407
BgpAttrPtr ReplaceCommunityAndLocate(const BgpAttr *attr, CommunityPtr community)
Definition: bgp_attr.cc:1351
const IpAddress & nexthop() const
Definition: bgp_attr.h:889
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:918
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
Definition: bgp_config.h:426
TypePtr Locate(Type *attr)
uint32_t GetLabel() const
Definition: bgp_path.h:90
uint32_t GetFlags() const
Definition: bgp_path.h:101
static std::string PathIdString(uint32_t path_id)
Definition: bgp_path.cc:19
virtual bool IsReplicated() const
Definition: bgp_path.h:92
bool IsFeasible() const
Definition: bgp_path.h:93
@ StaticRoute
Definition: bgp_path.h:42
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
int PathCompare(const BgpPath &rhs, bool allow_ecmp) const
Definition: bgp_path.cc:59
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
const BgpRoute * src_rt() const
Definition: bgp_path.h:187
const BgpTable * src_table() const
Definition: bgp_path.h:183
ExtCommunityDB * extcomm_db()
Definition: bgp_server.h:194
BgpConditionListener * condition_listener(Address::Family family)
Definition: bgp_server.h:119
as_t autonomous_system() const
Definition: bgp_server.h:213
RoutePathReplicator * replicator(Address::Family family)
Definition: bgp_server.h:153
BgpAttrDB * attr_db()
Definition: bgp_server.h:188
BgpServer * server()
Definition: bgp_table.cc:88
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
static uint32_t CommunityFromString(const std::string &comm, boost::system::error_code *perr=NULL)
static const std::string CommunityToString(uint32_t comm)
uint32_t num_matchstate() const
virtual std::string ToString() const =0
void ClearDelete()
Definition: db_entry.h:48
bool IsDeleted() const
Definition: db_entry.h:49
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
Definition: db_table.cc:496
ExtCommunityPtr AppendAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &list)
Definition: community.cc:766
ExtCommunityPtr ReplaceOriginVnAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityValue &origin_vn)
Definition: community.cc:954
ExtCommunityPtr ReplaceRTargetAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &export_list)
Definition: community.cc:813
std::vector< ExtCommunityValue > ExtCommunityList
Definition: community.h:156
const bytes_type & GetExtCommunity() const
Definition: origin_vn.h:34
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
Definition: route.h:47
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
Definition: static_route.h:116
virtual uint32_t GetDownRouteCount() const
virtual void EnableUnregisterTrigger()
T::AddressT AddressT
Definition: static_route.h:71
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)
T::PrefixT PrefixT
Definition: static_route.h:70
bool StaticRouteEventCallback(StaticRouteRequest *req)
WorkQueue< StaticRouteRequest * > * static_route_queue_
Definition: static_route.h:148
static int static_route_task_id_
Definition: static_route.h:120
void UnregisterAndResolveStaticRoute(StaticRoutePtr entry)
DISALLOW_COPY_AND_ASSIGN(StaticRouteState)
StaticRouteState(StaticRoutePtr info)
Definition: static_route.cc:39
StaticRoutePtr info_
Definition: static_route.cc:46
StaticRoutePtr info()
Definition: static_route.cc:41
virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route, bool deleted)
DISALLOW_COPY_AND_ASSIGN(StaticRoute)
T::PrefixT PrefixT
Definition: static_route.cc:55
set< uint32_t > NexthopPathIdList
Definition: static_route.cc:63
RoutingInstance * routing_instance_
virtual string ToString() const
BgpRoute * nexthop_route() const
Definition: static_route.cc:98
CommunityPtr community_
RoutingInstance * routing_instance() const
Definition: static_route.cc:90
CompareResult CompareConfig(const StaticRouteConfig &config)
ExtCommunityPtr UpdateExtCommunity(const BgpAttr *attr) const
Address::Family GetFamily() const
Definition: static_route.cc:74
set< RouteTarget > RouteTargetList
Definition: static_route.cc:60
BgpRoute * nexthop_route_
const IpAddress & nexthop() const
Definition: static_route.cc:86
bool unregistered() const
bool unregistered_
IpAddress nexthop_
const RouteTargetList & rtarget_list() const
StaticRouteMgr< T > StaticRouteMgrT
Definition: static_route.cc:57
AddressT GetAddress(IpAddress addr) const
Definition: static_route.cc:75
void set_unregistered()
T::VpnRouteT VpnRouteT
Definition: static_route.cc:54
StaticRoute(RoutingInstance *rtinstance, StaticRouteMgrT *manager, const PrefixT &static_route, const StaticRouteConfig &config)
void NotifyRoute()
const PrefixT & prefix() const
Definition: static_route.cc:82
NexthopPathIdList * NexthopPathIds()
void set_nexthop_route(BgpRoute *nexthop_route)
void FillShowInfo(StaticRouteInfo *info) const
void AddStaticRoute(NexthopPathIdList *list)
StaticRouteMgrT * manager_
NexthopPathIdList nexthop_path_ids_
void UpdateStaticRoute()
T::RouteT RouteT
Definition: static_route.cc:53
RouteTargetList rtarget_list_
T::AddressT AddressT
Definition: static_route.cc:56
bool is_nexthop_route(BgpRoute *route)
PrefixT prefix_
bool IsPending() const
void UpdateAttributes(const StaticRouteConfig &config)
BgpTable * bgp_table() const
Definition: static_route.cc:94
CommunityPtr GetCommunity(const StaticRouteConfig &config)
void RemoveStaticRoute()
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
static TaskScheduler * GetInstance()
Definition: task.cc:574
boost::intrusive_ptr< const Community > CommunityPtr
Definition: community.h:112
boost::intrusive_ptr< const ExtCommunity > ExtCommunityPtr
Definition: community.h:451
uint8_t type
Definition: load_balance.h:2
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
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
Definition: static_route.h:43
std::vector< uint32_t > communities
Definition: community.h:36
IpAddress nexthop
Definition: bgp_config.h:342
IpAddress address
Definition: bgp_config.h:340
std::vector< std::string > route_targets
Definition: bgp_config.h:343
std::vector< std::string > communities
Definition: bgp_config.h:344
BgpTable * table_
Definition: static_route.h:57
StaticRoutePtr info_
Definition: static_route.h:59
RequestType type_
Definition: static_route.h:56
#define CHECK_CONCURRENCY(...)
#define KEY_COMPARE(x, y)
Definition: util.h:71