Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / surf / ns3 / my-point-to-point-helper.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
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;
8  *
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.
13  *
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
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20
21 #include "ns3/abort.h"
22 #include "ns3/log.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"
34
35 #include "ns3/trace-helper.h"
36 #include "my-point-to-point-helper.h"
37
38 NS_LOG_COMPONENT_DEFINE ("MyPointToPointHelper");
39
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";
95
96 namespace ns3 {
97
98 MyPointToPointHelper::MyPointToPointHelper ()
99 {
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");
113 }
114
115 void 
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)
125 {
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);
135 }
136
137 void 
138 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
139 {
140   m_deviceFactory.Set (n1, v1);
141 }
142
143 void 
144 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
145 {
146   m_channelFactory.Set (n1, v1);
147   m_remoteChannelFactory.Set (n1, v1);
148 }
149
150 void 
151 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
152 {
153   //
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.
157   //
158   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
159   if (device == 0)
160     {
161       NS_LOG_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device " << device << " not of type ns3::PointToPointNetDevice");
162       return;
163     }
164
165   PcapHelper pcapHelper;
166
167   std::string filename;
168   if (explicitFilename)
169     {
170       filename = prefix;
171     }
172   else
173     {
174       filename = pcapHelper.GetFilenameFromDevice (prefix, device);
175     }
176
177   Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, 
178                                                      PcapHelper::DLT_PPP);
179   pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
180 }
181
182 void 
183 MyPointToPointHelper::EnableAsciiInternal (
184   Ptr<OutputStreamWrapper> stream, 
185   std::string prefix, 
186   Ptr<NetDevice> nd,
187   bool explicitFilename)
188 {
189   //
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.
193   //
194   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
195   if (device == 0)
196     {
197       NS_LOG_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device " << device <<
198                    " not of type ns3::PointToPointNetDevice");
199       return;
200     }
201
202   //
203   // Our default trace sinks are going to use packet printing, so we have to 
204   // make sure that is turned on.
205   //
206   Packet::EnablePrinting ();
207
208   //
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
212   // be redundant.
213   //
214   if (stream == 0)
215     {
216       //
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.
220       //
221       AsciiTraceHelper asciiTraceHelper;
222
223       std::string filename;
224       if (explicitFilename)
225         {
226           filename = prefix;
227         }
228       else
229         {
230           filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
231         }
232
233       Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
234
235       //
236       // The MacRx trace source provides our "r" event.
237       //
238       asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
239
240       //
241       // The "+", '-', and 'd' events are driven by trace sources actually in the
242       // transmit queue.
243       //
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);
248
249       // PhyRxDrop trace source for "d" event
250       asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
251
252       return;
253     }
254
255   //
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
260   // with the context.
261   //
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.
266   //
267   uint32_t nodeid = nd->GetNode ()->GetId ();
268   uint32_t deviceid = nd->GetIfIndex ();
269   std::ostringstream oss;
270
271   oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
272   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
273
274   oss.str ("");
275   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
276   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
277
278   oss.str ("");
279   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
280   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
281
282   oss.str ("");
283   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
284   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
285
286   oss.str ("");
287   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
288   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
289 }
290
291 NetDeviceContainer 
292 MyPointToPointHelper::Install (NodeContainer c)
293 {
294   NS_ASSERT (c.GetN () == 2);
295   return Install (c.Get (0), c.Get (1));
296 }
297
298 NetDeviceContainer 
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)
300 {
301   NetDeviceContainer container;
302   Ptr<Queue> queueA;
303   Ptr<Queue> queueB;
304
305   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
306   devA->SetAddress (Mac48Address::Allocate ());
307   a->AddDevice (devA);
308
309   if(type_a == NS3_NETWORK_ELEMENT_ROUTER){
310         queueA = m_queueFactory_red.Create<Queue> ();
311   }
312   else
313           queueA = m_queueFactory.Create<Queue> ();
314   devA->SetQueue (queueA);
315
316   Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
317   devB->SetAddress (Mac48Address::Allocate ());
318   b->AddDevice (devB);
319
320   if(type_b == NS3_NETWORK_ELEMENT_ROUTER){
321         queueB = m_queueFactory_red.Create<Queue> ();
322   }
323   else
324           queueB = m_queueFactory.Create<Queue> ();
325   devB->SetQueue (queueB);
326
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 ())
333     {
334       uint32_t n1SystemId = a->GetSystemId ();
335       uint32_t n2SystemId = b->GetSystemId ();
336       uint32_t currSystemId = MpiInterface::GetSystemId ();
337       if (n1SystemId != currSystemId || n2SystemId != currSystemId) 
338         {
339           useNormalChannel = false;
340         }
341     }
342   if (useNormalChannel)
343     {
344       channel = m_channelFactory.Create<PointToPointChannel> ();
345     }
346   else
347     {
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);
355     }
356
357   devA->Attach (channel);
358   devB->Attach (channel);
359   container.Add (devA);
360   container.Add (devB);
361
362   return container;
363 }
364
365 NetDeviceContainer 
366 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
367 {
368   NetDeviceContainer container;
369
370   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
371   devA->SetAddress (Mac48Address::Allocate ());
372   a->AddDevice (devA);
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 ());
377   b->AddDevice (devB);
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 ())
386     {
387       uint32_t n1SystemId = a->GetSystemId ();
388       uint32_t n2SystemId = b->GetSystemId ();
389       uint32_t currSystemId = MpiInterface::GetSystemId ();
390       if (n1SystemId != currSystemId || n2SystemId != currSystemId)
391         {
392           useNormalChannel = false;
393         }
394     }
395   if (useNormalChannel)
396     {
397       channel = m_channelFactory.Create<PointToPointChannel> ();
398     }
399   else
400     {
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);
408     }
409
410   devA->Attach (channel);
411   devB->Attach (channel);
412   container.Add (devA);
413   container.Add (devB);
414
415   return container;
416 }
417
418 NetDeviceContainer
419 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
420 {
421   Ptr<Node> b = Names::Find<Node> (bName);
422   return Install (a, b);
423 }
424
425 NetDeviceContainer 
426 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
427 {
428   Ptr<Node> a = Names::Find<Node> (aName);
429   return Install (a, b);
430 }
431
432 NetDeviceContainer 
433 MyPointToPointHelper::Install (std::string aName, std::string bName)
434 {
435   Ptr<Node> a = Names::Find<Node> (aName);
436   Ptr<Node> b = Names::Find<Node> (bName);
437   return Install (a, b);
438 }
439
440 } // namespace ns3