akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | c6a8522 | 2017-01-03 16:54:34 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
| 20 | * |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 21 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 23 | #ifndef NLSR_ROUTE_FIB_HPP |
| 24 | #define NLSR_ROUTE_FIB_HPP |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 25 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 26 | #include "face-map.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | #include "fib-entry.hpp" |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 28 | #include "test-access-control.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Nick Gordon | 2108808 | 2017-05-24 10:57:06 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/time.hpp> |
| 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | namespace nlsr { |
| 34 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 35 | typedef std::function<void(FibEntry&)> afterRefreshCallback; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 36 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 37 | class AdjacencyList; |
| 38 | class ConfParameter; |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 39 | class FibEntry; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 41 | /*! \brief Maps names to lists of next hops, and exports this information to NFD. |
| 42 | * |
| 43 | * The FIB (Forwarding Information Base) is the "authoritative" source |
| 44 | * of how to route Interests on this router to other nodes running |
| 45 | * NLSR. In essence, the FIB is a map that takes name prefixes to a |
| 46 | * list of next-hops out of this router. This class also contains |
| 47 | * methods to inform NFD about these relationships. The FIB has its |
| 48 | * entries populated by the NamePrefixTable |
| 49 | * |
| 50 | * \sa nlsr::NamePrefixTable |
| 51 | * \sa nlsr::NamePrefixTable::addEntry |
| 52 | * \sa nlsr::NamePrefixTable::updateWithNewRoute |
| 53 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | class Fib |
| 55 | { |
| 56 | public: |
Vince Lehman | b7079a1 | 2014-11-04 12:45:50 -0600 | [diff] [blame] | 57 | Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList, ConfParameter& conf, |
| 58 | ndn::KeyChain& keyChain) |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 59 | : m_scheduler(scheduler) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | , m_refreshTime(0) |
Vince Lehman | b7079a1 | 2014-11-04 12:45:50 -0600 | [diff] [blame] | 61 | , m_controller(face, keyChain) |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 62 | , m_adjacencyList(adjacencyList) |
| 63 | , m_confParameter(conf) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 64 | { |
| 65 | } |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 66 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 67 | /*! \brief Completely remove a name prefix from the FIB. |
| 68 | * |
| 69 | * If a name prefix is found to no longer be reachable from this |
| 70 | * router, it will be removed from the FIB and all of its next-hops |
| 71 | * will be unregistered from NFD. |
| 72 | * |
| 73 | * \sa nlsr::NamePrefixTable::removeEntry |
| 74 | */ |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 75 | VIRTUAL_WITH_TESTS void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 76 | remove(const ndn::Name& name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 78 | /*! \brief Set the nexthop list of a name. |
| 79 | * |
| 80 | * This method is the entry for others to add next-hop information |
| 81 | * to the FIB. Formally put, this method registers in NFD all |
| 82 | * next-hops in allHops, and unregisters the set difference of |
| 83 | * newHops - oldHops. This method also schedules the regular refresh |
| 84 | * of those next hops. |
| 85 | * |
| 86 | * \param name The name prefix that the next-hops apply to |
| 87 | * \param allHops A complete list of next-hops to associate with name. |
| 88 | */ |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 89 | VIRTUAL_WITH_TESTS void |
| 90 | update(const ndn::Name& name, NexthopList& allHops); |
| 91 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 92 | /*! \brief Remove all entries from the FIB. |
| 93 | * |
| 94 | * This method is called before terminating NLSR to minimize the |
| 95 | * time NFD spends routing on now-invalid information. This is not |
| 96 | * strictly necessary, because eventually those prefix registrations |
| 97 | * will expire, but cleaning up after ourselves improves |
| 98 | * performance. |
| 99 | * |
| 100 | * \sa NlsrRunner::run |
| 101 | * |
| 102 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 104 | clean(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | |
| 106 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 107 | setEntryRefreshTime(int32_t fert) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | { |
| 109 | m_refreshTime = fert; |
| 110 | } |
| 111 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 112 | /*! \brief Inform NFD of a next-hop |
| 113 | * |
| 114 | * This method informs NFD of a next-hop for some name prefix. This |
| 115 | * method actually submits the information to NFD's RIB, which then |
| 116 | * aggregates its own best hops and updates NFD's (the actual) |
| 117 | * FIB. Typically, NLSR's FIB and NFD's FIB will be almost the |
| 118 | * same. However, this is not necessarily the case and there may be |
| 119 | * cases when other sources of information provide better next-hops |
| 120 | * to NFD that NLSR doesn't know about. For example, an operator |
| 121 | * could set up a direct link to a node that isn't running NLSR. |
| 122 | * |
| 123 | * \param namePrefix The name prefix to register a next-hop for |
| 124 | * \param faceUri The faceUri of the adjacent that this prefix can be reached through |
| 125 | * \param faceCost The cost to reach namePrefix through faceUri |
| 126 | * \param timeout How long this registration should last |
| 127 | * \param flags Route inheritance flags (CAPTURE, CHILD_INHERIT) |
| 128 | * \param times How many times we have failed to register this prefix since the last success. |
| 129 | * |
| 130 | * \sa Fib::registerPrefixInNfd |
| 131 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 133 | registerPrefix(const ndn::Name& namePrefix, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 134 | const ndn::util::FaceUri& faceUri, |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 135 | uint64_t faceCost, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 136 | const ndn::time::milliseconds& timeout, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 137 | uint64_t flags, |
| 138 | uint8_t times); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 139 | |
| 140 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 141 | setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 142 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 143 | void |
| 144 | writeLog(); |
| 145 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 146 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 147 | /*! \brief Indicates whether a prefix is a direct neighbor or not. |
| 148 | * |
| 149 | * \return Whether the name is NOT associated with a direct neighbor |
| 150 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 151 | bool |
| 152 | isPrefixUpdatable(const ndn::Name& name); |
| 153 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 154 | /*! \brief Does one half of the updating of a FibEntry with new next-hops. |
| 155 | * |
| 156 | * Adds nexthops to a FibEntry and registers them in NFD. |
| 157 | * \sa Fib::update |
| 158 | * \sa Fib::removeOldNextHopsFromFibEntryAndNfd |
| 159 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 160 | void |
| 161 | addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd); |
| 162 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 163 | unsigned int |
| 164 | getNumberOfFacesForName(NexthopList& nextHopList); |
| 165 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 166 | /*! \brief Unregisters a prefix from NFD's RIB. |
| 167 | * |
| 168 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 169 | void |
| 170 | unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri); |
| 171 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 172 | /*! \brief Log registration success, and update the Face ID associated with a URI. |
| 173 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 174 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 175 | onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 176 | const std::string& message, const ndn::util::FaceUri& faceUri); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 177 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 178 | /*! \brief Retry a prefix (next-hop) registration up to three (3) times. |
| 179 | */ |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 180 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 181 | onRegistrationFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 182 | const std::string& message, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 183 | const ndn::nfd::ControlParameters& parameters, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 184 | const ndn::util::FaceUri& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 185 | uint8_t times); |
| 186 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 187 | /*! \brief Log a successful unregistration. |
| 188 | */ |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 189 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 190 | onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 191 | const std::string& message); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 192 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 193 | /*! \brief Log an unregistration failure. Does not retry. |
| 194 | */ |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 195 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 196 | onUnregistrationFailure(const ndn::nfd::ControlResponse& response, |
| 197 | const std::string& message); |
| 198 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 199 | /*! \brief Log a successful strategy setting. |
| 200 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 201 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 202 | onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 203 | const std::string& message); |
| 204 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 205 | /*! \brief Retry a strategy setting up to three (3) times. |
| 206 | */ |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 207 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 208 | onSetStrategyFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 209 | const ndn::nfd::ControlParameters& parameters, |
| 210 | uint32_t count, |
| 211 | const std::string& message); |
| 212 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 213 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 214 | /*! \brief Internal portion of Fib::update. |
| 215 | * \sa Fib::update |
| 216 | */ |
| 217 | FibEntry* |
| 218 | processUpdate(const ndn::Name& name, NexthopList& allHops); |
| 219 | |
| 220 | /*! \brief Schedule a refresh event for an entry. |
| 221 | * |
| 222 | * Schedules a refresh event for an entry. In order to form a |
| 223 | * perpetual loop, refreshCallback needs to call |
| 224 | * Fib::scheduleEntryRefresh in some way, with refreshCallback being |
| 225 | * the same each time. In the current implementation, this is |
| 226 | * accomplished by having a separate function, Fib::scheduleLoop, |
| 227 | * that does this work. |
| 228 | * \sa Fib::scheduleLoop |
| 229 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 230 | void |
| 231 | scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCb); |
| 232 | |
| 233 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 234 | /*! \brief Continue the entry refresh cycle. |
| 235 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 236 | void |
| 237 | scheduleLoop(FibEntry& entry); |
| 238 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 239 | /*! \brief Cancel an entry's refresh event. |
| 240 | * |
| 241 | * Cancel an entry's refresh event. This only needs to be done when |
| 242 | * an entry is removed. Typically this happens when NLSR is |
| 243 | * terminated or crashes, and we don't want the scheduler to crash |
| 244 | * because it's referencing memory that has no valid function. |
| 245 | * |
| 246 | * \sa NlsrRunner::run |
| 247 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 248 | void |
| 249 | cancelEntryRefresh(const FibEntry& entry); |
| 250 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 251 | /*! \brief Refreshes an entry in NFD. |
| 252 | */ |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 253 | void |
| 254 | refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb); |
| 255 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 256 | private: |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 257 | ndn::Scheduler& m_scheduler; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 258 | int32_t m_refreshTime; |
| 259 | ndn::nfd::Controller m_controller; |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 260 | |
| 261 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 262 | FaceMap m_faceMap; |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 263 | std::map<ndn::Name, FibEntry> m_table; |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 264 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 265 | private: |
| 266 | AdjacencyList& m_adjacencyList; |
| 267 | ConfParameter& m_confParameter; |
| 268 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 269 | /*! GRACE_PERIOD A "window" we append to the timeout time to |
| 270 | * allow for things like stuttering prefix registrations and |
| 271 | * processing time when refreshing events. |
| 272 | */ |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 273 | static const uint64_t GRACE_PERIOD; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 274 | }; |
| 275 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 276 | } // namespace nlsr |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 277 | |
| 278 | #endif // NLSR_ROUTE_FIB_HPP |