blob: db5805045cb4176a2f75eb9ac8a5a481c0b086b7 [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
39namespace nfd {
40namespace fw {
41
42NFD_LOG_INIT(SelfLearningStrategy);
43NFD_REGISTER_STRATEGY(SelfLearningStrategy);
44
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045constexpr time::milliseconds ROUTE_RENEW_LIFETIME = 10_min;
Teng Liang39465c22018-11-12 19:43:04 -070046
47SelfLearningStrategy::SelfLearningStrategy(Forwarder& forwarder, const Name& name)
48 : Strategy(forwarder)
49{
50 ParsedInstanceName parsed = parseInstanceName(name);
51 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050052 NDN_THROW(std::invalid_argument("SelfLearningStrategy does not accept parameters"));
Teng Liang39465c22018-11-12 19:43:04 -070053 }
54 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050055 NDN_THROW(std::invalid_argument(
Teng Liang39465c22018-11-12 19:43:04 -070056 "SelfLearningStrategy does not support version " + to_string(*parsed.version)));
57 }
58 this->setInstanceName(makeInstanceName(name, getStrategyName()));
59}
60
61const Name&
62SelfLearningStrategy::getStrategyName()
63{
Eric Newberry358414d2021-03-21 20:56:42 -070064 static const auto strategyName = Name("/localhost/nfd/strategy/self-learning").appendVersion(1);
Teng Liang39465c22018-11-12 19:43:04 -070065 return strategyName;
66}
67
68void
Davide Pesavento0498ce82021-06-14 02:02:21 -040069SelfLearningStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Teng Liang39465c22018-11-12 19:43:04 -070070 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 Rahmanc88d2d42019-08-28 20:19:47 +000076 auto inRecordInfo = pitEntry->getInRecord(ingress.face)->insertStrategyInfo<InRecordInfo>().first;
Teng Liang39465c22018-11-12 19:43:04 -070077 if (isNonDiscovery) { // "non-discovery" Interest
78 inRecordInfo->isNonDiscoveryInterest = true;
79 if (nexthops.empty()) { // return NACK if no matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000080 NFD_LOG_DEBUG("NACK non-discovery Interest=" << interest << " from=" << ingress << " noNextHop");
Teng Liang39465c22018-11-12 19:43:04 -070081 lp::NackHeader nackHeader;
82 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040083 this->sendNack(nackHeader, ingress.face, pitEntry);
Teng Liang39465c22018-11-12 19:43:04 -070084 this->rejectPendingInterest(pitEntry);
85 }
86 else { // multicast it if matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000087 multicastInterest(interest, ingress.face, pitEntry, nexthops);
Teng Liang39465c22018-11-12 19:43:04 -070088 }
89 }
90 else { // "discovery" Interest
91 inRecordInfo->isNonDiscoveryInterest = false;
92 if (nexthops.empty()) { // broadcast it if no matching FIB entry exists
ashiqopuc7079482019-02-20 05:34:37 +000093 broadcastInterest(interest, ingress.face, pitEntry);
Teng Liang39465c22018-11-12 19:43:04 -070094 }
95 else { // multicast it with "non-discovery" mark if matching FIB entry exists
96 interest.setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
ashiqopuc7079482019-02-20 05:34:37 +000097 multicastInterest(interest, ingress.face, pitEntry, nexthops);
Teng Liang39465c22018-11-12 19:43:04 -070098 }
99 }
100}
101
102void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400103SelfLearningStrategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress,
104 const shared_ptr<pit::Entry>& pitEntry)
Teng Liang39465c22018-11-12 19:43:04 -0700105{
Teng Liang8e531272019-10-12 11:07:53 -0700106 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 Liang39465c22018-11-12 19:43:04 -0700113 if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery
114 if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases)
Davide Pesavento0498ce82021-06-14 02:02:21 -0400115 sendDataToAll(data, pitEntry, ingress.face);
Teng Liang39465c22018-11-12 19:43:04 -0700116 }
117 else { // needs a PA (to respond discovery Interest)
ashiqopuc7079482019-02-20 05:34:37 +0000118 asyncProcessData(pitEntry, ingress.face, data);
Teng Liang39465c22018-11-12 19:43:04 -0700119 }
120 }
121 else { // outgoing Interest was discovery
122 auto paTag = data.getTag<lp::PrefixAnnouncementTag>();
123 if (paTag != nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000124 addRoute(pitEntry, ingress.face, data, *paTag->get().getPrefixAnn());
Teng Liang39465c22018-11-12 19:43:04 -0700125 }
126 else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning
127 }
Davide Pesavento0498ce82021-06-14 02:02:21 -0400128 sendDataToAll(data, pitEntry, ingress.face);
Teng Liang39465c22018-11-12 19:43:04 -0700129 }
130}
131
132void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400133SelfLearningStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Teng Liang39465c22018-11-12 19:43:04 -0700134 const shared_ptr<pit::Entry>& pitEntry)
135{
ashiqopuc7079482019-02-20 05:34:37 +0000136 NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << ingress
137 << " reason=" << nack.getReason());
Teng Liang39465c22018-11-12 19:43:04 -0700138 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 Pesavento0498ce82021-06-14 02:02:21 -0400141 this->sendNacks(nack.getHeader(), pitEntry);
ashiqopuc7079482019-02-20 05:34:37 +0000142 renewRoute(nack.getInterest().getName(), ingress.face.getId(), 0_ms);
Teng Liang39465c22018-11-12 19:43:04 -0700143 }
144}
145
146void
147SelfLearningStrategy::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 Newberry2377ada2020-09-28 22:40:14 -0700152 wouldViolateScope(inFace, interest, outFace) ||
153 outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
Teng Liang39465c22018-11-12 19:43:04 -0700154 continue;
155 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700156
157 NFD_LOG_DEBUG("send discovery Interest=" << interest << " from=" << inFace.getId() <<
158 " to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400159 auto outRecord = this->sendInterest(interest, outFace, pitEntry);
Eric Newberry2377ada2020-09-28 22:40:14 -0700160 if (outRecord != nullptr) {
161 outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false;
162 }
Teng Liang39465c22018-11-12 19:43:04 -0700163 }
164}
165
166void
167SelfLearningStrategy::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 Newberry2377ada2020-09-28 22:40:14 -0700172 if (!isNextHopEligible(inFace, interest, nexthop, pitEntry)) {
Teng Liang39465c22018-11-12 19:43:04 -0700173 continue;
174 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700175
176 Face& outFace = nexthop.getFace();
177 NFD_LOG_DEBUG("send non-discovery Interest=" << interest << " from=" << inFace.getId() <<
178 " to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400179 auto outRecord = this->sendInterest(interest, outFace, pitEntry);
Eric Newberry2377ada2020-09-28 22:40:14 -0700180 if (outRecord != nullptr) {
181 outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true;
182 }
Teng Liang39465c22018-11-12 19:43:04 -0700183 }
184}
185
186void
187SelfLearningStrategy::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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400194 runOnRibIoService([this, pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data] {
Teng Liang39465c22018-11-12 19:43:04 -0700195 rib::Service::get().getRibManager().slFindAnn(data.getName(),
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400196 [this, pitEntryWeak, inFaceId, data] (std::optional<ndn::PrefixAnnouncement> paOpt) {
Teng Liang39465c22018-11-12 19:43:04 -0700197 if (paOpt) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400198 runOnMainIoService([this, pitEntryWeak, inFaceId, data, pa = std::move(*paOpt)] {
Teng Liang39465c22018-11-12 19:43:04 -0700199 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 Pesavento0498ce82021-06-14 02:02:21 -0400204 this->sendDataToAll(data, pitEntry, *inFace);
Teng Liang39465c22018-11-12 19:43:04 -0700205 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
216bool
217SelfLearningStrategy::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
237void
238SelfLearningStrategy::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 Pesavento8a05c7f2019-02-28 02:26:19 -0500243 [] (RibManager::SlAnnounceResult res) {
Teng Liang39465c22018-11-12 19:43:04 -0700244 NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res);
245 });
246 });
247}
248
249void
250SelfLearningStrategy::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 Pesavento8a05c7f2019-02-28 02:26:19 -0500255 [] (RibManager::SlAnnounceResult res) {
Teng Liang39465c22018-11-12 19:43:04 -0700256 NFD_LOG_DEBUG("Renew route with result=" << res);
257 });
258 });
259}
260
261} // namespace fw
262} // namespace nfd