blob: 936021dc8241c3c6134fb695ffaee262304ed6e2 [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 Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-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 Afanasyev2b4c9472012-08-09 15:00:38 -070038NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039
40namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070044
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080045TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080047{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080049 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050 .SetGroupName ("Ndn")
51 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080052 TypeId::ATTR_GET, // allow only getting it.
53 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080055 MakeUintegerChecker<uint32_t> ())
56
57 .AddAttribute ("BucketMax", "Maximum size of leaky bucket",
58 DoubleValue (-1.0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 MakeDoubleAccessor (&Face::m_bucketMax),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080060 MakeDoubleChecker<double> ())
61 .AddAttribute ("BucketLeak", "Normalized bucket leak size",
62 DoubleValue (0.0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070063 MakeDoubleAccessor (&Face::m_bucketLeak),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080064 MakeDoubleChecker<double> ())
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070065
66 .AddAttribute ("RandomizeLimitChecking", "Whether or not to randomize the limit checking procedure. false (persistent) by default",
67 BooleanValue (false),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068 MakeBooleanAccessor (&Face::m_randomizeLimitChecking),
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070069 MakeBooleanChecker ())
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080070
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070071 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
72 // BooleanValue (false),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073 // MakeBooleanAccessor (&Face::m_enableMetricTagging),
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070074 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070075
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070080 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080082 ;
83 return tid;
84}
85
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070087 * By default, Ndn face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070089 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093 , m_bucket (0.0)
94 , m_bucketMax (-1.0)
95 , m_bucketLeak (0.0)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070097 , m_ifup (false)
98 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080099 , m_lastLeakTime (0)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800100 , m_metric (0)
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700101 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700102{
103 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104
105 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106}
107
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700108Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109{
110 NS_LOG_FUNCTION_NOARGS ();
111}
112
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700114{
115}
116
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700118{
119 return *this;
120}
121
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800122Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800124{
125 return m_node;
126}
127
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700129Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131 NS_LOG_FUNCTION_NOARGS ();
132
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800133 m_protocolHandler = handler;
134}
135
136bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137Face::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800138{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800139 NS_LOG_FUNCTION_NOARGS ();
140
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800141 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800142
Lucas64b85362012-01-30 16:28:37 -0800143 if (!IsUp ()){
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144 NS_LOG_INFO("Face is not up.");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145 return false;
Lucas64b85362012-01-30 16:28:37 -0800146 }
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800147
148 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800149
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150 if (m_bucketMax > 0)
151 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800152 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800154 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800155 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800156 return false;
157 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800158
Alexander Afanasyev98d85e12012-08-09 10:06:05 -0700159 if (m_randomizeLimitChecking)
160 {
161 static NormalVariable acceptanceProbability (m_bucketMax, m_bucketMax/6.0, m_bucketMax/2.0);
162 // static UniformVariable acceptanceProbability (0, m_bucketMax);
163 double value = acceptanceProbability.GetValue ();
164 if (value > m_bucketMax)
165 value -= m_bucketMax;
166
167 if (m_bucket < value)
168 {
169 m_bucket += 1.0;
170 return true;
171 }
172 else
173 return false;
174 }
175 else
176 {
177 m_bucket += 1.0;
178 return true;
179 }
180 }
181
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800182 return true;
183}
184
185bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800187{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800188 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700189 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800190
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800191 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700192 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700194 return false;
195 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800196
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700197 // if (m_enableMetricTagging)
198 // {
199 // // update path information
200 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
201 // Ptr<WeightsPathStretchTag> tag;
202 // if (origTag == 0)
203 // {
204 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
205 // }
206 // else
207 // {
208 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
209 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800210
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700211 // tag->AddPathInfo (m_node, GetMetric ());
212 // packet->AddPacketTag (tag);
213 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700214
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700215 bool ok = SendImpl (packet);
216 if (ok)
217 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700219 return true;
220 }
221 else
222 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700223 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700224 return false;
225 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800226}
227
228bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700229Face::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800230{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800231 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800232
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800233 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700234 {
235 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
236 return false;
237 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800238
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800240 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800241
242 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243}
244
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800245void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700246Face::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800247{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800248 if (m_lastLeakTime.IsZero ())
249 {
250 m_lastLeakTime = Simulator::Now ();
251 return;
252 }
253
254 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800255 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800256 if (leak >= 1.0)
257 {
258 m_bucket = std::max (0.0, m_bucket - leak);
259 m_lastLeakTime = Simulator::Now ();
260 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800261
262 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
263}
264
265void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700266Face::SetBucketMax (double bucket)
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800267{
268 NS_LOG_FUNCTION (this << bucket);
269 m_bucketMax = bucket;
270}
271
272void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700273Face::SetBucketLeak (double leak)
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800274{
275 NS_LOG_FUNCTION (this << leak);
276 m_bucketLeak = leak;
277}
278
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800279void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700280Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800281{
282 NS_LOG_FUNCTION (metric);
283 m_metric = metric;
284}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700285
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800286uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700287Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800288{
289 NS_LOG_FUNCTION_NOARGS ();
290 return m_metric;
291}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700292
293/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700294 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700295 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700296 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700297 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800298
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700299bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700300Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700301{
302 NS_LOG_FUNCTION_NOARGS ();
303 return m_ifup;
304}
305
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700306void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700307Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700308{
309 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800310 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700311}
312
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700313bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700314Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700315{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800316 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
317 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700318
319 return (m_id == face.m_id);
320}
321
322bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700323Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700324{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800325 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
326 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700327
328 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700329}
330
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700331std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700332Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700333{
334 os << "id=" << GetId ();
335 return os;
336}
337
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700338std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700339operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700340{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700341 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700342 return os;
343}
344
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700345} // namespace ndn
346} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700347