fw: Adaptive SRTT-based Forwarding strategy

refs: #3566

Change-Id: Idae198bb0c2ae25e25aeceec0552b1c11be89c14
diff --git a/tests/daemon/fw/asf-measurements.t.cpp b/tests/daemon/fw/asf-measurements.t.cpp
new file mode 100644
index 0000000..2b83004
--- /dev/null
+++ b/tests/daemon/fw/asf-measurements.t.cpp
@@ -0,0 +1,130 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  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 "fw/asf-measurements.hpp"
+
+#include "tests/daemon/face/dummy-face.hpp"
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace fw {
+namespace asf {
+namespace tests {
+
+using namespace nfd::tests;
+
+BOOST_AUTO_TEST_SUITE(Fw)
+BOOST_AUTO_TEST_SUITE(TestAsfMeasurements)
+
+BOOST_AUTO_TEST_SUITE(TestRttStats)
+
+BOOST_FIXTURE_TEST_CASE(Basic, BaseFixture)
+{
+  RttStats stats;
+
+  BOOST_CHECK_EQUAL(stats.getRtt(), RttStats::RTT_NO_MEASUREMENT);
+  BOOST_CHECK_EQUAL(stats.getSrtt(), RttStats::RTT_NO_MEASUREMENT);
+
+  // Receive Data back in 50ms
+  RttEstimator::Duration rtt(time::milliseconds(50));
+  stats.addRttMeasurement(rtt);
+
+  BOOST_CHECK_EQUAL(stats.getRtt(), rtt);
+  BOOST_CHECK_EQUAL(stats.getSrtt(), rtt);
+
+  // Receive Data back in 50ms
+  stats.addRttMeasurement(rtt);
+
+  BOOST_CHECK_EQUAL(stats.getRtt(), rtt);
+  BOOST_CHECK_EQUAL(stats.getSrtt(), rtt);
+
+  // Receive Data back with a higher RTT
+  RttEstimator::Duration higherRtt(time::milliseconds(100));
+  stats.addRttMeasurement(higherRtt);
+
+  BOOST_CHECK_EQUAL(stats.getRtt(), higherRtt);
+  BOOST_CHECK_GT(stats.getSrtt(), rtt);
+  BOOST_CHECK_LT(stats.getSrtt(), higherRtt);
+
+  // Simulate timeout
+  RttStats::Rtt previousSrtt = stats.getSrtt();
+
+  stats.recordTimeout();
+
+  BOOST_CHECK_EQUAL(stats.getRtt(), RttStats::RTT_TIMEOUT);
+  BOOST_CHECK_EQUAL(stats.getSrtt(), previousSrtt);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestRttStats
+
+BOOST_AUTO_TEST_SUITE(TestFaceInfo)
+
+BOOST_FIXTURE_TEST_CASE(Basic, UnitTestTimeFixture)
+{
+  FaceInfo info;
+
+  scheduler::EventId id = scheduler::schedule(time::seconds(1), []{});
+  ndn::Name interestName("/ndn/interest");
+
+  // Receive Interest and forward to next hop; should update RTO information
+  info.setTimeoutEvent(id, interestName);
+  BOOST_CHECK_EQUAL(info.isTimeoutScheduled(), true);
+
+  // If the strategy tries to schedule an RTO when one is already scheduled, throw an exception
+  BOOST_CHECK_THROW(info.setTimeoutEvent(id, interestName), FaceInfo::Error);
+
+  // Receive Data
+  shared_ptr<Interest> interest = makeInterest(interestName);
+  shared_ptr<pit::Entry> pitEntry = make_shared<pit::Entry>(*interest);
+  std::shared_ptr<DummyFace> face = make_shared<DummyFace>();
+
+  pitEntry->insertOrUpdateOutRecord(face, *interest);
+
+  RttEstimator::Duration rtt(50);
+  this->advanceClocks(time::milliseconds(5), rtt);
+
+  info.recordRtt(pitEntry, *face);
+  info.cancelTimeoutEvent(interestName);
+
+  BOOST_CHECK_EQUAL(info.getRtt(), rtt);
+  BOOST_CHECK_EQUAL(info.getSrtt(), rtt);
+
+  // Send out another Interest which times out
+  info.setTimeoutEvent(id, interestName);
+
+  info.recordTimeout(interestName);
+  BOOST_CHECK_EQUAL(info.getRtt(), RttStats::RTT_TIMEOUT);
+  BOOST_CHECK_EQUAL(info.isTimeoutScheduled(), false);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestFaceInfo
+
+BOOST_AUTO_TEST_SUITE_END() // TestAsfStrategy
+BOOST_AUTO_TEST_SUITE_END() // Fw
+
+} // namespace tests
+} // namespace asf
+} // namespace fw
+} // namespace nfd
diff --git a/tests/daemon/fw/asf-strategy.t.cpp b/tests/daemon/fw/asf-strategy.t.cpp
new file mode 100644
index 0000000..687b0e2
--- /dev/null
+++ b/tests/daemon/fw/asf-strategy.t.cpp
@@ -0,0 +1,190 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  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 "fw/asf-strategy.hpp"
+
+#include "tests/test-common.hpp"
+#include "topology-tester.hpp"
+
+namespace nfd {
+namespace fw {
+namespace asf {
+namespace tests {
+
+using namespace nfd::fw::tests;
+
+BOOST_AUTO_TEST_SUITE(Fw)
+BOOST_FIXTURE_TEST_SUITE(TestAsfStrategy, UnitTestTimeFixture)
+
+class AsfGridFixture : public UnitTestTimeFixture
+{
+protected:
+  AsfGridFixture()
+  {
+    /*
+     *                  +---------+
+     *           +----->|  nodeB  |<------+
+     *           |      +---------+       |
+     *      10ms |                        | 10ms
+     *           v                        v
+     *      +---------+              +---------+
+     *      |  nodeA  |              |  nodeC  |
+     *      +---------+              +---------+
+     *           ^                        ^
+     *     100ms |                        | 100ms
+     *           |      +---------+       |
+     *           +----->|  nodeD  |<------+
+     *                  +---------+
+     */
+
+    nodeA = topo.addForwarder("A");
+    nodeB = topo.addForwarder("B");
+    nodeC = topo.addForwarder("C");
+    nodeD = topo.addForwarder("D");
+
+    topo.setStrategy<fw::AsfStrategy>(nodeA);
+    topo.setStrategy<fw::AsfStrategy>(nodeB);
+    topo.setStrategy<fw::AsfStrategy>(nodeC);
+    topo.setStrategy<fw::AsfStrategy>(nodeD);
+
+    linkAB = topo.addLink("AB", time::milliseconds(10), {nodeA, nodeB});
+    linkAD = topo.addLink("AD", time::milliseconds(100), {nodeA, nodeD});
+    linkBC = topo.addLink("BC", time::milliseconds(10), {nodeB, nodeC});
+    linkCD = topo.addLink("CD", time::milliseconds(100), {nodeC, nodeD});
+
+    consumer = topo.addAppFace("c", nodeA);
+    producer = topo.addAppFace("p", nodeC, PRODUCER_PREFIX);
+    topo.addEchoProducer(producer->getClientFace());
+
+    // Register producer prefix on consumer node
+    topo.registerPrefix(nodeA, linkAB->getFace(nodeA), PRODUCER_PREFIX, 10);
+    topo.registerPrefix(nodeA, linkAD->getFace(nodeA), PRODUCER_PREFIX, 5);
+  }
+
+  void
+  runConsumer()
+  {
+    topo.addIntervalConsumer(consumer->getClientFace(), PRODUCER_PREFIX, time::seconds(1), 30);
+    this->advanceClocks(time::milliseconds(1), time::seconds(30));
+  }
+
+protected:
+  TopologyTester topo;
+
+  TopologyNode nodeA;
+  TopologyNode nodeB;
+  TopologyNode nodeC;
+  TopologyNode nodeD;
+
+  shared_ptr<TopologyLink> linkAB;
+  shared_ptr<TopologyLink> linkAD;
+  shared_ptr<TopologyLink> linkBC;
+  shared_ptr<TopologyLink> linkCD;
+
+  shared_ptr<TopologyAppLink> consumer;
+  shared_ptr<TopologyAppLink> producer;
+
+  static const Name PRODUCER_PREFIX;
+};
+
+const Name AsfGridFixture::PRODUCER_PREFIX = Name("ndn:/hr/C");
+
+BOOST_FIXTURE_TEST_CASE(Basic, AsfGridFixture)
+{
+  // Both nodeB and nodeD have FIB entries to reach the producer
+  topo.registerPrefix(nodeB, linkBC->getFace(nodeB), PRODUCER_PREFIX);
+  topo.registerPrefix(nodeD, linkCD->getFace(nodeD), PRODUCER_PREFIX);
+
+  runConsumer();
+
+  // ASF should use the Face to nodeD because it has lower routing cost.
+  // After 5 seconds, a probe Interest should be sent to the Face to nodeB,
+  // and the probe should return Data quicker. ASF should then use the Face
+  // to nodeB to forward the remaining Interests.
+  BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 30);
+  BOOST_CHECK_GE(linkAB->getFace(nodeA).getCounters().nOutInterests, 24);
+  BOOST_CHECK_LE(linkAD->getFace(nodeA).getCounters().nOutInterests, 6);
+
+  // If the link from nodeA to nodeB fails, ASF should start using the Face
+  // to nodeD again.
+  linkAB->fail();
+
+  runConsumer();
+
+  // Only 59 Data because the first Interest to nodeB after the failure should timeout
+  BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 59);
+  BOOST_CHECK_LE(linkAB->getFace(nodeA).getCounters().nOutInterests, 30);
+  BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nOutInterests, 30);
+
+  // If the link from nodeA to nodeB recovers, ASF should probe the Face
+  // to nodeB and start using it again.
+  linkAB->recover();
+
+  // Advance time to ensure probing is due
+  this->advanceClocks(time::milliseconds(1), time::seconds(10));
+
+  runConsumer();
+
+  BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 89);
+  BOOST_CHECK_GE(linkAB->getFace(nodeA).getCounters().nOutInterests, 50);
+  BOOST_CHECK_LE(linkAD->getFace(nodeA).getCounters().nOutInterests, 40);
+
+  // If both links fail, nodeA should forward to the next hop with the lowest cost
+  linkAB->fail();
+  linkAD->fail();
+
+  runConsumer();
+
+  BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 89);
+  BOOST_CHECK_LE(linkAB->getFace(nodeA).getCounters().nOutInterests, 60);
+  BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nOutInterests, 60);
+}
+
+BOOST_FIXTURE_TEST_CASE(Nack, AsfGridFixture)
+{
+  // nodeB has a FIB entry to reach the producer, but nodeD does not
+  topo.registerPrefix(nodeB, linkBC->getFace(nodeB), PRODUCER_PREFIX);
+
+  // The strategy should first try to send to nodeD. But since nodeD does not have a route for
+  // the producer's prefix, it should return a NO_ROUTE Nack. The strategy should then start using the Face to
+  // nodeB.
+  runConsumer();
+
+  BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nInNacks, 1);
+  BOOST_CHECK_EQUAL(consumer->getForwarderFace().getCounters().nOutData, 29);
+  BOOST_CHECK_EQUAL(linkAB->getFace(nodeA).getCounters().nOutInterests, 29);
+
+  // nodeD should receive 2 Interests: one for the very first Interest and
+  // another from a probe
+  BOOST_CHECK_GE(linkAD->getFace(nodeA).getCounters().nOutInterests, 2);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestAsfStrategy
+BOOST_AUTO_TEST_SUITE_END() // Fw
+
+} // namespace tests
+} // namespace asf
+} // namespace fw
+} // namespace nfd