blob: 7215c63084be0f0a1a835dd3eba08b66060f20a0 [file] [log] [blame]
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev45b92d42011-08-14 23:11:38 -07002/*
3 * Copyright (c) 2011 UCLA
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 *
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070020 */
21
22/**
23 * \ingroup ccnx
24 * \defgroup CcnxStackModel Ccnx Stack Model
25 *
26 * \section CcnxStackTracingModel Tracing in the Ccnx Stack
27 *
28 * The ccnx stack provides a number of trace sources in its various
29 * protocol implementations. These trace sources can be hooked using your own
30 * custom trace code, or you can use our helper functions in some cases to
31 * arrange for tracing to be enabled.
32 *
33 * \subsection CcnxStackCcnxTracingModel Tracing in Ccnx
34 *
35 * The Ccnx layer three protocol provides three trace hooks. These are the
36 * "Tx" (ns3::CcnxL3Protocol::m_txTrace), "Rx" (ns3::CcnxL3Protocol::m_rxTrace)
37 * and "Drop" (ns3::CcnxL3Protocol::m_dropTrace) trace sources.
38 *
39 * The "Tx" trace is fired in a number of situations, all of which indicate that
40 * a given packet is about to be sent down to a given ns3::CcnxFace.
41 *
42 * - \todo list Tx trace events
43 *
44 * The "Rx" trace is fired when a packet is passed from the device up to the
45 * ns3::CcnxL3Protocol::Receive function.
46 *
47 * - In the receive function, the CcnxFaceList is iterated, and if the
48 * CcnxFace corresponding to the receiving device is found to be in the
49 * UP state, the trace is fired.
50 *
51 * The "Drop" trace is fired in any case where the packet is dropped (in both
52 * the transmit and receive paths).
53 *
54 * - \todo list Drop trace events
55 */
56
57#include "ns3/assert.h"
58#include "ns3/log.h"
59#include "ns3/object.h"
60#include "ns3/names.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070061#include "ns3/packet-socket-factory.h"
62#include "ns3/config.h"
63#include "ns3/simulator.h"
64#include "ns3/string.h"
65#include "ns3/net-device.h"
66#include "ns3/callback.h"
67#include "ns3/node.h"
68#include "ns3/core-config.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070069#include "ns3/ccnx-forwarding-strategy.h"
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070070#include "ns3/ccnx-net-device-face.h"
71#include "ns3/ccnx-l3-protocol.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070072#include "ns3/ccnx-fib.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080073#include "ns3/node-list.h"
74#include "ns3/ipv4.h"
75#include "ns3/ipv4-routing-helper.h"
76#include "ns3/ipv4-global-routing-ordered-nexthops.h"
77#include "ns3/global-router-interface.h"
78#include "ns3/ipv4-global-routing-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070079
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070080#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070081#include "ccnx-stack-helper.h"
82#include "ccnx-forwarding-helper.h"
83
84#include <limits>
85#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080086#include <boost/foreach.hpp>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070087
88NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
89
90namespace ns3 {
91
92// Things are going to work differently here with respect to trace
93// file handling than in most places because the Tx and Rx trace
94// sources we are interested in are going to multiplex receive and
95// transmit callbacks for all Ccnx and face pairs through one
96// callback. We want packets to or from each distinct pair to go to
97// an individual file, so we have got to demultiplex the Ccnx and face
98// pair into a corresponding Ptr<PcapFileWrapper> at the callback.
99//
100// A complication in this situation is that the trace sources are
101// hooked on a protocol basis. There is no trace source hooked by an
102// Ccnx and face pair. This means that if we naively proceed to hook,
103// say, a drop trace for a given Ccnx with face 0, and then hook for
104// Ccnx with face 1 we will hook the drop trace twice and get two
105// callbacks per event. What we need to do is to hook the event once,
106// and that will result in a single callback per drop event, and the
107// trace source will provide the face which we filter on in the trace
108// sink.
109//
110// This has got to continue to work properly after the helper has been
111// destroyed; but must be cleaned up at the end of time to avoid
112// leaks. Global maps of protocol/face pairs to file objects seems to
113// fit the bill.
114//
115typedef std::pair<Ptr<Ccnx>, uint32_t> FacePairCcnx;
116typedef std::map<FacePairCcnx, Ptr<PcapFileWrapper> > FaceFileMapCcnx;
117typedef std::map<FacePairCcnx, Ptr<OutputStreamWrapper> > FaceStreamMapCcnx;
118
119static FaceFileMapCcnx g_faceFileMapCcnx; /**< A mapping of Ccnx/face pairs to pcap files */
120static FaceStreamMapCcnx g_faceStreamMapCcnx; /**< A mapping of Ccnx/face pairs to ascii streams */
121
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700122
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700123CcnxStackHelper::CcnxStackHelper ()
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700124 : m_forwardingHelper (Ccnx::NDN_FLOODING)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700125{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126}
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700127
128CcnxStackHelper::CcnxStackHelper (Ccnx::ForwardingStrategy strategy)
129 : m_forwardingHelper (strategy)
130{
131}
132
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700133CcnxStackHelper::~CcnxStackHelper ()
134{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700135}
136
137CcnxStackHelper::CcnxStackHelper (const CcnxStackHelper &o)
138{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700139}
140
141CcnxStackHelper &
142CcnxStackHelper::operator = (const CcnxStackHelper &o)
143{
144 if (this == &o)
145 {
146 return *this;
147 }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700148 return *this;
149}
150
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700151void
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700152CcnxStackHelper::SetForwardingStrategy (Ccnx::ForwardingStrategy strategy)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700153{
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700154 CcnxForwardingHelper newForwardingHelper (strategy);
155 m_forwardingHelper = newForwardingHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700156}
157
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700158Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700159CcnxStackHelper::Install (NodeContainer c) const
160{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700161 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700162 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
163 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700164 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700165 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700166 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700167}
168
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700169Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700170CcnxStackHelper::InstallAll (void) const
171{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700172 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700173}
174
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700175// void
176// CcnxStackHelper::CreateAndAggregateObjectFromTypeId (Ptr<Node> node, const std::string typeId)
177// {
178// ObjectFactory factory;
179// factory.SetTypeId (typeId);
180// Ptr<Object> protocol = factory.Create <Object> ();
181// node->AggregateObject (protocol);
182// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700183
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700184Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700185CcnxStackHelper::Install (Ptr<Node> node) const
186{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700187 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
188 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700189
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700190 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700191 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
193 "a CcnxStack to a node with an existing Ccnx object");
194 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700195 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700196
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700197 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
198 node->AggregateObject (fib);
199
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700200 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201 node->AggregateObject (ccnx);
202
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700203 Ptr<CcnxPit> pit = ccnx->GetPit();
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800204 NS_LOG_INFO("NODE->GetNDevices()="<<node->GetNDevices());
205
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700206 for (uint32_t index=0; index < node->GetNDevices (); index++)
207 {
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800208 Ptr<PointToPointNetDevice> device = DynamicCast<PointToPointNetDevice>(node->GetDevice(index));
209 if(device == 0)
210 continue;
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700211
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700212 Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node->GetDevice (index));
213 face->SetNode (node);
214 uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face);
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700215 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id);
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700216 // Setup bucket filtering
217 // Assume that we know average data packet size, and this size is equal default size
218 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700219
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700220 DataRateValue dataRate;
221 device->GetAttribute ("DataRate", dataRate);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700222 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
223 pit->maxBucketsPerFace[face->GetId()] = 0.1 * dataRate.Get().GetBitRate () / 8 /(NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader));
224 NS_LOG_INFO("maxBucketsPerFace["<<face->GetId()<<"] = " << pit->maxBucketsPerFace[face->GetId()]);
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700225 pit->leakSize[face->GetId()] = 0.97 * NDN_INTEREST_RESET_PERIOD / SECOND * dataRate.Get().GetBitRate () / 8 / (NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader));
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700226 NS_LOG_INFO("pit->leakSize["<<face->GetId()<<"] = " << pit->leakSize[face->GetId()]);
227
228
229 if(face->IsLocal()==true)
230 NS_LOG_INFO("Face #" << face_id << " is turned on");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700231 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700232 faces->Add (face);
233 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700234
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700235 m_forwardingHelper.SetForwarding (ccnx, pit);
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800236
237 ccnx->ScheduleLeakage ();
238
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700239 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700240}
241
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700242Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700243CcnxStackHelper::Install (std::string nodeName) const
244{
245 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700246 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700247}
248
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249
250void
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800251CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
252{
253 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
254
255 CcnxNameComponentsValue prefixValue;
256 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
257 fib->Add (prefixValue.Get (), face, metric);
258}
259
260void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700261CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
262{
263 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
264
265 Ptr<Node> node = Names::Find<Node> (nodeName);
266 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800267
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800268 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
269 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
270
271 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
272 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
273
274 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700275}
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700276/*
277void
278CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, uint32_t faceId, int32_t metric)
279{
280 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
281
282 NS_ASSERT_MSG (node != 0, "Node does not exist");
283
284 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
285 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
286 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
287 NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
288
289 CcnxNameComponentsValue prefixValue;
290 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
291 fib->Add (prefixValue.Get (), face, metric);
292}
293*/
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800294
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700295static void
296CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face)
297{
298 NS_LOG_FUNCTION (p << ccnx << face);
299
300 //
301 // Since trace sources are independent of face, if we hook a source
302 // on a particular protocol we will get traces for all of its faces.
303 // We need to filter this to only report faces for which the user
304 // has expressed interest.
305 //
306 FacePairCcnx pair = std::make_pair (ccnx, face);
307 if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ())
308 {
309 NS_LOG_INFO ("Ignoring packet to/from face " << face);
310 return;
311 }
312
313 Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair];
314 file->Write (Simulator::Now (), p);
315}
316
317bool
318CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx)
319{
320 for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin ();
321 i != g_faceFileMapCcnx.end ();
322 ++i)
323 {
324 if ((*i).first.first == ccnx)
325 {
326 return true;
327 }
328 }
329 return false;
330}
331
332void
333CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename)
334{
335 NS_LOG_FUNCTION (prefix << ccnx << face);
336
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700337 //
338 // We have to create a file and a mapping from protocol/face to file
339 // irrespective of how many times we want to trace a particular protocol.
340 //
341 PcapHelper pcapHelper;
342
343 std::string filename;
344 if (explicitFilename)
345 {
346 filename = prefix;
347 }
348 else
349 {
350 filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
351 }
352
353 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
354
355 //
356 // However, we only hook the trace source once to avoid multiple trace sink
357 // calls per event (connect is independent of face).
358 //
359 if (!PcapHooked (ccnx))
360 {
361 //
362 // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to
363 // node so we can get to CcnxL3Protocol through Ccnx.
364 //
365 Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
366 NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): "
367 "m_ccnxEnabled and ccnxL3Protocol inconsistent");
368
369 bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink));
370 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
371 "Unable to connect ccnxL3Protocol \"Tx\"");
372
373 result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink));
374 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
375 "Unable to connect ccnxL3Protocol \"Rx\"");
376 // cast result to void, to suppress ‘result’ set but not used compiler-warning
377 // for optimized builds
378 (void) result;
379 }
380
381 g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file;
382}
383
384static void
385CcnxL3ProtocolDropSinkWithoutContext (
386 Ptr<OutputStreamWrapper> stream,
387 Ptr<const Packet> packet,
388 CcnxL3Protocol::DropReason reason,
389 Ptr<Ccnx> ccnx,
390 uint32_t face)
391{
392 //
393 // Since trace sources are independent of face, if we hook a source
394 // on a particular protocol we will get traces for all of its faces.
395 // We need to filter this to only report faces for which the user
396 // has expressed interest.
397 //
398 FacePairCcnx pair = std::make_pair (ccnx, face);
399 if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
400 {
401 NS_LOG_INFO ("Ignoring packet to/from face " << face);
402 return;
403 }
404
405 *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl;
406}
407
408static void
409CcnxL3ProtocolDropSinkWithContext (
410 Ptr<OutputStreamWrapper> stream,
411 std::string context,
412 Ptr<const Packet> packet,
413 CcnxL3Protocol::DropReason reason,
414 Ptr<Ccnx> ccnx,
415 uint32_t face)
416{
417 //
418 // Since trace sources are independent of face, if we hook a source
419 // on a particular protocol we will get traces for all of its faces.
420 // We need to filter this to only report faces for which the user
421 // has expressed interest.
422 //
423 FacePairCcnx pair = std::make_pair (ccnx, face);
424 if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
425 {
426 NS_LOG_INFO ("Ignoring packet to/from face " << face);
427 return;
428 }
429
430 *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") "
431 << *packet << std::endl;
432}
433
434bool
435CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx)
436{
437 for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin ();
438 i != g_faceStreamMapCcnx.end ();
439 ++i)
440 {
441 if ((*i).first.first == ccnx)
442 {
443 return true;
444 }
445 }
446 return false;
447}
448
449void
450CcnxStackHelper::EnableAsciiCcnxInternal (
451 Ptr<OutputStreamWrapper> stream,
452 std::string prefix,
453 Ptr<Ccnx> ccnx,
454 uint32_t face,
455 bool explicitFilename)
456{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700457 //
458 // Our trace sinks are going to use packet printing, so we have to
459 // make sure that is turned on.
460 //
461 Packet::EnablePrinting ();
462
463 //
464 // If we are not provided an OutputStreamWrapper, we are expected to create
465 // one using the usual trace filename conventions and hook WithoutContext
466 // since there will be one file per context and therefore the context would
467 // be redundant.
468 //
469 if (stream == 0)
470 {
471 //
472 // Set up an output stream object to deal with private ofstream copy
473 // constructor and lifetime issues. Let the helper decide the actual
474 // name of the file given the prefix.
475 //
476 // We have to create a stream and a mapping from protocol/face to
477 // stream irrespective of how many times we want to trace a particular
478 // protocol.
479 //
480 AsciiTraceHelper asciiTraceHelper;
481
482 std::string filename;
483 if (explicitFilename)
484 {
485 filename = prefix;
486 }
487 else
488 {
489 filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
490 }
491
492 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
493
494 //
495 // However, we only hook the trace sources once to avoid multiple trace sink
496 // calls per event (connect is independent of face).
497 //
498 if (!AsciiHooked (ccnx))
499 {
500 //
501 // The drop sink for the CcnxL3Protocol uses a different signature than
502 // the default sink, so we have to cook one up for ourselves. We can get
503 // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both
504 // be aggregated to the same node.
505 //
506 Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
507 bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop",
508 MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream));
509 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): "
510 "Unable to connect ccnxL3Protocol \"Drop\"");
511 }
512
513 g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream;
514 return;
515 }
516
517 //
518 // If we are provided an OutputStreamWrapper, we are expected to use it, and
519 // to provide a context. We are free to come up with our own context if we
520 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
521 // compatibility and simplicity, we just use Config::Connect and let it deal
522 // with the context.
523 //
524 // We need to associate the ccnx/face with a stream to express interest
525 // in tracing events on that pair, however, we only hook the trace sources
526 // once to avoid multiple trace sink calls per event (connect is independent
527 // of face).
528 //
529 if (!AsciiHooked (ccnx))
530 {
531 Ptr<Node> node = ccnx->GetObject<Node> ();
532 std::ostringstream oss;
533
534 //
535 // This has all kinds of parameters coming with, so we have to cook up our
536 // own sink.
537 //
538 oss.str ("");
539 oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop";
540 Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream));
541 }
542
543 g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream;
544}
545
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800546void
547CcnxStackHelper::InstallFakeGlobalRoutes ()
548{
549 for (NodeList::Iterator node = NodeList::Begin ();
550 node != NodeList::End ();
551 node ++)
552 {
553 NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0,
554 "InternetStack should be installed on all nodes");
555
556 NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops>
557 (
558 (*node)->GetObject<Ipv4> ()->GetRoutingProtocol ()
559 ),
560 "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol");
561 // Example:
562 //
563 // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops");
564 // stack.SetRoutingHelper (ipv4RoutingHelper);
565 //
566
567 Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> ();
568 if (globalRouter == 0) continue;
569
570 globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255"));
571 }
572
573 Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables ();
574}
575
576void
577CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode)
578{
579 std::ostringstream destPrefix;
580 destPrefix << "/" << destNode->GetId ();
581
582 Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ());
583
584 for (NodeList::Iterator node = NodeList::Begin ();
585 node != NodeList::End ();
586 node ++)
587 {
588 if (destNode == *node) continue;
589
590 Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> ();
591 NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes");
592
593 Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> ();
594 NS_ASSERT_MSG (ipv4 != 0,
595 "InternetStack should be installed on all nodes");
596
597 Ptr<Ipv4GlobalRoutingOrderedNexthops> routing =
598 Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ());
599 NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack");
600
601 Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
602 routes = routing->Lookup (destIpv4);
603
604 NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer");
605
606 BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes)
607 {
608 Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ());
609 NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos");
610
611 Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice);
612 NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president");
613
614 AddRoute (*node, destPrefix.str(), face, entry.GetMetric ());
615 }
616 }
617}
618
619void
620CcnxStackHelper::InstallRoutesToAll ()
621{
622 for (NodeList::Iterator node = NodeList::Begin ();
623 node != NodeList::End ();
624 node ++)
625 {
626 InstallRouteTo (*node);
627 }
628}
629
630
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700631} // namespace ns3