blob: b1f7b42b051451849f545b620cc0e09bec191aee [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 Afanasyev4975f732011-12-20 17:52:19 -080030#include "ns3/simulator.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080032#include <boost/ref.hpp>
33
Alexander Afanasyev98256102011-08-14 01:00:02 -070034NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
36namespace ns3 {
37
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080038TypeId
39CcnxFace::GetTypeId ()
40{
41 static TypeId tid = TypeId ("ns3::CcnxFace")
42 .SetParent<Object> ()
43 .SetGroupName ("Ccnx")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080044 .AddAttribute ("Id", "Face id (unique integer for the CCNx stack on this node)",
45 TypeId::ATTR_GET, // allow only getting it.
46 UintegerValue (0),
47 MakeUintegerAccessor (&CcnxFace::m_id),
48 MakeUintegerChecker<uint32_t> ())
49
50 .AddAttribute ("BucketMax", "Maximum size of leaky bucket",
51 DoubleValue (-1.0),
52 MakeDoubleAccessor (&CcnxFace::m_bucketMax),
53 MakeDoubleChecker<double> ())
54 .AddAttribute ("BucketLeak", "Normalized bucket leak size",
55 DoubleValue (0.0),
56 MakeDoubleAccessor (&CcnxFace::m_bucketLeak),
57 MakeDoubleChecker<double> ())
58
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080059 ;
60 return tid;
61}
62
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070064 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065 * with no IP addresses. Before becoming useable, the user must
66 * invoke SetUp on them once an Ccnx address and mask have been set.
67 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080070 , m_bucket (0.0)
71 , m_bucketMax (-1.0)
72 , m_bucketLeak (0.0)
73 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074 , m_ifup (false)
75 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080076 , m_lastLeakTime (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077{
78 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079
80 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081}
82
Alexander Afanasyev98256102011-08-14 01:00:02 -070083CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084{
85 NS_LOG_FUNCTION_NOARGS ();
86}
87
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070088CcnxFace::CcnxFace (const CcnxFace &)
89{
90}
91
92CcnxFace& CcnxFace::operator= (const CcnxFace &)
93{
94 return *this;
95}
96
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080097Ptr<Node>
98CcnxFace::GetNode () const
99{
100 return m_node;
101}
102
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103void
104CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 NS_LOG_FUNCTION_NOARGS ();
107
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 m_protocolHandler = handler;
109}
110
111bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800112CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800114 NS_LOG_FUNCTION_NOARGS ();
115
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800117
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 if (!IsUp ())
119 return false;
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800120
121 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800122
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800123 if (m_bucketMax > 0)
124 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800125 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800127 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800128 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800129 return false;
130 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131
132 m_bucket += 1.0;
133 }
134
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135 return true;
136}
137
138bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800139CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800141 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800142
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143 /// \todo Implement tracing, if requested
144
145 if (!IsUp ())
146 return false;
147
148 SendImpl (packet);
149 return true;
150}
151
152bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800153CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800155 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800156
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800157 /// \todo Implement tracing, if requested
158
159 if (!IsUp ())
160 return false;
161
162 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800163
164 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700165}
166
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800167void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800168CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800169{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800170 if (m_lastLeakTime.IsZero ())
171 {
172 m_lastLeakTime = Simulator::Now ();
173 return;
174 }
175
176 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800177 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800178 if (leak >= 1.0)
179 {
180 m_bucket = std::max (0.0, m_bucket - leak);
181 m_lastLeakTime = Simulator::Now ();
182 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800183
184 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
185}
186
187void
188CcnxFace::SetBucketMax (double bucket)
189{
190 NS_LOG_FUNCTION (this << bucket);
191 m_bucketMax = bucket;
192}
193
194void
195CcnxFace::SetBucketLeak (double leak)
196{
197 NS_LOG_FUNCTION (this << leak);
198 m_bucketLeak = leak;
199}
200
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201// void
202// CcnxFace::SetMetric (uint16_t metric)
203// {
204// NS_LOG_FUNCTION (metric);
205// m_metric = metric;
206// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700208// uint16_t
209// CcnxFace::GetMetric (void) const
210// {
211// NS_LOG_FUNCTION_NOARGS ();
212// return m_metric;
213// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214
215/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700216 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700218 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800220
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700221bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700222CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223{
224 NS_LOG_FUNCTION_NOARGS ();
225 return m_ifup;
226}
227
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800229CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230{
231 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800232 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233}
234
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700235bool
236CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800238 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
239 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700240
241 return (m_id == face.m_id);
242}
243
244bool
245CcnxFace::operator< (const CcnxFace &face) const
246{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800247 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
248 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700249
250 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700251}
252
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700253std::ostream&
254CcnxFace::Print (std::ostream &os) const
255{
256 os << "id=" << GetId ();
257 return os;
258}
259
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700260std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700261{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700262 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700263 return os;
264}
265
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700266}; // namespace ns3
267