Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "bgp/inet6/inet6_table.h" 7 : 8 : #include "bgp/bgp_server.h" 9 : #include "bgp/bgp_update.h" 10 : #include "bgp/inet6vpn/inet6vpn_route.h" 11 : #include "bgp/routing-instance/path_resolver.h" 12 : #include "bgp/routing-instance/routing_instance.h" 13 : 14 51139 : Inet6Table::Inet6Table(DB *db, const std::string &name) 15 51139 : : BgpTable(db, name) { 16 51139 : } 17 : 18 5065382 : size_t Inet6Table::HashFunction(const Inet6Prefix &prefix) { 19 5065382 : const Ip6Address::bytes_type &addr_bytes = prefix.ToBytes(); 20 10123196 : return boost::hash_range(addr_bytes.begin(), addr_bytes.end()); 21 : } 22 : 23 196623 : std::unique_ptr<DBEntry> Inet6Table::AllocEntry(const DBRequestKey *key) const { 24 196623 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 25 196623 : return std::unique_ptr<DBEntry> (new Inet6Route(rkey->prefix)); 26 : } 27 : 28 12 : std::unique_ptr<DBEntry> Inet6Table::AllocEntryStr( 29 : const std::string &key_str) const { 30 12 : Inet6Prefix prefix = Inet6Prefix::FromString(key_str); 31 24 : return std::unique_ptr<DBEntry> (new Inet6Route(prefix)); 32 : } 33 : 34 3507106 : size_t Inet6Table::Hash(const DBEntry *entry) const { 35 3507106 : const Inet6Route *route = static_cast<const Inet6Route *>(entry); 36 3507106 : size_t value = HashFunction(route->GetPrefix()); 37 3503705 : return value % DB::PartitionCount(); 38 : } 39 : 40 38132 : size_t Inet6Table::Hash(const DBRequestKey *key) const { 41 38132 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 42 38132 : size_t value = HashFunction(rkey->prefix); 43 38132 : return value % DB::PartitionCount(); 44 : } 45 : 46 27003 : BgpRoute *Inet6Table::TableFind(DBTablePartition *partition, 47 : const DBRequestKey *key) { 48 27003 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 49 27003 : Inet6Route route(rkey->prefix); 50 54020 : return static_cast<BgpRoute *>(partition->Find(&route)); 51 27015 : } 52 : 53 51136 : DBTableBase *Inet6Table::CreateTable(DB *db, const std::string &name) { 54 51136 : Inet6Table *table = new Inet6Table(db, name); 55 51139 : table->Init(); 56 51139 : return table; 57 : } 58 : 59 294224 : BgpRoute *Inet6Table::RouteReplicate(BgpServer *server, BgpTable *src_table, 60 : BgpRoute *src_rt, const BgpPath *path, ExtCommunityPtr community) { 61 294224 : assert((src_table->family() == Address::INET6) || 62 : (src_table->family() == Address::INET6VPN)); 63 : 64 294224 : Inet6Route *source = dynamic_cast<Inet6Route *>(src_rt); 65 : 66 294224 : RouteDistinguisher rd; 67 : 68 294220 : boost::scoped_ptr<Inet6Prefix> prefix; 69 294218 : if (source) { 70 56644 : prefix.reset(new Inet6Prefix(source->GetPrefix().ip6_addr(), 71 28320 : source->GetPrefix().prefixlen())); 72 : } else { 73 265894 : Inet6VpnRoute *vpn_route = dynamic_cast<Inet6VpnRoute *> (src_rt); 74 265894 : assert(vpn_route); 75 265894 : rd = vpn_route->GetPrefix().route_distinguisher(); 76 531757 : prefix.reset(new Inet6Prefix(vpn_route->GetPrefix().addr(), 77 265878 : vpn_route->GetPrefix().prefixlen())); 78 : } 79 : 80 294185 : Inet6Route route(*prefix); 81 : DBTablePartition *partition = 82 294222 : static_cast<DBTablePartition *>(GetTablePartition(&route)); 83 294157 : BgpRoute *dest_route = static_cast<BgpRoute *>(partition->Find(&route)); 84 294220 : if (dest_route == NULL) { 85 167839 : dest_route = new Inet6Route(route.GetPrefix()); 86 167843 : partition->Add(dest_route); 87 : } else { 88 126381 : dest_route->ClearDelete(); 89 : } 90 : 91 : // Replace the extended community with the one provided. 92 294215 : BgpAttrDB *attr_db = server->attr_db(); 93 : BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(path->GetAttr(), 94 294206 : community); 95 : 96 294233 : if (!source) { 97 265911 : new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd); 98 : } 99 : 100 : // Check whether there's already a path with the given peer and path id. 101 : BgpPath *dest_path = 102 294235 : dest_route->FindSecondaryPath(src_rt, path->GetSource(), 103 : path->GetPeer(), path->GetPathId()); 104 294227 : if (dest_path != NULL) { 105 242760 : if ((new_attr != dest_path->GetOriginalAttr()) || 106 242759 : (path->GetFlags() != dest_path->GetFlags()) || 107 105461 : (path->GetLabel() != dest_path->GetLabel())) { 108 : // Update Attributes and notify (if needed) 109 18958 : if (dest_path->NeedsResolution()) { 110 0 : path_resolver()->StopPathResolution(partition->index(), 111 : dest_path); 112 : } 113 18957 : assert(dest_route->RemoveSecondaryPath(src_rt, path->GetSource(), 114 : path->GetPeer(), path->GetPathId())); 115 : } else { 116 105440 : return dest_route; 117 : } 118 : } 119 : 120 : BgpSecondaryPath *replicated_path = 121 188786 : new BgpSecondaryPath(path->GetPeer(), path->GetPathId(), 122 188786 : path->GetSource(), new_attr, path->GetFlags(), path->GetLabel()); 123 188791 : replicated_path->SetReplicateInfo(src_table, src_rt); 124 : 125 : // For VPN to VRF replication, start path resolution if fast convergence is 126 : // enabled and update path flag to indicate need for resolution. 127 188789 : if (!source && (server->IsNextHopCheckEnabled()) && 128 0 : (replicated_path->GetSource() == BgpPath::BGP_XMPP)) { 129 0 : Address::Family family = replicated_path->GetAttr()->nexthop_family(); 130 0 : RoutingInstanceMgr *mgr = server->routing_instance_mgr(); 131 0 : RoutingInstance *master_ri = mgr->GetDefaultRoutingInstance(); 132 0 : BgpTable *table = master_ri->GetTable(family); 133 0 : replicated_path->SetResolveNextHop(); 134 0 : path_resolver()->StartPathResolution(dest_route, replicated_path, 135 : table); 136 : } 137 : 138 188786 : dest_route->InsertPath(replicated_path); 139 : 140 : // Notify the route even if the best path may not have changed. For XMPP 141 : // peers, we support sending multiple ECMP next-hops for a single route. 142 : // 143 : // TODO(ananth): Can be optimized for changes that does not result in 144 : // any change to ECMP list 145 188786 : partition->Notify(dest_route); 146 : 147 188785 : return dest_route; 148 294225 : } 149 : 150 217678 : bool Inet6Table::Export(RibOut *ribout, Route *route, const RibPeerSet &peerset, 151 : UpdateInfoSList &uinfo_slist) { 152 217678 : BgpRoute *bgp_route = static_cast<BgpRoute *> (route); 153 217678 : UpdateInfo *uinfo = GetUpdateInfo(ribout, bgp_route, peerset); 154 217716 : if (!uinfo) { 155 4486 : return false; 156 : } 157 : 158 213230 : if (ribout->ExportPolicy().encoding == RibExportPolicy::BGP) { 159 4549 : BgpAttrDB *attr_db = routing_instance()->server()->attr_db(); 160 : // Strip ExtCommunity. 161 4548 : if (uinfo->roattr.attr()->ext_community()) { 162 : BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate( 163 1676 : uinfo->roattr.attr(), NULL); 164 1676 : uinfo->roattr.set_attr(this, new_attr); 165 1676 : } 166 : 167 : // Strip OriginVnPath. 168 4548 : if (uinfo->roattr.attr()->origin_vn_path()) { 169 : BgpAttrPtr new_attr = attr_db->ReplaceOriginVnPathAndLocate( 170 0 : uinfo->roattr.attr(), NULL); 171 0 : uinfo->roattr.set_attr(this, new_attr); 172 0 : } 173 : } 174 213230 : uinfo_slist->push_front(*uinfo); 175 : 176 213231 : return true; 177 : } 178 : 179 42907 : PathResolver *Inet6Table::CreatePathResolver() { 180 42907 : if (routing_instance()->IsMasterRoutingInstance()) 181 0 : return NULL; 182 42911 : return (new PathResolver(this)); 183 : } 184 : 185 159 : static void RegisterFactory() { 186 159 : DB::RegisterFactory("inet6.0", &Inet6Table::CreateTable); 187 159 : } 188 : MODULE_INITIALIZER(RegisterFactory);