akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 2 | /* |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 20 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 21 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 22 | #ifndef NLSR_ROUTE_FIB_HPP |
| 23 | #define NLSR_ROUTE_FIB_HPP |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 24 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 25 | #include "test-access-control.hpp" |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 26 | #include "nexthop-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | |
Nick Gordon | 2108808 | 2017-05-24 10:57:06 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/util/scheduler.hpp> |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/util/time.hpp> |
| 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 34 | using NextHopsUriSortedSet = NexthopListT<NextHopUriSortedComparator>; |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 36 | struct FibEntry |
| 37 | { |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 38 | ndn::Name name; |
| 39 | ndn::scheduler::ScopedEventId refreshEventId; |
| 40 | int32_t seqNo = 1; |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 41 | NextHopsUriSortedSet nexthopSet; |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 44 | using AfterRefreshCallback = std::function<void(FibEntry&)>; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 45 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 46 | class AdjacencyList; |
| 47 | class ConfParameter; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 49 | /*! \brief Maps names to lists of next hops, and exports this information to NFD. |
| 50 | * |
| 51 | * The FIB (Forwarding Information Base) is the "authoritative" source |
| 52 | * of how to route Interests on this router to other nodes running |
| 53 | * NLSR. In essence, the FIB is a map that takes name prefixes to a |
| 54 | * list of next-hops out of this router. This class also contains |
| 55 | * methods to inform NFD about these relationships. The FIB has its |
| 56 | * entries populated by the NamePrefixTable |
| 57 | * |
| 58 | * \sa nlsr::NamePrefixTable |
| 59 | * \sa nlsr::NamePrefixTable::addEntry |
| 60 | * \sa nlsr::NamePrefixTable::updateWithNewRoute |
| 61 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | class Fib |
| 63 | { |
| 64 | public: |
Ashlesh Gawande | e5002b3 | 2018-12-20 21:07:31 -0600 | [diff] [blame] | 65 | Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 66 | ConfParameter& conf, ndn::security::KeyChain& keyChain); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 67 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 68 | /*! \brief Completely remove a name prefix from the FIB. |
| 69 | * |
| 70 | * If a name prefix is found to no longer be reachable from this |
| 71 | * router, it will be removed from the FIB and all of its next-hops |
| 72 | * will be unregistered from NFD. |
| 73 | * |
| 74 | * \sa nlsr::NamePrefixTable::removeEntry |
| 75 | */ |
Ashlesh Gawande | 744e481 | 2018-08-22 16:26:24 -0500 | [diff] [blame] | 76 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 77 | remove(const ndn::Name& name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 79 | /*! \brief Set the nexthop list of a name. |
| 80 | * |
| 81 | * This method is the entry for others to add next-hop information |
| 82 | * to the FIB. Formally put, this method registers in NFD all |
| 83 | * next-hops in allHops, and unregisters the set difference of |
| 84 | * newHops - oldHops. This method also schedules the regular refresh |
| 85 | * of those next hops. |
| 86 | * |
| 87 | * \param name The name prefix that the next-hops apply to |
| 88 | * \param allHops A complete list of next-hops to associate with name. |
| 89 | */ |
Ashlesh Gawande | 744e481 | 2018-08-22 16:26:24 -0500 | [diff] [blame] | 90 | void |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 91 | update(const ndn::Name& name, const NexthopList& allHops); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 92 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 93 | /*! \brief Remove all entries from the FIB. |
| 94 | * |
| 95 | * This method is called before terminating NLSR to minimize the |
| 96 | * time NFD spends routing on now-invalid information. This is not |
| 97 | * strictly necessary, because eventually those prefix registrations |
| 98 | * will expire, but cleaning up after ourselves improves |
| 99 | * performance. |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 100 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 102 | clean(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | |
| 104 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 105 | setEntryRefreshTime(int32_t fert) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | { |
| 107 | m_refreshTime = fert; |
| 108 | } |
| 109 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 110 | /*! \brief Inform NFD of a next-hop |
| 111 | * |
| 112 | * This method informs NFD of a next-hop for some name prefix. This |
| 113 | * method actually submits the information to NFD's RIB, which then |
| 114 | * aggregates its own best hops and updates NFD's (the actual) |
| 115 | * FIB. Typically, NLSR's FIB and NFD's FIB will be almost the |
| 116 | * same. However, this is not necessarily the case and there may be |
| 117 | * cases when other sources of information provide better next-hops |
| 118 | * to NFD that NLSR doesn't know about. For example, an operator |
| 119 | * could set up a direct link to a node that isn't running NLSR. |
| 120 | * |
| 121 | * \param namePrefix The name prefix to register a next-hop for |
| 122 | * \param faceUri The faceUri of the adjacent that this prefix can be reached through |
| 123 | * \param faceCost The cost to reach namePrefix through faceUri |
| 124 | * \param timeout How long this registration should last |
| 125 | * \param flags Route inheritance flags (CAPTURE, CHILD_INHERIT) |
| 126 | * \param times How many times we have failed to register this prefix since the last success. |
| 127 | * |
| 128 | * \sa Fib::registerPrefixInNfd |
| 129 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 131 | registerPrefix(const ndn::Name& namePrefix, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 132 | const ndn::FaceUri& faceUri, |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 133 | uint64_t faceCost, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 134 | const ndn::time::milliseconds& timeout, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 135 | uint64_t flags, |
| 136 | uint8_t times); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 137 | |
| 138 | void |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 139 | setStrategy(const ndn::Name& name, const ndn::Name& strategy, uint32_t count); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 140 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 141 | void |
| 142 | writeLog(); |
| 143 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 144 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 145 | /*! \brief Indicates whether a prefix is a direct neighbor or not. |
| 146 | * |
| 147 | * \return Whether the name is NOT associated with a direct neighbor |
| 148 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 149 | bool |
Ashlesh Gawande | e5002b3 | 2018-12-20 21:07:31 -0600 | [diff] [blame] | 150 | isNotNeighbor(const ndn::Name& name); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 151 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 152 | /*! \brief Does one half of the updating of a FibEntry with new next-hops. |
| 153 | * |
| 154 | * Adds nexthops to a FibEntry and registers them in NFD. |
| 155 | * \sa Fib::update |
| 156 | * \sa Fib::removeOldNextHopsFromFibEntryAndNfd |
| 157 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 158 | void |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 159 | addNextHopsToFibEntryAndNfd(FibEntry& entry, const NextHopsUriSortedSet& hopsToAdd); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 160 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 161 | unsigned int |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 162 | getNumberOfFacesForName(const NexthopList& nextHopList); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 163 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 164 | /*! \brief Unregisters a prefix from NFD's RIB. |
| 165 | * |
| 166 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 167 | void |
| 168 | unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri); |
| 169 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 170 | /*! \brief Log registration success, and update the Face ID associated with a URI. |
| 171 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 172 | void |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 173 | onRegistrationSuccess(const ndn::nfd::ControlParameters& param, |
| 174 | const ndn::FaceUri& faceUri); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 175 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 176 | /*! \brief Retry a prefix (next-hop) registration up to three (3) times. |
| 177 | */ |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 178 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 179 | onRegistrationFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 180 | const ndn::nfd::ControlParameters& parameters, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 181 | const ndn::FaceUri& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 182 | uint8_t times); |
| 183 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 184 | /*! \brief Log a successful strategy setting. |
| 185 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 186 | void |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 187 | onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult); |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 188 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 189 | /*! \brief Retry a strategy setting up to three (3) times. |
| 190 | */ |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 191 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 192 | onSetStrategyFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 193 | const ndn::nfd::ControlParameters& parameters, |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 194 | uint32_t count); |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 195 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 196 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 197 | /*! \brief Schedule a refresh event for an entry. |
| 198 | * |
| 199 | * Schedules a refresh event for an entry. In order to form a |
| 200 | * perpetual loop, refreshCallback needs to call |
| 201 | * Fib::scheduleEntryRefresh in some way, with refreshCallback being |
| 202 | * the same each time. In the current implementation, this is |
| 203 | * accomplished by having a separate function, Fib::scheduleLoop, |
| 204 | * that does this work. |
| 205 | * \sa Fib::scheduleLoop |
| 206 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 207 | void |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 208 | scheduleEntryRefresh(FibEntry& entry, const AfterRefreshCallback& refreshCb); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 209 | |
| 210 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 211 | /*! \brief Continue the entry refresh cycle. |
| 212 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 213 | void |
| 214 | scheduleLoop(FibEntry& entry); |
| 215 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 216 | /*! \brief Refreshes an entry in NFD. |
| 217 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 218 | void |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 219 | refreshEntry(const ndn::Name& name, AfterRefreshCallback refreshCb); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 220 | |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 221 | public: |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 222 | static inline const ndn::Name MULTICAST_STRATEGY{"/localhost/nfd/strategy/multicast"}; |
| 223 | static inline const ndn::Name BEST_ROUTE_STRATEGY{"/localhost/nfd/strategy/best-route"}; |
| 224 | |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame^] | 225 | ndn::signal::Signal<Fib, ndn::Name> onPrefixRegistrationSuccess; |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 226 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | private: |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 228 | ndn::Scheduler& m_scheduler; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 229 | int32_t m_refreshTime; |
| 230 | ndn::nfd::Controller m_controller; |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 231 | |
| 232 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 233 | std::map<ndn::Name, FibEntry> m_table; |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 234 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 235 | private: |
| 236 | AdjacencyList& m_adjacencyList; |
| 237 | ConfParameter& m_confParameter; |
| 238 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 239 | /*! GRACE_PERIOD A "window" we append to the timeout time to |
| 240 | * allow for things like stuttering prefix registrations and |
| 241 | * processing time when refreshing events. |
| 242 | */ |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 243 | static constexpr uint64_t GRACE_PERIOD = 10; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 244 | }; |
| 245 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 246 | } // namespace nlsr |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 247 | |
| 248 | #endif // NLSR_ROUTE_FIB_HPP |