akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [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 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("NamePrefixTable"); |
| 35 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 36 | bool |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 37 | npteCompare(std::shared_ptr<NamePrefixTableEntry>& npte, const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 39 | return npte->getNamePrefix() == name; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | } |
| 41 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 43 | NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 45 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 46 | // Check if the advertised name prefix is in the table already. |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 47 | NptEntryList::iterator nameItr = |
| 48 | std::find_if(m_table.begin(), |
| 49 | m_table.end(), |
| 50 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 51 | return name == entry->getNamePrefix(); |
| 52 | }); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 53 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 54 | // Attempt to find a routing table pool entry (RTPE) we can use. |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 55 | RoutingTableEntryPool::iterator rtpeItr = m_rtpool.find(destRouter); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 56 | |
| 57 | // These declarations just to make the compiler happy... |
| 58 | RoutingTablePoolEntry rtpe; |
| 59 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr(nullptr); |
| 60 | |
| 61 | // There isn't currently a routing table entry in the pool for this name |
| 62 | if (rtpeItr == m_rtpool.end()) { |
| 63 | // See if there is a routing table entry available we could use |
| 64 | RoutingTableEntry* routeEntryPtr = m_nlsr.getRoutingTable() |
| 65 | .findRoutingTableEntry(destRouter); |
| 66 | |
| 67 | // We have to create a new routing table entry |
| 68 | if (routeEntryPtr == nullptr) { |
| 69 | rtpe = RoutingTablePoolEntry(destRouter, 0); |
| 70 | } |
| 71 | // There was already a usable one in the routing table |
| 72 | else { |
| 73 | rtpe = RoutingTablePoolEntry(*routeEntryPtr, 0); |
| 74 | } |
| 75 | |
| 76 | // Add the new pool object to the pool. |
| 77 | rtpePtr = addRtpeToPool(rtpe); |
| 78 | } |
| 79 | // There was one already, so just fetch that one. |
| 80 | else { |
| 81 | rtpePtr = (*rtpeItr).second; |
| 82 | } |
| 83 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 84 | std::shared_ptr<NamePrefixTableEntry> npte; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 85 | // Either we have to make a new NPT entry or there already was one. |
| 86 | if (nameItr == m_table.end()) { |
| 87 | _LOG_DEBUG("Adding origin: " << rtpePtr->getDestination() |
| 88 | << " to a new name prefix: " << name); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 89 | npte = make_shared<NamePrefixTableEntry>(name); |
| 90 | npte->addRoutingTableEntry(rtpePtr); |
| 91 | npte->generateNhlfromRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 92 | m_table.push_back(npte); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 93 | // If this entry has next hops, we need to inform the FIB |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 94 | if (npte->getNexthopList().getSize() > 0) { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 95 | _LOG_TRACE("Updating FIB with next hops for " << npte); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 96 | m_nlsr.getFib().update(name, npte->getNexthopList()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 97 | } |
| 98 | // The routing table may recalculate and add a routing table entry |
| 99 | // with no next hops to replace an existing routing table entry. In |
| 100 | // this case, the name prefix is no longer reachable through a next |
| 101 | // hop and should be removed from the FIB. But, the prefix should |
| 102 | // remain in the Name Prefix Table as a future routing table |
| 103 | // calculation may add next hops. |
| 104 | else { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 105 | _LOG_TRACE(*npte << " has no next hops; removing from FIB"); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 106 | m_nlsr.getFib().remove(name); |
| 107 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 109 | else { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 110 | npte = *nameItr; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 111 | _LOG_TRACE("Adding origin: " << rtpePtr->getDestination() |
| 112 | << " to existing prefix: " << *nameItr); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 113 | (*nameItr)->addRoutingTableEntry(rtpePtr); |
| 114 | (*nameItr)->generateNhlfromRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 115 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 116 | if ((*nameItr)->getNexthopList().getSize() > 0) { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 117 | _LOG_TRACE("Updating FIB with next hops for " << (*nameItr)); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 118 | m_nlsr.getFib().update(name, (*nameItr)->getNexthopList()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 119 | } |
| 120 | else { |
| 121 | _LOG_TRACE((*nameItr) << " has no next hops; removing from FIB"); |
| 122 | m_nlsr.getFib().remove(name); |
| 123 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | } |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 125 | // Add the reference to this NPT to the RTPE. |
| 126 | rtpePtr->namePrefixTableEntries.emplace( |
| 127 | std::make_pair(npte->getNamePrefix(), std::weak_ptr<NamePrefixTableEntry>(npte))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 131 | NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 133 | _LOG_DEBUG("Removing origin: " << destRouter << " from " << name); |
| 134 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 135 | // Fetch an iterator to the appropriate pair object in the pool. |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 136 | RoutingTableEntryPool::iterator rtpeItr = m_rtpool.find(destRouter); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 137 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 138 | // Simple error checking to prevent any unusual behavior in the case |
| 139 | // that we try to remove an entry that isn't there. |
| 140 | if (rtpeItr == m_rtpool.end()) { |
| 141 | _LOG_DEBUG("No entry for origin: " << destRouter |
| 142 | << " found, so it cannot be removed from prefix: " |
| 143 | << name); |
| 144 | return; |
| 145 | } |
| 146 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = rtpeItr->second; |
| 147 | |
| 148 | // Ensure that the entry exists |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 149 | NptEntryList::iterator nameItr = |
| 150 | std::find_if(m_table.begin(), m_table.end(), |
| 151 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 152 | return entry->getNamePrefix() == name; |
| 153 | }); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 154 | if (nameItr != m_table.end()) { |
| 155 | _LOG_TRACE("Removing origin: " << rtpePtr->getDestination() |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 156 | << " from prefix: " << **nameItr); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 157 | |
| 158 | // Rather than iterating through the whole list periodically, just |
| 159 | // delete them here if they have no references. |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 160 | if ((*nameItr)->removeRoutingTableEntry(rtpePtr) == 0) { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 161 | deleteRtpeFromPool(rtpePtr); |
| 162 | } |
| 163 | |
| 164 | // If the prefix is a router prefix and it does not have any other |
| 165 | // routing table entries, the Adjacency/Coordinate LSA associated |
| 166 | // with that origin router has been removed from the LSDB and so |
| 167 | // the router prefix should be removed from the Name Prefix Table. |
| 168 | // |
| 169 | // If the prefix is an advertised name prefix: If another router |
| 170 | // advertises this name prefix, the RteList should have another |
| 171 | // entry for that router; the next hops should be recalculated |
| 172 | // and installed in the FIB. |
| 173 | // |
| 174 | // If no other router advertises this name prefix, the RteList |
| 175 | // should be empty and the prefix can be removed from the Name |
| 176 | // Prefix Table. Once a new Name LSA advertises this prefix, a |
| 177 | // new entry for the prefix will be created. |
| 178 | // |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 179 | if ((*nameItr)->getRteListSize() == 0) { |
| 180 | _LOG_TRACE(**nameItr << " has no routing table entries;" |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 181 | << " removing from table and FIB"); |
| 182 | m_table.erase(nameItr); |
| 183 | m_nlsr.getFib().remove(name); |
| 184 | } |
| 185 | else { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 186 | _LOG_TRACE(**nameItr << " has other routing table entries;" |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 187 | << " updating FIB with next hops"); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 188 | (*nameItr)->generateNhlfromRteList(); |
| 189 | m_nlsr.getFib().update(name, (*nameItr)->getNexthopList()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 190 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 192 | else { |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 193 | _LOG_DEBUG("Attempted to remove origin: " << rtpePtr->getDestination() |
Ashlesh Gawande | 90173ad | 2017-08-09 15:19:50 -0500 | [diff] [blame] | 194 | << " from non-existent prefix: " << name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 199 | NamePrefixTable::updateWithNewRoute() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 201 | _LOG_DEBUG("Updating table with newly calculated routes"); |
| 202 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 203 | // Iterate over each pool entry we have |
| 204 | for (auto&& poolEntryPair : m_rtpool) { |
| 205 | auto&& poolEntry = poolEntryPair.second; |
| 206 | RoutingTableEntry* sourceEntry = |
| 207 | m_nlsr.getRoutingTable().findRoutingTableEntry(poolEntry->getDestination()); |
| 208 | // If this pool entry has a corresponding entry in the routing table now |
| 209 | if (sourceEntry != nullptr && poolEntry->getNexthopList() != sourceEntry->getNexthopList()) { |
| 210 | _LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " has changed next-hops."); |
| 211 | poolEntry->setNexthopList(sourceEntry->getNexthopList()); |
| 212 | for (const auto& nameEntry : poolEntry->namePrefixTableEntries) { |
| 213 | auto nameEntryFullPtr = nameEntry.second.lock(); |
| 214 | addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination()); |
| 215 | } |
| 216 | } |
| 217 | else if (sourceEntry == nullptr) { |
| 218 | _LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " now has no next-hops."); |
| 219 | poolEntry->getNexthopList().reset(); |
| 220 | for (const auto& nameEntry : poolEntry->namePrefixTableEntries) { |
| 221 | auto nameEntryFullPtr = nameEntry.second.lock(); |
| 222 | addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination()); |
| 223 | } |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 224 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 225 | } |
| 226 | else { |
| 227 | _LOG_TRACE("No change in routing entry:" << poolEntry->getDestination() |
| 228 | << ", no action necessary."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 233 | // Inserts the routing table pool entry into the NPT's RTE storage |
| 234 | // pool. This cannot fail, so the pool is guaranteed to contain the |
| 235 | // item after this occurs. |
| 236 | std::shared_ptr<RoutingTablePoolEntry> |
| 237 | NamePrefixTable::addRtpeToPool(RoutingTablePoolEntry& rtpe) |
| 238 | { |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 239 | RoutingTableEntryPool::iterator poolItr = |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 240 | m_rtpool.insert(std::make_pair(rtpe.getDestination(), |
| 241 | std::make_shared<RoutingTablePoolEntry> |
| 242 | (rtpe))) |
| 243 | .first; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 244 | return poolItr->second; |
| 245 | } |
| 246 | |
| 247 | // Removes the routing table pool entry from the storage pool. The |
| 248 | // postconditions of this function are guaranteed to include that |
| 249 | // the storage pool does not contain such an item. Additionally, |
| 250 | // this function cannot fail, but nonetheless debug information is |
| 251 | // given in the case that this function is called with an entry that |
| 252 | // isn't in the pool. |
| 253 | void |
| 254 | NamePrefixTable::deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr) |
| 255 | { |
| 256 | if (m_rtpool.erase(rtpePtr->getDestination()) != 1) { |
Ashlesh Gawande | 90173ad | 2017-08-09 15:19:50 -0500 | [diff] [blame] | 257 | _LOG_DEBUG("Attempted to delete non-existent origin: " |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 258 | << rtpePtr->getDestination() |
| 259 | << " from NPT routing table entry storage pool."); |
| 260 | } |
| 261 | } |
| 262 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 263 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 264 | NamePrefixTable::writeLog() |
| 265 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 266 | _LOG_DEBUG(*this); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 269 | std::ostream& |
| 270 | operator<<(std::ostream& os, const NamePrefixTable& table) |
| 271 | { |
| 272 | os << "----------------NPT----------------------\n"; |
| 273 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 274 | for (const auto& entryPtr : table) { |
| 275 | os << *entryPtr << std::endl; |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return os; |
| 279 | } |
| 280 | |
| 281 | } // namespace nlsr |