blob: 234a64cd778c810444a2986824c469408c3fb33a [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"
Alexander Afanasyev11453142011-11-25 16:13:33 -080074#include "ns3/loopback-net-device.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080075#include "ns3/global-router-interface.h"
Alexander Afanasyev11453142011-11-25 16:13:33 -080076#include "ns3/ipv4.h"
77#include "ns3/ipv4-global-routing.h"
78#include "ns3/ipv4-global-routing-ordered-nexthops.h"
79#include "ns3/ipv4-routing-helper.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080080#include "ns3/ipv4-global-routing-helper.h"
Alexander Afanasyev11453142011-11-25 16:13:33 -080081#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070082
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070083#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070084#include "ccnx-stack-helper.h"
85#include "ccnx-forwarding-helper.h"
86
87#include <limits>
88#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080089#include <boost/foreach.hpp>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070090
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080091#define NDN_DEFAULT_DATA_SIZE 1024
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080092
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070093NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
94
95namespace ns3 {
96
97// Things are going to work differently here with respect to trace
98// file handling than in most places because the Tx and Rx trace
99// sources we are interested in are going to multiplex receive and
100// transmit callbacks for all Ccnx and face pairs through one
101// callback. We want packets to or from each distinct pair to go to
102// an individual file, so we have got to demultiplex the Ccnx and face
103// pair into a corresponding Ptr<PcapFileWrapper> at the callback.
104//
105// A complication in this situation is that the trace sources are
106// hooked on a protocol basis. There is no trace source hooked by an
107// Ccnx and face pair. This means that if we naively proceed to hook,
108// say, a drop trace for a given Ccnx with face 0, and then hook for
109// Ccnx with face 1 we will hook the drop trace twice and get two
110// callbacks per event. What we need to do is to hook the event once,
111// and that will result in a single callback per drop event, and the
112// trace source will provide the face which we filter on in the trace
113// sink.
114//
115// This has got to continue to work properly after the helper has been
116// destroyed; but must be cleaned up at the end of time to avoid
117// leaks. Global maps of protocol/face pairs to file objects seems to
118// fit the bill.
119//
Alexander Afanasyev11453142011-11-25 16:13:33 -0800120// typedef std::pair<Ptr<Ccnx>, uint32_t> FacePairCcnx;
121// typedef std::map<FacePairCcnx, Ptr<PcapFileWrapper> > FaceFileMapCcnx;
122// typedef std::map<FacePairCcnx, Ptr<OutputStreamWrapper> > FaceStreamMapCcnx;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700123
Alexander Afanasyev11453142011-11-25 16:13:33 -0800124// static FaceFileMapCcnx g_faceFileMapCcnx; /**< A mapping of Ccnx/face pairs to pcap files */
125// static FaceStreamMapCcnx g_faceStreamMapCcnx; /**< A mapping of Ccnx/face pairs to ascii streams */
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700127
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700128CcnxStackHelper::CcnxStackHelper ()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700129{
Alexander Afanasyev11453142011-11-25 16:13:33 -0800130 m_strategyFactory.SetTypeId ("ns3::CcnxFloodingStrategy");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700131}
132
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700133CcnxStackHelper::~CcnxStackHelper ()
134{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700135}
136
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700137void
Alexander Afanasyev11453142011-11-25 16:13:33 -0800138CcnxStackHelper::SetForwardingStrategy (std::string strategy)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700139{
Alexander Afanasyev11453142011-11-25 16:13:33 -0800140 m_strategyFactory.SetTypeId (strategy);
141}
142
143void
144CcnxStackHelper::EnableLimits (bool enable/* = true*/)
145{
146 m_limitsEnabled = enable;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700147}
148
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700149Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700150CcnxStackHelper::Install (NodeContainer c) const
151{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700152 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700153 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
154 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700155 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700156 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700157 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700158}
159
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700160Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700161CcnxStackHelper::InstallAll (void) const
162{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700163 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700164}
165
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700166Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700167CcnxStackHelper::Install (Ptr<Node> node) const
168{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700169 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
170 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700171
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700173 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
175 "a CcnxStack to a node with an existing Ccnx object");
176 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700177 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700178
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700179 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
180 node->AggregateObject (fib);
181
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700182 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183 node->AggregateObject (ccnx);
184
Alexander Afanasyev11453142011-11-25 16:13:33 -0800185 ccnx->SetForwardingStrategy
186 (DynamicCast<CcnxForwardingStrategy> (m_strategyFactory.Create<Object> ()));
187
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700188 for (uint32_t index=0; index < node->GetNDevices (); index++)
189 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800190 Ptr<NetDevice> device = node->GetDevice (index);
191 if (DynamicCast<LoopbackNetDevice> (device) != 0)
192 continue; // don't create face for a LoopbackNetDevice
193
194 Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800195
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face);
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700197 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800198
199 if (m_limitsEnabled)
200 {
201 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
202 if (p2p == 0)
203 continue; // only PointToPointNetDevice supports limits
204
205 // Setup bucket filtering
206 // Assume that we know average data packet size, and this size is equal default size
207 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700208
Alexander Afanasyev11453142011-11-25 16:13:33 -0800209 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
210
211 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
212 face->SetBucketMax
213 (0.1 * dataRate.Get().GetBitRate () / (NDN_DEFAULT_DATA_SIZE + sizeof (CcnxInterestHeader)));
214
215 face->SetBucketLeak
216 (0.97 * dataRate.Get().GetBitRate () / (NDN_DEFAULT_DATA_SIZE + sizeof (CcnxInterestHeader)));
217 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700218
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700219 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700220 faces->Add (face);
221 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700222
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700223 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700224}
225
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700226Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700227CcnxStackHelper::Install (std::string nodeName) const
228{
229 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700230 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700231}
232
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233
234void
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800235CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
236{
237 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
238
239 CcnxNameComponentsValue prefixValue;
240 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
241 fib->Add (prefixValue.Get (), face, metric);
242}
243
244void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700245CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
246{
247 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
248
249 Ptr<Node> node = Names::Find<Node> (nodeName);
250 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800251
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800252 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
253 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
254
255 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
256 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
257
258 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700259}
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700260/*
Alexander Afanasyev11453142011-11-25 16:13:33 -0800261 void
262 CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, uint32_t faceId, int32_t metric)
263 {
264 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700265
266 NS_ASSERT_MSG (node != 0, "Node does not exist");
267
268 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
269 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
270 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
271 NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
272
273 CcnxNameComponentsValue prefixValue;
274 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
275 fib->Add (prefixValue.Get (), face, metric);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800276 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700277*/
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800278
Alexander Afanasyev11453142011-11-25 16:13:33 -0800279// static void
280// CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face)
281// {
282// NS_LOG_FUNCTION (p << ccnx << face);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700283
Alexander Afanasyev11453142011-11-25 16:13:33 -0800284// //
285// // Since trace sources are independent of face, if we hook a source
286// // on a particular protocol we will get traces for all of its faces.
287// // We need to filter this to only report faces for which the user
288// // has expressed interest.
289// //
290// FacePairCcnx pair = std::make_pair (ccnx, face);
291// if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ())
292// {
293// NS_LOG_INFO ("Ignoring packet to/from face " << face);
294// return;
295// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700296
Alexander Afanasyev11453142011-11-25 16:13:33 -0800297// Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair];
298// file->Write (Simulator::Now (), p);
299// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700300
Alexander Afanasyev11453142011-11-25 16:13:33 -0800301// bool
302// CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx)
303// {
304// for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin ();
305// i != g_faceFileMapCcnx.end ();
306// ++i)
307// {
308// if ((*i).first.first == ccnx)
309// {
310// return true;
311// }
312// }
313// return false;
314// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700315
Alexander Afanasyev11453142011-11-25 16:13:33 -0800316// void
317// CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename)
318// {
319// NS_LOG_FUNCTION (prefix << ccnx << face);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700320
Alexander Afanasyev11453142011-11-25 16:13:33 -0800321// //
322// // We have to create a file and a mapping from protocol/face to file
323// // irrespective of how many times we want to trace a particular protocol.
324// //
325// PcapHelper pcapHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700326
Alexander Afanasyev11453142011-11-25 16:13:33 -0800327// std::string filename;
328// if (explicitFilename)
329// {
330// filename = prefix;
331// }
332// else
333// {
334// filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
335// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700336
Alexander Afanasyev11453142011-11-25 16:13:33 -0800337// Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700338
Alexander Afanasyev11453142011-11-25 16:13:33 -0800339// //
340// // However, we only hook the trace source once to avoid multiple trace sink
341// // calls per event (connect is independent of face).
342// //
343// if (!PcapHooked (ccnx))
344// {
345// //
346// // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to
347// // node so we can get to CcnxL3Protocol through Ccnx.
348// //
349// Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
350// NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): "
351// "m_ccnxEnabled and ccnxL3Protocol inconsistent");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700352
Alexander Afanasyev11453142011-11-25 16:13:33 -0800353// bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink));
354// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
355// "Unable to connect ccnxL3Protocol \"Tx\"");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700356
Alexander Afanasyev11453142011-11-25 16:13:33 -0800357// result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink));
358// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
359// "Unable to connect ccnxL3Protocol \"Rx\"");
360// // cast result to void, to suppress ‘result’ set but not used compiler-warning
361// // for optimized builds
362// (void) result;
363// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700364
Alexander Afanasyev11453142011-11-25 16:13:33 -0800365// g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file;
366// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700367
Alexander Afanasyev11453142011-11-25 16:13:33 -0800368// static void
369// CcnxL3ProtocolDropSinkWithoutContext (
370// Ptr<OutputStreamWrapper> stream,
371// Ptr<const Packet> packet,
372// CcnxL3Protocol::DropReason reason,
373// Ptr<Ccnx> ccnx,
374// uint32_t face)
375// {
376// //
377// // Since trace sources are independent of face, if we hook a source
378// // on a particular protocol we will get traces for all of its faces.
379// // We need to filter this to only report faces for which the user
380// // has expressed interest.
381// //
382// FacePairCcnx pair = std::make_pair (ccnx, face);
383// if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
384// {
385// NS_LOG_INFO ("Ignoring packet to/from face " << face);
386// return;
387// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700388
Alexander Afanasyev11453142011-11-25 16:13:33 -0800389// *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl;
390// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700391
Alexander Afanasyev11453142011-11-25 16:13:33 -0800392// static void
393// CcnxL3ProtocolDropSinkWithContext (
394// Ptr<OutputStreamWrapper> stream,
395// std::string context,
396// Ptr<const Packet> packet,
397// CcnxL3Protocol::DropReason reason,
398// Ptr<Ccnx> ccnx,
399// uint32_t face)
400// {
401// //
402// // Since trace sources are independent of face, if we hook a source
403// // on a particular protocol we will get traces for all of its faces.
404// // We need to filter this to only report faces for which the user
405// // has expressed interest.
406// //
407// FacePairCcnx pair = std::make_pair (ccnx, face);
408// if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
409// {
410// NS_LOG_INFO ("Ignoring packet to/from face " << face);
411// return;
412// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700413
Alexander Afanasyev11453142011-11-25 16:13:33 -0800414// *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") "
415// << *packet << std::endl;
416// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700417
Alexander Afanasyev11453142011-11-25 16:13:33 -0800418// bool
419// CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx)
420// {
421// for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin ();
422// i != g_faceStreamMapCcnx.end ();
423// ++i)
424// {
425// if ((*i).first.first == ccnx)
426// {
427// return true;
428// }
429// }
430// return false;
431// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700432
Alexander Afanasyev11453142011-11-25 16:13:33 -0800433// void
434// CcnxStackHelper::EnableAsciiCcnxInternal (
435// Ptr<OutputStreamWrapper> stream,
436// std::string prefix,
437// Ptr<Ccnx> ccnx,
438// uint32_t face,
439// bool explicitFilename)
440// {
441// //
442// // Our trace sinks are going to use packet printing, so we have to
443// // make sure that is turned on.
444// //
445// Packet::EnablePrinting ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700446
Alexander Afanasyev11453142011-11-25 16:13:33 -0800447// //
448// // If we are not provided an OutputStreamWrapper, we are expected to create
449// // one using the usual trace filename conventions and hook WithoutContext
450// // since there will be one file per context and therefore the context would
451// // be redundant.
452// //
453// if (stream == 0)
454// {
455// //
456// // Set up an output stream object to deal with private ofstream copy
457// // constructor and lifetime issues. Let the helper decide the actual
458// // name of the file given the prefix.
459// //
460// // We have to create a stream and a mapping from protocol/face to
461// // stream irrespective of how many times we want to trace a particular
462// // protocol.
463// //
464// AsciiTraceHelper asciiTraceHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700465
Alexander Afanasyev11453142011-11-25 16:13:33 -0800466// std::string filename;
467// if (explicitFilename)
468// {
469// filename = prefix;
470// }
471// else
472// {
473// filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
474// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700475
Alexander Afanasyev11453142011-11-25 16:13:33 -0800476// Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700477
Alexander Afanasyev11453142011-11-25 16:13:33 -0800478// //
479// // However, we only hook the trace sources once to avoid multiple trace sink
480// // calls per event (connect is independent of face).
481// //
482// if (!AsciiHooked (ccnx))
483// {
484// //
485// // The drop sink for the CcnxL3Protocol uses a different signature than
486// // the default sink, so we have to cook one up for ourselves. We can get
487// // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both
488// // be aggregated to the same node.
489// //
490// Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
491// bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop",
492// MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream));
493// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): "
494// "Unable to connect ccnxL3Protocol \"Drop\"");
495// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700496
Alexander Afanasyev11453142011-11-25 16:13:33 -0800497// g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream;
498// return;
499// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700500
Alexander Afanasyev11453142011-11-25 16:13:33 -0800501// //
502// // If we are provided an OutputStreamWrapper, we are expected to use it, and
503// // to provide a context. We are free to come up with our own context if we
504// // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
505// // compatibility and simplicity, we just use Config::Connect and let it deal
506// // with the context.
507// //
508// // We need to associate the ccnx/face with a stream to express interest
509// // in tracing events on that pair, however, we only hook the trace sources
510// // once to avoid multiple trace sink calls per event (connect is independent
511// // of face).
512// //
513// if (!AsciiHooked (ccnx))
514// {
515// Ptr<Node> node = ccnx->GetObject<Node> ();
516// std::ostringstream oss;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700517
Alexander Afanasyev11453142011-11-25 16:13:33 -0800518// //
519// // This has all kinds of parameters coming with, so we have to cook up our
520// // own sink.
521// //
522// oss.str ("");
523// oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop";
524// Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream));
525// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700526
Alexander Afanasyev11453142011-11-25 16:13:33 -0800527// g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream;
528// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700529
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800530void
531CcnxStackHelper::InstallFakeGlobalRoutes ()
532{
533 for (NodeList::Iterator node = NodeList::Begin ();
534 node != NodeList::End ();
535 node ++)
536 {
537 NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0,
538 "InternetStack should be installed on all nodes");
539
540 NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops>
541 (
542 (*node)->GetObject<Ipv4> ()->GetRoutingProtocol ()
Alexander Afanasyev11453142011-11-25 16:13:33 -0800543 ),
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800544 "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol");
545 // Example:
546 //
547 // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops");
548 // stack.SetRoutingHelper (ipv4RoutingHelper);
549 //
550
551 Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> ();
552 if (globalRouter == 0) continue;
553
554 globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255"));
555 }
556
557 Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables ();
558}
559
560void
561CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode)
562{
563 std::ostringstream destPrefix;
564 destPrefix << "/" << destNode->GetId ();
565
566 Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ());
567
568 for (NodeList::Iterator node = NodeList::Begin ();
569 node != NodeList::End ();
570 node ++)
571 {
572 if (destNode == *node) continue;
573
574 Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> ();
575 NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes");
576
577 Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> ();
578 NS_ASSERT_MSG (ipv4 != 0,
579 "InternetStack should be installed on all nodes");
580
581 Ptr<Ipv4GlobalRoutingOrderedNexthops> routing =
582 Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ());
583 NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack");
584
585 Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
586 routes = routing->Lookup (destIpv4);
587
588 NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer");
589
590 BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes)
591 {
592 Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ());
593 NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos");
594
595 Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice);
596 NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president");
597
598 AddRoute (*node, destPrefix.str(), face, entry.GetMetric ());
599 }
600 }
601}
602
603void
604CcnxStackHelper::InstallRoutesToAll ()
605{
606 for (NodeList::Iterator node = NodeList::Begin ();
607 node != NodeList::End ();
608 node ++)
609 {
610 InstallRouteTo (*node);
611 }
612}
613
614
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700615} // namespace ns3