blob: ba9e4ec20e6041e26a2f04c96b2876a4f715c0f3 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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 Afanasyev56f79ea2011-08-17 23:54:27 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 *
20 */
21
Alexander Afanasyev98256102011-08-14 01:00:02 -070022#include "ccnx-face.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080024#include "ns3/packet.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070025#include "ns3/log.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/node.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070027#include "ns3/assert.h"
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080028#include "ns3/uinteger.h"
29#include "ns3/double.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080030#include "ns3/boolean.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080031#include "ns3/simulator.h"
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070032#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070034// #include "ns3/weights-path-stretch-tag.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080035
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080036#include <boost/ref.hpp>
37
Alexander Afanasyev98256102011-08-14 01:00:02 -070038NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039
40namespace ns3 {
41
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070042NS_OBJECT_ENSURE_REGISTERED (CcnxFace);
43
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080044TypeId
45CcnxFace::GetTypeId ()
46{
47 static TypeId tid = TypeId ("ns3::CcnxFace")
48 .SetParent<Object> ()
49 .SetGroupName ("Ccnx")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080050 .AddAttribute ("Id", "Face id (unique integer for the CCNx stack on this node)",
51 TypeId::ATTR_GET, // allow only getting it.
52 UintegerValue (0),
53 MakeUintegerAccessor (&CcnxFace::m_id),
54 MakeUintegerChecker<uint32_t> ())
55
56 .AddAttribute ("BucketMax", "Maximum size of leaky bucket",
57 DoubleValue (-1.0),
58 MakeDoubleAccessor (&CcnxFace::m_bucketMax),
59 MakeDoubleChecker<double> ())
60 .AddAttribute ("BucketLeak", "Normalized bucket leak size",
61 DoubleValue (0.0),
62 MakeDoubleAccessor (&CcnxFace::m_bucketLeak),
63 MakeDoubleChecker<double> ())
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070064
65 .AddAttribute ("RandomizeLimitChecking", "Whether or not to randomize the limit checking procedure. false (persistent) by default",
66 BooleanValue (false),
67 MakeBooleanAccessor (&CcnxFace::m_randomizeLimitChecking),
68 MakeBooleanChecker ())
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080069
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070070 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
71 // BooleanValue (false),
72 // MakeBooleanAccessor (&CcnxFace::m_enableMetricTagging),
73 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070074
75 .AddTraceSource ("CcnxTx", "Transmitted packet trace",
76 MakeTraceSourceAccessor (&CcnxFace::m_ccnxTxTrace))
77 .AddTraceSource ("CcnxRx", "Received packet trace",
78 MakeTraceSourceAccessor (&CcnxFace::m_ccnxRxTrace))
79 .AddTraceSource ("CcnxDrop", "Dropped packet trace",
80 MakeTraceSourceAccessor (&CcnxFace::m_ccnxDropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080081 ;
82 return tid;
83}
84
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070086 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070087 * with no IP addresses. Before becoming useable, the user must
88 * invoke SetUp on them once an Ccnx address and mask have been set.
89 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080090CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080092 , m_bucket (0.0)
93 , m_bucketMax (-1.0)
94 , m_bucketLeak (0.0)
95 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070096 , m_ifup (false)
97 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080098 , m_lastLeakTime (0)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080099 , m_metric (0)
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700100 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101{
102 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800103
104 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105}
106
Alexander Afanasyev98256102011-08-14 01:00:02 -0700107CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108{
109 NS_LOG_FUNCTION_NOARGS ();
110}
111
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700112CcnxFace::CcnxFace (const CcnxFace &)
113{
114}
115
116CcnxFace& CcnxFace::operator= (const CcnxFace &)
117{
118 return *this;
119}
120
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800121Ptr<Node>
122CcnxFace::GetNode () const
123{
124 return m_node;
125}
126
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127void
128CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800130 NS_LOG_FUNCTION_NOARGS ();
131
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 m_protocolHandler = handler;
133}
134
135bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800136CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800138 NS_LOG_FUNCTION_NOARGS ();
139
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800141
Lucas64b85362012-01-30 16:28:37 -0800142 if (!IsUp ()){
143 NS_LOG_INFO("CcnxFace is not up.");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144 return false;
Lucas64b85362012-01-30 16:28:37 -0800145 }
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800146
147 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800148
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149 if (m_bucketMax > 0)
150 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800151 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800153 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800154 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800155 return false;
156 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800157
Alexander Afanasyev98d85e12012-08-09 10:06:05 -0700158 if (m_randomizeLimitChecking)
159 {
160 static NormalVariable acceptanceProbability (m_bucketMax, m_bucketMax/6.0, m_bucketMax/2.0);
161 // static UniformVariable acceptanceProbability (0, m_bucketMax);
162 double value = acceptanceProbability.GetValue ();
163 if (value > m_bucketMax)
164 value -= m_bucketMax;
165
166 if (m_bucket < value)
167 {
168 m_bucket += 1.0;
169 return true;
170 }
171 else
172 return false;
173 }
174 else
175 {
176 m_bucket += 1.0;
177 return true;
178 }
179 }
180
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181 return true;
182}
183
184bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800185CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800187 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700188 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800189
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800190 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700191 {
192 m_ccnxDropTrace (packet);
193 return false;
194 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800195
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700196 // if (m_enableMetricTagging)
197 // {
198 // // update path information
199 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
200 // Ptr<WeightsPathStretchTag> tag;
201 // if (origTag == 0)
202 // {
203 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
204 // }
205 // else
206 // {
207 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
208 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800209
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700210 // tag->AddPathInfo (m_node, GetMetric ());
211 // packet->AddPacketTag (tag);
212 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700213
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700214 bool ok = SendImpl (packet);
215 if (ok)
216 {
217 m_ccnxTxTrace (packet);
218 return true;
219 }
220 else
221 {
222 m_ccnxDropTrace (packet);
223 return false;
224 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800225}
226
227bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800228CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800229{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800230 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800231
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800232 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700233 {
234 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
235 return false;
236 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800237
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700238 m_ccnxRxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800239 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800240
241 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242}
243
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800244void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800245CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800246{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800247 if (m_lastLeakTime.IsZero ())
248 {
249 m_lastLeakTime = Simulator::Now ();
250 return;
251 }
252
253 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800254 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800255 if (leak >= 1.0)
256 {
257 m_bucket = std::max (0.0, m_bucket - leak);
258 m_lastLeakTime = Simulator::Now ();
259 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800260
261 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
262}
263
264void
265CcnxFace::SetBucketMax (double bucket)
266{
267 NS_LOG_FUNCTION (this << bucket);
268 m_bucketMax = bucket;
269}
270
271void
272CcnxFace::SetBucketLeak (double leak)
273{
274 NS_LOG_FUNCTION (this << leak);
275 m_bucketLeak = leak;
276}
277
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800278void
279CcnxFace::SetMetric (uint16_t metric)
280{
281 NS_LOG_FUNCTION (metric);
282 m_metric = metric;
283}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700284
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800285uint16_t
286CcnxFace::GetMetric (void) const
287{
288 NS_LOG_FUNCTION_NOARGS ();
289 return m_metric;
290}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700291
292/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700293 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700294 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700295 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700296 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800297
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700298bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700299CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700300{
301 NS_LOG_FUNCTION_NOARGS ();
302 return m_ifup;
303}
304
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700305void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800306CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700307{
308 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800309 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700310}
311
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700312bool
313CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700314{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800315 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
316 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700317
318 return (m_id == face.m_id);
319}
320
321bool
322CcnxFace::operator< (const CcnxFace &face) const
323{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800324 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
325 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700326
327 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700328}
329
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700330std::ostream&
331CcnxFace::Print (std::ostream &os) const
332{
333 os << "id=" << GetId ();
334 return os;
335}
336
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700337std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700338{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700339 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700340 return os;
341}
342
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700343}; // namespace ns3
344