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