OpenSDN source code
bgp_multicast.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 
6 #include "bgp/bgp_multicast.h"
7 
8 #include <boost/bind/bind.hpp>
9 #include <boost/foreach.hpp>
10 
11 #include "base/string_util.h"
12 #include "base/task_annotations.h"
13 #include "bgp/bgp_log.h"
14 #include "bgp/bgp_mvpn.h"
15 #include "bgp/bgp_server.h"
16 #include "bgp/bgp_update.h"
18 #include "bgp/mvpn/mvpn_route.h"
20 #include "bgp/routing-instance/routing_instance_analytics_types.h"
23 
24 using std::string;
25 using std::vector;
26 using namespace boost::placeholders;
27 
29 public:
30  explicit DeleteActor(McastTreeManager *tree_manager)
31  : LifetimeActor(tree_manager->table_->routing_instance()->server()->
32  lifetime_manager()),
33  tree_manager_(tree_manager) {
34  }
35  virtual ~DeleteActor() {
36  }
37 
38  virtual bool MayDelete() const {
39  return tree_manager_->MayDelete();
40  }
41 
42  virtual void Shutdown() {
43  tree_manager_->Shutdown();
44  }
45 
46  virtual void Destroy() {
47  tree_manager_->table_->DestroyTreeManager();
48  }
49 
50 private:
52 };
53 
54 //
55 // Constructor for McastForwarder. The level is determined by the route type.
56 // We get the address of the forwarder and the label_block from the attributes
57 // of the active path. The LabelBLockPtr needs to be copied so that we can
58 // release the label when processing a delete notification - we won't have the
59 // path at that point.
60 //
61 // The RD will be zero for BGP learnt routes and the RouterId will be zero for
62 // XMPP learnt routes.
63 //
65  : sg_entry_(sg_entry),
66  route_(route),
67  global_tree_route_(NULL),
68  label_(0),
69  address_(0),
70  rd_(route->GetPrefix().route_distinguisher()),
71  router_id_(route->GetPrefix().router_id()) {
72  const BgpPath *path = route->BestPath();
73  const BgpAttr *attr = path->GetAttr();
74 
77  address_ = attr->nexthop().to_v4();
78  label_block_ = attr->label_block();
79  } else {
81  const EdgeDiscovery::Edge *edge = attr->edge_discovery()->edge_list[0];
82  address_ = edge->address;
83  label_block_ = edge->label_block;
84  }
85 
86  if (path->GetAttr()->ext_community())
88 }
89 
90 //
91 // Destructor for McastForwarder. Flushes forward and reverse links to and
92 // from other McastForwarders.
93 //
96  FlushLinks();
97  ReleaseLabel();
98 }
99 
100 //
101 // Update the McastForwarder based on information in the ErmVpnRoute.
102 // Return true if something changed.
103 //
105  McastForwarder forwarder(sg_entry_, route);
106 
107  bool changed = false;
108  if (label_block_ != forwarder.label_block_) {
109  ReleaseLabel();
110  label_block_ = forwarder.label_block_;
111  changed = true;
112  }
113  if (address_ != forwarder.address_) {
114  address_ = forwarder.address_;
115  changed = true;
116  }
117  if (encap_ != forwarder.encap_) {
118  encap_ = forwarder.encap_;
119  changed = true;
120  }
121 
122  return changed;
123 }
124 
125 //
126 // Printable string for McastForwarder.
127 //
128 std::string McastForwarder::ToString() const {
130  return rd_.ToString() + " -> " + integerToString(label_);
131  } else {
132  return router_id_.to_string() + " -> " + integerToString(label_);
133  }
134 }
135 
136 //
137 // Find a link to the given McastForwarder.
138 //
140  for (McastForwarderList::iterator it = tree_links_.begin();
141  it != tree_links_.end(); ++it) {
142  if (*it == forwarder) return forwarder;
143  }
144  return NULL;
145 }
146 
147 //
148 // Add a link to the given McastForwarder.
149 //
151  assert(!FindLink(forwarder));
152  tree_links_.push_back(forwarder);
153 }
154 
155 //
156 // Remove a link to the given McastForwarder.
157 //
159  for (McastForwarderList::iterator it = tree_links_.begin();
160  it != tree_links_.end(); ++it) {
161  if (*it == forwarder) {
162  tree_links_.erase(it);
163  return;
164  }
165  }
166 }
167 
168 //
169 // Flush all links from this McastForwarder. Takes care of removing the
170 // reverse links as well.
171 //
173  for (McastForwarderList::iterator it = tree_links_.begin();
174  it != tree_links_.end(); ++it) {
175  (*it)->RemoveLink(this);
176  }
177  tree_links_.clear();
178 }
179 
180 //
181 // Allocate a label for this McastForwarder. The label gets allocated from
182 // the LabelBlock corresponding to the label range advertised by the peer.
183 // This is used when updating the distribution tree for the McastSGEntry to
184 // this McastForwarder belongs.
185 //
187  label_ = label_block_->AllocateLabel();
188 }
189 
190 //
191 // Release the label, if any, for this McastForwarder. This is required when
192 // updating the distribution tree for the McastSGEntry to which we belong.
193 //
195  if (label_ != 0) {
196  label_block_->ReleaseLabel(label_);
197  label_ = 0;
198  }
199 }
200 
201 //
202 // Add the GlobalTreeRoute for this McastForwarder. The GlobalTreeRoute is
203 // used by the tree builder to tell the associated control-node about the
204 // forwarding edges for Native McastForwarders attached it.
205 //
208  assert(!global_tree_route_);
209 
210  // Bail if there's no label allocated.
211  if (label_ == 0)
212  return;
213 
214  // Bail if we can't build a source RD.
215  if (sg_entry_->GetSourceRd().IsZero())
216  return;
217 
218  // Construct the prefix and route key.
219  BgpTable *table = static_cast<BgpTable *>(route_->get_table());
223  ErmVpnRoute rt_key(prefix);
224 
225  // Find or create the route.
226  McastManagerPartition *partition = sg_entry_->partition();
227  DBTablePartition *tbl_partition =
228  static_cast<DBTablePartition *>(partition->GetTablePartition());
229  ErmVpnRoute *route =
230  static_cast<ErmVpnRoute *>(tbl_partition->Find(&rt_key));
231  if (!route) {
232  route = new ErmVpnRoute(prefix);
233  tbl_partition->Add(route);
234  } else {
235  route->ClearDelete();
236  }
237 
238  // Build the attributes. Need to go through the tree links to build the
239  // EdgeForwardingSpec.
240  BgpServer *server = table->routing_instance()->server();
241  BgpAttrSpec attr_spec;
242  BgpAttrNextHop nexthop(server->bgp_identifier());
243  attr_spec.push_back(&nexthop);
244  BgpAttrSourceRd source_rd(sg_entry_->GetSourceRd());
245  attr_spec.push_back(&source_rd);
246  EdgeForwardingSpec efspec;
247  for (McastForwarderList::const_iterator it = tree_links_.begin();
248  it != tree_links_.end(); ++it) {
251  edge->inbound_label = label_;
252  edge->SetOutboundIp4Address((*it)->address());
253  edge->outbound_label = (*it)->label();
254  efspec.edge_list.push_back(edge);
255  }
256  attr_spec.push_back(&efspec);
257  // Add tunnel encaps for remote nodes
258  ExtCommunitySpec ext;
259  ext.AddTunnelEncaps(encap_);
260  if (!ext.communities.empty())
261  attr_spec.push_back(&ext);
262  BgpAttrPtr attr = server->attr_db()->Locate(attr_spec);
263 
264  // Add a path with source BgpPath::Local.
265  BgpPath *path = new BgpPath(0, BgpPath::Local, attr);
266  route->InsertPath(path);
267  tbl_partition->Notify(route);
269 }
270 
271 //
272 // Delete the GlobalTreeRoute for this McastForwarder.
273 //
275  if (!global_tree_route_)
276  return;
277 
278  McastManagerPartition *partition = sg_entry_->partition();
279  DBTablePartition *tbl_partition =
280  static_cast<DBTablePartition *>(partition->GetTablePartition());
282 
283  if (!global_tree_route_->HasPaths()) {
284  tbl_partition->Delete(global_tree_route_);
285  } else {
286  tbl_partition->Notify(global_tree_route_);
287  }
288  global_tree_route_ = NULL;
289 }
290 
291 //
292 // Append list of BgpOListElems from the Local tree to the BgpOListSpec. The
293 // list is built based on the tree links in this McastForwarder.
294 //
297 
298  for (McastForwarderList::const_iterator it = tree_links_.begin();
299  it != tree_links_.end(); ++it) {
300  BgpOListElem elem((*it)->address(), (*it)->label(), (*it)->encap());
301  olist_spec->elements.push_back(elem);
302  }
303 }
304 
305 //
306 // Append list of BgpOListElems from the Global tree to the BgpOListSpec. The
307 // list is built based on EdgeForwarding attribute in the GlobalTreeRoute.
308 //
311 
312  // Bail if this is not the forest node for the Local tree.
313  if (!sg_entry_->IsForestNode(this))
314  return;
315 
317  if (!route)
318  return;
319 
320  const BgpPath *path = route->BestPath();
321  if (!path)
322  return;
323  const BgpAttr *attr = path->GetAttr();
324  vector<string> encaps;
325  if (attr && attr->ext_community())
326  encaps = attr->ext_community()->GetTunnelEncap();
327 
328  // Go through each forwarding edge and add it to the list.
329  const EdgeForwarding *eforwarding = path->GetAttr()->edge_forwarding();
330  for (EdgeForwarding::EdgeList::const_iterator it =
331  eforwarding->edge_list.begin(); it != eforwarding->edge_list.end();
332  ++it) {
333  const EdgeForwarding::Edge *edge = *it;
334  if (edge->inbound_address == address_) {
335  BgpOListElem elem(edge->outbound_address, edge->outbound_label,
336  encaps);
337  olist_spec->elements.push_back(elem);
338  }
339  }
340 }
341 
342 //
343 // Construct an UpdateInfo with the RibOutAttr that needs to be advertised to
344 // the IPeer for the ErmVpnRoute associated with this McastForwarder. This is
345 // used as Export method of the ErmVpnTable. It is expected that the caller
346 // fills in the target RibPeerSet in the UpdateInfo.
347 //
348 // The main functionality here is to transform the McastForwarderList for the
349 // distribution tree and the EdgeForwarding attribute from the GlobalTreeRoute
350 // into a BgpOList.
351 //
353  CHECK_CONCURRENCY("db::DBTable");
354 
356 
357  BgpOListSpec olist_spec(BgpAttribute::OList);
358  AddLocalOListElems(&olist_spec);
359  AddGlobalOListElems(&olist_spec);
360 
361  // Bail if there is no label allocated.
362  if (label_ == 0)
363  return NULL;
364 
365  BgpAttrSpec attr_spec;
366  attr_spec.push_back(&olist_spec);
367  BgpAttrPtr attr = table->server()->attr_db()->Locate(attr_spec);
368 
369  UpdateInfo *uinfo = new UpdateInfo;
370  uinfo->roattr = RibOutAttr(table, route_, attr.get(), label_, true, true);
371  if (route_ && sg_entry_->IsForestNode(this) &&
374  }
375  return uinfo;
376 }
377 
378 //
379 // Constructor for McastSGEntry.
380 //
382  Ip4Address group, Ip4Address source)
383  : partition_(partition),
384  group_(group),
385  source_(source),
386  forest_node_(NULL),
387  local_tree_route_(NULL),
388  tree_result_route_(NULL),
389  on_work_queue_(false) {
390  for (int level = McastTreeManager::LevelFirst;
391  level < McastTreeManager::LevelCount; ++level) {
392  ForwarderSet *forwarders = new ForwarderSet;
393  forwarder_sets_.push_back(forwarders);
394  update_needed_.push_back(false);
395  }
396 }
397 
398 //
399 // Destructor for McastSGEntry.
400 //
403 }
404 
405 //
406 // Printable string for McastSGEntry.
407 //
408 std::string McastSGEntry::ToString() const {
409  return group_.to_string() + "," + source_.to_string();
410 }
411 
412 //
413 // Add the given McastForwarder under this McastSGEntry and trigger update
414 // of the distribution tree.
415 //
417  uint8_t level = forwarder->level();
418  forwarder_sets_[level]->insert(forwarder);
419  update_needed_[level] = true;
420  partition_->EnqueueSGEntry(this);
421 }
422 
423 //
424 // Handle change for the given McastForwarder under this McastSGEntry. Trigger
425 // update of the distribution tree.
426 //
427 // Note that this method only handles the change = the caller determines that
428 // there has been a change.
429 //
431  uint8_t level = forwarder->level();
432  update_needed_[level] = true;
433  partition_->EnqueueSGEntry(this);
434 }
435 
436 //
437 // Delete the given McastForwarder from this McastSGEntry and trigger update
438 // of the distribution tree.
439 //
441  if (forwarder == forest_node_)
442  forest_node_ = NULL;
443  uint8_t level = forwarder->level();
444  forwarder_sets_[level]->erase(forwarder);
445  update_needed_[level] = true;
446  partition_->EnqueueSGEntry(this);
447 }
448 
449 //
450 // Get the SourceRD to be used when adding [Local|Global]TreeRoutes. This
451 // SourceRD gets used as the RD when the ErmVpnRoute is replicated from the
452 // VRF table to the VPN table.
453 //
454 // We simply use the RD for the forest node.
455 //
457  if (!forest_node_)
460 }
461 
462 //
463 // Add the LocalTreeRoute for this McastSGEntry. This route advertises a set
464 // of candidate edges from McastForwarders attached to this control-node that
465 // can be used by the tree builder to build the higher level tree. We simply
466 // advertise edges McastTreeManager::kDegree - 1 edges from the forest node.
467 //
468 // We advertise kDegree-1 candidate edges via the EdgeDiscovery attribute. All
469 // the edges are for the forest node for the tree of native McastForwarders.
470 // The label block for each edge in the EdgeDiscovery attribute is of size 1 -
471 // this is label that has been allocated for the forest node. Using a single
472 // label is acceptable because the tree builder algorithm does not change the
473 // relative order of nodes in the tree.
474 //
476  assert(!forest_node_);
477  assert(!local_tree_route_);
478 
479  // Select last usable leaf in the distribution tree as the forest node.
480  // A leaf is considered usable if it has a valid label i.e. it has not
481  // run out of labels.
482  uint8_t level = McastTreeManager::LevelNative;
483  ForwarderSet *forwarders = forwarder_sets_[level];
484  for (ForwarderSet::reverse_iterator rit = forwarders->rbegin();
485  rit != forwarders->rend(); ++rit) {
486  McastForwarder *forwarder = *rit;
487  if (forwarder->label()) {
488  forest_node_ = forwarder;
489  break;
490  }
491  }
492 
493  // Bail if we couldn't designate a forest node.
494  if (!forest_node_)
495  return;
496 
497  // Construct the prefix and route key.
498  BgpServer *server = partition_->server();
499  Ip4Address router_id(server->bgp_identifier());
502  ErmVpnRoute rt_key(prefix);
503 
504  // Find or create the route.
505  DBTablePartition *tbl_partition =
506  static_cast<DBTablePartition *>(partition_->GetTablePartition());
507  ErmVpnRoute *route =
508  static_cast<ErmVpnRoute *>(tbl_partition->Find(&rt_key));
509  if (!route) {
510  route = new ErmVpnRoute(prefix);
511  tbl_partition->Add(route);
512  } else {
513  route->ClearDelete();
514  }
515 
516  // Build the attributes.
517  BgpAttrSpec attr_spec;
518  BgpAttrNextHop nexthop(server->bgp_identifier());
519  attr_spec.push_back(&nexthop);
520  BgpAttrSourceRd source_rd(GetSourceRd());
521  attr_spec.push_back(&source_rd);
522  EdgeDiscoverySpec edspec;
523  for (int idx = 1; idx <= McastTreeManager::kDegree - 1; ++idx) {
527  edspec.edge_list.push_back(edge);
528  }
529  attr_spec.push_back(&edspec);
530  // Add tunnel encaps for remote nodes
531  ExtCommunitySpec ext;
533  if (!ext.communities.empty())
534  attr_spec.push_back(&ext);
535  BgpAttrPtr attr = server->attr_db()->Locate(attr_spec);
536 
537  // Add a path with source BgpPath::Local.
538  BgpPath *path = new BgpPath(0, BgpPath::Local, attr);
539  route->InsertPath(path);
540  tbl_partition->Notify(route);
541  local_tree_route_ = route;
542 }
543 
544 //
545 // Delete the LocalTreeRoute for this McastSGEntry.
546 //
548  if (!local_tree_route_)
549  return;
550 
551  forest_node_ = NULL;
552  DBTablePartition *tbl_partition =
553  static_cast<DBTablePartition *>(partition_->GetTablePartition());
555  if (!local_tree_route_->HasPaths()) {
556  tbl_partition->Delete(local_tree_route_);
557  } else {
558  tbl_partition->Notify(local_tree_route_);
559  }
560  local_tree_route_ = NULL;
561 }
562 
563 //
564 // Update the LocalTreeRoute for this McastSGEntry if RouterId has changed.
565 //
567  if (!local_tree_route_)
568  return;
569 
570  // Bail if the RouterId hasn't changed.
571  const BgpServer *server = partition_->server();
573  if (router_id.to_ulong() == server->bgp_identifier())
574  return;
575 
576  // Add and delete the route.
579 }
580 
581 //
582 // Update relevant [Local|Global]TreeRoutes for the McastSGEntry.
583 //
584 void McastSGEntry::UpdateRoutes(uint8_t level) {
585  if (level == McastTreeManager::LevelNative) {
588  } else {
589  ForwarderSet *forwarders = forwarder_sets_[level];
590  for (ForwarderSet::iterator it = forwarders->begin();
591  it != forwarders->end(); ++it) {
592  (*it)->DeleteGlobalTreeRoute();
593  (*it)->AddGlobalTreeRoute();
594  }
595  }
596 }
597 
600  return NULL;
602  assert(!forwarders->empty());
603  ForwarderSet::const_iterator it = forwarders->begin();
604  return (*it)->global_tree_route();
605 }
606 
607 //
608 // Implement tree builder election.
609 //
610 bool McastSGEntry::IsTreeBuilder(uint8_t level) const {
611  if (level == McastTreeManager::LevelNative)
612  return true;
613 
614  const ForwarderSet *forwarders = forwarder_sets_[level];
615  ForwarderSet::const_iterator it = forwarders->begin();
616  if (it == forwarders->end())
617  return false;
618 
619  Ip4Address router_id(partition_->server()->bgp_identifier());
620  if ((*it)->router_id() != router_id)
621  return false;
622 
623  return true;
624 }
625 
626 //
627 //
628 // Update specified distribution tree for the McastSGEntry. We traverse all
629 // McastForwarders in sorted order and arrange them in breadth first fashion
630 // in a k-ary tree. Building the tree in this manner guarantees that we get
631 // the same tree for a given set of forwarders, independent of the order in
632 // in which they joined. This predictability is deemed to be more important
633 // than other criteria such as minimizing disruption of traffic, minimizing
634 // the cost/weight of the tree etc.
635 //
636 void McastSGEntry::UpdateTree(uint8_t level) {
637  CHECK_CONCURRENCY("db::DBTable");
638 
639  if (!update_needed_[level])
640  return;
641  update_needed_[level] = false;
642 
643  int degree;
644  if (level == McastTreeManager::LevelNative) {
645  degree = McastTreeManager::kDegree;
646  } else {
647  degree = McastTreeManager::kDegree - 1;
648  }
649 
650  // First get rid of the previous distribution tree and enqueue all the
651  // associated ErmVpnRoutes for notification. Note that DBListeners will
652  // not get invoked until after this routine is done.
653  ForwarderSet *forwarders = forwarder_sets_[level];
654  for (ForwarderSet::iterator it = forwarders->begin();
655  it != forwarders->end(); ++it) {
656  (*it)->FlushLinks();
657  (*it)->ReleaseLabel();
658  partition_->GetTablePartition()->Notify((*it)->route());
659  }
660 
661  // Bail if we're not the tree builder.
662  if (!IsTreeBuilder(level)) {
663  UpdateRoutes(level);
664  return;
665  }
666 
667  // Create a vector of pointers to the McastForwarders in sorted order.
668  // We do this because std::set doesn't support random access iterators.
669  // Skip if we can't allocate a label for the McastForwarder.
670  McastForwarderList vec;
671  vec.reserve(forwarders->size());
672  for (ForwarderSet::iterator it = forwarders->begin();
673  it != forwarders->end(); ++it) {
674  McastForwarder *forwarder = *it;
675  forwarder->AllocateLabel();
676  if (!forwarder->label())
677  continue;
678  vec.push_back(forwarder);
679  }
680 
681  // Go through each McastForwarder in the vector and link it to it's parent
682  // McastForwarder in the k-ary tree. We also add a link from the parent to
683  // the entry in question.
684  for (McastForwarderList::iterator it = vec.begin(); it != vec.end(); ++it) {
685  int idx = it - vec.begin();
686  if (idx == 0)
687  continue;
688 
689  int parent_idx = (idx - 1) / degree;
690  McastForwarderList::iterator parent_it = vec.begin() + parent_idx;
691  assert(parent_it != vec.end());
692  McastForwarder *forwarder = *it;
693  McastForwarder *parent_forwarder = *parent_it;
694  forwarder->AddLink(parent_forwarder);
695  parent_forwarder->AddLink(forwarder);
696  }
697 
698  // Update [Local|Global]TreeRoutes.
699  UpdateRoutes(level);
700 }
701 
702 //
703 // Update distribution trees for both levels.
704 //
706  for (uint8_t level = McastTreeManager::LevelFirst;
707  level < McastTreeManager::LevelCount; ++level) {
708  UpdateTree(level);
709  }
710 }
711 
712 //
713 // Trigger notification of the ErmVpnRoute associated with the McastForwarder
714 // that is the forest node. This is used to trigger a rebuild of the BgpOlist
715 // when the GlobalTreeRoute is updated.
716 //
718  if (!forest_node_)
719  return;
721 }
722 
723 bool McastSGEntry::GetForestNodePMSI(uint32_t *label, Ip4Address *address,
724  vector<string> *tunnel_encap) const {
725  if (!forest_node_)
726  return false;
727  *label = forest_node_->label();
728  *address = forest_node_->address();
729  *tunnel_encap = forest_node_->encap();
730  return true;
731 }
732 
734  return (forwarder == forest_node_);
735 }
736 
737 bool McastSGEntry::empty() const {
739  return false;
741  return false;
743  return false;
744  return true;
745 }
746 
747 //
748 // Constructor for McastManagerPartition.
749 //
751  size_t part_id)
752  : tree_manager_(tree_manager),
753  part_id_(part_id),
754  update_count_(0),
755  work_queue_(TaskScheduler::GetInstance()->GetTaskId("db::DBTable"),
756  part_id_,
757  boost::bind(&McastManagerPartition::ProcessSGEntry, this, _1)) {
758 }
759 
760 //
761 // Destructor for McastManagerPartition.
762 //
765 }
766 
767 // Find the McastSGEntry for the given group and source.
769  const Ip4Address &group, const Ip4Address &source) {
770  return const_cast<McastSGEntry *>(
771  static_cast<const McastManagerPartition *>(this)->FindSGEntry(group,
772  source));
773 }
774 
775 //
776 // Find the McastSGEntry for the given group and source.
777 //
779  const Ip4Address &group, const Ip4Address &source) const {
780  McastSGEntry temp_sg_entry(const_cast<McastManagerPartition *>(this),
781  group, source);
782  SGList::const_iterator it = sg_list_.find(&temp_sg_entry);
783  return (it != sg_list_.end() ? *it : NULL);
784 }
785 
786 //
787 // Find or create the McastSGEntry for the given group and source.
788 //
790  Ip4Address group, Ip4Address source) {
791  McastSGEntry *sg_entry = FindSGEntry(group, source);
792  if (!sg_entry) {
793  sg_entry = new McastSGEntry(this, group, source);
794  sg_list_.insert(sg_entry);
795  }
796  return sg_entry;
797 }
798 
800  const Ip4Address &source, const Ip4Address &group) const {
801  const McastSGEntry *sg = FindSGEntry(group, source);
802  return sg ? sg->GetGlobalTreeRootRoute() : NULL;
803 }
804 
806  const Ip4Address &source, const Ip4Address &group) {
807  McastSGEntry *sg = FindSGEntry(group, source);
808  if (sg)
809  sg->NotifyForestNode();
810 }
811 
813  Ip4Address *address, vector<string> *encap) const {
814  const McastSGEntry *sg = FindSGEntry(rt->GetPrefix().group(),
815  rt->GetPrefix().source());
816  return sg ? sg->GetForestNodePMSI(label, address, encap) : false;
817 }
818 
819 //
820 // Enqueue the given McastSGEntry on the WorkQueue if it's not already on it.
821 //
823  if (sg_entry->on_work_queue())
824  return;
825  work_queue_.Enqueue(sg_entry);
826  sg_entry->set_on_work_queue();
827 }
828 
829 //
830 // Callback for the WorkQueue. Updates distribution trees for the McastSGEntry.
831 // Also gets rid of the McastSGEntry if it is eligible to be deleted.
832 //
834  CHECK_CONCURRENCY("db::DBTable");
835 
836  sg_entry->clear_on_work_queue();
837  sg_entry->UpdateTree();
838  update_count_++;
839 
840  if (sg_entry->empty()) {
841  sg_list_.erase(sg_entry);
842  delete sg_entry;
843  }
844 
845  if (sg_list_.empty())
847 
848  return true;
849 }
850 
851 //
852 // Get the DBTablePartBase for the ErmVpnTable for our partition id.
853 //
856 }
857 
859  return tree_manager_->table()->routing_instance();
860 }
861 
863  return tree_manager_->table()->server();
864 }
865 
867  return tree_manager_->table()->server();
868 }
869 
870 //
871 // Constructor for McastTreeManager.
872 //
874  : table_(table),
875  listener_id_(DBTable::kInvalidId),
876  table_delete_ref_(this, table->deleter()) {
877  deleter_.reset(new DeleteActor(this));
878 }
879 
880 //
881 // Destructor for McastTreeManager.
882 //
884 }
885 
886 //
887 // Initialize the McastTreeManager. We allocate the McastManagerPartitions
888 // and register a DBListener for the ErmVpnTable.
889 //
891  AllocPartitions();
893  boost::bind(&McastTreeManager::RouteListener, this, _1, _2),
894  "McastTreeManager");
895 }
896 
897 //
898 // Terminate the McastTreeManager. We free the McastManagerPartitions
899 // and unregister from the ErmVpnTable.
900 //
903  FreePartitions();
904 }
905 
906 //
907 // Allocate the McastManagerPartitions.
908 //
910  for (int part_id = 0; part_id < table_->PartitionCount(); part_id++) {
911  partitions_.push_back(new McastManagerPartition(this, part_id));
912  }
913 }
914 
915 //
916 // Free the McastManagerPartitions.
917 //
919  for (size_t part_id = 0; part_id < partitions_.size(); part_id++) {
920  delete partitions_[part_id];
921  }
922  partitions_.clear();
923 }
924 
926  return partitions_[part_id];
927 }
928 
930  return partitions_[part_id];
931 }
932 
933 //
934 // Get the DBTablePartBase for the ErmVpnTable for given partition id.
935 //
937  return table_->GetTablePartition(part_id);
938 }
939 
940 //
941 // Construct export state for the given ErmVpnRoute. Note that the route
942 // only needs to be exported to the IPeer from which it was learnt.
943 //
945  CHECK_CONCURRENCY("db::DBTable");
946 
947  DBState *dbstate = route->GetState(table_, listener_id_);
948  McastForwarder *forwarder = dynamic_cast<McastForwarder *>(dbstate);
949 
950  if (!forwarder)
951  return NULL;
952 
953  return forwarder->GetUpdateInfo(table_);
954 }
955 
956 //
957 // DBListener callback handler for Native and Local routes in the ErmVpnTable.
958 // It creates, updates or deletes the associated McastForwarder as appropriate.
959 //
960 // Creates a McastSGEntry if one doesn't already exist. However, McastSGEntrys
961 // don't get deleted from here. They only get deleted from WorkQueue callback
962 // routine i.e. McastManagerPartition::ProcessSGEntry.
963 //
965  ErmVpnRoute *route) {
966  CHECK_CONCURRENCY("db::DBTable");
967 
968  DBState *dbstate = route->GetState(table_, listener_id_);
969  if (!dbstate) {
970  // We have no previous DBState for this route.
971  // Bail if the route is not valid.
972  if (!route->IsValid())
973  return;
974 
975  // Create a new McastForwarder and associate it with the route.
976  McastSGEntry *sg_entry = partition->LocateSGEntry(
977  route->GetPrefix().group(), route->GetPrefix().source());
978  McastForwarder *forwarder = new McastForwarder(sg_entry, route);
979  sg_entry->AddForwarder(forwarder);
980  route->SetState(table_, listener_id_, forwarder);
981 
982  // Update local tree route if our RouterId has changed. Ideally,
983  // we should trigger an update of all local trees routes when we
984  // detect a change in RouterId. Instead, we currently check and
985  // update the local route when we detect a new local route from
986  // another node.
987  if (route->GetPrefix().type() == ErmVpnPrefix::LocalTreeRoute)
988  sg_entry->UpdateLocalTreeRoute();
989  } else {
990  McastSGEntry *sg_entry = partition->FindSGEntry(
991  route->GetPrefix().group(), route->GetPrefix().source());
992  assert(sg_entry);
993  McastForwarder *forwarder = dynamic_cast<McastForwarder *>(dbstate);
994  assert(forwarder);
995 
996  if (!route->IsValid()) {
997  // Delete the McastForwarder associated with the route.
998  route->ClearState(table_, listener_id_);
999  sg_entry->DeleteForwarder(forwarder);
1000  delete forwarder;
1001  } else if (forwarder->Update(route)) {
1002  // Trigger update of the distribution tree.
1003  sg_entry->ChangeForwarder(forwarder);
1004  }
1005  }
1006 }
1007 
1008 //
1009 // DBListener callback handler for GlobalTreeRoutes in the ErmVpnTable. It
1010 // updates the tree_result_route_ and triggers re-evaluation of the forest
1011 // node McastForwarder's BgpOlist.
1012 //
1014  ErmVpnRoute *route) {
1015  CHECK_CONCURRENCY("db::DBTable");
1016 
1017  DBState *dbstate = route->GetState(table_, listener_id_);
1018  if (!dbstate) {
1019  // We have no previous DBState for this route.
1020  // Bail if the route is not valid.
1021  if (!route->IsValid())
1022  return;
1023 
1024  // Ignore GlobalTreeRoute if it's not applicable to this control-node.
1025  BgpServer *server = table_->routing_instance()->server();
1026  if (route->GetPrefix().router_id().to_ulong() !=
1027  server->bgp_identifier())
1028  return;
1029 
1030  McastSGEntry *sg_entry = partition->LocateSGEntry(
1031  route->GetPrefix().group(), route->GetPrefix().source());
1032  route->SetState(table_, listener_id_, sg_entry);
1033  sg_entry->set_tree_result_route(route);
1034  sg_entry->NotifyForestNode();
1035  } else {
1036  McastSGEntry *sg_entry = dynamic_cast<McastSGEntry *>(dbstate);
1037  assert(sg_entry);
1038 
1039  if (!route->IsValid()) {
1040  sg_entry->clear_tree_result_route();
1041  route->ClearState(table_, listener_id_);
1042  partition->EnqueueSGEntry(sg_entry);
1043  }
1044  sg_entry->NotifyForestNode();
1045  }
1046 }
1047 
1048 //
1049 // DBListener callback handler for the ErmVpnTable. GlobalTreeRoutes provide
1050 // result information and hence are handled differently than Native and Local
1051 // routes, which result in update of a McastForwarder.
1052 //
1054  DBTablePartBase *tpart, DBEntryBase *db_entry) {
1055  CHECK_CONCURRENCY("db::DBTable");
1056 
1057  McastManagerPartition *partition = partitions_[tpart->index()];
1058  ErmVpnRoute *route = dynamic_cast<ErmVpnRoute *>(db_entry);
1059  if (route->GetPrefix().type() == ErmVpnPrefix::GlobalTreeRoute) {
1060  TreeResultListener(partition, route);
1061  } else {
1062  TreeNodeListener(partition, route);
1063  }
1064 }
1065 
1066 
1067 //
1068 // Check if the McastTreeManager can be deleted. This can happen only if all
1069 // the McastManagerPartitions are empty.
1070 //
1072  CHECK_CONCURRENCY("bgp::Config");
1073 
1074  for (PartitionList::const_iterator it = partitions_.begin();
1075  it != partitions_.end(); ++it) {
1076  if (!(*it)->empty())
1077  return false;
1078  }
1079 
1080  return true;
1081 }
1082 
1083 //
1084 // Initiate shutdown for the McastTreeManager.
1085 //
1087  CHECK_CONCURRENCY("bgp::Config");
1088 }
1089 
1090 //
1091 // Trigger deletion of the McastTreeManager and propagate the delete to any
1092 // dependents.
1093 //
1095  deleter_->Delete();
1096 }
1097 
1098 //
1099 // Attempt to enqueue a delete for the McastTreeManager.
1100 //
1102  if (!deleter()->IsDeleted())
1103  return;
1104  deleter()->RetryDelete();
1105 }
1106 
1107 //
1108 // Return the LifetimeActor for the McastTreeManager.
1109 //
1111  return deleter_.get();
1112 }
1113 
1114 //
1115 // Return the LifetimeActor for the McastTreeManager.
1116 // Const version.
1117 //
1119  return deleter_.get();
1120 }
1121 
1122 //
1123 // Return true if the McastTreeManager is deleted.
1124 //
1126  return deleter_->IsDeleted();
1127 }
1128 
1130  const Ip4Address &source, const Ip4Address &group) const {
1131  const McastManagerPartition *partition = GetPartition(table_->Hash(group));
1132  return partition->GetGlobalTreeRootRoute(source, group);
1133 }
1134 
1135 void McastTreeManager::NotifyForestNode(int part_id, const Ip4Address &source,
1136  const Ip4Address &group) {
1137  McastManagerPartition *partition = GetPartition(part_id);
1138  partition->NotifyForestNode(source, group);
1139 }
1140 
1142  Ip4Address *address, vector<string> *encap) const {
1143  if (!rt || !rt->IsUsable())
1144  return false;
1145  const McastManagerPartition *partition =
1147  return partition->GetForestNodePMSI(rt, label, address, encap);
1148 }
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
std::vector< McastForwarder * > McastForwarderList
Definition: bgp_multicast.h:33
const EdgeForwarding * edge_forwarding() const
Definition: bgp_attr.h:927
LabelBlockPtr label_block() const
Definition: bgp_attr.h:930
const IpAddress & nexthop() const
Definition: bgp_attr.h:889
const EdgeDiscovery * edge_discovery() const
Definition: bgp_attr.h:924
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:918
TypePtr Locate(Type *attr)
@ Local
Definition: bgp_path.h:44
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
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
bool IsUsable() const
Definition: bgp_route.cc:326
void InsertPath(BgpPath *path)
Definition: bgp_route.cc:62
bool HasPaths() const
Definition: bgp_route.h:29
uint32_t bgp_identifier() const
Definition: bgp_server.h:216
BgpAttrDB * attr_db()
Definition: bgp_server.h:188
BgpServer * server()
Definition: bgp_table.cc:88
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
DBTableBase * get_table() const
Definition: db_entry.cc:120
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
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:208
void Unregister(ListenerId listener)
Definition: db_table.cc:213
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
EdgeList edge_list
Definition: bgp_attr.h:494
EdgeList edge_list
Definition: bgp_attr.h:601
Ip4Address source() const
Definition: ermvpn_route.h:61
uint8_t type() const
Definition: ermvpn_route.h:57
Ip4Address router_id() const
Definition: ermvpn_route.h:59
const RouteDistinguisher & route_distinguisher() const
Definition: ermvpn_route.h:58
Ip4Address group() const
Definition: ermvpn_route.h:60
virtual bool IsValid() const
const ErmVpnPrefix & GetPrefix() const
Definition: ermvpn_route.h:82
void GetMvpnSourceAddress(ErmVpnRoute *ermvpn_route, Ip4Address *address) const
virtual int PartitionCount() const
Definition: ermvpn_table.h:45
virtual size_t Hash(const DBEntry *entry) const
Definition: ermvpn_table.cc:42
void AddTunnelEncaps(std::vector< std::string > encaps)
Definition: community.cc:283
std::vector< uint64_t > communities
Definition: community.h:146
std::vector< std::string > GetTunnelEncap() const
Definition: community.cc:665
void RetryDelete()
Definition: lifetime.cc:74
void RemoveLink(McastForwarder *forwarder)
void AddGlobalOListElems(BgpOListSpec *olist_spec)
RouteDistinguisher rd_
ErmVpnRoute * route_
void AddGlobalTreeRoute()
void DeleteGlobalTreeRoute()
McastForwarder(McastSGEntry *sg_entry, ErmVpnRoute *route)
void AddLink(McastForwarder *forwarder)
LabelBlockPtr label_block_
ErmVpnRoute * route()
Ip4Address address() const
Ip4Address address_
McastForwarder * FindLink(McastForwarder *forwarder)
uint8_t level() const
std::vector< std::string > encap() const
ErmVpnRoute * global_tree_route_
McastSGEntry * sg_entry_
McastForwarderList tree_links_
void AddLocalOListElems(BgpOListSpec *olist_spec)
std::vector< std::string > encap_
uint32_t label() const
Ip4Address router_id_
bool Update(ErmVpnRoute *route)
std::string ToString() const
UpdateInfo * GetUpdateInfo(ErmVpnTable *table)
WorkQueue< McastSGEntry * > work_queue_
McastManagerPartition(McastTreeManager *tree_manager, size_t part_id)
bool GetForestNodePMSI(ErmVpnRoute *rt, uint32_t *label, Ip4Address *address, std::vector< std::string > *encap) const
ErmVpnRoute * GetGlobalTreeRootRoute(const Ip4Address &source, const Ip4Address &group) const
void EnqueueSGEntry(McastSGEntry *sg_entry)
bool ProcessSGEntry(McastSGEntry *sg_entry)
McastSGEntry * LocateSGEntry(Ip4Address group, Ip4Address source)
DBTablePartBase * GetTablePartition()
void NotifyForestNode(const Ip4Address &source, const Ip4Address &group)
const RoutingInstance * routing_instance() const
McastSGEntry * FindSGEntry(const Ip4Address &group, const Ip4Address &source)
McastTreeManager * tree_manager_
void AddForwarder(McastForwarder *forwarder)
void NotifyForestNode()
std::vector< ForwarderSet * > forwarder_sets_
McastForwarder * forest_node_
Ip4Address group_
void set_tree_result_route(ErmVpnRoute *route)
void set_on_work_queue()
Ip4Address source() const
void AddLocalTreeRoute()
std::string ToString() const
std::set< McastForwarder *, McastForwarderCompare > ForwarderSet
McastSGEntry(McastManagerPartition *partition, Ip4Address group, Ip4Address source)
void clear_on_work_queue()
ErmVpnRoute * local_tree_route_
Ip4Address source_
std::vector< bool > update_needed_
void UpdateLocalTreeRoute()
bool IsForestNode(McastForwarder *forwarder)
void DeleteLocalTreeRoute()
bool GetForestNodePMSI(uint32_t *label, Ip4Address *address, std::vector< std::string > *encap) const
void clear_tree_result_route()
bool on_work_queue()
bool empty() const
void DeleteForwarder(McastForwarder *forwarder)
McastManagerPartition * partition_
Ip4Address group() const
const ErmVpnRoute * tree_result_route() const
void UpdateRoutes(uint8_t level)
const RouteDistinguisher & GetSourceRd() const
bool IsTreeBuilder(uint8_t level) const
void ChangeForwarder(McastForwarder *forwarder)
ErmVpnRoute * tree_result_route_
McastManagerPartition * partition()
ErmVpnRoute * GetGlobalTreeRootRoute() const
virtual bool MayDelete() const
McastTreeManager * tree_manager_
DeleteActor(McastTreeManager *tree_manager)
McastManagerPartition * GetPartition(int part_id)
ErmVpnTable * table()
void RouteListener(DBTablePartBase *tpart, DBEntryBase *db_entry)
DBTablePartBase * GetTablePartition(size_t part_id)
void TreeResultListener(McastManagerPartition *partition, ErmVpnRoute *route)
PartitionList partitions_
LifetimeActor * deleter()
void NotifyForestNode(int part_id, const Ip4Address &source, const Ip4Address &group)
void TreeNodeListener(McastManagerPartition *partition, ErmVpnRoute *route)
bool deleted() const
ErmVpnTable * table_
virtual bool GetForestNodePMSI(ErmVpnRoute *rt, uint32_t *label, Ip4Address *address, std::vector< std::string > *encap) const
static const int kDegree
McastTreeManager(ErmVpnTable *table)
virtual UpdateInfo * GetUpdateInfo(ErmVpnRoute *route)
virtual void Initialize()
virtual ErmVpnRoute * GetGlobalTreeRootRoute(const Ip4Address &source, const Ip4Address &group) const
virtual ~McastTreeManager()
boost::scoped_ptr< DeleteActor > deleter_
virtual void Terminate()
bool MayDelete() const
const Ip4Address & source_address() const
Definition: bgp_ribout.h:123
bool IsZero() const
Definition: rd.h:44
static RouteDistinguisher kZeroRd
Definition: rd.h:15
std::string ToString() const
Definition: rd.cc:57
BgpServer * server()
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
Elements elements
Definition: bgp_attr.h:695
void SetLabels(uint32_t first_label, uint32_t last_label)
Definition: bgp_attr.cc:463
void SetIp4Address(Ip4Address addr)
Definition: bgp_attr.cc:451
EdgeList edge_list
Definition: bgp_attr.h:460
LabelBlockPtr label_block
Definition: bgp_attr.h:482
Ip4Address address
Definition: bgp_attr.h:481
void SetOutboundIp4Address(Ip4Address addr)
Definition: bgp_attr.cc:610
void SetInboundIp4Address(Ip4Address addr)
Definition: bgp_attr.cc:603
EdgeList edge_list
Definition: bgp_attr.h:567
uint32_t outbound_label
Definition: bgp_attr.h:590
Ip4Address outbound_address
Definition: bgp_attr.h:589
Ip4Address inbound_address
Definition: bgp_attr.h:589
RibOutAttr roattr
Definition: bgp_update.h:101
#define CHECK_CONCURRENCY(...)
void STLDeleteValues(Container *container)
Definition: util.h:102