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_queueFactory_red.SetTypeId ("ns3::RedQueue");
61 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
62 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
63 m_remoteChannelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
67 MyPointToPointHelper::SetQueue (std::string type,
68 std::string n1, const AttributeValue &v1,
69 std::string n2, const AttributeValue &v2,
70 std::string n3, const AttributeValue &v3,
71 std::string n4, const AttributeValue &v4,
72 std::string n5, const AttributeValue &v5,
73 std::string n6, const AttributeValue &v6,
74 std::string n7, const AttributeValue &v7,
75 std::string n8, const AttributeValue &v8)
77 m_queueFactory.SetTypeId (type);
78 m_queueFactory.Set (n1, v1);
79 m_queueFactory.Set (n2, v2);
80 m_queueFactory.Set (n3, v3);
81 m_queueFactory.Set (n4, v4);
82 m_queueFactory.Set (n5, v5);
83 m_queueFactory.Set (n6, v6);
84 m_queueFactory.Set (n7, v7);
85 m_queueFactory.Set (n8, v8);
89 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
91 m_deviceFactory.Set (n1, v1);
95 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
97 m_channelFactory.Set (n1, v1);
98 m_remoteChannelFactory.Set (n1, v1);
102 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
105 // All of the Pcap enable functions vector through here including the ones
106 // that are wandering through all of devices on perhaps all of the nodes in
107 // the system. We can only deal with devices of type PointToPointNetDevice.
109 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
112 XBT_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device not of type ns3::PointToPointNetDevice");
116 PcapHelper pcapHelper;
118 std::string filename;
119 if (explicitFilename)
125 filename = pcapHelper.GetFilenameFromDevice (prefix, device);
128 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
129 PcapHelper::DLT_PPP);
130 pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
134 MyPointToPointHelper::EnableAsciiInternal (
135 Ptr<OutputStreamWrapper> stream,
138 bool explicitFilename)
141 // All of the ascii enable functions vector through here including the ones
142 // that are wandering through all of devices on perhaps all of the nodes in
143 // the system. We can only deal with devices of type PointToPointNetDevice.
145 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
148 XBT_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device not of type ns3::PointToPointNetDevice");
153 // Our default trace sinks are going to use packet printing, so we have to
154 // make sure that is turned on.
156 Packet::EnablePrinting ();
159 // If we are not provided an OutputStreamWrapper, we are expected to create
160 // one using the usual trace filename conventions and do a Hook*WithoutContext
161 // since there will be one file per context and therefore the context would
167 // Set up an output stream object to deal with private ofstream copy
168 // constructor and lifetime issues. Let the helper decide the actual
169 // name of the file given the prefix.
171 AsciiTraceHelper asciiTraceHelper;
173 std::string filename;
174 if (explicitFilename)
180 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
183 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
186 // The MacRx trace source provides our "r" event.
188 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
191 // The "+", '-', and 'd' events are driven by trace sources actually in the
194 Ptr<Queue> queue = device->GetQueue ();
195 asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
196 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
197 asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
199 // PhyRxDrop trace source for "d" event
200 asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
206 // If we are provided an OutputStreamWrapper, we are expected to use it, and
207 // to providd a context. We are free to come up with our own context if we
208 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
209 // compatibility and simplicity, we just use Config::Connect and let it deal
212 // Note that we are going to use the default trace sinks provided by the
213 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
214 // but the default trace sinks are actually publicly available static
215 // functions that are always there waiting for just such a case.
217 uint32_t nodeid = nd->GetNode ()->GetId ();
218 uint32_t deviceid = nd->GetIfIndex ();
219 std::ostringstream oss;
221 oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
222 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
225 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
226 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
229 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
230 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
233 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
234 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
237 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
238 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
242 MyPointToPointHelper::Install (NodeContainer c)
244 NS_ASSERT (c.GetN () == 2);
245 return Install (c.Get (0), c.Get (1));
249 MyPointToPointHelper::Install (Ptr<Node> a, e_ns3_network_element_type_t type_a, Ptr<Node> b, e_ns3_network_element_type_t type_b)
251 NetDeviceContainer container;
255 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
256 devA->SetAddress (Mac48Address::Allocate ());
259 if(type_a == NS3_NETWORK_ELEMENT_ROUTER){
260 queueA = m_queueFactory_red.Create<Queue> ();
263 queueA = m_queueFactory.Create<Queue> ();
264 devA->SetQueue (queueA);
266 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
267 devB->SetAddress (Mac48Address::Allocate ());
270 if(type_b == NS3_NETWORK_ELEMENT_ROUTER){
271 queueB = m_queueFactory_red.Create<Queue> ();
274 queueB = m_queueFactory.Create<Queue> ();
275 devB->SetQueue (queueB);
277 // If MPI is enabled, we need to see if both nodes have the same system id
278 // (rank), and the rank is the same as this instance. If both are true,
279 //use a normal p2p channel, otherwise use a remote channel
280 bool useNormalChannel = true;
281 Ptr<PointToPointChannel> channel = 0;
282 if (MpiInterface::IsEnabled ())
284 uint32_t n1SystemId = a->GetSystemId ();
285 uint32_t n2SystemId = b->GetSystemId ();
286 uint32_t currSystemId = MpiInterface::GetSystemId ();
287 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
288 useNormalChannel = false;
290 if (useNormalChannel)
292 channel = m_channelFactory.Create<PointToPointChannel> ();
296 channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
297 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
298 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
299 mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
300 mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
301 devA->AggregateObject (mpiRecA);
302 devB->AggregateObject (mpiRecB);
305 devA->Attach (channel);
306 devB->Attach (channel);
307 container.Add (devA);
308 container.Add (devB);
314 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
316 NetDeviceContainer container;
318 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
319 devA->SetAddress (Mac48Address::Allocate ());
321 Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
322 devA->SetQueue (queueA);
323 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
324 devB->SetAddress (Mac48Address::Allocate ());
326 Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
327 devB->SetQueue (queueB);
328 // If MPI is enabled, we need to see if both nodes have the same system id
329 // (rank), and the rank is the same as this instance. If both are true,
330 //use a normal p2p channel, otherwise use a remote channel
331 bool useNormalChannel = true;
332 Ptr<PointToPointChannel> channel = 0;
333 if (MpiInterface::IsEnabled ())
335 uint32_t n1SystemId = a->GetSystemId ();
336 uint32_t n2SystemId = b->GetSystemId ();
337 uint32_t currSystemId = MpiInterface::GetSystemId ();
338 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
339 useNormalChannel = false;
341 if (useNormalChannel)
343 channel = m_channelFactory.Create<PointToPointChannel> ();
347 channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
348 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
349 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
350 mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
351 mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
352 devA->AggregateObject (mpiRecA);
353 devB->AggregateObject (mpiRecB);
356 devA->Attach (channel);
357 devB->Attach (channel);
358 container.Add (devA);
359 container.Add (devB);
365 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
367 Ptr<Node> b = Names::Find<Node> (bName);
368 return Install (a, b);
372 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
374 Ptr<Node> a = Names::Find<Node> (aName);
375 return Install (a, b);
379 MyPointToPointHelper::Install (std::string aName, std::string bName)
381 Ptr<Node> a = Names::Find<Node> (aName);
382 Ptr<Node> b = Names::Find<Node> (bName);
383 return Install (a, b);