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