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, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * 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] | 19 | **/ |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 20 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 21 | #include "fib.hpp" |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 22 | #include "adjacency-list.hpp" |
| 23 | #include "conf-parameter.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 24 | #include "logger.hpp" |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 25 | #include "nexthop-list.hpp" |
| 26 | |
| 27 | #include <map> |
| 28 | #include <cmath> |
laqinfan | cd7ca05 | 2017-06-02 04:57:18 -0500 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <iterator> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | INIT_LOGGER("Fib"); |
| 35 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 36 | const uint64_t Fib::GRACE_PERIOD = 10; |
| 37 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | using namespace std; |
| 39 | using namespace ndn; |
| 40 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 42 | Fib::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 44 | _LOG_DEBUG("Fib::remove called"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 45 | std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 46 | if (it != m_table.end()) { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 47 | for (std::set<NextHop, NextHopComparator>::iterator nhit = |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 48 | (it->second).getNexthopList().getNextHops().begin(); |
| 49 | nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | //remove entry from NDN-FIB |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 51 | if (isPrefixUpdatable((it->second).getName())) { |
| 52 | unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 53 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | } |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 55 | cancelEntryRefresh(it->second); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | m_table.erase(it); |
| 57 | } |
| 58 | } |
| 59 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 60 | bool |
| 61 | compareFaceUri(const NextHop& hop, const std::string& faceUri) |
| 62 | { |
| 63 | return hop.getConnectingFaceUri() == faceUri; |
| 64 | } |
| 65 | |
| 66 | void |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 67 | Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 68 | { |
| 69 | const ndn::Name& name = entry.getName(); |
| 70 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 71 | for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 72 | { |
| 73 | // Add nexthop to FIB entry |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 74 | entry.getNexthopList().addNextHop(*it); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 75 | |
| 76 | if (isPrefixUpdatable(name)) { |
| 77 | // Add nexthop to NDN-FIB |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 78 | registerPrefix(name, it->getConnectingFaceUri(), |
| 79 | it->getRouteCostAsAdjustedInteger(), |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 80 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 81 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 82 | } |
| 83 | } |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 87 | Fib::update(const ndn::Name& name, NexthopList& allHops) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 88 | { |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 89 | FibEntry* entry = processUpdate(name, allHops); |
| 90 | if (entry != nullptr && !entry->getRefreshEventId()) { |
| 91 | scheduleEntryRefresh(*entry, |
| 92 | [this] (FibEntry& fibEntry) { |
| 93 | this->scheduleLoop(fibEntry); |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | FibEntry* |
| 99 | Fib::processUpdate(const ndn::Name& name, NexthopList& allHops) |
| 100 | { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 101 | _LOG_DEBUG("Fib::update called"); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 102 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 103 | // Get the max possible faces which is the minumum of the configuration setting and |
| 104 | // the length of the list of all next hops. |
| 105 | unsigned int maxFaces = getNumberOfFacesForName(allHops); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 106 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 107 | NexthopList hopsToAdd; |
| 108 | unsigned int nFaces = 0; |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 109 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 110 | // Create a list of next hops to be installed with length == maxFaces |
| 111 | for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces; |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 112 | ++it, ++nFaces) { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 113 | hopsToAdd.addNextHop(*it); |
| 114 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 115 | |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 116 | std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 117 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 118 | // New FIB entry that has nextHops |
| 119 | if (entryIt == m_table.end() && hopsToAdd.getSize() != 0) { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 120 | _LOG_DEBUG("New FIB Entry"); |
| 121 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 122 | FibEntry entry(name); |
| 123 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 124 | addNextHopsToFibEntryAndNfd(entry, hopsToAdd); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 125 | |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 126 | m_table.emplace(name, entry); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 127 | |
| 128 | return &m_table.find(name)->second; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | } |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 130 | // Existing FIB entry that may or may not have nextHops |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 131 | else { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 132 | // Existing FIB entry |
| 133 | _LOG_DEBUG("Existing FIB Entry"); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 134 | |
| 135 | // Remove empty FIB entry |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 136 | if (hopsToAdd.getSize() == 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 137 | remove(name); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 138 | return nullptr; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 140 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 141 | FibEntry& entry = (entryIt->second); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 142 | addNextHopsToFibEntryAndNfd(entry, hopsToAdd); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 143 | |
laqinfan | cd7ca05 | 2017-06-02 04:57:18 -0500 | [diff] [blame] | 144 | std::set<NextHop, NextHopComparator> hopsToRemove; |
| 145 | std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(), |
| 146 | hopsToAdd.begin(), hopsToAdd.end(), |
| 147 | std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator()); |
| 148 | |
| 149 | bool isUpdatable = isPrefixUpdatable(entry.getName()); |
| 150 | // Remove the uninstalled next hops from NFD and FIB entry |
| 151 | for (const auto& hop: hopsToRemove){ |
| 152 | if (isUpdatable) { |
| 153 | unregisterPrefix(entry.getName(), hop.getConnectingFaceUri()); |
| 154 | } |
| 155 | _LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName()); |
| 156 | entry.getNexthopList().removeNextHop(hop); |
| 157 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 158 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 159 | // Increment sequence number |
| 160 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 161 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 162 | return &(m_table.find(name)->second); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 167 | Fib::clean() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 169 | _LOG_DEBUG("Fib::clean called"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 170 | for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin(); |
| 171 | it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 172 | ++it) { |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 173 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->second.getName()); |
| 174 | cancelEntryRefresh(it->second); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 175 | for (std::set<NextHop, NextHopComparator>::iterator nhit = |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 176 | (it->second).getNexthopList().getNextHops().begin(); |
| 177 | nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | //Remove entry from NDN-FIB |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 179 | unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 182 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 183 | m_table.clear(); |
| 184 | } |
| 185 | } |
| 186 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 187 | unsigned int |
| 188 | Fib::getNumberOfFacesForName(NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 189 | { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 190 | uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size()); |
| 191 | uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix(); |
| 192 | |
| 193 | // Allow all faces |
| 194 | if (nMaxFaces == 0) { |
| 195 | return nNextHops; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 197 | else { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 198 | return std::min(nNextHops, nMaxFaces); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 199 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 202 | bool |
| 203 | Fib::isPrefixUpdatable(const ndn::Name& name) { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 204 | if (!m_adjacencyList.isNeighbor(name)) { |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 212 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 213 | uint64_t faceCost, const ndn::time::milliseconds& timeout, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 214 | uint64_t flags, uint8_t times) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 215 | { |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 216 | uint64_t faceId = m_adjacencyList.getFaceId(ndn::util::FaceUri(faceUri)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 217 | if (faceId != 0) { |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 218 | ndn::nfd::ControlParameters faceParameters; |
| 219 | faceParameters |
| 220 | .setName(namePrefix) |
| 221 | .setFaceId(faceId) |
| 222 | .setFlags(flags) |
| 223 | .setCost(faceCost) |
| 224 | .setExpirationPeriod(timeout) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 225 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 226 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 227 | _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri |
| 228 | << " Face Id: " << faceId); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 229 | registerPrefixInNfd(faceParameters, faceUri, times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 230 | } |
| 231 | else { |
| 232 | _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri); |
| 233 | } |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 234 | } |
| 235 | |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 236 | void |
| 237 | Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters, |
| 238 | const std::string& faceUri, |
| 239 | uint8_t times) |
| 240 | { |
dmcoomes | 9eaf3f4 | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 241 | _LOG_DEBUG("Registering prefix: " << parameters.getName() << " faceUri: " << faceUri); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 242 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 243 | std::bind(&Fib::onRegistrationSuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 244 | "Successful in name registration", |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 245 | faceUri), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 246 | std::bind(&Fib::onRegistrationFailure, |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame^] | 247 | this, _1, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 248 | "Failed in name registration", |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 249 | parameters, |
| 250 | faceUri, times)); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 251 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 252 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 253 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 254 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 255 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 256 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 257 | _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 258 | if (faceId > 0) { |
| 259 | ndn::nfd::ControlParameters controlParameters; |
| 260 | controlParameters |
| 261 | .setName(namePrefix) |
| 262 | .setFaceId(faceId) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 263 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 264 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 265 | std::bind(&Fib::onUnregistrationSuccess, this, _1, |
| 266 | "Successful in unregistering name"), |
| 267 | std::bind(&Fib::onUnregistrationFailure, |
| 268 | this, _1, |
| 269 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 270 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 274 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 275 | { |
| 276 | ndn::nfd::ControlParameters parameters; |
| 277 | parameters |
| 278 | .setName(name) |
| 279 | .setStrategy(strategy); |
| 280 | |
| 281 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 282 | std::bind(&Fib::onSetStrategySuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 283 | "Successfully set strategy choice"), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 284 | std::bind(&Fib::onSetStrategyFailure, this, _1, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 285 | parameters, |
| 286 | count, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 287 | "Failed to set strategy choice")); |
| 288 | } |
| 289 | |
| 290 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 291 | Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 292 | const std::string& message, const std::string& faceUri) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 293 | { |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 294 | _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() << |
| 295 | " Face Uri: " << faceUri); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 296 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame^] | 297 | // Update the adjacency list with the new Face ID |
| 298 | m_adjacencyList.findAdjacent(faceUri)->setFaceId(commandSuccessResult.getFaceId()); |
| 299 | // Update the fast-access FaceMap with the new Face ID, too |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 300 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 301 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 302 | } |
| 303 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 304 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 305 | Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 306 | const std::string& message, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 307 | const ndn::nfd::ControlParameters& parameters, |
| 308 | const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 309 | uint8_t times) |
| 310 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 311 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 312 | _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 313 | if (times < 3) { |
| 314 | _LOG_DEBUG("Trying to register again..."); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 315 | registerPrefix(parameters.getName(), faceUri, |
| 316 | parameters.getCost(), |
| 317 | parameters.getExpirationPeriod(), |
| 318 | parameters.getFlags(), times+1); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 319 | } |
| 320 | else { |
| 321 | _LOG_DEBUG("Registration trial given up"); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 326 | Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 327 | const std::string& message) |
| 328 | { |
| 329 | _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() << |
| 330 | " Face Id: " << commandSuccessResult.getFaceId()); |
| 331 | } |
| 332 | |
| 333 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 334 | Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response, |
| 335 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 336 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 337 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 338 | } |
| 339 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 340 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 341 | Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 342 | const std::string& message) |
| 343 | { |
| 344 | _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " " |
| 345 | << "for name: " << commandSuccessResult.getName()); |
| 346 | } |
| 347 | |
| 348 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 349 | Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response, |
| 350 | const ndn::nfd::ControlParameters& parameters, |
| 351 | uint32_t count, |
| 352 | const std::string& message) |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 353 | { |
| 354 | _LOG_DEBUG(message << ": " << parameters.getStrategy() << " " |
| 355 | << "for name: " << parameters.getName()); |
| 356 | if (count < 3) { |
| 357 | setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 362 | Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback) |
| 363 | { |
| 364 | _LOG_DEBUG("Scheduling refresh for " << entry.getName() |
| 365 | << " Seq Num: " << entry.getSeqNo() |
| 366 | << " in " << m_refreshTime << " seconds"); |
| 367 | |
| 368 | entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime), |
| 369 | std::bind(&Fib::refreshEntry, this, |
| 370 | entry.getName(), refreshCallback))); |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | Fib::scheduleLoop(FibEntry& entry) |
| 375 | { |
| 376 | scheduleEntryRefresh(entry, |
| 377 | std::bind(&Fib::scheduleLoop, this, _1)); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | Fib::cancelEntryRefresh(const FibEntry& entry) |
| 382 | { |
| 383 | _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 384 | |
| 385 | m_scheduler.cancelEvent(entry.getRefreshEventId()); |
| 386 | entry.getRefreshEventId().reset(); |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb) |
| 391 | { |
| 392 | std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name); |
| 393 | |
| 394 | if (it == m_table.end()) { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | FibEntry& entry = it->second; |
| 399 | _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 400 | |
| 401 | // Increment sequence number |
| 402 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 403 | |
| 404 | for (const NextHop& hop : entry) { |
| 405 | registerPrefix(entry.getName(), |
| 406 | hop.getConnectingFaceUri(), |
| 407 | hop.getRouteCostAsAdjustedInteger(), |
| 408 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 409 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 410 | } |
| 411 | |
| 412 | refreshCb(entry); |
| 413 | } |
| 414 | |
| 415 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 416 | Fib::writeLog() |
| 417 | { |
| 418 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 419 | for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin(); |
| 420 | it != m_table.end(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 421 | ++it) { |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 422 | (it->second).writeLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 425 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 426 | } // namespace nlsr |