blob: 781bb72123be8066a992cd8bc034427ba0778446 [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
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127 if (!IsUp ())
128 return false;
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800129
130 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 if (m_bucketMax > 0)
133 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800134 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800136 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800137 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800138 return false;
139 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140
141 m_bucket += 1.0;
142 }
143
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144 return true;
145}
146
147bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800148CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800150 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800151
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152 /// \todo Implement tracing, if requested
153
154 if (!IsUp ())
155 return false;
156
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800157 if (m_enableMetricTagging)
158 {
159 // update path information
160 Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
161 Ptr<WeightsPathStretchTag> tag;
162 if (origTag == 0)
163 {
164 tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
165 }
166 else
167 {
168 tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
169 }
170
171 tag->AddPathInfo (m_node, GetMetric ());
172 packet->AddPacketTag (tag);
173 }
174
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800175 SendImpl (packet);
176 return true;
177}
178
179bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800180CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800182 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800183
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184 /// \todo Implement tracing, if requested
185
186 if (!IsUp ())
187 return false;
188
189 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800190
191 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192}
193
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800194void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800195CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800196{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800197 if (m_lastLeakTime.IsZero ())
198 {
199 m_lastLeakTime = Simulator::Now ();
200 return;
201 }
202
203 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800204 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800205 if (leak >= 1.0)
206 {
207 m_bucket = std::max (0.0, m_bucket - leak);
208 m_lastLeakTime = Simulator::Now ();
209 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800210
211 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
212}
213
214void
215CcnxFace::SetBucketMax (double bucket)
216{
217 NS_LOG_FUNCTION (this << bucket);
218 m_bucketMax = bucket;
219}
220
221void
222CcnxFace::SetBucketLeak (double leak)
223{
224 NS_LOG_FUNCTION (this << leak);
225 m_bucketLeak = leak;
226}
227
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800228void
229CcnxFace::SetMetric (uint16_t metric)
230{
231 NS_LOG_FUNCTION (metric);
232 m_metric = metric;
233}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700234
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800235uint16_t
236CcnxFace::GetMetric (void) const
237{
238 NS_LOG_FUNCTION_NOARGS ();
239 return m_metric;
240}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241
242/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700243 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700244 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700245 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700246 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800247
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700249CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250{
251 NS_LOG_FUNCTION_NOARGS ();
252 return m_ifup;
253}
254
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700255void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800256CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700257{
258 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800259 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260}
261
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700262bool
263CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700264{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800265 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
266 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700267
268 return (m_id == face.m_id);
269}
270
271bool
272CcnxFace::operator< (const CcnxFace &face) const
273{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800274 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
275 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700276
277 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700278}
279
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700280std::ostream&
281CcnxFace::Print (std::ostream &os) const
282{
283 os << "id=" << GetId ();
284 return os;
285}
286
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700287std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700288{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700289 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700290 return os;
291}
292
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700293}; // namespace ns3
294