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 | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [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> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 29 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | namespace nlsr { |
| 31 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 32 | INIT_LOGGER("Fib"); |
| 33 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 34 | const uint64_t Fib::GRACE_PERIOD = 10; |
| 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | using namespace ndn; |
| 38 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 40 | Fib::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 42 | _LOG_DEBUG("Fib::remove called"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 43 | std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 44 | if (it != m_table.end()) { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 45 | for (std::set<NextHop, NextHopComparator>::iterator nhit = |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 46 | (it->second).getNexthopList().getNextHops().begin(); |
| 47 | nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | //remove entry from NDN-FIB |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 49 | if (isPrefixUpdatable((it->second).getName())) { |
| 50 | unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 51 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | } |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 53 | cancelEntryRefresh(it->second); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | m_table.erase(it); |
| 55 | } |
| 56 | } |
| 57 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 58 | bool |
| 59 | compareFaceUri(const NextHop& hop, const std::string& faceUri) |
| 60 | { |
| 61 | return hop.getConnectingFaceUri() == faceUri; |
| 62 | } |
| 63 | |
| 64 | void |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 65 | Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 66 | { |
| 67 | const ndn::Name& name = entry.getName(); |
| 68 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 69 | for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 70 | { |
| 71 | // Add nexthop to FIB entry |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 72 | entry.getNexthopList().addNextHop(*it); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 73 | |
| 74 | if (isPrefixUpdatable(name)) { |
| 75 | // Add nexthop to NDN-FIB |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 76 | registerPrefix(name, it->getConnectingFaceUri(), |
| 77 | it->getRouteCostAsAdjustedInteger(), |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 78 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 79 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 80 | } |
| 81 | } |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops) |
| 86 | { |
| 87 | _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called"); |
| 88 | |
| 89 | const ndn::Name& name = entry.getName(); |
| 90 | NexthopList& entryHopList = entry.getNexthopList(); |
| 91 | |
| 92 | for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) { |
| 93 | |
| 94 | const std::string& faceUri = it->getConnectingFaceUri(); |
| 95 | |
| 96 | // See if the nexthop is installed in NFD's FIB |
| 97 | NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(), |
| 98 | installedHops.cend(), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 99 | std::bind(&compareFaceUri, _1, faceUri)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 100 | |
| 101 | // The next hop is not installed |
| 102 | if (foundIt == installedHops.cend()) { |
| 103 | |
| 104 | if (isPrefixUpdatable(name)) { |
| 105 | // Remove the nexthop from NDN's FIB |
| 106 | unregisterPrefix(name, it->getConnectingFaceUri()); |
| 107 | } |
| 108 | |
| 109 | // Remove the next hop from the FIB entry |
| 110 | _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name); |
| 111 | // Since the iterator will be invalidated on removal, dereference the original |
| 112 | // and increment the copy |
| 113 | entryHopList.removeNextHop(*(it++)); |
| 114 | } |
| 115 | else { |
| 116 | ++it; |
| 117 | } |
| 118 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 122 | Fib::update(const ndn::Name& name, NexthopList& allHops) |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 123 | { |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 124 | FibEntry* entry = processUpdate(name, allHops); |
| 125 | if (entry != nullptr && !entry->getRefreshEventId()) { |
| 126 | scheduleEntryRefresh(*entry, |
| 127 | [this] (FibEntry& fibEntry) { |
| 128 | this->scheduleLoop(fibEntry); |
| 129 | }); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | FibEntry* |
| 134 | Fib::processUpdate(const ndn::Name& name, NexthopList& allHops) |
| 135 | { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 136 | _LOG_DEBUG("Fib::update called"); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 137 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 138 | // Get the max possible faces which is the minumum of the configuration setting and |
| 139 | // the length of the list of all next hops. |
| 140 | unsigned int maxFaces = getNumberOfFacesForName(allHops); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 141 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 142 | NexthopList hopsToAdd; |
| 143 | unsigned int nFaces = 0; |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 144 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 145 | // Create a list of next hops to be installed with length == maxFaces |
| 146 | for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces; |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 147 | ++it, ++nFaces) { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 148 | hopsToAdd.addNextHop(*it); |
| 149 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 150 | |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 151 | std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 152 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 153 | // New FIB entry that has nextHops |
| 154 | if (entryIt == m_table.end() && hopsToAdd.getSize() != 0) { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 155 | _LOG_DEBUG("New FIB Entry"); |
| 156 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 157 | FibEntry entry(name); |
| 158 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 159 | addNextHopsToFibEntryAndNfd(entry, hopsToAdd); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 160 | |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 161 | m_table.emplace(name, entry); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 162 | |
| 163 | return &m_table.find(name)->second; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | } |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 165 | // Existing FIB entry that may or may not have nextHops |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 166 | else { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 167 | // Existing FIB entry |
| 168 | _LOG_DEBUG("Existing FIB Entry"); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 169 | |
| 170 | // Remove empty FIB entry |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 171 | if (hopsToAdd.getSize() == 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 172 | remove(name); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 173 | return nullptr; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 175 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 176 | FibEntry& entry = (entryIt->second); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 177 | addNextHopsToFibEntryAndNfd(entry, hopsToAdd); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 178 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 179 | removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 180 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 181 | // Increment sequence number |
| 182 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 183 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 184 | return &(m_table.find(name)->second); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 189 | Fib::clean() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 191 | _LOG_DEBUG("Fib::clean called"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 192 | for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin(); |
| 193 | it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 194 | ++it) { |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 195 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->second.getName()); |
| 196 | cancelEntryRefresh(it->second); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 197 | for (std::set<NextHop, NextHopComparator>::iterator nhit = |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 198 | (it->second).getNexthopList().getNextHops().begin(); |
| 199 | nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | //Remove entry from NDN-FIB |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 201 | unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 204 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 205 | m_table.clear(); |
| 206 | } |
| 207 | } |
| 208 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 209 | unsigned int |
| 210 | Fib::getNumberOfFacesForName(NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 212 | uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size()); |
| 213 | uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix(); |
| 214 | |
| 215 | // Allow all faces |
| 216 | if (nMaxFaces == 0) { |
| 217 | return nNextHops; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 219 | else { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 220 | return std::min(nNextHops, nMaxFaces); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 221 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | } |
| 223 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 224 | bool |
| 225 | Fib::isPrefixUpdatable(const ndn::Name& name) { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 226 | if (!m_adjacencyList.isNeighbor(name)) { |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
| 230 | return false; |
| 231 | } |
| 232 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 234 | Fib::createFace(const std::string& faceUri, |
| 235 | const CommandSucceedCallback& onSuccess, |
| 236 | const CommandFailCallback& onFailure) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 237 | { |
Vince Lehman | 27f1add | 2014-10-16 17:14:46 -0500 | [diff] [blame] | 238 | m_faceController.createFace(faceUri, onSuccess, onFailure); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
| 242 | Fib::destroyFace(const std::string& faceUri, |
| 243 | const CommandSucceedCallback& onSuccess, |
| 244 | const CommandFailCallback& onFailure) |
| 245 | { |
| 246 | createFace(faceUri, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 247 | std::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure), |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 248 | onFailure); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult, |
| 253 | const CommandSucceedCallback& onSuccess, |
| 254 | const CommandFailCallback& onFailure) |
| 255 | { |
| 256 | ndn::nfd::ControlParameters faceParameters; |
| 257 | faceParameters |
| 258 | .setFaceId(faceDestroyResult.getFaceId()); |
| 259 | m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters, |
| 260 | onSuccess, |
| 261 | onFailure); |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 266 | uint64_t faceCost, const ndn::time::milliseconds& timeout, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 267 | uint64_t flags, uint8_t times) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 268 | { |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 269 | uint64_t faceId = m_adjacencyList.getFaceId(ndn::util::FaceUri(faceUri)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 270 | if (faceId != 0) { |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 271 | ndn::nfd::ControlParameters faceParameters; |
| 272 | faceParameters |
| 273 | .setName(namePrefix) |
| 274 | .setFaceId(faceId) |
| 275 | .setFlags(flags) |
| 276 | .setCost(faceCost) |
| 277 | .setExpirationPeriod(timeout) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 278 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 279 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 280 | _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri |
| 281 | << " Face Id: " << faceId); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 282 | registerPrefixInNfd(faceParameters, faceUri, times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 283 | } |
| 284 | else { |
| 285 | _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri); |
| 286 | } |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 289 | typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&, |
| 290 | const ndn::nfd::ControlParameters&, uint8_t, |
| 291 | const CommandSucceedCallback&, |
| 292 | const CommandFailCallback&); |
| 293 | |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 294 | void |
| 295 | Fib::registerPrefix(const ndn::Name& namePrefix, |
| 296 | const std::string& faceUri, |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 297 | uint64_t faceCost, |
| 298 | const ndn::time::milliseconds& timeout, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 299 | uint64_t flags, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 300 | uint8_t times, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 301 | const CommandSucceedCallback& onSuccess, |
| 302 | const CommandFailCallback& onFailure) |
| 303 | |
| 304 | { |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 305 | ndn::nfd::ControlParameters parameters; |
| 306 | parameters |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 307 | .setName(namePrefix) |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 308 | .setFlags(flags) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 309 | .setCost(faceCost) |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 310 | .setExpirationPeriod(timeout) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 311 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 312 | createFace(faceUri, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 313 | std::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd), |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 314 | this, _1, parameters, times, onSuccess, onFailure), |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 315 | onFailure); |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters, |
| 320 | const std::string& faceUri, |
| 321 | uint8_t times) |
| 322 | { |
dmcoomes | 9eaf3f4 | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 323 | _LOG_DEBUG("Registering prefix: " << parameters.getName() << " faceUri: " << faceUri); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 324 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 325 | std::bind(&Fib::onRegistrationSuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 326 | "Successful in name registration", |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 327 | faceUri), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 328 | std::bind(&Fib::onRegistrationFailure, |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 329 | this, _1, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 330 | "Failed in name registration", |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 331 | parameters, |
| 332 | faceUri, times)); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 333 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 334 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 335 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 336 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 337 | const ndn::nfd::ControlParameters& parameters, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 338 | uint8_t times, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 339 | const CommandSucceedCallback& onSuccess, |
| 340 | const CommandFailCallback& onFailure) |
| 341 | { |
| 342 | ndn::nfd::ControlParameters controlParameters; |
| 343 | controlParameters |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 344 | .setName(parameters.getName()) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 345 | .setFaceId(faceCreateResult.getFaceId()) |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 346 | .setCost(parameters.getCost()) |
| 347 | .setFlags(parameters.getFlags()) |
| 348 | .setExpirationPeriod(parameters.getExpirationPeriod()) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 349 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 350 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
| 351 | onSuccess, |
| 352 | onFailure); |
| 353 | } |
| 354 | |
| 355 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 356 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 357 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 358 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 359 | _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 360 | if (faceId > 0) { |
| 361 | ndn::nfd::ControlParameters controlParameters; |
| 362 | controlParameters |
| 363 | .setName(namePrefix) |
| 364 | .setFaceId(faceId) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 365 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 366 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 367 | std::bind(&Fib::onUnregistrationSuccess, this, _1, |
| 368 | "Successful in unregistering name"), |
| 369 | std::bind(&Fib::onUnregistrationFailure, |
| 370 | this, _1, |
| 371 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 372 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 376 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 377 | { |
| 378 | ndn::nfd::ControlParameters parameters; |
| 379 | parameters |
| 380 | .setName(name) |
| 381 | .setStrategy(strategy); |
| 382 | |
| 383 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 384 | std::bind(&Fib::onSetStrategySuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 385 | "Successfully set strategy choice"), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 386 | std::bind(&Fib::onSetStrategyFailure, this, _1, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 387 | parameters, |
| 388 | count, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 389 | "Failed to set strategy choice")); |
| 390 | } |
| 391 | |
| 392 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 393 | Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 394 | const std::string& message, const std::string& faceUri) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 395 | { |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 396 | _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() << |
| 397 | " Face Uri: " << faceUri); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 398 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 399 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 400 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 401 | } |
| 402 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 403 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 404 | Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 405 | const std::string& message, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 406 | const ndn::nfd::ControlParameters& parameters, |
| 407 | const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 408 | uint8_t times) |
| 409 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 410 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 411 | _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 412 | if (times < 3) { |
| 413 | _LOG_DEBUG("Trying to register again..."); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 414 | registerPrefix(parameters.getName(), faceUri, |
| 415 | parameters.getCost(), |
| 416 | parameters.getExpirationPeriod(), |
| 417 | parameters.getFlags(), times+1); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 418 | } |
| 419 | else { |
| 420 | _LOG_DEBUG("Registration trial given up"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 425 | Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 426 | const std::string& message) |
| 427 | { |
| 428 | _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() << |
| 429 | " Face Id: " << commandSuccessResult.getFaceId()); |
| 430 | } |
| 431 | |
| 432 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 433 | Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response, |
| 434 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 435 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 436 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 437 | } |
| 438 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 439 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 440 | Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 441 | const std::string& message) |
| 442 | { |
| 443 | _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " " |
| 444 | << "for name: " << commandSuccessResult.getName()); |
| 445 | } |
| 446 | |
| 447 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 448 | Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response, |
| 449 | const ndn::nfd::ControlParameters& parameters, |
| 450 | uint32_t count, |
| 451 | const std::string& message) |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 452 | { |
| 453 | _LOG_DEBUG(message << ": " << parameters.getStrategy() << " " |
| 454 | << "for name: " << parameters.getName()); |
| 455 | if (count < 3) { |
| 456 | setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 461 | Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback) |
| 462 | { |
| 463 | _LOG_DEBUG("Scheduling refresh for " << entry.getName() |
| 464 | << " Seq Num: " << entry.getSeqNo() |
| 465 | << " in " << m_refreshTime << " seconds"); |
| 466 | |
| 467 | entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime), |
| 468 | std::bind(&Fib::refreshEntry, this, |
| 469 | entry.getName(), refreshCallback))); |
| 470 | } |
| 471 | |
| 472 | void |
| 473 | Fib::scheduleLoop(FibEntry& entry) |
| 474 | { |
| 475 | scheduleEntryRefresh(entry, |
| 476 | std::bind(&Fib::scheduleLoop, this, _1)); |
| 477 | } |
| 478 | |
| 479 | void |
| 480 | Fib::cancelEntryRefresh(const FibEntry& entry) |
| 481 | { |
| 482 | _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 483 | |
| 484 | m_scheduler.cancelEvent(entry.getRefreshEventId()); |
| 485 | entry.getRefreshEventId().reset(); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb) |
| 490 | { |
| 491 | std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name); |
| 492 | |
| 493 | if (it == m_table.end()) { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | FibEntry& entry = it->second; |
| 498 | _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 499 | |
| 500 | // Increment sequence number |
| 501 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 502 | |
| 503 | for (const NextHop& hop : entry) { |
| 504 | registerPrefix(entry.getName(), |
| 505 | hop.getConnectingFaceUri(), |
| 506 | hop.getRouteCostAsAdjustedInteger(), |
| 507 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 508 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 509 | } |
| 510 | |
| 511 | refreshCb(entry); |
| 512 | } |
| 513 | |
| 514 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 515 | Fib::writeLog() |
| 516 | { |
| 517 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 518 | for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin(); |
| 519 | it != m_table.end(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 520 | ++it) { |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 521 | (it->second).writeLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 522 | } |
| 523 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 524 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 525 | } // namespace nlsr |