blob: acf1f22b1afe3878e4fe49405c29fd7493af0421 [file] [log] [blame]
Teng Liang39465c22018-11-12 19:43:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Alex Lane653eb072023-07-27 22:11:46 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040037#include <boost/asio/post.hpp>
Teng Liang39465c22018-11-12 19:43:04 -070038#include <boost/range/adaptor/reversed.hpp>
39
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040namespace nfd::fw {
Teng Liang39465c22018-11-12 19:43:04 -070041
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
Alex Lane653eb072023-07-27 22:11:46 -040080 NFD_LOG_INTEREST_FROM(interest, ingress, "non-discovery no-nexthop");
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()) {
Alex Lane653eb072023-07-27 22:11:46 -0400108 NFD_LOG_DATA_FROM(data, ingress, "no-out-record");
Teng Liang8e531272019-10-12 11:07:53 -0700109 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{
Alex Lane653eb072023-07-27 22:11:46 -0400136 NFD_LOG_NACK_FROM(nack, ingress, "");
137
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());
Alex Lane653eb072023-07-27 22:11:46 -0400140 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
Alex Lane653eb072023-07-27 22:11:46 -0400157 NFD_LOG_INTEREST_FROM(interest, inFace.getId(), "send discovery 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();
Alex Lane653eb072023-07-27 22:11:46 -0400176 NFD_LOG_INTEREST_FROM(interest, inFace.getId(), "send non-discovery to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400177 auto outRecord = this->sendInterest(interest, outFace, pitEntry);
Eric Newberry2377ada2020-09-28 22:40:14 -0700178 if (outRecord != nullptr) {
179 outRecord->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true;
180 }
Teng Liang39465c22018-11-12 19:43:04 -0700181 }
182}
183
184void
185SelfLearningStrategy::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 Pesaventoa9e1ab22023-10-02 22:10:45 -0400192 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 Liang39465c22018-11-12 19:43:04 -0700213 });
Teng Liang39465c22018-11-12 19:43:04 -0700214}
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{
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400241 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 Liang39465c22018-11-12 19:43:04 -0700248}
249
250void
251SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime)
252{
253 // renew route with PA or ignore PA (if route has no PA)
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400254 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 Liang39465c22018-11-12 19:43:04 -0700261}
262
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400263} // namespace nfd::fw