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