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 | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 37 | #include <boost/asio/post.hpp> |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 38 | #include <boost/range/adaptor/reversed.hpp> |
| 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | namespace nfd::fw { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 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 | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 55 | NDN_THROW(std::invalid_argument("SelfLearningStrategy does not support version " + |
| 56 | std::to_string(*parsed.version))); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 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; |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame] | 76 | auto inRecordInfo = pitEntry->findInRecord(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 |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 80 | NFD_LOG_INTEREST_FROM(interest, ingress, "non-discovery no-nexthop"); |
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 | { |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame] | 106 | auto outRecord = pitEntry->findOutRecord(ingress.face); |
Teng Liang | 8e53127 | 2019-10-12 11:07:53 -0700 | [diff] [blame] | 107 | if (outRecord == pitEntry->out_end()) { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 108 | NFD_LOG_DATA_FROM(data, ingress, "no-out-record"); |
Teng Liang | 8e53127 | 2019-10-12 11:07:53 -0700 | [diff] [blame] | 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 | { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 136 | NFD_LOG_NACK_FROM(nack, ingress, ""); |
| 137 | |
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()); |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 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 | |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 157 | NFD_LOG_INTEREST_FROM(interest, inFace.getId(), "send discovery to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 158 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 159 | if (outRecord != nullptr) { |
| 160 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false; |
| 161 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | SelfLearningStrategy::multicastInterest(const Interest& interest, const Face& inFace, |
| 167 | const shared_ptr<pit::Entry>& pitEntry, |
| 168 | const fib::NextHopList& nexthops) |
| 169 | { |
| 170 | for (const auto& nexthop : nexthops) { |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 171 | if (!isNextHopEligible(inFace, interest, nexthop, pitEntry)) { |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 172 | continue; |
| 173 | } |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 174 | |
| 175 | Face& outFace = nexthop.getFace(); |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 176 | 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] | 177 | auto outRecord = this->sendInterest(interest, outFace, pitEntry); |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame] | 178 | if (outRecord != nullptr) { |
| 179 | outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true; |
| 180 | } |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data) |
| 186 | { |
| 187 | // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first |
| 188 | // to ensure that the entry will not be removed before the whole processing is finished |
| 189 | // (the PIT entry's expiry timer was set to 0 before dispatching) |
| 190 | this->setExpiryTimer(pitEntry, 1_s); |
| 191 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 192 | boost::asio::post(getRibIoService(), |
| 193 | [this, pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data] { |
| 194 | rib::Service::get().getRibManager().slFindAnn(data.getName(), |
| 195 | [this, pitEntryWeak, inFaceId, data] (std::optional<ndn::PrefixAnnouncement> paOpt) { |
| 196 | if (paOpt) { |
| 197 | boost::asio::post(getMainIoService(), |
| 198 | [this, pitEntryWeak, inFaceId, data, pa = std::move(*paOpt)] { |
| 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))); |
| 204 | this->sendDataToAll(data, pitEntry, *inFace); |
| 205 | this->setExpiryTimer(pitEntry, 0_ms); |
| 206 | } |
| 207 | else { |
| 208 | NFD_LOG_DEBUG("PIT entry or face no longer exists"); |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | }); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 213 | }); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 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 | { |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 241 | boost::asio::post(getRibIoService(), |
| 242 | [pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] { |
| 243 | rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME, |
| 244 | [] (RibManager::SlAnnounceResult res) { |
| 245 | NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res); |
| 246 | }); |
| 247 | }); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void |
| 251 | SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime) |
| 252 | { |
| 253 | // renew route with PA or ignore PA (if route has no PA) |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 254 | boost::asio::post(getRibIoService(), |
| 255 | [name, inFaceId, maxLifetime] { |
| 256 | rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime, |
| 257 | [] (RibManager::SlAnnounceResult res) { |
| 258 | NFD_LOG_DEBUG("Renew route with result=" << res); |
| 259 | }); |
| 260 | }); |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 263 | } // namespace nfd::fw |