Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfee8545d4631e22682e694d49dd4daa0034d645
[simgrid.git] / src / surf / ns3 / my-point-to-point-helper.cc
1 /* Copyright (c) 2011, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /*
8  * Copyright (c) 2008 INRIA
9  *
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;
13  *
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.
18  *
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
22  *
23  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
24  */
25
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"
38
39 #include "ns3/trace-helper.h"
40 #include "my-point-to-point-helper.h"
41
42 #include "xbt/log.h"
43
44 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
45
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";
54
55 namespace ns3 {
56
57 MyPointToPointHelper::MyPointToPointHelper ()
58 {
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");
64 }
65
66 void 
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)
76 {
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);
86 }
87
88 void 
89 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
90 {
91   m_deviceFactory.Set (n1, v1);
92 }
93
94 void 
95 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
96 {
97   m_channelFactory.Set (n1, v1);
98   m_remoteChannelFactory.Set (n1, v1);
99 }
100
101 void 
102 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
103 {
104   //
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.
108   //
109   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
110   if (device == 0)
111     {
112       XBT_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device not of type ns3::PointToPointNetDevice");
113       return;
114     }
115
116   PcapHelper pcapHelper;
117
118   std::string filename;
119   if (explicitFilename)
120     {
121       filename = prefix;
122     }
123   else
124     {
125       filename = pcapHelper.GetFilenameFromDevice (prefix, device);
126     }
127
128   Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, 
129                                                      PcapHelper::DLT_PPP);
130   pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
131 }
132
133 void 
134 MyPointToPointHelper::EnableAsciiInternal (
135   Ptr<OutputStreamWrapper> stream, 
136   std::string prefix, 
137   Ptr<NetDevice> nd,
138   bool explicitFilename)
139 {
140   //
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.
144   //
145   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
146   if (device == 0)
147     {
148       XBT_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device not of type ns3::PointToPointNetDevice");
149       return;
150     }
151
152   //
153   // Our default trace sinks are going to use packet printing, so we have to 
154   // make sure that is turned on.
155   //
156   Packet::EnablePrinting ();
157
158   //
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
162   // be redundant.
163   //
164   if (stream == 0)
165     {
166       //
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.
170       //
171       AsciiTraceHelper asciiTraceHelper;
172
173       std::string filename;
174       if (explicitFilename)
175         {
176           filename = prefix;
177         }
178       else
179         {
180           filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
181         }
182
183       Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
184
185       //
186       // The MacRx trace source provides our "r" event.
187       //
188       asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
189
190       //
191       // The "+", '-', and 'd' events are driven by trace sources actually in the
192       // transmit queue.
193       //
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);
198
199       // PhyRxDrop trace source for "d" event
200       asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
201
202       return;
203     }
204
205   //
206   // If we are provided an OutputStreamWrapper, we are expected to use it, and
207   // to provide 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
210   // with the context.
211   //
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.
216   //
217   uint32_t nodeid = nd->GetNode ()->GetId ();
218   uint32_t deviceid = nd->GetIfIndex ();
219   std::ostringstream oss;
220
221   oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
222   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
223
224   oss.str ("");
225   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
226   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
227
228   oss.str ("");
229   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
230   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
231
232   oss.str ("");
233   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
234   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
235
236   oss.str ("");
237   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
238   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
239 }
240
241 NetDeviceContainer 
242 MyPointToPointHelper::Install (NodeContainer c)
243 {
244   NS_ASSERT (c.GetN () == 2);
245   return Install (c.Get (0), c.Get (1));
246 }
247
248 NetDeviceContainer 
249 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
250 {
251   NetDeviceContainer container;
252
253   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
254   devA->SetAddress (Mac48Address::Allocate ());
255   a->AddDevice (devA);
256   Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
257   devA->SetQueue (queueA);
258   Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
259   devB->SetAddress (Mac48Address::Allocate ());
260   b->AddDevice (devB);
261   Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
262   devB->SetQueue (queueB);
263   // If MPI is enabled, we need to see if both nodes have the same system id
264   // (rank), and the rank is the same as this instance.  If both are true,
265   //use a normal p2p channel, otherwise use a remote channel
266   bool useNormalChannel = true;
267   Ptr<PointToPointChannel> channel = 0;
268   if (MpiInterface::IsEnabled ()) {
269       uint32_t n1SystemId = a->GetSystemId ();
270       uint32_t n2SystemId = b->GetSystemId ();
271       uint32_t currSystemId = MpiInterface::GetSystemId ();
272       if (n1SystemId != currSystemId || n2SystemId != currSystemId)
273           useNormalChannel = false;
274   }
275   if (useNormalChannel)
276       channel = m_channelFactory.Create<PointToPointChannel> ();
277   else {
278       channel = m_remoteChannelFactory.Create<PointToPointRemoteChannel> ();
279       Ptr<MpiReceiver> mpiRecA = CreateObject<MpiReceiver> ();
280       Ptr<MpiReceiver> mpiRecB = CreateObject<MpiReceiver> ();
281       mpiRecA->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devA));
282       mpiRecB->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, devB));
283       devA->AggregateObject (mpiRecA);
284       devB->AggregateObject (mpiRecB);
285     }
286
287   devA->Attach (channel);
288   devB->Attach (channel);
289   container.Add (devA);
290   container.Add (devB);
291
292   return container;
293 }
294
295 NetDeviceContainer
296 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
297 {
298   Ptr<Node> b = Names::Find<Node> (bName);
299   return Install (a, b);
300 }
301
302 NetDeviceContainer 
303 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
304 {
305   Ptr<Node> a = Names::Find<Node> (aName);
306   return Install (a, b);
307 }
308
309 NetDeviceContainer 
310 MyPointToPointHelper::Install (std::string aName, std::string bName)
311 {
312   Ptr<Node> a = Names::Find<Node> (aName);
313   Ptr<Node> b = Names::Find<Node> (bName);
314   return Install (a, b);
315 }
316
317 } // namespace ns3