A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd-net-device-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
7 *
8 */
9
11
12#include "ns3/abort.h"
13#include "ns3/config.h"
14#include "ns3/fd-net-device.h"
15#include "ns3/log.h"
16#include "ns3/names.h"
17#include "ns3/object-factory.h"
18#include "ns3/packet.h"
19#include "ns3/simulator.h"
20#include "ns3/trace-helper.h"
21
22#include <string>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("FdNetDeviceHelper");
28
30{
31 m_deviceFactory.SetTypeId("ns3::FdNetDevice");
32}
33
34void
36{
37 m_deviceFactory.SetTypeId(type);
38}
39
40void
42{
43 NS_LOG_FUNCTION(this);
44 m_deviceFactory.Set(n1, v1);
45}
46
47void
50 bool promiscuous,
51 bool explicitFilename)
52{
53 //
54 // All of the Pcap enable functions vector through here including the ones
55 // that are wandering through all of devices on perhaps all of the nodes in
56 // the system. We can only deal with devices of type FdNetDevice.
57 //
58 Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice>();
59 if (!device)
60 {
61 NS_LOG_INFO("FdNetDeviceHelper::EnablePcapInternal(): Device "
62 << device << " not of type ns3::FdNetDevice");
63 return;
64 }
65
66 PcapHelper pcapHelper;
67
68 std::string filename;
69 if (explicitFilename)
70 {
71 filename = prefix;
72 }
73 else
74 {
75 filename = pcapHelper.GetFilenameFromDevice(prefix, device);
76 }
77
79 pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_EN10MB);
80 if (promiscuous)
81 {
82 pcapHelper.HookDefaultSink<FdNetDevice>(device, "PromiscSniffer", file);
83 }
84 else
85 {
86 pcapHelper.HookDefaultSink<FdNetDevice>(device, "Sniffer", file);
87 }
88}
89
90void
92 std::string prefix,
94 bool explicitFilename)
95{
96 //
97 // All of the ascii enable functions vector through here including the ones
98 // that are wandering through all of devices on perhaps all of the nodes in
99 // the system. We can only deal with devices of type FdNetDevice.
100 //
101 Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice>();
102 if (!device)
103 {
104 NS_LOG_INFO("FdNetDeviceHelper::EnableAsciiInternal(): Device "
105 << device << " not of type ns3::FdNetDevice");
106 return;
107 }
108
109 //
110 // Our default trace sinks are going to use packet printing, so we have to
111 // make sure that is turned on.
112 //
114
115 //
116 // If we are not provided an OutputStreamWrapper, we are expected to create
117 // one using the usual trace filename conventions and do a Hook*WithoutContext
118 // since there will be one file per context and therefore the context would
119 // be redundant.
120 //
121 if (!stream)
122 {
123 //
124 // Set up an output stream object to deal with private ofstream copy
125 // constructor and lifetime issues. Let the helper decide the actual
126 // name of the file given the prefix.
127 //
128 AsciiTraceHelper asciiTraceHelper;
129
130 std::string filename;
131 if (explicitFilename)
132 {
133 filename = prefix;
134 }
135 else
136 {
137 filename = asciiTraceHelper.GetFilenameFromDevice(prefix, device);
138 }
139
140 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
141
142 //
143 // The MacRx trace source provides our "r" event.
144 //
145 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<FdNetDevice>(device,
146 "MacRx",
147 theStream);
148
149 return;
150 }
151
152 //
153 // If we are provided an OutputStreamWrapper, we are expected to use it, and
154 // to providd a context. We are free to come up with our own context if we
155 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
156 // compatibility and simplicity, we just use Config::Connect and let it deal
157 // with the context.
158 //
159 // Note that we are going to use the default trace sinks provided by the
160 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
161 // but the default trace sinks are actually publicly available static
162 // functions that are always there waiting for just such a case.
163 //
164 uint32_t deviceid = nd->GetIfIndex();
165 std::ostringstream oss;
166
167 oss << "/NodeList/" << nd->GetNode()->GetId() << "/DeviceList/" << deviceid
168 << "/$ns3::FdNetDevice/MacRx";
169 Config::Connect(oss.str(),
171}
172
178
180FdNetDeviceHelper::Install(std::string nodeName) const
181{
182 Ptr<Node> node = Names::Find<Node>(nodeName);
183 return NetDeviceContainer(InstallPriv(node));
184}
185
188{
190
191 for (auto i = c.Begin(); i != c.End(); i++)
192 {
193 devs.Add(InstallPriv(*i));
194 }
195
196 return devs;
197}
198
201{
203 device->SetAddress(Mac48Address::Allocate());
204 node->AddDevice(device);
205 return device;
206}
207
208} // namespace ns3
Manage ASCII trace files for device models.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
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.
void HookDefaultReceiveSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default receive operation trace sink that does not accept nor log a trace ...
Hold a value for an Attribute.
Definition attribute.h:59
void SetAttribute(std::string n1, const AttributeValue &v1)
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
ObjectFactory m_deviceFactory
factory for the NetDevices
virtual Ptr< NetDevice > InstallPriv(Ptr< Node > node) const
This method creates an ns3::FdNetDevice and associates it to a node.
void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename) override
Enable pcap output on the indicated net device.
FdNetDeviceHelper()
Construct a FdNetDeviceHelper.
void SetTypeId(std::string type)
Set the TypeId of the Objects to be created by this helper.
void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename) override
Enable ascii trace output on the indicated net device.
a NetDevice to read/write network traffic from/into a file descriptor.
static Mac48Address Allocate()
Allocate a new Mac48Address.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
Manage pcap files for device models.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for a pcap file associated with a device.
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.
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:970
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:264
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Every class exported by the ns3 library is enclosed in the ns3 namespace.