blob: 315ad63cff2a8aeb26d7280b452466c1873e7c79 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07002/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
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 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko172763c2011-10-28 13:21:53 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070020 */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070021
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-l3-protocol.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070023
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070025#include "ns3/node.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/log.h"
27#include "ns3/callback.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028#include "ns3/uinteger.h"
29#include "ns3/trace-source-accessor.h"
30#include "ns3/object-vector.h"
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080031#include "ns3/pointer.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080032#include "ns3/simulator.h"
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070033#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035#include "ns3/ndn-header-helper.h"
36#include "ns3/ndn-pit.h"
37#include "ns3/ndn-interest-header.h"
38#include "ns3/ndn-content-object-header.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070039
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040#include "ns3/ndn-face.h"
41#include "ns3/ndn-forwarding-strategy.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070042
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043#include "ndn-net-device-face.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080044
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070045#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047NS_LOG_COMPONENT_DEFINE ("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053
Alexander Afanasyev07827182011-12-13 01:07:32 -080054
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055NS_OBJECT_ENSURE_REGISTERED (L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056
57TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058L3Protocol::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 static TypeId tid = TypeId ("ns3::ndn::L3Protocol")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070061 .SetGroupName ("ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062 .AddConstructor<L3Protocol> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 .AddAttribute ("FaceList", "List of faces associated with ndn stack",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080064 ObjectVectorValue (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 MakeObjectVectorAccessor (&L3Protocol::m_faces),
66 MakeObjectVectorChecker<Face> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 ;
68 return tid;
69}
70
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071L3Protocol::L3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070072: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073{
74 NS_LOG_FUNCTION (this);
75}
76
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077L3Protocol::~L3Protocol ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078{
79 NS_LOG_FUNCTION (this);
80}
81
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082/*
83 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085 */
86void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087L3Protocol::NotifyNewAggregate ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070089 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090 if (m_node == 0)
91 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070092 m_node = GetObject<Node> ();
93 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070095 // NS_ASSERT_MSG (m_pit != 0 && m_fib != 0 && m_contentStore != 0 && m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 // "PIT, FIB, and ContentStore should be aggregated before L3Protocol");
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070097 NS_ASSERT_MSG (m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099 }
100 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700101 // if (m_pit == 0)
102 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103 // m_pit = GetObject<Pit> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700104 // }
105 // if (m_fib == 0)
106 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107 // m_fib = GetObject<Fib> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700108 // }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700109 if (m_forwardingStrategy == 0)
110 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111 m_forwardingStrategy = GetObject<ForwardingStrategy> ();
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700112 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700113 // if (m_contentStore == 0)
114 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115 // m_contentStore = GetObject<ContentStore> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700116 // }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700117
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700118 Object::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700119}
120
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122L3Protocol::DoDispose (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123{
124 NS_LOG_FUNCTION (this);
125
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126 for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700127 {
128 *i = 0;
129 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700130 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800132
133 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800134 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800135
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136 Object::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137}
138
139uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140L3Protocol::AddFace (const Ptr<Face> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700141{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700142 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700143
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700144 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700145
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700146 // ask face to register in lower-layer stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147 face->RegisterProtocolHandler (MakeCallback (&L3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700148
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700149 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700151 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700152}
153
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700154void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155L3Protocol::RemoveFace (Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700156{
157 // ask face to register in lower-layer stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700158 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ());
159 Ptr<Pit> pit = GetObject<Pit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160
161 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700162 std::list< Ptr<pit::Entry> > entriesToRemoves;
163 for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700165 pitEntry->RemoveAllReferencesToFace (face);
166
167 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
168 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700169 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
170 pitEntry->GetFibEntry ()->m_faces.begin ()->m_face == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700171 {
172 entriesToRemoves.push_back (pitEntry);
173 }
174 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700176 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700177 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178 }
179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180 FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700181 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
182 m_faces.erase (face_it);
183}
184
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185Ptr<Face>
186L3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700188 BOOST_FOREACH (const Ptr<Face> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700189 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700190 if (face->GetId () == index)
191 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192 }
193 return 0;
194}
195
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196Ptr<Face>
197L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800198{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199 BOOST_FOREACH (const Ptr<Face> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800200 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800202 if (netDeviceFace == 0) continue;
203
204 if (netDeviceFace->GetNetDevice () == netDevice)
205 return face;
206 }
207 return 0;
208}
209
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700211L3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700213 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214}
215
Alexander Afanasyev98256102011-08-14 01:00:02 -0700216// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218L3Protocol::Receive (const Ptr<Face> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219{
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700220 if (!face->IsUp ())
221 return;
222
223 NS_LOG_DEBUG (*p);
224
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700225 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700227 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700228 try
229 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700231 switch (type)
232 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700233 case HeaderHelper::INTEREST:
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700234 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235 Ptr<InterestHeader> header = Create<InterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700236
237 // Deserialization. Exception may be thrown
238 packet->RemoveHeader (*header);
239 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800240
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700241 m_forwardingStrategy->OnInterest (face, header, p/*original packet*/);
242 // if (header->GetNack () > 0)
243 // OnNack (face, header, p/*original packet*/);
244 // else
245 // OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700246 break;
247 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248 case HeaderHelper::CONTENT_OBJECT:
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700249 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700250 Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700251
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252 static ContentObjectTail contentObjectTrailer; //there is no data in this object
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700253
254 // Deserialization. Exception may be thrown
255 packet->RemoveHeader (*header);
256 packet->RemoveTrailer (contentObjectTrailer);
257
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700258 m_forwardingStrategy->OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700259 break;
260 }
261 }
262
263 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700264 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700265 catch (UnknownHeaderException)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700266 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700267 NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen");
268 NS_LOG_ERROR ("Unknown NDN header. Should not happen");
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700269 return;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700270 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700271}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700272
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800273
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274} //namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700275} //namespace ns3