blob: e89c9ea2354245a74f7954d419c1b024ad939404 [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-pit.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080036#include "ns3/ndn-interest.h"
37#include "ns3/ndn-content-object.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070038
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070039#include "ns3/ndn-face.h"
40#include "ns3/ndn-forwarding-strategy.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070041
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042#include "ndn-net-device-face.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080043
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070044#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046NS_LOG_COMPONENT_DEFINE ("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047
48namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053NS_OBJECT_ENSURE_REGISTERED (L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080055TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056L3Protocol::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 static TypeId tid = TypeId ("ns3::ndn::L3Protocol")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059 .SetGroupName ("ndn")
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070060 .SetParent<Object> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 .AddConstructor<L3Protocol> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 .AddAttribute ("FaceList", "List of faces associated with ndn stack",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080063 ObjectVectorValue (),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064 MakeObjectVectorAccessor (&L3Protocol::m_faces),
65 MakeObjectVectorChecker<Face> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066 ;
67 return tid;
68}
69
Alexander Afanasyevb989b122013-07-10 17:15:46 -070070L3Protocol::L3Protocol ()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070071: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072{
73 NS_LOG_FUNCTION (this);
74}
75
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076L3Protocol::~L3Protocol ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077{
78 NS_LOG_FUNCTION (this);
79}
80
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081/*
82 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070083 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084 */
85void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086L3Protocol::NotifyNewAggregate ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070087{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070088 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089 if (m_node == 0)
90 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070091 m_node = GetObject<Node> ();
92 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070093 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070094 NS_ASSERT_MSG (m_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070096 }
97 }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070098 if (m_forwardingStrategy == 0)
99 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100 m_forwardingStrategy = GetObject<ForwardingStrategy> ();
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700101 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700102
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103 Object::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700104}
105
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800106void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107L3Protocol::DoDispose (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108{
109 NS_LOG_FUNCTION (this);
110
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700111 // for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
112 // {
113 // *i = 0;
114 // }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700115 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700116 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800117
118 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800119 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800120
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121 Object::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700122}
123
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800124uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125L3Protocol::AddFace (const Ptr<Face> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700127 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700129 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700131 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700132 face->RegisterProtocolHandlers (MakeCallback (&ForwardingStrategy::OnInterest, m_forwardingStrategy),
133 MakeCallback (&ForwardingStrategy::OnData, m_forwardingStrategy));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700134
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700135 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136 m_faceCounter++;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700137
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800138 m_forwardingStrategy->AddFace (face); // notify that face is added
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700139 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140}
141
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700142void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143L3Protocol::RemoveFace (Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700144{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700145 NS_LOG_FUNCTION (this << boost::cref (*face));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700146 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700147 face->UnRegisterProtocolHandlers ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148 Ptr<Pit> pit = GetObject<Pit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149
150 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700151 std::list< Ptr<pit::Entry> > entriesToRemoves;
152 for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700154 pitEntry->RemoveAllReferencesToFace (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800155
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700156 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
157 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700158 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800159 pitEntry->GetFibEntry ()->m_faces.begin ()->GetFace () == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700160 {
161 entriesToRemoves.push_back (pitEntry);
162 }
163 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700165 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700166 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800167 }
168
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700169 FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700170 if (face_it == m_faces.end ())
171 {
172 return;
173 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174 m_faces.erase (face_it);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700175
176 GetObject<Fib> ()->RemoveFromAll (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800177 m_forwardingStrategy->RemoveFace (face); // notify that face is removed
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178}
179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180Ptr<Face>
181L3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182{
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700183 NS_ASSERT (0 <= index && index < m_faces.size ());
184 return m_faces[index];
185}
186
187Ptr<Face>
188L3Protocol::GetFaceById (uint32_t index) const
189{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700190 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 -0700191 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700192 if (face->GetId () == index)
193 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194 }
195 return 0;
196}
197
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700198Ptr<Face>
199L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800200{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201 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 -0800202 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800204 if (netDeviceFace == 0) continue;
205
206 if (netDeviceFace->GetNetDevice () == netDevice)
207 return face;
208 }
209 return 0;
210}
211
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800212uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213L3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700215 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216}
217
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218} //namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219} //namespace ns3