akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Muktadir R Chowdhury | 800833b | 2016-07-29 13:43:59 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | static 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, RoutingTableEntry& rte) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 45 | // Check if the advertised name prefix is in the table already. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 46 | NptEntryList::iterator it = std::find_if(m_table.begin(), |
| 47 | m_table.end(), |
| 48 | bind(&npteCompare, _1, name)); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 49 | // If not, create a new entry and add it. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 50 | if (it == m_table.end()) { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 51 | _LOG_TRACE("Adding origin: " << rte.getDestination() << " to new name prefix: " << name); |
| 52 | |
| 53 | NamePrefixTableEntry entry(name); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 54 | entry.addRoutingTableEntry(rte); // Add this RTE to this new NPT entry. |
| 55 | entry.generateNhlfromRteList(); // Generate a list of next-hops from the RTE. |
| 56 | entry.getNexthopList().sort(); // Sort it. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 57 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 58 | m_table.push_back(entry); // Add the new, completed entry into the main table. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 59 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 60 | // If the RTE we added has any next hops, we inform the FIB of this. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | if (rte.getNexthopList().getSize() > 0) { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 62 | _LOG_TRACE("Updating FIB with next hops for " << entry); |
| 63 | m_nlsr.getFib().update(name, entry.getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 64 | } |
| 65 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | else { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 67 | _LOG_TRACE("Adding origin: " << rte.getDestination() << " to existing prefix: " << *it); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 68 | // Update the existing entry with the new RTE. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 69 | it->addRoutingTableEntry(rte); |
| 70 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 71 | it->generateNhlfromRteList(); // Rebuild the list of next-hops |
| 72 | it->getNexthopList().sort(); // Sort it. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 73 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 74 | // As above, inform the FIB of this fact. |
| 75 | // We may possibly have a new best next-hop for this name prefix |
| 76 | // So this is a necessary step. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 77 | if (it->getNexthopList().getSize() > 0) { |
| 78 | _LOG_TRACE("Updating FIB with next hops for " << *it); |
| 79 | m_nlsr.getFib().update(name, it->getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | else { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 82 | // The routing table may recalculate and add a routing table |
| 83 | // entry with no next hops to replace an existing routing table |
| 84 | // entry. In this case, the name prefix is no longer reachable |
| 85 | // through a next hop and should be removed from the FIB. But, |
| 86 | // the prefix should remain in the Name Prefix Table as a future |
| 87 | // routing table calculation may add next hops. |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 88 | _LOG_TRACE(*it << " has no next hops; removing from FIB"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 89 | m_nlsr.getFib().remove(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 95 | NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 96 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 97 | NptEntryList::iterator it = std::find_if(m_table.begin(), |
| 98 | m_table.end(), |
| 99 | bind(&npteCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | if (it != m_table.end()) { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 101 | _LOG_TRACE("Removing origin: " << rte.getDestination() << " from prefix: " << *it); |
| 102 | |
Vince Lehman | 9630fbf | 2015-05-05 12:33:44 -0500 | [diff] [blame] | 103 | it->removeRoutingTableEntry(rte); |
| 104 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 105 | // If the prefix is a router prefix and it does not have any other routing table entries, |
| 106 | // the Adjacency/Coordinate LSA associated with that origin router has been removed from |
| 107 | // the LSDB and so the router prefix should be removed from the Name Prefix Table. |
| 108 | // |
| 109 | // If the prefix is an advertised name prefix: |
| 110 | // If another router advertises this name prefix, the RteList should have another entry |
| 111 | // for that router; the next hops should be recalculated and installed in the FIB. |
| 112 | // |
| 113 | // If no other router advertises this name prefix, the RteList should be empty and the |
| 114 | // prefix can be removed from the Name Prefix Table. Once a new Name LSA advertises this |
| 115 | // prefix, a new entry for the prefix will be created. |
| 116 | // |
| 117 | if (it->getRteListSize() == 0) { |
| 118 | _LOG_TRACE(*it << " has no routing table entries; removing from table and FIB"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 119 | m_table.erase(it); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 120 | m_nlsr.getFib().remove(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 122 | else { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 123 | _LOG_TRACE(*it << " has other routing table entries; updating FIB with next hops"); |
| 124 | it->generateNhlfromRteList(); |
| 125 | it->getNexthopList().sort(); |
| 126 | |
| 127 | m_nlsr.getFib().update(name, it->getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 133 | NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 135 | _LOG_DEBUG("Adding origin: " << destRouter << " to " << name); |
| 136 | |
| 137 | RoutingTableEntry* rteCheck = m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 138 | |
| 139 | if (rteCheck != nullptr) { |
| 140 | addEntry(name, *rteCheck); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 142 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | RoutingTableEntry rte(destRouter); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 144 | addEntry(name, rte); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 149 | NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 150 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 151 | _LOG_DEBUG("Removing origin: " << destRouter << " from " << name); |
| 152 | |
| 153 | RoutingTableEntry* rteCheck = m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 154 | |
| 155 | if (rteCheck != nullptr) { |
| 156 | removeEntry(name, *rteCheck); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 158 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | RoutingTableEntry rte(destRouter); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 160 | removeEntry(name, rte); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 165 | NamePrefixTable::updateWithNewRoute() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 167 | _LOG_DEBUG("Updating table with newly calculated routes"); |
| 168 | |
| 169 | // Update each name prefix entry in the Name Prefix Table with newly calculated next hops |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 170 | // For each entry in the NPT |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 171 | for (const NamePrefixTableEntry& prefixEntry : m_table) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 172 | // For each routing table entry |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 173 | for (const RoutingTableEntry& routingEntry : prefixEntry.getRteList()) { |
| 174 | _LOG_TRACE("Updating next hops to origin: " << routingEntry.getDestination() |
| 175 | << " for prefix: " << prefixEntry); |
| 176 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 177 | RoutingTableEntry* rteCheck = |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 178 | m_nlsr.getRoutingTable().findRoutingTableEntry(routingEntry.getDestination()); |
| 179 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 180 | // 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] | 181 | if (rteCheck != nullptr) { |
| 182 | addEntry(prefixEntry.getNamePrefix(), *rteCheck); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 183 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 184 | else { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 185 | RoutingTableEntry rte(routingEntry.getDestination()); |
| 186 | addEntry(prefixEntry.getNamePrefix(), rte); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 193 | NamePrefixTable::writeLog() |
| 194 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 195 | _LOG_DEBUG(*this); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 198 | std::ostream& |
| 199 | operator<<(std::ostream& os, const NamePrefixTable& table) |
| 200 | { |
| 201 | os << "----------------NPT----------------------\n"; |
| 202 | |
| 203 | for (const NamePrefixTableEntry& entry : table) { |
| 204 | os << entry << std::endl; |
| 205 | } |
| 206 | |
| 207 | return os; |
| 208 | } |
| 209 | |
| 210 | } // namespace nlsr |