blob: a1dec50f262eda63b08f7a3c506e84d01656789d [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 Afanasyev30c33e32012-06-05 21:28:44 -070041NS_OBJECT_ENSURE_REGISTERED (CcnxFace);
42
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080043TypeId
44CcnxFace::GetTypeId ()
45{
46 static TypeId tid = TypeId ("ns3::CcnxFace")
47 .SetParent<Object> ()
48 .SetGroupName ("Ccnx")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080049 .AddAttribute ("Id", "Face id (unique integer for the CCNx stack on this node)",
50 TypeId::ATTR_GET, // allow only getting it.
51 UintegerValue (0),
52 MakeUintegerAccessor (&CcnxFace::m_id),
53 MakeUintegerChecker<uint32_t> ())
54
55 .AddAttribute ("BucketMax", "Maximum size of leaky bucket",
56 DoubleValue (-1.0),
57 MakeDoubleAccessor (&CcnxFace::m_bucketMax),
58 MakeDoubleChecker<double> ())
59 .AddAttribute ("BucketLeak", "Normalized bucket leak size",
60 DoubleValue (0.0),
61 MakeDoubleAccessor (&CcnxFace::m_bucketLeak),
62 MakeDoubleChecker<double> ())
63
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070064 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
65 // BooleanValue (false),
66 // MakeBooleanAccessor (&CcnxFace::m_enableMetricTagging),
67 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070068
69 .AddTraceSource ("CcnxTx", "Transmitted packet trace",
70 MakeTraceSourceAccessor (&CcnxFace::m_ccnxTxTrace))
71 .AddTraceSource ("CcnxRx", "Received packet trace",
72 MakeTraceSourceAccessor (&CcnxFace::m_ccnxRxTrace))
73 .AddTraceSource ("CcnxDrop", "Dropped packet trace",
74 MakeTraceSourceAccessor (&CcnxFace::m_ccnxDropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080075 ;
76 return tid;
77}
78
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070079/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070080 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081 * with no IP addresses. Before becoming useable, the user must
82 * invoke SetUp on them once an Ccnx address and mask have been set.
83 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080084CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086 , m_bucket (0.0)
87 , m_bucketMax (-1.0)
88 , m_bucketLeak (0.0)
89 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070090 , m_ifup (false)
91 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080092 , m_lastLeakTime (0)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080093 , m_metric (0)
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070094 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070095{
96 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097
98 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099}
100
Alexander Afanasyev98256102011-08-14 01:00:02 -0700101CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700102{
103 NS_LOG_FUNCTION_NOARGS ();
104}
105
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700106CcnxFace::CcnxFace (const CcnxFace &)
107{
108}
109
110CcnxFace& CcnxFace::operator= (const CcnxFace &)
111{
112 return *this;
113}
114
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800115Ptr<Node>
116CcnxFace::GetNode () const
117{
118 return m_node;
119}
120
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121void
122CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800124 NS_LOG_FUNCTION_NOARGS ();
125
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126 m_protocolHandler = handler;
127}
128
129bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800130CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800132 NS_LOG_FUNCTION_NOARGS ();
133
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800135
Lucas64b85362012-01-30 16:28:37 -0800136 if (!IsUp ()){
137 NS_LOG_INFO("CcnxFace is not up.");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800138 return false;
Lucas64b85362012-01-30 16:28:37 -0800139 }
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800140
141 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800142
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143 if (m_bucketMax > 0)
144 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800145 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800147 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800148 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800149 return false;
150 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151
152 m_bucket += 1.0;
153 }
154
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800155 return true;
156}
157
158bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800159CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800161 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700162 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800163
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700165 {
166 m_ccnxDropTrace (packet);
167 return false;
168 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800169
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700170 // if (m_enableMetricTagging)
171 // {
172 // // update path information
173 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
174 // Ptr<WeightsPathStretchTag> tag;
175 // if (origTag == 0)
176 // {
177 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
178 // }
179 // else
180 // {
181 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
182 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800183
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700184 // tag->AddPathInfo (m_node, GetMetric ());
185 // packet->AddPacketTag (tag);
186 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700187
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700188 bool ok = SendImpl (packet);
189 if (ok)
190 {
191 m_ccnxTxTrace (packet);
192 return true;
193 }
194 else
195 {
196 m_ccnxDropTrace (packet);
197 return false;
198 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800199}
200
201bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800202CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800203{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800204 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800205
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800206 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700207 {
208 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
209 return false;
210 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800211
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700212 m_ccnxRxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800213 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800214
215 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216}
217
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800218void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800219CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800220{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800221 if (m_lastLeakTime.IsZero ())
222 {
223 m_lastLeakTime = Simulator::Now ();
224 return;
225 }
226
227 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800228 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800229 if (leak >= 1.0)
230 {
231 m_bucket = std::max (0.0, m_bucket - leak);
232 m_lastLeakTime = Simulator::Now ();
233 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800234
235 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
236}
237
238void
239CcnxFace::SetBucketMax (double bucket)
240{
241 NS_LOG_FUNCTION (this << bucket);
242 m_bucketMax = bucket;
243}
244
245void
246CcnxFace::SetBucketLeak (double leak)
247{
248 NS_LOG_FUNCTION (this << leak);
249 m_bucketLeak = leak;
250}
251
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800252void
253CcnxFace::SetMetric (uint16_t metric)
254{
255 NS_LOG_FUNCTION (metric);
256 m_metric = metric;
257}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800259uint16_t
260CcnxFace::GetMetric (void) const
261{
262 NS_LOG_FUNCTION_NOARGS ();
263 return m_metric;
264}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700265
266/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700267 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700268 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700269 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700270 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800271
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700272bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700273CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700274{
275 NS_LOG_FUNCTION_NOARGS ();
276 return m_ifup;
277}
278
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700279void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800280CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700281{
282 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800283 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700284}
285
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700286bool
287CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700288{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800289 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
290 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700291
292 return (m_id == face.m_id);
293}
294
295bool
296CcnxFace::operator< (const CcnxFace &face) const
297{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800298 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
299 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700300
301 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700302}
303
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700304std::ostream&
305CcnxFace::Print (std::ostream &os) const
306{
307 os << "id=" << GetId ();
308 return os;
309}
310
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700311std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700312{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700313 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700314 return os;
315}
316
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700317}; // namespace ns3
318