blob: ef89bb1739b04b682d001dcd35ce48034df54675 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi82e7f582014-09-07 15:15:40 -07003 * 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025
26#include "fw/ncc-strategy.hpp"
27#include "strategy-tester.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070028#include "tests/daemon/face/dummy-face.hpp"
29#include "tests/limited-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "tests/test-common.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070032
33namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070035
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, BaseFixture)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070037
38// NccStrategy is fairly complex.
39// The most important property is:
40// it remembers which upstream is the fastest to return Data,
41// and favors this upstream in subsequent Interests.
42BOOST_AUTO_TEST_CASE(FavorRespondingUpstream)
43{
44 LimitedIo limitedIo;
45 Forwarder forwarder;
46 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
Alexander Afanasyevf6980282014-05-13 18:28:40 -070047 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048 strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
49
50 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
51 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
52 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
53 forwarder.addFace(face1);
54 forwarder.addFace(face2);
55 forwarder.addFace(face3);
56
57 Fib& fib = forwarder.getFib();
58 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070059 fibEntry->addNextHop(face1, 10);
60 fibEntry->addNextHop(face2, 20);
61
Junxiao Shi7bb01512014-03-05 21:34:09 -070062 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
63 strategyChoice.install(strategy);
64 strategyChoice.insert(Name(), strategy->getName());
65
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070066 Pit& pit = forwarder.getPit();
67
68 // first Interest: strategy knows nothing and follows routing
Junxiao Shif3c07812014-03-11 21:48:49 -070069 shared_ptr<Interest> interest1p = makeInterest("ndn:/0Jm1ajrW/%00");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070070 Interest& interest1 = *interest1p;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070071 interest1.setInterestLifetime(time::milliseconds(8000));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070072 shared_ptr<pit::Entry> pitEntry1 = pit.insert(interest1).first;
73
74 pitEntry1->insertOrUpdateInRecord(face3, interest1);
75 strategy->afterReceiveInterest(*face3, interest1, fibEntry, pitEntry1);
76
77 // forwards to face1 because routing says it's best
Junxiao Shi15539102014-10-08 22:39:51 -070078 // (no io run here: afterReceiveInterest has already sent the Interest)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070079 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
80 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
81
82 // forwards to face2 because face1 doesn't respond
Junxiao Shi15539102014-10-08 22:39:51 -070083 limitedIo.run(1, time::milliseconds(500));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070084 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
85 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
86
87 // face2 responds
Junxiao Shif3c07812014-03-11 21:48:49 -070088 shared_ptr<Data> data1p = makeData("ndn:/0Jm1ajrW/%00");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070089 Data& data1 = *data1p;
Junxiao Shi82e7f582014-09-07 15:15:40 -070090 strategy->beforeSatisfyInterest(pitEntry1, *face2, data1);
Junxiao Shi15539102014-10-08 22:39:51 -070091 limitedIo.defer(time::milliseconds(500));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070092
93 // second Interest: strategy knows face2 is best
Junxiao Shif3c07812014-03-11 21:48:49 -070094 shared_ptr<Interest> interest2p = makeInterest("ndn:/0Jm1ajrW/%00%01");
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070095 Interest& interest2 = *interest2p;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070096 interest2.setInterestLifetime(time::milliseconds(8000));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097 shared_ptr<pit::Entry> pitEntry2 = pit.insert(interest2).first;
98
99 pitEntry2->insertOrUpdateInRecord(face3, interest2);
100 strategy->afterReceiveInterest(*face3, interest2, fibEntry, pitEntry2);
101
102 // forwards to face2 because it responds previously
Junxiao Shi15539102014-10-08 22:39:51 -0700103 limitedIo.defer(time::milliseconds(1));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700104 BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
105 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face2);
106}
107
Junxiao Shicbb490a2014-08-13 12:24:24 -0700108BOOST_AUTO_TEST_CASE(Bug1853)
109{
110 LimitedIo limitedIo;
111 Forwarder forwarder;
112 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
113 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
114 strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
115
116 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
117 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
118 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
119 forwarder.addFace(face1);
120 forwarder.addFace(face2);
121 forwarder.addFace(face3);
122
123 Fib& fib = forwarder.getFib();
124 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
125 fibEntry->addNextHop(face1, 10);
126
127 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
128 strategyChoice.install(strategy);
129 strategyChoice.insert(Name(), strategy->getName());
130
131 Pit& pit = forwarder.getPit();
132
133 // first Interest: strategy follows routing and forwards to face1
134 shared_ptr<Interest> interest1 = makeInterest("ndn:/nztwIvHX/%00");
135 interest1->setInterestLifetime(time::milliseconds(8000));
136 shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
137
138 pitEntry1->insertOrUpdateInRecord(face3, *interest1);
139 strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
140
141 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1));
142 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
143 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
144
145 // face1 responds
146 shared_ptr<Data> data1 = makeData("ndn:/nztwIvHX/%00");
Junxiao Shi82e7f582014-09-07 15:15:40 -0700147 strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
Junxiao Shicbb490a2014-08-13 12:24:24 -0700148 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(500));
149
150 // second Interest: bestFace is face1, nUpstreams becomes 0,
151 // therefore pitEntryInfo->maxInterval cannot be calculated from deferRange and nUpstreams
152 shared_ptr<Interest> interest2 = makeInterest("ndn:/nztwIvHX/%01");
153 interest2->setInterestLifetime(time::milliseconds(8000));
154 shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first;
155
156 pitEntry2->insertOrUpdateInRecord(face3, *interest2);
157 strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2);
158
159 // FIB entry is changed before doPropagate executes
160 fibEntry->addNextHop(face2, 20);
161 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(1000));// should not crash
162}
163
Junxiao Shi82e7f582014-09-07 15:15:40 -0700164BOOST_AUTO_TEST_CASE(Bug1961)
165{
166 LimitedIo limitedIo;
167 Forwarder forwarder;
168 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
169 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
170 strategy->onAction += bind(&LimitedIo::afterOp, &limitedIo);
171
172 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
173 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
174 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
175 forwarder.addFace(face1);
176 forwarder.addFace(face2);
177 forwarder.addFace(face3);
178
179 Fib& fib = forwarder.getFib();
180 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
181 fibEntry->addNextHop(face1, 10);
182 fibEntry->addNextHop(face2, 20);
183
184 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
185 strategyChoice.install(strategy);
186 strategyChoice.insert(Name(), strategy->getName());
187
188 Pit& pit = forwarder.getPit();
189
190 // first Interest: strategy forwards to face1 and face2
191 shared_ptr<Interest> interest1 = makeInterest("ndn:/seRMz5a6/%00");
192 interest1->setInterestLifetime(time::milliseconds(2000));
193 shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
194
195 pitEntry1->insertOrUpdateInRecord(face3, *interest1);
196 strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
197 limitedIo.run(2 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
198 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
199 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
200 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
201
202 // face1 responds
203 shared_ptr<Data> data1 = makeData("ndn:/seRMz5a6/%00");
204 strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
205 pitEntry1->deleteInRecords();
206 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10));
207 // face2 also responds
208 strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1);
209 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(10));
210
211 // second Interest: bestFace should be face 1
212 shared_ptr<Interest> interest2 = makeInterest("ndn:/seRMz5a6/%01");
213 interest2->setInterestLifetime(time::milliseconds(2000));
214 shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first;
215
216 pitEntry2->insertOrUpdateInRecord(face3, *interest2);
217 strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2);
218 limitedIo.run(3 - strategy->m_sendInterestHistory.size(), time::milliseconds(2000));
219
220 BOOST_REQUIRE_GE(strategy->m_sendInterestHistory.size(), 3);
221 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[2].get<1>(), face1);
222}
Junxiao Shicbb490a2014-08-13 12:24:24 -0700223
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700224BOOST_AUTO_TEST_SUITE_END()
225
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700226} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700227} // namespace nfd