Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 39 | namespace nfd::fw { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 40 | |
| 41 | NFD_LOG_INIT(SelfLearningStrategy); |
| 42 | NFD_REGISTER_STRATEGY(SelfLearningStrategy); |
| 43 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 44 | constexpr time::milliseconds ROUTE_RENEW_LIFETIME = 10_min; |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 45 | |
| 46 | SelfLearningStrategy::SelfLearningStrategy(Forwarder& forwarder, const Name& name) |
| 47 | : Strategy(forwarder) |
| 48 | { |
| 49 | ParsedInstanceName parsed = parseInstanceName(name); |
| 50 | if (!parsed.parameters.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 51 | NDN_THROW(std::invalid_argument("SelfLearningStrategy does not accept parameters")); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 52 | } |
| 53 | if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 54 | NDN_THROW(std::invalid_argument( |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 55 | "SelfLearningStrategy does not support version " + to_string(*parsed.version))); |
| 56 | } |
| 57 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
| 58 | } |
| 59 | |
| 60 | const Name& |
| 61 | SelfLearningStrategy::getStrategyName() |
| 62 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 63 | static const auto strategyName = Name("/localhost/nfd/strategy/self-learning").appendVersion(1); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 64 | return strategyName; |
| 65 | } |
| 66 | |
| 67 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 68 | SelfLearningStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 69 | const shared_ptr<pit::Entry>& pitEntry) |
| 70 | { |
| 71 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 72 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
| 73 | |
| 74 | bool isNonDiscovery = interest.getTag<lp::NonDiscoveryTag>() != nullptr; |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 75 | auto inRecordInfo = pitEntry->getInRecord(ingress.face)->insertStrategyInfo<InRecordInfo>().first; |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 76 | if (isNonDiscovery) { // "non-discovery" Interest |
| 77 | inRecordInfo->isNonDiscoveryInterest = true; |
| 78 | if (nexthops.empty()) { // return NACK if no matching FIB entry exists |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 79 | NFD_LOG_INTEREST_FROM(interest, ingress, "non-discovery no-nexthop"); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 80 | lp::NackHeader nackHeader; |
| 81 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 82 | this->sendNack(nackHeader, ingress.face, pitEntry); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 83 | this->rejectPendingInterest(pitEntry); |
| 84 | } |
| 85 | else { // multicast it if matching FIB entry exists |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 86 | multicastInterest(interest, ingress.face, pitEntry, nexthops); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | else { // "discovery" Interest |
| 90 | inRecordInfo->isNonDiscoveryInterest = false; |
| 91 | if (nexthops.empty()) { // broadcast it if no matching FIB entry exists |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 92 | broadcastInterest(interest, ingress.face, pitEntry); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 93 | } |
| 94 | else { // multicast it with "non-discovery" mark if matching FIB entry exists |
| 95 | interest.setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{})); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 96 | multicastInterest(interest, ingress.face, pitEntry, nexthops); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 102 | SelfLearningStrategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress, |
| 103 | const shared_ptr<pit::Entry>& pitEntry) |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 104 | { |
Teng Liang | 8e53127 | 2019-10-12 11:07:53 -0700 | [diff] [blame] | 105 | auto outRecord = pitEntry->getOutRecord(ingress.face); |
| 106 | if (outRecord == pitEntry->out_end()) { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 107 | NFD_LOG_DATA_FROM(data, ingress, "no-out-record"); |
Teng Liang | 8e53127 | 2019-10-12 11:07:53 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | OutRecordInfo* outRecordInfo = outRecord->getStrategyInfo<OutRecordInfo>(); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 112 | if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery |
| 113 | if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases) |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 114 | sendDataToAll(data, pitEntry, ingress.face); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 115 | } |
| 116 | else { // needs a PA (to respond discovery Interest) |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 117 | asyncProcessData(pitEntry, ingress.face, data); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | else { // outgoing Interest was discovery |
| 121 | auto paTag = data.getTag<lp::PrefixAnnouncementTag>(); |
| 122 | if (paTag != nullptr) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 123 | addRoute(pitEntry, ingress.face, data, *paTag->get().getPrefixAnn()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 124 | } |
| 125 | else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning |
| 126 | } |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 127 | sendDataToAll(data, pitEntry, ingress.face); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 132 | SelfLearningStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 133 | const shared_ptr<pit::Entry>& pitEntry) |
| 134 | { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 135 | NFD_LOG_NACK_FROM(nack, ingress, ""); |
| 136 | |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 137 | if (nack.getReason() == lp::NackReason::NO_ROUTE) { // remove FIB entries |
| 138 | BOOST_ASSERT(this->lookupFib(*pitEntry).hasNextHops()); |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 139 | NFD_LOG_DEBUG("Send Nack to all downstreams"); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 140 | this->sendNacks(nack.getHeader(), pitEntry); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 141 | renewRoute(nack.getInterest().getName(), ingress.face.getId(), 0_ms); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | SelfLearningStrategy::broadcastInterest(const Interest& interest, const Face& inFace, |
| 147 | const shared_ptr<pit::Entry>& pitEntry) |
| 148 | { |
| 149 | for (auto& outFace : this->getFaceTable() | boost::adaptors::reversed) { |
| 150 | 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] | 151 | wouldViolateScope(inFace, interest, outFace) || |
| 152 | outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 153 | continue; |
| 154 | } |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 155 | |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 156 | NFD_LOG_INTEREST_FROM(interest, inFace.getId(), "send discovery to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 157 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 158 | if (outRecord != nullptr) { |
| 159 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false; |
| 160 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | SelfLearningStrategy::multicastInterest(const Interest& interest, const Face& inFace, |
| 166 | const shared_ptr<pit::Entry>& pitEntry, |
| 167 | const fib::NextHopList& nexthops) |
| 168 | { |
| 169 | for (const auto& nexthop : nexthops) { |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 170 | if (!isNextHopEligible(inFace, interest, nexthop, pitEntry)) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 173 | |
| 174 | Face& outFace = nexthop.getFace(); |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 175 | NFD_LOG_INTEREST_FROM(interest, inFace.getId(), "send non-discovery to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 176 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 177 | if (outRecord != nullptr) { |
| 178 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true; |
| 179 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data) |
| 185 | { |
| 186 | // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first |
| 187 | // to ensure that the entry will not be removed before the whole processing is finished |
| 188 | // (the PIT entry's expiry timer was set to 0 before dispatching) |
| 189 | this->setExpiryTimer(pitEntry, 1_s); |
| 190 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 191 | runOnRibIoService([this, pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data] { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 192 | rib::Service::get().getRibManager().slFindAnn(data.getName(), |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 193 | [this, pitEntryWeak, inFaceId, data] (std::optional<ndn::PrefixAnnouncement> paOpt) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 194 | if (paOpt) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 195 | runOnMainIoService([this, pitEntryWeak, inFaceId, data, pa = std::move(*paOpt)] { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 196 | auto pitEntry = pitEntryWeak.lock(); |
| 197 | auto inFace = this->getFace(inFaceId); |
| 198 | if (pitEntry && inFace) { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 199 | NFD_LOG_DEBUG("Found PrefixAnnouncement=" << pa.getAnnouncedName()); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 200 | data.setTag(make_shared<lp::PrefixAnnouncementTag>(lp::PrefixAnnouncementHeader(pa))); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 201 | this->sendDataToAll(data, pitEntry, *inFace); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 202 | this->setExpiryTimer(pitEntry, 0_ms); |
| 203 | } |
| 204 | else { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame^] | 205 | NFD_LOG_DEBUG("PIT entry or face no longer exists"); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 206 | } |
| 207 | }); |
| 208 | } |
| 209 | }); |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | bool |
| 214 | SelfLearningStrategy::needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry) |
| 215 | { |
| 216 | bool hasDiscoveryInterest = false; |
| 217 | bool directToConsumer = true; |
| 218 | |
| 219 | auto now = time::steady_clock::now(); |
| 220 | for (const auto& inRecord : pitEntry->getInRecords()) { |
| 221 | if (inRecord.getExpiry() > now) { |
| 222 | InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>(); |
| 223 | if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) { |
| 224 | hasDiscoveryInterest = true; |
| 225 | } |
| 226 | if (inRecord.getFace().getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 227 | directToConsumer = false; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | return hasDiscoveryInterest && !directToConsumer; |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | SelfLearningStrategy::addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, |
| 236 | const Data& data, const ndn::PrefixAnnouncement& pa) |
| 237 | { |
| 238 | runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] { |
| 239 | rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME, |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 240 | [] (RibManager::SlAnnounceResult res) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 241 | NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res); |
| 242 | }); |
| 243 | }); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime) |
| 248 | { |
| 249 | // renew route with PA or ignore PA (if route has no PA) |
| 250 | runOnRibIoService([name, inFaceId, maxLifetime] { |
| 251 | rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime, |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 252 | [] (RibManager::SlAnnounceResult res) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 253 | NFD_LOG_DEBUG("Renew route with result=" << res); |
| 254 | }); |
| 255 | }); |
| 256 | } |
| 257 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 258 | } // namespace nfd::fw |