LCOV - code coverage report
Current view: top level - bgp/evpn - evpn_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 145 237 61.2 %
Date: 2026-11-16 02:06:09 Functions: 17 22 77.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/evpn/evpn_table.h"
       7             : 
       8             : #include "bgp/ipeer.h"
       9             : #include "bgp/bgp_factory.h"
      10             : #include "bgp/bgp_evpn.h"
      11             : #include "bgp/bgp_server.h"
      12             : #include "bgp/bgp_update.h"
      13             : #include "bgp/inet/inet_table.h"
      14             : #include "bgp/inet6/inet6_table.h"
      15             : #include "bgp/origin-vn/origin_vn.h"
      16             : #include "bgp/routing-instance/path_resolver.h"
      17             : #include "bgp/routing-instance/routing_instance.h"
      18             : 
      19             : using std::unique_ptr;
      20             : using std::string;
      21             : 
      22       62968 : size_t EvpnTable::HashFunction(const EvpnPrefix &prefix) {
      23       62968 :     if (prefix.type() == EvpnPrefix::MacAdvertisementRoute) {
      24       62967 :         if (prefix.mac_addr().IsBroadcast())
      25           0 :             return 0;
      26       62968 :         const uint8_t *data = prefix.mac_addr().GetData();
      27       62968 :         uint32_t value = get_value(data + 2, 4);
      28       62967 :         return boost::hash_value(value);
      29             :     }
      30           0 :     if (prefix.type() == EvpnPrefix::IpPrefixRoute) {
      31           0 :         if (prefix.ip_address().is_v4()) {
      32           0 :             return InetTable::HashFunction(prefix.inet_prefix());
      33             :         } else {
      34           0 :             return Inet6Table::HashFunction(prefix.inet6_prefix());
      35             :         }
      36             :     }
      37           0 :     return 0;
      38             : }
      39             : 
      40         100 : EvpnTable::EvpnTable(DB *db, const string &name)
      41         100 :     : BgpTable(db, name), evpn_manager_(NULL) {
      42         100 :     mac_route_count_ = 0;
      43         100 :     unique_mac_route_count_ = 0;
      44         100 :     im_route_count_ = 0;
      45         100 :     ip_route_count_ = 0;
      46         100 : }
      47             : 
      48        5857 : unique_ptr<DBEntry> EvpnTable::AllocEntry(
      49             :         const DBRequestKey *key) const {
      50        5857 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      51        5857 :     return unique_ptr<DBEntry> (new EvpnRoute(pfxkey->prefix));
      52             : }
      53             : 
      54           0 : unique_ptr<DBEntry> EvpnTable::AllocEntryStr(
      55             :         const string &key_str) const {
      56           0 :     EvpnPrefix prefix = EvpnPrefix::FromString(key_str);
      57           0 :     return unique_ptr<DBEntry> (new EvpnRoute(prefix));
      58             : }
      59             : 
      60        3840 : void EvpnTable::AddRemoveCallback(const DBEntryBase *entry, bool add) const {
      61        3840 :     if (IsVpnTable())
      62        3328 :         return;
      63         512 :     const EvpnRoute *evpn_rt = static_cast<const EvpnRoute *>(entry);
      64         512 :     const EvpnPrefix &evpn_prefix = evpn_rt->GetPrefix();
      65         512 :     switch (evpn_prefix.type()) {
      66         512 :     case EvpnPrefix::MacAdvertisementRoute:
      67             :         // Ignore Broadcast MAC routes.
      68         512 :         if (evpn_prefix.mac_addr().IsBroadcast())
      69           0 :             break;
      70             : 
      71         512 :         if (add) {
      72         256 :             mac_route_count_++;
      73             :         } else {
      74         256 :             mac_route_count_--;
      75             :         }
      76             : 
      77             :         // Ignore MAC routes with IP addresses.
      78         512 :         if (evpn_prefix.family() != Address::UNSPEC)
      79           0 :             break;
      80             : 
      81         512 :         if (add) {
      82         256 :             unique_mac_route_count_++;
      83             :         } else {
      84         256 :             unique_mac_route_count_--;
      85             :         }
      86         512 :         break;
      87             : 
      88           0 :     case EvpnPrefix::InclusiveMulticastRoute:
      89           0 :         if (add) {
      90           0 :             im_route_count_++;
      91             :         } else {
      92           0 :             im_route_count_--;
      93             :         }
      94           0 :         break;
      95             : 
      96           0 :     case EvpnPrefix::IpPrefixRoute:
      97           0 :         if (add) {
      98           0 :             ip_route_count_++;
      99             :         } else {
     100           0 :             ip_route_count_--;
     101             :         }
     102           0 :         break;
     103             : 
     104           0 :     default:
     105           0 :         break;
     106             :     }
     107             : }
     108             : 
     109        2304 : size_t EvpnTable::Hash(const DBRequestKey *key) const {
     110        2304 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
     111        2304 :     size_t value = HashFunction(rkey->prefix);
     112        2304 :     return value % DB::PartitionCount();
     113             : }
     114             : 
     115       60668 : size_t EvpnTable::Hash(const DBEntry *entry) const {
     116       60668 :     const EvpnRoute *rt_entry = static_cast<const EvpnRoute *>(entry);
     117       60668 :     size_t value = HashFunction(rt_entry->GetPrefix());
     118       60668 :     return value % DB::PartitionCount();
     119             : }
     120             : 
     121        2304 : BgpRoute *EvpnTable::TableFind(DBTablePartition *rtp,
     122             :         const DBRequestKey *prefix) {
     123        2304 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
     124        2304 :     EvpnRoute rt_key(pfxkey->prefix);
     125        4608 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
     126        2304 : }
     127             : 
     128          80 : PathResolver *EvpnTable::CreatePathResolver() {
     129          80 :     if (routing_instance()->IsMasterRoutingInstance())
     130           0 :         return NULL;
     131          80 :     return (new PathResolver(this));
     132             : }
     133             : 
     134         100 : DBTableBase *EvpnTable::CreateTable(DB *db, const string &name) {
     135         100 :     EvpnTable *table = new EvpnTable(db, name);
     136         100 :     table->Init();
     137         100 :     return table;
     138             : }
     139             : 
     140             : // Find the route.
     141           0 : EvpnRoute *EvpnTable::FindRoute(const EvpnPrefix &prefix) {
     142           0 :     EvpnRoute rt_key(prefix);
     143             :     DBTablePartition *rtp = static_cast<DBTablePartition *>(
     144           0 :         GetTablePartition(&rt_key));
     145           0 :     return dynamic_cast<EvpnRoute *>(rtp->Find(&rt_key));
     146           0 : }
     147             : 
     148           0 : const EvpnRoute *EvpnTable::FindRoute(const EvpnPrefix &prefix) const {
     149           0 :     EvpnRoute rt_key(prefix);
     150             :     const DBTablePartition *rtp = static_cast<const DBTablePartition *>(
     151           0 :         GetTablePartition(&rt_key));
     152           0 :     return dynamic_cast<const EvpnRoute *>(rtp->Find(&rt_key));
     153           0 : }
     154             : 
     155        9088 : bool EvpnTable::ShouldReplicate(const BgpServer *server,
     156             :                                 const BgpTable *src_table,
     157             :                                 const ExtCommunityPtr community,
     158             :                                 const  EvpnPrefix &evpn_prefix) const {
     159             :     // Always replicate into master table.
     160        9088 :     if (IsMaster())
     161         384 :         return true;
     162             : 
     163             :     // Always replicate Type-5 routes.
     164        8704 :     if (evpn_prefix.type() == EvpnPrefix::IpPrefixRoute)
     165           0 :         return true;
     166             : 
     167             :     // Don't replicate to a VRF from other VRF tables.
     168             :     const EvpnTable *src_evpn_table =
     169        8704 :         dynamic_cast<const EvpnTable *>(src_table);
     170        8704 :     if (!src_evpn_table->IsMaster())
     171        1152 :         return false;
     172             : 
     173             :     // Replicate to VRF from the VPN table if OriginVn matches.
     174       15104 :     if (community->ContainsOriginVn(server->autonomous_system(),
     175        7552 :                        routing_instance()->virtual_network_index())) {
     176           0 :         return true;
     177             :     }
     178             : 
     179             :     // Do not replicate non AD routes as the OriginVN does not match.
     180        7552 :     if (evpn_prefix.type() != EvpnPrefix::AutoDiscoveryRoute)
     181        7552 :         return false;
     182             : 
     183           0 :     string es_target = server->autonomous_system() > 0xffFF ?
     184           0 :         integerToString(EVPN_ES_IMPORT_ROUTE_TARGET_AS4) :
     185           0 :         integerToString(EVPN_ES_IMPORT_ROUTE_TARGET_AS2);
     186             : 
     187             :     // Replicate if AD route target is associated with the route.
     188           0 :     RouteTarget rtarget = RouteTarget::FromString("target:" +
     189           0 :         integerToString(server->autonomous_system()) + ":" + es_target);
     190           0 :     if (community->ContainsRTarget(rtarget.GetExtCommunity()))
     191           0 :         return true;
     192           0 :     return false;
     193           0 : }
     194             : 
     195        9088 : BgpRoute *EvpnTable::RouteReplicate(BgpServer *server,
     196             :         BgpTable *src_table, BgpRoute *src_rt, const BgpPath *src_path,
     197             :         ExtCommunityPtr community) {
     198        9088 :     assert(src_table->family() == Address::EVPN);
     199        9088 :     EvpnRoute *evpn_rt = dynamic_cast<EvpnRoute *>(src_rt);
     200        9088 :     assert(evpn_rt);
     201        9088 :     EvpnPrefix evpn_prefix(evpn_rt->GetPrefix());
     202             : 
     203             :     // Check if this evpn route should be replicated.
     204        9088 :     if (!ShouldReplicate(server, src_table, community, evpn_prefix))
     205        8704 :         return NULL;
     206         384 :     if (evpn_prefix.type() == EvpnPrefix::AutoDiscoveryRoute) {
     207           0 :         if (IsMaster() || evpn_prefix.tag() != EvpnPrefix::kMaxTag)
     208           0 :             return NULL;
     209           0 :         community = server->extcomm_db()->ReplaceRTargetAndLocate(
     210           0 :             community.get(), ExtCommunity::ExtCommunityList());
     211             :     }
     212         384 :     if (evpn_prefix.type() == EvpnPrefix::SegmentRoute)
     213           0 :         return NULL;
     214         768 :     if (evpn_prefix.type() == EvpnPrefix::MacAdvertisementRoute &&
     215         384 :         evpn_prefix.mac_addr().IsBroadcast())
     216           0 :         return NULL;
     217             : 
     218         384 :     BgpAttrDB *attr_db = server->attr_db();
     219         384 :     BgpAttrPtr new_attr(src_path->GetAttr());
     220             : 
     221         384 :     if (IsMaster()) {
     222         384 :         if (evpn_prefix.route_distinguisher().IsZero()) {
     223         384 :             if (new_attr->sub_protocol() == "bgpaas") {
     224           0 :                 boost::system::error_code ec;
     225             :                 Ip4Address addr =
     226           0 :                     Ip4Address::from_string(src_path->GetPeer()->ToString(), ec);
     227           0 :                 if ((ec.value() != 0)) {
     228           0 :                     evpn_prefix.set_route_distinguisher(new_attr->source_rd());
     229             :                 } else {
     230             :                     RouteDistinguisher new_source_rd =
     231           0 :                         RouteDistinguisher(addr.to_ulong(), 0);
     232           0 :                     evpn_prefix.set_route_distinguisher(new_source_rd);
     233             :                 }
     234             :             } else {
     235         384 :                 evpn_prefix.set_route_distinguisher(new_attr->source_rd());
     236             :             }
     237             :         }
     238             :     } else {
     239           0 :         if (evpn_prefix.type() == EvpnPrefix::AutoDiscoveryRoute ||
     240           0 :             evpn_prefix.type() == EvpnPrefix::MacAdvertisementRoute ||
     241           0 :             evpn_prefix.type() == EvpnPrefix::IpPrefixRoute) {
     242           0 :             evpn_prefix.set_route_distinguisher(RouteDistinguisher::kZeroRd);
     243             :         }
     244             :     }
     245         384 :     EvpnRoute rt_key(evpn_prefix);
     246             : 
     247             :     // Find or create the route.
     248             :     DBTablePartition *rtp =
     249         384 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
     250         384 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
     251         384 :     if (dest_route == NULL) {
     252         256 :         dest_route = new EvpnRoute(evpn_prefix);
     253         256 :         rtp->Add(dest_route);
     254             :     } else {
     255         128 :         dest_route->ClearDelete();
     256             :     }
     257             : 
     258         384 :     new_attr = attr_db->ReplaceExtCommunityAndLocate(new_attr.get(), community);
     259             : 
     260             :     // Check whether peer already has a path
     261         384 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     262             :             src_path->GetSource(), src_path->GetPeer(),
     263             :             src_path->GetPathId());
     264         384 :     if (dest_path != NULL) {
     265         256 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     266         128 :             (src_path->GetFlags() != dest_path->GetFlags()) ||
     267         256 :             (src_path->GetLabel() != dest_path->GetLabel()) ||
     268           0 :             (src_path->GetL3Label() != dest_path->GetL3Label())) {
     269         128 :             if (dest_path->NeedsResolution()) {
     270           0 :                 path_resolver()->StopPathResolution(rtp->index(), dest_path);
     271             :             }
     272         128 :             bool success = dest_route->RemoveSecondaryPath(src_rt,
     273             :                 src_path->GetSource(), src_path->GetPeer(),
     274             :                 src_path->GetPathId());
     275         128 :             assert(success);
     276             :         } else {
     277           0 :             return dest_route;
     278             :         }
     279             :     }
     280             : 
     281             :     // Create replicated path and insert it on the route
     282             :     BgpSecondaryPath *replicated_path =
     283         384 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     284         384 :                              src_path->GetSource(), new_attr,
     285         384 :                              src_path->GetFlags(), src_path->GetLabel(),
     286         768 :                              src_path->GetL3Label());
     287         384 :     replicated_path->SetReplicateInfo(src_table, src_rt);
     288             : 
     289             :     // For VPN to VRF replication, start path resolution if fast convergence is
     290             :     // enabled and update path flag to indicate need for resolution.
     291         384 :     if (!IsMaster() && server->IsNextHopCheckEnabled() &&
     292           0 :         (replicated_path->GetSource() == BgpPath::BGP_XMPP)) {
     293           0 :         Address::Family family = src_path->GetAttr()->nexthop_family();
     294           0 :         RoutingInstanceMgr *mgr = server->routing_instance_mgr();
     295           0 :         RoutingInstance *master_ri = mgr->GetDefaultRoutingInstance();
     296           0 :         BgpTable *table = master_ri->GetTable(family);
     297           0 :         replicated_path->SetResolveNextHop();
     298           0 :         path_resolver()->StartPathResolution(dest_route,
     299             :                                              replicated_path, table);
     300             :     }
     301             : 
     302         384 :     dest_route->InsertPath(replicated_path);
     303             : 
     304             :     // Always trigger notification.
     305         384 :     rtp->Notify(dest_route);
     306             : 
     307         384 :     return dest_route;
     308         384 : }
     309             : 
     310        4493 : bool EvpnTable::Export(RibOut *ribout, Route *route,
     311             :         const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     312        4493 :     EvpnRoute *evpn_route = dynamic_cast<EvpnRoute *>(route);
     313        4493 :     assert(evpn_route);
     314             : 
     315        4493 :     if (ribout->IsEncodingBgp()) {
     316        4109 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, evpn_route, peerset);
     317        4109 :         if (!uinfo)
     318        2289 :             return false;
     319        1820 :         uinfo_slist->push_front(*uinfo);
     320        1820 :         return true;
     321             :     }
     322             : 
     323         384 :     const EvpnPrefix &evpn_prefix = evpn_route->GetPrefix();
     324         384 :     if (evpn_prefix.type() != EvpnPrefix::MacAdvertisementRoute &&
     325         384 :             evpn_prefix.type() != EvpnPrefix::IpPrefixRoute &&
     326           0 :             evpn_prefix.type() != EvpnPrefix::SelectiveMulticastRoute) {
     327           0 :         return false;
     328             :     }
     329             : 
     330         768 :     if (!evpn_prefix.mac_addr().IsBroadcast() &&
     331         384 :             (evpn_prefix.type() != EvpnPrefix::SelectiveMulticastRoute)) {
     332         384 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, evpn_route, peerset);
     333         384 :         if (!uinfo)
     334           0 :             return false;
     335         384 :         uinfo_slist->push_front(*uinfo);
     336         384 :         return true;
     337             :     }
     338             : 
     339           0 :     if (!evpn_manager_ || evpn_manager_->deleter()->IsDeleted())
     340           0 :         return false;
     341             : 
     342           0 :     const IPeer *peer = evpn_route->BestPath()->GetPeer();
     343           0 :     if (!peer || !ribout->IsRegistered(const_cast<IPeer *>(peer)))
     344           0 :         return false;
     345             : 
     346           0 :     size_t peerbit = ribout->GetPeerIndex(const_cast<IPeer *>(peer));
     347           0 :     if (!peerset.test(peerbit))
     348           0 :         return false;
     349             : 
     350           0 :     UpdateInfo *uinfo = evpn_manager_->GetUpdateInfo(evpn_route);
     351           0 :     if (!uinfo)
     352           0 :         return false;
     353             : 
     354           0 :     uinfo->target.set(peerbit);
     355           0 :     uinfo_slist->push_front(*uinfo);
     356           0 :     return true;
     357             : }
     358             : 
     359         100 : void EvpnTable::CreateEvpnManager() {
     360         100 :     if (IsVpnTable())
     361          20 :         return;
     362          80 :     assert(!evpn_manager_);
     363          80 :     evpn_manager_ = BgpStaticObjectFactory::Create<EvpnManager>(this);
     364          80 :     evpn_manager_->Initialize();
     365             : }
     366             : 
     367          80 : void EvpnTable::DestroyEvpnManager() {
     368          80 :     assert(evpn_manager_);
     369          80 :     evpn_manager_->Terminate();
     370          80 :     delete evpn_manager_;
     371          80 :     evpn_manager_ = NULL;
     372          80 : }
     373             : 
     374           0 : EvpnManager *EvpnTable::GetEvpnManager() {
     375           0 :     return evpn_manager_;
     376             : }
     377             : 
     378           0 : const EvpnManager *EvpnTable::GetEvpnManager() const {
     379           0 :     return evpn_manager_;
     380             : }
     381             : 
     382         100 : void EvpnTable::set_routing_instance(RoutingInstance *rtinstance) {
     383         100 :     BgpTable::set_routing_instance(rtinstance);
     384         100 :     CreateEvpnManager();
     385         100 : }
     386             : 
     387       30821 : bool EvpnTable::IsMaster() const {
     388       30821 :     return routing_instance()->IsMasterRoutingInstance();
     389             : }
     390             : 
     391           1 : static void RegisterFactory() {
     392           1 :     DB::RegisterFactory("evpn.0", &EvpnTable::CreateTable);
     393           1 : }
     394             : 
     395             : MODULE_INITIALIZER(RegisterFactory);

Generated by: LCOV version 1.14