blob: d9f9d6bb94a0c145a101bc32f02692aab0700ed2 [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
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800144CcnxStackHelper::EnableLimits (bool enable/* = true*/, Time avgRtt/*=Seconds(0.1)*/, uint32_t avgContentObject/*=1100*/, uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800145{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800146 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800147 m_limitsEnabled = enable;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800148 m_avgContentObjectSize = avgContentObject;
149 m_avgInterestSize = avgInterest;
150 m_avgRtt = avgRtt;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700151}
152
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700153Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700154CcnxStackHelper::Install (NodeContainer c) const
155{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700156 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700157 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
158 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700159 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700160 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700161 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700162}
163
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700164Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700165CcnxStackHelper::InstallAll (void) const
166{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700167 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700168}
169
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700170Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700171CcnxStackHelper::Install (Ptr<Node> node) const
172{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700173 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
174 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700175
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700176 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700177 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
179 "a CcnxStack to a node with an existing Ccnx object");
180 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700181 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700182
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
184 node->AggregateObject (fib);
185
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700186 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700187 node->AggregateObject (ccnx);
188
Alexander Afanasyev11453142011-11-25 16:13:33 -0800189 ccnx->SetForwardingStrategy
190 (DynamicCast<CcnxForwardingStrategy> (m_strategyFactory.Create<Object> ()));
191
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192 for (uint32_t index=0; index < node->GetNDevices (); index++)
193 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800194 Ptr<NetDevice> device = node->GetDevice (index);
195 if (DynamicCast<LoopbackNetDevice> (device) != 0)
196 continue; // don't create face for a LoopbackNetDevice
197
198 Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800199
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700200 uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face);
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700201 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800202
203 if (m_limitsEnabled)
204 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800205 NS_LOG_INFO ("Limits are enabled");
Alexander Afanasyev11453142011-11-25 16:13:33 -0800206 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
207 if (p2p == 0)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800208 {
209 NS_LOG_INFO ("Non p2p interface");
210 continue; // only PointToPointNetDevice supports limits
211 }
Alexander Afanasyev11453142011-11-25 16:13:33 -0800212
213 // Setup bucket filtering
214 // Assume that we know average data packet size, and this size is equal default size
215 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700216
Alexander Afanasyev11453142011-11-25 16:13:33 -0800217 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
218
219 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800220
221 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / m_avgContentObjectSize;
222 NS_LOG_INFO ("BucketMax: " << maxInterestPackets);
223
224 // Set bucket max to BDP
225 face->SetBucketMax (m_avgRtt.ToDouble (Time::S) * maxInterestPackets); // number of interests allowed
226 face->SetBucketLeak (maxInterestPackets);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800227 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700228
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700229 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700230 faces->Add (face);
231 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700232
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700233 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700234}
235
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700236Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700237CcnxStackHelper::Install (std::string nodeName) const
238{
239 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700240 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700241}
242
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700243
244void
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800245CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
246{
247 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
248
249 CcnxNameComponentsValue prefixValue;
250 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
251 fib->Add (prefixValue.Get (), face, metric);
252}
253
254void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700255CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
256{
257 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
258
259 Ptr<Node> node = Names::Find<Node> (nodeName);
260 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800261
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800262 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
263 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
264
265 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
266 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
267
268 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700269}
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700270/*
Alexander Afanasyev11453142011-11-25 16:13:33 -0800271 void
272 CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, uint32_t faceId, int32_t metric)
273 {
274 NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700275
276 NS_ASSERT_MSG (node != 0, "Node does not exist");
277
278 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
279 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
280 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
281 NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
282
283 CcnxNameComponentsValue prefixValue;
284 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
285 fib->Add (prefixValue.Get (), face, metric);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800286 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700287*/
Ilya Moiseenkoae394872011-11-15 17:56:36 -0800288
Alexander Afanasyev11453142011-11-25 16:13:33 -0800289// static void
290// CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face)
291// {
292// NS_LOG_FUNCTION (p << ccnx << face);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700293
Alexander Afanasyev11453142011-11-25 16:13:33 -0800294// //
295// // Since trace sources are independent of face, if we hook a source
296// // on a particular protocol we will get traces for all of its faces.
297// // We need to filter this to only report faces for which the user
298// // has expressed interest.
299// //
300// FacePairCcnx pair = std::make_pair (ccnx, face);
301// if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ())
302// {
303// NS_LOG_INFO ("Ignoring packet to/from face " << face);
304// return;
305// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700306
Alexander Afanasyev11453142011-11-25 16:13:33 -0800307// Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair];
308// file->Write (Simulator::Now (), p);
309// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700310
Alexander Afanasyev11453142011-11-25 16:13:33 -0800311// bool
312// CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx)
313// {
314// for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin ();
315// i != g_faceFileMapCcnx.end ();
316// ++i)
317// {
318// if ((*i).first.first == ccnx)
319// {
320// return true;
321// }
322// }
323// return false;
324// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700325
Alexander Afanasyev11453142011-11-25 16:13:33 -0800326// void
327// CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename)
328// {
329// NS_LOG_FUNCTION (prefix << ccnx << face);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700330
Alexander Afanasyev11453142011-11-25 16:13:33 -0800331// //
332// // We have to create a file and a mapping from protocol/face to file
333// // irrespective of how many times we want to trace a particular protocol.
334// //
335// PcapHelper pcapHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700336
Alexander Afanasyev11453142011-11-25 16:13:33 -0800337// std::string filename;
338// if (explicitFilename)
339// {
340// filename = prefix;
341// }
342// else
343// {
344// filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
345// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700346
Alexander Afanasyev11453142011-11-25 16:13:33 -0800347// Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700348
Alexander Afanasyev11453142011-11-25 16:13:33 -0800349// //
350// // However, we only hook the trace source once to avoid multiple trace sink
351// // calls per event (connect is independent of face).
352// //
353// if (!PcapHooked (ccnx))
354// {
355// //
356// // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to
357// // node so we can get to CcnxL3Protocol through Ccnx.
358// //
359// Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
360// NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): "
361// "m_ccnxEnabled and ccnxL3Protocol inconsistent");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700362
Alexander Afanasyev11453142011-11-25 16:13:33 -0800363// bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink));
364// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
365// "Unable to connect ccnxL3Protocol \"Tx\"");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700366
Alexander Afanasyev11453142011-11-25 16:13:33 -0800367// result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink));
368// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): "
369// "Unable to connect ccnxL3Protocol \"Rx\"");
370// // cast result to void, to suppress ‘result’ set but not used compiler-warning
371// // for optimized builds
372// (void) result;
373// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700374
Alexander Afanasyev11453142011-11-25 16:13:33 -0800375// g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file;
376// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700377
Alexander Afanasyev11453142011-11-25 16:13:33 -0800378// static void
379// CcnxL3ProtocolDropSinkWithoutContext (
380// Ptr<OutputStreamWrapper> stream,
381// Ptr<const Packet> packet,
382// CcnxL3Protocol::DropReason reason,
383// Ptr<Ccnx> ccnx,
384// uint32_t face)
385// {
386// //
387// // Since trace sources are independent of face, if we hook a source
388// // on a particular protocol we will get traces for all of its faces.
389// // We need to filter this to only report faces for which the user
390// // has expressed interest.
391// //
392// FacePairCcnx pair = std::make_pair (ccnx, face);
393// if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
394// {
395// NS_LOG_INFO ("Ignoring packet to/from face " << face);
396// return;
397// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700398
Alexander Afanasyev11453142011-11-25 16:13:33 -0800399// *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl;
400// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700401
Alexander Afanasyev11453142011-11-25 16:13:33 -0800402// static void
403// CcnxL3ProtocolDropSinkWithContext (
404// Ptr<OutputStreamWrapper> stream,
405// std::string context,
406// Ptr<const Packet> packet,
407// CcnxL3Protocol::DropReason reason,
408// Ptr<Ccnx> ccnx,
409// uint32_t face)
410// {
411// //
412// // Since trace sources are independent of face, if we hook a source
413// // on a particular protocol we will get traces for all of its faces.
414// // We need to filter this to only report faces for which the user
415// // has expressed interest.
416// //
417// FacePairCcnx pair = std::make_pair (ccnx, face);
418// if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ())
419// {
420// NS_LOG_INFO ("Ignoring packet to/from face " << face);
421// return;
422// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700423
Alexander Afanasyev11453142011-11-25 16:13:33 -0800424// *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") "
425// << *packet << std::endl;
426// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700427
Alexander Afanasyev11453142011-11-25 16:13:33 -0800428// bool
429// CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx)
430// {
431// for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin ();
432// i != g_faceStreamMapCcnx.end ();
433// ++i)
434// {
435// if ((*i).first.first == ccnx)
436// {
437// return true;
438// }
439// }
440// return false;
441// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700442
Alexander Afanasyev11453142011-11-25 16:13:33 -0800443// void
444// CcnxStackHelper::EnableAsciiCcnxInternal (
445// Ptr<OutputStreamWrapper> stream,
446// std::string prefix,
447// Ptr<Ccnx> ccnx,
448// uint32_t face,
449// bool explicitFilename)
450// {
451// //
452// // Our trace sinks are going to use packet printing, so we have to
453// // make sure that is turned on.
454// //
455// Packet::EnablePrinting ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700456
Alexander Afanasyev11453142011-11-25 16:13:33 -0800457// //
458// // If we are not provided an OutputStreamWrapper, we are expected to create
459// // one using the usual trace filename conventions and hook WithoutContext
460// // since there will be one file per context and therefore the context would
461// // be redundant.
462// //
463// if (stream == 0)
464// {
465// //
466// // Set up an output stream object to deal with private ofstream copy
467// // constructor and lifetime issues. Let the helper decide the actual
468// // name of the file given the prefix.
469// //
470// // We have to create a stream and a mapping from protocol/face to
471// // stream irrespective of how many times we want to trace a particular
472// // protocol.
473// //
474// AsciiTraceHelper asciiTraceHelper;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700475
Alexander Afanasyev11453142011-11-25 16:13:33 -0800476// std::string filename;
477// if (explicitFilename)
478// {
479// filename = prefix;
480// }
481// else
482// {
483// filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face);
484// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700485
Alexander Afanasyev11453142011-11-25 16:13:33 -0800486// Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700487
Alexander Afanasyev11453142011-11-25 16:13:33 -0800488// //
489// // However, we only hook the trace sources once to avoid multiple trace sink
490// // calls per event (connect is independent of face).
491// //
492// if (!AsciiHooked (ccnx))
493// {
494// //
495// // The drop sink for the CcnxL3Protocol uses a different signature than
496// // the default sink, so we have to cook one up for ourselves. We can get
497// // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both
498// // be aggregated to the same node.
499// //
500// Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> ();
501// bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop",
502// MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream));
503// NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): "
504// "Unable to connect ccnxL3Protocol \"Drop\"");
505// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700506
Alexander Afanasyev11453142011-11-25 16:13:33 -0800507// g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream;
508// return;
509// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700510
Alexander Afanasyev11453142011-11-25 16:13:33 -0800511// //
512// // If we are provided an OutputStreamWrapper, we are expected to use it, and
513// // to provide a context. We are free to come up with our own context if we
514// // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
515// // compatibility and simplicity, we just use Config::Connect and let it deal
516// // with the context.
517// //
518// // We need to associate the ccnx/face with a stream to express interest
519// // in tracing events on that pair, however, we only hook the trace sources
520// // once to avoid multiple trace sink calls per event (connect is independent
521// // of face).
522// //
523// if (!AsciiHooked (ccnx))
524// {
525// Ptr<Node> node = ccnx->GetObject<Node> ();
526// std::ostringstream oss;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700527
Alexander Afanasyev11453142011-11-25 16:13:33 -0800528// //
529// // This has all kinds of parameters coming with, so we have to cook up our
530// // own sink.
531// //
532// oss.str ("");
533// oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop";
534// Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream));
535// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700536
Alexander Afanasyev11453142011-11-25 16:13:33 -0800537// g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream;
538// }
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700539
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800540void
541CcnxStackHelper::InstallFakeGlobalRoutes ()
542{
543 for (NodeList::Iterator node = NodeList::Begin ();
544 node != NodeList::End ();
545 node ++)
546 {
547 NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0,
548 "InternetStack should be installed on all nodes");
549
550 NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops>
551 (
552 (*node)->GetObject<Ipv4> ()->GetRoutingProtocol ()
Alexander Afanasyev11453142011-11-25 16:13:33 -0800553 ),
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800554 "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol");
555 // Example:
556 //
557 // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops");
558 // stack.SetRoutingHelper (ipv4RoutingHelper);
559 //
560
561 Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> ();
562 if (globalRouter == 0) continue;
563
564 globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255"));
565 }
566
567 Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables ();
568}
569
570void
571CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode)
572{
573 std::ostringstream destPrefix;
574 destPrefix << "/" << destNode->GetId ();
575
576 Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ());
577
578 for (NodeList::Iterator node = NodeList::Begin ();
579 node != NodeList::End ();
580 node ++)
581 {
582 if (destNode == *node) continue;
583
584 Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> ();
585 NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes");
586
587 Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> ();
588 NS_ASSERT_MSG (ipv4 != 0,
589 "InternetStack should be installed on all nodes");
590
591 Ptr<Ipv4GlobalRoutingOrderedNexthops> routing =
592 Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ());
593 NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack");
594
595 Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
596 routes = routing->Lookup (destIpv4);
597
598 NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer");
599
600 BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes)
601 {
602 Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ());
603 NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos");
604
605 Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice);
606 NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president");
607
608 AddRoute (*node, destPrefix.str(), face, entry.GetMetric ());
609 }
610 }
611}
612
613void
614CcnxStackHelper::InstallRoutesToAll ()
615{
616 for (NodeList::Iterator node = NodeList::Begin ();
617 node != NodeList::End ();
618 node ++)
619 {
620 InstallRouteTo (*node);
621 }
622}
623
624
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700625} // namespace ns3