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