A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
click-internet-stack-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Author: Faker Moatamri <faker.moatamri@sophia.inria.fr>
19 * Author: Lalith Suresh <suresh.lalith@gmail.com>
20 */
21
23
24#include "ns3/arp-l3-protocol.h"
25#include "ns3/assert.h"
26#include "ns3/callback.h"
27#include "ns3/config.h"
28#include "ns3/core-config.h"
29#include "ns3/ipv4-click-routing.h"
30#include "ns3/ipv4-l3-click-protocol.h"
31#include "ns3/ipv4.h"
32#include "ns3/log.h"
33#include "ns3/names.h"
34#include "ns3/net-device.h"
35#include "ns3/node.h"
36#include "ns3/object.h"
37#include "ns3/packet-socket-factory.h"
38#include "ns3/simulator.h"
39#include "ns3/string.h"
40#include "ns3/trace-helper.h"
41
42#include <limits>
43#include <map>
44
45namespace ns3
46{
47
48NS_LOG_COMPONENT_DEFINE("ClickInternetStackHelper");
49
50#define INTERFACE_CONTEXT
51
52typedef std::pair<Ptr<Ipv4>, uint32_t> InterfacePairIpv4;
53typedef std::map<InterfacePairIpv4, Ptr<PcapFileWrapper>> InterfaceFileMapIpv4;
54typedef std::map<InterfacePairIpv4, Ptr<OutputStreamWrapper>> InterfaceStreamMapIpv4;
55
57 g_interfaceFileMapIpv4; /**< A mapping of Ipv4/interface pairs to pcap files */
59 g_interfaceStreamMapIpv4; /**< A mapping of Ipv4/interface pairs to ascii streams */
60
61/**
62 * IPv4 Rx / Tx packet callback.
63 *
64 * \param p Packet.
65 * \param ipv4 IPv4 stack.
66 * \param interface Interface number.
67 */
68static void
70{
71 NS_LOG_FUNCTION(p << ipv4 << interface);
72
73 //
74 // Since trace sources are independent of interface, if we hook a source
75 // on a particular protocol we will get traces for all of its interfaces.
76 // We need to filter this to only report interfaces for which the user
77 // has expressed interest.
78 //
79 InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
80 if (g_interfaceFileMapIpv4.find(pair) == g_interfaceFileMapIpv4.end())
81 {
82 NS_LOG_INFO("Ignoring packet to/from interface " << interface);
83 return;
84 }
85
87 file->Write(Simulator::Now(), p);
88}
89
90/**
91 * Packet dropped callback without context.
92 *
93 * \param stream Output stream.
94 * \param header IPv4 header.
95 * \param packet Packet.
96 * \param reason Packet drop reason.
97 * \param ipv4 IPv4 stack.
98 * \param interface Interface number.
99 */
100static void
102 const Ipv4Header& header,
103 Ptr<const Packet> packet,
105 Ptr<Ipv4> ipv4,
106 uint32_t interface)
107{
108 //
109 // Since trace sources are independent of interface, if we hook a source
110 // on a particular protocol we will get traces for all of its interfaces.
111 // We need to filter this to only report interfaces for which the user
112 // has expressed interest.
113 //
114 InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
116 {
117 NS_LOG_INFO("Ignoring packet to/from interface " << interface);
118 return;
119 }
120
121 Ptr<Packet> p = packet->Copy();
122 p->AddHeader(header);
123 *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
124}
125
126/**
127 * Packet dropped callback with context.
128 *
129 * \param stream Output stream.
130 * \param context Context.
131 * \param header IPv4 header.
132 * \param packet Packet.
133 * \param reason Packet drop reason.
134 * \param ipv4 IPv4 stack.
135 * \param interface Interface number.
136 */
137static void
139 std::string context,
140 const Ipv4Header& header,
141 Ptr<const Packet> packet,
143 Ptr<Ipv4> ipv4,
144 uint32_t interface)
145{
146 //
147 // Since trace sources are independent of interface, if we hook a source
148 // on a particular protocol we will get traces for all of its interfaces.
149 // We need to filter this to only report interfaces for which the user
150 // has expressed interest.
151 //
152 InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
154 {
155 NS_LOG_INFO("Ignoring packet to/from interface " << interface);
156 return;
157 }
158
159 Ptr<Packet> p = packet->Copy();
160 p->AddHeader(header);
161#ifdef INTERFACE_CONTEXT
162 *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << context << "("
163 << interface << ") " << *p << std::endl;
164#else
165 *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << context << " " << *p
166 << std::endl;
167#endif
168}
169
171 : m_ipv4Enabled(true)
172{
173 Initialize();
174}
175
176void
178{
179}
180
182{
183}
184
186{
188}
189
192{
193 if (this != &o)
194 {
196 }
197 return *this;
198}
199
200void
202{
203 m_ipv4Enabled = true;
204 Initialize();
205}
206
207void
209{
210 for (auto i = c.Begin(); i != c.End(); ++i)
211 {
212 SetClickFile(*i, clickfile);
213 }
214}
215
216void
218{
219 m_nodeToClickFileMap.insert(std::make_pair(node, clickfile));
220}
221
222void
223ClickInternetStackHelper::SetDefines(NodeContainer c, std::map<std::string, std::string> defines)
224{
225 for (auto i = c.Begin(); i != c.End(); ++i)
226 {
227 SetDefines(*i, defines);
228 }
229}
230
231void
232ClickInternetStackHelper::SetDefines(Ptr<Node> node, std::map<std::string, std::string> defines)
233{
234 m_nodeToDefinesMap.insert(std::make_pair(node, defines));
235}
236
237void
239{
240 for (auto i = c.Begin(); i != c.End(); ++i)
241 {
243 }
244}
245
246void
248{
249 m_nodeToRoutingTableElementMap.insert(std::make_pair(node, rt));
250}
251
252void
254{
255 for (auto i = c.Begin(); i != c.End(); ++i)
256 {
257 Install(*i);
258 }
259}
260
261void
263{
265}
266
267void
269 const std::string typeId)
270{
271 ObjectFactory factory;
272 factory.SetTypeId(typeId);
273 Ptr<Object> protocol = factory.Create<Object>();
274 node->AggregateObject(protocol);
275}
276
277void
279{
280 if (m_ipv4Enabled)
281 {
282 if (node->GetObject<Ipv4>())
283 {
284 NS_FATAL_ERROR("ClickInternetStackHelper::Install (): Aggregating "
285 "an InternetStack to a node with an existing Ipv4 object");
286 return;
287 }
288
289 CreateAndAggregateObjectFromTypeId(node, "ns3::ArpL3Protocol");
290 CreateAndAggregateObjectFromTypeId(node, "ns3::Ipv4L3ClickProtocol");
291 CreateAndAggregateObjectFromTypeId(node, "ns3::Icmpv4L4Protocol");
292 CreateAndAggregateObjectFromTypeId(node, "ns3::UdpL4Protocol");
293 CreateAndAggregateObjectFromTypeId(node, "ns3::TcpL4Protocol");
294 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory>();
295 node->AggregateObject(factory);
296 // Set routing
297 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
298 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting>();
299 auto it = m_nodeToClickFileMap.find(node);
300
301 if (it != m_nodeToClickFileMap.end())
302 {
303 ipv4Routing->SetClickFile(it->second);
304 }
305
306 auto definesIt = m_nodeToDefinesMap.find(node);
307 if (definesIt != m_nodeToDefinesMap.end())
308 {
309 ipv4Routing->SetDefines(definesIt->second);
310 }
311
312 it = m_nodeToRoutingTableElementMap.find(node);
313 if (it != m_nodeToRoutingTableElementMap.end())
314 {
315 ipv4Routing->SetClickRoutingTableElement(it->second);
316 }
317 ipv4->SetRoutingProtocol(ipv4Routing);
318 node->AggregateObject(ipv4Routing);
319 }
320}
321
322void
323ClickInternetStackHelper::Install(std::string nodeName) const
324{
325 Ptr<Node> node = Names::Find<Node>(nodeName);
326 Install(node);
327}
328
329bool
331{
332 for (auto i = g_interfaceFileMapIpv4.begin(); i != g_interfaceFileMapIpv4.end(); ++i)
333 {
334 if ((*i).first.first == ipv4)
335 {
336 return true;
337 }
338 }
339 return false;
340}
341
342void
344 Ptr<Ipv4> ipv4,
345 uint32_t interface,
346 bool explicitFilename)
347{
348 NS_LOG_FUNCTION(prefix << ipv4 << interface);
349
350 if (!m_ipv4Enabled)
351 {
352 NS_LOG_INFO("Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
353 return;
354 }
355
356 //
357 // We have to create a file and a mapping from protocol/interface to file
358 // irrespective of how many times we want to trace a particular protocol.
359 //
360 PcapHelper pcapHelper;
361
362 std::string filename;
363 if (explicitFilename)
364 {
365 filename = prefix;
366 }
367 else
368 {
369 filename = pcapHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
370 }
371
372 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_RAW);
373
374 //
375 // However, we only hook the trace source once to avoid multiple trace sink
376 // calls per event (connect is independent of interface).
377 //
378 if (!PcapHooked(ipv4))
379 {
380 //
381 // Ptr<Ipv4> is aggregated to node and Ipv4L3Protocol is aggregated to
382 // node so we can get to Ipv4L3Protocol through Ipv4.
383 //
384 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
385 NS_ASSERT_MSG(ipv4L3Protocol,
386 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
387 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
388
389 bool result =
390 ipv4L3Protocol->TraceConnectWithoutContext("Tx", MakeCallback(&Ipv4L3ProtocolRxTxSink));
391 NS_ASSERT_MSG(result == true,
392 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
393 "Unable to connect ipv4L3Protocol \"Tx\"");
394
395 result =
396 ipv4L3Protocol->TraceConnectWithoutContext("Rx", MakeCallback(&Ipv4L3ProtocolRxTxSink));
397 NS_ASSERT_MSG(result == true,
398 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
399 "Unable to connect ipv4L3Protocol \"Rx\"");
400 }
401
402 g_interfaceFileMapIpv4[std::make_pair(ipv4, interface)] = file;
403}
404
405bool
407{
408 for (auto i = g_interfaceStreamMapIpv4.begin(); i != g_interfaceStreamMapIpv4.end(); ++i)
409 {
410 if ((*i).first.first == ipv4)
411 {
412 return true;
413 }
414 }
415 return false;
416}
417
418void
420 std::string prefix,
421 Ptr<Ipv4> ipv4,
422 uint32_t interface,
423 bool explicitFilename)
424{
425 if (!m_ipv4Enabled)
426 {
427 NS_LOG_INFO("Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
428 return;
429 }
430
431 //
432 // Our trace sinks are going to use packet printing, so we have to
433 // make sure that is turned on.
434 //
436
437 //
438 // If we are not provided an OutputStreamWrapper, we are expected to create
439 // one using the usual trace filename conventions and hook WithoutContext
440 // since there will be one file per context and therefore the context would
441 // be redundant.
442 //
443 if (!stream)
444 {
445 //
446 // Set up an output stream object to deal with private ofstream copy
447 // constructor and lifetime issues. Let the helper decide the actual
448 // name of the file given the prefix.
449 //
450 // We have to create a stream and a mapping from protocol/interface to
451 // stream irrespective of how many times we want to trace a particular
452 // protocol.
453 //
454 AsciiTraceHelper asciiTraceHelper;
455
456 std::string filename;
457 if (explicitFilename)
458 {
459 filename = prefix;
460 }
461 else
462 {
463 filename = asciiTraceHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
464 }
465
466 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
467
468 //
469 // However, we only hook the trace sources once to avoid multiple trace sink
470 // calls per event (connect is independent of interface).
471 //
472 if (!AsciiHooked(ipv4))
473 {
474 //
475 // We can use the default drop sink for the ArpL3Protocol since it has
476 // the usual signature. We can get to the Ptr<ArpL3Protocol> through
477 // our Ptr<Ipv4> since they must both be aggregated to the same node.
478 //
479 Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol>();
480 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol>(arpL3Protocol,
481 "Drop",
482 theStream);
483
484 //
485 // The drop sink for the Ipv4L3Protocol uses a different signature than
486 // the default sink, so we have to cook one up for ourselves. We can get
487 // to the Ptr<Ipv4L3Protocol> through our Ptr<Ipv4> since they must both
488 // be aggregated to the same node.
489 //
490 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
491 bool result = ipv4L3Protocol->TraceConnectWithoutContext(
492 "Drop",
494 NS_ASSERT_MSG(result == true,
495 "ClickInternetStackHelper::EnableAsciiIpv4Internal(): "
496 "Unable to connect ipv4L3Protocol \"Drop\"");
497 }
498
499 g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = theStream;
500 return;
501 }
502
503 //
504 // If we are provided an OutputStreamWrapper, we are expected to use it, and
505 // to provide a context. We are free to come up with our own context if we
506 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
507 // compatibility and simplicity, we just use Config::Connect and let it deal
508 // with the context.
509 //
510 // We need to associate the ipv4/interface with a stream to express interest
511 // in tracing events on that pair, however, we only hook the trace sources
512 // once to avoid multiple trace sink calls per event (connect is independent
513 // of interface).
514 //
515 if (!AsciiHooked(ipv4))
516 {
517 Ptr<Node> node = ipv4->GetObject<Node>();
518 std::ostringstream oss;
519
520 //
521 // For the ARP Drop, we are going to use the default trace sink provided by
522 // the ascii trace helper. There is actually no AsciiTraceHelper in sight
523 // here, but the default trace sinks are actually publicly available static
524 // functions that are always there waiting for just such a case.
525 //
526 oss << "/NodeList/" << node->GetId() << "/$ns3::ArpL3Protocol/Drop";
527 Config::Connect(oss.str(),
529
530 //
531 // This has all kinds of parameters coming with, so we have to cook up our
532 // own sink.
533 //
534 oss.str("");
535 oss << "/NodeList/" << node->GetId() << "/$ns3::Ipv4L3Protocol/Drop";
537 }
538
539 g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = stream;
540}
541
542} // namespace ns3
An implementation of the ARP protocol.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
void HookDefaultDropSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default drop operation trace sink that does not accept nor log a trace con...
Definition: trace-helper.h:534
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
std::string GetFilenameFromInterfacePair(std::string prefix, Ptr< Object > object, uint32_t interface, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
aggregate Click/IP/TCP/UDP functionality to existing Nodes.
std::map< Ptr< Node >, std::string > m_nodeToClickFileMap
Node to Click file mapping.
~ClickInternetStackHelper() override
Destroy the ClickInternetStackHelper.
ClickInternetStackHelper & operator=(const ClickInternetStackHelper &o)
Assignment operator.
bool PcapHooked(Ptr< Ipv4 > ipv4)
Check if PCAP is hooked.
void SetDefines(NodeContainer c, std::map< std::string, std::string > defines)
Set defines to be used for a group of nodes.
static void CreateAndAggregateObjectFromTypeId(Ptr< Node > node, const std::string typeId)
Create and aggregate object from type ID.
std::map< Ptr< Node >, std::string > m_nodeToRoutingTableElementMap
Node to Routing Table Element mapping.
void Initialize()
Initialize stack helper.
bool m_ipv4Enabled
IPv4 install state (enabled/disabled) ?
void SetClickFile(NodeContainer c, std::string clickfile)
Set a Click file to be used for a group of nodes.
bool AsciiHooked(Ptr< Ipv4 > ipv4)
Check if ASCII is hooked.
ClickInternetStackHelper()
Create a new ClickInternetStackHelper which uses Ipv4ClickRouting for routing.
void SetRoutingTableElement(NodeContainer c, std::string rt)
Set a Click routing table element for a group of nodes.
void InstallAll() const
Aggregate IPv4, UDP, and TCP stacks to all nodes in the simulation.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4L3ClickProtocol, ns3::ArpL3Protocol, ns3::Udp,...
void EnablePcapIpv4Internal(std::string prefix, Ptr< Ipv4 > ipv4, uint32_t interface, bool explicitFilename) override
Enable pcap output the indicated Ipv4 and interface pair.
void Reset()
Return helper internal state to that of a newly constructed one.
std::map< Ptr< Node >, std::map< std::string, std::string > > m_nodeToDefinesMap
Node to Click defines mapping.
void EnableAsciiIpv4Internal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< Ipv4 > ipv4, uint32_t interface, bool explicitFilename) override
Enable ascii trace output on the indicated Ipv4 and interface pair.
Packet header for IPv4.
Definition: ipv4-header.h:34
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
Implement the IPv4 layer.
DropReason
Reason why a packet has been dropped.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
static NodeContainer GetGlobal()
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
A network Node.
Definition: node.h:57
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:322
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
A base class which provides memory management and object aggregation.
Definition: object.h:89
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
Manage pcap files for device models.
Definition: trace-helper.h:40
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, DataLinkType dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
std::string GetFilenameFromInterfacePair(std::string prefix, Ptr< Object > object, uint32_t interface, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for the pcap file associated with a node.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:767
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::pair< Ptr< Ipv4 >, uint32_t > InterfacePairIpv4
Ipv4/interface pair.
std::map< InterfacePairIpv4, Ptr< OutputStreamWrapper > > InterfaceStreamMapIpv4
Ipv4/interface and output stream container.
static void Ipv4L3ProtocolDropSinkWithContext(Ptr< OutputStreamWrapper > stream, std::string context, const Ipv4Header &header, Ptr< const Packet > packet, Ipv4L3Protocol::DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
Packet dropped callback with context.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:706
std::map< InterfacePairIpv4, Ptr< PcapFileWrapper > > InterfaceFileMapIpv4
Ipv4/interface and Pcap file wrapper container.
static void Ipv4L3ProtocolRxTxSink(Ptr< const Packet > p, Ptr< Ipv4 > ipv4, uint32_t interface)
IPv4 Rx / Tx packet callback.
static InterfaceFileMapIpv4 g_interfaceFileMapIpv4
A mapping of Ipv4/interface pairs to pcap files.
static InterfaceStreamMapIpv4 g_interfaceStreamMapIpv4
A mapping of Ipv4/interface pairs to ascii streams.
static void Ipv4L3ProtocolDropSinkWithoutContext(Ptr< OutputStreamWrapper > stream, const Ipv4Header &header, Ptr< const Packet > packet, Ipv4L3Protocol::DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
Packet dropped callback without context.