blob: 3ee91327e774271a93823a3361111befab6e603d [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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 Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
Vince Lehmancae33b62015-06-05 09:21:30 -050022#include "name-prefix-table.hpp"
23
24#include "logger.hpp"
25#include "nlsr.hpp"
26#include "routing-table.hpp"
27
28#include <algorithm>
akmhoque53353462014-04-22 08:43:45 -050029#include <list>
30#include <utility>
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
dmcoomescf8d0ed2017-02-21 11:39:01 -060034INIT_LOGGER(route.NamePrefixTable);
akmhoque674b0b12014-05-20 14:33:28 -050035
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070036NamePrefixTable::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 Gawande85998a12017-12-07 22:22:13 -060042 , m_routingTable(routingTable)
Nick Gordonb7b58392017-08-17 16:29:21 -050043{
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070044 m_afterRoutingChangeConnection = afterRoutingChangeSignal.connect(
Nick Gordonb7b58392017-08-17 16:29:21 -050045 [this] (const std::list<RoutingTableEntry>& entries) {
46 updateWithNewRoute(entries);
47 });
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070048
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 Gordonb7b58392017-08-17 16:29:21 -050055}
56
57NamePrefixTable::~NamePrefixTable()
58{
59 m_afterRoutingChangeConnection.disconnect();
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070060 m_afterLsdbModified.disconnect();
61}
62
63void
64NamePrefixTable::updateFromLsdb(std::shared_ptr<Lsa> lsa, LsdbUpdate updateType,
65 const std::list<ndn::Name>& namesToAdd,
66 const std::list<ndn::Name>& namesToRemove)
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);
78 for (const auto& name : nlsa->getNpl().getNames()) {
79 if (name != m_ownRouterName) {
80 addEntry(name, lsa->getOriginRouter());
81 }
82 }
83 }
84 }
85 else if (updateType == LsdbUpdate::UPDATED) {
86 if (lsa->getType() != Lsa::Type::NAME) {
87 return;
88 }
89
90 for (const auto& name : namesToAdd) {
91 if (name != m_ownRouterName) {
92 addEntry(name, lsa->getOriginRouter());
93 }
94 }
95
96 for (const auto& name : namesToRemove) {
97 if (name != m_ownRouterName) {
98 removeEntry(name, lsa->getOriginRouter());
99 }
100 }
101 }
102 else {
103 removeEntry(lsa->getOriginRouter(), lsa->getOriginRouter());
104 if (lsa->getType() == Lsa::Type::NAME) {
105 auto nlsa = std::static_pointer_cast<NameLsa>(lsa);
106 for (const auto& name : nlsa->getNpl().getNames()) {
107 if (name != m_ownRouterName) {
108 removeEntry(name, lsa->getOriginRouter());
109 }
110 }
111 }
112 }
Nick Gordonb7b58392017-08-17 16:29:21 -0500113}
114
akmhoque53353462014-04-22 08:43:45 -0500115void
akmhoque31d1d4b2014-05-05 22:08:14 -0500116NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500117{
Nick Gordonb50e51b2016-07-22 16:05:57 -0500118 // Check if the advertised name prefix is in the table already.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400119 auto nameItr = std::find_if(m_table.begin(), m_table.end(),
120 [&] (const auto& entry) { return name == entry->getNamePrefix(); });
Vince Lehmancae33b62015-06-05 09:21:30 -0500121
Nick Gordonb50e51b2016-07-22 16:05:57 -0500122 // Attempt to find a routing table pool entry (RTPE) we can use.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400123 auto rtpeItr = m_rtpool.find(destRouter);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500124
125 // These declarations just to make the compiler happy...
126 RoutingTablePoolEntry rtpe;
127 std::shared_ptr<RoutingTablePoolEntry> rtpePtr(nullptr);
128
129 // There isn't currently a routing table entry in the pool for this name
130 if (rtpeItr == m_rtpool.end()) {
131 // See if there is a routing table entry available we could use
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600132 RoutingTableEntry* routeEntryPtr = m_routingTable.findRoutingTableEntry(destRouter);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500133
134 // We have to create a new routing table entry
135 if (routeEntryPtr == nullptr) {
136 rtpe = RoutingTablePoolEntry(destRouter, 0);
137 }
138 // There was already a usable one in the routing table
139 else {
140 rtpe = RoutingTablePoolEntry(*routeEntryPtr, 0);
141 }
142
143 // Add the new pool object to the pool.
144 rtpePtr = addRtpeToPool(rtpe);
145 }
146 // There was one already, so just fetch that one.
147 else {
148 rtpePtr = (*rtpeItr).second;
149 }
150
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500151 std::shared_ptr<NamePrefixTableEntry> npte;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500152 // Either we have to make a new NPT entry or there already was one.
153 if (nameItr == m_table.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500154 NLSR_LOG_DEBUG("Adding origin: " << rtpePtr->getDestination()
Davide Pesaventod90338d2021-01-07 17:50:05 -0500155 << " to a new name prefix: " << name);
156 npte = std::make_shared<NamePrefixTableEntry>(name);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500157 npte->addRoutingTableEntry(rtpePtr);
158 npte->generateNhlfromRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500159 m_table.push_back(npte);
Davide Pesaventod90338d2021-01-07 17:50:05 -0500160
Nick Gordonb50e51b2016-07-22 16:05:57 -0500161 // If this entry has next hops, we need to inform the FIB
Nick Gordonff9a6272017-10-12 13:38:29 -0500162 if (npte->getNexthopList().size() > 0) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500163 NLSR_LOG_TRACE("Updating FIB with next hops for " << npte->getNamePrefix());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 m_fib.update(name, npte->getNexthopList());
Nick Gordonb50e51b2016-07-22 16:05:57 -0500165 }
166 // The routing table may recalculate and add a routing table entry
167 // with no next hops to replace an existing routing table entry. In
168 // this case, the name prefix is no longer reachable through a next
169 // hop and should be removed from the FIB. But, the prefix should
170 // remain in the Name Prefix Table as a future routing table
171 // calculation may add next hops.
172 else {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500173 NLSR_LOG_TRACE(npte->getNamePrefix() << " has no next hops; removing from FIB");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 m_fib.remove(name);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500175 }
akmhoque53353462014-04-22 08:43:45 -0500176 }
akmhoque157b0a42014-05-13 00:26:37 -0500177 else {
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500178 npte = *nameItr;
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600179 NLSR_LOG_TRACE("Adding origin: " << rtpePtr->getDestination() <<
180 " to existing prefix: " << **nameItr);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500181 (*nameItr)->addRoutingTableEntry(rtpePtr);
182 (*nameItr)->generateNhlfromRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500183
Nick Gordonff9a6272017-10-12 13:38:29 -0500184 if ((*nameItr)->getNexthopList().size() > 0) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600185 NLSR_LOG_TRACE("Updating FIB with next hops for " << (**nameItr));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600186 m_fib.update(name, (*nameItr)->getNexthopList());
Nick Gordonb50e51b2016-07-22 16:05:57 -0500187 }
188 else {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500189 NLSR_LOG_TRACE(npte->getNamePrefix() << " has no next hops; removing from FIB");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600190 m_fib.remove(name);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500191 }
akmhoque53353462014-04-22 08:43:45 -0500192 }
Davide Pesaventod90338d2021-01-07 17:50:05 -0500193
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500194 // Add the reference to this NPT to the RTPE.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400195 rtpePtr->namePrefixTableEntries.try_emplace(npte->getNamePrefix(),
196 std::weak_ptr<NamePrefixTableEntry>(npte));
akmhoque53353462014-04-22 08:43:45 -0500197}
198
199void
akmhoque31d1d4b2014-05-05 22:08:14 -0500200NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500201{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500202 NLSR_LOG_DEBUG("Removing origin: " << destRouter << " from " << name);
Vince Lehmancae33b62015-06-05 09:21:30 -0500203
Nick Gordonb50e51b2016-07-22 16:05:57 -0500204 // Fetch an iterator to the appropriate pair object in the pool.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400205 auto rtpeItr = m_rtpool.find(destRouter);
Vince Lehmancae33b62015-06-05 09:21:30 -0500206
Nick Gordonb50e51b2016-07-22 16:05:57 -0500207 // Simple error checking to prevent any unusual behavior in the case
208 // that we try to remove an entry that isn't there.
209 if (rtpeItr == m_rtpool.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500210 NLSR_LOG_DEBUG("No entry for origin: " << destRouter
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400211 << " found, so it cannot be removed from prefix: " << name);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500212 return;
213 }
214 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = rtpeItr->second;
215
216 // Ensure that the entry exists
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400217 auto nameItr = std::find_if(m_table.begin(), m_table.end(),
218 [&] (const auto& entry) { return entry->getNamePrefix() == name; });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500219 if (nameItr != m_table.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500220 NLSR_LOG_TRACE("Removing origin: " << rtpePtr->getDestination()
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400221 << " from prefix: " << **nameItr);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500222
223 // Rather than iterating through the whole list periodically, just
224 // delete them here if they have no references.
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500225 if ((*nameItr)->removeRoutingTableEntry(rtpePtr) == 0) {
Nick Gordonb50e51b2016-07-22 16:05:57 -0500226 deleteRtpeFromPool(rtpePtr);
227 }
228
229 // If the prefix is a router prefix and it does not have any other
230 // routing table entries, the Adjacency/Coordinate LSA associated
231 // with that origin router has been removed from the LSDB and so
232 // the router prefix should be removed from the Name Prefix Table.
233 //
234 // If the prefix is an advertised name prefix: If another router
235 // advertises this name prefix, the RteList should have another
236 // entry for that router; the next hops should be recalculated
237 // and installed in the FIB.
238 //
239 // If no other router advertises this name prefix, the RteList
240 // should be empty and the prefix can be removed from the Name
241 // Prefix Table. Once a new Name LSA advertises this prefix, a
242 // new entry for the prefix will be created.
243 //
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500244 if ((*nameItr)->getRteListSize() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500245 NLSR_LOG_TRACE(**nameItr << " has no routing table entries;"
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400246 << " removing from table and FIB");
Nick Gordonb50e51b2016-07-22 16:05:57 -0500247 m_table.erase(nameItr);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600248 m_fib.remove(name);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500249 }
250 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500251 NLSR_LOG_TRACE(**nameItr << " has other routing table entries;"
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400252 << " updating FIB with next hops");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500253 (*nameItr)->generateNhlfromRteList();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600254 m_fib.update(name, (*nameItr)->getNexthopList());
Nick Gordonb50e51b2016-07-22 16:05:57 -0500255 }
akmhoque53353462014-04-22 08:43:45 -0500256 }
akmhoque157b0a42014-05-13 00:26:37 -0500257 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500258 NLSR_LOG_DEBUG("Attempted to remove origin: " << rtpePtr->getDestination()
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400259 << " from non-existent prefix: " << name);
akmhoque53353462014-04-22 08:43:45 -0500260 }
261}
262
263void
Nick Gordonb7b58392017-08-17 16:29:21 -0500264NamePrefixTable::updateWithNewRoute(const std::list<RoutingTableEntry>& entries)
akmhoque53353462014-04-22 08:43:45 -0500265{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500266 NLSR_LOG_DEBUG("Updating table with newly calculated routes");
Vince Lehmancae33b62015-06-05 09:21:30 -0500267
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500268 // Iterate over each pool entry we have
269 for (auto&& poolEntryPair : m_rtpool) {
270 auto&& poolEntry = poolEntryPair.second;
Nick Gordonb7b58392017-08-17 16:29:21 -0500271 auto sourceEntry = std::find_if(entries.begin(), entries.end(),
272 [&poolEntry] (const RoutingTableEntry& entry) {
273 return poolEntry->getDestination() == entry.getDestination();
274 });
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500275 // If this pool entry has a corresponding entry in the routing table now
Nick Gordonb7b58392017-08-17 16:29:21 -0500276 if (sourceEntry != entries.end()
277 && poolEntry->getNexthopList() != sourceEntry->getNexthopList()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500278 NLSR_LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " has changed next-hops.");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500279 poolEntry->setNexthopList(sourceEntry->getNexthopList());
280 for (const auto& nameEntry : poolEntry->namePrefixTableEntries) {
281 auto nameEntryFullPtr = nameEntry.second.lock();
282 addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination());
283 }
284 }
Nick Gordonb7b58392017-08-17 16:29:21 -0500285 else if (sourceEntry == entries.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500286 NLSR_LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " now has no next-hops.");
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700287 poolEntry->getNexthopList().clear();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500288 for (const auto& nameEntry : poolEntry->namePrefixTableEntries) {
289 auto nameEntryFullPtr = nameEntry.second.lock();
290 addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination());
291 }
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500292 }
293 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500294 NLSR_LOG_TRACE("No change in routing entry:" << poolEntry->getDestination()
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500295 << ", no action necessary.");
akmhoque53353462014-04-22 08:43:45 -0500296 }
297 }
298}
299
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400300// Inserts the routing table pool entry into the NPT's RTE storage
301// pool. This cannot fail, so the pool is guaranteed to contain the
302// item after this occurs.
Nick Gordonb50e51b2016-07-22 16:05:57 -0500303std::shared_ptr<RoutingTablePoolEntry>
304NamePrefixTable::addRtpeToPool(RoutingTablePoolEntry& rtpe)
305{
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400306 auto poolIt = m_rtpool.try_emplace(rtpe.getDestination(),
307 std::make_shared<RoutingTablePoolEntry>(rtpe)).first;
308 return poolIt->second;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500309}
310
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400311// Removes the routing table pool entry from the storage pool. The
312// postconditions of this function are guaranteed to include that
313// the storage pool does not contain such an item. Additionally,
314// this function cannot fail, but nonetheless debug information is
315// given in the case that this function is called with an entry that
316// isn't in the pool.
Nick Gordonb50e51b2016-07-22 16:05:57 -0500317void
318NamePrefixTable::deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr)
319{
320 if (m_rtpool.erase(rtpePtr->getDestination()) != 1) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500321 NLSR_LOG_DEBUG("Attempted to delete non-existent origin: "
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400322 << rtpePtr->getDestination()
323 << " from NPT routing table entry storage pool.");
Nick Gordonb50e51b2016-07-22 16:05:57 -0500324 }
325}
326
akmhoque53353462014-04-22 08:43:45 -0500327void
akmhoque674b0b12014-05-20 14:33:28 -0500328NamePrefixTable::writeLog()
329{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500330 NLSR_LOG_DEBUG(*this);
akmhoque674b0b12014-05-20 14:33:28 -0500331}
332
Vince Lehmancae33b62015-06-05 09:21:30 -0500333std::ostream&
334operator<<(std::ostream& os, const NamePrefixTable& table)
335{
336 os << "----------------NPT----------------------\n";
337
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500338 for (const auto& entryPtr : table) {
339 os << *entryPtr << std::endl;
Vince Lehmancae33b62015-06-05 09:21:30 -0500340 }
341
342 return os;
343}
344
345} // namespace nlsr