blob: f965fc780b7a21f9fede2b9ccb1af0aa2db2bea5 [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
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080088#define NDN_DEFAULT_DATA_SIZE 1024
89#define NDN_INTEREST_RESET_PERIOD Seconds(0.01)
90
91
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070092NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
93
94namespace ns3 {
95
96// Things are going to work differently here with respect to trace
97// file handling than in most places because the Tx and Rx trace
98// sources we are interested in are going to multiplex receive and
99// transmit callbacks for all Ccnx and face pairs through one
100// callback. We want packets to or from each distinct pair to go to
101// an individual file, so we have got to demultiplex the Ccnx and face
102// pair into a corresponding Ptr<PcapFileWrapper> at the callback.
103//
104// A complication in this situation is that the trace sources are
105// hooked on a protocol basis. There is no trace source hooked by an
106// Ccnx and face pair. This means that if we naively proceed to hook,
107// say, a drop trace for a given Ccnx with face 0, and then hook for
108// Ccnx with face 1 we will hook the drop trace twice and get two
109// callbacks per event. What we need to do is to hook the event once,
110// and that will result in a single callback per drop event, and the
111// trace source will provide the face which we filter on in the trace
112// sink.
113//
114// This has got to continue to work properly after the helper has been
115// destroyed; but must be cleaned up at the end of time to avoid
116// leaks. Global maps of protocol/face pairs to file objects seems to
117// fit the bill.
118//
119typedef std::pair<Ptr<Ccnx>, uint32_t> FacePairCcnx;
120typedef std::map<FacePairCcnx, Ptr<PcapFileWrapper> > FaceFileMapCcnx;
121typedef std::map<FacePairCcnx, Ptr<OutputStreamWrapper> > FaceStreamMapCcnx;
122
123static FaceFileMapCcnx g_faceFileMapCcnx; /**< A mapping of Ccnx/face pairs to pcap files */
124static FaceStreamMapCcnx g_faceStreamMapCcnx; /**< A mapping of Ccnx/face pairs to ascii streams */
125
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700126
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700127CcnxStackHelper::CcnxStackHelper ()
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700128 : m_forwardingHelper (Ccnx::NDN_FLOODING)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700129{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700130}
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700131
132CcnxStackHelper::CcnxStackHelper (Ccnx::ForwardingStrategy strategy)
133 : m_forwardingHelper (strategy)
134{
135}
136
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700137CcnxStackHelper::~CcnxStackHelper ()
138{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700139}
140
141CcnxStackHelper::CcnxStackHelper (const CcnxStackHelper &o)
142{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700143}
144
145CcnxStackHelper &
146CcnxStackHelper::operator = (const CcnxStackHelper &o)
147{
148 if (this == &o)
149 {
150 return *this;
151 }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700152 return *this;
153}
154
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700155void
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700156CcnxStackHelper::SetForwardingStrategy (Ccnx::ForwardingStrategy strategy)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700157{
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700158 CcnxForwardingHelper newForwardingHelper (strategy);
159 m_forwardingHelper = newForwardingHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700160}
161
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700162Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700163CcnxStackHelper::Install (NodeContainer c) const
164{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700165 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700166 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
167 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700168 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700169 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700170 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700171}
172
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700173Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700174CcnxStackHelper::InstallAll (void) const
175{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700176 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700177}
178
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700179// void
180// CcnxStackHelper::CreateAndAggregateObjectFromTypeId (Ptr<Node> node, const std::string typeId)
181// {
182// ObjectFactory factory;
183// factory.SetTypeId (typeId);
184// Ptr<Object> protocol = factory.Create <Object> ();
185// node->AggregateObject (protocol);
186// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700187
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700188Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700189CcnxStackHelper::Install (Ptr<Node> node) const
190{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700191 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
192 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700193
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700194 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700195 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
197 "a CcnxStack to a node with an existing Ccnx object");
198 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700199 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700200
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
202 node->AggregateObject (fib);
203
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700204 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700205 node->AggregateObject (ccnx);
206
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800207 NS_LOG_INFO("NODE->GetNDevices()=" << node->GetNDevices());
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800208
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209 for (uint32_t index=0; index < node->GetNDevices (); index++)
210 {
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800211 Ptr<PointToPointNetDevice> device = DynamicCast<PointToPointNetDevice>(node->GetDevice(index));
212 if(device == 0)
213 continue;
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700214
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800215 Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node, node->GetDevice (index));
216
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700217 uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face);
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700218 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id);
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700219 // Setup bucket filtering
220 // Assume that we know average data packet size, and this size is equal default size
221 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700222
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800223 // DataRateValue dataRate;
224 // device->GetAttribute ("DataRate", dataRate);
225 // NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
226 // pit->maxBucketsPerFace[face->GetId()] = 0.1 * dataRate.Get().GetBitRate () /(NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader));
227 // NS_LOG_INFO("maxBucketsPerFace["<<face->GetId()<<"] = " << pit->maxBucketsPerFace[face->GetId()]);
228 // pit->leakSize[face->GetId()] = 0.97 * NDN_INTEREST_RESET_PERIOD.ToDouble(Time::S) * dataRate.Get().GetBitRate () / (NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader));
229 // NS_LOG_INFO("pit->leakSize["<<face->GetId()<<"] = " << pit->leakSize[face->GetId()]);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700230
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800231 NS_LOG_INFO("Face #" << face_id << " is turned on");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700232 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233 faces->Add (face);
234 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700235
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800236 m_forwardingHelper.SetForwarding (ccnx);
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800237
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800238 // ccnx->ScheduleLeakage ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800239
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700240 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700241}
242
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700243Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700244CcnxStackHelper::Install (std::string nodeName) const
245{
246 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700247 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700248}
249
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700250
251void
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800252CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
253{
254 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
255
256 CcnxNameComponentsValue prefixValue;
257 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
258 fib->Add (prefixValue.Get (), face, metric);
259}
260
261void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700262CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
263{
264 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
265
266 Ptr<Node> node = Names::Find<Node> (nodeName);
267 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800268
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800269 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
270 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
271
272 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
273 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
274
275 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700276}
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700277/*
278void
279CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, uint32_t faceId, int32_t metric)
280{
281 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
282
283 NS_ASSERT_MSG (node != 0, "Node does not exist");
284
285 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
286 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
287 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
288 NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
289
290 CcnxNameComponentsValue prefixValue;
291 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
292 fib->Add (prefixValue.Get (), face, metric);
293}
294*/
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800295
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700296static void
297CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face)
298{
299 NS_LOG_FUNCTION (p << ccnx << face);
300
301 //
302 // Since trace sources are independent of face, if we hook a source
303 // on a particular protocol we will get traces for all of its faces.
304 // We need to filter this to only report faces for which the user
305 // has expressed interest.
306 //
307 FacePairCcnx pair = std::make_pair (ccnx, face);
308 if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ())
309 {
310 NS_LOG_INFO ("Ignoring packet to/from face " << face);
311 return;
312 }
313
314 Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair];
315 file->Write (Simulator::Now (), p);
316}
317
318bool
319CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx)
320{
321 for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin ();
322 i != g_faceFileMapCcnx.end ();
323 ++i)
324 {
325 if ((*i).first.first == ccnx)
326 {
327 return true;
328 }
329 }
330 return false;
331}
332
333void
334CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename)
335{
336 NS_LOG_FUNCTION (prefix << ccnx << face);
337
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700338 //
339 // We have to create a file and a mapping from protocol/face to file
340 // irrespective of how many times we want to trace a particular protocol.
341 //
342 PcapHelper pcapHelper;
343
344 std::string filename;
345 if (explicitFilename)
346 {
347 filename = prefix;
348 }
349 else
350 {
351 filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
352 }
353
354 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
355
356 //
357 // However, we only hook the trace source once to avoid multiple trace sink
358 // calls per event (connect is independent of face).
359 //
360 if (!PcapHooked (ccnx))
361 {
362 //
363 // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to
364 // node so we can get to CcnxL3Protocol through Ccnx.
365 //
366 Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
367 NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): "
368 "m_ccnxEnabled and ccnxL3Protocol inconsistent");
369
370 bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink));
371 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
372 "Unable to connect ccnxL3Protocol \"Tx\"");
373
374 result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink));
375 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
376 "Unable to connect ccnxL3Protocol \"Rx\"");
377 // cast result to void, to suppress ‘result’ set but not used compiler-warning
378 // for optimized builds
379 (void) result;
380 }
381
382 g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file;
383}
384
385static void
386CcnxL3ProtocolDropSinkWithoutContext (
387 Ptr<OutputStreamWrapper> stream,
388 Ptr<const Packet> packet,
389 CcnxL3Protocol::DropReason reason,
390 Ptr<Ccnx> ccnx,
391 uint32_t face)
392{
393 //
394 // Since trace sources are independent of face, if we hook a source
395 // on a particular protocol we will get traces for all of its faces.
396 // We need to filter this to only report faces for which the user
397 // has expressed interest.
398 //
399 FacePairCcnx pair = std::make_pair (ccnx, face);
400 if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
401 {
402 NS_LOG_INFO ("Ignoring packet to/from face " << face);
403 return;
404 }
405
406 *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl;
407}
408
409static void
410CcnxL3ProtocolDropSinkWithContext (
411 Ptr<OutputStreamWrapper> stream,
412 std::string context,
413 Ptr<const Packet> packet,
414 CcnxL3Protocol::DropReason reason,
415 Ptr<Ccnx> ccnx,
416 uint32_t face)
417{
418 //
419 // Since trace sources are independent of face, if we hook a source
420 // on a particular protocol we will get traces for all of its faces.
421 // We need to filter this to only report faces for which the user
422 // has expressed interest.
423 //
424 FacePairCcnx pair = std::make_pair (ccnx, face);
425 if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
426 {
427 NS_LOG_INFO ("Ignoring packet to/from face " << face);
428 return;
429 }
430
431 *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") "
432 << *packet << std::endl;
433}
434
435bool
436CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx)
437{
438 for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin ();
439 i != g_faceStreamMapCcnx.end ();
440 ++i)
441 {
442 if ((*i).first.first == ccnx)
443 {
444 return true;
445 }
446 }
447 return false;
448}
449
450void
451CcnxStackHelper::EnableAsciiCcnxInternal (
452 Ptr<OutputStreamWrapper> stream,
453 std::string prefix,
454 Ptr<Ccnx> ccnx,
455 uint32_t face,
456 bool explicitFilename)
457{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700458 //
459 // Our trace sinks are going to use packet printing, so we have to
460 // make sure that is turned on.
461 //
462 Packet::EnablePrinting ();
463
464 //
465 // If we are not provided an OutputStreamWrapper, we are expected to create
466 // one using the usual trace filename conventions and hook WithoutContext
467 // since there will be one file per context and therefore the context would
468 // be redundant.
469 //
470 if (stream == 0)
471 {
472 //
473 // Set up an output stream object to deal with private ofstream copy
474 // constructor and lifetime issues. Let the helper decide the actual
475 // name of the file given the prefix.
476 //
477 // We have to create a stream and a mapping from protocol/face to
478 // stream irrespective of how many times we want to trace a particular
479 // protocol.
480 //
481 AsciiTraceHelper asciiTraceHelper;
482
483 std::string filename;
484 if (explicitFilename)
485 {
486 filename = prefix;
487 }
488 else
489 {
490 filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
491 }
492
493 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
494
495 //
496 // However, we only hook the trace sources once to avoid multiple trace sink
497 // calls per event (connect is independent of face).
498 //
499 if (!AsciiHooked (ccnx))
500 {
501 //
502 // The drop sink for the CcnxL3Protocol uses a different signature than
503 // the default sink, so we have to cook one up for ourselves. We can get
504 // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both
505 // be aggregated to the same node.
506 //
507 Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
508 bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop",
509 MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream));
510 NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): "
511 "Unable to connect ccnxL3Protocol \"Drop\"");
512 }
513
514 g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream;
515 return;
516 }
517
518 //
519 // If we are provided an OutputStreamWrapper, we are expected to use it, and
520 // to provide a context. We are free to come up with our own context if we
521 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
522 // compatibility and simplicity, we just use Config::Connect and let it deal
523 // with the context.
524 //
525 // We need to associate the ccnx/face with a stream to express interest
526 // in tracing events on that pair, however, we only hook the trace sources
527 // once to avoid multiple trace sink calls per event (connect is independent
528 // of face).
529 //
530 if (!AsciiHooked (ccnx))
531 {
532 Ptr<Node> node = ccnx->GetObject<Node> ();
533 std::ostringstream oss;
534
535 //
536 // This has all kinds of parameters coming with, so we have to cook up our
537 // own sink.
538 //
539 oss.str ("");
540 oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop";
541 Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream));
542 }
543
544 g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream;
545}
546
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800547void
548CcnxStackHelper::InstallFakeGlobalRoutes ()
549{
550 for (NodeList::Iterator node = NodeList::Begin ();
551 node != NodeList::End ();
552 node ++)
553 {
554 NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0,
555 "InternetStack should be installed on all nodes");
556
557 NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops>
558 (
559 (*node)->GetObject<Ipv4> ()->GetRoutingProtocol ()
560 ),
561 "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol");
562 // Example:
563 //
564 // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops");
565 // stack.SetRoutingHelper (ipv4RoutingHelper);
566 //
567
568 Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> ();
569 if (globalRouter == 0) continue;
570
571 globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255"));
572 }
573
574 Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables ();
575}
576
577void
578CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode)
579{
580 std::ostringstream destPrefix;
581 destPrefix << "/" << destNode->GetId ();
582
583 Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ());
584
585 for (NodeList::Iterator node = NodeList::Begin ();
586 node != NodeList::End ();
587 node ++)
588 {
589 if (destNode == *node) continue;
590
591 Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> ();
592 NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes");
593
594 Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> ();
595 NS_ASSERT_MSG (ipv4 != 0,
596 "InternetStack should be installed on all nodes");
597
598 Ptr<Ipv4GlobalRoutingOrderedNexthops> routing =
599 Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ());
600 NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack");
601
602 Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
603 routes = routing->Lookup (destIpv4);
604
605 NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer");
606
607 BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes)
608 {
609 Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ());
610 NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos");
611
612 Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice);
613 NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president");
614
615 AddRoute (*node, destPrefix.str(), face, entry.GetMetric ());
616 }
617 }
618}
619
620void
621CcnxStackHelper::InstallRoutesToAll ()
622{
623 for (NodeList::Iterator node = NodeList::Begin ();
624 node != NodeList::End ();
625 node ++)
626 {
627 InstallRouteTo (*node);
628 }
629}
630
631
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700632} // namespace ns3