blob: d7505e7ff05b74975323a873b9128a0a0185d5fc [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 Afanasyevbe55cf62014-12-20 17:51:09 -080046NS_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 Afanasyevbe55cf62014-12-20 17:51:09 -080054NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080056TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 static TypeId tid =
60 TypeId("ns3::ndn::L3Protocol")
61 .SetGroupName("ndn")
62 .SetParent<Object>()
63 .AddConstructor<L3Protocol>()
64 .AddAttribute("FaceList", "List of faces associated with ndn stack", ObjectVectorValue(),
65 MakeObjectVectorAccessor(&L3Protocol::m_faces),
66 MakeObjectVectorChecker<Face>());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 return tid;
68}
69
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070L3Protocol::L3Protocol()
71 : m_faceCounter(0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074}
75
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070079}
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 Afanasyevbe55cf62014-12-20 17:51:09 -080086L3Protocol::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 Afanasyevbe55cf62014-12-20 17:51:09 -080089 if (m_node == 0) {
90 m_node = GetObject<Node>();
91 if (m_node != 0) {
92 NS_ASSERT_MSG(m_forwardingStrategy != 0,
93 "Forwarding strategy should be aggregated before L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 }
96 if (m_forwardingStrategy == 0) {
97 m_forwardingStrategy = GetObject<ForwardingStrategy>();
98 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101}
102
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800103void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700107
Alexander Afanasyeva4e74282013-07-11 15:23:20 -0700108 // for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
109 // {
110 // *i = 0;
111 // }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 m_faces.clear();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700113 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800114
115 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800116 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800117
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700119}
120
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800121uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122L3Protocol::AddFace(const Ptr<Face>& face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 NS_LOG_FUNCTION(this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 face->SetId(
127 m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700129 // ask face to register in lower-layer stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 face->RegisterProtocolHandlers(MakeCallback(&ForwardingStrategy::OnInterest,
131 m_forwardingStrategy),
132 MakeCallback(&ForwardingStrategy::OnData, m_forwardingStrategy));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 m_faces.push_back(face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135 m_faceCounter++;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 m_forwardingStrategy->AddFace(face); // notify that face is added
138 return face->GetId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139}
140
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700141void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142L3Protocol::RemoveFace(Ptr<Face> face)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700143{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 NS_LOG_FUNCTION(this << boost::cref(*face));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700145 // ask face to register in lower-layer stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 face->UnRegisterProtocolHandlers();
147 Ptr<Pit> pit = GetObject<Pit>();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148
149 // just to be on a safe side. Do the process in two steps
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 std::list<Ptr<pit::Entry>> entriesToRemoves;
151 for (Ptr<pit::Entry> pitEntry = pit->Begin(); pitEntry != 0; pitEntry = pit->Next(pitEntry)) {
152 pitEntry->RemoveAllReferencesToFace(face);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800153
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
155 // Thus, we have to remove the whole PIT entry
156 if (pitEntry->GetFibEntry()->m_faces.size() == 1
157 && pitEntry->GetFibEntry()->m_faces.begin()->GetFace() == face) {
158 entriesToRemoves.push_back(pitEntry);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700159 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 }
161 BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves) {
162 pit->MarkErased(removedEntry);
163 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 FaceList::iterator face_it = find(m_faces.begin(), m_faces.end(), face);
166 if (face_it == m_faces.end()) {
167 return;
168 }
169 m_faces.erase(face_it);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700170
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171 GetObject<Fib>()->RemoveFromAll(face);
172 m_forwardingStrategy->RemoveFace(face); // notify that face is removed
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700173}
174
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175Ptr<Face>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176L3Protocol::GetFace(uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700177{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800178 NS_ASSERT(0 <= index && index < m_faces.size());
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700179 return m_faces[index];
180}
181
182Ptr<Face>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183L3Protocol::GetFaceById(uint32_t index) const
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700184{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185 BOOST_FOREACH (const Ptr<Face>& face, m_faces) // this function is not supposed to be called
186 // often, so linear search is fine
187 {
188 if (face->GetId() == index)
189 return face;
190 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700191 return 0;
192}
193
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700194Ptr<Face>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195L3Protocol::GetFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800196{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 BOOST_FOREACH (const Ptr<Face>& face, m_faces) // this function is not supposed to be called
198 // often, so linear search is fine
199 {
200 Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace>(face);
201 if (netDeviceFace == 0)
202 continue;
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800203
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800204 if (netDeviceFace->GetNetDevice() == netDevice)
205 return face;
206 }
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800207 return 0;
208}
209
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800210uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211L3Protocol::GetNFaces(void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 return m_faces.size();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214}
215
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800216} // namespace ndn
217} // namespace ns3