/
topology-tester.hpp
370 lines (314 loc) · 9.57 KB
/
topology-tester.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* \brief Allows testing forwarding in a network topology.
*/
#ifndef NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
#define NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
#include "face/internal-transport.hpp"
#include "face/face.hpp"
#include "fw/strategy.hpp"
#include "choose-strategy.hpp"
#include "tests/test-common.hpp"
#include <ndn-cxx/face.hpp>
#include <unordered_map>
namespace nfd::tests {
/** \brief Identifies a node (forwarder) in the topology.
*/
typedef size_t TopologyNode;
/** \brief Represents a network link in the topology which connects two or more nodes.
*/
class TopologyLink : noncopyable
{
public:
explicit
TopologyLink(time::nanoseconds delay);
/** \brief Fail the link, cause packets to be dropped silently.
*/
void
fail()
{
m_isUp = false;
}
/** \brief Recover the link from a failure.
*/
void
recover()
{
m_isUp = true;
}
/** \brief Block transmission from i to j.
*
* Packets transmitted by i would not be delivered to j. Packets from j to i are unaffected.
* This can be used to simulate a wireless channel.
*/
void
block(TopologyNode i, TopologyNode j);
/** \brief Unblock transmission from i to j.
*/
void
unblock(TopologyNode i, TopologyNode j);
/** \brief Change the link delay.
* \param delay link delay, must be positive
*/
void
setDelay(time::nanoseconds delay);
/** \brief Attach a face to the link.
* \param i forwarder index
* \param face a Face with InternalForwarderTransport
*/
void
addFace(TopologyNode i, shared_ptr<Face> face);
/** \return a face of forwarder \p i which is attached to this link
*/
Face&
getFace(TopologyNode i)
{
return *m_transports.at(i).face;
}
private:
void
transmit(TopologyNode i, const Block& packet);
private:
bool m_isUp = true;
time::nanoseconds m_delay;
class ReceiveProxy final : public face::InternalTransportBase
{
public:
using Callback = std::function<void(const Block&)>;
explicit
ReceiveProxy(Callback cb)
: m_cb(std::move(cb))
{
}
void
receivePacket(const Block& packet) final
{
m_cb(packet);
}
private:
Callback m_cb;
};
class NodeTransport
{
public:
NodeTransport(shared_ptr<Face> face, ReceiveProxy::Callback receiveCallback);
public:
shared_ptr<Face> face;
face::InternalForwarderTransport* transport;
ReceiveProxy proxy;
std::set<TopologyNode> blockedDestinations;
};
std::unordered_map<TopologyNode, NodeTransport> m_transports;
};
/** \brief Represents a link on a single forwarder.
*/
class TopologySingleLink : noncopyable
{
public:
/** \brief Constructor.
* \param forwarderFace a Face with InternalForwarderTransport
*/
explicit
TopologySingleLink(shared_ptr<Face> forwarderFace);
/** \return face on forwarder side
*/
Face&
getForwarderFace()
{
return *m_face;
}
protected:
shared_ptr<Face> m_face;
face::InternalForwarderTransport* m_forwarderTransport;
};
/** \brief Represents a link to a local application.
*/
class TopologyAppLink : public TopologySingleLink
{
public:
/** \brief Constructor.
* \param forwarderFace a Face with InternalForwarderTransport
*/
explicit
TopologyAppLink(shared_ptr<Face> forwarderFace);
/** \brief Fail the link, cause packets to be dropped silently.
*/
void
fail();
/** \brief Recover the link from a failure.
*/
void
recover();
/** \return face on application side
*/
ndn::Face&
getClientFace()
{
return *m_client;
}
private:
shared_ptr<face::InternalClientTransport> m_clientTransport;
shared_ptr<ndn::Face> m_client;
};
/** \brief Allows the test case to inject and observe L2 packets on a link.
*/
class TopologyBareLink : public TopologySingleLink
{
public:
/** \brief Constructor.
* \param forwarderFace a Face with InternalForwarderTransport
*/
explicit
TopologyBareLink(shared_ptr<Face> forwarderFace);
void
receivePacket(const Block& packet);
public:
std::vector<Block> sentPackets;
private:
class Observer;
unique_ptr<Observer> m_observer;
};
/** \brief Captured packets on a face.
*/
class TopologyPcap : noncopyable
{
public:
std::vector<Interest> sentInterests;
std::vector<Data> sentData;
std::vector<lp::Nack> sentNacks;
};
/** \brief Captured packet timestamp tag.
*/
using TopologyPcapTimestamp = ndn::SimpleTag<time::steady_clock::time_point, 0>;
/** \brief Builds a topology for forwarding tests.
*/
class TopologyTester : noncopyable
{
public:
/** \brief Creates a forwarder.
* \return index of new forwarder
*/
TopologyNode
addForwarder(const std::string& label);
/** \return forwarder instance \p i
*/
Forwarder&
getForwarder(TopologyNode i)
{
return m_forwarders.at(i)->forwarder;
}
/** \brief Sets strategy on forwarder \p i.
* \tparam S the strategy type
* \note Test scenario can also access StrategyChoice table directly.
*/
template<typename S>
void
setStrategy(TopologyNode i, Name prefix = Name("ndn:/"),
Name instanceName = S::getStrategyName())
{
Forwarder& forwarder = this->getForwarder(i);
choose<S>(forwarder, prefix, instanceName);
}
/** \brief Makes a link that interconnects two or more forwarders.
* \param linkType Desired link type; LINK_TYPE_NONE to use point-to-point for two forwarders
* and multi-access for more than two forwarders; it's an error to specify
* point-to-point when there are more than two forwarders.
*
* A face is created on each of \p forwarders.
* When a packet is sent onto one of the faces on this link, the packet will be
* received by all other faces on this link after the specified delay.
*/
shared_ptr<TopologyLink>
addLink(const std::string& label, time::nanoseconds delay,
std::initializer_list<TopologyNode> forwarders,
ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE);
/** \brief Makes a link to a local application.
*/
shared_ptr<TopologyAppLink>
addAppFace(const std::string& label, TopologyNode i);
/** \brief Makes a link to a local application and registers a prefix.
*/
shared_ptr<TopologyAppLink>
addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost = 0);
/** \brief Makes a link that allows the test case to inject and observe layer-2 packets.
*/
shared_ptr<TopologyBareLink>
addBareLink(const std::string& label, TopologyNode i,
ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_LOCAL,
ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT);
/** \brief Enables packet capture on every forwarder face.
*/
void
enablePcap(bool isEnabled = true)
{
m_wantPcap = isEnabled;
}
/** \return captured packets on a forwarder face
* \pre enablePcap(true) is in effect when the face was created
*/
TopologyPcap&
getPcap(const Face& face);
/** \brief Registers a prefix on a forwarder face.
*/
void
registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost = 0);
/** \brief Creates a producer application that answers every Interest with Data of same Name.
*/
void
addEchoProducer(ndn::Face& face, const Name& prefix = "/", time::nanoseconds replyDelay = 0_ns);
/** \brief Creates a consumer application that sends \p n Interests under \p prefix
* at \p interval fixed rate.
* \param seq if non-negative, append sequence number instead of timestamp
*/
void
addIntervalConsumer(ndn::Face& face, const Name& prefix, time::nanoseconds interval,
size_t n, int seq = -1);
private:
shared_ptr<Face>
makeFace(TopologyNode i, const FaceUri& localUri, const FaceUri& remoteUri,
ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType);
private:
class TopologyForwarder
{
public:
explicit
TopologyForwarder(const std::string& label)
: label(label)
{
}
public:
std::string label;
FaceTable faceTable;
Forwarder forwarder{faceTable};
};
std::vector<unique_ptr<TopologyForwarder>> m_forwarders;
std::vector<shared_ptr<TopologyLink>> m_links;
std::vector<shared_ptr<TopologyAppLink>> m_appLinks;
std::vector<shared_ptr<TopologyBareLink>> m_bareLinks;
bool m_wantPcap = false;
};
} // namespace nfd::tests
#endif // NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP