akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 22 | #include "name-prefix-table.hpp" |
| 23 | |
| 24 | #include "logger.hpp" |
| 25 | #include "nlsr.hpp" |
| 26 | #include "routing-table.hpp" |
| 27 | |
| 28 | #include <algorithm> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <utility> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
| 33 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 34 | INIT_LOGGER(route.NamePrefixTable); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 35 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 36 | NamePrefixTable::NamePrefixTable(const ndn::Name& ownRouterName, Fib& fib, |
| 37 | RoutingTable& routingTable, |
| 38 | AfterRoutingChange& afterRoutingChangeSignal, |
| 39 | Lsdb::AfterLsdbModified& afterLsdbModifiedSignal) |
| 40 | : m_ownRouterName(ownRouterName) |
| 41 | , m_fib(fib) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 42 | , m_routingTable(routingTable) |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 43 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 44 | m_afterRoutingChangeConnection = afterRoutingChangeSignal.connect( |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 45 | [this] (const std::list<RoutingTableEntry>& entries) { |
| 46 | updateWithNewRoute(entries); |
| 47 | }); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 48 | |
| 49 | m_afterLsdbModified = afterLsdbModifiedSignal.connect( |
| 50 | [this] (std::shared_ptr<Lsa> lsa, LsdbUpdate updateType, |
| 51 | const auto& namesToAdd, const auto& namesToRemove) { |
| 52 | updateFromLsdb(lsa, updateType, namesToAdd, namesToRemove); |
| 53 | } |
| 54 | ); |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | NamePrefixTable::~NamePrefixTable() |
| 58 | { |
| 59 | m_afterRoutingChangeConnection.disconnect(); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 60 | m_afterLsdbModified.disconnect(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | NamePrefixTable::updateFromLsdb(std::shared_ptr<Lsa> lsa, LsdbUpdate updateType, |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 65 | const std::list<nlsr::PrefixInfo>& namesToAdd, |
| 66 | const std::list<nlsr::PrefixInfo>& namesToRemove) |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 67 | { |
| 68 | if (m_ownRouterName == lsa->getOriginRouter()) { |
| 69 | return; |
| 70 | } |
| 71 | NLSR_LOG_TRACE("Got update from Lsdb for router: " << lsa->getOriginRouter()); |
| 72 | |
| 73 | if (updateType == LsdbUpdate::INSTALLED) { |
| 74 | addEntry(lsa->getOriginRouter(), lsa->getOriginRouter()); |
| 75 | |
| 76 | if (lsa->getType() == Lsa::Type::NAME) { |
| 77 | auto nlsa = std::static_pointer_cast<NameLsa>(lsa); |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 78 | for (const auto &prefix : nlsa->getNpl().getPrefixInfo()) { |
| 79 | if (prefix.getName() != m_ownRouterName) { |
| 80 | m_nexthopCost[DestNameKey(lsa->getOriginRouter(), prefix.getName())] = prefix.getCost(); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 81 | // Don't use capture flag on advertised prefixes... |
| 82 | addEntry(prefix.getName(), lsa->getOriginRouter(), ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | else if (updateType == LsdbUpdate::UPDATED) { |
| 88 | if (lsa->getType() != Lsa::Type::NAME) { |
| 89 | return; |
| 90 | } |
| 91 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 92 | for (const auto &prefix : namesToAdd) { |
| 93 | if (prefix.getName() != m_ownRouterName) { |
| 94 | m_nexthopCost[DestNameKey(lsa->getOriginRouter(), prefix.getName())] = prefix.getCost(); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 95 | // Don't use capture flag on advertised prefixes... |
| 96 | addEntry(prefix.getName(), lsa->getOriginRouter(), ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 100 | for (const auto &prefix : namesToRemove) { |
| 101 | if (prefix.getName() != m_ownRouterName) { |
| 102 | m_nexthopCost.erase(m_nexthopCost.find(DestNameKey(lsa->getOriginRouter(), prefix.getName()))); |
| 103 | removeEntry(prefix.getName(), lsa->getOriginRouter()); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | else { |
| 108 | removeEntry(lsa->getOriginRouter(), lsa->getOriginRouter()); |
| 109 | if (lsa->getType() == Lsa::Type::NAME) { |
| 110 | auto nlsa = std::static_pointer_cast<NameLsa>(lsa); |
| 111 | for (const auto& name : nlsa->getNpl().getNames()) { |
| 112 | if (name != m_ownRouterName) { |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 113 | m_nexthopCost.erase(m_nexthopCost.find(DestNameKey(lsa->getOriginRouter(), name))); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 114 | removeEntry(name, lsa->getOriginRouter()); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 119 | } |
| 120 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 121 | NexthopList |
| 122 | NamePrefixTable::adjustNexthopCosts(const NexthopList& nhlist, const ndn::Name& nameToCheck, const ndn::Name& destRouterName) |
| 123 | { |
| 124 | NexthopList new_nhList; |
| 125 | for (const auto& nh : nhlist.getNextHops()) { |
| 126 | const NextHop newNextHop = NextHop(nh.getConnectingFaceUri(), nh.getRouteCost() + |
| 127 | m_nexthopCost[DestNameKey(destRouterName, nameToCheck)]); |
| 128 | new_nhList.addNextHop(newNextHop); |
| 129 | } |
| 130 | return new_nhList; |
| 131 | } |
| 132 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | void |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 134 | NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter, uint64_t routeFlags) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 136 | // Check if the advertised name prefix is in the table already. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 137 | auto nameItr = std::find_if(m_table.begin(), m_table.end(), |
| 138 | [&] (const auto& entry) { return name == entry->getNamePrefix(); }); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 139 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 140 | // Attempt to find a routing table pool entry (RTPE) we can use. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 141 | auto rtpeItr = m_rtpool.find(destRouter); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 142 | |
| 143 | // These declarations just to make the compiler happy... |
| 144 | RoutingTablePoolEntry rtpe; |
| 145 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr(nullptr); |
| 146 | |
| 147 | // There isn't currently a routing table entry in the pool for this name |
| 148 | if (rtpeItr == m_rtpool.end()) { |
| 149 | // See if there is a routing table entry available we could use |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 150 | RoutingTableEntry* routeEntryPtr = m_routingTable.findRoutingTableEntry(destRouter); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 151 | |
| 152 | // We have to create a new routing table entry |
| 153 | if (routeEntryPtr == nullptr) { |
| 154 | rtpe = RoutingTablePoolEntry(destRouter, 0); |
| 155 | } |
| 156 | // There was already a usable one in the routing table |
| 157 | else { |
| 158 | rtpe = RoutingTablePoolEntry(*routeEntryPtr, 0); |
| 159 | } |
| 160 | |
| 161 | // Add the new pool object to the pool. |
| 162 | rtpePtr = addRtpeToPool(rtpe); |
| 163 | } |
| 164 | // There was one already, so just fetch that one. |
| 165 | else { |
| 166 | rtpePtr = (*rtpeItr).second; |
| 167 | } |
| 168 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 169 | std::shared_ptr<NamePrefixTableEntry> npte; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 170 | // Either we have to make a new NPT entry or there already was one. |
| 171 | if (nameItr == m_table.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 172 | NLSR_LOG_DEBUG("Adding origin: " << rtpePtr->getDestination() |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 173 | << " to a new name prefix: " << name); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 174 | |
| 175 | npte = std::make_shared<NamePrefixTableEntry>(name, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 176 | npte->addRoutingTableEntry(rtpePtr); |
| 177 | npte->generateNhlfromRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 178 | m_table.push_back(npte); |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 179 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 180 | // If this entry has next hops, we need to inform the FIB |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 181 | if (npte->getNexthopList().size() > 0) { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 182 | NLSR_LOG_TRACE("Updating FIB with next hops for " << npte->getNamePrefix()); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 183 | m_fib.update(name, adjustNexthopCosts(npte->getNexthopList(), name, destRouter), npte->getFlags()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 184 | } |
| 185 | // The routing table may recalculate and add a routing table entry |
| 186 | // with no next hops to replace an existing routing table entry. In |
| 187 | // this case, the name prefix is no longer reachable through a next |
| 188 | // hop and should be removed from the FIB. But, the prefix should |
| 189 | // remain in the Name Prefix Table as a future routing table |
| 190 | // calculation may add next hops. |
| 191 | else { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 192 | NLSR_LOG_TRACE(npte->getNamePrefix() << " has no next hops; removing from FIB"); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 193 | m_fib.remove(name); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 194 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | else { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 197 | npte = *nameItr; |
Ashlesh Gawande | e5002b3 | 2018-12-20 21:07:31 -0600 | [diff] [blame] | 198 | NLSR_LOG_TRACE("Adding origin: " << rtpePtr->getDestination() << |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 199 | " to existing prefix: " << *npte); |
| 200 | // We should not downgrade the capture flag to child-inherit unless there are no nexthops |
| 201 | if (npte->getFlags() != routeFlags && npte->getNexthopList().size() == 0) { |
| 202 | npte->setFlags(routeFlags); |
| 203 | } |
| 204 | npte->addRoutingTableEntry(rtpePtr); |
| 205 | npte->generateNhlfromRteList(); |
| 206 | if (npte->getNexthopList().size() > 0) { |
| 207 | NLSR_LOG_TRACE("Updating FIB with next hops for " << *npte); |
| 208 | m_fib.update(name, adjustNexthopCosts(npte->getNexthopList(), name, destRouter), npte->getFlags()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 209 | } |
| 210 | else { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 211 | NLSR_LOG_TRACE(npte->getNamePrefix() << " has no next hops; removing from FIB"); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 212 | m_fib.remove(name); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 213 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 215 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 216 | // Add the reference to this NPT to the RTPE. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 217 | rtpePtr->namePrefixTableEntries.try_emplace(npte->getNamePrefix(), |
| 218 | std::weak_ptr<NamePrefixTableEntry>(npte)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 222 | NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 223 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 224 | NLSR_LOG_DEBUG("Removing origin: " << destRouter << " from " << name); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 225 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 226 | // Fetch an iterator to the appropriate pair object in the pool. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 227 | auto rtpeItr = m_rtpool.find(destRouter); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 228 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 229 | // Simple error checking to prevent any unusual behavior in the case |
| 230 | // that we try to remove an entry that isn't there. |
| 231 | if (rtpeItr == m_rtpool.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 232 | NLSR_LOG_DEBUG("No entry for origin: " << destRouter |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 233 | << " found, so it cannot be removed from prefix: " << name); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = rtpeItr->second; |
| 237 | |
| 238 | // Ensure that the entry exists |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 239 | auto nameItr = std::find_if(m_table.begin(), m_table.end(), |
| 240 | [&] (const auto& entry) { return entry->getNamePrefix() == name; }); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 241 | if (nameItr != m_table.end()) { |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 242 | std::shared_ptr<NamePrefixTableEntry> npte = *nameItr; |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 243 | NLSR_LOG_TRACE("Removing origin: " << rtpePtr->getDestination() |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 244 | << " from prefix: " << **nameItr); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 245 | |
| 246 | // Rather than iterating through the whole list periodically, just |
| 247 | // delete them here if they have no references. |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 248 | if (npte->removeRoutingTableEntry(rtpePtr) == 0) { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 249 | deleteRtpeFromPool(rtpePtr); |
| 250 | } |
| 251 | |
| 252 | // If the prefix is a router prefix and it does not have any other |
| 253 | // routing table entries, the Adjacency/Coordinate LSA associated |
| 254 | // with that origin router has been removed from the LSDB and so |
| 255 | // the router prefix should be removed from the Name Prefix Table. |
| 256 | // |
| 257 | // If the prefix is an advertised name prefix: If another router |
| 258 | // advertises this name prefix, the RteList should have another |
| 259 | // entry for that router; the next hops should be recalculated |
| 260 | // and installed in the FIB. |
| 261 | // |
| 262 | // If no other router advertises this name prefix, the RteList |
| 263 | // should be empty and the prefix can be removed from the Name |
| 264 | // Prefix Table. Once a new Name LSA advertises this prefix, a |
| 265 | // new entry for the prefix will be created. |
| 266 | // |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 267 | if (npte->getRteListSize() == 0) { |
| 268 | NLSR_LOG_TRACE(*npte << " has no routing table entries;" |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 269 | << " removing from table and FIB"); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 270 | m_table.erase(nameItr); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 271 | m_fib.remove(name); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 272 | } |
| 273 | else { |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 274 | NLSR_LOG_TRACE(*npte << " has other routing table entries;" |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 275 | << " updating FIB with next hops"); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 276 | (*nameItr)->generateNhlfromRteList(); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 277 | m_fib.update(name, adjustNexthopCosts(npte->getNexthopList(), name, destRouter), npte->getFlags()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 278 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 279 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 280 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 281 | NLSR_LOG_DEBUG("Attempted to remove origin: " << rtpePtr->getDestination() |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 282 | << " from non-existent prefix: " << name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 287 | NamePrefixTable::updateWithNewRoute(const std::list<RoutingTableEntry>& entries) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 288 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 289 | NLSR_LOG_DEBUG("Updating table with newly calculated routes"); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 290 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 291 | // Iterate over each pool entry we have |
| 292 | for (auto&& poolEntryPair : m_rtpool) { |
| 293 | auto&& poolEntry = poolEntryPair.second; |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 294 | auto sourceEntry = std::find_if(entries.begin(), entries.end(), |
| 295 | [&poolEntry] (const RoutingTableEntry& entry) { |
| 296 | return poolEntry->getDestination() == entry.getDestination(); |
| 297 | }); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 298 | // If this pool entry has a corresponding entry in the routing table now |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 299 | if (sourceEntry != entries.end() |
| 300 | && poolEntry->getNexthopList() != sourceEntry->getNexthopList()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 301 | NLSR_LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " has changed next-hops."); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 302 | poolEntry->setNexthopList(sourceEntry->getNexthopList()); |
| 303 | for (const auto& nameEntry : poolEntry->namePrefixTableEntries) { |
| 304 | auto nameEntryFullPtr = nameEntry.second.lock(); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 305 | addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination(), nameEntryFullPtr->getFlags()); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 308 | else if (sourceEntry == entries.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 309 | NLSR_LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " now has no next-hops."); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 310 | poolEntry->getNexthopList().clear(); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 311 | for (const auto& nameEntry : poolEntry->namePrefixTableEntries) { |
| 312 | auto nameEntryFullPtr = nameEntry.second.lock(); |
awlane | fc0b45a | 2025-05-07 15:51:02 -0500 | [diff] [blame^] | 313 | addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination(), nameEntryFullPtr->getFlags()); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 314 | } |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 315 | } |
| 316 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 317 | NLSR_LOG_TRACE("No change in routing entry:" << poolEntry->getDestination() |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 318 | << ", no action necessary."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 323 | // Inserts the routing table pool entry into the NPT's RTE storage |
| 324 | // pool. This cannot fail, so the pool is guaranteed to contain the |
| 325 | // item after this occurs. |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 326 | std::shared_ptr<RoutingTablePoolEntry> |
| 327 | NamePrefixTable::addRtpeToPool(RoutingTablePoolEntry& rtpe) |
| 328 | { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 329 | auto poolIt = m_rtpool.try_emplace(rtpe.getDestination(), |
| 330 | std::make_shared<RoutingTablePoolEntry>(rtpe)).first; |
| 331 | return poolIt->second; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 334 | // Removes the routing table pool entry from the storage pool. The |
| 335 | // postconditions of this function are guaranteed to include that |
| 336 | // the storage pool does not contain such an item. Additionally, |
| 337 | // this function cannot fail, but nonetheless debug information is |
| 338 | // given in the case that this function is called with an entry that |
| 339 | // isn't in the pool. |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 340 | void |
| 341 | NamePrefixTable::deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr) |
| 342 | { |
| 343 | if (m_rtpool.erase(rtpePtr->getDestination()) != 1) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 344 | NLSR_LOG_DEBUG("Attempted to delete non-existent origin: " |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 345 | << rtpePtr->getDestination() |
| 346 | << " from NPT routing table entry storage pool."); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 350 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 351 | NamePrefixTable::writeLog() |
| 352 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 353 | NLSR_LOG_DEBUG(*this); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 356 | std::ostream& |
| 357 | operator<<(std::ostream& os, const NamePrefixTable& table) |
| 358 | { |
| 359 | os << "----------------NPT----------------------\n"; |
| 360 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 361 | for (const auto& entryPtr : table) { |
| 362 | os << *entryPtr << std::endl; |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return os; |
| 366 | } |
| 367 | |
| 368 | } // namespace nlsr |