blob: da4e488fc09ef89c38c139598dbbe5a241b87fef [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -05002/*
Davide Pesavento284bd622019-03-31 02:10:02 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -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/** \file
27 * \brief allows testing forwarding in a network topology
28 */
29
Davide Pesavento97210d52016-10-14 15:45:48 +020030#ifndef NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
31#define NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070032
Junxiao Shi6535f1e2015-10-08 13:02:18 -070033#include "face/internal-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070034#include "face/face.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070035#include "fw/strategy.hpp"
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000036#include "choose-strategy.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070037#include "tests/test-common.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070038
Davide Pesavento97210d52016-10-14 15:45:48 +020039#include <ndn-cxx/face.hpp>
40
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070041namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080042namespace fw {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070043namespace tests {
44
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080045using namespace nfd::tests;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070046
47/** \brief identifies a node (forwarder) in the topology
48 */
49typedef size_t TopologyNode;
50
Junxiao Shi6535f1e2015-10-08 13:02:18 -070051/** \brief represents a network link in the topology which connects two or more nodes
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070052 */
Junxiao Shi6535f1e2015-10-08 13:02:18 -070053class TopologyLink : noncopyable
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070054{
55public:
Junxiao Shi6535f1e2015-10-08 13:02:18 -070056 explicit
Teng Liangf995f382017-04-04 22:09:39 +000057 TopologyLink(time::nanoseconds delay);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058
59 /** \brief fail the link, cause packets to be dropped silently
60 */
61 void
62 fail()
63 {
64 m_isUp = false;
65 }
66
67 /** \brief recover the link from a failure
68 */
69 void
70 recover()
71 {
72 m_isUp = true;
73 }
74
Teng Liangf995f382017-04-04 22:09:39 +000075 /** \brief block transmission from i to j
76 *
77 * Packets transmitted by i would not be delivered to j. Packets from j to i are unaffected.
78 * This can be used to simulate a wireless channel.
79 */
80 void
81 block(TopologyNode i, TopologyNode j);
82
83 /** \brief unblock transmission from i to j
84 */
85 void
86 unblock(TopologyNode i, TopologyNode j);
87
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070088 /** \brief change the link delay
89 * \param delay link delay, must be positive
90 */
91 void
Teng Liangf995f382017-04-04 22:09:39 +000092 setDelay(time::nanoseconds delay);
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070093
Junxiao Shicde37ad2015-12-24 01:02:05 -070094 /** \brief attach a face to the link
95 * \param i forwarder index
96 * \param face a Face with InternalForwarderTransport
97 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070098 void
Junxiao Shicde37ad2015-12-24 01:02:05 -070099 addFace(TopologyNode i, shared_ptr<Face> face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700100
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700101 /** \return a face of forwarder \p i which is attached to this link
102 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700103 Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700104 getFace(TopologyNode i)
105 {
Teng Liangf995f382017-04-04 22:09:39 +0000106 return *m_transports.at(i).face;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700107 }
108
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700109private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700110 void
Davide Pesavento284bd622019-03-31 02:10:02 -0400111 transmit(TopologyNode i, Block&& packet);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700112
113 void
Davide Pesavento284bd622019-03-31 02:10:02 -0400114 scheduleReceive(face::InternalTransportBase* recipient, Block&& packet);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700115
116private:
Davide Pesavento284bd622019-03-31 02:10:02 -0400117 bool m_isUp = true;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700118 time::nanoseconds m_delay;
Teng Liangf995f382017-04-04 22:09:39 +0000119
Davide Pesavento284bd622019-03-31 02:10:02 -0400120 class ReceiveProxy : public face::InternalTransportBase
Teng Liangf995f382017-04-04 22:09:39 +0000121 {
Davide Pesavento284bd622019-03-31 02:10:02 -0400122 public:
123 using Callback = std::function<void(Block&&)>;
124
125 explicit
126 ReceiveProxy(Callback cb)
127 : m_cb(std::move(cb))
128 {
129 }
130
131 void
132 receivePacket(Block&& packet) final
133 {
134 m_cb(std::move(packet));
135 }
136
137 private:
138 Callback m_cb;
139 };
140
141 class NodeTransport
142 {
143 public:
144 NodeTransport(shared_ptr<Face> face, ReceiveProxy::Callback receiveCallback);
145
146 public:
Teng Liangf995f382017-04-04 22:09:39 +0000147 shared_ptr<Face> face;
Davide Pesavento284bd622019-03-31 02:10:02 -0400148 face::InternalForwarderTransport* transport;
149 ReceiveProxy proxy;
Teng Liangf995f382017-04-04 22:09:39 +0000150 std::set<TopologyNode> blockedDestinations;
151 };
Davide Pesavento284bd622019-03-31 02:10:02 -0400152
Teng Liangf995f382017-04-04 22:09:39 +0000153 std::unordered_map<TopologyNode, NodeTransport> m_transports;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700154};
155
156/** \brief represents a link to a local application
157 */
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700158class TopologyAppLink : noncopyable
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700159{
160public:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700161 /** \brief constructor
162 * \param forwarderFace a Face with InternalForwarderTransport
163 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 explicit
Junxiao Shicde37ad2015-12-24 01:02:05 -0700165 TopologyAppLink(shared_ptr<Face> forwarderFace);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700166
167 /** \brief fail the link, cause packets to be dropped silently
168 */
169 void
170 fail();
171
172 /** \brief recover the link from a failure
173 */
174 void
175 recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700176
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700177 /** \return face on forwarder side
178 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700179 Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700180 getForwarderFace()
181 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700182 return *m_face;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700183 }
184
185 /** \return face on application side
186 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700187 ndn::Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700188 getClientFace()
189 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700190 return *m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700191 }
192
193private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700194 shared_ptr<Face> m_face;
195 face::InternalForwarderTransport* m_forwarderTransport;
196 shared_ptr<face::InternalClientTransport> m_clientTransport;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700197 shared_ptr<ndn::Face> m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700198};
199
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000200/** \brief captured packets on a face
201 */
202class TopologyPcap : noncopyable
203{
204public:
205 std::vector<Interest> sentInterests;
206 std::vector<Data> sentData;
207 std::vector<lp::Nack> sentNacks;
208};
209
210/** \brief captured packet timestamp tag
211 */
212using TopologyPcapTimestamp = ndn::SimpleTag<time::steady_clock::TimePoint, 0>;
213
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700214/** \brief builds a topology for forwarding tests
215 */
216class TopologyTester : noncopyable
217{
218public:
219 /** \brief creates a forwarder
220 * \return index of new forwarder
221 */
222 TopologyNode
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700223 addForwarder(const std::string& label);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700224
225 /** \return forwarder instance \p i
226 */
227 Forwarder&
228 getForwarder(TopologyNode i)
229 {
230 return *m_forwarders.at(i);
231 }
232
233 /** \brief sets strategy on forwarder \p i
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000234 * \tparam S the strategy type
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700235 * \note Test scenario can also access StrategyChoice table directly.
236 */
237 template<typename S>
238 void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500239 setStrategy(TopologyNode i, Name prefix = Name("ndn:/"),
240 Name instanceName = S::getStrategyName())
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700241 {
242 Forwarder& forwarder = this->getForwarder(i);
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500243 choose<S>(forwarder, prefix, instanceName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700244 }
245
246 /** \brief makes a link that interconnects two or more forwarders
Teng Liangf995f382017-04-04 22:09:39 +0000247 * \brief linkType desired link type; LINK_TYPE_NONE to use point-to-point for two forwarders
248 * and multi-access for more than two forwarders; it's an error to specify
249 * point-to-point when there are more than two forwarders
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700250 *
251 * A face is created on each of \p forwarders .
252 * When a packet is sent onto one of the faces on this link,
253 * this packet will be received by all other faces on this link after \p delay .
254 */
255 shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000256 addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700257 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000258 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259
260 /** \brief makes a link to local application
261 */
262 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700263 addAppFace(const std::string& label, TopologyNode i);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264
265 /** \brief makes a link to local application, and register a prefix
266 */
267 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700268 addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000270 /** \brief enables packet capture on every forwarder face
271 */
272 void
273 enablePcap(bool isEnabled = true);
274
275 /** \return captured packets on a forwarder face
276 * \pre enablePcap(true) is in effect when the face was created
277 */
278 TopologyPcap&
279 getPcap(const Face& face);
280
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700281 /** \brief registers a prefix on a forwarder face
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700282 */
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283 void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700284 registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700285
286 /** \brief creates a producer application that answers every Interest with Data of same Name
287 */
288 void
Saurab Dulala6dec222019-04-01 00:15:10 -0500289 addEchoProducer(ndn::Face& face, const Name& prefix = "/", time::nanoseconds replyDelay = 0_ns);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700290
291 /** \brief creates a consumer application that sends \p n Interests under \p prefix
292 * at \p interval fixed rate.
Teng Liangf995f382017-04-04 22:09:39 +0000293 * \param seq if non-negative, append sequence number instead of timestamp
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700294 */
295 void
Teng Liangf995f382017-04-04 22:09:39 +0000296 addIntervalConsumer(ndn::Face& face, const Name& prefix, time::nanoseconds interval,
297 size_t n, int seq = -1);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700298
299private:
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000300 bool m_wantPcap = false;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700301 std::vector<unique_ptr<Forwarder>> m_forwarders;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700302 std::vector<std::string> m_forwarderLabels;
303 std::vector<shared_ptr<TopologyLink>> m_links;
304 std::vector<shared_ptr<TopologyAppLink>> m_appLinks;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700305};
306
307} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800308} // namespace fw
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700309} // namespace nfd
310
Davide Pesavento97210d52016-10-14 15:45:48 +0200311#endif // NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP