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: 138 237 58.2 %
Date: 2026-11-23 02:04:52 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       58398 : size_t EvpnTable::HashFunction(const EvpnPrefix &prefix) {
      23       58398 :     if (prefix.type() == EvpnPrefix::MacAdvertisementRoute) {
      24           0 :         if (prefix.mac_addr().IsBroadcast())
      25           0 :             return 0;
      26           0 :         const uint8_t *data = prefix.mac_addr().GetData();
      27           0 :         uint32_t value = get_value(data + 2, 4);
      28           0 :         return boost::hash_value(value);
      29             :     }
      30       58380 :     if (prefix.type() == EvpnPrefix::IpPrefixRoute) {
      31       58350 :         if (prefix.ip_address().is_v4()) {
      32       36619 :             return InetTable::HashFunction(prefix.inet_prefix());
      33             :         } else {
      34       21926 :             return Inet6Table::HashFunction(prefix.inet6_prefix());
      35             :         }
      36             :     }
      37           0 :     return 0;
      38             : }
      39             : 
      40         102 : EvpnTable::EvpnTable(DB *db, const string &name)
      41         102 :     : BgpTable(db, name), evpn_manager_(NULL) {
      42         102 :     mac_route_count_ = 0;
      43         102 :     unique_mac_route_count_ = 0;
      44         102 :     im_route_count_ = 0;
      45         102 :     ip_route_count_ = 0;
      46         102 : }
      47             : 
      48        4442 : unique_ptr<DBEntry> EvpnTable::AllocEntry(
      49             :         const DBRequestKey *key) const {
      50        4442 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(key);
      51        4442 :     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        2690 : void EvpnTable::AddRemoveCallback(const DBEntryBase *entry, bool add) const {
      61        2690 :     if (IsVpnTable())
      62        1922 :         return;
      63         768 :     const EvpnRoute *evpn_rt = static_cast<const EvpnRoute *>(entry);
      64         768 :     const EvpnPrefix &evpn_prefix = evpn_rt->GetPrefix();
      65         768 :     switch (evpn_prefix.type()) {
      66           0 :     case EvpnPrefix::MacAdvertisementRoute:
      67             :         // Ignore Broadcast MAC routes.
      68           0 :         if (evpn_prefix.mac_addr().IsBroadcast())
      69           0 :             break;
      70             : 
      71           0 :         if (add) {
      72           0 :             mac_route_count_++;
      73             :         } else {
      74           0 :             mac_route_count_--;
      75             :         }
      76             : 
      77             :         // Ignore MAC routes with IP addresses.
      78           0 :         if (evpn_prefix.family() != Address::UNSPEC)
      79           0 :             break;
      80             : 
      81           0 :         if (add) {
      82           0 :             unique_mac_route_count_++;
      83             :         } else {
      84           0 :             unique_mac_route_count_--;
      85             :         }
      86           0 :         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         768 :     case EvpnPrefix::IpPrefixRoute:
      97         768 :         if (add) {
      98         384 :             ip_route_count_++;
      99             :         } else {
     100         384 :             ip_route_count_--;
     101             :         }
     102         768 :         break;
     103             : 
     104           0 :     default:
     105           0 :         break;
     106             :     }
     107             : }
     108             : 
     109        2998 : size_t EvpnTable::Hash(const DBRequestKey *key) const {
     110        2998 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
     111        2998 :     size_t value = HashFunction(rkey->prefix);
     112        2998 :     return value % DB::PartitionCount();
     113             : }
     114             : 
     115       55447 : size_t EvpnTable::Hash(const DBEntry *entry) const {
     116       55447 :     const EvpnRoute *rt_entry = static_cast<const EvpnRoute *>(entry);
     117       55447 :     size_t value = HashFunction(rt_entry->GetPrefix());
     118       55555 :     return value % DB::PartitionCount();
     119             : }
     120             : 
     121        2998 : BgpRoute *EvpnTable::TableFind(DBTablePartition *rtp,
     122             :         const DBRequestKey *prefix) {
     123        2998 :     const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix);
     124        2998 :     EvpnRoute rt_key(pfxkey->prefix);
     125        5996 :     return static_cast<BgpRoute *>(rtp->Find(&rt_key));
     126        2998 : }
     127             : 
     128          34 : PathResolver *EvpnTable::CreatePathResolver() {
     129          34 :     if (routing_instance()->IsMasterRoutingInstance())
     130           0 :         return NULL;
     131          34 :     return (new PathResolver(this));
     132             : }
     133             : 
     134         102 : DBTableBase *EvpnTable::CreateTable(DB *db, const string &name) {
     135         102 :     EvpnTable *table = new EvpnTable(db, name);
     136         102 :     table->Init();
     137         102 :     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        1347 : 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        1347 :     if (IsMaster())
     161        1347 :         return true;
     162             : 
     163             :     // Always replicate Type-5 routes.
     164           0 :     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           0 :         dynamic_cast<const EvpnTable *>(src_table);
     170           0 :     if (!src_evpn_table->IsMaster())
     171           0 :         return false;
     172             : 
     173             :     // Replicate to VRF from the VPN table if OriginVn matches.
     174           0 :     if (community->ContainsOriginVn(server->autonomous_system(),
     175           0 :                        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           0 :     if (evpn_prefix.type() != EvpnPrefix::AutoDiscoveryRoute)
     181           0 :         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        1348 : BgpRoute *EvpnTable::RouteReplicate(BgpServer *server,
     196             :         BgpTable *src_table, BgpRoute *src_rt, const BgpPath *src_path,
     197             :         ExtCommunityPtr community) {
     198        1348 :     assert(src_table->family() == Address::EVPN);
     199        1348 :     EvpnRoute *evpn_rt = dynamic_cast<EvpnRoute *>(src_rt);
     200        1348 :     assert(evpn_rt);
     201        1348 :     EvpnPrefix evpn_prefix(evpn_rt->GetPrefix());
     202             : 
     203             :     // Check if this evpn route should be replicated.
     204        1348 :     if (!ShouldReplicate(server, src_table, community, evpn_prefix))
     205           0 :         return NULL;
     206        1348 :     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        1348 :     if (evpn_prefix.type() == EvpnPrefix::SegmentRoute)
     213           0 :         return NULL;
     214        1348 :     if (evpn_prefix.type() == EvpnPrefix::MacAdvertisementRoute &&
     215           0 :         evpn_prefix.mac_addr().IsBroadcast())
     216           0 :         return NULL;
     217             : 
     218        1348 :     BgpAttrDB *attr_db = server->attr_db();
     219        1348 :     BgpAttrPtr new_attr(src_path->GetAttr());
     220             : 
     221        1348 :     if (IsMaster()) {
     222        1348 :         if (evpn_prefix.route_distinguisher().IsZero()) {
     223        1348 :             if (new_attr->sub_protocol() == "bgpaas") {
     224        1188 :                 boost::system::error_code ec;
     225             :                 Ip4Address addr =
     226        1188 :                     Ip4Address::from_string(src_path->GetPeer()->ToString(), ec);
     227        1187 :                 if ((ec.value() != 0)) {
     228           0 :                     evpn_prefix.set_route_distinguisher(new_attr->source_rd());
     229             :                 } else {
     230             :                     RouteDistinguisher new_source_rd =
     231        1187 :                         RouteDistinguisher(addr.to_ulong(), 0);
     232        1188 :                     evpn_prefix.set_route_distinguisher(new_source_rd);
     233             :                 }
     234             :             } else {
     235         160 :                 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        1348 :     EvpnRoute rt_key(evpn_prefix);
     246             : 
     247             :     // Find or create the route.
     248             :     DBTablePartition *rtp =
     249        1348 :         static_cast<DBTablePartition *>(GetTablePartition(&rt_key));
     250        1347 :     BgpRoute *dest_route = static_cast<BgpRoute *>(rtp->Find(&rt_key));
     251        1348 :     if (dest_route == NULL) {
     252         489 :         dest_route = new EvpnRoute(evpn_prefix);
     253         489 :         rtp->Add(dest_route);
     254             :     } else {
     255         859 :         dest_route->ClearDelete();
     256             :     }
     257             : 
     258        1348 :     new_attr = attr_db->ReplaceExtCommunityAndLocate(new_attr.get(), community);
     259             : 
     260             :     // Check whether peer already has a path
     261        1348 :     BgpPath *dest_path = dest_route->FindSecondaryPath(src_rt,
     262             :             src_path->GetSource(), src_path->GetPeer(),
     263             :             src_path->GetPathId());
     264        1348 :     if (dest_path != NULL) {
     265        1590 :         if ((new_attr != dest_path->GetOriginalAttr()) ||
     266        1311 :             (src_path->GetFlags() != dest_path->GetFlags()) ||
     267        2106 :             (src_path->GetLabel() != dest_path->GetLabel()) ||
     268         516 :             (src_path->GetL3Label() != dest_path->GetL3Label())) {
     269         279 :             if (dest_path->NeedsResolution()) {
     270           0 :                 path_resolver()->StopPathResolution(rtp->index(), dest_path);
     271             :             }
     272         279 :             bool success = dest_route->RemoveSecondaryPath(src_rt,
     273             :                 src_path->GetSource(), src_path->GetPeer(),
     274             :                 src_path->GetPathId());
     275         279 :             assert(success);
     276             :         } else {
     277         516 :             return dest_route;
     278             :         }
     279             :     }
     280             : 
     281             :     // Create replicated path and insert it on the route
     282             :     BgpSecondaryPath *replicated_path =
     283         832 :         new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(),
     284         832 :                              src_path->GetSource(), new_attr,
     285         832 :                              src_path->GetFlags(), src_path->GetLabel(),
     286        1664 :                              src_path->GetL3Label());
     287         832 :     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         832 :     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         832 :     dest_route->InsertPath(replicated_path);
     303             : 
     304             :     // Always trigger notification.
     305         832 :     rtp->Notify(dest_route);
     306             : 
     307         832 :     return dest_route;
     308        1348 : }
     309             : 
     310        5474 : bool EvpnTable::Export(RibOut *ribout, Route *route,
     311             :         const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) {
     312        5474 :     EvpnRoute *evpn_route = dynamic_cast<EvpnRoute *>(route);
     313        5474 :     assert(evpn_route);
     314             : 
     315        5474 :     if (ribout->IsEncodingBgp()) {
     316        3769 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, evpn_route, peerset);
     317        3820 :         if (!uinfo)
     318        2276 :             return false;
     319        1544 :         uinfo_slist->push_front(*uinfo);
     320        1543 :         return true;
     321             :     }
     322             : 
     323        1736 :     const EvpnPrefix &evpn_prefix = evpn_route->GetPrefix();
     324        3468 :     if (evpn_prefix.type() != EvpnPrefix::MacAdvertisementRoute &&
     325        3468 :             evpn_prefix.type() != EvpnPrefix::IpPrefixRoute &&
     326           0 :             evpn_prefix.type() != EvpnPrefix::SelectiveMulticastRoute) {
     327           0 :         return false;
     328             :     }
     329             : 
     330        3468 :     if (!evpn_prefix.mac_addr().IsBroadcast() &&
     331        1734 :             (evpn_prefix.type() != EvpnPrefix::SelectiveMulticastRoute)) {
     332        1734 :         UpdateInfo *uinfo = GetUpdateInfo(ribout, evpn_route, peerset);
     333        1734 :         if (!uinfo)
     334         409 :             return false;
     335        1325 :         uinfo_slist->push_front(*uinfo);
     336        1325 :         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         102 : void EvpnTable::CreateEvpnManager() {
     360         102 :     if (IsVpnTable())
     361          68 :         return;
     362          34 :     assert(!evpn_manager_);
     363          34 :     evpn_manager_ = BgpStaticObjectFactory::Create<EvpnManager>(this);
     364          34 :     evpn_manager_->Initialize();
     365             : }
     366             : 
     367          34 : void EvpnTable::DestroyEvpnManager() {
     368          34 :     assert(evpn_manager_);
     369          34 :     evpn_manager_->Terminate();
     370          34 :     delete evpn_manager_;
     371          34 :     evpn_manager_ = NULL;
     372          34 : }
     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         102 : void EvpnTable::set_routing_instance(RoutingInstance *rtinstance) {
     383         102 :     BgpTable::set_routing_instance(rtinstance);
     384         102 :     CreateEvpnManager();
     385         102 : }
     386             : 
     387       10772 : bool EvpnTable::IsMaster() const {
     388       10772 :     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