blob: 6a70ead4044d68cacf6605a10ce67faa1db25e9e [file] [log] [blame]
Junxiao Shi5e5e4452015-09-24 16:56:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe4b22382018-06-10 14:37:24 -04002/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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"
Davide Pesavento284bd622019-03-31 02:10:02 -040027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Junxiao Shi5e5e4452015-09-24 16:56:52 -070029#include "face/generic-link-service.hpp"
30
Davide Pesaventoe4b22382018-06-10 14:37:24 -040031#include <ndn-cxx/encoding/encoding-buffer-fwd.hpp>
32
Junxiao Shi5e5e4452015-09-24 16:56:52 -070033namespace nfd {
34namespace fw {
35namespace tests {
36
Junxiao Shi6535f1e2015-10-08 13:02:18 -070037using face::GenericLinkService;
Davide Pesavento284bd622019-03-31 02:10:02 -040038using face::InternalClientTransport;
39using face::InternalForwarderTransport;
40
41TopologyLink::NodeTransport::NodeTransport(shared_ptr<Face> f, ReceiveProxy::Callback cb)
42 : face(std::move(f))
43 , transport(dynamic_cast<InternalForwarderTransport*>(face->getTransport()))
44 , proxy(std::move(cb))
45{
46 BOOST_ASSERT(transport != nullptr);
47}
Junxiao Shi5e5e4452015-09-24 16:56:52 -070048
Teng Liangf995f382017-04-04 22:09:39 +000049TopologyLink::TopologyLink(time::nanoseconds delay)
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070050{
51 this->setDelay(delay);
52}
53
54void
Teng Liangf995f382017-04-04 22:09:39 +000055TopologyLink::block(TopologyNode i, TopologyNode j)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070056{
Teng Liangf995f382017-04-04 22:09:39 +000057 m_transports.at(i).blockedDestinations.insert(j);
58}
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070059
Teng Liangf995f382017-04-04 22:09:39 +000060void
61TopologyLink::unblock(TopologyNode i, TopologyNode j)
62{
63 m_transports.at(i).blockedDestinations.erase(j);
64}
65
66void
67TopologyLink::setDelay(time::nanoseconds delay)
68{
69 BOOST_ASSERT(delay > time::nanoseconds::zero()); // zero delay does not work on macOS
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070070 m_delay = delay;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070071}
72
73void
Junxiao Shicde37ad2015-12-24 01:02:05 -070074TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070075{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040076 auto receiveCb = [this, i] (const Block& packet) { transmit(i, packet); };
Junxiao Shi5e5e4452015-09-24 16:56:52 -070077
Davide Pesavento284bd622019-03-31 02:10:02 -040078 auto ret = m_transports.emplace(std::piecewise_construct,
79 std::forward_as_tuple(i),
80 std::forward_as_tuple(std::move(face), std::move(receiveCb)));
81 BOOST_ASSERT(ret.second);
Teng Liangf995f382017-04-04 22:09:39 +000082
Davide Pesavento284bd622019-03-31 02:10:02 -040083 auto& node = ret.first->second;
84 node.transport->setPeer(&node.proxy);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070085}
86
87void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040088TopologyLink::transmit(TopologyNode i, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070089{
90 if (!m_isUp) {
91 return;
92 }
93
Teng Liangf995f382017-04-04 22:09:39 +000094 const auto& blockedDestinations = m_transports.at(i).blockedDestinations;
95
Davide Pesavento97210d52016-10-14 15:45:48 +020096 for (const auto& p : m_transports) {
Teng Liangf995f382017-04-04 22:09:39 +000097 if (p.first == i || blockedDestinations.count(p.first) > 0) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070098 continue;
99 }
100
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400101 getScheduler().schedule(m_delay, [packet, recipient = p.second.transport] {
102 recipient->receivePacket(packet);
103 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700104 }
105}
106
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600107TopologySingleLink::TopologySingleLink(shared_ptr<Face> forwarderFace)
Davide Pesavento284bd622019-03-31 02:10:02 -0400108 : m_face(std::move(forwarderFace))
109 , m_forwarderTransport(static_cast<InternalForwarderTransport*>(m_face->getTransport()))
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600110{
111}
112
113TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace)
114 : TopologySingleLink(std::move(forwarderFace))
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700115 , m_clientTransport(make_shared<InternalClientTransport>())
116 , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700117{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700118 this->recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700119}
120
121void
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700122TopologyAppLink::fail()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700123{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700124 m_clientTransport->connectToForwarder(nullptr);
125}
126
127void
128TopologyAppLink::recover()
129{
130 m_clientTransport->connectToForwarder(m_forwarderTransport);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700131}
132
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600133class TopologyBareLink::Observer : public face::InternalTransportBase
134{
135public:
136 explicit
137 Observer(std::vector<Block>& receivedPackets)
138 : m_receivedPackets(receivedPackets)
139 {
140 }
141
142 void
143 receivePacket(const Block& packet) final
144 {
145 m_receivedPackets.push_back(packet);
146 }
147
148private:
149 std::vector<Block>& m_receivedPackets;
150};
151
152TopologyBareLink::TopologyBareLink(shared_ptr<Face> forwarderFace)
153 : TopologySingleLink(std::move(forwarderFace))
154 , m_observer(make_unique<Observer>(sentPackets))
155{
156 m_forwarderTransport->setPeer(m_observer.get());
157}
158
159void
160TopologyBareLink::receivePacket(const Block& packet)
161{
162 m_forwarderTransport->receivePacket(packet);
163}
164
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000165class TopologyPcapLinkService : public GenericLinkService
166 , public TopologyPcap
167{
168private:
169 ///\todo #3941 call GenericLinkServiceCounters constructor in TopologyPcapLinkService constructor
170
171 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000172 doSendInterest(const Interest& interest, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000173 {
174 this->sentInterests.push_back(interest);
175 this->sentInterests.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000176 this->GenericLinkService::doSendInterest(interest, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000177 }
178
179 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000180 doSendData(const Data& data, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000181 {
182 this->sentData.push_back(data);
183 this->sentData.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000184 this->GenericLinkService::doSendData(data, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000185 }
186
187 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000188 doSendNack(const lp::Nack& nack, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000189 {
190 this->sentNacks.push_back(nack);
191 this->sentNacks.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000192 this->GenericLinkService::doSendNack(nack, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000193 }
194};
195
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700196TopologyNode
197TopologyTester::addForwarder(const std::string& label)
198{
199 size_t i = m_forwarders.size();
Davide Pesavento459cf192016-03-03 02:20:12 +0100200 m_forwarders.push_back(make_unique<Forwarder>());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700201 m_forwarderLabels.push_back(label);
202 BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size());
203 return i;
204}
205
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600206shared_ptr<Face>
207TopologyTester::makeFace(TopologyNode i, const FaceUri& localUri, const FaceUri& remoteUri,
208 ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
209{
210 Forwarder& forwarder = this->getForwarder(i);
211 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
212 make_unique<GenericLinkService>();
213 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, scope, linkType);
214 auto face = make_shared<Face>(std::move(service), std::move(transport));
215 forwarder.addFace(face);
216 return face;
217}
218
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700219shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000220TopologyTester::addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700221 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000222 ndn::nfd::LinkType linkType)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700223{
Yumin Xiaab497452016-05-10 20:23:24 +0800224 auto link = std::make_shared<TopologyLink>(delay);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700225 FaceUri remoteUri("topology://link/" + label);
Teng Liangf995f382017-04-04 22:09:39 +0000226 if (linkType == ndn::nfd::LINK_TYPE_NONE) {
227 linkType = forwarders.size() > 2 ? ndn::nfd::LINK_TYPE_MULTI_ACCESS :
228 ndn::nfd::LINK_TYPE_POINT_TO_POINT;
229 }
230 BOOST_ASSERT(forwarders.size() <= 2 || linkType != ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700231
232 for (TopologyNode i : forwarders) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700233 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600234 auto face = makeFace(i, localUri, remoteUri, ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType);
Davide Pesavento284bd622019-03-31 02:10:02 -0400235 link->addFace(i, std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700236 }
237
238 m_links.push_back(link); // keep a shared_ptr so callers don't have to
239 return link;
240}
241
242shared_ptr<TopologyAppLink>
243TopologyTester::addAppFace(const std::string& label, TopologyNode i)
244{
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700245 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label);
246 FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600247 auto face = makeFace(i, localUri, remoteUri, ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700248
Davide Pesavento284bd622019-03-31 02:10:02 -0400249 auto al = make_shared<TopologyAppLink>(std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700250 m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to
251 return al;
252}
253
254shared_ptr<TopologyAppLink>
255TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost)
256{
257 shared_ptr<TopologyAppLink> al = this->addAppFace(label, i);
258 this->registerPrefix(i, al->getForwarderFace(), prefix, cost);
259 return al;
260}
261
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600262shared_ptr<TopologyBareLink>
263TopologyTester::addBareLink(const std::string& label, TopologyNode i, ndn::nfd::FaceScope scope,
264 ndn::nfd::LinkType linkType)
265{
266 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label);
267 FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/bare/" + label);
268 auto face = makeFace(i, localUri, remoteUri, scope, linkType);
269
270 auto bl = make_shared<TopologyBareLink>(std::move(face));
271 m_bareLinks.push_back(bl); // keep a shared_ptr so callers don't have to
272 return bl;
273}
274
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700275void
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000276TopologyTester::enablePcap(bool isEnabled)
277{
278 m_wantPcap = isEnabled;
279}
280
281TopologyPcap&
282TopologyTester::getPcap(const Face& face)
283{
284 return dynamic_cast<TopologyPcapLinkService&>(*face.getLinkService());
285}
286
287void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700288TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost)
289{
290 Forwarder& forwarder = this->getForwarder(i);
291 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000292 fib::Entry* entry = fib.insert(prefix).first;
293 fib.addOrUpdateNextHop(*entry, const_cast<Face&>(face), cost);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700294}
295
296void
Saurab Dulala6dec222019-04-01 00:15:10 -0500297TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix, time::nanoseconds replyDelay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700298{
Saurab Dulala6dec222019-04-01 00:15:10 -0500299 BOOST_ASSERT(replyDelay >= 0_ns);
300
301 face.setInterestFilter(prefix, [=, &face] (const auto&, const auto& interest) {
302 auto data = makeData(interest.getName());
303 if (replyDelay == 0_ns) {
304 // reply immediately
305 face.put(*data);
306 }
307 else {
308 // delay the reply
309 getScheduler().schedule(replyDelay, [&face, data = std::move(data)] {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700310 face.put(*data);
311 });
Saurab Dulala6dec222019-04-01 00:15:10 -0500312 }
313 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700314}
315
316void
317TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix,
Teng Liangf995f382017-04-04 22:09:39 +0000318 time::nanoseconds interval, size_t n, int seq)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700319{
320 Name name(prefix);
Teng Liangf995f382017-04-04 22:09:39 +0000321 if (seq >= 0) {
322 name.appendSequenceNumber(seq);
323 ++seq;
324 }
325 else {
326 name.appendTimestamp();
327 }
328
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400329 auto interest = makeInterest(name);
Junxiao Shi9ede5bd2016-08-09 03:51:27 +0000330 face.expressInterest(*interest, nullptr, nullptr, nullptr);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700331
332 if (n > 1) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600333 getScheduler().schedule(interval, [=, &face] {
334 addIntervalConsumer(face, prefix, interval, n - 1, seq);
335 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700336 }
337}
338
339} // namespace tests
340} // namespace fw
341} // namespace nfd