blob: 1ba748862f32e2854f9e3eabaafe866c7bc89fcc [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 Afanasyev78cf0c92011-09-01 19:57:14 -0700182 // m_dropTrace (p, 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> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700196
197 // Deserialization. Exception may be thrown
198 packet->RemoveHeader (*header);
199 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
200
201 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700202 break;
203 }
204 case CcnxHeaderHelper::CONTENT_OBJECT:
205 {
206 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700207
208 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
209
210 // Deserialization. Exception may be thrown
211 packet->RemoveHeader (*header);
212 packet->RemoveTrailer (contentObjectTrailer);
213
214 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700215 break;
216 }
217 }
218
219 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700220 }
221 catch (CcnxUnknownHeaderException)
222 {
223 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
224 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700225}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700227// Processing Interests
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700228void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
229 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700230 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700231{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700232 NS_LOG_LOGIC ("Receiving interest from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700233 m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700234
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700235
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700236 /// \todo Processing of Interest packets
237
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700238 // NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
239 // if (!m_forwardingStrategy->RouteInput (packet, incomingFace,
240 // MakeCallback (&CcnxL3Protocol::Send, this),
241 // MakeCallback (&CcnxL3Protocol::RouteInputError, this)
242 // ))
243 // {
244 // NS_LOG_WARN ("No route found for forwarding packet. Drop.");
245 // m_dropTrace (packet, DROP_NO_ROUTE, m_node->GetObject<Ccnx> (), incomingFace);
246 // }
247}
248
249// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700250void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
251 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700252 Ptr<Packet> &payload,
253 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700254{
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700255
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700256 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700257 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700258
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700259 // 1. Lookup PIT entry
260 try
261 {
262 const CcnxPitEntry &pitEntry = m_pit.Lookup (*header);
263
264 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700265
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700266 // Update metric status for the incoming interface in the corresponding FIB entry
267 m_pit.modify (m_pit.iterator_to (pitEntry),
268 CcnxPitEntry::UpdateFibStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
269
270 // Add or update entry in the content store
271 m_contentStore.Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700272
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700273 CcnxPitEntryOutgoingFaceContainer::type::iterator
274 out = pitEntry.m_outgoing.find (incomingFace);
275
276 // If we have sent interest for this data via this face, then update stats.
277 if (out != pitEntry.m_outgoing.end ())
278 {
279 m_pit.modify (m_pit.iterator_to (pitEntry), CcnxPitEntry::EstimateRttAndRemoveFace(out));
280 // face will be removed in the above call
281 }
282 else
283 {
284 NS_LOG_WARN ("Node "<< m_node->GetId() <<
285 ". PIT entry for "<< header->GetName ()<<" is valid, "
286 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
287 }
288
289 //satisfy all pending incoming Interests
290 BOOST_FOREACH (const CcnxPitEntryIncomingFace &interest, pitEntry.m_incoming)
291 {
292 if (interest.m_face == incomingFace) continue;
293
294 // may not work either because of 'const' thing
295 interest.m_face->Send (packet->Copy ()); // unfortunately, we have to copy packet...
296 m_transmittedDataTrace (header, payload, FORWARDED, m_node->GetObject<Ccnx> (), interest.m_face);
297 }
298
299 m_pit.modify (m_pit.iterator_to (pitEntry), CcnxPitEntry::ClearIncoming()); // satisfy all incoming interests
300
301 if( pitEntry.m_outgoing.size()==0 ) // remove PIT when all outgoing interests are "satisfied"
302 {
303 m_pit.erase (m_pit.iterator_to (pitEntry));
304 }
305
306 }
307 catch (CcnxPitEntryNotFound)
308 {
309 // 2. Drop data packet if PIT entry is not found
310 // (unsolicited data packets should not "poison" content store)
311
312 //drop dulicated or not requested data packet
313 m_droppeddDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
314 return; // do not process unsoliced data packets
315 }
316}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700317
318void
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700319CcnxL3Protocol::Send (const Ptr<CcnxFace> &face, const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700320{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700321 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face); //
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700322
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700323 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
324
325 if (face->IsUp ())
326 {
327 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700328 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700329 face->Send (packet);
330 }
331 else
332 {
333 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700334 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700335 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700336}
337
338
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700339} //namespace ns3