A Discrete-Event Network Simulator
API
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 
22 #ifdef NS3_CLICK
23 
25 
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"
33 #include "ns3/ipv4.h"
34 #include "ns3/log.h"
35 #include "ns3/names.h"
36 #include "ns3/net-device.h"
37 #include "ns3/node.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"
43 
44 #include <limits>
45 #include <map>
46 
47 namespace ns3
48 {
49 
50 NS_LOG_COMPONENT_DEFINE("ClickInternetStackHelper");
51 
52 #define INTERFACE_CONTEXT
53 
54 typedef std::pair<Ptr<Ipv4>, uint32_t> InterfacePairIpv4;
55 typedef std::map<InterfacePairIpv4, Ptr<PcapFileWrapper>> InterfaceFileMapIpv4;
56 typedef std::map<InterfacePairIpv4, Ptr<OutputStreamWrapper>> InterfaceStreamMapIpv4;
57 
63 ClickInternetStackHelper::ClickInternetStackHelper()
64  : m_ipv4Enabled(true)
65 {
66  Initialize();
67 }
68 
69 // private method called by both constructor and Reset ()
70 void
71 ClickInternetStackHelper::Initialize()
72 {
73  SetTcp("ns3::TcpL4Protocol");
74 }
75 
76 ClickInternetStackHelper::~ClickInternetStackHelper()
77 {
78 }
79 
80 ClickInternetStackHelper::ClickInternetStackHelper(const ClickInternetStackHelper& o)
81 {
82  m_ipv4Enabled = o.m_ipv4Enabled;
83  m_tcpFactory = o.m_tcpFactory;
84 }
85 
86 ClickInternetStackHelper&
87 ClickInternetStackHelper::operator=(const ClickInternetStackHelper& o)
88 {
89  if (this == &o)
90  {
91  return *this;
92  }
93  return *this;
94 }
95 
96 void
98 {
99  m_ipv4Enabled = true;
100  Initialize();
101 }
102 
103 void
104 ClickInternetStackHelper::SetTcp(const std::string tid)
105 {
106  m_tcpFactory.SetTypeId(tid);
107 }
108 
109 void
110 ClickInternetStackHelper::SetTcp(std::string tid, std::string n0, const AttributeValue& v0)
111 {
112  m_tcpFactory.SetTypeId(tid);
113  m_tcpFactory.Set(n0, v0);
114 }
115 
116 void
117 ClickInternetStackHelper::SetClickFile(NodeContainer c, std::string clickfile)
118 {
119  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
120  {
121  SetClickFile(*i, clickfile);
122  }
123 }
124 
125 void
126 ClickInternetStackHelper::SetClickFile(Ptr<Node> node, std::string clickfile)
127 {
128  m_nodeToClickFileMap.insert(std::make_pair(node, clickfile));
129 }
130 
131 void
132 ClickInternetStackHelper::SetDefines(NodeContainer c, std::map<std::string, std::string> defines)
133 {
134  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
135  {
136  SetDefines(*i, defines);
137  }
138 }
139 
140 void
141 ClickInternetStackHelper::SetDefines(Ptr<Node> node, std::map<std::string, std::string> defines)
142 {
143  m_nodeToDefinesMap.insert(std::make_pair(node, defines));
144 }
145 
146 void
147 ClickInternetStackHelper::SetRoutingTableElement(NodeContainer c, std::string rt)
148 {
149  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
150  {
151  SetRoutingTableElement(*i, rt);
152  }
153 }
154 
155 void
156 ClickInternetStackHelper::SetRoutingTableElement(Ptr<Node> node, std::string rt)
157 {
158  m_nodeToRoutingTableElementMap.insert(std::make_pair(node, rt));
159 }
160 
161 void
162 ClickInternetStackHelper::Install(NodeContainer c) const
163 {
164  for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i)
165  {
166  Install(*i);
167  }
168 }
169 
170 void
171 ClickInternetStackHelper::InstallAll() const
172 {
173  Install(NodeContainer::GetGlobal());
174 }
175 
176 void
177 ClickInternetStackHelper::CreateAndAggregateObjectFromTypeId(Ptr<Node> node,
178  const std::string typeId)
179 {
180  ObjectFactory factory;
181  factory.SetTypeId(typeId);
182  Ptr<Object> protocol = factory.Create<Object>();
183  node->AggregateObject(protocol);
184 }
185 
186 void
187 ClickInternetStackHelper::Install(Ptr<Node> node) const
188 {
189  if (m_ipv4Enabled)
190  {
191  if (node->GetObject<Ipv4>())
192  {
193  NS_FATAL_ERROR("ClickInternetStackHelper::Install (): Aggregating "
194  "an InternetStack to a node with an existing Ipv4 object");
195  return;
196  }
197 
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);
205  // Set routing
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);
210 
211  if (it != m_nodeToClickFileMap.end())
212  {
213  ipv4Routing->SetClickFile(it->second);
214  }
215 
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())
219  {
220  ipv4Routing->SetDefines(definesIt->second);
221  }
222 
223  it = m_nodeToRoutingTableElementMap.find(node);
224  if (it != m_nodeToRoutingTableElementMap.end())
225  {
226  ipv4Routing->SetClickRoutingTableElement(it->second);
227  }
228  ipv4->SetRoutingProtocol(ipv4Routing);
229  node->AggregateObject(ipv4Routing);
230  }
231 }
232 
233 void
234 ClickInternetStackHelper::Install(std::string nodeName) const
235 {
236  Ptr<Node> node = Names::Find<Node>(nodeName);
237  Install(node);
238 }
239 
240 static void
241 Ipv4L3ProtocolRxTxSink(Ptr<const Packet> p, Ptr<Ipv4> ipv4, uint32_t interface)
242 {
243  NS_LOG_FUNCTION(p << ipv4 << interface);
244 
245  //
246  // Since trace sources are independent of interface, if we hook a source
247  // on a particular protocol we will get traces for all of its interfaces.
248  // We need to filter this to only report interfaces for which the user
249  // has expressed interest.
250  //
251  InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
252  if (g_interfaceFileMapIpv4.find(pair) == g_interfaceFileMapIpv4.end())
253  {
254  NS_LOG_INFO("Ignoring packet to/from interface " << interface);
255  return;
256  }
257 
258  Ptr<PcapFileWrapper> file = g_interfaceFileMapIpv4[pair];
259  file->Write(Simulator::Now(), p);
260 }
261 
262 bool
263 ClickInternetStackHelper::PcapHooked(Ptr<Ipv4> ipv4)
264 {
265  for (InterfaceFileMapIpv4::const_iterator i = g_interfaceFileMapIpv4.begin();
266  i != g_interfaceFileMapIpv4.end();
267  ++i)
268  {
269  if ((*i).first.first == ipv4)
270  {
271  return true;
272  }
273  }
274  return false;
275 }
276 
277 void
278 ClickInternetStackHelper::EnablePcapIpv4Internal(std::string prefix,
279  Ptr<Ipv4> ipv4,
280  uint32_t interface,
281  bool explicitFilename)
282 {
283  NS_LOG_FUNCTION(prefix << ipv4 << interface);
284 
285  if (!m_ipv4Enabled)
286  {
287  NS_LOG_INFO("Call to enable Ipv4 pcap tracing but Ipv4 not enabled");
288  return;
289  }
290 
291  //
292  // We have to create a file and a mapping from protocol/interface to file
293  // irrespective of how many times we want to trace a particular protocol.
294  //
295  PcapHelper pcapHelper;
296 
297  std::string filename;
298  if (explicitFilename)
299  {
300  filename = prefix;
301  }
302  else
303  {
304  filename = pcapHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
305  }
306 
307  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_RAW);
308 
309  //
310  // However, we only hook the trace source once to avoid multiple trace sink
311  // calls per event (connect is independent of interface).
312  //
313  if (!PcapHooked(ipv4))
314  {
315  //
316  // Ptr<Ipv4> is aggregated to node and Ipv4L3Protocol is aggregated to
317  // node so we can get to Ipv4L3Protocol through Ipv4.
318  //
319  Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
320  NS_ASSERT_MSG(ipv4L3Protocol,
321  "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
322  "m_ipv4Enabled and ipv4L3Protocol inconsistent");
323 
324  bool result =
325  ipv4L3Protocol->TraceConnectWithoutContext("Tx", MakeCallback(&Ipv4L3ProtocolRxTxSink));
326  NS_ASSERT_MSG(result == true,
327  "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
328  "Unable to connect ipv4L3Protocol \"Tx\"");
329 
330  result =
331  ipv4L3Protocol->TraceConnectWithoutContext("Rx", MakeCallback(&Ipv4L3ProtocolRxTxSink));
332  NS_ASSERT_MSG(result == true,
333  "ClickInternetStackHelper::EnablePcapIpv4Internal(): "
334  "Unable to connect ipv4L3Protocol \"Rx\"");
335  }
336 
337  g_interfaceFileMapIpv4[std::make_pair(ipv4, interface)] = file;
338 }
339 
340 static void
341 Ipv4L3ProtocolDropSinkWithoutContext(Ptr<OutputStreamWrapper> stream,
342  const Ipv4Header& header,
343  Ptr<const Packet> packet,
344  Ipv4L3Protocol::DropReason reason,
345  Ptr<Ipv4> ipv4,
346  uint32_t interface)
347 {
348  //
349  // Since trace sources are independent of interface, if we hook a source
350  // on a particular protocol we will get traces for all of its interfaces.
351  // We need to filter this to only report interfaces for which the user
352  // has expressed interest.
353  //
354  InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
355  if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
356  {
357  NS_LOG_INFO("Ignoring packet to/from interface " << interface);
358  return;
359  }
360 
361  Ptr<Packet> p = packet->Copy();
362  p->AddHeader(header);
363  *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << *p << std::endl;
364 }
365 
366 static void
367 Ipv4L3ProtocolDropSinkWithContext(Ptr<OutputStreamWrapper> stream,
368  std::string context,
369  const Ipv4Header& header,
370  Ptr<const Packet> packet,
371  Ipv4L3Protocol::DropReason reason,
372  Ptr<Ipv4> ipv4,
373  uint32_t interface)
374 {
375  //
376  // Since trace sources are independent of interface, if we hook a source
377  // on a particular protocol we will get traces for all of its interfaces.
378  // We need to filter this to only report interfaces for which the user
379  // has expressed interest.
380  //
381  InterfacePairIpv4 pair = std::make_pair(ipv4, interface);
382  if (g_interfaceStreamMapIpv4.find(pair) == g_interfaceStreamMapIpv4.end())
383  {
384  NS_LOG_INFO("Ignoring packet to/from interface " << interface);
385  return;
386  }
387 
388  Ptr<Packet> p = packet->Copy();
389  p->AddHeader(header);
390 #ifdef INTERFACE_CONTEXT
391  *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << context << "("
392  << interface << ") " << *p << std::endl;
393 #else
394  *stream->GetStream() << "d " << Simulator::Now().GetSeconds() << " " << context << " " << *p
395  << std::endl;
396 #endif
397 }
398 
399 bool
400 ClickInternetStackHelper::AsciiHooked(Ptr<Ipv4> ipv4)
401 {
402  for (InterfaceStreamMapIpv4::const_iterator i = g_interfaceStreamMapIpv4.begin();
403  i != g_interfaceStreamMapIpv4.end();
404  ++i)
405  {
406  if ((*i).first.first == ipv4)
407  {
408  return true;
409  }
410  }
411  return false;
412 }
413 
414 void
415 ClickInternetStackHelper::EnableAsciiIpv4Internal(Ptr<OutputStreamWrapper> stream,
416  std::string prefix,
417  Ptr<Ipv4> ipv4,
418  uint32_t interface,
419  bool explicitFilename)
420 {
421  if (!m_ipv4Enabled)
422  {
423  NS_LOG_INFO("Call to enable Ipv4 ascii tracing but Ipv4 not enabled");
424  return;
425  }
426 
427  //
428  // Our trace sinks are going to use packet printing, so we have to
429  // make sure that is turned on.
430  //
431  Packet::EnablePrinting();
432 
433  //
434  // If we are not provided an OutputStreamWrapper, we are expected to create
435  // one using the usual trace filename conventions and hook WithoutContext
436  // since there will be one file per context and therefore the context would
437  // be redundant.
438  //
439  if (!stream)
440  {
441  //
442  // Set up an output stream object to deal with private ofstream copy
443  // constructor and lifetime issues. Let the helper decide the actual
444  // name of the file given the prefix.
445  //
446  // We have to create a stream and a mapping from protocol/interface to
447  // stream irrespective of how many times we want to trace a particular
448  // protocol.
449  //
450  AsciiTraceHelper asciiTraceHelper;
451 
452  std::string filename;
453  if (explicitFilename)
454  {
455  filename = prefix;
456  }
457  else
458  {
459  filename = asciiTraceHelper.GetFilenameFromInterfacePair(prefix, ipv4, interface);
460  }
461 
462  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
463 
464  //
465  // However, we only hook the trace sources once to avoid multiple trace sink
466  // calls per event (connect is independent of interface).
467  //
468  if (!AsciiHooked(ipv4))
469  {
470  //
471  // We can use the default drop sink for the ArpL3Protocol since it has
472  // the usual signature. We can get to the Ptr<ArpL3Protocol> through
473  // our Ptr<Ipv4> since they must both be aggregated to the same node.
474  //
475  Ptr<ArpL3Protocol> arpL3Protocol = ipv4->GetObject<ArpL3Protocol>();
476  asciiTraceHelper.HookDefaultDropSinkWithoutContext<ArpL3Protocol>(arpL3Protocol,
477  "Drop",
478  theStream);
479 
480  //
481  // The drop sink for the Ipv4L3Protocol uses a different signature than
482  // the default sink, so we have to cook one up for ourselves. We can get
483  // to the Ptr<Ipv4L3Protocol> through our Ptr<Ipv4> since they must both
484  // be aggregated to the same node.
485  //
486  Ptr<Ipv4L3Protocol> ipv4L3Protocol = ipv4->GetObject<Ipv4L3Protocol>();
487  bool result = ipv4L3Protocol->TraceConnectWithoutContext(
488  "Drop",
490  NS_ASSERT_MSG(result == true,
491  "ClickInternetStackHelper::EanableAsciiIpv4Internal(): "
492  "Unable to connect ipv4L3Protocol \"Drop\"");
493  }
494 
495  g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = theStream;
496  return;
497  }
498 
499  //
500  // If we are provided an OutputStreamWrapper, we are expected to use it, and
501  // to provide a context. We are free to come up with our own context if we
502  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
503  // compatibility and simplicity, we just use Config::Connect and let it deal
504  // with the context.
505  //
506  // We need to associate the ipv4/interface with a stream to express interest
507  // in tracing events on that pair, however, we only hook the trace sources
508  // once to avoid multiple trace sink calls per event (connect is independent
509  // of interface).
510  //
511  if (!AsciiHooked(ipv4))
512  {
513  Ptr<Node> node = ipv4->GetObject<Node>();
514  std::ostringstream oss;
515 
516  //
517  // For the ARP Drop, we are going to use the default trace sink provided by
518  // the ascii trace helper. There is actually no AsciiTraceHelper in sight
519  // here, but the default trace sinks are actually publicly available static
520  // functions that are always there waiting for just such a case.
521  //
522  oss << "/NodeList/" << node->GetId() << "/$ns3::ArpL3Protocol/Drop";
523  Config::Connect(oss.str(),
524  MakeBoundCallback(&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
525 
526  //
527  // This has all kinds of parameters coming with, so we have to cook up our
528  // own sink.
529  //
530  oss.str("");
531  oss << "/NodeList/" << node->GetId() << "/$ns3::Ipv4L3Protocol/Drop";
533  }
534 
535  g_interfaceStreamMapIpv4[std::make_pair(ipv4, interface)] = stream;
536 }
537 
538 } // namespace ns3
539 
540 #endif // NS3_CLICK
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
#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 Reset()
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition: config.cc:856
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
#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:768
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
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...
Definition: callback.h:707
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.