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