1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
21 #include "ns3/abort.h"
23 #include "ns3/simulator.h"
24 #include "ns3/point-to-point-net-device.h"
25 #include "ns3/point-to-point-channel.h"
26 #include "ns3/point-to-point-remote-channel.h"
27 #include "ns3/queue.h"
28 #include "ns3/config.h"
29 #include "ns3/packet.h"
30 #include "ns3/names.h"
31 #include "ns3/string.h"
32 #include "ns3/mpi-interface.h"
33 #include "ns3/mpi-receiver.h"
35 #include "ns3/trace-helper.h"
36 #include "my-point-to-point-helper.h"
38 NS_LOG_COMPONENT_DEFINE ("MyPointToPointHelper");
40 ///> RED Parameters see src/node/red-queue.* for details
41 //.AddAttribute ("Mode",
42 // "Whether to use Bytes (see MaxBytes) or Packets (see MaxPackets) as the maximum queue size metric.",
43 // EnumValue (BYTES), ///> currently supports BYTES only
44 // MakeEnumAccessor (&RedQueue::SetMode),
45 // MakeEnumChecker (BYTES, "Bytes",
46 // PACKETS, "Packets"))
47 // .AddAttribute ("MaxPackets",
48 // "The maximum number of packets accepted by this RedQueue.",
49 // UintegerValue (100),
50 // MakeUintegerAccessor (&RedQueue::m_maxPackets),
51 // MakeUintegerChecker<uint32_t> ())
52 // .AddAttribute ("MaxBytes",
53 // "The maximum number of bytes accepted by this RedQueue.",
54 // UintegerValue (100000),
55 // MakeUintegerAccessor (&RedQueue::m_maxBytes),
56 // MakeUintegerChecker<uint32_t> ())
57 // .AddAttribute ("m_burst",
58 // "maximum number of m_burst packets accepted by this queue",
59 // UintegerValue (6), ///> bursts must be > minTh/avpkt
60 // MakeUintegerAccessor (&RedQueue::m_burst),
61 // MakeUintegerChecker<uint32_t> ())
62 // .AddAttribute ("m_avPkt",
63 // "In bytes, use with m_burst to determine the time constant for average queue size calculations",
64 // UintegerValue (1024), ///> average packet size
65 // MakeUintegerAccessor (&RedQueue::m_avPkt),
66 // MakeUintegerChecker<uint32_t> ())
67 // .AddAttribute ("m_minTh",
68 // "Average queue size at which marking becomes a m_prob",
69 // UintegerValue (5120), ///> in bytes 1024x5
70 // MakeUintegerAccessor (&RedQueue::m_minTh),
71 // MakeUintegerChecker<uint32_t> ())
72 // .AddAttribute ("m_maxTh",
73 // "Maximal marking m_prob, should be at least twice min to prevent synchronous retransmits",
74 // UintegerValue (15360), ///> in bytes 1024x15
75 // MakeUintegerAccessor (&RedQueue::m_maxTh),
76 // MakeUintegerChecker<uint32_t> ())
77 // .AddAttribute ("m_rate",
78 // "this m_rate is used for calculating the average queue size after some idle time.",
79 // UintegerValue (1500000), ///> in bps, should be set to bandwidth of interface
80 // MakeUintegerAccessor (&RedQueue::m_rate),
81 // MakeUintegerChecker<uint64_t> ())
82 // .AddAttribute ("m_prob",
83 // "Probability for marking, suggested values are 0.01 and 0.02",
84 // DoubleValue (0.02),
85 // MakeDoubleAccessor (&RedQueue::m_prob),
86 // MakeDoubleChecker <double> ())
87 std::string qMode = "Bytes";
88 std::string qBurst = "6";
89 std::string qAvPkt = "1024";
90 std::string qLimit = "25600"; //"100000";
91 std::string qthMin = "5120"; // 1024 x 5 bytes
92 std::string qthMax = "15360"; // 1024 x 15 bytes
93 std::string qIdleRate = "1500000"; //1.5 Mbps
94 std::string qProb = "0.02";
98 MyPointToPointHelper::MyPointToPointHelper ()
100 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
101 m_queueFactory_red.SetTypeId ("ns3::RedQueue");
102 // m_queueFactory_red.Set ("Mode", StringValue (qMode));
103 // m_queueFactory_red.Set ("MaxBytes",StringValue (qLimit));
104 // m_queueFactory_red.Set ("m_burst", StringValue (qBurst));
105 // m_queueFactory_red.Set ("m_avPkt", StringValue (qAvPkt));
106 // m_queueFactory_red.Set ("m_minTh", StringValue (qthMin));
107 // m_queueFactory_red.Set ("m_maxTh", StringValue (qthMax));
108 // m_queueFactory_red.Set ("m_rate", StringValue (qIdleRate));
109 // m_queueFactory_red.Set ("m_prob", StringValue (qProb));
110 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
111 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
112 m_remoteChannelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
116 MyPointToPointHelper::SetQueue (std::string type,
117 std::string n1, const AttributeValue &v1,
118 std::string n2, const AttributeValue &v2,
119 std::string n3, const AttributeValue &v3,
120 std::string n4, const AttributeValue &v4,
121 std::string n5, const AttributeValue &v5,
122 std::string n6, const AttributeValue &v6,
123 std::string n7, const AttributeValue &v7,
124 std::string n8, const AttributeValue &v8)
126 m_queueFactory.SetTypeId (type);
127 m_queueFactory.Set (n1, v1);
128 m_queueFactory.Set (n2, v2);
129 m_queueFactory.Set (n3, v3);
130 m_queueFactory.Set (n4, v4);
131 m_queueFactory.Set (n5, v5);
132 m_queueFactory.Set (n6, v6);
133 m_queueFactory.Set (n7, v7);
134 m_queueFactory.Set (n8, v8);
138 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
140 m_deviceFactory.Set (n1, v1);
144 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
146 m_channelFactory.Set (n1, v1);
147 m_remoteChannelFactory.Set (n1, v1);
151 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
154 // All of the Pcap enable functions vector through here including the ones
155 // that are wandering through all of devices on perhaps all of the nodes in
156 // the system. We can only deal with devices of type PointToPointNetDevice.
158 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
161 NS_LOG_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
165 PcapHelper pcapHelper;
167 std::string filename;
168 if (explicitFilename)
174 filename = pcapHelper.GetFilenameFromDevice (prefix, device);
177 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
178 PcapHelper::DLT_PPP);
179 pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
183 MyPointToPointHelper::EnableAsciiInternal (
184 Ptr<OutputStreamWrapper> stream,
187 bool explicitFilename)
190 // All of the ascii enable functions vector through here including the ones
191 // that are wandering through all of devices on perhaps all of the nodes in
192 // the system. We can only deal with devices of type PointToPointNetDevice.
194 Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
197 NS_LOG_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device " << device <<
198 " not of type ns3::PointToPointNetDevice");
203 // Our default trace sinks are going to use packet printing, so we have to
204 // make sure that is turned on.
206 Packet::EnablePrinting ();
209 // If we are not provided an OutputStreamWrapper, we are expected to create
210 // one using the usual trace filename conventions and do a Hook*WithoutContext
211 // since there will be one file per context and therefore the context would
217 // Set up an output stream object to deal with private ofstream copy
218 // constructor and lifetime issues. Let the helper decide the actual
219 // name of the file given the prefix.
221 AsciiTraceHelper asciiTraceHelper;
223 std::string filename;
224 if (explicitFilename)
230 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
233 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
236 // The MacRx trace source provides our "r" event.
238 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
241 // The "+", '-', and 'd' events are driven by trace sources actually in the
244 Ptr<Queue> queue = device->GetQueue ();
245 asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
246 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
247 asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
249 // PhyRxDrop trace source for "d" event
250 asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
256 // If we are provided an OutputStreamWrapper, we are expected to use it, and
257 // to providd a context. We are free to come up with our own context if we
258 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
259 // compatibility and simplicity, we just use Config::Connect and let it deal
262 // Note that we are going to use the default trace sinks provided by the
263 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
264 // but the default trace sinks are actually publicly available static
265 // functions that are always there waiting for just such a case.
267 uint32_t nodeid = nd->GetNode ()->GetId ();
268 uint32_t deviceid = nd->GetIfIndex ();
269 std::ostringstream oss;
271 oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
272 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
275 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
276 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
279 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
280 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
283 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
284 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
287 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
288 Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
292 MyPointToPointHelper::Install (NodeContainer c)
294 NS_ASSERT (c.GetN () == 2);
295 return Install (c.Get (0), c.Get (1));
299 MyPointToPointHelper::Install (Ptr<Node> a, e_ns3_network_element_type_t type_a, Ptr<Node> b, e_ns3_network_element_type_t type_b)
301 NetDeviceContainer container;
305 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
306 devA->SetAddress (Mac48Address::Allocate ());
309 if(type_a == NS3_NETWORK_ELEMENT_ROUTER){
310 queueA = m_queueFactory_red.Create<Queue> ();
313 queueA = m_queueFactory.Create<Queue> ();
314 devA->SetQueue (queueA);
316 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
317 devB->SetAddress (Mac48Address::Allocate ());
320 if(type_b == NS3_NETWORK_ELEMENT_ROUTER){
321 queueB = m_queueFactory_red.Create<Queue> ();
324 queueB = m_queueFactory.Create<Queue> ();
325 devB->SetQueue (queueB);
327 // If MPI is enabled, we need to see if both nodes have the same system id
328 // (rank), and the rank is the same as this instance. If both are true,
329 //use a normal p2p channel, otherwise use a remote channel
330 bool useNormalChannel = true;
331 Ptr<PointToPointChannel> channel = 0;
332 if (MpiInterface::IsEnabled ())
334 uint32_t n1SystemId = a->GetSystemId ();
335 uint32_t n2SystemId = b->GetSystemId ();
336 uint32_t currSystemId = MpiInterface::GetSystemId ();
337 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
339 useNormalChannel = false;
342 if (useNormalChannel)
344 channel = m_channelFactory.Create<PointToPointChannel> ();
348 channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
349 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
350 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
351 mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
352 mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
353 devA->AggregateObject (mpiRecA);
354 devB->AggregateObject (mpiRecB);
357 devA->Attach (channel);
358 devB->Attach (channel);
359 container.Add (devA);
360 container.Add (devB);
366 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
368 NetDeviceContainer container;
370 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
371 devA->SetAddress (Mac48Address::Allocate ());
373 Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
374 devA->SetQueue (queueA);
375 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
376 devB->SetAddress (Mac48Address::Allocate ());
378 Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
379 devB->SetQueue (queueB);
380 // If MPI is enabled, we need to see if both nodes have the same system id
381 // (rank), and the rank is the same as this instance. If both are true,
382 //use a normal p2p channel, otherwise use a remote channel
383 bool useNormalChannel = true;
384 Ptr<PointToPointChannel> channel = 0;
385 if (MpiInterface::IsEnabled ())
387 uint32_t n1SystemId = a->GetSystemId ();
388 uint32_t n2SystemId = b->GetSystemId ();
389 uint32_t currSystemId = MpiInterface::GetSystemId ();
390 if (n1SystemId != currSystemId || n2SystemId != currSystemId)
392 useNormalChannel = false;
395 if (useNormalChannel)
397 channel = m_channelFactory.Create<PointToPointChannel> ();
401 channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
402 Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
403 Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
404 mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
405 mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
406 devA->AggregateObject (mpiRecA);
407 devB->AggregateObject (mpiRecB);
410 devA->Attach (channel);
411 devB->Attach (channel);
412 container.Add (devA);
413 container.Add (devB);
419 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
421 Ptr<Node> b = Names::Find<Node> (bName);
422 return Install (a, b);
426 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
428 Ptr<Node> a = Names::Find<Node> (aName);
429 return Install (a, b);
433 MyPointToPointHelper::Install (std::string aName, std::string bName)
435 Ptr<Node> a = Names::Find<Node> (aName);
436 Ptr<Node> b = Names::Find<Node> (bName);
437 return Install (a, b);