blob: 86daa2674c35ee37f7ef6866d868edb7139e745a [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 Afanasyev08d984e2011-08-13 19:20:22 -070032
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070033// #include "ns3/weights-path-stretch-tag.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080034
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080035#include <boost/ref.hpp>
36
Alexander Afanasyev98256102011-08-14 01:00:02 -070037NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070038
39namespace ns3 {
40
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080041TypeId
42CcnxFace::GetTypeId ()
43{
44 static TypeId tid = TypeId ("ns3::CcnxFace")
45 .SetParent<Object> ()
46 .SetGroupName ("Ccnx")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080047 .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 Afanasyevde7601a2012-05-31 12:14:54 -070062 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
63 // BooleanValue (false),
64 // MakeBooleanAccessor (&CcnxFace::m_enableMetricTagging),
65 // MakeBooleanChecker ())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080066 ;
67 return tid;
68}
69
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070070/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070071 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072 * 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 Afanasyev09c7deb2011-11-23 14:50:10 -080075CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080076 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 , 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 Afanasyev56f79ea2011-08-17 23:54:27 -070081 , m_ifup (false)
82 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080083 , m_lastLeakTime (0)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080084 , m_metric (0)
85 , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086{
87 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080088
89 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090}
91
Alexander Afanasyev98256102011-08-14 01:00:02 -070092CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070093{
94 NS_LOG_FUNCTION_NOARGS ();
95}
96
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070097CcnxFace::CcnxFace (const CcnxFace &)
98{
99}
100
101CcnxFace& CcnxFace::operator= (const CcnxFace &)
102{
103 return *this;
104}
105
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800106Ptr<Node>
107CcnxFace::GetNode () const
108{
109 return m_node;
110}
111
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112void
113CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700114{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800115 NS_LOG_FUNCTION_NOARGS ();
116
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 m_protocolHandler = handler;
118}
119
120bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800121CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800122{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800123 NS_LOG_FUNCTION_NOARGS ();
124
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800126
Lucas64b85362012-01-30 16:28:37 -0800127 if (!IsUp ()){
128 NS_LOG_INFO("CcnxFace is not up.");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129 return false;
Lucas64b85362012-01-30 16:28:37 -0800130 }
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800131
132 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800133
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134 if (m_bucketMax > 0)
135 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800136 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800138 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800139 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800140 return false;
141 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142
143 m_bucket += 1.0;
144 }
145
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146 return true;
147}
148
149bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800150CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800152 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700153 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800154
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800155 /// \todo Implement tracing, if requested
156
157 if (!IsUp ())
158 return false;
159
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700160 // if (m_enableMetricTagging)
161 // {
162 // // update path information
163 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
164 // Ptr<WeightsPathStretchTag> tag;
165 // if (origTag == 0)
166 // {
167 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
168 // }
169 // else
170 // {
171 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
172 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800173
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700174 // tag->AddPathInfo (m_node, GetMetric ());
175 // packet->AddPacketTag (tag);
176 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800177
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178 SendImpl (packet);
179 return true;
180}
181
182bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800183CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800185 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800186
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800187 /// \todo Implement tracing, if requested
188
189 if (!IsUp ())
190 return false;
191
192 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800193
194 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195}
196
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800197void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800198CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800199{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800200 if (m_lastLeakTime.IsZero ())
201 {
202 m_lastLeakTime = Simulator::Now ();
203 return;
204 }
205
206 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800207 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800208 if (leak >= 1.0)
209 {
210 m_bucket = std::max (0.0, m_bucket - leak);
211 m_lastLeakTime = Simulator::Now ();
212 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800213
214 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
215}
216
217void
218CcnxFace::SetBucketMax (double bucket)
219{
220 NS_LOG_FUNCTION (this << bucket);
221 m_bucketMax = bucket;
222}
223
224void
225CcnxFace::SetBucketLeak (double leak)
226{
227 NS_LOG_FUNCTION (this << leak);
228 m_bucketLeak = leak;
229}
230
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800231void
232CcnxFace::SetMetric (uint16_t metric)
233{
234 NS_LOG_FUNCTION (metric);
235 m_metric = metric;
236}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800238uint16_t
239CcnxFace::GetMetric (void) const
240{
241 NS_LOG_FUNCTION_NOARGS ();
242 return m_metric;
243}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700244
245/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700246 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700248 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700249 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800250
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700251bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700252CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700253{
254 NS_LOG_FUNCTION_NOARGS ();
255 return m_ifup;
256}
257
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800259CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260{
261 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800262 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700263}
264
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700265bool
266CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700267{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800268 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
269 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700270
271 return (m_id == face.m_id);
272}
273
274bool
275CcnxFace::operator< (const CcnxFace &face) const
276{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800277 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
278 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700279
280 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700281}
282
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700283std::ostream&
284CcnxFace::Print (std::ostream &os) const
285{
286 os << "id=" << GetId ();
287 return os;
288}
289
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700290std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700291{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700292 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700293 return os;
294}
295
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700296}; // namespace ns3
297