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