26 #include "ns3/arp-l3-protocol.h"
27 #include "ns3/assert.h"
28 #include "ns3/callback.h"
29 #include "ns3/config.h"
30 #include "ns3/core-config.h"
31 #include "ns3/ipv4-click-routing.h"
32 #include "ns3/ipv4-l3-click-protocol.h"
35 #include "ns3/names.h"
36 #include "ns3/net-device.h"
38 #include "ns3/object.h"
39 #include "ns3/packet-socket-factory.h"
40 #include "ns3/simulator.h"
41 #include "ns3/string.h"
42 #include "ns3/trace-helper.h"
52 #define INTERFACE_CONTEXT
63 ClickInternetStackHelper::ClickInternetStackHelper()
71 ClickInternetStackHelper::Initialize()
73 SetTcp(
"ns3::TcpL4Protocol");
76 ClickInternetStackHelper::~ClickInternetStackHelper()
80 ClickInternetStackHelper::ClickInternetStackHelper(
const ClickInternetStackHelper& o)
82 m_ipv4Enabled = o.m_ipv4Enabled;
83 m_tcpFactory = o.m_tcpFactory;
86 ClickInternetStackHelper&
87 ClickInternetStackHelper::operator=(
const ClickInternetStackHelper& o)
104 ClickInternetStackHelper::SetTcp(
const std::string tid)
106 m_tcpFactory.SetTypeId(tid);
110 ClickInternetStackHelper::SetTcp(std::string tid, std::string n0,
const AttributeValue& v0)
112 m_tcpFactory.SetTypeId(tid);
113 m_tcpFactory.Set(n0, v0);
117 ClickInternetStackHelper::SetClickFile(NodeContainer c, std::string clickfile)
119 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
121 SetClickFile(*i, clickfile);
126 ClickInternetStackHelper::SetClickFile(Ptr<Node> node, std::string clickfile)
128 m_nodeToClickFileMap.insert(std::make_pair(node, clickfile));
132 ClickInternetStackHelper::SetDefines(NodeContainer c, std::map<std::string, std::string> defines)
134 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
136 SetDefines(*i, defines);
141 ClickInternetStackHelper::SetDefines(Ptr<Node> node, std::map<std::string, std::string> defines)
143 m_nodeToDefinesMap.insert(std::make_pair(node, defines));
147 ClickInternetStackHelper::SetRoutingTableElement(NodeContainer c, std::string rt)
149 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
151 SetRoutingTableElement(*i, rt);
156 ClickInternetStackHelper::SetRoutingTableElement(Ptr<Node> node, std::string rt)
158 m_nodeToRoutingTableElementMap.insert(std::make_pair(node, rt));
162 ClickInternetStackHelper::Install(NodeContainer c)
const
164 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
171 ClickInternetStackHelper::InstallAll()
const
173 Install(NodeContainer::GetGlobal());
177 ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId(Ptr<Node> node,
178 const std::string typeId)
180 ObjectFactory factory;
181 factory.SetTypeId(typeId);
182 Ptr<Object> protocol = factory.Create<Object>();
183 node->AggregateObject(protocol);
187 ClickInternetStackHelper::Install(Ptr<Node> node)
const
191 if (node->GetObject<Ipv4>())
193 NS_FATAL_ERROR(
"ClickInternetStackHelper::Install (): Aggregating "
194 "an InternetStack to a node with an existing Ipv4 object");
198 CreateAndAggregateObjectFromTypeId(node,
"ns3::ArpL3Protocol");
199 CreateAndAggregateObjectFromTypeId(node,
"ns3::Ipv4L3ClickProtocol");
200 CreateAndAggregateObjectFromTypeId(node,
"ns3::Icmpv4L4Protocol");
201 CreateAndAggregateObjectFromTypeId(node,
"ns3::UdpL4Protocol");
202 node->AggregateObject(m_tcpFactory.Create<Object>());
203 Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory>();
204 node->AggregateObject(factory);
206 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
207 Ptr<Ipv4ClickRouting> ipv4Routing = CreateObject<Ipv4ClickRouting>();
208 std::map<Ptr<Node>, std::string>::const_iterator it;
209 it = m_nodeToClickFileMap.find(node);
211 if (it != m_nodeToClickFileMap.end())
213 ipv4Routing->SetClickFile(it->second);
216 std::map<Ptr<Node>, std::map<std::string, std::string>>::const_iterator definesIt;
217 definesIt = m_nodeToDefinesMap.find(node);
218 if (definesIt != m_nodeToDefinesMap.end())
220 ipv4Routing->SetDefines(definesIt->second);
223 it = m_nodeToRoutingTableElementMap.find(node);
224 if (it != m_nodeToRoutingTableElementMap.end())
226 ipv4Routing->SetClickRoutingTableElement(it->second);
228 ipv4->SetRoutingProtocol(ipv4Routing);
229 node->AggregateObject(ipv4Routing);
234 ClickInternetStackHelper::Install(std::string nodeName)
const
236 Ptr<Node> node = Names::Find<Node>(nodeName);
254 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
263 ClickInternetStackHelper::PcapHooked(Ptr<Ipv4> ipv4)
269 if ((*i).first.first == ipv4)
278 ClickInternetStackHelper::EnablePcapIpv4Internal(std::string prefix,
281 bool explicitFilename)
287 NS_LOG_INFO(
"Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
295 PcapHelper pcapHelper;
297 std::string filename;
298 if (explicitFilename)
304 filename = pcapHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
307 Ptr<PcapFileWrapper>
file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_RAW);
313 if (!PcapHooked(ipv4))
319 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
321 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
322 "m_ipv4Enabled and ipv4L3Protocol inconsistent");
327 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
328 "Unable to connect ipv4L3Protocol \"Tx\"");
333 "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
334 "Unable to connect ipv4L3Protocol \"Rx\"");
342 const Ipv4Header& header,
343 Ptr<const Packet> packet,
344 Ipv4L3Protocol::DropReason reason,
357 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
361 Ptr<Packet> p = packet->Copy();
362 p->AddHeader(header);
369 const Ipv4Header& header,
370 Ptr<const Packet> packet,
371 Ipv4L3Protocol::DropReason reason,
384 NS_LOG_INFO(
"Ignoring packet to/from interface " << interface);
388 Ptr<Packet> p = packet->Copy();
389 p->AddHeader(header);
390 #ifdef INTERFACE_CONTEXT
392 <<
interface << ") " << *p << std::endl;
400 ClickInternetStackHelper::AsciiHooked(Ptr<Ipv4> ipv4)
406 if ((*i).first.first == ipv4)
415 ClickInternetStackHelper::EnableAsciiIpv4Internal(Ptr<OutputStreamWrapper> stream,
419 bool explicitFilename)
423 NS_LOG_INFO(
"Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
431 Packet::EnablePrinting();
450 AsciiTraceHelper asciiTraceHelper;
452 std::string filename;
453 if (explicitFilename)
459 filename = asciiTraceHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
462 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
468 if (!AsciiHooked(ipv4))
475 Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol>();
476 asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol>(arpL3Protocol,
486 Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
487 bool result = ipv4L3Protocol->TraceConnectWithoutContext(
491 "ClickInternetStackHelper::EanableAsciiIpv4Internal(): "
492 "Unable to connect ipv4L3Protocol \"Drop\"");
511 if (!AsciiHooked(ipv4))
513 Ptr<Node> node = ipv4->GetObject<Node>();
514 std::ostringstream oss;
522 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::ArpL3Protocol/Drop";
531 oss <<
"/NodeList/" << node->GetId() <<
"/$ns3::Ipv4L3Protocol/Drop";
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
void Reset()
Reset the initial value of every attribute as well as the value of every global to what they were bef...
void Connect(std::string path, const CallbackBase &cb)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#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.
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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)
Sync function for IPv4 dropped packet - Ascii output.
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...
std::map< InterfacePairIpv4, Ptr< OutputStreamWrapper > > InterfaceStreamMapIpv4
Ipv4/interface and output stream container.
static void Ipv4L3ProtocolRxTxSink(Ptr< const Packet > p, Ptr< Ipv4 > ipv4, uint32_t interface)
Sync function for IPv4 packet - Pcap output.
std::map< InterfacePairIpv4, Ptr< PcapFileWrapper > > InterfaceFileMapIpv4
Ipv4/interface and Pcap file wrapper container.
std::pair< uint32_t, uint32_t > InterfacePairIpv4
Ipv4/interface pair.
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)
Sync function for IPv4 dropped packet - Ascii output.