blob: 399b97026fbbf5b5f5a0fc0a73c308b62aec1f88 [file] [log] [blame]
Teng Liang39465c22018-11-12 19:43:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Teng Liang39465c22018-11-12 19:43:04 -07004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
30#include "common/logger.hpp"
Teng Liang39465c22018-11-12 19:43:04 -070031#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 Pesaventoe422f9e2022-06-03 01:30:23 -040039namespace nfd::fw {
Teng Liang39465c22018-11-12 19:43:04 -070040
41NFD_LOG_INIT(SelfLearningStrategy);
42NFD_REGISTER_STRATEGY(SelfLearningStrategy);
43
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040044constexpr time::milliseconds ROUTE_RENEW_LIFETIME = 10_min;
Teng Liang39465c22018-11-12 19:43:04 -070045
46SelfLearningStrategy::SelfLearningStrategy(Forwarder& forwarder, const Name& name)
47 : Strategy(forwarder)
48{
49 ParsedInstanceName parsed = parseInstanceName(name);
50 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050051 NDN_THROW(std::invalid_argument("SelfLearningStrategy does not accept parameters"));
Teng Liang39465c22018-11-12 19:43:04 -070052 }
53 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050054 NDN_THROW(std::invalid_argument(
Teng Liang39465c22018-11-12 19:43:04 -070055 "SelfLearningStrategy does not support version " + to_string(*parsed.version)));
56 }
57 this->setInstanceName(makeInstanceName(name, getStrategyName()));
58}
59
60const Name&
61SelfLearningStrategy::getStrategyName()
62{
Eric Newberry358414d2021-03-21 20:56:42 -070063 static const auto strategyName = Name("/localhost/nfd/strategy/self-learning").appendVersion(1);
Teng Liang39465c22018-11-12 19:43:04 -070064 return strategyName;
65}
66
67void
Davide Pesavento0498ce82021-06-14 02:02:21 -040068SelfLearningStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Teng Liang39465c22018-11-12 19:43:04 -070069 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 Rahmanc88d2d42019-08-28 20:19:47 +000075 auto inRecordInfo = pitEntry->getInRecord(ingress.face)->insertStrategyInfo<InRecordInfo>().first;
Teng Liang39465c22018-11-12 19:43:04 -070076 if (isNonDiscovery) { // "non-discovery" Interest
77 inRecordInfo->isNonDiscoveryInterest = true;
78 if (nexthops.empty()) { // return NACK if no matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000079 NFD_LOG_DEBUG("NACK non-discovery Interest=" << interest << " from=" << ingress << " noNextHop");
Teng Liang39465c22018-11-12 19:43:04 -070080 lp::NackHeader nackHeader;
81 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040082 this->sendNack(nackHeader, ingress.face, pitEntry);
Teng Liang39465c22018-11-12 19:43:04 -070083 this->rejectPendingInterest(pitEntry);
84 }
85 else { // multicast it if matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000086 multicastInterest(interest, ingress.face, pitEntry, nexthops);
Teng Liang39465c22018-11-12 19:43:04 -070087 }
88 }
89 else { // "discovery" Interest
90 inRecordInfo->isNonDiscoveryInterest = false;
91 if (nexthops.empty()) { // broadcast it if no matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000092 broadcastInterest(interest, ingress.face, pitEntry);
Teng Liang39465c22018-11-12 19:43:04 -070093 }
94 else { // multicast it with "non-discovery" mark if matching FIB entry exists
95 interest.setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
ashiqopuc7079482019-02-20 05:34:37 +000096 multicastInterest(interest, ingress.face, pitEntry, nexthops);
Teng Liang39465c22018-11-12 19:43:04 -070097 }
98 }
99}
100
101void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400102SelfLearningStrategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress,
103 const shared_ptr<pit::Entry>& pitEntry)
Teng Liang39465c22018-11-12 19:43:04 -0700104{
Teng Liang8e531272019-10-12 11:07:53 -0700105 auto outRecord = pitEntry->getOutRecord(ingress.face);
106 if (outRecord == pitEntry->out_end()) {
107 NFD_LOG_DEBUG("Data " << data.getName() << " from=" << ingress << " no out-record");
108 return;
109 }
110
111 OutRecordInfo* outRecordInfo = outRecord->getStrategyInfo<OutRecordInfo>();
Teng Liang39465c22018-11-12 19:43:04 -0700112 if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery
113 if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases)
Davide Pesavento0498ce82021-06-14 02:02:21 -0400114 sendDataToAll(data, pitEntry, ingress.face);
Teng Liang39465c22018-11-12 19:43:04 -0700115 }
116 else { // needs a PA (to respond discovery Interest)
ashiqopuc7079482019-02-20 05:34:37 +0000117 asyncProcessData(pitEntry, ingress.face, data);
Teng Liang39465c22018-11-12 19:43:04 -0700118 }
119 }
120 else { // outgoing Interest was discovery
121 auto paTag = data.getTag<lp::PrefixAnnouncementTag>();
122 if (paTag != nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000123 addRoute(pitEntry, ingress.face, data, *paTag->get().getPrefixAnn());
Teng Liang39465c22018-11-12 19:43:04 -0700124 }
125 else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning
126 }
Davide Pesavento0498ce82021-06-14 02:02:21 -0400127 sendDataToAll(data, pitEntry, ingress.face);
Teng Liang39465c22018-11-12 19:43:04 -0700128 }
129}
130
131void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400132SelfLearningStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Teng Liang39465c22018-11-12 19:43:04 -0700133 const shared_ptr<pit::Entry>& pitEntry)
134{
ashiqopuc7079482019-02-20 05:34:37 +0000135 NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << ingress
136 << " reason=" << nack.getReason());
Teng Liang39465c22018-11-12 19:43:04 -0700137 if (nack.getReason() == lp::NackReason::NO_ROUTE) { // remove FIB entries
138 BOOST_ASSERT(this->lookupFib(*pitEntry).hasNextHops());
139 NFD_LOG_DEBUG("Send NACK to all downstreams");
Davide Pesavento0498ce82021-06-14 02:02:21 -0400140 this->sendNacks(nack.getHeader(), pitEntry);
ashiqopuc7079482019-02-20 05:34:37 +0000141 renewRoute(nack.getInterest().getName(), ingress.face.getId(), 0_ms);
Teng Liang39465c22018-11-12 19:43:04 -0700142 }
143}
144
145void
146SelfLearningStrategy::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 Newberry2377ada2020-09-28 22:40:14 -0700151 wouldViolateScope(inFace, interest, outFace) ||
152 outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
Teng Liang39465c22018-11-12 19:43:04 -0700153 continue;
154 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700155
156 NFD_LOG_DEBUG("send discovery Interest=" << interest << " from=" << inFace.getId() <<
157 " to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400158 auto outRecord = this->sendInterest(interest, outFace, pitEntry);
Eric Newberry2377ada2020-09-28 22:40:14 -0700159 if (outRecord != nullptr) {
160 outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false;
161 }
Teng Liang39465c22018-11-12 19:43:04 -0700162 }
163}
164
165void
166SelfLearningStrategy::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 Newberry2377ada2020-09-28 22:40:14 -0700171 if (!isNextHopEligible(inFace, interest, nexthop, pitEntry)) {
Teng Liang39465c22018-11-12 19:43:04 -0700172 continue;
173 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700174
175 Face& outFace = nexthop.getFace();
176 NFD_LOG_DEBUG("send non-discovery Interest=" << interest << " from=" << inFace.getId() <<
177 " to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400178 auto outRecord = this->sendInterest(interest, outFace, pitEntry);
Eric Newberry2377ada2020-09-28 22:40:14 -0700179 if (outRecord != nullptr) {
180 outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true;
181 }
Teng Liang39465c22018-11-12 19:43:04 -0700182 }
183}
184
185void
186SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data)
187{
188 // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first
189 // to ensure that the entry will not be removed before the whole processing is finished
190 // (the PIT entry's expiry timer was set to 0 before dispatching)
191 this->setExpiryTimer(pitEntry, 1_s);
192
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400193 runOnRibIoService([this, pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data] {
Teng Liang39465c22018-11-12 19:43:04 -0700194 rib::Service::get().getRibManager().slFindAnn(data.getName(),
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400195 [this, pitEntryWeak, inFaceId, data] (std::optional<ndn::PrefixAnnouncement> paOpt) {
Teng Liang39465c22018-11-12 19:43:04 -0700196 if (paOpt) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400197 runOnMainIoService([this, pitEntryWeak, inFaceId, data, pa = std::move(*paOpt)] {
Teng Liang39465c22018-11-12 19:43:04 -0700198 auto pitEntry = pitEntryWeak.lock();
199 auto inFace = this->getFace(inFaceId);
200 if (pitEntry && inFace) {
201 NFD_LOG_DEBUG("found PrefixAnnouncement=" << pa.getAnnouncedName());
202 data.setTag(make_shared<lp::PrefixAnnouncementTag>(lp::PrefixAnnouncementHeader(pa)));
Davide Pesavento0498ce82021-06-14 02:02:21 -0400203 this->sendDataToAll(data, pitEntry, *inFace);
Teng Liang39465c22018-11-12 19:43:04 -0700204 this->setExpiryTimer(pitEntry, 0_ms);
205 }
206 else {
207 NFD_LOG_DEBUG("PIT entry or Face no longer exists");
208 }
209 });
210 }
211 });
212 });
213}
214
215bool
216SelfLearningStrategy::needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry)
217{
218 bool hasDiscoveryInterest = false;
219 bool directToConsumer = true;
220
221 auto now = time::steady_clock::now();
222 for (const auto& inRecord : pitEntry->getInRecords()) {
223 if (inRecord.getExpiry() > now) {
224 InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>();
225 if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) {
226 hasDiscoveryInterest = true;
227 }
228 if (inRecord.getFace().getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
229 directToConsumer = false;
230 }
231 }
232 }
233 return hasDiscoveryInterest && !directToConsumer;
234}
235
236void
237SelfLearningStrategy::addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace,
238 const Data& data, const ndn::PrefixAnnouncement& pa)
239{
240 runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] {
241 rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME,
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500242 [] (RibManager::SlAnnounceResult res) {
Teng Liang39465c22018-11-12 19:43:04 -0700243 NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res);
244 });
245 });
246}
247
248void
249SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime)
250{
251 // renew route with PA or ignore PA (if route has no PA)
252 runOnRibIoService([name, inFaceId, maxLifetime] {
253 rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime,
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500254 [] (RibManager::SlAnnounceResult res) {
Teng Liang39465c22018-11-12 19:43:04 -0700255 NFD_LOG_DEBUG("Renew route with result=" << res);
256 });
257 });
258}
259
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400260} // namespace nfd::fw