blob: ab3f6410941bf80144a822708883d280c414ce5e [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 Shi3cb4fc62014-12-25 22:17:39 -070036BOOST_FIXTURE_TEST_SUITE(FwNccStrategy, UnitTestTimeFixture)
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{
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070044 LimitedIo limitedIo(this);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045 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 Shi3cb4fc62014-12-25 22:17:39 -070048 strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049
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 Shi3cb4fc62014-12-25 22:17:39 -070083 limitedIo.run(1, time::milliseconds(500), time::milliseconds(10));
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 Shi3cb4fc62014-12-25 22:17:39 -070091 this->advanceClocks(time::milliseconds(10), 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 Shi3cb4fc62014-12-25 22:17:39 -0700103 this->advanceClocks(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{
Junxiao Shicbb490a2014-08-13 12:24:24 -0700110 Forwarder forwarder;
111 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
112 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
Junxiao Shicbb490a2014-08-13 12:24:24 -0700113
114 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
115 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
116 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
117 forwarder.addFace(face1);
118 forwarder.addFace(face2);
119 forwarder.addFace(face3);
120
121 Fib& fib = forwarder.getFib();
122 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
123 fibEntry->addNextHop(face1, 10);
124
125 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
126 strategyChoice.install(strategy);
127 strategyChoice.insert(Name(), strategy->getName());
128
129 Pit& pit = forwarder.getPit();
130
131 // first Interest: strategy follows routing and forwards to face1
132 shared_ptr<Interest> interest1 = makeInterest("ndn:/nztwIvHX/%00");
133 interest1->setInterestLifetime(time::milliseconds(8000));
134 shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
135
136 pitEntry1->insertOrUpdateInRecord(face3, *interest1);
137 strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
138
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700139 this->advanceClocks(time::milliseconds(1));
Junxiao Shicbb490a2014-08-13 12:24:24 -0700140 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
141 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
142
143 // face1 responds
144 shared_ptr<Data> data1 = makeData("ndn:/nztwIvHX/%00");
Junxiao Shi82e7f582014-09-07 15:15:40 -0700145 strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700146 this->advanceClocks(time::milliseconds(10), time::milliseconds(500));
Junxiao Shicbb490a2014-08-13 12:24:24 -0700147
148 // second Interest: bestFace is face1, nUpstreams becomes 0,
149 // therefore pitEntryInfo->maxInterval cannot be calculated from deferRange and nUpstreams
150 shared_ptr<Interest> interest2 = makeInterest("ndn:/nztwIvHX/%01");
151 interest2->setInterestLifetime(time::milliseconds(8000));
152 shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first;
153
154 pitEntry2->insertOrUpdateInRecord(face3, *interest2);
155 strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2);
156
157 // FIB entry is changed before doPropagate executes
158 fibEntry->addNextHop(face2, 20);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700159 this->advanceClocks(time::milliseconds(10), time::milliseconds(1000));// should not crash
Junxiao Shicbb490a2014-08-13 12:24:24 -0700160}
161
Junxiao Shi82e7f582014-09-07 15:15:40 -0700162BOOST_AUTO_TEST_CASE(Bug1961)
163{
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700164 LimitedIo limitedIo(this);
Junxiao Shi82e7f582014-09-07 15:15:40 -0700165 Forwarder forwarder;
166 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
167 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700168 strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
Junxiao Shi82e7f582014-09-07 15:15:40 -0700169
170 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
171 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
172 shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
173 forwarder.addFace(face1);
174 forwarder.addFace(face2);
175 forwarder.addFace(face3);
176
177 Fib& fib = forwarder.getFib();
178 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
179 fibEntry->addNextHop(face1, 10);
180 fibEntry->addNextHop(face2, 20);
181
182 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
183 strategyChoice.install(strategy);
184 strategyChoice.insert(Name(), strategy->getName());
185
186 Pit& pit = forwarder.getPit();
187
188 // first Interest: strategy forwards to face1 and face2
189 shared_ptr<Interest> interest1 = makeInterest("ndn:/seRMz5a6/%00");
190 interest1->setInterestLifetime(time::milliseconds(2000));
191 shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
192
193 pitEntry1->insertOrUpdateInRecord(face3, *interest1);
194 strategy->afterReceiveInterest(*face3, *interest1, fibEntry, pitEntry1);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700195 limitedIo.run(2 - strategy->m_sendInterestHistory.size(),
196 time::milliseconds(2000), time::milliseconds(10));
Junxiao Shi82e7f582014-09-07 15:15:40 -0700197 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
198 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face1);
199 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
200
201 // face1 responds
202 shared_ptr<Data> data1 = makeData("ndn:/seRMz5a6/%00");
203 strategy->beforeSatisfyInterest(pitEntry1, *face1, *data1);
204 pitEntry1->deleteInRecords();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700205 this->advanceClocks(time::milliseconds(10));
Junxiao Shi82e7f582014-09-07 15:15:40 -0700206 // face2 also responds
207 strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700208 this->advanceClocks(time::milliseconds(10));
Junxiao Shi82e7f582014-09-07 15:15:40 -0700209
210 // second Interest: bestFace should be face 1
211 shared_ptr<Interest> interest2 = makeInterest("ndn:/seRMz5a6/%01");
212 interest2->setInterestLifetime(time::milliseconds(2000));
213 shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first;
214
215 pitEntry2->insertOrUpdateInRecord(face3, *interest2);
216 strategy->afterReceiveInterest(*face3, *interest2, fibEntry, pitEntry2);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700217 limitedIo.run(3 - strategy->m_sendInterestHistory.size(),
218 time::milliseconds(2000), time::milliseconds(10));
Junxiao Shi82e7f582014-09-07 15:15:40 -0700219
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 Shi8bfd56d2014-10-08 10:06:00 -0700224BOOST_AUTO_TEST_CASE(Bug1971)
225{
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700226 LimitedIo limitedIo(this);
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700227 Forwarder forwarder;
228 typedef StrategyTester<fw::NccStrategy> NccStrategyTester;
229 shared_ptr<NccStrategyTester> strategy = make_shared<NccStrategyTester>(ref(forwarder));
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700230 strategy->onAction.connect(bind(&LimitedIo::afterOp, &limitedIo));
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700231
232 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
233 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
234 forwarder.addFace(face1);
235 forwarder.addFace(face2);
236
237 Fib& fib = forwarder.getFib();
238 shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
239 fibEntry->addNextHop(face2, 10);
240
241 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
242 strategyChoice.install(strategy);
243 strategyChoice.insert(Name(), strategy->getName());
244
245 Pit& pit = forwarder.getPit();
246
247 // first Interest: strategy forwards to face2
248 shared_ptr<Interest> interest1 = makeInterest("ndn:/M4mBXCsd");
249 interest1->setInterestLifetime(time::milliseconds(2000));
250 shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
251
252 pitEntry1->insertOrUpdateInRecord(face1, *interest1);
253 strategy->afterReceiveInterest(*face1, *interest1, fibEntry, pitEntry1);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700254 limitedIo.run(1 - strategy->m_sendInterestHistory.size(),
255 time::milliseconds(2000), time::milliseconds(10));
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700256 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 1);
257 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[0].get<1>(), face2);
258
259 // face2 responds
260 shared_ptr<Data> data1 = makeData("ndn:/M4mBXCsd");
261 data1->setFreshnessPeriod(time::milliseconds(5));
262 strategy->beforeSatisfyInterest(pitEntry1, *face2, *data1);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700263 pitEntry1->deleteOutRecord(*face2);
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700264 pitEntry1->deleteInRecords();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700265 this->advanceClocks(time::milliseconds(10));
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700266
267 // similar Interest: strategy should still forward it
268 pitEntry1->insertOrUpdateInRecord(face1, *interest1);
269 strategy->afterReceiveInterest(*face1, *interest1, fibEntry, pitEntry1);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700270 limitedIo.run(2 - strategy->m_sendInterestHistory.size(),
271 time::milliseconds(2000), time::milliseconds(10));
Junxiao Shi8bfd56d2014-10-08 10:06:00 -0700272 BOOST_REQUIRE_EQUAL(strategy->m_sendInterestHistory.size(), 2);
273 BOOST_CHECK_EQUAL(strategy->m_sendInterestHistory[1].get<1>(), face2);
274}
275
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700276BOOST_AUTO_TEST_SUITE_END()
277
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700278} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700279} // namespace nfd