blob: 2909b38e27007f3921c9696888b61ab2aaf721e0 [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>
19 */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070020
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070021#include "ccnx-l3-protocol.h"
22
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070024#include "ns3/net-device.h"
25#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 Afanasyev08d984e2011-08-13 19:20:22 -070031#include "ns3/boolean.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070033#include "ns3/ccnx-header-helper.h"
34
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070035#include "ccnx-face.h"
36#include "ccnx-route.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070037#include "ccnx-forwarding-strategy.h"
38#include "ccnx-interest-header.h"
39#include "ccnx-content-object-header.h"
40
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070041#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042
43NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
44
45namespace ns3 {
46
Alexander Afanasyev7112f482011-08-17 14:05:57 -070047const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
50
51TypeId
52CcnxL3Protocol::GetTypeId (void)
53{
54 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
55 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070056 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev7112f482011-08-17 14:05:57 -070058 // .AddTraceSource ("Tx", "Send ccnx packet to outgoing interface.",
59 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_txTrace))
60 // .AddTraceSource ("Rx", "Receive ccnx packet from incoming interface.",
61 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_rxTrace))
62 // .AddTraceSource ("Drop", "Drop ccnx packet",
63 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_dropTrace))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064 .AddAttribute ("InterfaceList", "The set of Ccnx interfaces associated to this Ccnx stack.",
65 ObjectVectorValue (),
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070066 MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
67 MakeObjectVectorChecker<CcnxFace> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068
Alexander Afanasyev7112f482011-08-17 14:05:57 -070069 // .AddTraceSource ("SendOutgoing", "A newly-generated packet by this node is about to be queued for transmission",
70 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_sendOutgoingTrace))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070071
72 ;
73 return tid;
74}
75
76CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070077: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078{
79 NS_LOG_FUNCTION (this);
80}
81
82CcnxL3Protocol::~CcnxL3Protocol ()
83{
84 NS_LOG_FUNCTION (this);
85}
86
87void
88CcnxL3Protocol::SetNode (Ptr<Node> node)
89{
90 m_node = node;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070091}
92
93/*
94 * This method is called by AddAgregate and completes the aggregation
95 * by setting the node in the ccnx stack
96 */
97void
98CcnxL3Protocol::NotifyNewAggregate ()
99{
100 if (m_node == 0)
101 {
102 Ptr<Node>node = this->GetObject<Node>();
103 // verify that it's a valid node and that
104 // the node has not been set before
105 if (node != 0)
106 {
107 this->SetNode (node);
108 }
109 }
110 Object::NotifyNewAggregate ();
111}
112
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700113void
114CcnxL3Protocol::DoDispose (void)
115{
116 NS_LOG_FUNCTION (this);
117
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700118 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700119 {
120 *i = 0;
121 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700122 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123 m_node = 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700124 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125 Object::DoDispose ();
126}
127
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700128void
129CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
130{
131 NS_LOG_FUNCTION (this);
132 m_forwardingStrategy = forwardingStrategy;
133 m_forwardingStrategy->SetCcnx (this);
134}
135
136Ptr<CcnxForwardingStrategy>
137CcnxL3Protocol::GetForwardingStrategy (void) const
138{
139 return m_forwardingStrategy;
140}
141
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700143CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700145 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700147 face->SetNode (m_node);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700148 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700149
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700150 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700151 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700152
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700153 m_faces.push_back (face);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700154 m_faceCounter ++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700155 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156}
157
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700158Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700159CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700161 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700163 if (face->GetId () == index)
164 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700165 }
166 return 0;
167}
168
169uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700170CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700171{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700172 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700173}
174
Alexander Afanasyev98256102011-08-14 01:00:02 -0700175// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700177CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700178{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700179 if (face->IsUp ())
180 {
181 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700182 m_dropTrace (p, DROP_INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700183 return;
184 }
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700185 NS_LOG_LOGIC ("Packet from face " << &face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700186
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700187 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700188 try
189 {
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700190 CcnxHeaderHelper::Type type = CcnxHeaderHelper::CreateCorrectCcnxHeader (p);
191 switch (type)
192 {
193 case CcnxHeaderHelper::INTEREST:
194 {
195 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
196 OnInterest (face, header, packet);
197 break;
198 }
199 case CcnxHeaderHelper::CONTENT_OBJECT:
200 {
201 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
202 OnData (face, header, packet);
203 break;
204 }
205 }
206
207 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700208 }
209 catch (CcnxUnknownHeaderException)
210 {
211 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
212 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700213}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700215// Processing Interests
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700216void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
217 Ptr<CcnxInterestHeader> &header,
218 Ptr<Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700219{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700220 NS_LOG_LOGIC ("Receiving interest from " << &incomingFace);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700221 m_receivedInterestsTrace (packet, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
222
223 // dangerous place. Trying to deserialize header
224 packet->RemoveHeader (*header);
225
226 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700227
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700228 /// \todo Processing of Interest packets
229
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700230 // NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
231 // if (!m_forwardingStrategy->RouteInput (packet, incomingFace,
232 // MakeCallback (&CcnxL3Protocol::Send, this),
233 // MakeCallback (&CcnxL3Protocol::RouteInputError, this)
234 // ))
235 // {
236 // NS_LOG_WARN ("No route found for forwarding packet. Drop.");
237 // m_dropTrace (packet, DROP_NO_ROUTE, m_node->GetObject<Ccnx> (), incomingFace);
238 // }
239}
240
241// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700242void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
243 Ptr<CcnxContentObjectHeader> &header,
244 Ptr<Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700245{
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700246 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
247
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700248 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700249 m_receivedDataTrace (packet, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700250
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700251 // dangerous place. Trying to deserialize header
252 packet->RemoveHeader (*header);
253 packet->RemoveTrailer (contentObjectTrailer);
254
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700255 /// \todo Processing of ContentObject packets
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700256}
257
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258
259void
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700260CcnxL3Protocol::Send (const Ptr<CcnxFace> &face, const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700261{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700262 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face); //
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700263
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700264 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
265
266 if (face->IsUp ())
267 {
268 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700269 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700270 face->Send (packet);
271 }
272 else
273 {
274 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
275 m_dropTrace (packet, DROP_INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
276 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700277}
278
279
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700280} //namespace ns3