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 | { |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 269 | uint64_t faceId = m_adjacencyList.getFaceId(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 | { |
| 323 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 324 | std::bind(&Fib::onRegistrationSuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 325 | "Successful in name registration", |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 326 | faceUri), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 327 | std::bind(&Fib::onRegistrationFailure, |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 328 | this, _1, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 329 | "Failed in name registration", |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 330 | parameters, |
| 331 | faceUri, times)); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 332 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 333 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 334 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 335 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 336 | const ndn::nfd::ControlParameters& parameters, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 337 | uint8_t times, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 338 | const CommandSucceedCallback& onSuccess, |
| 339 | const CommandFailCallback& onFailure) |
| 340 | { |
| 341 | ndn::nfd::ControlParameters controlParameters; |
| 342 | controlParameters |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 343 | .setName(parameters.getName()) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 344 | .setFaceId(faceCreateResult.getFaceId()) |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 345 | .setCost(parameters.getCost()) |
| 346 | .setFlags(parameters.getFlags()) |
| 347 | .setExpirationPeriod(parameters.getExpirationPeriod()) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 348 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 349 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
| 350 | onSuccess, |
| 351 | onFailure); |
| 352 | } |
| 353 | |
| 354 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 355 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 356 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 357 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 358 | _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 359 | if (faceId > 0) { |
| 360 | ndn::nfd::ControlParameters controlParameters; |
| 361 | controlParameters |
| 362 | .setName(namePrefix) |
| 363 | .setFaceId(faceId) |
Davide Pesavento | 87911da | 2017-02-21 22:10:55 -0500 | [diff] [blame] | 364 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 365 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 366 | std::bind(&Fib::onUnregistrationSuccess, this, _1, |
| 367 | "Successful in unregistering name"), |
| 368 | std::bind(&Fib::onUnregistrationFailure, |
| 369 | this, _1, |
| 370 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 371 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 375 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 376 | { |
| 377 | ndn::nfd::ControlParameters parameters; |
| 378 | parameters |
| 379 | .setName(name) |
| 380 | .setStrategy(strategy); |
| 381 | |
| 382 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 383 | std::bind(&Fib::onSetStrategySuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 384 | "Successfully set strategy choice"), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 385 | std::bind(&Fib::onSetStrategyFailure, this, _1, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 386 | parameters, |
| 387 | count, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 388 | "Failed to set strategy choice")); |
| 389 | } |
| 390 | |
| 391 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 392 | Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 393 | const std::string& message, const std::string& faceUri) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 394 | { |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 395 | _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() << |
| 396 | " Face Uri: " << faceUri); |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 397 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 398 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 399 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 400 | } |
| 401 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 402 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 403 | Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 404 | const std::string& message, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 405 | const ndn::nfd::ControlParameters& parameters, |
| 406 | const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 407 | uint8_t times) |
| 408 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 409 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 410 | _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 411 | if (times < 3) { |
| 412 | _LOG_DEBUG("Trying to register again..."); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 413 | registerPrefix(parameters.getName(), faceUri, |
| 414 | parameters.getCost(), |
| 415 | parameters.getExpirationPeriod(), |
| 416 | parameters.getFlags(), times+1); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 417 | } |
| 418 | else { |
| 419 | _LOG_DEBUG("Registration trial given up"); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 424 | Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 425 | const std::string& message) |
| 426 | { |
| 427 | _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() << |
| 428 | " Face Id: " << commandSuccessResult.getFaceId()); |
| 429 | } |
| 430 | |
| 431 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 432 | Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response, |
| 433 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 434 | { |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 435 | _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 436 | } |
| 437 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 438 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 439 | Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 440 | const std::string& message) |
| 441 | { |
| 442 | _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " " |
| 443 | << "for name: " << commandSuccessResult.getName()); |
| 444 | } |
| 445 | |
| 446 | void |
Junxiao Shi | 63bd034 | 2016-08-17 16:57:14 +0000 | [diff] [blame] | 447 | Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response, |
| 448 | const ndn::nfd::ControlParameters& parameters, |
| 449 | uint32_t count, |
| 450 | const std::string& message) |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 451 | { |
| 452 | _LOG_DEBUG(message << ": " << parameters.getStrategy() << " " |
| 453 | << "for name: " << parameters.getName()); |
| 454 | if (count < 3) { |
| 455 | setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame^] | 460 | Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback) |
| 461 | { |
| 462 | _LOG_DEBUG("Scheduling refresh for " << entry.getName() |
| 463 | << " Seq Num: " << entry.getSeqNo() |
| 464 | << " in " << m_refreshTime << " seconds"); |
| 465 | |
| 466 | entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime), |
| 467 | std::bind(&Fib::refreshEntry, this, |
| 468 | entry.getName(), refreshCallback))); |
| 469 | } |
| 470 | |
| 471 | void |
| 472 | Fib::scheduleLoop(FibEntry& entry) |
| 473 | { |
| 474 | scheduleEntryRefresh(entry, |
| 475 | std::bind(&Fib::scheduleLoop, this, _1)); |
| 476 | } |
| 477 | |
| 478 | void |
| 479 | Fib::cancelEntryRefresh(const FibEntry& entry) |
| 480 | { |
| 481 | _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 482 | |
| 483 | m_scheduler.cancelEvent(entry.getRefreshEventId()); |
| 484 | entry.getRefreshEventId().reset(); |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb) |
| 489 | { |
| 490 | std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name); |
| 491 | |
| 492 | if (it == m_table.end()) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | FibEntry& entry = it->second; |
| 497 | _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo()); |
| 498 | |
| 499 | // Increment sequence number |
| 500 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 501 | |
| 502 | for (const NextHop& hop : entry) { |
| 503 | registerPrefix(entry.getName(), |
| 504 | hop.getConnectingFaceUri(), |
| 505 | hop.getRouteCostAsAdjustedInteger(), |
| 506 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 507 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 508 | } |
| 509 | |
| 510 | refreshCb(entry); |
| 511 | } |
| 512 | |
| 513 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 514 | Fib::writeLog() |
| 515 | { |
| 516 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 517 | for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin(); |
| 518 | it != m_table.end(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 519 | ++it) { |
Nick Gordon | b716847 | 2016-09-21 13:57:17 -0500 | [diff] [blame] | 520 | (it->second).writeLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 521 | } |
| 522 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 523 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 524 | } // namespace nlsr |