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