blob: 3c18af05e6c5390c29afc6e9a7c6f8bc145a5907 [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
188 m_ccnxTxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 SendImpl (packet);
190 return true;
191}
192
193bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800194CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800195{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800196 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800197
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800198 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700199 {
200 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
201 return false;
202 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800203
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700204 m_ccnxRxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800205 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800206
207 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208}
209
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800210void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800211CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800212{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800213 if (m_lastLeakTime.IsZero ())
214 {
215 m_lastLeakTime = Simulator::Now ();
216 return;
217 }
218
219 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800220 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800221 if (leak >= 1.0)
222 {
223 m_bucket = std::max (0.0, m_bucket - leak);
224 m_lastLeakTime = Simulator::Now ();
225 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800226
227 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
228}
229
230void
231CcnxFace::SetBucketMax (double bucket)
232{
233 NS_LOG_FUNCTION (this << bucket);
234 m_bucketMax = bucket;
235}
236
237void
238CcnxFace::SetBucketLeak (double leak)
239{
240 NS_LOG_FUNCTION (this << leak);
241 m_bucketLeak = leak;
242}
243
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800244void
245CcnxFace::SetMetric (uint16_t metric)
246{
247 NS_LOG_FUNCTION (metric);
248 m_metric = metric;
249}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800251uint16_t
252CcnxFace::GetMetric (void) const
253{
254 NS_LOG_FUNCTION_NOARGS ();
255 return m_metric;
256}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700257
258/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700259 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700261 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800263
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700264bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700265CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700266{
267 NS_LOG_FUNCTION_NOARGS ();
268 return m_ifup;
269}
270
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700271void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800272CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700273{
274 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800275 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700276}
277
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700278bool
279CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700280{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800281 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
282 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700283
284 return (m_id == face.m_id);
285}
286
287bool
288CcnxFace::operator< (const CcnxFace &face) const
289{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800290 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
291 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700292
293 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700294}
295
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700296std::ostream&
297CcnxFace::Print (std::ostream &os) const
298{
299 os << "id=" << GetId ();
300 return os;
301}
302
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700303std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700304{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700305 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700306 return os;
307}
308
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700309}; // namespace ns3
310