fw: implement self-learning forwarding strategy

refs #4279

Change-Id: I3c80820f9e3382d204a75ea3248234581b4d97d2
diff --git a/daemon/face/generic-link-service.hpp b/daemon/face/generic-link-service.hpp
index c8eea37..cd124c0 100644
--- a/daemon/face/generic-link-service.hpp
+++ b/daemon/face/generic-link-service.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -146,7 +146,7 @@
 
     /** \brief enables self-learning forwarding support
      */
-    bool allowSelfLearning = false;
+    bool allowSelfLearning = true;
   };
 
   /** \brief counters provided by GenericLinkService
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index d55da5a..b53b82d 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -515,6 +515,7 @@
 void
 Forwarder::setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
 {
+  BOOST_ASSERT(pitEntry);
   BOOST_ASSERT(duration >= 0_ms);
 
   scheduler::cancel(pitEntry->expiryTimer);
diff --git a/daemon/fw/self-learning-strategy.cpp b/daemon/fw/self-learning-strategy.cpp
new file mode 100644
index 0000000..5a43a26
--- /dev/null
+++ b/daemon/fw/self-learning-strategy.cpp
@@ -0,0 +1,249 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "self-learning-strategy.hpp"
+#include "algorithm.hpp"
+
+#include "core/logger.hpp"
+#include "core/global-io.hpp"
+#include "rib/service.hpp"
+
+#include <ndn-cxx/lp/empty-value.hpp>
+#include <ndn-cxx/lp/prefix-announcement-header.hpp>
+#include <ndn-cxx/lp/tags.hpp>
+
+#include <boost/range/adaptor/reversed.hpp>
+
+namespace nfd {
+namespace fw {
+
+NFD_LOG_INIT(SelfLearningStrategy);
+NFD_REGISTER_STRATEGY(SelfLearningStrategy);
+
+const time::milliseconds SelfLearningStrategy::ROUTE_RENEW_LIFETIME(600_s);
+
+SelfLearningStrategy::SelfLearningStrategy(Forwarder& forwarder, const Name& name)
+  : Strategy(forwarder)
+{
+  ParsedInstanceName parsed = parseInstanceName(name);
+  if (!parsed.parameters.empty()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("SelfLearningStrategy does not accept parameters"));
+  }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "SelfLearningStrategy does not support version " + to_string(*parsed.version)));
+  }
+  this->setInstanceName(makeInstanceName(name, getStrategyName()));
+}
+
+const Name&
+SelfLearningStrategy::getStrategyName()
+{
+  static Name strategyName("/localhost/nfd/strategy/self-learning/%FD%01");
+  return strategyName;
+}
+
+void
+SelfLearningStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
+                                           const shared_ptr<pit::Entry>& pitEntry)
+{
+  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
+  const fib::NextHopList& nexthops = fibEntry.getNextHops();
+
+  bool isNonDiscovery = interest.getTag<lp::NonDiscoveryTag>() != nullptr;
+  auto inRecordInfo = pitEntry->getInRecord(inFace)->insertStrategyInfo<InRecordInfo>().first;
+  if (isNonDiscovery) { // "non-discovery" Interest
+    inRecordInfo->isNonDiscoveryInterest = true;
+    if (nexthops.empty()) { // return NACK if no matching FIB entry exists
+      NFD_LOG_DEBUG("NACK non-discovery Interest=" << interest << " from=" << inFace.getId() << " noNextHop");
+      lp::NackHeader nackHeader;
+      nackHeader.setReason(lp::NackReason::NO_ROUTE);
+      this->sendNack(pitEntry, inFace, nackHeader);
+      this->rejectPendingInterest(pitEntry);
+    }
+    else { // multicast it if matching FIB entry exists
+      multicastInterest(interest, inFace, pitEntry, nexthops);
+    }
+  }
+  else { // "discovery" Interest
+    inRecordInfo->isNonDiscoveryInterest = false;
+    if (nexthops.empty()) { // broadcast it if no matching FIB entry exists
+      broadcastInterest(interest, inFace, pitEntry);
+    }
+    else { // multicast it with "non-discovery" mark if matching FIB entry exists
+      interest.setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
+      multicastInterest(interest, inFace, pitEntry, nexthops);
+    }
+  }
+}
+
+void
+SelfLearningStrategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
+                                       const Face& inFace, const Data& data)
+{
+  OutRecordInfo* outRecordInfo = pitEntry->getOutRecord(inFace)->getStrategyInfo<OutRecordInfo>();
+  if (outRecordInfo && outRecordInfo->isNonDiscoveryInterest) { // outgoing Interest was non-discovery
+    if (!needPrefixAnn(pitEntry)) { // no need to attach a PA (common cases)
+      sendDataToAll(pitEntry, inFace, data);
+    }
+    else { // needs a PA (to respond discovery Interest)
+      asyncProcessData(pitEntry, inFace, data);
+    }
+  }
+  else { // outgoing Interest was discovery
+    auto paTag = data.getTag<lp::PrefixAnnouncementTag>();
+    if (paTag != nullptr) {
+      addRoute(pitEntry, inFace, data, *paTag->get().getPrefixAnn());
+    }
+    else { // Data contains no PrefixAnnouncement, upstreams do not support self-learning
+    }
+    sendDataToAll(pitEntry, inFace, data);
+  }
+}
+
+void
+SelfLearningStrategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
+                                       const shared_ptr<pit::Entry>& pitEntry)
+{
+  NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << inFace.getId() << ": " << nack.getReason());
+  if (nack.getReason() == lp::NackReason::NO_ROUTE) { // remove FIB entries
+    BOOST_ASSERT(this->lookupFib(*pitEntry).hasNextHops());
+    NFD_LOG_DEBUG("Send NACK to all downstreams");
+    this->sendNacks(pitEntry, nack.getHeader());
+    renewRoute(nack.getInterest().getName(), inFace.getId(), 0_ms);
+  }
+}
+
+void
+SelfLearningStrategy::broadcastInterest(const Interest& interest, const Face& inFace,
+                                        const shared_ptr<pit::Entry>& pitEntry)
+{
+  for (auto& outFace : this->getFaceTable() | boost::adaptors::reversed) {
+    if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
+        wouldViolateScope(inFace, interest, outFace) || outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
+      continue;
+    }
+    this->sendInterest(pitEntry, outFace, interest);
+    pitEntry->getOutRecord(outFace)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = false;
+    NFD_LOG_DEBUG("send discovery Interest=" << interest << " from="
+                  << inFace.getId() << " to=" << outFace.getId());
+  }
+}
+
+void
+SelfLearningStrategy::multicastInterest(const Interest& interest, const Face& inFace,
+                                        const shared_ptr<pit::Entry>& pitEntry,
+                                        const fib::NextHopList& nexthops)
+{
+  for (const auto& nexthop : nexthops) {
+    Face& outFace = nexthop.getFace();
+    if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
+        wouldViolateScope(inFace, interest, outFace)) {
+      continue;
+    }
+    this->sendInterest(pitEntry, outFace, interest);
+    pitEntry->getOutRecord(outFace)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest = true;
+    NFD_LOG_DEBUG("send non-discovery Interest=" << interest << " from="
+                  << inFace.getId() << " to=" << outFace.getId());
+  }
+}
+
+void
+SelfLearningStrategy::asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data)
+{
+  // Given that this processing is asynchronous, the PIT entry's expiry timer is extended first
+  // to ensure that the entry will not be removed before the whole processing is finished
+  // (the PIT entry's expiry timer was set to 0 before dispatching)
+  this->setExpiryTimer(pitEntry, 1_s);
+
+  runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, this] {
+    rib::Service::get().getRibManager().slFindAnn(data.getName(),
+      [pitEntryWeak, inFaceId, data, this] (optional<ndn::PrefixAnnouncement> paOpt) {
+        if (paOpt) {
+          runOnMainIoService([pitEntryWeak, inFaceId, data, pa = std::move(*paOpt), this] {
+            auto pitEntry = pitEntryWeak.lock();
+            auto inFace = this->getFace(inFaceId);
+            if (pitEntry && inFace) {
+              NFD_LOG_DEBUG("found PrefixAnnouncement=" << pa.getAnnouncedName());
+              data.setTag(make_shared<lp::PrefixAnnouncementTag>(lp::PrefixAnnouncementHeader(pa)));
+              this->sendDataToAll(pitEntry, *inFace, data);
+              this->setExpiryTimer(pitEntry, 0_ms);
+            }
+            else {
+              NFD_LOG_DEBUG("PIT entry or Face no longer exists");
+            }
+          });
+        }
+    });
+  });
+}
+
+bool
+SelfLearningStrategy::needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry)
+{
+  bool hasDiscoveryInterest = false;
+  bool directToConsumer = true;
+
+  auto now = time::steady_clock::now();
+  for (const auto& inRecord : pitEntry->getInRecords()) {
+    if (inRecord.getExpiry() > now) {
+      InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>();
+      if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) {
+        hasDiscoveryInterest = true;
+      }
+      if (inRecord.getFace().getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
+        directToConsumer = false;
+      }
+    }
+  }
+  return hasDiscoveryInterest && !directToConsumer;
+}
+
+void
+SelfLearningStrategy::addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace,
+                               const Data& data, const ndn::PrefixAnnouncement& pa)
+{
+  runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] {
+    rib::Service::get().getRibManager().slAnnounce(pa, inFaceId, ROUTE_RENEW_LIFETIME,
+      [] (rib::RibManager::SlAnnounceResult res) {
+        NFD_LOG_DEBUG("Add route via PrefixAnnouncement with result=" << res);
+      });
+  });
+}
+
+void
+SelfLearningStrategy::renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime)
+{
+  // renew route with PA or ignore PA (if route has no PA)
+  runOnRibIoService([name, inFaceId, maxLifetime] {
+    rib::Service::get().getRibManager().slRenew(name, inFaceId, maxLifetime,
+      [] (rib::RibManager::SlAnnounceResult res) {
+        NFD_LOG_DEBUG("Renew route with result=" << res);
+      });
+  });
+}
+
+} // namespace fw
+} // namespace nfd
diff --git a/daemon/fw/self-learning-strategy.hpp b/daemon/fw/self-learning-strategy.hpp
new file mode 100644
index 0000000..e0c3962
--- /dev/null
+++ b/daemon/fw/self-learning-strategy.hpp
@@ -0,0 +1,148 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_FW_SELF_LEARNING_STRATEGY_HPP
+#define NFD_DAEMON_FW_SELF_LEARNING_STRATEGY_HPP
+
+#include "fw/strategy.hpp"
+
+#include <ndn-cxx/lp/prefix-announcement-header.hpp>
+
+namespace nfd {
+namespace fw {
+
+/** \brief Self-learning strategy
+ *
+ *  This strategy first broadcasts Interest to learn a single path towards data,
+ *  then unicasts subsequent Interests along the learned path
+ *
+ *  \see https://redmine.named-data.net/attachments/864/Self-learning-strategy-v1.pdf
+ */
+class SelfLearningStrategy : public Strategy
+{
+public:
+  explicit
+  SelfLearningStrategy(Forwarder& forwarder, const Name& name = getStrategyName());
+
+  static const Name&
+  getStrategyName();
+
+  /// StrategyInfo on pit::InRecord
+  class InRecordInfo : public StrategyInfo
+  {
+  public:
+    static constexpr int
+    getTypeId()
+    {
+      return 1040;
+    }
+
+  public:
+    bool isNonDiscoveryInterest = false;
+  };
+
+  /// StrategyInfo on pit::OutRecord
+  class OutRecordInfo : public StrategyInfo
+  {
+  public:
+    static constexpr int
+    getTypeId()
+    {
+      return 1041;
+    }
+
+  public:
+    bool isNonDiscoveryInterest = false;
+  };
+
+public: // triggers
+  void
+  afterReceiveInterest(const Face& inFace, const Interest& interest,
+                       const shared_ptr<pit::Entry>& pitEntry) override;
+
+  void
+  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
+                   const Face& inFace, const Data& data) override;
+
+  void
+  afterReceiveNack(const Face& inFace, const lp::Nack& nack,
+                   const shared_ptr<pit::Entry>& pitEntry) override;
+
+private: // operations
+
+  /** \brief Send an Interest to all possible faces
+   *
+   *  This function is invoked when the forwarder has no matching FIB entries for
+   *  an incoming discovery Interest, which will be forwarded to faces that
+   *    - do not violate the Interest scope
+   *    - are non-local
+   *    - are not the face from which the Interest arrived, unless the face is ad-hoc
+   */
+  void
+  broadcastInterest(const Interest& interest, const Face& inFace,
+                    const shared_ptr<pit::Entry>& pitEntry);
+
+  /** \brief Send an Interest to \p nexthops
+   */
+  void
+  multicastInterest(const Interest& interest, const Face& inFace,
+                    const shared_ptr<pit::Entry>& pitEntry,
+                    const fib::NextHopList& nexthops);
+
+  /** \brief Find a Prefix Announcement for the Data on the RIB thread, and forward
+   *         the Data with the Prefix Announcement on the main thread
+   */
+  void
+  asyncProcessData(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data);
+
+  /** \brief Check whether a PrefixAnnouncement needs to be attached to an incoming Data
+   *
+   *  The conditions that a Data packet requires a PrefixAnnouncement are
+   *    - the incoming Interest was discovery and
+   *    - the outgoing Interest was non-discovery and
+   *    - this forwarder does not directly connect to the consumer
+   */
+  static bool
+  needPrefixAnn(const shared_ptr<pit::Entry>& pitEntry);
+
+  /** \brief Add a route using RibManager::slAnnounce on the RIB thread
+   */
+  void
+  addRoute(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace,
+           const Data& data, const ndn::PrefixAnnouncement& pa);
+
+  /** \brief renew a route using RibManager::slRenew on the RIB thread
+   */
+  void
+  renewRoute(const Name& name, FaceId inFaceId, time::milliseconds maxLifetime);
+
+private:
+  static const time::milliseconds ROUTE_RENEW_LIFETIME;
+};
+
+} // namespace fw
+} // namespace nfd
+
+#endif // NFD_DAEMON_FW_SELF_LEARNING_STRATEGY_HPP