blob: 78266fe9fd722ff0cddb3cb18477d7ed2a16ac57 [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 Afanasyev8e0d2812012-01-19 22:38:14 -080033#include "ccnx-path-stretch-tag.h"
34
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 Afanasyev8e0d2812012-01-19 22:38:14 -080062 .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 Afanasyev19426ef2011-11-23 20:55:28 -0800153
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154 /// \todo Implement tracing, if requested
155
156 if (!IsUp ())
157 return false;
158
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800159 if (m_enableMetricTagging)
160 {
161 // update path information
162 Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
163 Ptr<WeightsPathStretchTag> tag;
164 if (origTag == 0)
165 {
166 tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
167 }
168 else
169 {
170 tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
171 }
172
173 tag->AddPathInfo (m_node, GetMetric ());
174 packet->AddPacketTag (tag);
175 }
176
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177 SendImpl (packet);
178 return true;
179}
180
181bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800182CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800183{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800184 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800185
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186 /// \todo Implement tracing, if requested
187
188 if (!IsUp ())
189 return false;
190
191 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800192
193 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194}
195
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800196void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800197CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800198{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800199 if (m_lastLeakTime.IsZero ())
200 {
201 m_lastLeakTime = Simulator::Now ();
202 return;
203 }
204
205 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800206 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800207 if (leak >= 1.0)
208 {
209 m_bucket = std::max (0.0, m_bucket - leak);
210 m_lastLeakTime = Simulator::Now ();
211 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800212
213 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
214}
215
216void
217CcnxFace::SetBucketMax (double bucket)
218{
219 NS_LOG_FUNCTION (this << bucket);
220 m_bucketMax = bucket;
221}
222
223void
224CcnxFace::SetBucketLeak (double leak)
225{
226 NS_LOG_FUNCTION (this << leak);
227 m_bucketLeak = leak;
228}
229
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800230void
231CcnxFace::SetMetric (uint16_t metric)
232{
233 NS_LOG_FUNCTION (metric);
234 m_metric = metric;
235}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700236
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800237uint16_t
238CcnxFace::GetMetric (void) const
239{
240 NS_LOG_FUNCTION_NOARGS ();
241 return m_metric;
242}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243
244/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700245 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700246 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700247 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800249
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700251CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700252{
253 NS_LOG_FUNCTION_NOARGS ();
254 return m_ifup;
255}
256
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700257void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800258CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700259{
260 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800261 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262}
263
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700264bool
265CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700266{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800267 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
268 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700269
270 return (m_id == face.m_id);
271}
272
273bool
274CcnxFace::operator< (const CcnxFace &face) const
275{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800276 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
277 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700278
279 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700280}
281
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700282std::ostream&
283CcnxFace::Print (std::ostream &os) const
284{
285 os << "id=" << GetId ();
286 return os;
287}
288
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700289std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700290{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700291 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700292 return os;
293}
294
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700295}; // namespace ns3
296