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