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