Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 2 | /* |
Ilya Moiseenko | 956d054 | 2012-01-02 15:26:40 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011 University of California, Los Angeles |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 22 | #include "ccnx-face.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 24 | #include "ns3/packet.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 26 | #include "ns3/node.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 27 | #include "ns3/assert.h" |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 28 | #include "ns3/uinteger.h" |
| 29 | #include "ns3/double.h" |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 30 | #include "ns3/boolean.h" |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 31 | #include "ns3/simulator.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 33 | #include "ccnx-path-stretch-tag.h" |
| 34 | |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 35 | #include <boost/ref.hpp> |
| 36 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 37 | NS_LOG_COMPONENT_DEFINE ("CcnxFace"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ns3 { |
| 40 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 41 | TypeId |
| 42 | CcnxFace::GetTypeId () |
| 43 | { |
| 44 | static TypeId tid = TypeId ("ns3::CcnxFace") |
| 45 | .SetParent<Object> () |
| 46 | .SetGroupName ("Ccnx") |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 47 | .AddAttribute ("Id", "Face id (unique integer for the CCNx stack on this node)", |
| 48 | TypeId::ATTR_GET, // allow only getting it. |
| 49 | UintegerValue (0), |
| 50 | MakeUintegerAccessor (&CcnxFace::m_id), |
| 51 | MakeUintegerChecker<uint32_t> ()) |
| 52 | |
| 53 | .AddAttribute ("BucketMax", "Maximum size of leaky bucket", |
| 54 | DoubleValue (-1.0), |
| 55 | MakeDoubleAccessor (&CcnxFace::m_bucketMax), |
| 56 | MakeDoubleChecker<double> ()) |
| 57 | .AddAttribute ("BucketLeak", "Normalized bucket leak size", |
| 58 | DoubleValue (0.0), |
| 59 | MakeDoubleAccessor (&CcnxFace::m_bucketLeak), |
| 60 | MakeDoubleChecker<double> ()) |
| 61 | |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 62 | .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)", |
| 63 | BooleanValue (false), |
| 64 | MakeBooleanAccessor (&CcnxFace::m_enableMetricTagging), |
| 65 | MakeBooleanChecker ()) |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 66 | ; |
| 67 | return tid; |
| 68 | } |
| 69 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 70 | /** |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 71 | * By default, Ccnx face are created in the "down" state |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 72 | * with no IP addresses. Before becoming useable, the user must |
| 73 | * invoke SetUp on them once an Ccnx address and mask have been set. |
| 74 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 75 | CcnxFace::CcnxFace (Ptr<Node> node) |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 76 | : m_node (node) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 77 | , m_bucket (0.0) |
| 78 | , m_bucketMax (-1.0) |
| 79 | , m_bucketLeak (0.0) |
| 80 | , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ()) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 81 | , m_ifup (false) |
| 82 | , m_id ((uint32_t)-1) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 83 | , m_lastLeakTime (0) |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 84 | , m_metric (0) |
| 85 | , m_enableMetricTagging (false) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 86 | { |
| 87 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 88 | |
| 89 | NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 92 | CcnxFace::~CcnxFace () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 93 | { |
| 94 | NS_LOG_FUNCTION_NOARGS (); |
| 95 | } |
| 96 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 97 | CcnxFace::CcnxFace (const CcnxFace &) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | CcnxFace& CcnxFace::operator= (const CcnxFace &) |
| 102 | { |
| 103 | return *this; |
| 104 | } |
| 105 | |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 106 | Ptr<Node> |
| 107 | CcnxFace::GetNode () const |
| 108 | { |
| 109 | return m_node; |
| 110 | } |
| 111 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 112 | void |
| 113 | CcnxFace::RegisterProtocolHandler (ProtocolHandler handler) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 114 | { |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 115 | NS_LOG_FUNCTION_NOARGS (); |
| 116 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 117 | m_protocolHandler = handler; |
| 118 | } |
| 119 | |
| 120 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 121 | CcnxFace::IsBelowLimit () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 122 | { |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 123 | NS_LOG_FUNCTION_NOARGS (); |
| 124 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 125 | /// \todo Implement tracing, if requested |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 127 | if (!IsUp ()) |
| 128 | return false; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 129 | |
| 130 | LeakBucket (); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 132 | if (m_bucketMax > 0) |
| 133 | { |
Alexander Afanasyev | 120bf31 | 2011-12-19 01:24:47 -0800 | [diff] [blame] | 134 | NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 135 | if (m_bucket+1.0 > m_bucketMax) |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 136 | { |
Alexander Afanasyev | e67a97f | 2011-11-29 14:28:59 -0800 | [diff] [blame] | 137 | //NS_LOG_DEBUG ("Returning false"); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 138 | return false; |
| 139 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 140 | |
| 141 | m_bucket += 1.0; |
| 142 | } |
| 143 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 148 | CcnxFace::Send (Ptr<Packet> packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 149 | { |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 150 | NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ()); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 152 | /// \todo Implement tracing, if requested |
| 153 | |
| 154 | if (!IsUp ()) |
| 155 | return false; |
| 156 | |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 157 | if (m_enableMetricTagging) |
| 158 | { |
| 159 | // update path information |
| 160 | Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> (); |
| 161 | Ptr<WeightsPathStretchTag> tag; |
| 162 | if (origTag == 0) |
| 163 | { |
| 164 | tag = CreateObject<WeightsPathStretchTag> (); // create a new tag |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag |
| 169 | } |
| 170 | |
| 171 | tag->AddPathInfo (m_node, GetMetric ()); |
| 172 | packet->AddPacketTag (tag); |
| 173 | } |
| 174 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 175 | SendImpl (packet); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 180 | CcnxFace::Receive (const Ptr<const Packet> &packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 181 | { |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 182 | NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ()); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 184 | /// \todo Implement tracing, if requested |
| 185 | |
| 186 | if (!IsUp ()) |
| 187 | return false; |
| 188 | |
| 189 | m_protocolHandler (this, packet); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 190 | |
| 191 | return true; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 194 | void |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 195 | CcnxFace::LeakBucket () |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 196 | { |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 197 | if (m_lastLeakTime.IsZero ()) |
| 198 | { |
| 199 | m_lastLeakTime = Simulator::Now (); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | Time interval = Simulator::Now () - m_lastLeakTime; |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 204 | const double leak = m_bucketLeak * interval.ToDouble (Time::S); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 205 | if (leak >= 1.0) |
| 206 | { |
| 207 | m_bucket = std::max (0.0, m_bucket - leak); |
| 208 | m_lastLeakTime = Simulator::Now (); |
| 209 | } |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 210 | |
| 211 | // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | CcnxFace::SetBucketMax (double bucket) |
| 216 | { |
| 217 | NS_LOG_FUNCTION (this << bucket); |
| 218 | m_bucketMax = bucket; |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | CcnxFace::SetBucketLeak (double leak) |
| 223 | { |
| 224 | NS_LOG_FUNCTION (this << leak); |
| 225 | m_bucketLeak = leak; |
| 226 | } |
| 227 | |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 228 | void |
| 229 | CcnxFace::SetMetric (uint16_t metric) |
| 230 | { |
| 231 | NS_LOG_FUNCTION (metric); |
| 232 | m_metric = metric; |
| 233 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 234 | |
Alexander Afanasyev | 8e0d281 | 2012-01-19 22:38:14 -0800 | [diff] [blame] | 235 | uint16_t |
| 236 | CcnxFace::GetMetric (void) const |
| 237 | { |
| 238 | NS_LOG_FUNCTION_NOARGS (); |
| 239 | return m_metric; |
| 240 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 243 | * These are face states and may be distinct from |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 244 | * NetDevice states, such as found in real implementations |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 245 | * (where the device may be down but face state is still up). |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 246 | */ |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 247 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 248 | bool |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 249 | CcnxFace::IsUp (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 250 | { |
| 251 | NS_LOG_FUNCTION_NOARGS (); |
| 252 | return m_ifup; |
| 253 | } |
| 254 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 255 | void |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 256 | CcnxFace::SetUp (bool up/* = true*/) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 257 | { |
| 258 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 259 | m_ifup = up; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 262 | bool |
| 263 | CcnxFace::operator== (const CcnxFace &face) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 264 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 265 | NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (), |
| 266 | "Faces of different nodes should not be compared to each other"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 267 | |
| 268 | return (m_id == face.m_id); |
| 269 | } |
| 270 | |
| 271 | bool |
| 272 | CcnxFace::operator< (const CcnxFace &face) const |
| 273 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 274 | NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (), |
| 275 | "Faces of different nodes should not be compared to each other"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 276 | |
| 277 | return (m_id < face.m_id); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 280 | std::ostream& |
| 281 | CcnxFace::Print (std::ostream &os) const |
| 282 | { |
| 283 | os << "id=" << GetId (); |
| 284 | return os; |
| 285 | } |
| 286 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 287 | std::ostream& operator<< (std::ostream& os, const CcnxFace &face) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 288 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 289 | face.Print (os); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 290 | return os; |
| 291 | } |
| 292 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 293 | }; // namespace ns3 |
| 294 | |