blob: b0957c52bf4d951e791fc2f9e55017b4921054fa [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"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080037#include "ns3/ndn-interest.h"
38#include "ns3/ndn-content-object.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 Afanasyevff229772012-09-03 13:30:29 -070054uint64_t L3Protocol::s_interestCounter = 0;
55uint64_t L3Protocol::s_dataCounter = 0;
Alexander Afanasyev07827182011-12-13 01:07:32 -080056
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057NS_OBJECT_ENSURE_REGISTERED (L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058
Alexander Afanasyevff229772012-09-03 13:30:29 -070059uint64_t
60L3Protocol::GetInterestCounter ()
61{
62 return s_interestCounter;
63}
64
65uint64_t
66L3Protocol::GetDataCounter ()
67{
68 return s_dataCounter;
69}
70
71
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073L3Protocol::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070075 static TypeId tid = TypeId ("ns3::ndn::L3Protocol")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076 .SetGroupName ("ndn")
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070077 .SetParent<Object> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078 .AddConstructor<L3Protocol> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079 .AddAttribute ("FaceList", "List of faces associated with ndn stack",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080080 ObjectVectorValue (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 MakeObjectVectorAccessor (&L3Protocol::m_faces),
82 MakeObjectVectorChecker<Face> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 ;
84 return tid;
85}
86
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087L3Protocol::L3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070088: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089{
90 NS_LOG_FUNCTION (this);
91}
92
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093L3Protocol::~L3Protocol ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094{
95 NS_LOG_FUNCTION (this);
96}
97
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070098/*
99 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101 */
102void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103L3Protocol::NotifyNewAggregate ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700104{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700105 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106 if (m_node == 0)
107 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700108 m_node = GetObject<Node> ();
109 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700110 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700111 // NS_ASSERT_MSG (m_pit != 0 && m_fib != 0 && m_contentStore != 0 && m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112 // "PIT, FIB, and ContentStore should be aggregated before L3Protocol");
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700113 NS_ASSERT_MSG (m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700115 }
116 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700117 // if (m_pit == 0)
118 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119 // m_pit = GetObject<Pit> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700120 // }
121 // if (m_fib == 0)
122 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123 // m_fib = GetObject<Fib> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700124 // }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700125 if (m_forwardingStrategy == 0)
126 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127 m_forwardingStrategy = GetObject<ForwardingStrategy> ();
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700128 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700129 // if (m_contentStore == 0)
130 // {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700131 // m_contentStore = GetObject<ContentStore> ();
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700132 // }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700133
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134 Object::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135}
136
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138L3Protocol::DoDispose (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139{
140 NS_LOG_FUNCTION (this);
141
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700143 {
144 *i = 0;
145 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700146 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700147 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800148
149 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800150 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800151
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 Object::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700153}
154
155uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700156L3Protocol::AddFace (const Ptr<Face> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700157{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700158 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700160 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700162 // ask face to register in lower-layer stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163 face->RegisterProtocolHandler (MakeCallback (&L3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700165 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 m_faceCounter++;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700167
168 m_forwardingStrategy->AddFace (face); // notify that face is added
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700169 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170}
171
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173L3Protocol::RemoveFace (Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174{
175 // ask face to register in lower-layer stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700176 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ());
177 Ptr<Pit> pit = GetObject<Pit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178
179 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180 std::list< Ptr<pit::Entry> > entriesToRemoves;
181 for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800182 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700183 pitEntry->RemoveAllReferencesToFace (face);
184
185 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
186 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700187 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
188 pitEntry->GetFibEntry ()->m_faces.begin ()->m_face == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700189 {
190 entriesToRemoves.push_back (pitEntry);
191 }
192 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700194 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700195 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800196 }
197
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700198 FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700199 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
200 m_faces.erase (face_it);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700201
202 GetObject<Fib> ()->RemoveFromAll (face);
203 m_forwardingStrategy->RemoveFace (face); // notify that face is removed
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700204}
205
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206Ptr<Face>
207L3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208{
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700209 NS_ASSERT (0 <= index && index < m_faces.size ());
210 return m_faces[index];
211}
212
213Ptr<Face>
214L3Protocol::GetFaceById (uint32_t index) const
215{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700216 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 -0700217 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218 if (face->GetId () == index)
219 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220 }
221 return 0;
222}
223
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700224Ptr<Face>
225L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800226{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700227 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 -0800228 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700229 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800230 if (netDeviceFace == 0) continue;
231
232 if (netDeviceFace->GetNetDevice () == netDevice)
233 return face;
234 }
235 return 0;
236}
237
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700238uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239L3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700240{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700241 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242}
243
Alexander Afanasyev98256102011-08-14 01:00:02 -0700244// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700246L3Protocol::Receive (const Ptr<Face> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247{
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700248 if (!face->IsUp ())
249 return;
250
251 NS_LOG_DEBUG (*p);
252
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700253 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700254
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700255 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700256 try
257 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700258 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700259 switch (type)
260 {
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800261 case HeaderHelper::INTEREST_NDNSIM:
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700262 {
Alexander Afanasyevff229772012-09-03 13:30:29 -0700263 s_interestCounter ++;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264 Ptr<InterestHeader> header = Create<InterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700265
266 // Deserialization. Exception may be thrown
267 packet->RemoveHeader (*header);
268 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800269
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700270 m_forwardingStrategy->OnInterest (face, header, p/*original packet*/);
271 // if (header->GetNack () > 0)
272 // OnNack (face, header, p/*original packet*/);
273 // else
274 // OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700275 break;
276 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800277 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700278 {
Alexander Afanasyevff229772012-09-03 13:30:29 -0700279 s_dataCounter ++;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700280 Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700281
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700282 static ContentObjectTail contentObjectTrailer; //there is no data in this object
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700283
284 // Deserialization. Exception may be thrown
285 packet->RemoveHeader (*header);
286 packet->RemoveTrailer (contentObjectTrailer);
287
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700288 m_forwardingStrategy->OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700289 break;
290 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800291 case HeaderHelper::INTEREST_CCNB:
292 case HeaderHelper::CONTENT_OBJECT_CCNB:
293 NS_FATAL_ERROR ("ccnb support is broken in this implementation");
294 break;
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700295 }
296
297 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700298 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700299 catch (UnknownHeaderException)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700300 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700301 NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen");
302 NS_LOG_ERROR ("Unknown NDN header. Should not happen");
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700303 return;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700304 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700305}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700306
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800307
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700308} //namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700309} //namespace ns3