Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a useless element in the NS3 extension of hosts
[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 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
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, e_ns3_network_element_type_t type_a, Ptr<Node> b, e_ns3_network_element_type_t type_b)
250 {
251   NetDeviceContainer container;
252   Ptr<Queue> queueA;
253   Ptr<Queue> queueB;
254
255   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
256   devA->SetAddress (Mac48Address::Allocate ());
257   a->AddDevice (devA);
258
259   if(type_a == NS3_NETWORK_ELEMENT_ROUTER){
260         queueA = m_queueFactory_red.Create<Queue> ();
261   }
262   else
263           queueA = m_queueFactory.Create<Queue> ();
264   devA->SetQueue (queueA);
265
266   Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
267   devB->SetAddress (Mac48Address::Allocate ());
268   b->AddDevice (devB);
269
270   if(type_b == NS3_NETWORK_ELEMENT_ROUTER){
271         queueB = m_queueFactory_red.Create<Queue> ();
272   }
273   else
274           queueB = m_queueFactory.Create<Queue> ();
275   devB->SetQueue (queueB);
276
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 ())
283     {
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;
289     }
290   if (useNormalChannel)
291     {
292       channel = m_channelFactory.Create<PointToPointChannel> ();
293     }
294   else
295     {
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);
303     }
304
305   devA->Attach (channel);
306   devB->Attach (channel);
307   container.Add (devA);
308   container.Add (devB);
309
310   return container;
311 }
312
313 NetDeviceContainer 
314 MyPointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
315 {
316   NetDeviceContainer container;
317
318   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
319   devA->SetAddress (Mac48Address::Allocate ());
320   a->AddDevice (devA);
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 ());
325   b->AddDevice (devB);
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 ())
334     {
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;
340     }
341   if (useNormalChannel)
342     {
343       channel = m_channelFactory.Create<PointToPointChannel> ();
344     }
345   else
346     {
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);
354     }
355
356   devA->Attach (channel);
357   devB->Attach (channel);
358   container.Add (devA);
359   container.Add (devB);
360
361   return container;
362 }
363
364 NetDeviceContainer
365 MyPointToPointHelper::Install (Ptr<Node> a, std::string bName)
366 {
367   Ptr<Node> b = Names::Find<Node> (bName);
368   return Install (a, b);
369 }
370
371 NetDeviceContainer 
372 MyPointToPointHelper::Install (std::string aName, Ptr<Node> b)
373 {
374   Ptr<Node> a = Names::Find<Node> (aName);
375   return Install (a, b);
376 }
377
378 NetDeviceContainer 
379 MyPointToPointHelper::Install (std::string aName, std::string bName)
380 {
381   Ptr<Node> a = Names::Find<Node> (aName);
382   Ptr<Node> b = Names::Find<Node> (bName);
383   return Install (a, b);
384 }
385
386 } // namespace ns3