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