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 | ae39487 | 2011-11-15 17:56:36 -0800 | [diff] [blame] | 204 | NS_LOG_INFO("NODE->GetNDevices()="<<node->GetNDevices()); |
| 205 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 206 | for (uint32_t index=0; index < node->GetNDevices (); index++) |
| 207 | { |
Ilya Moiseenko | ae39487 | 2011-11-15 17:56:36 -0800 | [diff] [blame] | 208 | Ptr<PointToPointNetDevice> device = DynamicCast<PointToPointNetDevice>(node->GetDevice(index)); |
| 209 | if(device == 0) |
| 210 | continue; |
Ilya Moiseenko | c926604 | 2011-11-02 17:49:21 -0700 | [diff] [blame] | 211 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 212 | Ptr<CcnxNetDeviceFace> face = Create<CcnxNetDeviceFace> (node->GetDevice (index)); |
| 213 | face->SetNode (node); |
| 214 | uint32_t __attribute__ ((unused)) face_id = ccnx->AddFace (face); |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 215 | NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << face_id); |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame] | 216 | // Setup bucket filtering |
| 217 | // Assume that we know average data packet size, and this size is equal default size |
| 218 | // Set maximum buckets (averaging over 1 second) |
Ilya Moiseenko | c926604 | 2011-11-02 17:49:21 -0700 | [diff] [blame] | 219 | |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame] | 220 | DataRateValue dataRate; |
| 221 | device->GetAttribute ("DataRate", dataRate); |
Ilya Moiseenko | c926604 | 2011-11-02 17:49:21 -0700 | [diff] [blame] | 222 | NS_LOG_INFO("DataRate for this link is " << dataRate.Get()); |
| 223 | pit->maxBucketsPerFace[face->GetId()] = 0.1 * dataRate.Get().GetBitRate () / 8 /(NDN_DEFAULT_DATA_SIZE + sizeof(CcnxInterestHeader)); |
| 224 | NS_LOG_INFO("maxBucketsPerFace["<<face->GetId()<<"] = " << pit->maxBucketsPerFace[face->GetId()]); |
Ilya Moiseenko | fbd0a8b | 2011-10-28 13:07:16 -0700 | [diff] [blame] | 225 | 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] | 226 | NS_LOG_INFO("pit->leakSize["<<face->GetId()<<"] = " << pit->leakSize[face->GetId()]); |
| 227 | |
| 228 | |
| 229 | if(face->IsLocal()==true) |
| 230 | NS_LOG_INFO("Face #" << face_id << " is turned on"); |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 231 | face->SetUp (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 232 | faces->Add (face); |
| 233 | } |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 234 | |
Ilya Moiseenko | c926604 | 2011-11-02 17:49:21 -0700 | [diff] [blame] | 235 | m_forwardingHelper.SetForwarding (ccnx, pit); |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 236 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 237 | return faces; |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 240 | Ptr<CcnxFaceContainer> |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 241 | CcnxStackHelper::Install (std::string nodeName) const |
| 242 | { |
| 243 | Ptr<Node> node = Names::Find<Node> (nodeName); |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 244 | return Install (node); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 247 | |
| 248 | void |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 249 | CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric) |
| 250 | { |
| 251 | Ptr<CcnxFib> fib = node->GetObject<CcnxFib> (); |
| 252 | |
| 253 | CcnxNameComponentsValue prefixValue; |
| 254 | prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ()); |
| 255 | fib->Add (prefixValue.Get (), face, metric); |
| 256 | } |
| 257 | |
| 258 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 259 | CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric) |
| 260 | { |
| 261 | NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric); |
| 262 | |
| 263 | Ptr<Node> node = Names::Find<Node> (nodeName); |
| 264 | NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist"); |
Alexander Afanasyev | a4e3f85 | 2011-11-15 20:39:33 -0800 | [diff] [blame^] | 265 | |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 266 | Ptr<Ccnx> ccnx = node->GetObject<Ccnx> (); |
| 267 | NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node"); |
| 268 | |
| 269 | Ptr<CcnxFace> face = ccnx->GetFace (faceId); |
| 270 | NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]"); |
| 271 | |
| 272 | AddRoute (node, prefix, face, metric); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 273 | } |
Ilya Moiseenko | c926604 | 2011-11-02 17:49:21 -0700 | [diff] [blame] | 274 | /* |
| 275 | void |
| 276 | CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, uint32_t faceId, int32_t metric) |
| 277 | { |
| 278 | NS_LOG_LOGIC ("[" << nodeName << "]$ route add " << prefix << " via " << faceId << " metric " << metric); |
| 279 | |
| 280 | NS_ASSERT_MSG (node != 0, "Node does not exist"); |
| 281 | |
| 282 | Ptr<Ccnx> ccnx = node->GetObject<Ccnx> (); |
| 283 | Ptr<CcnxFib> fib = node->GetObject<CcnxFib> (); |
| 284 | Ptr<CcnxFace> face = ccnx->GetFace (faceId); |
| 285 | NS_ASSERT_MSG (node != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]"); |
| 286 | |
| 287 | CcnxNameComponentsValue prefixValue; |
| 288 | prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ()); |
| 289 | fib->Add (prefixValue.Get (), face, metric); |
| 290 | } |
| 291 | */ |
Ilya Moiseenko | ae39487 | 2011-11-15 17:56:36 -0800 | [diff] [blame] | 292 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 293 | static void |
| 294 | CcnxL3ProtocolRxTxSink (Ptr<const Packet> p, Ptr<Ccnx> ccnx, uint32_t face) |
| 295 | { |
| 296 | NS_LOG_FUNCTION (p << ccnx << face); |
| 297 | |
| 298 | // |
| 299 | // Since trace sources are independent of face, if we hook a source |
| 300 | // on a particular protocol we will get traces for all of its faces. |
| 301 | // We need to filter this to only report faces for which the user |
| 302 | // has expressed interest. |
| 303 | // |
| 304 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 305 | if (g_faceFileMapCcnx.find (pair) == g_faceFileMapCcnx.end ()) |
| 306 | { |
| 307 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | Ptr<PcapFileWrapper> file = g_faceFileMapCcnx[pair]; |
| 312 | file->Write (Simulator::Now (), p); |
| 313 | } |
| 314 | |
| 315 | bool |
| 316 | CcnxStackHelper::PcapHooked (Ptr<Ccnx> ccnx) |
| 317 | { |
| 318 | for (FaceFileMapCcnx::const_iterator i = g_faceFileMapCcnx.begin (); |
| 319 | i != g_faceFileMapCcnx.end (); |
| 320 | ++i) |
| 321 | { |
| 322 | if ((*i).first.first == ccnx) |
| 323 | { |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | CcnxStackHelper::EnablePcapCcnxInternal (std::string prefix, Ptr<Ccnx> ccnx, uint32_t face, bool explicitFilename) |
| 332 | { |
| 333 | NS_LOG_FUNCTION (prefix << ccnx << face); |
| 334 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 335 | // |
| 336 | // We have to create a file and a mapping from protocol/face to file |
| 337 | // irrespective of how many times we want to trace a particular protocol. |
| 338 | // |
| 339 | PcapHelper pcapHelper; |
| 340 | |
| 341 | std::string filename; |
| 342 | if (explicitFilename) |
| 343 | { |
| 344 | filename = prefix; |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | filename = pcapHelper.GetFilenameFromInterfacePair (prefix, ccnx, face); |
| 349 | } |
| 350 | |
| 351 | Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_RAW); |
| 352 | |
| 353 | // |
| 354 | // However, we only hook the trace source once to avoid multiple trace sink |
| 355 | // calls per event (connect is independent of face). |
| 356 | // |
| 357 | if (!PcapHooked (ccnx)) |
| 358 | { |
| 359 | // |
| 360 | // Ptr<Ccnx> is aggregated to node and CcnxL3Protocol is aggregated to |
| 361 | // node so we can get to CcnxL3Protocol through Ccnx. |
| 362 | // |
| 363 | Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> (); |
| 364 | NS_ASSERT_MSG (ccnxL3Protocol, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 365 | "m_ccnxEnabled and ccnxL3Protocol inconsistent"); |
| 366 | |
| 367 | bool result = ccnxL3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&CcnxL3ProtocolRxTxSink)); |
| 368 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 369 | "Unable to connect ccnxL3Protocol \"Tx\""); |
| 370 | |
| 371 | result = ccnxL3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&CcnxL3ProtocolRxTxSink)); |
| 372 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EnablePcapCcnxInternal(): " |
| 373 | "Unable to connect ccnxL3Protocol \"Rx\""); |
| 374 | // cast result to void, to suppress ‘result’ set but not used compiler-warning |
| 375 | // for optimized builds |
| 376 | (void) result; |
| 377 | } |
| 378 | |
| 379 | g_faceFileMapCcnx[std::make_pair (ccnx, face)] = file; |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | CcnxL3ProtocolDropSinkWithoutContext ( |
| 384 | Ptr<OutputStreamWrapper> stream, |
| 385 | Ptr<const Packet> packet, |
| 386 | CcnxL3Protocol::DropReason reason, |
| 387 | Ptr<Ccnx> ccnx, |
| 388 | uint32_t face) |
| 389 | { |
| 390 | // |
| 391 | // Since trace sources are independent of face, if we hook a source |
| 392 | // on a particular protocol we will get traces for all of its faces. |
| 393 | // We need to filter this to only report faces for which the user |
| 394 | // has expressed interest. |
| 395 | // |
| 396 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 397 | if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ()) |
| 398 | { |
| 399 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << *packet << std::endl; |
| 404 | } |
| 405 | |
| 406 | static void |
| 407 | CcnxL3ProtocolDropSinkWithContext ( |
| 408 | Ptr<OutputStreamWrapper> stream, |
| 409 | std::string context, |
| 410 | Ptr<const Packet> packet, |
| 411 | CcnxL3Protocol::DropReason reason, |
| 412 | Ptr<Ccnx> ccnx, |
| 413 | uint32_t face) |
| 414 | { |
| 415 | // |
| 416 | // Since trace sources are independent of face, if we hook a source |
| 417 | // on a particular protocol we will get traces for all of its faces. |
| 418 | // We need to filter this to only report faces for which the user |
| 419 | // has expressed interest. |
| 420 | // |
| 421 | FacePairCcnx pair = std::make_pair (ccnx, face); |
| 422 | if (g_faceStreamMapCcnx.find (pair) == g_faceStreamMapCcnx.end ()) |
| 423 | { |
| 424 | NS_LOG_INFO ("Ignoring packet to/from face " << face); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | *stream->GetStream () << "d " << Simulator::Now ().GetSeconds () << " " << context << "(" << face << ") " |
| 429 | << *packet << std::endl; |
| 430 | } |
| 431 | |
| 432 | bool |
| 433 | CcnxStackHelper::AsciiHooked (Ptr<Ccnx> ccnx) |
| 434 | { |
| 435 | for ( FaceStreamMapCcnx::const_iterator i = g_faceStreamMapCcnx.begin (); |
| 436 | i != g_faceStreamMapCcnx.end (); |
| 437 | ++i) |
| 438 | { |
| 439 | if ((*i).first.first == ccnx) |
| 440 | { |
| 441 | return true; |
| 442 | } |
| 443 | } |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | CcnxStackHelper::EnableAsciiCcnxInternal ( |
| 449 | Ptr<OutputStreamWrapper> stream, |
| 450 | std::string prefix, |
| 451 | Ptr<Ccnx> ccnx, |
| 452 | uint32_t face, |
| 453 | bool explicitFilename) |
| 454 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 455 | // |
| 456 | // Our trace sinks are going to use packet printing, so we have to |
| 457 | // make sure that is turned on. |
| 458 | // |
| 459 | Packet::EnablePrinting (); |
| 460 | |
| 461 | // |
| 462 | // If we are not provided an OutputStreamWrapper, we are expected to create |
| 463 | // one using the usual trace filename conventions and hook WithoutContext |
| 464 | // since there will be one file per context and therefore the context would |
| 465 | // be redundant. |
| 466 | // |
| 467 | if (stream == 0) |
| 468 | { |
| 469 | // |
| 470 | // Set up an output stream object to deal with private ofstream copy |
| 471 | // constructor and lifetime issues. Let the helper decide the actual |
| 472 | // name of the file given the prefix. |
| 473 | // |
| 474 | // We have to create a stream and a mapping from protocol/face to |
| 475 | // stream irrespective of how many times we want to trace a particular |
| 476 | // protocol. |
| 477 | // |
| 478 | AsciiTraceHelper asciiTraceHelper; |
| 479 | |
| 480 | std::string filename; |
| 481 | if (explicitFilename) |
| 482 | { |
| 483 | filename = prefix; |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | filename = asciiTraceHelper.GetFilenameFromInterfacePair (prefix, ccnx, face); |
| 488 | } |
| 489 | |
| 490 | Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename); |
| 491 | |
| 492 | // |
| 493 | // However, we only hook the trace sources once to avoid multiple trace sink |
| 494 | // calls per event (connect is independent of face). |
| 495 | // |
| 496 | if (!AsciiHooked (ccnx)) |
| 497 | { |
| 498 | // |
| 499 | // The drop sink for the CcnxL3Protocol uses a different signature than |
| 500 | // the default sink, so we have to cook one up for ourselves. We can get |
| 501 | // to the Ptr<CcnxL3Protocol> through our Ptr<Ccnx> since they must both |
| 502 | // be aggregated to the same node. |
| 503 | // |
| 504 | Ptr<CcnxL3Protocol> ccnxL3Protocol = ccnx->GetObject<CcnxL3Protocol> (); |
| 505 | bool __attribute__ ((unused)) result = ccnxL3Protocol->TraceConnectWithoutContext ("Drop", |
| 506 | MakeBoundCallback (&CcnxL3ProtocolDropSinkWithoutContext, theStream)); |
| 507 | NS_ASSERT_MSG (result == true, "CcnxStackHelper::EanableAsciiCcnxInternal(): " |
| 508 | "Unable to connect ccnxL3Protocol \"Drop\""); |
| 509 | } |
| 510 | |
| 511 | g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = theStream; |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | // |
| 516 | // If we are provided an OutputStreamWrapper, we are expected to use it, and |
| 517 | // to provide a context. We are free to come up with our own context if we |
| 518 | // want, and use the AsciiTraceHelper Hook*WithContext functions, but for |
| 519 | // compatibility and simplicity, we just use Config::Connect and let it deal |
| 520 | // with the context. |
| 521 | // |
| 522 | // We need to associate the ccnx/face with a stream to express interest |
| 523 | // in tracing events on that pair, however, we only hook the trace sources |
| 524 | // once to avoid multiple trace sink calls per event (connect is independent |
| 525 | // of face). |
| 526 | // |
| 527 | if (!AsciiHooked (ccnx)) |
| 528 | { |
| 529 | Ptr<Node> node = ccnx->GetObject<Node> (); |
| 530 | std::ostringstream oss; |
| 531 | |
| 532 | // |
| 533 | // This has all kinds of parameters coming with, so we have to cook up our |
| 534 | // own sink. |
| 535 | // |
| 536 | oss.str (""); |
| 537 | oss << "/NodeList/" << node->GetId () << "/$ns3::CcnxL3Protocol/Drop"; |
| 538 | Config::Connect (oss.str (), MakeBoundCallback (&CcnxL3ProtocolDropSinkWithContext, stream)); |
| 539 | } |
| 540 | |
| 541 | g_faceStreamMapCcnx[std::make_pair (ccnx, face)] = stream; |
| 542 | } |
| 543 | |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 544 | void |
| 545 | CcnxStackHelper::InstallFakeGlobalRoutes () |
| 546 | { |
| 547 | for (NodeList::Iterator node = NodeList::Begin (); |
| 548 | node != NodeList::End (); |
| 549 | node ++) |
| 550 | { |
| 551 | NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0, |
| 552 | "InternetStack should be installed on all nodes"); |
| 553 | |
| 554 | NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> |
| 555 | ( |
| 556 | (*node)->GetObject<Ipv4> ()->GetRoutingProtocol () |
| 557 | ), |
| 558 | "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol"); |
| 559 | // Example: |
| 560 | // |
| 561 | // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops"); |
| 562 | // stack.SetRoutingHelper (ipv4RoutingHelper); |
| 563 | // |
| 564 | |
| 565 | Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> (); |
| 566 | if (globalRouter == 0) continue; |
| 567 | |
| 568 | globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255")); |
| 569 | } |
| 570 | |
| 571 | Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables (); |
| 572 | } |
| 573 | |
| 574 | void |
| 575 | CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode) |
| 576 | { |
| 577 | std::ostringstream destPrefix; |
| 578 | destPrefix << "/" << destNode->GetId (); |
| 579 | |
| 580 | Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ()); |
| 581 | |
| 582 | for (NodeList::Iterator node = NodeList::Begin (); |
| 583 | node != NodeList::End (); |
| 584 | node ++) |
| 585 | { |
| 586 | if (destNode == *node) continue; |
| 587 | |
| 588 | Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> (); |
| 589 | NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes"); |
| 590 | |
| 591 | Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> (); |
| 592 | NS_ASSERT_MSG (ipv4 != 0, |
| 593 | "InternetStack should be installed on all nodes"); |
| 594 | |
| 595 | Ptr<Ipv4GlobalRoutingOrderedNexthops> routing = |
| 596 | Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ()); |
| 597 | NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack"); |
| 598 | |
| 599 | Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer> |
| 600 | routes = routing->Lookup (destIpv4); |
| 601 | |
| 602 | NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer"); |
| 603 | |
| 604 | BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes) |
| 605 | { |
| 606 | Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ()); |
| 607 | NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos"); |
| 608 | |
| 609 | Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice); |
| 610 | NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president"); |
| 611 | |
| 612 | AddRoute (*node, destPrefix.str(), face, entry.GetMetric ()); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | void |
| 618 | CcnxStackHelper::InstallRoutesToAll () |
| 619 | { |
| 620 | for (NodeList::Iterator node = NodeList::Begin (); |
| 621 | node != NodeList::End (); |
| 622 | node ++) |
| 623 | { |
| 624 | InstallRouteTo (*node); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 629 | } // namespace ns3 |