OpenSDN source code
service_chaining.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/foreach.hpp>
9 #include <boost/assign/list_of.hpp>
10 
11 #include <algorithm>
12 
13 #include "base/task_annotations.h"
14 #include "base/task_trigger.h"
15 #include "bgp/bgp_config.h"
16 #include "bgp/bgp_log.h"
17 #include "bgp/bgp_membership.h"
18 #include "bgp/bgp_server.h"
25 #include "bgp/routing-instance/service_chaining_types.h"
27 #include "net/community_type.h"
28 
29 using boost::bind;
30 using boost::system::error_code;
31 using std::make_pair;
32 using std::sort;
33 using std::string;
34 using std::vector;
35 
36 template<>
38 template<>
40 template<>
42 template<>
44 
45 static int GetOriginVnIndex(const BgpTable *table, const BgpRoute *route) {
46  const BgpPath *path = route->BestPath();
47  if (!path)
48  return 0;
49 
50  const BgpAttr *attr = path->GetAttr();
51  const ExtCommunity *ext_community = attr->ext_community();
52  if (ext_community) {
53  BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
54  ext_community->communities()) {
55  if (!ExtCommunity::is_origin_vn(comm))
56  continue;
57  OriginVn origin_vn(comm);
58  return origin_vn.vn_index();
59  }
60  }
61  if (path->IsVrfOriginated())
62  return table->routing_instance()->virtual_network_index();
63  return 0;
64 }
65 
86 template<typename T>
88  BgpRoute *&route, BgpTable *&table, PrefixT prefix, bool create) {
94  RoutingInstance *src_ri = src_routing_instance();
95  IpAddress addr = prefix.addr();
96  int plen = prefix.prefixlen();
97  if (GetSCFamily() == SCAddress::EVPN) {
101  table = src_ri->GetTable(Address::INET);
102  Ip4Prefix inet_prefix = Ip4Prefix(addr.to_v4(), plen);
103  InetRoute inet_route(inet_prefix);
104  partition = static_cast<DBTablePartition *>
105  (table->GetTablePartition(&inet_route));
106  route = static_cast<BgpRoute *>(partition->Find(&inet_route));
107  if (create) {
111  if (route == NULL) {
112  route = new InetRoute(inet_prefix);
113  partition->Add(route);
114  } else {
115  route->ClearDelete();
116  }
117  }
118  } else if (GetSCFamily() == SCAddress::EVPN6) {
122  table = src_ri->GetTable(Address::INET6);
123  Inet6Prefix inet6_prefix = Inet6Prefix(addr.to_v6(), plen);
124  Inet6Route inet6_route(inet6_prefix);
125  partition = static_cast<DBTablePartition *>
126  (table->GetTablePartition(&inet6_route));
127  route = static_cast<BgpRoute *>(partition->Find(&inet6_route));
128  if (create) {
132  if (route == NULL) {
133  route = new Inet6Route(inet6_prefix);
134  partition->Add(route);
135  } else {
136  route->ClearDelete();
137  }
138  }
139  } else {
144  table = src_ri->GetTable(Address::EVPN);
145  string type_rd_tag("5-0:0-0-");
146  string prefix_str = type_rd_tag + addr.to_string() + "/" +
147  boost::lexical_cast<std::string>(plen);
148  EvpnPrefix evpn_prefix(EvpnPrefix::FromString(prefix_str));
149  EvpnRoute evpn_route(evpn_prefix);
150  partition = static_cast<DBTablePartition *>
151  (table->GetTablePartition(&evpn_route));
152  route = static_cast<BgpRoute *>(partition->Find(&evpn_route));
153  if (create) {
157  if (route == NULL) {
158  route = new EvpnRoute(evpn_prefix);
159  partition->Add(route);
160  } else {
161  route->ClearDelete();
162  }
163  }
164  }
165 }
166 
179 template<typename T>
181  BgpAttrPtr attr, BgpRoute *&route, DBTablePartition *&partition,
182  bool aggregate, BgpTable *bgptable) {
183  BgpPath *existing_path =
184  route->FindPath(BgpPath::ServiceChain, NULL, path_id);
185  uint32_t label = path->GetLabel();
186  bool path_updated = false;
187 
192  if (bgptable->family() == Address::EVPN) {
193  const RoutingInstance *conn_ri =
196  connected_->name()));
197  if (!conn_ri) {
198  // conn_ri is not expected to be found only in unit tests.
199  assert(bgp_log_test::unit_test());
200  conn_ri = connected_routing_instance();
201  }
202  label = conn_ri->vxlan_id();
203  if (!label) {
204  label = conn_ri->virtual_network_index();
205  }
206  }
207 
211  if (existing_path != NULL) {
212  // Existing path can be reused.
213  if ((attr.get() == existing_path->GetAttr()) &&
214  (path->GetLabel() == existing_path->GetLabel()) &&
215  (path->GetFlags() == existing_path->GetFlags())) {
216  return;
217  }
218 
222  path_updated = true;
223  route->RemovePath(BgpPath::ServiceChain, NULL, path_id);
224  }
225 
230  BgpPath *new_path =
231  new BgpPath(path_id, BgpPath::ServiceChain, attr.get(),
232  path->GetFlags(), label);
233  route->InsertPath(new_path);
234  partition->Notify(route);
235 
236  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
237  (path_updated ? "Updated " : "Added ") <<
238  (aggregate ? "Aggregate" : "ExtConnected") <<
239  " ServiceChain path " << route->ToString() <<
240  " path_id " << BgpPath::PathIdString(path_id) <<
241  " in table " << bgptable->name() <<
242  " .Path label: " << label);
243 }
244 
245 template <typename T>
247 public:
248  explicit DeleteActor(ServiceChainMgr<T> *manager)
249  : LifetimeActor(manager->server_->lifetime_manager()),
250  manager_(manager) {
251  }
252  virtual bool MayDelete() const {
253  return manager_->MayDelete();
254  }
255  virtual void Shutdown() {
256  }
257  virtual void Destroy() {
258  manager_->Terminate();
259  }
260 
261 private:
263 };
264 
265 template <typename T>
268  RoutingInstance *connected, const vector<string> &subnets, AddressT addr,
269  bool head, bool retain_as_path)
270  : manager_(manager),
271  group_(group),
272  src_(src),
273  dest_(dest),
274  connected_(connected),
275  connected_route_(NULL),
276  service_chain_addr_(addr),
277  group_oper_state_up_(group ? false : true),
278  connected_table_unregistered_(false),
279  dest_table_unregistered_(false),
280  aggregate_(false),
281  sc_head_(head),
282  retain_as_path_(retain_as_path),
283  src_table_delete_ref_(this, src_table()->deleter()),
284  dest_table_delete_ref_(this, dest_table()->deleter()),
285  connected_table_delete_ref_(this, connected_table()->deleter()) {
286  for (vector<string>::const_iterator it = subnets.begin();
287  it != subnets.end(); ++it) {
288  string prefix = *it;
289  error_code ec;
294  if (GetFamily() == Address::EVPN) {
295  prefix = "5-0:0-0-" + prefix;
296  EvpnPrefix subnet = EvpnPrefix::FromString(prefix, &ec);
297  if (GetSCFamily() == SCAddress::EVPN) {
298  if (subnet.family() == Address::INET6) {
299  continue;
300  }
301  } else {
302  if (subnet.family() != Address::INET6) {
303  continue;
304  }
305  }
306  }
307  PrefixT ipam_subnet = PrefixT::FromString(prefix, &ec);
308  if (ec.failed())
309  continue;
310  prefix_to_routelist_map_[ipam_subnet] = RouteList();
311  }
312 }
313 
314 template <typename T>
316  return src_->GetTable(GetFamily());
317 }
318 
319 template <typename T>
321  return connected_->GetTable(GetConnectedFamily());
322 }
323 
324 template <typename T>
326  return dest_->GetTable(GetFamily());
327 }
328 
329 //
330 // Compare this ServiceChain against the ServiceChainConfig.
331 // Return true if the configuration has not changed, false otherwise.
332 //
333 template <typename T>
335  const ServiceChainConfig &config) {
336  if (deleted())
337  return false;
338  if (dest_->name() != config.routing_instance)
339  return false;
340  if (connected_->name() != config.source_routing_instance)
341  return false;
342  if (service_chain_addr_.to_string() != config.service_chain_address)
343  return false;
344  if (!group_ && !config.service_chain_id.empty())
345  return false;
346  if (group_ && config.service_chain_id.empty())
347  return false;
348  if (group_ && group_->name() != config.service_chain_id)
349  return false;
350 
351  if (prefix_to_routelist_map_.size() != config.prefix.size())
352  return false;
353  for (vector<string>::const_iterator it = config.prefix.begin();
354  it != config.prefix.end(); ++it) {
355  error_code ec;
356  PrefixT ipam_subnet = PrefixT::FromString(*it, &ec);
357  if (prefix_to_routelist_map_.find(ipam_subnet) ==
358  prefix_to_routelist_map_.end()) {
359  return false;
360  }
361  }
362  return true;
363 }
364 
365 //
366 // Match function called from BgpConditionListener
367 // Concurrency : db::DBTable
368 // For the purpose of route aggregation, two condition needs to be matched
369 // 1. More specific route present in any of the Dest BgpTable partition
370 // 2. Connected route(for nexthop) present in Src BgpTable
371 //
372 template <typename T>
373 bool ServiceChain<T>::Match(BgpServer *server, BgpTable *table, BgpRoute *route,
374  bool deleted) {
375  CHECK_CONCURRENCY("db::DBTable");
376 
378  PrefixT aggregate_match;
379 
380  if (table == dest_table() && !dest_table_unregistered()) {
381  // For EVPN service-chaining, we are only interested in Type 5 routes
382  // from the destination table. Ignore any other route.
383  if (GetFamily() == Address::EVPN) {
384  if (!IsEvpnType5Route(route)) {
385  return (false);
386  }
387  }
388 
389  // Skip connected routes
390  if (IsConnectedRoute(route)) {
391  return false;
392  }
393 
394  // Skip aggregate routes
395  if (aggregate_enable() && IsAggregate(route))
396  return false;
397 
398  if (aggregate_enable() && IsMoreSpecific(route, &aggregate_match)) {
399  // More specific
400  if (deleted) {
401  type = ServiceChainRequestT::MORE_SPECIFIC_DELETE;
402  } else {
403  type = ServiceChainRequestT::MORE_SPECIFIC_ADD_CHG;
404  }
405  } else {
406  // External connecting routes
407  if (!deleted) {
408  if (!route->BestPath() || !route->BestPath()->IsFeasible()) {
409  deleted = true;
410  } else {
411  const BgpAttr *attr = route->BestPath()->GetAttr();
412  const Community *comm = attr ? attr->community() : NULL;
413  if (comm) {
416  deleted = true;
417  }
418 
419  int vn_index = GetOriginVnIndex(table, route);
420  int src_vn_index = src_->virtual_network_index();
421  int dest_vn_index = dest_->virtual_network_index();
422  if (!vn_index || dest_vn_index != vn_index) {
423  if (src_vn_index == vn_index)
424  deleted = true;
425  if (!dest_->virtual_network_allow_transit())
426  deleted = true;
427  if (!dest_vn_index)
428  deleted = true;
429  }
430 
431  const OriginVnPath *ovnpath =
432  attr ? attr->origin_vn_path() : NULL;
433  if (ovnpath && ovnpath->Contains(
434  server->autonomous_system(), src_vn_index)) {
435  deleted = true;
436  }
437  }
438  }
439 
440  if (deleted) {
441  type = ServiceChainRequestT::EXT_CONNECT_ROUTE_DELETE;
442  } else {
443  type = ServiceChainRequestT::EXT_CONNECT_ROUTE_ADD_CHG;
444  }
445  }
446  } else if ((table == connected_table()) &&
447  !connected_table_unregistered() &&
448  IsConnectedRoute(route, true)) {
449  // Connected routes from source table
450  if (!deleted) {
451  if (!route->IsValid() ||
452  route->BestPath()->GetSource() != BgpPath::BGP_XMPP) {
453  deleted = true;
454  }
455  }
456 
457  // Connected route for service chain
458  if (deleted) {
459  type = ServiceChainRequestT::CONNECTED_ROUTE_DELETE;
460  } else {
461  type = ServiceChainRequestT::CONNECTED_ROUTE_ADD_CHG;
462  }
463  } else {
464  return false;
465  }
466 
467  BgpConditionListener *listener = manager_->GetListener();
468  ServiceChainState *state = static_cast<ServiceChainState *>(
469  listener->GetMatchState(table, route, this));
470  if (!deleted) {
471  // MatchState is added to the Route to ensure that DBEntry is not
472  // deleted before the ServiceChain module processes the WorkQueue
473  // request.
474  if (!state) {
475  state = new ServiceChainState(ServiceChainPtr(this));
476  listener->SetMatchState(table, route, this, state);
477  }
478  } else {
479  // MatchState is set on all the Routes that matches the conditions
480  // Retrieve to check and ignore delete of unseen Add Match
481  if (state == NULL) {
482  // Not seen ADD ignore DELETE
483  return false;
484  }
485  }
486 
487  // The MatchState reference is taken to ensure that the route is not
488  // deleted when request is still present in the queue
489  // This is to handle the case where MatchState already exists and
490  // deleted entry gets reused or reused entry gets deleted.
491  state->IncrementRefCnt();
492 
493  // Post the Match result to ServiceChain task to take Action
494  // More_Specific_Present + Connected_Route_exists ==> Add Aggregate Route
495  // and stitch the nexthop from connected route
497  type, table, route, aggregate_match, ServiceChainPtr(this));
498  manager_->Enqueue(req);
499  return true;
500 }
501 
502 template <typename T>
504  return (string("ServiceChain " ) + service_chain_addr_.to_string());
505 }
506 
507 template <typename T>
509  connected_route_ = connected;
510  connected_path_ids_.clear();
511  if (!connected_route_)
512  return;
513 
514  for (Route::PathList::iterator it = connected->GetPathList().begin();
515  it != connected->GetPathList().end(); ++it) {
516  BgpPath *path = static_cast<BgpPath *>(it.operator->());
517 
518  // Infeasible paths are not considered.
519  if (!path->IsFeasible())
520  break;
521 
522  // Bail if it's not ECMP with the best path.
523  if (connected_route_->BestPath()->PathCompare(*path, true))
524  break;
525 
526  // Use nexthop attribute of connected path as path id.
527  uint32_t path_id = path->GetAttr()->nexthop().to_v4().to_ulong();
528  connected_path_ids_.insert(path_id);
529  }
530 }
531 
532 template <typename T>
534  return (connected_route_ && connected_route_->IsValid());
535 }
536 
537 template <typename T>
539  PrefixT *aggregate_match) const {
540  const RouteT *ip_route = static_cast<RouteT *>(route);
541  const PrefixT &ip_prefix = ip_route->GetPrefix();
542  for (typename PrefixToRouteListMap::const_iterator it =
543  prefix_to_route_list_map()->begin();
544  it != prefix_to_route_list_map()->end(); ++it) {
545  if (ip_prefix.IsMoreSpecific(it->first)) {
546  *aggregate_match = it->first;
547  return true;
548  }
549  }
550  return false;
551 }
552 
553 template <typename T>
555  RouteT *ip_route = dynamic_cast<RouteT *>(route);
556  for (typename PrefixToRouteListMap::const_iterator it =
557  prefix_to_route_list_map()->begin();
558  it != prefix_to_route_list_map()->end(); ++it) {
559  if (it->first == ip_route->GetPrefix())
560  return true;
561  }
562  return false;
563 }
564 
570 template <typename T>
572  bool is_conn_table) const {
573  if (is_conn_table && GetFamily() == Address::EVPN) {
574  if (GetConnectedFamily() == Address::INET) {
575  InetRoute *inet_route = dynamic_cast<InetRoute *>(route);
576  return (service_chain_addr() == inet_route->GetPrefix().addr());
577  } else {
578  Inet6Route *inet6_route = dynamic_cast<Inet6Route *>(route);
579  return (service_chain_addr() == inet6_route->GetPrefix().addr());
580  }
581  } else {
585  RouteT *ip_route = dynamic_cast<RouteT *>(route);
586  return (service_chain_addr() == ip_route->GetPrefix().addr());
587  }
588 }
589 
596 template <typename T>
598  if (GetFamily() != Address::EVPN) {
599  return false;
600  }
601 
602  EvpnRoute *evpn_route = static_cast<EvpnRoute *>(route);
603  EvpnPrefix prefix = evpn_route->GetPrefix();
604  if (prefix.type() != EvpnPrefix::IpPrefixRoute) {
605  return false;
606  }
607  if (GetSCFamily() == SCAddress::EVPN &&
608  prefix.family() == Address::INET6) {
609  return false;
610  }
611  if (GetSCFamily() == SCAddress::EVPN6 &&
612  prefix.family() == Address::INET) {
613  return false;
614  }
615  return true;
616 }
617 
618 template <typename T>
620  ServiceChainState *state) {
621  if (deleted() || route->IsDeleted()) {
622  // At this point we are ready to release the MatchState on the DBEntry
623  // So mark it as deleted.. Actual removal of the state is done when
624  // ref count is 0
625  state->set_deleted();
626  }
627 }
628 
629 /*
630  * To support BMS to VM service-chaining, we could have traffic being
631  * chained between different address-families. This entails the need for
632  * replicating the service-chain route across address-families.
633  * The different possibilities are listed below.
634  * ---------------------------------------------------
635  * | service-chain info |
636  * ----------------------------------------------------------------------
637  * Traffic direction | Destination AF Source AF Replication Table |
638  * ----------------------------------------------------------------------
639  * VM(v4) --> BMS | EVPN INET InetTable |
640  * VM(v6) --> BMS | EVPN INET6 Inet6Table |
641  * BMS --> VM (v4) | INET INET EvpnTable |
642  * BMS --> VM (v6) | INET6 INET6 EvpnTable |
643  * ----------------------------------------------------------------------
644  *
645  * This is only done at the RI belonging to the head SI in the service
646  * chain. At the RIs belonging to other SIs in the chain, we always
647  * install the route only in the INET or INET6 table and not in the EVPN
648  * table. This is because, at the RI belonging to the first SI in the
649  * service-chain, we may need to originate a Type 5 route if a BMS happens
650  * to be connected to it.
651  */
652 template <typename T>
653 void ServiceChain<T>::DeleteServiceChainRoute(PrefixT prefix, bool aggregate) {
654 
655  CHECK_CONCURRENCY("bgp::ServiceChain");
656 
657  /*
658  * For deletion within the same AF.
659  * At the RIs belonging to SIs NOT at the head of the service-chain, do
660  * not need to delete EVPN SC routes since only INET or INET6 routes
661  * would have been installed.
662  */
663  if (is_sc_head() || GetFamily() != Address::EVPN) {
664  BgpTable *bgptable = src_table();
665  RouteT rt_key(prefix);
666  DBTablePartition *partition =
667  static_cast<DBTablePartition *>(bgptable->
668  GetTablePartition(&rt_key));
669  BgpRoute *service_chain_route =
670  static_cast<BgpRoute *>(partition->Find(&rt_key));
671 
672  if (service_chain_route && !service_chain_route->IsDeleted()) {
673  DeleteServiceChainRouteInternal(service_chain_route, partition,
674  bgptable, aggregate);
675  }
676  }
677 
678  /*
679  * For deletion from replication table.
680  * At the RIs belonging to SIs NOT at the head of the service-chain, do
681  * not need to delete SC routes in the EVPN table since they would have
682  * been installed only in the INET or INET6 table..
683  */
684  if (is_sc_head() || GetFamily() == Address::EVPN) {
685  BgpTable *repl_table;
686  BgpRoute *repl_sc_route;
687  DBTablePartition *repl_partition;
688  GetReplicationFamilyInfo(repl_partition, repl_sc_route, repl_table,
689  prefix, false);
690  if (repl_sc_route && !repl_sc_route->IsDeleted()) {
691  DeleteServiceChainRouteInternal(repl_sc_route, repl_partition,
692  repl_table, aggregate);
693  }
694  }
695 }
696 
697 template <typename T>
699  BgpRoute *service_chain_route,
700  DBTablePartition *partition,
701  BgpTable *bgptable,
702  bool aggregate) {
703  CHECK_CONCURRENCY("bgp::ServiceChain");
704 
705  for (ConnectedPathIdList::const_iterator it = GetConnectedPathIds().begin();
706  it != GetConnectedPathIds().end(); ++it) {
707  uint32_t path_id = *it;
708  service_chain_route->RemovePath(BgpPath::ServiceChain, NULL, path_id);
709  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
710  "Removed " << (aggregate ? "Aggregate" : "ExtConnected") <<
711  " ServiceChain path " << service_chain_route->ToString() <<
712  " path_id " << BgpPath::PathIdString(path_id) <<
713  " in table " << bgptable->name());
714  }
715 
716  if (!service_chain_route->HasPaths()) {
717  partition->Delete(service_chain_route);
718  } else {
719  partition->Notify(service_chain_route);
720  }
721 }
722 
723 /*
724  * To support BMS to VM service-chaining, we could have traffic being
725  * chained between different address-families. This entails the need for
726  * replicating the service-chain route across address-families.
727  * The different possibilities are listed below.
728  * ---------------------------------------------------
729  * | service-chain info |
730  * ----------------------------------------------------------------------
731  * Traffic direction | Destination AF Source AF Replication Table |
732  * ----------------------------------------------------------------------
733  * VM(v4) --> BMS | EVPN INET InetTable |
734  * VM(v6) --> BMS | EVPN INET6 Inet6Table |
735  * BMS --> VM (v4) | INET INET EvpnTable |
736  * BMS --> VM (v6) | INET6 INET6 EvpnTable |
737  * ----------------------------------------------------------------------
738  *
739  * This is only done at the RI belonging to the head SI in the service
740  * chain. At the RIs belonging to other SIs in the chain, we always
741  * install the route only in the INET or INET6 table and not in the EVPN
742  * table. This is because, at the RI belonging to the first SI in the
743  * service-chain, we may need to originate a Type 5 route if a BMS happens
744  * to be connected to it.
745  */
746 template <typename T>
748  const RouteT *orig_route, const ConnectedPathIdList &old_path_ids,
749  bool aggregate) {
750 
751  CHECK_CONCURRENCY("bgp::ServiceChain");
752 
753  /*
754  * For re-origination within the same AF.
755  * At the RIs belonging to SIs NOT at the head of the service-chain, do
756  * not install EVPN SC routes. Only install INET or INET6 routes.
757  */
758  if (is_sc_head() || GetFamily() != Address::EVPN) {
759  BgpTable *bgptable = src_table();
760  RouteT rt_key(prefix);
761  DBTablePartition *partition =
762  static_cast<DBTablePartition *>
763  (bgptable->GetTablePartition(&rt_key));
764  BgpRoute *service_chain_route =
765  static_cast<BgpRoute *>(partition->Find(&rt_key));
766 
767  if (service_chain_route == NULL) {
768  service_chain_route = new RouteT(prefix);
769  partition->Add(service_chain_route);
770  } else {
771  service_chain_route->ClearDelete();
772  }
773 
774  UpdateServiceChainRouteInternal(orig_route, old_path_ids,
775  service_chain_route, partition,
776  bgptable, aggregate);
777  }
778 
779  /*
780  * For re-origination to replication table.
781  * At the RIs belonging to SIs NOT at the head of the service-chain, do
782  * not install SC routes in the EVPN table. Only install INET or INET6
783  * routes.
784  */
785  if (is_sc_head() || GetFamily() == Address::EVPN) {
786  BgpTable *repl_table;
787  BgpRoute *repl_sc_route;
788  DBTablePartition *repl_partition;
789  GetReplicationFamilyInfo(repl_partition, repl_sc_route, repl_table,
790  prefix, true);
791  UpdateServiceChainRouteInternal(orig_route, old_path_ids,
792  repl_sc_route, repl_partition,
793  repl_table, aggregate);
794  }
795 }
796 
797 template <typename T>
799  const ConnectedPathIdList &old_path_ids, BgpRoute *service_chain_route,
800  DBTablePartition *partition, BgpTable *bgptable, bool aggregate) {
801  CHECK_CONCURRENCY("bgp::ServiceChain");
802 
803  int vn_index = dest_routing_instance()->virtual_network_index();
804  BgpServer *server = dest_routing_instance()->server();
805  OriginVn origin_vn(server->autonomous_system(), vn_index);
806  OriginVn origin_vn4(server->autonomous_system(), AS_TRANS);
807  OriginVn origin_vn_trans(AS_TRANS, vn_index);
808  const OriginVnPath::OriginVnValue origin_vn_bytes = origin_vn.GetExtCommunity();
809  const OriginVnPath::OriginVnValue origin_vn_trans_bytes =
810  origin_vn_trans.GetExtCommunity();
811  const OriginVnPath::OriginVnValue origin_vn4_bytes =
812  origin_vn4.GetExtCommunity();
813 
814  SiteOfOrigin soo;
817  LoadBalance load_balance;
818  bool load_balance_present = false;
819  const Community *orig_community = nullptr;
820  const OriginVnPath *orig_ovnpath = nullptr;
821  const AsPath *orig_aspath = nullptr;
822  RouteDistinguisher orig_rd;
823  if (orig_route) {
824  const BgpPath *orig_path = orig_route->BestPath();
825  const BgpAttr *orig_attr = nullptr;
826  const ExtCommunity *ext_community = nullptr;
827  const LargeCommunity *large_community = nullptr;
828  if (orig_path != nullptr)
829  orig_attr = orig_path->GetAttr();
830  if (orig_attr != nullptr) {
831  orig_community = orig_attr->community();
832  ext_community = orig_attr->ext_community();
833  large_community = orig_attr->large_community();
834  orig_ovnpath = orig_attr->origin_vn_path();
835  orig_rd = orig_attr->source_rd();
836  orig_aspath = orig_attr->as_path();
837  }
838  if (ext_community != nullptr) {
839  for(const ExtCommunity::ExtCommunityValue &comm:
840  ext_community->communities()) {
842  sgid_list.push_back(comm);
843  if (ExtCommunity::is_site_of_origin(comm) && soo.IsNull())
844  soo = SiteOfOrigin(comm);
845  if (ExtCommunity::is_load_balance(comm)) {
846  load_balance = LoadBalance(comm);
847  load_balance_present = true;
848  }
849  }
850  }
851  if (large_community != nullptr) {
852  for(const auto &comm : large_community->communities()) {
853  if (LargeCommunity::is_tag(comm)) {
854  tag_list.push_back(comm);
855  }
856  }
857  }
858  }
859 
860  BgpAttrDB *attr_db = server->attr_db();
861  CommunityDB *comm_db = server->comm_db();
862  CommunityPtr new_community = comm_db->AppendAndLocate(
863  orig_community, CommunityType::AcceptOwnNexthop);
864  ExtCommunityDB *extcomm_db = server->extcomm_db();
865  LargeCommunityDB *largecomm_db = server->largecomm_db();
866  BgpMembershipManager *membership_mgr = server->membership_mgr();
867  OriginVnPathDB *ovnpath_db = server->ovnpath_db();
868  OriginVnPathPtr new_ovnpath;
869  if (server->autonomous_system() > AS2_MAX && vn_index > 0xffff) {
870  new_ovnpath = ovnpath_db->PrependAndLocate(orig_ovnpath,
871  origin_vn4_bytes);
872  new_ovnpath = ovnpath_db->PrependAndLocate(new_ovnpath.get(),
873  origin_vn_trans_bytes);
874  } else {
875  new_ovnpath = ovnpath_db->PrependAndLocate(
876  orig_ovnpath, origin_vn_bytes);
877  }
878 
879  ConnectedPathIdList new_path_ids;
880  for (Route::PathList::iterator it =
881  connected_route()->GetPathList().begin();
882  it != connected_route()->GetPathList().end(); ++it) {
883  BgpPath *connected_path = static_cast<BgpPath *>(it.operator->());
884 
885  // Infeasible paths are not considered
886  if (!connected_path->IsFeasible())
887  break;
888 
889  // take snapshot of all ECMP paths
890  if (connected_route()->BestPath()->PathCompare(*connected_path, true))
891  break;
892 
893  // Skip paths with duplicate forwarding information. This ensures
894  // that we generate only one path with any given next hop and label
895  // when there are multiple connected paths from the original source
896  // received via different peers e.g. directly via XMPP and via BGP.
897  if (connected_route()->DuplicateForwardingPath(connected_path))
898  continue;
899 
900  const BgpAttr *attr = connected_path->GetAttr();
901 
902  ExtCommunityPtr new_ext_community;
903  LargeCommunityPtr new_large_community;
904 
905  // Strip any RouteTargets from the connected attributes.
906  new_ext_community = extcomm_db->ReplaceRTargetAndLocate(
908 
909  // Add the export route target list from the source routing instance
910  // when inserting into EVPN table. This is required for the case when
911  // service-chain routes are replicated to the BGP table to be used on
912  // the BMS. The TOR switch does not import the cooked-up RT of the
913  // service-RI. It only imports the RTs of the primary RI. Hence, we
914  // add the primary RIs RTs to the route.
915  // NOTE: There is an assumption that connected_ri on the head SI
916  // will always point to the primary RI. Need to change that if the
917  // assumption is not true.
918  // Also, we pick only the export route targets in the range used by
919  // schema transformer for non user-configured RTs.
920  if (is_sc_head() && bgptable->family() == Address::EVPN) {
921  ExtCommunity::ExtCommunityList export_list;
922  const RoutingInstance *conn_ri =
925  connected_->name()));
926  if (!conn_ri) {
927  // conn_ri is not expected to be found only in unit tests.
928  assert(bgp_log_test::unit_test());
929  conn_ri = connected_routing_instance();
930  }
931  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
932  "Adding primary RI " << conn_ri->name() << " route targets " <<
933  "to service-chain route for EVPN table " << bgptable->name());
934  BOOST_FOREACH(const RouteTarget &rtarget,
935  conn_ri->GetExportList()) {
937  rtarget.GetExtCommunity()) != 0) {
938  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG,
939  BGP_LOG_FLAG_TRACE, "RT value " << rtarget.ToString());
940  export_list.push_back(rtarget.GetExtCommunity());
941  }
942  }
943  new_ext_community = extcomm_db->AppendAndLocate(
944  new_ext_community.get(), export_list);
945  }
946 
947  // Replace the SGID list with the list from the original route.
948  new_ext_community = extcomm_db->ReplaceSGIDListAndLocate(
949  new_ext_community.get(), sgid_list);
950 
951  // Replace the Tag list with the list from the original route.
952  new_large_community = largecomm_db->ReplaceTagListAndLocate(
953  new_large_community.get(), tag_list);
954 
955  // Replace SiteOfOrigin with value from original route if any.
956  if (soo.IsNull()) {
957  new_ext_community = extcomm_db->RemoveSiteOfOriginAndLocate(
958  new_ext_community.get());
959  } else {
960  new_ext_community = extcomm_db->ReplaceSiteOfOriginAndLocate(
961  new_ext_community.get(), soo.GetExtCommunity());
962  }
963 
964  // Inherit load balance attribute of orig_route if connected path
965  // does not have one already.
966  if (!LoadBalance::IsPresent(connected_path) && load_balance_present) {
967  new_ext_community = extcomm_db->AppendAndLocate(
968  new_ext_community.get(), load_balance.GetExtCommunity());
969  }
970 
971  // Replace the OriginVn with the value from the original route
972  // or the value associated with the dest routing instance.
973  if (server->autonomous_system() > AS2_MAX && vn_index > 0xffff) {
974  new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
975  new_ext_community.get(), origin_vn4_bytes);
976  new_ext_community = extcomm_db->AppendAndLocate(
977  new_ext_community.get(), origin_vn_trans_bytes);
978  } else {
979  new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
980  new_ext_community.get(), origin_vn_bytes);
981  }
982 
983  // Connected routes always have mpls (udp or gre) as encap.
984  // If updating service-chain route in the EVPN table, change
985  // tunnel encap to include VxLAN and MPLS. BMS only supports VxLAN.
986  // Vrouter has the choice of using VxLAN or MPLS based on config.
987  if (is_sc_head() && bgptable->family() == Address::EVPN) {
988  ExtCommunity::ExtCommunityList encaps_list;
989  vector<string> tunnel_encaps = boost::assign::list_of("vxlan");
990  BOOST_FOREACH(string encap, tunnel_encaps) {
991  encaps_list.push_back(TunnelEncap(encap).GetExtCommunity());
992  }
993  new_ext_community = extcomm_db->
994  ReplaceTunnelEncapsulationAndLocate(new_ext_community.get(),
995  encaps_list);
996  }
997 
998  // Replace extended community, community and origin vn path.
999  BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
1000  attr, new_ext_community);
1001  new_attr =
1002  attr_db->ReplaceLargeCommunityAndLocate(new_attr.get(),
1003  new_large_community);
1004  new_attr =
1005  attr_db->ReplaceCommunityAndLocate(new_attr.get(), new_community);
1006  new_attr = attr_db->ReplaceOriginVnPathAndLocate(new_attr.get(),
1007  new_ovnpath);
1008 
1009  // Strip as_path if needed. This is required when the connected route is
1010  // learnt via BGP. If retain_as_path knob is configured replace the
1011  // AsPath with the value from the original route.
1012  if (retain_as_path() && orig_aspath) {
1013  new_attr = attr_db->ReplaceAsPathAndLocate(new_attr.get(),
1014  orig_aspath);
1015  } else {
1016  new_attr = attr_db->ReplaceAsPathAndLocate(new_attr.get(),
1017  AsPathPtr());
1018  }
1019 
1020  // If the connected path is learnt via XMPP, construct RD based on
1021  // the id registered with source table instead of connected table.
1022  // This allows chaining of multiple in-network service instances
1023  // that are on the same compute node.
1024  const IPeer *peer = connected_path->GetPeer();
1025  if (src_ != connected_ && peer && peer->IsXmppPeer()) {
1026  int instance_id = -1;
1027  bool is_registered = membership_mgr->GetRegistrationInfo(peer,
1028  bgptable, &instance_id);
1029  if (!is_registered)
1030  continue;
1031  RouteDistinguisher connected_rd = attr->source_rd();
1032  if (connected_rd.Type() != RouteDistinguisher::TypeIpAddressBased)
1033  continue;
1034 
1035  RouteDistinguisher rd(connected_rd.GetAddress(), instance_id);
1036  new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
1037  }
1038 
1039  // Replace the source rd if the connected path is a secondary path
1040  // of a primary path in the l3vpn table. Use the RD of the primary.
1041  if (connected_path->IsReplicated()) {
1042  const BgpSecondaryPath *spath =
1043  static_cast<const BgpSecondaryPath *>(connected_path);
1044  const RoutingInstance *ri = spath->src_table()->routing_instance();
1045  if (ri->IsMasterRoutingInstance()) {
1046  const VpnRouteT *vpn_route =
1047  static_cast<const VpnRouteT *>(spath->src_rt());
1048  new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(),
1049  vpn_route->GetPrefix().route_distinguisher());
1050  }
1051  }
1052 
1053  // Skip paths with Source RD same as source RD of the connected path
1054  if (!orig_rd.IsZero() && new_attr->source_rd() == orig_rd)
1055  continue;
1056 
1057  // Check whether we already have a path with the associated path id.
1058  uint32_t path_id =
1059  connected_path->GetAttr()->nexthop().to_v4().to_ulong();
1060  ProcessServiceChainPath(path_id, connected_path, new_attr,
1061  service_chain_route, partition,
1062  aggregate, bgptable);
1063  new_path_ids.insert(path_id);
1064  }
1065 
1066  // Remove stale paths.
1067  for (ConnectedPathIdList::const_iterator it = old_path_ids.begin();
1068  it != old_path_ids.end(); ++it) {
1069  uint32_t path_id = *it;
1070  if (new_path_ids.find(path_id) != new_path_ids.end())
1071  continue;
1072  service_chain_route->RemovePath(BgpPath::ServiceChain, NULL, path_id);
1073  partition->Notify(service_chain_route);
1074 
1075  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
1076  "Removed " << (aggregate ? "Aggregate" : "ExtConnected") <<
1077  " ServiceChain path " << service_chain_route->ToString() <<
1078  " path_id " << BgpPath::PathIdString(path_id) <<
1079  " in table " << bgptable->name());
1080 
1081  }
1082 
1083  // Delete the route if there's no paths.
1084  if (!service_chain_route->HasPaths())
1085  partition->Delete(service_chain_route);
1086 }
1087 
1088 template <typename T>
1090  BgpRoute *more_specific) {
1091  typename PrefixToRouteListMap::iterator it =
1092  prefix_to_routelist_map_.find(aggregate);
1093  assert(it != prefix_to_routelist_map_.end());
1094  bool ret = false;
1095  if (it->second.empty()) {
1096  // Add the aggregate for the first time
1097  ret = true;
1098  }
1099  it->second.insert(more_specific);
1100  return ret;
1101 }
1102 
1103 template <typename T>
1105  BgpRoute *more_specific) {
1106  typename PrefixToRouteListMap::iterator it =
1107  prefix_to_routelist_map_.find(aggregate);
1108  assert(it != prefix_to_routelist_map_.end());
1109  it->second.erase(more_specific);
1110  return it->second.empty();
1111 }
1112 
1113 template <typename T>
1114 void ServiceChain<T>::FillServiceChainInfo(ShowServicechainInfo *info) const {
1115  if (deleted()) {
1116  info->set_state("deleted");
1117  } else if (!IsConnectedRouteValid()) {
1118  info->set_state("down");
1119  } else if (!group_oper_state_up()) {
1120  info->set_state("group down");
1121  } else {
1122  info->set_state("active");
1123  }
1124 
1125  ConnectedRouteInfo connected_rt_info;
1126  connected_rt_info.set_service_chain_addr(
1127  service_chain_addr().to_string());
1128  if (connected_route()) {
1129  ShowRoute show_route;
1130  connected_route()->FillRouteInfo(connected_table(), &show_route);
1131  connected_rt_info.set_connected_rt(show_route);
1132  }
1133  info->set_connected_route(connected_rt_info);
1134 
1135  vector<PrefixToRouteListInfo> more_vec;
1136  for (typename PrefixToRouteListMap::const_iterator it =
1137  prefix_to_route_list_map()->begin();
1138  it != prefix_to_route_list_map()->end(); ++it) {
1139  PrefixToRouteListInfo prefix_list_info;
1140  prefix_list_info.set_prefix(it->first.ToString());
1141 
1142  BgpTable *bgptable = src_table();
1143  RouteT rt_key(it->first);
1144  BgpRoute *aggregate = static_cast<BgpRoute *>(bgptable->Find(&rt_key));
1145  if (aggregate) {
1146  prefix_list_info.set_aggregate(true);
1147  ShowRoute show_route;
1148  aggregate->FillRouteInfo(bgptable, &show_route);
1149  prefix_list_info.set_aggregate_rt(show_route);
1150  } else {
1151  prefix_list_info.set_aggregate(false);
1152  }
1153 
1154  vector<string> rt_list;
1155  for (RouteList::iterator rt_it = it->second.begin();
1156  rt_it != it->second.end(); ++rt_it) {
1157  rt_list.push_back((*rt_it)->ToString());
1158  }
1159  prefix_list_info.set_more_specific_list(rt_list);
1160  more_vec.push_back(prefix_list_info);
1161  }
1162  info->set_more_specifics(more_vec);
1163 
1164  vector<ExtConnectRouteInfo> ext_connecting_rt_info_list;
1165  for (ExtConnectRouteList::const_iterator it =
1166  ext_connecting_routes().begin();
1167  it != ext_connecting_routes().end(); ++it) {
1168  ExtConnectRouteInfo ext_rt_info;
1169  ext_rt_info.set_ext_rt_prefix((*it)->ToString());
1170  BgpTable *bgptable = src_table();
1171  RouteT *ext_route = static_cast<RouteT *>(*it);
1172  RouteT rt_key(ext_route->GetPrefix());
1173  BgpRoute *ext_connecting =
1174  static_cast<BgpRoute *>(bgptable->Find(&rt_key));
1175  if (ext_connecting) {
1176  ShowRoute show_route;
1177  ext_connecting->FillRouteInfo(bgptable, &show_route);
1178  ext_rt_info.set_ext_rt_svc_rt(show_route);
1179  }
1180  ext_connecting_rt_info_list.push_back(ext_rt_info);
1181  }
1182  info->set_ext_connecting_rt_info_list(ext_connecting_rt_info_list);
1183  info->set_aggregate_enable(aggregate_enable());
1184 }
1185 
1187  const string &name)
1188  : manager_(manager),
1189  name_(name),
1190  oper_state_up_(false) {
1191 }
1192 
1194  assert(chain_set_.empty());
1195 }
1196 
1197 //
1198 // Add a RoutingInstance to this ServiceChainGroup.
1199 // The caller must ensure that multiple bgp::ConfigHelper tasks do not
1200 // invoke this method in parallel.
1201 //
1203  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
1204  chain_set_.insert(rtinstance);
1206 }
1207 
1208 //
1209 // Delete a RoutingInstance from this ServiceChainGroup.
1210 // The caller must ensure that multiple bgp::ConfigHelper tasks do not
1211 // invoke this method in parallel.
1212 //
1214  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
1215  chain_set_.erase(rtinstance);
1217 }
1218 
1219 //
1220 // Update the operational state of this ServiceChainGroup.
1221 // It's considered to be up if all ServiceChains in the ServiceChainGroup
1222 // are up.
1223 // Trigger an update on the operational state of each ServiceChain if the
1224 // operational state of the ServiceChainGroup changed.
1225 //
1227  bool old_oper_state_up = oper_state_up_;
1228  oper_state_up_ = true;
1229  BOOST_FOREACH(RoutingInstance *rtinstance, chain_set_) {
1230  if (manager_->ServiceChainIsUp(rtinstance))
1231  continue;
1232  oper_state_up_ = false;
1233  break;
1234  }
1235 
1236  if (old_oper_state_up == oper_state_up_)
1237  return;
1238 
1239  BOOST_FOREACH(RoutingInstance *rtinstance, chain_set_) {
1241  }
1242 }
1243 
1252 template <typename T>
1254  return server_->condition_listener(GetSCFamily());
1255 }
1256 
1257 template <typename T>
1259  CHECK_CONCURRENCY("bgp::ServiceChain");
1260  BgpTable *table = NULL;
1261  BgpRoute *route = NULL;
1262  PrefixT aggregate_match = req->aggregate_match_;
1263  ServiceChainT *info = NULL;
1264 
1265  table = req->table_;
1266  route = req->rt_;
1267  info = static_cast<ServiceChainT *>(req->info_.get());
1268 
1269  // Table where the aggregate route needs to be added
1270  aggregate_match = req->aggregate_match_;
1271 
1272  ServiceChainState *state = NULL;
1273  if (route) {
1274  state = static_cast<ServiceChainState *>
1275  (listener_->GetMatchState(table, route, info));
1276  }
1277 
1278  switch (req->type_) {
1279  case ServiceChainRequestT::MORE_SPECIFIC_ADD_CHG: {
1280  assert(state);
1281  if (state->deleted()) {
1282  state->reset_deleted();
1283  }
1284  if (!info->AddMoreSpecific(aggregate_match, route))
1285  break;
1286  if (!info->IsConnectedRouteValid())
1287  break;
1288  if (!info->group_oper_state_up())
1289  break;
1290 
1291  typename ServiceChainT::ConnectedPathIdList path_ids;
1293  aggregate_match, NULL, path_ids, true);
1294  break;
1295  }
1296  case ServiceChainRequestT::MORE_SPECIFIC_DELETE: {
1297  assert(state);
1298  if (info->DeleteMoreSpecific(aggregate_match, route)) {
1299  // Delete the aggregate route
1300  info->DeleteServiceChainRoute(aggregate_match, true);
1301  }
1302  info->RemoveMatchState(route, state);
1303  break;
1304  }
1305  case ServiceChainRequestT::CONNECTED_ROUTE_ADD_CHG: {
1306  assert(state);
1307  if (route->IsDeleted() || !route->BestPath() ||
1308  !route->BestPath()->IsFeasible()) {
1309  break;
1310  }
1311  UpdateServiceChainGroup(info->group());
1312 
1313  if (state->deleted()) {
1314  state->reset_deleted();
1315  }
1316 
1317  // Store the old path id list and populate the new one.
1318  typename ServiceChainT::ConnectedPathIdList path_ids =
1319  info->GetConnectedPathIds();
1320  info->SetConnectedRoute(route);
1321 
1322  if (!info->group_oper_state_up())
1323  break;
1324 
1325  UpdateServiceChainRoutes(info, path_ids);
1326  break;
1327  }
1328  case ServiceChainRequestT::CONNECTED_ROUTE_DELETE: {
1329  assert(state);
1330  UpdateServiceChainGroup(info->group());
1331  DeleteServiceChainRoutes(info);
1332  info->RemoveMatchState(route, state);
1333  info->SetConnectedRoute(NULL);
1334  break;
1335  }
1336  case ServiceChainRequestT::EXT_CONNECT_ROUTE_ADD_CHG: {
1337  assert(state);
1338  if (state->deleted()) {
1339  state->reset_deleted();
1340  }
1341  info->ext_connecting_routes()->insert(route);
1342  if (!info->IsConnectedRouteValid())
1343  break;
1344  if (!info->group_oper_state_up())
1345  break;
1346  RouteT *ext_route = dynamic_cast<RouteT *>(route);
1347  typename ServiceChainT::ConnectedPathIdList path_ids;
1349  ext_route->GetPrefix(), ext_route, path_ids, false);
1350  break;
1351  }
1352  case ServiceChainRequestT::EXT_CONNECT_ROUTE_DELETE: {
1353  assert(state);
1354  if (info->ext_connecting_routes()->erase(route)) {
1355  RouteT *inet_route = dynamic_cast<RouteT *>(route);
1356  info->DeleteServiceChainRoute(inet_route->GetPrefix(), false);
1357  }
1358  info->RemoveMatchState(route, state);
1359  break;
1360  }
1361  case ServiceChainRequestT::UPDATE_ALL_ROUTES: {
1362  if (info->dest_table_unregistered())
1363  break;
1364  if (info->connected_table_unregistered())
1365  break;
1366  if (!info->connected_route())
1367  break;
1368  if (!info->group_oper_state_up())
1369  break;
1370 
1371  typename ServiceChainT::ConnectedPathIdList path_ids =
1372  info->GetConnectedPathIds();
1373  UpdateServiceChainRoutes(info, path_ids);
1374  break;
1375  }
1376  case ServiceChainRequestT::DELETE_ALL_ROUTES: {
1377  DeleteServiceChainRoutes(info);
1378  break;
1379  }
1380  case ServiceChainRequestT::STOP_CHAIN_DONE: {
1381  if (table == info->connected_table()) {
1383  if (!info->num_matchstate()) {
1384  listener_->UnregisterMatchCondition(table, info);
1385  }
1386  }
1387  if (table == info->dest_table()) {
1389  if (!info->num_matchstate()) {
1390  listener_->UnregisterMatchCondition(table, info);
1391  }
1392  }
1393  if (info->unregistered()) {
1394  chain_set_.erase(info->src_routing_instance());
1395  StartResolve();
1396  }
1397  RetryDelete();
1398  break;
1399  }
1400  default: {
1401  assert(false);
1402  break;
1403  }
1404  }
1405 
1406  if (state) {
1407  state->DecrementRefCnt();
1408  if (state->refcnt() == 0 && state->deleted()) {
1409  listener_->RemoveMatchState(table, route, info);
1410  delete state;
1411  if (!info->num_matchstate()) {
1412  if (info->dest_table_unregistered()) {
1413  listener_->UnregisterMatchCondition(
1414  info->dest_table(), info);
1415  }
1416  if (info->connected_table_unregistered()) {
1417  listener_->UnregisterMatchCondition(
1418  info->connected_table(), info);
1419  }
1420  if (info->unregistered()) {
1421  chain_set_.erase(info->src_routing_instance());
1422  StartResolve();
1423  }
1424  }
1425  }
1426  }
1427  delete req;
1428  return true;
1429 }
1430 
1431 template <typename T>
1433  : server_(server),
1434  listener_(GetListener()),
1435  resolve_trigger_(new TaskTrigger(
1436  bind(&ServiceChainMgr::ResolvePendingServiceChain, this),
1437  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)),
1438  group_trigger_(new TaskTrigger(
1439  bind(&ServiceChainMgr::ProcessServiceChainGroups, this),
1440  TaskScheduler::GetInstance()->GetTaskId("bgp::ServiceChain"), 0)),
1441  aggregate_host_route_(false),
1442  deleter_(new DeleteActor(this)),
1443  server_delete_ref_(this, server->deleter()) {
1444  if (service_chain_task_id_ == -1) {
1446  service_chain_task_id_ = scheduler->GetTaskId("bgp::ServiceChain");
1447  }
1448 
1449  process_queue_.reset(
1451  bind(&ServiceChainMgr::RequestHandler, this, _1)));
1452 
1454  bind(&ServiceChainMgr::RoutingInstanceCallback, this, _1, _2));
1455 
1456  BgpMembershipManager *membership_mgr = server->membership_mgr();
1458  bind(&ServiceChainMgr::PeerRegistrationCallback, this, _1, _2, _3));
1459 }
1460 
1461 template <typename T>
1463  assert(group_set_.empty());
1464  assert(group_map_.empty());
1465 }
1466 
1467 template <typename T>
1469  process_queue_->Shutdown();
1470  RoutingInstanceMgr *ri_mgr = server_->routing_instance_mgr();
1471  ri_mgr->UnregisterInstanceOpCallback(id_);
1472  BgpMembershipManager *membership_mgr = server_->membership_mgr();
1473  membership_mgr->UnregisterPeerRegistrationCallback(registration_id_);
1474  server_delete_ref_.Reset(NULL);
1475 }
1476 
1477 template <typename T>
1479  deleter_->Delete();
1480 }
1481 
1482 template <typename T>
1484  if (!chain_set_.empty() || !pending_chains_.empty())
1485  return false;
1486  if (!group_set_.empty() || !group_map_.empty())
1487  return false;
1488  return true;
1489 }
1490 
1491 template <typename T>
1493  if (!deleter_->IsDeleted())
1494  return;
1495  deleter_->RetryDelete();
1496 }
1497 
1498 template <typename T>
1500  RoutingInstance *rtinstance) {
1501  if (ServiceChainIsPending(rtinstance)) {
1502  PendingChainState state = GetPendingServiceChain(rtinstance);
1503  return state.group;
1504  } else {
1505  const ServiceChain<T> *chain = FindServiceChain(rtinstance);
1506  return (chain ? chain->group() : NULL);
1507  }
1508 }
1509 
1510 template <typename T>
1512  const string &group_name) {
1513  GroupMap::iterator loc = group_map_.find(group_name);
1514  return (loc != group_map_.end() ? loc->second : NULL);
1515 }
1516 
1517 template <typename T>
1519  const string &group_name) {
1520  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
1521 
1522  GroupMap::iterator loc = group_map_.find(group_name);
1523  ServiceChainGroup *group = (loc != group_map_.end()) ? loc->second : NULL;
1524  if (!group) {
1525  string temp_group_name(group_name);
1526  group = new ServiceChainGroup(this, temp_group_name);
1527  group_map_.insert(temp_group_name, group);
1528  }
1529  return group;
1530 }
1531 
1532 template <typename T>
1534  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper", "bgp::ServiceChain");
1535 
1536  if (!group)
1537  return;
1538  group_set_.insert(group);
1539  group_trigger_->Set();
1540 }
1541 
1542 template <typename T>
1544  CHECK_CONCURRENCY("bgp::ServiceChain");
1545 
1546  BOOST_FOREACH(ServiceChainGroup *group, group_set_) {
1547  if (group->empty()) {
1548  string temp_group_name(group->name());
1549  group_map_.erase(temp_group_name);
1550  } else {
1551  group->UpdateOperState();
1552  }
1553  }
1554 
1555  group_set_.clear();
1556  RetryDelete();
1557  return true;
1558 }
1559 
1563 template <>
1565  return Address::INET;
1566 }
1567 
1568 template <>
1570  return Address::INET6;
1571 }
1572 
1573 template <>
1575  return Address::EVPN;
1576 }
1577 
1578 template <>
1580  return Address::EVPN;
1581 }
1582 
1586 template <>
1588  return Address::INET;
1589 }
1590 
1591 template <>
1593  return Address::INET6;
1594 }
1595 
1596 template <>
1598  return Address::INET;
1599 }
1600 
1601 template <>
1603  return Address::INET6;
1604 }
1605 
1609 template <>
1611  return SCAddress::INET;
1612 }
1613 
1614 template <>
1616  return SCAddress::INET6;
1617 }
1618 
1619 template <>
1621  return SCAddress::EVPN;
1622 }
1623 
1624 template <>
1626  return SCAddress::EVPN6;
1627 }
1628 
1629 template <>
1630 template <typename T>
1632  process_queue_->Enqueue(req);
1633 }
1634 
1635 template <typename T>
1637  string *reason) const {
1638  typename PendingChainList::const_iterator loc =
1639  pending_chains_.find(rtinstance);
1640  if (loc != pending_chains_.end()) {
1641  if (reason)
1642  *reason = loc->second.reason;
1643  return true;
1644  }
1645  return false;
1646 }
1647 
1648 template <typename T>
1650  if (ServiceChainIsPending(rtinstance))
1651  return false;
1652  const ServiceChain<T> *service_chain = FindServiceChain(rtinstance);
1653  if (!service_chain)
1654  return false;
1655  return service_chain->IsConnectedRouteValid();
1656 }
1657 
1658 template <typename T>
1660  ShowServicechainInfo *info) const {
1661  string pending_reason;
1662  if (ServiceChainIsPending(rtinstance, &pending_reason)) {
1663  info->set_state("pending");
1664  info->set_pending_reason(pending_reason);
1665  return true;
1666  }
1667  const ServiceChain<T> *service_chain = FindServiceChain(rtinstance);
1668  if (!service_chain)
1669  return false;
1670  service_chain->FillServiceChainInfo(info);
1671  return true;
1672 }
1673 
1674 
1675 
1676 template <typename T>
1678  const ServiceChainConfig &config) {
1679  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
1680 
1681  // Verify whether the entry already exists.
1682  std::scoped_lock lock(mutex_);
1683  ServiceChainMap::iterator it = chain_set_.find(rtinstance);
1684  if (it != chain_set_.end()) {
1685  ServiceChainT *chain = static_cast<ServiceChainT *>(it->second.get());
1686  if (chain->CompareServiceChainConfig(config)) {
1687  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
1688  "No update in ServiceChain config : " << rtinstance->name());
1689  return true;
1690  }
1691 
1692  ServiceChainGroup *group = chain->group();
1693  if (group) {
1694  group->DeleteRoutingInstance(rtinstance);
1695  chain->clear_group();
1696  }
1697 
1698  // Update of ServiceChainConfig.
1699  // Add the routing instance to pending list so that service chain is
1700  // created after stop done callback for the current incarnation gets
1701  // invoked.
1702  if (config.service_chain_id.empty()) {
1703  group = NULL;
1704  } else {
1705  group = LocateServiceChainGroup(config.service_chain_id);
1706  group->AddRoutingInstance(rtinstance);
1707  }
1708  string reason = "Waiting for deletion of previous incarnation";
1709  AddPendingServiceChain(rtinstance, group, reason);
1710 
1711  // Wait for the delete complete callback.
1712  if (chain->deleted())
1713  return false;
1714 
1716  bind(&ServiceChainMgr::StopServiceChainDone, this, _1, _2);
1717  listener_->RemoveMatchCondition(chain->dest_table(), chain, cb);
1718  listener_->RemoveMatchCondition(chain->connected_table(), chain, cb);
1719  return true;
1720  }
1721 
1722  RoutingInstanceMgr *mgr = server_->routing_instance_mgr();
1724 
1725  // Dissociate from the old ServiceChainGroup.
1726  ServiceChainGroup *group = FindServiceChainGroup(rtinstance);
1727  if (group)
1728  group->DeleteRoutingInstance(rtinstance);
1729 
1730  // Delete from the pending list. The instance would already have been
1731  // removed from the pending list if this method is called when trying
1732  // to resolve items in the pending list. However, if this method is
1733  // called when processing a change in the service chain config, then
1734  // we may need to remove it from the pending list.
1735  DeletePendingServiceChain(rtinstance);
1736 
1737  // Locate the new ServiceChainGroup.
1738  if (config.service_chain_id.empty()) {
1739  group = NULL;
1740  } else {
1741  group = LocateServiceChainGroup(config.service_chain_id);
1742  group->AddRoutingInstance(rtinstance);
1743  }
1744 
1745  // Destination routing instance does not exist.
1746  if (!dest) {
1747  string reason = "Destination routing instance does not exist";
1748  AddPendingServiceChain(rtinstance, group, reason);
1749  return false;
1750  }
1751 
1752  // Destination routing instance is being deleted.
1753  if (dest->deleted()) {
1754  string reason = "Destination routing instance is being deleted";
1755  AddPendingServiceChain(rtinstance, group, reason);
1756  return false;
1757  }
1758 
1759  // Destination virtual network index is unknown.
1760  if (!dest->virtual_network_index()) {
1761  string reason = "Destination virtual network index is unknown";
1762  AddPendingServiceChain(rtinstance, group, reason);
1763  return false;
1764  }
1765 
1766  RoutingInstance *connected_ri = NULL;
1767  if (config.source_routing_instance == "") {
1768  connected_ri = rtinstance;
1769  assert(!rtinstance->deleted());
1770  } else {
1771  connected_ri = mgr->GetRoutingInstance(config.source_routing_instance);
1772  }
1773 
1774  // Connected routing instance does not exist.
1775  if (!connected_ri) {
1776  string reason = "Connected routing instance does not exist";
1777  AddPendingServiceChain(rtinstance, group, reason);
1778  return false;
1779  }
1780 
1781  // Connected routing instance is being deleted.
1782  if (connected_ri->deleted()) {
1783  string reason = "Connected routing instance is being deleted";
1784  AddPendingServiceChain(rtinstance, group, reason);
1785  return false;
1786  }
1787 
1788  // Add to pending queue if the service chain address is invalid.
1789  error_code ec;
1790  AddressT chain_addr =
1791  AddressT::from_string(config.service_chain_address, ec);
1792  if (ec.failed()) {
1793  string reason = "Service chain address is invalid";
1794  AddPendingServiceChain(rtinstance, group, reason);
1795  return false;
1796  }
1797 
1803  BgpTable *connected_table = NULL;
1804  connected_table = connected_ri->GetTable(GetConnectedFamily());
1805  assert(connected_table);
1806  BgpTable *dest_table = dest->GetTable(GetFamily());
1807  assert(dest_table);
1808 
1809  // Allocate the new service chain.
1810  ServiceChainPtr chain = ServiceChainPtr(new ServiceChainT(this, group,
1811  rtinstance, dest, connected_ri, config.prefix, chain_addr,
1812  config.sc_head, config.retain_as_path));
1813 
1814  if (aggregate_host_route()) {
1815  ServiceChainT *obj = static_cast<ServiceChainT *>(chain.get());
1816  obj->set_aggregate_enable();
1817  }
1818 
1819  // Add the new service chain request
1820  chain_set_.insert(make_pair(rtinstance, chain));
1821  listener_->AddMatchCondition(
1822  connected_table, chain.get(), BgpConditionListener::RequestDoneCb());
1823  listener_->AddMatchCondition(
1824  dest_table, chain.get(), BgpConditionListener::RequestDoneCb());
1825 
1826  return true;
1827 }
1828 
1829 template <typename T>
1831  const string &instance) const {
1832  RoutingInstance *rtinstance =
1833  server_->routing_instance_mgr()->GetRoutingInstance(instance);
1834  if (!rtinstance)
1835  return NULL;
1836  ServiceChainMap::const_iterator it = chain_set_.find(rtinstance);
1837  if (it == chain_set_.end())
1838  return NULL;
1839  ServiceChainT *chain = static_cast<ServiceChainT *>(it->second.get());
1840  return chain;
1841 }
1842 
1843 template <typename T>
1845  RoutingInstance *rtinstance) const {
1846  ServiceChainMap::const_iterator it = chain_set_.find(rtinstance);
1847  if (it == chain_set_.end())
1848  return NULL;
1849  ServiceChainT *chain = static_cast<ServiceChainT *>(it->second.get());
1850  return chain;
1851 }
1852 
1853 template <typename T>
1855  CHECK_CONCURRENCY("bgp::Config");
1856  for (typename PendingChainList::iterator it = pending_chains_.begin(), next;
1857  it != pending_chains_.end(); it = next) {
1858  next = it;
1859  ++next;
1860  RoutingInstance *rtinstance = it->first;
1861  ServiceChainGroup *group = it->second.group;
1862  if (group)
1863  group->DeleteRoutingInstance(rtinstance);
1864  pending_chains_.erase(it);
1865  const ServiceChainConfig *sc_config =
1866  rtinstance->config()->service_chain_info(GetSCFamily());
1867  if (sc_config)
1868  LocateServiceChain(rtinstance, *sc_config);
1869  }
1870  RetryDelete();
1871  return true;
1872 }
1873 
1874 template <typename T>
1877  StartResolve();
1878 }
1879 
1880 template <typename T>
1882  if (pending_chains_.empty())
1883  return;
1884  resolve_trigger_->Set();
1885 }
1886 
1887 template <typename T>
1889  ConditionMatch *info) {
1890  // Post the RequestDone event to ServiceChain task to take Action
1891  ServiceChainRequestT *req =
1892  new ServiceChainRequestT(ServiceChainRequestT::STOP_CHAIN_DONE, table,
1893  NULL, PrefixT(), ServiceChainPtr(info));
1894  Enqueue(req);
1895  return;
1896 }
1897 
1898 template <typename T>
1900  CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper");
1901 
1902  // Remove the routing instance from pending chains list.
1903  std::scoped_lock lock(mutex_);
1904  if (ServiceChainIsPending(rtinstance)) {
1905  ServiceChainGroup *group = FindServiceChainGroup(rtinstance);
1906  if (group)
1907  group->DeleteRoutingInstance(rtinstance);
1908  DeletePendingServiceChain(rtinstance);
1909  RetryDelete();
1910  }
1911 
1912  ServiceChainT *chain = FindServiceChain(rtinstance);
1913  if (!chain)
1914  return;
1915 
1916  ServiceChainGroup *group = chain->group();
1917  if (group) {
1918  group->DeleteRoutingInstance(rtinstance);
1919  chain->clear_group();
1920  }
1921 
1922  if (chain->deleted())
1923  return;
1924 
1926  bind(&ServiceChainMgr::StopServiceChainDone, this, _1, _2);
1927  listener_->RemoveMatchCondition(chain->dest_table(), chain, cb);
1928  listener_->RemoveMatchCondition(chain->connected_table(), chain, cb);
1929 }
1930 
1931 template <typename T>
1933  bool group_oper_state_up) {
1934  // Bail if there's no service chain for the instance.
1935  ServiceChainT *chain = FindServiceChain(rtinstance);
1936  if (!chain)
1937  return;
1938 
1939  // Update the state in the service chain.
1940  chain->set_group_oper_state_up(group_oper_state_up);
1941 
1942  // Post event to ServiceChain task to update/delete all routes.
1943  typename ServiceChainRequestT::RequestType req_type;
1944  if (group_oper_state_up) {
1945  req_type = ServiceChainRequestT::UPDATE_ALL_ROUTES;
1946  } else {
1947  req_type = ServiceChainRequestT::DELETE_ALL_ROUTES;
1948  }
1950  req_type, NULL, NULL, PrefixT(), ServiceChainPtr(chain));
1951  Enqueue(req);
1952 }
1953 
1954 template <typename T>
1956  const typename ServiceChainT::ConnectedPathIdList &old_path_ids) {
1957  // Update ServiceChain routes for aggregates.
1958  typename ServiceChainT::PrefixToRouteListMap *vn_prefix_list =
1959  chain->prefix_to_route_list_map();
1960  for (typename ServiceChainT::PrefixToRouteListMap::iterator it =
1961  vn_prefix_list->begin(); it != vn_prefix_list->end(); ++it) {
1962  if (!it->second.empty())
1963  chain->UpdateServiceChainRoute(it->first, NULL, old_path_ids, true);
1964  }
1965 
1966  // Update ServiceChain routes for external connecting routes.
1967  for (typename ServiceChainT::ExtConnectRouteList::iterator it =
1968  chain->ext_connecting_routes()->begin();
1969  it != chain->ext_connecting_routes()->end(); ++it) {
1970  RouteT *ext_route = static_cast<RouteT *>(*it);
1971  chain->UpdateServiceChainRoute(
1972  ext_route->GetPrefix(), ext_route, old_path_ids, false);
1973  }
1974 }
1975 
1976 template <typename T>
1978  // Delete ServiceChain routes for aggregates.
1979  typename ServiceChainT::PrefixToRouteListMap *vn_prefix_list =
1980  chain->prefix_to_route_list_map();
1981  for (typename ServiceChainT::PrefixToRouteListMap::iterator it =
1982  vn_prefix_list->begin(); it != vn_prefix_list->end(); ++it) {
1983  chain->DeleteServiceChainRoute(it->first, true);
1984  }
1985 
1986  // Delete ServiceChain routes for external connecting routes.
1987  for (typename ServiceChainT::ExtConnectRouteList::iterator it =
1988  chain->ext_connecting_routes()->begin();
1989  it != chain->ext_connecting_routes()->end(); ++it) {
1990  RouteT *ext_route = static_cast<RouteT *>(*it);
1991  chain->DeleteServiceChainRoute(ext_route->GetPrefix(), false);
1992  }
1993 }
1994 
1995 template <typename T>
1997  bool unregister) {
1998  CHECK_CONCURRENCY("bgp::PeerMembership");
1999 
2000  // Bail if it's not an XMPP peer.
2001  if (!peer->IsXmppPeer())
2002  return;
2003 
2004  // Bail if there's no service chain for the instance.
2005  ServiceChainT *chain = FindServiceChain(table->routing_instance());
2006  if (!chain)
2007  return;
2008 
2009  // Post event to ServiceChain task to update all routes.
2010  ServiceChainRequestT *req =
2011  new ServiceChainRequestT(ServiceChainRequestT::UPDATE_ALL_ROUTES, NULL,
2012  NULL, PrefixT(), ServiceChainPtr(chain));
2013  Enqueue(req);
2014 }
2015 
2016 template <typename T>
2018  resolve_trigger_->set_disable();
2019 }
2020 
2021 template <typename T>
2023  resolve_trigger_->set_enable();
2024 }
2025 
2026 template <typename T>
2028  group_trigger_->set_disable();
2029 }
2030 
2031 template <typename T>
2033  group_trigger_->set_enable();
2034 }
2035 
2036 template <typename T>
2038  uint32_t count = 0;
2039  for (ServiceChainMap::const_iterator it = chain_set_.begin();
2040  it != chain_set_.end(); ++it) {
2041  const ServiceChainT *chain =
2042  static_cast<const ServiceChainT *>(it->second.get());
2043  if (!chain->IsConnectedRouteValid())
2044  count++;
2045  }
2046  return count;
2047 }
2048 
2049 // Explicit instantiation of ServiceChainMgr for INET and INET6.
2050 template class ServiceChainMgr<ServiceChainInet>;
2051 template class ServiceChainMgr<ServiceChainInet6>;
2052 template class ServiceChainMgr<ServiceChainEvpn>;
2053 template class ServiceChainMgr<ServiceChainEvpn6>;
boost::asio::ip::address IpAddress
Definition: address.h:14
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
#define BGP_LOG_FLAG_TRACE
Definition: bgp_log.h:44
#define BGP_LOG_STR(obj, level, flags, arg)
Definition: bgp_log.h:90
boost::intrusive_ptr< const OriginVnPath > OriginVnPathPtr
Family
Definition: address.h:25
@ INET
Definition: address.h:27
@ EVPN
Definition: address.h:32
@ INET6
Definition: address.h:28
BgpAttrPtr ReplaceAsPathAndLocate(const BgpAttr *attr, AsPathPtr aspath)
Definition: bgp_attr.cc:1343
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 ReplaceOriginVnPathAndLocate(const BgpAttr *attr, OriginVnPathPtr ovnpath)
Definition: bgp_attr.cc:1383
BgpAttrPtr ReplaceLargeCommunityAndLocate(const BgpAttr *attr, LargeCommunityPtr largecomm)
Definition: bgp_attr.cc:1375
BgpAttrPtr ReplaceCommunityAndLocate(const BgpAttr *attr, CommunityPtr community)
Definition: bgp_attr.cc:1351
const LargeCommunity * large_community() const
Definition: bgp_attr.h:919
const AsPath * as_path() const
Definition: bgp_attr.h:902
const OriginVnPath * origin_vn_path() const
Definition: bgp_attr.h:922
const Community * community() const
Definition: bgp_attr.h:917
const RouteDistinguisher & source_rd() const
Definition: bgp_attr.h:899
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)
const ServiceChainConfig * service_chain_info(SCAddress::Family family) const
Definition: bgp_config.cc:380
void UnregisterPeerRegistrationCallback(int id)
int RegisterPeerRegistrationCallback(PeerRegistrationCallback callback)
bool GetRegistrationInfo(const IPeer *peer, const BgpTable *table, int *instance_id=NULL, uint64_t *subscription_gen_id=NULL) const
bool IsVrfOriginated() const
Definition: bgp_path.h:69
uint32_t GetLabel() const
Definition: bgp_path.h:90
uint32_t GetFlags() const
Definition: bgp_path.h:101
IPeer * GetPeer()
Definition: bgp_path.h:77
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
@ ServiceChain
Definition: bgp_path.h:41
@ BGP_XMPP
Definition: bgp_path.h:40
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
PathSource GetSource() const
Definition: bgp_path.h:104
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
virtual bool IsValid() const
Definition: bgp_route.cc:340
void FillRouteInfo(const BgpTable *table, ShowRouteBrief *show_route) const
Definition: bgp_route.cc:434
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
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
as_t autonomous_system() const
Definition: bgp_server.h:213
OriginVnPathDB * ovnpath_db()
Definition: bgp_server.h:196
BgpAttrDB * attr_db()
Definition: bgp_server.h:188
CommunityDB * comm_db()
Definition: bgp_server.h:191
BgpMembershipManager * membership_mgr()
Definition: bgp_server.h:180
LargeCommunityDB * largecomm_db()
Definition: bgp_server.h:195
BgpServer * server()
Definition: bgp_table.cc:88
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
virtual Address::Family family() const =0
CommunityPtr AppendAndLocate(const Community *src, uint32_t value)
Definition: community.cc:134
bool ContainsValue(uint32_t value) const
Definition: community.cc:116
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
const std::string & name() const
Definition: db_table.h:111
void Notify(DBEntryBase *entry)
void Delete(DBEntryBase *)
virtual void Add(DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
DBEntry * Find(const DBEntry *entry)
Definition: db_table.cc:496
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
uint8_t type() const
static EvpnPrefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
Address::Family family() const
const EvpnPrefix & GetPrefix() const
ExtCommunityPtr AppendAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &list)
Definition: community.cc:766
ExtCommunityPtr RemoveSiteOfOriginAndLocate(const ExtCommunity *src)
Definition: community.cc:857
ExtCommunityPtr ReplaceSGIDListAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &sgid_list)
Definition: community.cc:827
ExtCommunityPtr ReplaceSiteOfOriginAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityValue &soo)
Definition: community.cc:870
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
boost::array< uint8_t, 8 > ExtCommunityValue
Definition: community.h:155
const ExtCommunityList & communities() const
Definition: community.h:183
static bool is_load_balance(const ExtCommunityValue &val)
Definition: community.h:375
static bool is_security_group(const ExtCommunityValue &val)
Definition: community.h:307
static bool is_origin_vn(const ExtCommunityValue &val)
Definition: community.h:196
static uint32_t get_rtarget_val(const ExtCommunityValue &val)
Definition: community.h:281
static bool is_site_of_origin(const ExtCommunityValue &val)
Definition: community.h:325
Definition: ipeer.h:187
virtual bool IsXmppPeer() const =0
virtual void UpdateServiceChainGroup(ServiceChainGroup *group)=0
virtual void UpdateServiceChain(RoutingInstance *rtinstance, bool group_oper_state_up)=0
virtual bool ServiceChainIsUp(RoutingInstance *rtinstance) const =0
Ip6Address addr() const
Definition: inet6_route.h:27
const Inet6Prefix & GetPrefix() const
Definition: inet6_route.h:81
const Ip4Prefix & GetPrefix() const
Definition: inet_route.h:71
Ip4Address addr() const
Definition: inet_route.h:25
This class represents a database for managing LargeCommunity objects. It is used to store,...
Definition: community.h:703
LargeCommunityPtr ReplaceTagListAndLocate(const LargeCommunity *src, const LargeCommunity::LargeCommunityList &tag_list)
Replace all tags in a LargeCommunity with a new tag list.
Definition: community.cc:1240
This class represents an array of BGP Large Community values. A LargeCommunity consists of one or mor...
Definition: community.h:557
const LargeCommunityList & communities() const
Get the list of Large Community values.
Definition: community.h:595
std::vector< LargeCommunityValue > LargeCommunityList
A list (vector) of LargeCommunityValue items.
Definition: community.h:563
static bool is_tag(const LargeCommunityValue &val)
Check if the LargeCommunity value is tag.
Definition: community.h:622
static bool IsPresent(const BgpPath *path)
const bytes_type & GetExtCommunity() const
Definition: load_balance.h:120
OriginVnPathPtr PrependAndLocate(const OriginVnPath *ovnpath, const OriginVnPath::OriginVnValue &value)
boost::array< uint8_t, 8 > OriginVnValue
bool Contains(const OriginVnValue &value) const
int vn_index() const
Definition: origin_vn.cc:123
const bytes_type & GetExtCommunity() const
Definition: origin_vn.h:34
uint16_t Type() const
Definition: rd.cc:45
uint32_t GetAddress() const
Definition: rd.cc:49
bool IsZero() const
Definition: rd.h:44
@ TypeIpAddressBased
Definition: rd.h:19
std::string ToString() const
const bytes_type & GetExtCommunity() const
const PathList & GetPathList() const
Definition: route.h:47
RoutingInstance * GetRoutingInstance(const std::string &name)
void UnregisterInstanceOpCallback(int id)
static std::string GetPrimaryRoutingInstanceName(const string &name_in)
int RegisterInstanceOpCallback(RoutingInstanceCb cb)
const std::string & name() const
bool deleted() const
int virtual_network_index() const
const BgpInstanceConfig * config() const
const RouteTargetList & GetExportList() const
BgpTable * GetTable(Address::Family fmly)
bool IsMasterRoutingInstance() const
ServiceChainGroup(IServiceChainMgr *manager, const std::string &name)
void DeleteRoutingInstance(RoutingInstance *rtinstance)
void AddRoutingInstance(RoutingInstance *rtinstance)
ServiceChainSet chain_set_
IServiceChainMgr * manager_
std::string name() const
DeleteActor(ServiceChainMgr< T > *manager)
ServiceChainMgr< T > * manager_
virtual bool MayDelete() const
BgpServer * server_
void UpdateServiceChainGroup(ServiceChainGroup *group)
virtual BgpConditionListener * GetListener()
void UpdateServiceChainRoutes(ServiceChainT *chain, const typename ServiceChainT::ConnectedPathIdList &old_path_ids)
void DeleteServiceChainRoutes(ServiceChainT *chain)
ServiceChainGroup * LocateServiceChainGroup(const std::string &group_name)
Address::Family GetConnectedFamily() const
SCAddress::Family GetSCFamily() const
virtual uint32_t GetDownServiceChainCount() const
virtual void StopServiceChain(RoutingInstance *rtinstance)
bool ProcessServiceChainGroups()
virtual void DisableGroupTrigger()
ServiceChainT * FindServiceChain(const std::string &instance) const
virtual void EnableGroupTrigger()
virtual void EnableResolveTrigger()
virtual bool LocateServiceChain(RoutingInstance *rtinstance, const ServiceChainConfig &config)
virtual ~ServiceChainMgr()
T::AddressT AddressT
ServiceChainGroup * FindServiceChainGroup(RoutingInstance *rtinstance)
void StopServiceChainDone(BgpTable *table, ConditionMatch *info)
ServiceChainMgr(BgpServer *server)
static int service_chain_task_id_
bool ResolvePendingServiceChain()
void RoutingInstanceCallback(std::string name, int op)
void Enqueue(ServiceChainRequestT *req)
bool RequestHandler(ServiceChainRequestT *req)
virtual void DisableResolveTrigger()
void PeerRegistrationCallback(IPeer *peer, BgpTable *table, bool unregister)
virtual void UpdateServiceChain(RoutingInstance *rtinstance, bool group_oper_state_up)
virtual bool FillServiceChainInfo(RoutingInstance *rtinstance, ShowServicechainInfo *info) const
boost::scoped_ptr< WorkQueue< ServiceChainRequestT * > > process_queue_
virtual bool ServiceChainIsPending(RoutingInstance *rtinstance, std::string *reason=NULL) const
virtual bool ServiceChainIsUp(RoutingInstance *rtinstance) const
Address::Family GetFamily() const
bool MayDelete() const
ServiceChainPtr info_
bool group_oper_state_up() const
const ConnectedPathIdList & GetConnectedPathIds()
SCAddress::Family GetSCFamily() const
bool dest_table_unregistered() const
bool IsEvpnType5Route(BgpRoute *route) const
virtual std::string ToString() const
void set_dest_table_unregistered()
PrefixToRouteListMap * prefix_to_route_list_map()
std::set< uint32_t > ConnectedPathIdList
bool AddMoreSpecific(PrefixT aggregate, BgpRoute *more_specific)
bool CompareServiceChainConfig(const ServiceChainConfig &config)
void DeleteServiceChainRouteInternal(BgpRoute *service_chain_route, DBTablePartition *partition, BgpTable *bgptable, bool aggregate)
void UpdateServiceChainRouteInternal(const RouteT *orig_route, const ConnectedPathIdList &old_path_ids, BgpRoute *sc_route, DBTablePartition *partition, BgpTable *bgptable, bool aggregate)
BgpRoute * connected_route() const
T::PrefixT PrefixT
void RemoveMatchState(BgpRoute *route, ServiceChainState *state)
T::RouteT RouteT
void UpdateServiceChainRoute(PrefixT prefix, const RouteT *orig_route, const ConnectedPathIdList &old_path_ids, bool aggregate)
PrefixToRouteListMap prefix_to_routelist_map_
bool unregistered() const
virtual bool Match(BgpServer *server, BgpTable *table, BgpRoute *route, bool deleted)
std::map< PrefixT, RouteList > PrefixToRouteListMap
void SetConnectedRoute(BgpRoute *connected)
T::AddressT AddressT
void FillServiceChainInfo(ShowServicechainInfo *info) const
bool DeleteMoreSpecific(PrefixT aggregate, BgpRoute *more_specific)
const ExtConnectRouteList & ext_connecting_routes() const
void ProcessServiceChainPath(uint32_t path_id, BgpPath *path, BgpAttrPtr attr, BgpRoute *&route, DBTablePartition *&partition, bool aggregate, BgpTable *bgptable)
BgpTable * dest_table() const
void set_group_oper_state_up(bool up)
void GetReplicationFamilyInfo(DBTablePartition *&partition, BgpRoute *&route, BgpTable *&table, PrefixT prefix, bool create)
BgpTable * connected_table() const
bool IsConnectedRoute(BgpRoute *route, bool is_conn_table=false) const
bool IsMoreSpecific(BgpRoute *route, PrefixT *aggregate_match) const
RoutingInstance * src_routing_instance() const
void set_connected_table_unregistered()
BgpTable * src_table() const
bool IsConnectedRouteValid() const
std::set< BgpRoute * > RouteList
ServiceChain(ServiceChainMgrT *manager, ServiceChainGroup *group, RoutingInstance *src, RoutingInstance *dest, RoutingInstance *connected, const std::vector< std::string > &subnets, AddressT addr, bool head, bool retain_as_path)
T::VpnRouteT VpnRouteT
bool IsAggregate(BgpRoute *route) const
ServiceChainGroup * group() const
Address::Family GetFamily() const
bool connected_table_unregistered() const
void DeleteServiceChainRoute(PrefixT prefix, bool aggregate)
void set_aggregate_enable()
const bytes_type & GetExtCommunity() const
bool IsNull() const
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 LargeCommunity > LargeCommunityPtr
Defines a type for automatic storage of a LargeCommunity instance.
Definition: community.h:687
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
bool unit_test()
Definition: bgp_log.cc:54
static PhysicalDevice::ManagementProtocol FromString(const string &proto)
static int GetOriginVnIndex(const BgpTable *table, const BgpRoute *route)
ConditionMatchPtr ServiceChainPtr
std::vector< std::string > prefix
Definition: bgp_config.h:329
std::string source_routing_instance
Definition: bgp_config.h:332
std::string service_chain_id
Definition: bgp_config.h:333
std::string service_chain_address
Definition: bgp_config.h:330
std::string routing_instance
Definition: bgp_config.h:328
#define CHECK_CONCURRENCY(...)