blob: fef30806d727953245194e3641657d484a6249dd [file] [log] [blame]
Junxiao Shi5e5e4452015-09-24 16:56:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifab9e0d2017-02-02 06:04:59 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shi5e5e4452015-09-24 16:56:52 -07004 * 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 "topology-tester.hpp"
27#include <ndn-cxx/encoding/encoding-buffer-fwd.hpp>
28#include "face/generic-link-service.hpp"
29
30namespace nfd {
31namespace fw {
32namespace tests {
33
Junxiao Shi6535f1e2015-10-08 13:02:18 -070034using face::InternalTransportBase;
35using face::InternalForwarderTransport;
36using face::InternalClientTransport;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070037using face::GenericLinkService;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070038
Teng Liangf995f382017-04-04 22:09:39 +000039TopologyLink::TopologyLink(time::nanoseconds delay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070040 : m_isUp(true)
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070041{
42 this->setDelay(delay);
43}
44
45void
Teng Liangf995f382017-04-04 22:09:39 +000046TopologyLink::block(TopologyNode i, TopologyNode j)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070047{
Teng Liangf995f382017-04-04 22:09:39 +000048 m_transports.at(i).blockedDestinations.insert(j);
49}
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070050
Teng Liangf995f382017-04-04 22:09:39 +000051void
52TopologyLink::unblock(TopologyNode i, TopologyNode j)
53{
54 m_transports.at(i).blockedDestinations.erase(j);
55}
56
57void
58TopologyLink::setDelay(time::nanoseconds delay)
59{
60 BOOST_ASSERT(delay > time::nanoseconds::zero()); // zero delay does not work on macOS
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070061 m_delay = delay;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070062}
63
64void
Junxiao Shicde37ad2015-12-24 01:02:05 -070065TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070066{
Junxiao Shi5e5e4452015-09-24 16:56:52 -070067 BOOST_ASSERT(m_transports.count(i) == 0);
Teng Liangf995f382017-04-04 22:09:39 +000068 auto& nodeTransport = m_transports[i];
Junxiao Shi5e5e4452015-09-24 16:56:52 -070069
Teng Liangf995f382017-04-04 22:09:39 +000070 nodeTransport.face = face;
71
72 nodeTransport.transport = dynamic_cast<InternalTransportBase*>(face->getTransport());
73 BOOST_ASSERT(nodeTransport.transport != nullptr);
74 nodeTransport.transport->afterSend.connect(
75 [this, i] (const Block& packet) { this->transmit(i, packet); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -070076}
77
78void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070079TopologyLink::transmit(TopologyNode i, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070080{
81 if (!m_isUp) {
82 return;
83 }
84
Teng Liangf995f382017-04-04 22:09:39 +000085 const auto& blockedDestinations = m_transports.at(i).blockedDestinations;
86
Davide Pesavento97210d52016-10-14 15:45:48 +020087 for (const auto& p : m_transports) {
Teng Liangf995f382017-04-04 22:09:39 +000088 if (p.first == i || blockedDestinations.count(p.first) > 0) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070089 continue;
90 }
91
Teng Liangf995f382017-04-04 22:09:39 +000092 InternalTransportBase* recipient = p.second.transport;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070093 this->scheduleReceive(recipient, packet);
94 }
95}
96
Junxiao Shi5e5e4452015-09-24 16:56:52 -070097void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070098TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070099{
100 scheduler::schedule(m_delay, [packet, recipient] {
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700101 recipient->receiveFromLink(packet);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700102 });
103}
104
Junxiao Shicde37ad2015-12-24 01:02:05 -0700105TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace)
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700106 : m_face(forwarderFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 , m_forwarderTransport(static_cast<InternalForwarderTransport*>(forwarderFace->getTransport()))
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700108 , m_clientTransport(make_shared<InternalClientTransport>())
109 , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700110{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700111 this->recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700112}
113
114void
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700115TopologyAppLink::fail()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700116{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700117 m_clientTransport->connectToForwarder(nullptr);
118}
119
120void
121TopologyAppLink::recover()
122{
123 m_clientTransport->connectToForwarder(m_forwarderTransport);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700124}
125
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000126class TopologyPcapLinkService : public GenericLinkService
127 , public TopologyPcap
128{
129private:
130 ///\todo #3941 call GenericLinkServiceCounters constructor in TopologyPcapLinkService constructor
131
132 void
133 doSendInterest(const Interest& interest) override
134 {
135 this->sentInterests.push_back(interest);
136 this->sentInterests.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
137 this->GenericLinkService::doSendInterest(interest);
138 }
139
140 void
141 doSendData(const Data& data) override
142 {
143 this->sentData.push_back(data);
144 this->sentData.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
145 this->GenericLinkService::doSendData(data);
146 }
147
148 void
149 doSendNack(const lp::Nack& nack) override
150 {
151 this->sentNacks.push_back(nack);
152 this->sentNacks.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
153 this->GenericLinkService::doSendNack(nack);
154 }
155};
156
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700157TopologyNode
158TopologyTester::addForwarder(const std::string& label)
159{
160 size_t i = m_forwarders.size();
Davide Pesavento459cf192016-03-03 02:20:12 +0100161 m_forwarders.push_back(make_unique<Forwarder>());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700162 m_forwarderLabels.push_back(label);
163 BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size());
164 return i;
165}
166
167shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000168TopologyTester::addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700169 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000170 ndn::nfd::LinkType linkType)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700171{
Yumin Xiaab497452016-05-10 20:23:24 +0800172 auto link = std::make_shared<TopologyLink>(delay);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 FaceUri remoteUri("topology://link/" + label);
Teng Liangf995f382017-04-04 22:09:39 +0000174 if (linkType == ndn::nfd::LINK_TYPE_NONE) {
175 linkType = forwarders.size() > 2 ? ndn::nfd::LINK_TYPE_MULTI_ACCESS :
176 ndn::nfd::LINK_TYPE_POINT_TO_POINT;
177 }
178 BOOST_ASSERT(forwarders.size() <= 2 || linkType != ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700179
180 for (TopologyNode i : forwarders) {
181 Forwarder& forwarder = this->getForwarder(i);
182 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label);
183
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000184 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
185 make_unique<GenericLinkService>();
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700186 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700187 ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700188 auto face = make_shared<Face>(std::move(service), std::move(transport));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189
Junxiao Shicde37ad2015-12-24 01:02:05 -0700190 forwarder.addFace(face);
191 link->addFace(i, face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700192 }
193
194 m_links.push_back(link); // keep a shared_ptr so callers don't have to
195 return link;
196}
197
198shared_ptr<TopologyAppLink>
199TopologyTester::addAppFace(const std::string& label, TopologyNode i)
200{
201 Forwarder& forwarder = this->getForwarder(i);
202 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label);
203 FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label);
204
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000205 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
206 make_unique<GenericLinkService>();
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700207 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700208 ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700209 auto face = make_shared<Face>(std::move(service), std::move(transport));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700210
Junxiao Shicde37ad2015-12-24 01:02:05 -0700211 forwarder.addFace(face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700212
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213 auto al = make_shared<TopologyAppLink>(face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700214 m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to
215 return al;
216}
217
218shared_ptr<TopologyAppLink>
219TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost)
220{
221 shared_ptr<TopologyAppLink> al = this->addAppFace(label, i);
222 this->registerPrefix(i, al->getForwarderFace(), prefix, cost);
223 return al;
224}
225
226void
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000227TopologyTester::enablePcap(bool isEnabled)
228{
229 m_wantPcap = isEnabled;
230}
231
232TopologyPcap&
233TopologyTester::getPcap(const Face& face)
234{
235 return dynamic_cast<TopologyPcapLinkService&>(*face.getLinkService());
236}
237
238void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700239TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost)
240{
241 Forwarder& forwarder = this->getForwarder(i);
242 Fib& fib = forwarder.getFib();
Junxiao Shia6de4292016-07-12 02:08:10 +0000243 fib.insert(prefix).first->addNextHop(const_cast<Face&>(face), cost);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700244}
245
246void
247TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix)
248{
249 face.setInterestFilter(prefix,
250 [&face] (const ndn::InterestFilter&, const Interest& interest) {
251 shared_ptr<Data> data = makeData(interest.getName());
252 face.put(*data);
253 });
254}
255
256void
257TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix,
Teng Liangf995f382017-04-04 22:09:39 +0000258 time::nanoseconds interval, size_t n, int seq)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700259{
260 Name name(prefix);
Teng Liangf995f382017-04-04 22:09:39 +0000261 if (seq >= 0) {
262 name.appendSequenceNumber(seq);
263 ++seq;
264 }
265 else {
266 name.appendTimestamp();
267 }
268
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700269 shared_ptr<Interest> interest = makeInterest(name);
Junxiao Shi9ede5bd2016-08-09 03:51:27 +0000270 face.expressInterest(*interest, nullptr, nullptr, nullptr);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700271
272 if (n > 1) {
273 scheduler::schedule(interval, bind(&TopologyTester::addIntervalConsumer, this,
Teng Liangf995f382017-04-04 22:09:39 +0000274 ref(face), prefix, interval, n - 1, seq));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700275 }
276}
277
278} // namespace tests
279} // namespace fw
280} // namespace nfd