blob: fece22ef497eebf393cdd4e95cff71da2ff32879 [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
4 * 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 "fw/best-route-strategy2.hpp"
27#include "strategy-tester.hpp"
28
29#include "tests/test-common.hpp"
30#include "tests/limited-io.hpp"
31#include "tests/daemon/face/dummy-face.hpp"
32
33namespace nfd {
34namespace tests {
35
36BOOST_FIXTURE_TEST_SUITE(FwBestRouteStrategy2, BaseFixture)
37
38BOOST_AUTO_TEST_CASE(Forward)
39{
40 LimitedIo limitedIo;
41 Forwarder forwarder;
42 typedef StrategyTester<fw::BestRouteStrategy2> BestRouteStrategy2Tester;
43 BestRouteStrategy2Tester strategy(forwarder);
44
45 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
46 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
47 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
48 shared_ptr<DummyFace> face4 = make_shared<DummyFace>();
49 shared_ptr<DummyFace> face5 = make_shared<DummyFace>();
50 forwarder.addFace(face1);
51 forwarder.addFace(face2);
52 forwarder.addFace(face3);
53 forwarder.addFace(face4);
54 forwarder.addFace(face5);
55
56 Fib& fib = forwarder.getFib();
57 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
58 fibEntry->addNextHop(face1, 10);
59 fibEntry->addNextHop(face2, 20);
60 fibEntry->addNextHop(face3, 30);
61
62 shared_ptr<Interest> interest = makeInterest("ndn:/BzgFBchqA");
63 Pit& pit = forwarder.getPit();
64 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
65
Junxiao Shi1f30aac2014-11-04 18:45:56 -070066 const time::nanoseconds RETRANSMISSION_10P = time::duration_cast<time::nanoseconds>(
67 fw::BestRouteStrategy2::MIN_RETRANSMISSION_INTERVAL * 0.1); // 10%
68 const time::nanoseconds RETRANSMISSION_2 = time::duration_cast<time::nanoseconds>(
69 fw::BestRouteStrategy2::MIN_RETRANSMISSION_INTERVAL * 2.0); // x2
Junxiao Shi986b8492014-08-20 12:07:14 -070070
Junxiao Shi1f30aac2014-11-04 18:45:56 -070071 // first Interest goes to nexthop with lowest FIB cost,
72 // however face1 is downstream so it cannot be used
Junxiao Shi986b8492014-08-20 12:07:14 -070073 pitEntry->insertOrUpdateInRecord(face1, *interest);
74 strategy.afterReceiveInterest(*face1, *interest, fibEntry, pitEntry);
75 BOOST_REQUIRE_EQUAL(strategy.m_sendInterestHistory.size(), 1);
76 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.back().get<1>(), face2);
Junxiao Shi986b8492014-08-20 12:07:14 -070077
Junxiao Shi1f30aac2014-11-04 18:45:56 -070078 // downstream retransmits frequently, but the strategy should not send Interests
79 // more often than MIN_RETRANSMISSION_INTERVAL
80 scheduler::EventId retxFrom4Evt;
81 size_t nSentLast = strategy.m_sendInterestHistory.size();
82 time::steady_clock::TimePoint timeSentLast = time::steady_clock::now();
83 function<void()> periodicalRetxFrom4 = [&] {
84 pitEntry->insertOrUpdateInRecord(face4, *interest);
85 strategy.afterReceiveInterest(*face4, *interest, fibEntry, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070086
Junxiao Shi1f30aac2014-11-04 18:45:56 -070087 size_t nSent = strategy.m_sendInterestHistory.size();
88 if (nSent > nSentLast) {
89 BOOST_CHECK_EQUAL(nSent - nSentLast, 1);
90 time::steady_clock::TimePoint timeSent = time::steady_clock::now();
91 BOOST_CHECK_GE(timeSent - timeSentLast,
92 fw::BestRouteStrategy2::MIN_RETRANSMISSION_INTERVAL);
93 nSentLast = nSent;
94 timeSentLast = timeSent;
95 }
Junxiao Shi986b8492014-08-20 12:07:14 -070096
Junxiao Shi1f30aac2014-11-04 18:45:56 -070097 retxFrom4Evt = scheduler::schedule(RETRANSMISSION_10P, periodicalRetxFrom4);
98 };
99 periodicalRetxFrom4();
100 limitedIo.defer(fw::BestRouteStrategy2::MIN_RETRANSMISSION_INTERVAL * 16);
101 scheduler::cancel(retxFrom4Evt);
Junxiao Shi986b8492014-08-20 12:07:14 -0700102
Junxiao Shi1f30aac2014-11-04 18:45:56 -0700103 // nexthops for accepted retransmissions: follow FIB cost,
104 // later forward to an eligible upstream with earliest OutRecord
105 BOOST_REQUIRE_GE(strategy.m_sendInterestHistory.size(), 6);
106 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[1].get<1>(), face1);
107 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[2].get<1>(), face3);
108 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[3].get<1>(), face2);
109 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[4].get<1>(), face1);
110 BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[5].get<1>(), face3);
Junxiao Shi986b8492014-08-20 12:07:14 -0700111
112 fibEntry->removeNextHop(face1);
113
Junxiao Shi1f30aac2014-11-04 18:45:56 -0700114 strategy.m_sendInterestHistory.clear();
115 for (int i = 0; i < 3; ++i) {
116 limitedIo.defer(RETRANSMISSION_2);
117 pitEntry->insertOrUpdateInRecord(face5, *interest);
118 strategy.afterReceiveInterest(*face5, *interest, fibEntry, pitEntry);
119 }
120 BOOST_REQUIRE_EQUAL(strategy.m_sendInterestHistory.size(), 3);
121 BOOST_CHECK_NE(strategy.m_sendInterestHistory[0].get<1>(), face1);
122 BOOST_CHECK_NE(strategy.m_sendInterestHistory[1].get<1>(), face1);
123 BOOST_CHECK_NE(strategy.m_sendInterestHistory[2].get<1>(), face1);
Junxiao Shi986b8492014-08-20 12:07:14 -0700124 // face1 cannot be used because it's gone from FIB entry
125}
126
127BOOST_AUTO_TEST_SUITE_END()
128
129} // namespace tests
130} // namespace nfd