Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "self-learning-strategy.hpp" |
| 27 | #include "algorithm.hpp" |
| 28 | |
| 29 | #include "core/logger.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 30 | #include "daemon/global.hpp" |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 31 | #include "rib/service.hpp" |
| 32 | |
| 33 | #include <ndn-cxx/lp/empty-value.hpp> |
| 34 | #include <ndn-cxx/lp/prefix-announcement-header.hpp> |
| 35 | #include <ndn-cxx/lp/tags.hpp> |
| 36 | |
| 37 | #include <boost/range/adaptor/reversed.hpp> |
| 38 | |
| 39 | namespace nfd { |
| 40 | namespace fw { |
| 41 | |
| 42 | NFD_LOG_INIT(SelfLearningStrategy); |
| 43 | NFD_REGISTER_STRATEGY(SelfLearningStrategy); |
| 44 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 45 | const time::milliseconds SelfLearningStrategy::ROUTE_RENEW_LIFETIME(10_min); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 46 | |
| 47 | SelfLearningStrategy::SelfLearningStrategy(Forwarder& forwarder, const Name& name) |
| 48 | : Strategy(forwarder) |
| 49 | { |
| 50 | ParsedInstanceName parsed = parseInstanceName(name); |
| 51 | if (!parsed.parameters.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 52 | NDN_THROW(std::invalid_argument("SelfLearningStrategy does not accept parameters")); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 53 | } |
| 54 | if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 55 | NDN_THROW(std::invalid_argument( |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 56 | "SelfLearningStrategy does not support version " + to_string(*parsed.version))); |
| 57 | } |
| 58 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
| 59 | } |
| 60 | |
| 61 | const Name& |
| 62 | SelfLearningStrategy::getStrategyName() |
| 63 | { |
| 64 | static Name strategyName("/localhost/nfd/strategy/self-learning/%FD%01"); |
| 65 | return strategyName; |
| 66 | } |
| 67 | |
| 68 | void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 69 | SelfLearningStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 70 | const shared_ptr<pit::Entry>& pitEntry) |
| 71 | { |
| 72 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 73 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
| 74 | |
| 75 | bool isNonDiscovery = interest.getTag<lp::NonDiscoveryTag>() != nullptr; |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 76 | auto inRecordInfo = pitEntry->getInRecord(ingress.face, ingress.endpoint)->insertStrategyInfo<InRecordInfo>().first; |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 77 | if (isNonDiscovery) { // "non-discovery" Interest |
| 78 | inRecordInfo->isNonDiscoveryInterest = true; |
| 79 | if (nexthops.empty()) { // return NACK if no matching FIB entry exists |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 80 | NFD_LOG_DEBUG("NACK non-discovery Interest=" << interest << " from=" << ingress << " noNextHop"); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 81 | lp::NackHeader nackHeader; |
| 82 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 83 | this->sendNack(pitEntry, ingress, nackHeader); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 84 | this->rejectPendingInterest(pitEntry); |
| 85 | } |
| 86 | else { // multicast it if matching FIB entry exists |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 87 | multicastInterest(interest, ingress.face, pitEntry, nexthops); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | else { // "discovery" Interest |
| 91 | inRecordInfo->isNonDiscoveryInterest = false; |
| 92 | if (nexthops.empty()) { // broadcast it if no matching FIB entry exists |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 93 | broadcastInterest(interest, ingress.face, pitEntry); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 94 | } |
| 95 | else { // multicast it with "non-discovery" mark if matching FIB entry exists |
| 96 | interest.setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{})); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 97 | multicastInterest(interest, ingress.face, pitEntry, nexthops); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | SelfLearningStrategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry, |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 104 | const FaceEndpoint& ingress, const Data& data) |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 105 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 106 | OutRecordInfo* outRecordInfo = pitEntry->getOutRecord(ingress.face, ingress.endpoint)->getStrategyInfo<OutRecordInfo>(); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 107 | if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery |
| 108 | if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases) |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 109 | sendDataToAll(pitEntry, ingress, data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 110 | } |
| 111 | else { // needs a PA (to respond discovery Interest) |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 112 | asyncProcessData(pitEntry, ingress.face, data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | else { // outgoing Interest was discovery |
| 116 | auto paTag = data.getTag<lp::PrefixAnnouncementTag>(); |
| 117 | if (paTag != nullptr) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 118 | addRoute(pitEntry, ingress.face, data, *paTag->get().getPrefixAnn()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 119 | } |
| 120 | else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning |
| 121 | } |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 122 | sendDataToAll(pitEntry, ingress, data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 127 | SelfLearningStrategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 128 | const shared_ptr<pit::Entry>& pitEntry) |
| 129 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 130 | NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << ingress |
| 131 | << " reason=" << nack.getReason()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 132 | if (nack.getReason() == lp::NackReason::NO_ROUTE) { // remove FIB entries |
| 133 | BOOST_ASSERT(this->lookupFib(*pitEntry).hasNextHops()); |
| 134 | NFD_LOG_DEBUG("Send NACK to all downstreams"); |
| 135 | this->sendNacks(pitEntry, nack.getHeader()); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 136 | renewRoute(nack.getInterest().getName(), ingress.face.getId(), 0_ms); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | SelfLearningStrategy::broadcastInterest(const Interest& interest, const Face& inFace, |
| 142 | const shared_ptr<pit::Entry>& pitEntry) |
| 143 | { |
| 144 | for (auto& outFace : this->getFaceTable() | boost::adaptors::reversed) { |
| 145 | if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) || |
| 146 | wouldViolateScope(inFace, interest, outFace) || outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 147 | continue; |
| 148 | } |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 149 | this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 150 | pitEntry->getOutRecord(outFace, 0)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false; |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 151 | NFD_LOG_DEBUG("send discovery Interest=" << interest << " from=" |
| 152 | << inFace.getId() << " to=" << outFace.getId()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | SelfLearningStrategy::multicastInterest(const Interest& interest, const Face& inFace, |
| 158 | const shared_ptr<pit::Entry>& pitEntry, |
| 159 | const fib::NextHopList& nexthops) |
| 160 | { |
| 161 | for (const auto& nexthop : nexthops) { |
| 162 | Face& outFace = nexthop.getFace(); |
| 163 | if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) || |
| 164 | wouldViolateScope(inFace, interest, outFace)) { |
| 165 | continue; |
| 166 | } |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 167 | this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 168 | pitEntry->getOutRecord(outFace, 0)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true; |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 169 | NFD_LOG_DEBUG("send non-discovery Interest=" << interest << " from=" |
| 170 | << inFace.getId() << " to=" << outFace.getId()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data) |
| 176 | { |
| 177 | // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first |
| 178 | // to ensure that the entry will not be removed before the whole processing is finished |
| 179 | // (the PIT entry's expiry timer was set to 0 before dispatching) |
| 180 | this->setExpiryTimer(pitEntry, 1_s); |
| 181 | |
| 182 | runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, this] { |
| 183 | rib::Service::get().getRibManager().slFindAnn(data.getName(), |
| 184 | [pitEntryWeak, inFaceId, data, this] (optional<ndn::PrefixAnnouncement> paOpt) { |
| 185 | if (paOpt) { |
| 186 | runOnMainIoService([pitEntryWeak, inFaceId, data, pa = std::move(*paOpt), this] { |
| 187 | auto pitEntry = pitEntryWeak.lock(); |
| 188 | auto inFace = this->getFace(inFaceId); |
| 189 | if (pitEntry && inFace) { |
| 190 | NFD_LOG_DEBUG("found PrefixAnnouncement=" << pa.getAnnouncedName()); |
| 191 | data.setTag(make_shared<lp::PrefixAnnouncementTag>(lp::PrefixAnnouncementHeader(pa))); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 192 | this->sendDataToAll(pitEntry, FaceEndpoint(*inFace, 0), data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 193 | this->setExpiryTimer(pitEntry, 0_ms); |
| 194 | } |
| 195 | else { |
| 196 | NFD_LOG_DEBUG("PIT entry or Face no longer exists"); |
| 197 | } |
| 198 | }); |
| 199 | } |
| 200 | }); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | bool |
| 205 | SelfLearningStrategy::needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry) |
| 206 | { |
| 207 | bool hasDiscoveryInterest = false; |
| 208 | bool directToConsumer = true; |
| 209 | |
| 210 | auto now = time::steady_clock::now(); |
| 211 | for (const auto& inRecord : pitEntry->getInRecords()) { |
| 212 | if (inRecord.getExpiry() > now) { |
| 213 | InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>(); |
| 214 | if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) { |
| 215 | hasDiscoveryInterest = true; |
| 216 | } |
| 217 | if (inRecord.getFace().getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 218 | directToConsumer = false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return hasDiscoveryInterest && !directToConsumer; |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | SelfLearningStrategy::addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, |
| 227 | const Data& data, const ndn::PrefixAnnouncement& pa) |
| 228 | { |
| 229 | runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] { |
| 230 | rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME, |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 231 | [] (RibManager::SlAnnounceResult res) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 232 | NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res); |
| 233 | }); |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime) |
| 239 | { |
| 240 | // renew route with PA or ignore PA (if route has no PA) |
| 241 | runOnRibIoService([name, inFaceId, maxLifetime] { |
| 242 | rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime, |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 243 | [] (RibManager::SlAnnounceResult res) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 244 | NFD_LOG_DEBUG("Renew route with result=" << res); |
| 245 | }); |
| 246 | }); |
| 247 | } |
| 248 | |
| 249 | } // namespace fw |
| 250 | } // namespace nfd |