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