Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't copy&change ns3::point-to-point-helper
[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_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
61   m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
62   m_remoteChannelFactory.SetTypeId ("ns3::PointToPointRemoteChannel");
63 }
64
65 void 
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)
75 {
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);
85 }
86
87 void 
88 MyPointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
89 {
90   m_deviceFactory.Set (n1, v1);
91 }
92
93 void 
94 MyPointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
95 {
96   m_channelFactory.Set (n1, v1);
97   m_remoteChannelFactory.Set (n1, v1);
98 }
99
100 void 
101 MyPointToPointHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
102 {
103   //
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.
107   //
108   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
109   if (device == 0)
110     {
111       XBT_INFO ("MyPointToPointHelper::EnablePcapInternal(): Device not of type ns3::PointToPointNetDevice");
112       return;
113     }
114
115   PcapHelper pcapHelper;
116
117   std::string filename;
118   if (explicitFilename)
119     {
120       filename = prefix;
121     }
122   else
123     {
124       filename = pcapHelper.GetFilenameFromDevice (prefix, device);
125     }
126
127   Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, 
128                                                      PcapHelper::DLT_PPP);
129   pcapHelper.HookDefaultSink<PointToPointNetDevice> (device, "PromiscSniffer", file);
130 }
131
132 void 
133 MyPointToPointHelper::EnableAsciiInternal (
134   Ptr<OutputStreamWrapper> stream, 
135   std::string prefix, 
136   Ptr<NetDevice> nd,
137   bool explicitFilename)
138 {
139   //
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.
143   //
144   Ptr<PointToPointNetDevice> device = nd->GetObject<PointToPointNetDevice> ();
145   if (device == 0)
146     {
147       XBT_INFO ("MyPointToPointHelper::EnableAsciiInternal(): Device not of type ns3::PointToPointNetDevice");
148       return;
149     }
150
151   //
152   // Our default trace sinks are going to use packet printing, so we have to 
153   // make sure that is turned on.
154   //
155   Packet::EnablePrinting ();
156
157   //
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
161   // be redundant.
162   //
163   if (stream == 0)
164     {
165       //
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.
169       //
170       AsciiTraceHelper asciiTraceHelper;
171
172       std::string filename;
173       if (explicitFilename)
174         {
175           filename = prefix;
176         }
177       else
178         {
179           filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
180         }
181
182       Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
183
184       //
185       // The MacRx trace source provides our "r" event.
186       //
187       asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<PointToPointNetDevice> (device, "MacRx", theStream);
188
189       //
190       // The "+", '-', and 'd' events are driven by trace sources actually in the
191       // transmit queue.
192       //
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);
197
198       // PhyRxDrop trace source for "d" event
199       asciiTraceHelper.HookDefaultDropSinkWithoutContext<PointToPointNetDevice> (device, "PhyRxDrop", theStream);
200
201       return;
202     }
203
204   //
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
209   // with the context.
210   //
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.
215   //
216   uint32_t nodeid = nd->GetNode ()->GetId ();
217   uint32_t deviceid = nd->GetIfIndex ();
218   std::ostringstream oss;
219
220   oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
221   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultReceiveSinkWithContext, stream));
222
223   oss.str ("");
224   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
225   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultEnqueueSinkWithContext, stream));
226
227   oss.str ("");
228   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
229   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDequeueSinkWithContext, stream));
230
231   oss.str ("");
232   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
233   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
234
235   oss.str ("");
236   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/PhyRxDrop";
237   Config::Connect (oss.str (), MakeBoundCallback (&AsciiTraceHelper::DefaultDropSinkWithContext, stream));
238 }
239
240 NetDeviceContainer 
241 MyPointToPointHelper::Install (NodeContainer c)
242 {
243   NS_ASSERT (c.GetN () == 2);
244   return Install (c.Get (0), c.Get (1));
245 }
246
247 NetDeviceContainer 
248 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
249 {
250   NetDeviceContainer container;
251
252   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
253   devA->SetAddress (Mac48Address::Allocate ());
254   a->AddDevice (devA);
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 ());
259   b->AddDevice (devB);
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;
273   }
274   if (useNormalChannel)
275       channel = m_channelFactory.Create<PointToPointChannel> ();
276   else {
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);
284     }
285
286   devA->Attach (channel);
287   devB->Attach (channel);
288   container.Add (devA);
289   container.Add (devB);
290
291   return container;
292 }
293
294 NetDeviceContainer
295 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
296 {
297   Ptr<Node> b = Names::Find<Node> (bName);
298   return Install (a, b);
299 }
300
301 NetDeviceContainer 
302 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
303 {
304   Ptr<Node> a = Names::Find<Node> (aName);
305   return Install (a, b);
306 }
307
308 NetDeviceContainer 
309 MyPointToPointHelper::Install (std::string aName, std::string bName)
310 {
311   Ptr<Node> a = Names::Find<Node> (aName);
312   Ptr<Node> b = Names::Find<Node> (bName);
313   return Install (a, b);
314 }
315
316 } // namespace ns3