blob: 59993acf1ca249a011a110fc7caa638bf4c140fc [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
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080071TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072L3Protocol::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 static TypeId tid = TypeId ("ns3::ndn::L3Protocol")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075 .SetGroupName ("ndn")
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070076 .SetParent<Object> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077 .AddConstructor<L3Protocol> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078 .AddAttribute ("FaceList", "List of faces associated with ndn stack",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080079 ObjectVectorValue (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 MakeObjectVectorAccessor (&L3Protocol::m_faces),
81 MakeObjectVectorChecker<Face> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082 ;
83 return tid;
84}
85
Alexander Afanasyevb989b122013-07-10 17:15:46 -070086L3Protocol::L3Protocol ()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070087: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088{
89 NS_LOG_FUNCTION (this);
90}
91
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092L3Protocol::~L3Protocol ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070093{
94 NS_LOG_FUNCTION (this);
95}
96
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097/*
98 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070099 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700100 */
101void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102L3Protocol::NotifyNewAggregate ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700103{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700104 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105 if (m_node == 0)
106 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700107 m_node = GetObject<Node> ();
108 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700110 NS_ASSERT_MSG (m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700112 }
113 }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700114 if (m_forwardingStrategy == 0)
115 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116 m_forwardingStrategy = GetObject<ForwardingStrategy> ();
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700117 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700118
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119 Object::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700120}
121
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800122void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123L3Protocol::DoDispose (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700124{
125 NS_LOG_FUNCTION (this);
126
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127 for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128 {
129 *i = 0;
130 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700131 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700132 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800133
134 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800135 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800136
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 Object::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700138}
139
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800140uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141L3Protocol::AddFace (const Ptr<Face> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700143 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700145 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700147 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700148 face->RegisterProtocolHandlers (MakeCallback (&ForwardingStrategy::OnInterest, m_forwardingStrategy),
149 MakeCallback (&ForwardingStrategy::OnData, m_forwardingStrategy));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700151 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152 m_faceCounter++;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700153
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800154 m_forwardingStrategy->AddFace (face); // notify that face is added
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700155 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156}
157
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700158void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159L3Protocol::RemoveFace (Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700160{
161 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700162 face->UnRegisterProtocolHandlers ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163 Ptr<Pit> pit = GetObject<Pit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164
165 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166 std::list< Ptr<pit::Entry> > entriesToRemoves;
167 for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700169 pitEntry->RemoveAllReferencesToFace (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800170
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700171 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
172 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700173 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800174 pitEntry->GetFibEntry ()->m_faces.begin ()->GetFace () == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700175 {
176 entriesToRemoves.push_back (pitEntry);
177 }
178 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700179 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700180 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700181 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800182 }
183
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700184 FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700185 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
186 m_faces.erase (face_it);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700187
188 GetObject<Fib> ()->RemoveFromAll (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800189 m_forwardingStrategy->RemoveFace (face); // notify that face is removed
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700190}
191
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192Ptr<Face>
193L3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194{
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700195 NS_ASSERT (0 <= index && index < m_faces.size ());
196 return m_faces[index];
197}
198
199Ptr<Face>
200L3Protocol::GetFaceById (uint32_t index) const
201{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700202 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 -0700203 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700204 if (face->GetId () == index)
205 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206 }
207 return 0;
208}
209
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700210Ptr<Face>
211L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800212{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 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 -0800214 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800216 if (netDeviceFace == 0) continue;
217
218 if (netDeviceFace->GetNetDevice () == netDevice)
219 return face;
220 }
221 return 0;
222}
223
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800224uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700225L3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700227 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228}
229
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230} //namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700231} //namespace ns3