blob: 625af55fc5c25b1a76ac17e07fd7ca09017cf8c0 [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 Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-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 Afanasyev98d85e12012-08-09 10:06:05 -070032#include "ns3/random-variable.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070033#include "ns3/pointer.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080035#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080036
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080037#include <boost/ref.hpp>
38
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070040
41namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070045
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080046TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080048{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080050 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051 .SetGroupName ("Ndn")
52 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080053 TypeId::ATTR_GET, // allow only getting it.
54 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080056 MakeUintegerChecker<uint32_t> ())
57
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070060 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070063 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080064 ;
65 return tid;
66}
67
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080068/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080070 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080073Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080074 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070075 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070076 , m_ifup (false)
77 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080078 , m_metric (0)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070079 , m_flags (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080{
81 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080082
83 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084}
85
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070087{
88 NS_LOG_FUNCTION_NOARGS ();
89}
90
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070092{
93}
94
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070096{
97 return *this;
98}
99
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800100Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800102{
103 return m_node;
104}
105
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800109 NS_LOG_FUNCTION_NOARGS ();
110
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111 m_protocolHandler = handler;
112}
113
114bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800117 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700118 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800119
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700121 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700123 return false;
124 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800126 FwHopCountTag hopCount;
127 bool tagExists = packet->RemovePacketTag (hopCount);
128 if (tagExists)
129 {
130 hopCount.Increment ();
131 packet->AddPacketTag (hopCount);
132 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700133
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700134 bool ok = SendImpl (packet);
135 if (ok)
136 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700138 return true;
139 }
140 else
141 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700143 return false;
144 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145}
146
147bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148Face::Receive (const Ptr<const 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 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700153 {
154 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
155 return false;
156 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800157
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700158 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159 m_protocolHandler (this, packet);
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800160
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800161 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162}
163
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800164void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800166{
167 NS_LOG_FUNCTION (metric);
168 m_metric = metric;
169}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800171uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700172Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800173{
174 NS_LOG_FUNCTION_NOARGS ();
175 return m_metric;
176}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700177
178/**
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800179 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700180 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700181 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800183
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800184bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700186{
187 NS_LOG_FUNCTION_NOARGS ();
188 return m_ifup;
189}
190
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800191void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700193{
194 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800195 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196}
197
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700198void
199Face::SetFlags (uint32_t flags)
200{
201 m_flags = flags;
202}
203
204
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700205bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800208 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800209 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700210
211 return (m_id == face.m_id);
212}
213
214bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700216{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800217 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800218 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700219
220 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700221}
222
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700223std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700224Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700225{
226 os << "id=" << GetId ();
227 return os;
228}
229
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700230std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700232{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700234 return os;
235}
236
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700237} // namespace ndn
238} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700239