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