blob: 5ab8cba5f853faacaf015944b9af506bfca13ce5 [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 Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-l3-protocol.hpp"
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 Afanasyev0c395372014-12-20 15:54:02 -080035#include "ns3/ndn-pit.hpp"
36#include "ns3/ndn-interest.hpp"
37#include "ns3/ndn-data.hpp"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070038
Alexander Afanasyev0c395372014-12-20 15:54:02 -080039#include "ns3/ndn-face.hpp"
40#include "ns3/ndn-forwarding-strategy.hpp"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070041
Alexander Afanasyev0c395372014-12-20 15:54:02 -080042#include "ndn-net-device-face.hpp"
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 Afanasyev016a5d82013-07-15 10:41:29 -070052const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054NS_OBJECT_ENSURE_REGISTERED (L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080056TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057L3Protocol::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 static TypeId tid = TypeId ("ns3::ndn::L3Protocol")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070060 .SetGroupName ("ndn")
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070061 .SetParent<Object> ()
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 Afanasyevb989b122013-07-10 17:15:46 -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_forwardingStrategy != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097 }
98 }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070099 if (m_forwardingStrategy == 0)
100 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 m_forwardingStrategy = GetObject<ForwardingStrategy> ();
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700102 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700103
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700104 Object::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105}
106
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800107void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700108L3Protocol::DoDispose (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109{
110 NS_LOG_FUNCTION (this);
111
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700112 // for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
113 // {
114 // *i = 0;
115 // }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700116 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700117 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800118
119 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800120 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800121
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122 Object::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123}
124
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800125uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126L3Protocol::AddFace (const Ptr<Face> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700127{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700128 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700130 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700132 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700133 face->RegisterProtocolHandlers (MakeCallback (&ForwardingStrategy::OnInterest, m_forwardingStrategy),
134 MakeCallback (&ForwardingStrategy::OnData, m_forwardingStrategy));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700136 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137 m_faceCounter++;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700138
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800139 m_forwardingStrategy->AddFace (face); // notify that face is added
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700140 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700141}
142
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700143void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144L3Protocol::RemoveFace (Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700145{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700146 NS_LOG_FUNCTION (this << boost::cref (*face));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700147 // ask face to register in lower-layer stack
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700148 face->UnRegisterProtocolHandlers ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149 Ptr<Pit> pit = GetObject<Pit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150
151 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 std::list< Ptr<pit::Entry> > entriesToRemoves;
153 for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700155 pitEntry->RemoveAllReferencesToFace (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800156
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700157 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
158 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700159 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800160 pitEntry->GetFibEntry ()->m_faces.begin ()->GetFace () == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700161 {
162 entriesToRemoves.push_back (pitEntry);
163 }
164 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700166 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700167 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 }
169
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700170 FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700171 if (face_it == m_faces.end ())
172 {
173 return;
174 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700175 m_faces.erase (face_it);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700176
177 GetObject<Fib> ()->RemoveFromAll (face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800178 m_forwardingStrategy->RemoveFace (face); // notify that face is removed
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700179}
180
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181Ptr<Face>
182L3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700183{
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700184 NS_ASSERT (0 <= index && index < m_faces.size ());
185 return m_faces[index];
186}
187
188Ptr<Face>
189L3Protocol::GetFaceById (uint32_t index) const
190{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191 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 -0700192 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700193 if (face->GetId () == index)
194 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195 }
196 return 0;
197}
198
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199Ptr<Face>
200L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800201{
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 Afanasyev52e9aa92011-11-15 20:23:20 -0800203 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700204 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800205 if (netDeviceFace == 0) continue;
206
207 if (netDeviceFace->GetNetDevice () == netDevice)
208 return face;
209 }
210 return 0;
211}
212
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800213uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214L3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700216 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217}
218
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219} //namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220} //namespace ns3