Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/inet6vpn/inet6vpn_table.h" 6 : 7 : #include "bgp/ipeer.h" 8 : #include "bgp/bgp_server.h" 9 : #include "bgp/bgp_peer.h" 10 : #include "bgp/bgp_update.h" 11 : #include "bgp/inet6/inet6_table.h" 12 : #include "bgp/routing-instance/routing_instance.h" 13 : #include "net/rd.h" 14 : 15 8228 : Inet6VpnTable::Inet6VpnTable(DB *db, const std::string &name) 16 8228 : : BgpTable(db, name) { 17 8228 : } 18 : 19 : std::unique_ptr<DBEntry> 20 100473 : Inet6VpnTable::AllocEntry(const DBRequestKey *key) const { 21 100473 : const RequestKey *pfxkey = static_cast<const RequestKey *>(key); 22 100473 : return std::unique_ptr<DBEntry>(new Inet6VpnRoute(pfxkey->prefix)); 23 : } 24 : 25 : std::unique_ptr<DBEntry> 26 20 : Inet6VpnTable::AllocEntryStr(const std::string &key_str) const { 27 20 : Inet6VpnPrefix vpn_prefix = Inet6VpnPrefix::FromString(key_str); 28 40 : return std::unique_ptr<DBEntry> (new Inet6VpnRoute(vpn_prefix)); 29 : } 30 : 31 1222827 : size_t Inet6VpnTable::Hash(const DBEntry *entry) const { 32 1222827 : const Inet6VpnRoute *vpn_route = static_cast<const Inet6VpnRoute *>(entry); 33 1222827 : const Inet6VpnPrefix &vpn_prefix = vpn_route->GetPrefix(); 34 1222798 : Inet6Prefix prefix(vpn_prefix.addr(), vpn_prefix.prefixlen()); 35 1222516 : size_t value = Inet6Table::HashFunction(prefix); 36 1222010 : return value % DB::PartitionCount(); 37 : } 38 : 39 64795 : size_t Inet6VpnTable::Hash(const DBRequestKey *key) const { 40 64795 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 41 64795 : Inet6Prefix prefix(rkey->prefix.addr(), rkey->prefix.prefixlen()); 42 64795 : size_t value = Inet6Table::HashFunction(prefix); 43 64796 : return value % DB::PartitionCount(); 44 : } 45 : 46 63028 : BgpRoute *Inet6VpnTable::TableFind(DBTablePartition *rtp, 47 : const DBRequestKey *prefix) { 48 63028 : const RequestKey *pfxkey = static_cast<const RequestKey *>(prefix); 49 63028 : Inet6VpnRoute vpn_route(pfxkey->prefix); 50 126039 : return static_cast<BgpRoute *>(rtp->Find(&vpn_route)); 51 63054 : } 52 : 53 8228 : DBTableBase *Inet6VpnTable::CreateTable(DB *db, const std::string &name) { 54 8228 : Inet6VpnTable *table = new Inet6VpnTable(db, name); 55 8228 : table->Init(); 56 8228 : return table; 57 : } 58 : 59 20347 : static RouteDistinguisher GenerateDistinguisher( 60 : const BgpTable *src_table, const BgpPath *src_path) { 61 20347 : const RouteDistinguisher &source_rd = src_path->GetAttr()->source_rd(); 62 20346 : if (!src_path->GetPeer() || !(src_path->GetPeer()->IsRouterTypeBGPaaS())){ 63 19104 : if (!source_rd.IsZero()) 64 11446 : return source_rd; 65 : 66 7658 : assert(!src_path->GetPeer() || !src_path->GetPeer()->IsXmppPeer()); 67 7658 : const RoutingInstance *src_instance = src_table->routing_instance(); 68 7658 : return *src_instance->GetRD(); 69 : } 70 1242 : uint16_t peer_port_id = 0; 71 1242 : as_t asn_num = src_path->GetAttr()->neighbor_as(); 72 : 73 1242 : std::string ri_name = src_table->routing_instance()->name(); 74 2484 : for (const BgpPeer *peer = src_path->GetPeer()->server()->FindNextPeer(); peer != NULL; 75 1242 : peer = src_path->GetPeer()->server()->FindNextPeer(peer->peer_name())) { 76 3726 : if ((peer->peer_name().find(ri_name)!= std::string::npos)&& 77 3726 : (peer->peer_address_string().find(src_path->GetPeer()->ToString())!= std::string::npos)){ 78 1242 : peer_port_id = peer->peer_port(); 79 1242 : asn_num = peer->peer_as(); 80 1242 : break; 81 : } 82 : } 83 1242 : RouteDistinguisher new_source_rd = RouteDistinguisher(true, asn_num, peer_port_id); 84 1242 : return new_source_rd; 85 1242 : } 86 : 87 20348 : BgpRoute *Inet6VpnTable::RouteReplicate(BgpServer *server, BgpTable *src_table, 88 : BgpRoute *source_rt, const BgpPath *src_path, 89 : ExtCommunityPtr community) { 90 20348 : assert(src_table->family() == Address::INET6); 91 : 92 20347 : Inet6Route *src_rt = dynamic_cast<Inet6Route *>(source_rt); 93 20347 : assert(src_rt); 94 : 95 20347 : const RouteDistinguisher &rd = GenerateDistinguisher(src_table, src_path); 96 20346 : Inet6VpnPrefix vpn_prefix(rd, src_rt->GetPrefix().ip6_addr(), 97 40692 : src_rt->GetPrefix().prefixlen()); 98 : 99 20346 : Inet6VpnRoute vpn_route(vpn_prefix); 100 : DBTablePartition *partition = 101 20346 : static_cast<DBTablePartition *>(GetTablePartition(&vpn_route)); 102 20344 : BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&vpn_route)); 103 20347 : if (dest_route == NULL) { 104 12216 : dest_route = new Inet6VpnRoute(vpn_prefix); 105 12214 : partition->Add(dest_route); 106 : } else { 107 8131 : dest_route->ClearDelete(); 108 : } 109 : 110 20347 : BgpAttrDB *attr_db = server->attr_db(); 111 : 112 : BgpAttrPtr new_attr = 113 : server->attr_db()->ReplaceExtCommunityAndLocate(src_path->GetAttr(), 114 20347 : community); 115 20348 : new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd); 116 : 117 : // Check whether there's already a path with the given peer and path id. 118 : BgpPath *dest_path = 119 20348 : dest_route->FindSecondaryPath(source_rt, src_path->GetSource(), 120 : src_path->GetPeer(), 121 : src_path->GetPathId()); 122 20348 : if (dest_path != NULL) { 123 14135 : if ((new_attr != dest_path->GetOriginalAttr()) || 124 14135 : (src_path->GetFlags() != dest_path->GetFlags()) || 125 3803 : (src_path->GetLabel() != dest_path->GetLabel())) { 126 : // Update Attributes and notify (if needed) 127 4047 : assert(dest_route->RemoveSecondaryPath(source_rt, 128 : src_path->GetSource(), src_path->GetPeer(), 129 : src_path->GetPathId())); 130 : } else { 131 3744 : return dest_route; 132 : } 133 : } 134 : 135 : // Create replicated path and insert it on the route 136 : BgpSecondaryPath *replicated_path = 137 16604 : new BgpSecondaryPath(src_path->GetPeer(), src_path->GetPathId(), 138 16604 : src_path->GetSource(), new_attr, 139 16604 : src_path->GetFlags(), src_path->GetLabel()); 140 16604 : replicated_path->SetReplicateInfo(src_table, source_rt); 141 16604 : dest_route->InsertPath(replicated_path); 142 : 143 : // Always trigger notification. 144 16603 : partition->Notify(dest_route); 145 : 146 16603 : return dest_route; 147 20347 : } 148 : 149 119248 : bool Inet6VpnTable::Export(RibOut *ribout, Route *route, 150 : const RibPeerSet &peerset, UpdateInfoSList &uinfo_slist) { 151 119248 : BgpRoute *bgp_route = static_cast<BgpRoute *> (route); 152 119248 : UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset); 153 119277 : if (!uinfo) { 154 53235 : return false; 155 : } 156 66042 : uinfo_slist->push_front(*uinfo); 157 : 158 66051 : return true; 159 : } 160 : 161 159 : static void RegisterFactory() { 162 159 : DB::RegisterFactory("bgp.l3vpn-inet6.0", &Inet6VpnTable::CreateTable); 163 159 : } 164 : MODULE_INITIALIZER(RegisterFactory);