Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 2 | /* |
| 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 Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame^] | 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 20 | */ |
| 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 Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 61 | #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 Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 69 | #include "ns3/ccnx-forwarding-strategy.h" |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 70 | #include "ns3/ccnx-net-device-face.h" |
| 71 | #include "ns3/ccnx-l3-protocol.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 72 | #include "ns3/ccnx-fib.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 74 | #include "ccnx-face-container.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 75 | #include "ccnx-stack-helper.h" |
| 76 | #include "ccnx-forwarding-helper.h" |
| 77 | |
| 78 | #include <limits> |
| 79 | #include <map> |
| 80 | |
| 81 | NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper"); |
| 82 | |
| 83 | namespace ns3 { |
| 84 | |
| 85 | // Things are going to work differently here with respect to trace |
| 86 | // file handling than in most places because the Tx and Rx trace |
| 87 | // sources we are interested in are going to multiplex receive and |
| 88 | // transmit callbacks for all Ccnx and face pairs through one |
| 89 | // callback. We want packets to or from each distinct pair to go to |
| 90 | // an individual file, so we have got to demultiplex the Ccnx and face |
| 91 | // pair into a corresponding Ptr<PcapFileWrapper> at the callback. |
| 92 | // |
| 93 | // A complication in this situation is that the trace sources are |
| 94 | // hooked on a protocol basis. There is no trace source hooked by an |
| 95 | // Ccnx and face pair. This means that if we naively proceed to hook, |
| 96 | // say, a drop trace for a given Ccnx with face 0, and then hook for |
| 97 | // Ccnx with face 1 we will hook the drop trace twice and get two |
| 98 | // callbacks per event. What we need to do is to hook the event once, |
| 99 | // and that will result in a single callback per drop event, and the |
| 100 | // trace source will provide the face which we filter on in the trace |
| 101 | // sink. |
| 102 | // |
| 103 | // This has got to continue to work properly after the helper has been |
| 104 | // destroyed; but must be cleaned up at the end of time to avoid |
| 105 | // leaks. Global maps of protocol/face pairs to file objects seems to |
| 106 | // fit the bill. |
| 107 | // |
| 108 | typedef std::pair<Ptr<Ccnx>, uint32_t> FacePairCcnx; |
| 109 | typedef std::map<FacePairCcnx, Ptr<PcapFileWrapper> > FaceFileMapCcnx; |
| 110 | typedef std::map<FacePairCcnx, Ptr<OutputStreamWrapper> > FaceStreamMapCcnx; |
| 111 | |
| 112 | static FaceFileMapCcnx g_faceFileMapCcnx; /**< A mapping of Ccnx/face pairs to pcap files */ |
| 113 | static FaceStreamMapCcnx g_faceStreamMapCcnx; /**< A mapping of Ccnx/face pairs to ascii streams */ |
| 114 | |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 116 | CcnxStackHelper::CcnxStackHelper () |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 117 | : m_forwardingHelper (Ccnx::NDN_FLOODING) |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 118 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 119 | } |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 120 | |
| 121 | CcnxStackHelper::CcnxStackHelper (Ccnx::ForwardingStrategy strategy) |
| 122 | : m_forwardingHelper (strategy) |
| 123 | { |
| 124 | } |
| 125 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 126 | CcnxStackHelper::~CcnxStackHelper () |
| 127 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | CcnxStackHelper::CcnxStackHelper (const CcnxStackHelper &o) |
| 131 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | CcnxStackHelper & |
| 135 | CcnxStackHelper::operator = (const CcnxStackHelper &o) |
| 136 | { |
| 137 | if (this == &o) |
| 138 | { |
| 139 | return *this; |
| 140 | } |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 141 | return *this; |
| 142 | } |
| 143 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 144 | void |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 145 | CcnxStackHelper::SetForwardingStrategy (Ccnx::ForwardingStrategy strategy) |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 146 | { |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 147 | CcnxForwardingHelper newForwardingHelper (strategy); |
| 148 | m_forwardingHelper = newForwardingHelper; |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 151 | Ptr<CcnxFaceContainer> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 152 | CcnxStackHelper::Install (NodeContainer c) const |
| 153 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 154 | Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> (); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 155 | for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) |
| 156 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 157 | faces->AddAll (Install (*i)); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 158 | } |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 159 | return faces; |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 162 | Ptr<CcnxFaceContainer> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 163 | CcnxStackHelper::InstallAll (void) const |
| 164 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 165 | return Install (NodeContainer::GetGlobal ()); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 168 | // void |
| 169 | // CcnxStackHelper::CreateAndAggregateObjectFromTypeId (Ptr<Node> node, const std::string typeId) |
| 170 | // { |
| 171 | // ObjectFactory factory; |
| 172 | // factory.SetTypeId (typeId); |
| 173 | // Ptr<Object> protocol = factory.Create <Object> (); |
| 174 | // node->AggregateObject (protocol); |
| 175 | // } |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 176 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 177 | Ptr<CcnxFaceContainer> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 178 | CcnxStackHelper::Install (Ptr<Node> node) const |
| 179 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 180 | // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method"); |
| 181 | Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> (); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 183 | if (node->GetObject<Ccnx> () != 0) |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 184 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 185 | NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing " |
| 186 | "a CcnxStack to a node with an existing Ccnx object"); |
| 187 | return 0; |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 188 | } |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 189 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 190 | Ptr<CcnxFib> fib = CreateObject<CcnxFib> (); |
| 191 | node->AggregateObject (fib); |
| 192 | |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame^] | 193 | Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 194 | node->AggregateObject (ccnx); |
| 195 | |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame^] | 196 | Ptr<CcnxPit> pit = ccnx->GetPit(); |
| 197 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 198 | for (uint32_t index=0; index < node->GetNDevices (); index++) |
| 199 | { |
| 200 | Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node->GetDevice (index)); |
| 201 | face->SetNode (node); |
| 202 | uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face); |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 203 | NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 204 | |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame^] | 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) |
| 208 | Ptr<PointToPointNetDevice> device = node->GetDevice(index)->GetObject<PointToPointNetDevice> (); |
| 209 | DataRateValue dataRate; |
| 210 | device->GetAttribute ("DataRate", dataRate); |
| 211 | pit->maxBucketsPerFace[face->GetId()] = 0.1 * dataRate.Get().GetBitRate () / 8 / (NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader)); |
| 212 | pit->leakSize[face->GetId()] = 0.97 * NDN_INTEREST_RESET_PERIOD / SECOND * dataRate.Get().GetBitRate () / 8 / (NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader)); |
| 213 | |
| 214 | |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 215 | face->SetUp (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 216 | faces->Add (face); |
| 217 | } |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 218 | |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame^] | 219 | /* |
| 220 | // Assume that we know average data packet size, and this size is equal default size |
| 221 | // Set maximum buckets (averaging over 1 second) |
| 222 | _pit.maxBucketsPerInterface[*it] = 0.1*_node->macData[*it]->bandwidth / 8 / (NDN_DEFAULT_DATA_SIZE+sizeof(NdnPacket)); |
| 223 | _pit.leakSize[*it] = 0.97 * NDN_INTEREST_RESET_PERIOD / SECOND * _node->macData[*it]->bandwidth/8 / (NDN_DEFAULT_DATA_SIZE+sizeof(NdnPacket)); |
| 224 | |
| 225 | */ |
| 226 | m_forwardingHelper.SetForwarding (ccnx, pit); |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 227 | |
| 228 | // Ptr<CcnxForwardingStrategy> ccnxForwarding = m_forwarding->Create (node); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 229 | // ccnx->SetForwardingStrategy (ccnxForwarding); |
| 230 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 231 | return faces; |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 234 | Ptr<CcnxFaceContainer> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 235 | CcnxStackHelper::Install (std::string nodeName) const |
| 236 | { |
| 237 | Ptr<Node> node = Names::Find<Node> (nodeName); |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 238 | return Install (node); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 241 | |
| 242 | void |
| 243 | CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric) |
| 244 | { |
| 245 | NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric); |
| 246 | |
| 247 | Ptr<Node> node = Names::Find<Node> (nodeName); |
| 248 | NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist"); |
| 249 | |
| 250 | Ptr<Ccnx> ccnx = node->GetObject<Ccnx> (); |
| 251 | Ptr<CcnxFib> fib = node->GetObject<CcnxFib> (); |
| 252 | Ptr<CcnxFace> face = ccnx->GetFace (faceId); |
| 253 | NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]"); |
| 254 | |
| 255 | CcnxNameComponentsValue prefixValue; |
| 256 | prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ()); |
| 257 | fib->Add (prefixValue.Get (), face, metric); |
| 258 | } |
| 259 | |
| 260 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 261 | static void |
| 262 | CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face) |
| 263 | { |
| 264 | NS_LOG_FUNCTION (p << ccnx << face); |
| 265 | |
| 266 | // |
| 267 | // Since trace sources are independent of face, if we hook a source |
| 268 | // on a particular protocol we will get traces for all of its faces. |
| 269 | // We need to filter this to only report faces for which the user |
| 270 | // has expressed interest. |
| 271 | // |
| 272 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 273 | if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ()) |
| 274 | { |
| 275 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair]; |
| 280 | file->Write (Simulator::Now (), p); |
| 281 | } |
| 282 | |
| 283 | bool |
| 284 | CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx) |
| 285 | { |
| 286 | for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin (); |
| 287 | i != g_faceFileMapCcnx.end (); |
| 288 | ++i) |
| 289 | { |
| 290 | if ((*i).first.first == ccnx) |
| 291 | { |
| 292 | return true; |
| 293 | } |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename) |
| 300 | { |
| 301 | NS_LOG_FUNCTION (prefix << ccnx << face); |
| 302 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 303 | // |
| 304 | // We have to create a file and a mapping from protocol/face to file |
| 305 | // irrespective of how many times we want to trace a particular protocol. |
| 306 | // |
| 307 | PcapHelper pcapHelper; |
| 308 | |
| 309 | std::string filename; |
| 310 | if (explicitFilename) |
| 311 | { |
| 312 | filename = prefix; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face); |
| 317 | } |
| 318 | |
| 319 | Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW); |
| 320 | |
| 321 | // |
| 322 | // However, we only hook the trace source once to avoid multiple trace sink |
| 323 | // calls per event (connect is independent of face). |
| 324 | // |
| 325 | if (!PcapHooked (ccnx)) |
| 326 | { |
| 327 | // |
| 328 | // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to |
| 329 | // node so we can get to CcnxL3Protocol through Ccnx. |
| 330 | // |
| 331 | Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> (); |
| 332 | NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 333 | "m_ccnxEnabled and ccnxL3Protocol inconsistent"); |
| 334 | |
| 335 | bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink)); |
| 336 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 337 | "Unable to connect ccnxL3Protocol \"Tx\""); |
| 338 | |
| 339 | result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink)); |
| 340 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 341 | "Unable to connect ccnxL3Protocol \"Rx\""); |
| 342 | // cast result to void, to suppress ‘result’ set but not used compiler-warning |
| 343 | // for optimized builds |
| 344 | (void) result; |
| 345 | } |
| 346 | |
| 347 | g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file; |
| 348 | } |
| 349 | |
| 350 | static void |
| 351 | CcnxL3ProtocolDropSinkWithoutContext ( |
| 352 | Ptr<OutputStreamWrapper> stream, |
| 353 | Ptr<const Packet> packet, |
| 354 | CcnxL3Protocol::DropReason reason, |
| 355 | Ptr<Ccnx> ccnx, |
| 356 | uint32_t face) |
| 357 | { |
| 358 | // |
| 359 | // Since trace sources are independent of face, if we hook a source |
| 360 | // on a particular protocol we will get traces for all of its faces. |
| 361 | // We need to filter this to only report faces for which the user |
| 362 | // has expressed interest. |
| 363 | // |
| 364 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 365 | if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ()) |
| 366 | { |
| 367 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl; |
| 372 | } |
| 373 | |
| 374 | static void |
| 375 | CcnxL3ProtocolDropSinkWithContext ( |
| 376 | Ptr<OutputStreamWrapper> stream, |
| 377 | std::string context, |
| 378 | Ptr<const Packet> packet, |
| 379 | CcnxL3Protocol::DropReason reason, |
| 380 | Ptr<Ccnx> ccnx, |
| 381 | uint32_t face) |
| 382 | { |
| 383 | // |
| 384 | // Since trace sources are independent of face, if we hook a source |
| 385 | // on a particular protocol we will get traces for all of its faces. |
| 386 | // We need to filter this to only report faces for which the user |
| 387 | // has expressed interest. |
| 388 | // |
| 389 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 390 | if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ()) |
| 391 | { |
| 392 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") " |
| 397 | << *packet << std::endl; |
| 398 | } |
| 399 | |
| 400 | bool |
| 401 | CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx) |
| 402 | { |
| 403 | for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin (); |
| 404 | i != g_faceStreamMapCcnx.end (); |
| 405 | ++i) |
| 406 | { |
| 407 | if ((*i).first.first == ccnx) |
| 408 | { |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | CcnxStackHelper::EnableAsciiCcnxInternal ( |
| 417 | Ptr<OutputStreamWrapper> stream, |
| 418 | std::string prefix, |
| 419 | Ptr<Ccnx> ccnx, |
| 420 | uint32_t face, |
| 421 | bool explicitFilename) |
| 422 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 423 | // |
| 424 | // Our trace sinks are going to use packet printing, so we have to |
| 425 | // make sure that is turned on. |
| 426 | // |
| 427 | Packet::EnablePrinting (); |
| 428 | |
| 429 | // |
| 430 | // If we are not provided an OutputStreamWrapper, we are expected to create |
| 431 | // one using the usual trace filename conventions and hook WithoutContext |
| 432 | // since there will be one file per context and therefore the context would |
| 433 | // be redundant. |
| 434 | // |
| 435 | if (stream == 0) |
| 436 | { |
| 437 | // |
| 438 | // Set up an output stream object to deal with private ofstream copy |
| 439 | // constructor and lifetime issues. Let the helper decide the actual |
| 440 | // name of the file given the prefix. |
| 441 | // |
| 442 | // We have to create a stream and a mapping from protocol/face to |
| 443 | // stream irrespective of how many times we want to trace a particular |
| 444 | // protocol. |
| 445 | // |
| 446 | AsciiTraceHelper asciiTraceHelper; |
| 447 | |
| 448 | std::string filename; |
| 449 | if (explicitFilename) |
| 450 | { |
| 451 | filename = prefix; |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face); |
| 456 | } |
| 457 | |
| 458 | Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename); |
| 459 | |
| 460 | // |
| 461 | // However, we only hook the trace sources once to avoid multiple trace sink |
| 462 | // calls per event (connect is independent of face). |
| 463 | // |
| 464 | if (!AsciiHooked (ccnx)) |
| 465 | { |
| 466 | // |
| 467 | // The drop sink for the CcnxL3Protocol uses a different signature than |
| 468 | // the default sink, so we have to cook one up for ourselves. We can get |
| 469 | // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both |
| 470 | // be aggregated to the same node. |
| 471 | // |
| 472 | Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> (); |
| 473 | bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop", |
| 474 | MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream)); |
| 475 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): " |
| 476 | "Unable to connect ccnxL3Protocol \"Drop\""); |
| 477 | } |
| 478 | |
| 479 | g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream; |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | // |
| 484 | // If we are provided an OutputStreamWrapper, we are expected to use it, and |
| 485 | // to provide a context. We are free to come up with our own context if we |
| 486 | // want, and use the AsciiTraceHelper Hook*WithContext functions, but for |
| 487 | // compatibility and simplicity, we just use Config::Connect and let it deal |
| 488 | // with the context. |
| 489 | // |
| 490 | // We need to associate the ccnx/face with a stream to express interest |
| 491 | // in tracing events on that pair, however, we only hook the trace sources |
| 492 | // once to avoid multiple trace sink calls per event (connect is independent |
| 493 | // of face). |
| 494 | // |
| 495 | if (!AsciiHooked (ccnx)) |
| 496 | { |
| 497 | Ptr<Node> node = ccnx->GetObject<Node> (); |
| 498 | std::ostringstream oss; |
| 499 | |
| 500 | // |
| 501 | // This has all kinds of parameters coming with, so we have to cook up our |
| 502 | // own sink. |
| 503 | // |
| 504 | oss.str (""); |
| 505 | oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop"; |
| 506 | Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream)); |
| 507 | } |
| 508 | |
| 509 | g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream; |
| 510 | } |
| 511 | |
| 512 | } // namespace ns3 |