1 /* Copyright (c) 2011, 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Copyright (c) 2008 INRIA
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation;
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
26 #include <ns3/abort.h>
27 #include "ns3/simulator.h"
28 #include "ns3/point-to-point-net-device.h"
29 #include "ns3/point-to-point-channel.h"
30 #include "ns3/point-to-point-remote-channel.h"
31 #include "ns3/queue.h"
32 #include "ns3/config.h"
33 #include "ns3/packet.h"
34 #include "ns3/names.h"
35 #include "ns3/string.h"
36 #include "ns3/mpi-interface.h"
37 #include "ns3/mpi-receiver.h"
39 #include "ns3/trace-helper.h"
40 #include "my-point-to-point-helper.h"
44 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
46 std::string qMode = "Bytes";
47 std::string qBurst = "6";
48 std::string qAvPkt = "1024";
49 std::string qLimit = "25600"; //"100000";
50 std::string qthMin = "5120"; // 1024 x 5 bytes
51 std::string qthMax = "15360"; // 1024 x 15 bytes
52 std::string qIdleRate = "1500000"; //1.5 Mbps
53 std::string qProb = "0.02";
57 MyPointToPointHelper::MyPointToPointHelper ()
59 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
60 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
61 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
62 m_remoteChannelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
66 MyPointToPointHelper::SetQueue (std::string type,
67 std::string n1, const AttributeValue &v1,
68 std::string n2, const AttributeValue &v2,
69 std::string n3, const AttributeValue &v3,
70 std::string n4, const AttributeValue &v4,
71 std::string n5, const AttributeValue &v5,
72 std::string n6, const AttributeValue &v6,
73 std::string n7, const AttributeValue &v7,
74 std::string n8, const AttributeValue &v8)
76 m_queueFactory.SetTypeId (type);
77 m_queueFactory.Set (n1, v1);
78 m_queueFactory.Set (n2, v2);
79 m_queueFactory.Set (n3, v3);
80 m_queueFactory.Set (n4, v4);
81 m_queueFactory.Set (n5, v5);
82 m_queueFactory.Set (n6, v6);
83 m_queueFactory.Set (n7, v7);
84 m_queueFactory.Set (n8, v8);
88 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
90 m_deviceFactory.Set (n1, v1);
94 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
96 m_channelFactory.Set (n1, v1);
97 m_remoteChannelFactory.Set (n1, v1);
101 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
104 // All of the Pcap enable functions vector through here including the ones
105 // that are wandering through all of devices on perhaps all of the nodes in
106 // the system. We can only deal with devices of type PointToPointNetDevice.
108 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
111 XBT_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device not of type ns3::PointToPointNetDevice");
115 PcapHelper pcapHelper;
117 std::string filename;
118 if (explicitFilename)
124 filename = pcapHelper.GetFilenameFromDevice (prefix, device);
127 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
128 PcapHelper::DLT_PPP);
129 pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
133 MyPointToPointHelper::EnableAsciiInternal (
134 Ptr<OutputStreamWrapper> stream,
137 bool explicitFilename)
140 // All of the ascii enable functions vector through here including the ones
141 // that are wandering through all of devices on perhaps all of the nodes in
142 // the system. We can only deal with devices of type PointToPointNetDevice.
144 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
147 XBT_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device not of type ns3::PointToPointNetDevice");
152 // Our default trace sinks are going to use packet printing, so we have to
153 // make sure that is turned on.
155 Packet::EnablePrinting ();
158 // If we are not provided an OutputStreamWrapper, we are expected to create
159 // one using the usual trace filename conventions and do a Hook*WithoutContext
160 // since there will be one file per context and therefore the context would
166 // Set up an output stream object to deal with private ofstream copy
167 // constructor and lifetime issues. Let the helper decide the actual
168 // name of the file given the prefix.
170 AsciiTraceHelper asciiTraceHelper;
172 std::string filename;
173 if (explicitFilename)
179 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
182 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
185 // The MacRx trace source provides our "r" event.
187 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
190 // The "+", '-', and 'd' events are driven by trace sources actually in the
193 Ptr<Queue> queue = device->GetQueue ();
194 asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
195 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
196 asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
198 // PhyRxDrop trace source for "d" event
199 asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
205 // If we are provided an OutputStreamWrapper, we are expected to use it, and
206 // to provide a context. We are free to come up with our own context if we
207 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
208 // compatibility and simplicity, we just use Config::Connect and let it deal
211 // Note that we are going to use the default trace sinks provided by the
212 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
213 // but the default trace sinks are actually publicly available static
214 // functions that are always there waiting for just such a case.
216 uint32_t nodeid = nd->GetNode ()->GetId ();
217 uint32_t deviceid = nd->GetIfIndex ();
218 std::ostringstream oss;
220 oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
221 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
224 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
225 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
228 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
229 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
232 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
233 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
236 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
237 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
241 MyPointToPointHelper::Install (NodeContainer c)
243 NS_ASSERT (c.GetN () == 2);
244 return Install (c.Get (0), c.Get (1));
248 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
250 NetDeviceContainer container;
252 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
253 devA->SetAddress (Mac48Address::Allocate ());
255 Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
256 devA->SetQueue (queueA);
257 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
258 devB->SetAddress (Mac48Address::Allocate ());
260 Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
261 devB->SetQueue (queueB);
262 // If MPI is enabled, we need to see if both nodes have the same system id
263 // (rank), and the rank is the same as this instance. If both are true,
264 //use a normal p2p channel, otherwise use a remote channel
265 bool useNormalChannel = true;
266 Ptr<PointToPointChannel> channel = 0;
267 if (MpiInterface::IsEnabled ()) {
268 uint32_t n1SystemId = a->GetSystemId ();
269 uint32_t n2SystemId = b->GetSystemId ();
270 uint32_t currSystemId = MpiInterface::GetSystemId ();
271 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
272 useNormalChannel = false;
274 if (useNormalChannel)
275 channel = m_channelFactory.Create<PointToPointChannel> ();
277 channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
278 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
279 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
280 mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
281 mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
282 devA->AggregateObject (mpiRecA);
283 devB->AggregateObject (mpiRecB);
286 devA->Attach (channel);
287 devB->Attach (channel);
288 container.Add (devA);
289 container.Add (devB);
295 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
297 Ptr<Node> b = Names::Find<Node> (bName);
298 return Install (a, b);
302 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
304 Ptr<Node> a = Names::Find<Node> (aName);
305 return Install (a, b);
309 MyPointToPointHelper::Install (std::string aName, std::string bName)
311 Ptr<Node> a = Names::Find<Node> (aName);
312 Ptr<Node> b = Names::Find<Node> (bName);
313 return Install (a, b);