Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "common/global.hpp" |
| 30 | #include "common/logger.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 | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 45 | constexpr time::milliseconds 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 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 64 | static const auto strategyName = Name("/localhost/nfd/strategy/self-learning").appendVersion(1); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 65 | return strategyName; |
| 66 | } |
| 67 | |
| 68 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 69 | SelfLearningStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
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; |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 76 | auto inRecordInfo = pitEntry->getInRecord(ingress.face)->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); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 83 | this->sendNack(nackHeader, ingress.face, pitEntry); |
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 |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 103 | SelfLearningStrategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress, |
| 104 | const shared_ptr<pit::Entry>& pitEntry) |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 105 | { |
Teng Liang | 8e53127 | 2019-10-12 11:07:53 -0700 | [diff] [blame] | 106 | auto outRecord = pitEntry->getOutRecord(ingress.face); |
| 107 | if (outRecord == pitEntry->out_end()) { |
| 108 | NFD_LOG_DEBUG("Data " << data.getName() << " from=" << ingress << " no out-record"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | OutRecordInfo* outRecordInfo = outRecord->getStrategyInfo<OutRecordInfo>(); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 113 | if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery |
| 114 | if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases) |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 115 | sendDataToAll(data, pitEntry, ingress.face); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 116 | } |
| 117 | else { // needs a PA (to respond discovery Interest) |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 118 | asyncProcessData(pitEntry, ingress.face, data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | else { // outgoing Interest was discovery |
| 122 | auto paTag = data.getTag<lp::PrefixAnnouncementTag>(); |
| 123 | if (paTag != nullptr) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 124 | addRoute(pitEntry, ingress.face, data, *paTag->get().getPrefixAnn()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 125 | } |
| 126 | else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning |
| 127 | } |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 128 | sendDataToAll(data, pitEntry, ingress.face); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 133 | SelfLearningStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 134 | const shared_ptr<pit::Entry>& pitEntry) |
| 135 | { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 136 | NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << ingress |
| 137 | << " reason=" << nack.getReason()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 138 | if (nack.getReason() == lp::NackReason::NO_ROUTE) { // remove FIB entries |
| 139 | BOOST_ASSERT(this->lookupFib(*pitEntry).hasNextHops()); |
| 140 | NFD_LOG_DEBUG("Send NACK to all downstreams"); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 141 | this->sendNacks(nack.getHeader(), pitEntry); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 142 | renewRoute(nack.getInterest().getName(), ingress.face.getId(), 0_ms); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | SelfLearningStrategy::broadcastInterest(const Interest& interest, const Face& inFace, |
| 148 | const shared_ptr<pit::Entry>& pitEntry) |
| 149 | { |
| 150 | for (auto& outFace : this->getFaceTable() | boost::adaptors::reversed) { |
| 151 | if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) || |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 152 | wouldViolateScope(inFace, interest, outFace) || |
| 153 | outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 154 | continue; |
| 155 | } |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 156 | |
| 157 | NFD_LOG_DEBUG("send discovery Interest=" << interest << " from=" << inFace.getId() << |
| 158 | " to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 159 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 160 | if (outRecord != nullptr) { |
| 161 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false; |
| 162 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | SelfLearningStrategy::multicastInterest(const Interest& interest, const Face& inFace, |
| 168 | const shared_ptr<pit::Entry>& pitEntry, |
| 169 | const fib::NextHopList& nexthops) |
| 170 | { |
| 171 | for (const auto& nexthop : nexthops) { |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 172 | if (!isNextHopEligible(inFace, interest, nexthop, pitEntry)) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 173 | continue; |
| 174 | } |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 175 | |
| 176 | Face& outFace = nexthop.getFace(); |
| 177 | NFD_LOG_DEBUG("send non-discovery Interest=" << interest << " from=" << inFace.getId() << |
| 178 | " to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 179 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 180 | if (outRecord != nullptr) { |
| 181 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true; |
| 182 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data) |
| 188 | { |
| 189 | // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first |
| 190 | // to ensure that the entry will not be removed before the whole processing is finished |
| 191 | // (the PIT entry's expiry timer was set to 0 before dispatching) |
| 192 | this->setExpiryTimer(pitEntry, 1_s); |
| 193 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 194 | runOnRibIoService([this, pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data] { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 195 | rib::Service::get().getRibManager().slFindAnn(data.getName(), |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 196 | [this, pitEntryWeak, inFaceId, data] (std::optional<ndn::PrefixAnnouncement> paOpt) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 197 | if (paOpt) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 198 | runOnMainIoService([this, pitEntryWeak, inFaceId, data, pa = std::move(*paOpt)] { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 199 | auto pitEntry = pitEntryWeak.lock(); |
| 200 | auto inFace = this->getFace(inFaceId); |
| 201 | if (pitEntry && inFace) { |
| 202 | NFD_LOG_DEBUG("found PrefixAnnouncement=" << pa.getAnnouncedName()); |
| 203 | data.setTag(make_shared<lp::PrefixAnnouncementTag>(lp::PrefixAnnouncementHeader(pa))); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 204 | this->sendDataToAll(data, pitEntry, *inFace); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 205 | this->setExpiryTimer(pitEntry, 0_ms); |
| 206 | } |
| 207 | else { |
| 208 | NFD_LOG_DEBUG("PIT entry or Face no longer exists"); |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | }); |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | bool |
| 217 | SelfLearningStrategy::needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry) |
| 218 | { |
| 219 | bool hasDiscoveryInterest = false; |
| 220 | bool directToConsumer = true; |
| 221 | |
| 222 | auto now = time::steady_clock::now(); |
| 223 | for (const auto& inRecord : pitEntry->getInRecords()) { |
| 224 | if (inRecord.getExpiry() > now) { |
| 225 | InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>(); |
| 226 | if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) { |
| 227 | hasDiscoveryInterest = true; |
| 228 | } |
| 229 | if (inRecord.getFace().getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 230 | directToConsumer = false; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return hasDiscoveryInterest && !directToConsumer; |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | SelfLearningStrategy::addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, |
| 239 | const Data& data, const ndn::PrefixAnnouncement& pa) |
| 240 | { |
| 241 | runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] { |
| 242 | rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME, |
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("Add route via PrefixAnnouncement with result=" << res); |
| 245 | }); |
| 246 | }); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime) |
| 251 | { |
| 252 | // renew route with PA or ignore PA (if route has no PA) |
| 253 | runOnRibIoService([name, inFaceId, maxLifetime] { |
| 254 | rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime, |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 255 | [] (RibManager::SlAnnounceResult res) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 256 | NFD_LOG_DEBUG("Renew route with result=" << res); |
| 257 | }); |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | } // namespace fw |
| 262 | } // namespace nfd |