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