Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 2 | /* |
| 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 Moiseenko | 172763c | 2011-10-28 13:21:53 -0700 | [diff] [blame] | 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 20 | */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 21 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ndn-l3-protocol.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 24 | #include "ns3/packet.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 25 | #include "ns3/node.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 26 | #include "ns3/log.h" |
| 27 | #include "ns3/callback.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 28 | #include "ns3/uinteger.h" |
| 29 | #include "ns3/trace-source-accessor.h" |
| 30 | #include "ns3/object-vector.h" |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 31 | #include "ns3/pointer.h" |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 32 | #include "ns3/simulator.h" |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 33 | #include "ns3/random-variable.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 35 | #include "ns3/ndn-header-helper.h" |
| 36 | #include "ns3/ndn-pit.h" |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 37 | #include "ns3/ndn-interest.h" |
| 38 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 40 | #include "ns3/ndn-face.h" |
| 41 | #include "ns3/ndn-forwarding-strategy.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 43 | #include "ndn-net-device-face.h" |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 45 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 47 | NS_LOG_COMPONENT_DEFINE ("ndn.L3Protocol"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 48 | |
| 49 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 50 | namespace ndn { |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 52 | const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | ff22977 | 2012-09-03 13:30:29 -0700 | [diff] [blame] | 54 | uint64_t L3Protocol::s_interestCounter = 0; |
| 55 | uint64_t L3Protocol::s_dataCounter = 0; |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 57 | NS_OBJECT_ENSURE_REGISTERED (L3Protocol); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | ff22977 | 2012-09-03 13:30:29 -0700 | [diff] [blame] | 59 | uint64_t |
| 60 | L3Protocol::GetInterestCounter () |
| 61 | { |
| 62 | return s_interestCounter; |
| 63 | } |
| 64 | |
| 65 | uint64_t |
| 66 | L3Protocol::GetDataCounter () |
| 67 | { |
| 68 | return s_dataCounter; |
| 69 | } |
| 70 | |
| 71 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 72 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 73 | L3Protocol::GetTypeId (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 75 | static TypeId tid = TypeId ("ns3::ndn::L3Protocol") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 76 | .SetGroupName ("ndn") |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 77 | .SetParent<Object> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 78 | .AddConstructor<L3Protocol> () |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 79 | .AddAttribute ("FaceList", "List of faces associated with ndn stack", |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 80 | ObjectVectorValue (), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 81 | MakeObjectVectorAccessor (&L3Protocol::m_faces), |
| 82 | MakeObjectVectorChecker<Face> ()) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 83 | ; |
| 84 | return tid; |
| 85 | } |
| 86 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 87 | L3Protocol::L3Protocol() |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 88 | : m_faceCounter (0) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 89 | { |
| 90 | NS_LOG_FUNCTION (this); |
| 91 | } |
| 92 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 93 | L3Protocol::~L3Protocol () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 94 | { |
| 95 | NS_LOG_FUNCTION (this); |
| 96 | } |
| 97 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 98 | /* |
| 99 | * This method is called by AddAgregate and completes the aggregation |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 100 | * by setting the node in the ndn stack |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 101 | */ |
| 102 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 103 | L3Protocol::NotifyNewAggregate () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 104 | { |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 105 | // not really efficient, but this will work only once |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 106 | if (m_node == 0) |
| 107 | { |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 108 | m_node = GetObject<Node> (); |
| 109 | if (m_node != 0) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 110 | { |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 111 | // NS_ASSERT_MSG (m_pit != 0 && m_fib != 0 && m_contentStore != 0 && m_forwardingStrategy != 0, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 112 | // "PIT, FIB, and ContentStore should be aggregated before L3Protocol"); |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 113 | NS_ASSERT_MSG (m_forwardingStrategy != 0, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 114 | "Forwarding strategy should be aggregated before L3Protocol"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 117 | // if (m_pit == 0) |
| 118 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 119 | // m_pit = GetObject<Pit> (); |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 120 | // } |
| 121 | // if (m_fib == 0) |
| 122 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 123 | // m_fib = GetObject<Fib> (); |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 124 | // } |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 125 | if (m_forwardingStrategy == 0) |
| 126 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 127 | m_forwardingStrategy = GetObject<ForwardingStrategy> (); |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 128 | } |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 129 | // if (m_contentStore == 0) |
| 130 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 131 | // m_contentStore = GetObject<ContentStore> (); |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 132 | // } |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 133 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 134 | Object::NotifyNewAggregate (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 137 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 138 | L3Protocol::DoDispose (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 139 | { |
| 140 | NS_LOG_FUNCTION (this); |
| 141 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 142 | for (FaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 143 | { |
| 144 | *i = 0; |
| 145 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 146 | m_faces.clear (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 147 | m_node = 0; |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 148 | |
| 149 | // Force delete on objects |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 150 | m_forwardingStrategy = 0; // there is a reference to PIT stored in here |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 152 | Object::DoDispose (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 155 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 156 | L3Protocol::AddFace (const Ptr<Face> &face) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 157 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 158 | NS_LOG_FUNCTION (this << &face); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 160 | face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 162 | // ask face to register in lower-layer stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 163 | face->RegisterProtocolHandler (MakeCallback (&L3Protocol::Receive, this)); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 164 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 165 | m_faces.push_back (face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 166 | m_faceCounter++; |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 168 | m_forwardingStrategy->AddFace (face); // notify that face is added |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 169 | return face->GetId (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 172 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 173 | L3Protocol::RemoveFace (Ptr<Face> face) |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 174 | { |
| 175 | // ask face to register in lower-layer stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 176 | face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ()); |
| 177 | Ptr<Pit> pit = GetObject<Pit> (); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 178 | |
| 179 | // just to be on a safe side. Do the process in two steps |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 180 | std::list< Ptr<pit::Entry> > entriesToRemoves; |
| 181 | for (Ptr<pit::Entry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry)) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 182 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 183 | pitEntry->RemoveAllReferencesToFace (face); |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 185 | // If this face is the only for the associated FIB entry, then FIB entry will be removed soon. |
| 186 | // Thus, we have to remove the whole PIT entry |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 187 | if (pitEntry->GetFibEntry ()->m_faces.size () == 1 && |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 188 | pitEntry->GetFibEntry ()->m_faces.begin ()->GetFace () == face) |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 189 | { |
| 190 | entriesToRemoves.push_back (pitEntry); |
| 191 | } |
| 192 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 193 | BOOST_FOREACH (Ptr<pit::Entry> removedEntry, entriesToRemoves) |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 194 | { |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 195 | pit->MarkErased (removedEntry); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 198 | FaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 199 | NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist"); |
| 200 | m_faces.erase (face_it); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 201 | |
| 202 | GetObject<Fib> ()->RemoveFromAll (face); |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 203 | m_forwardingStrategy->RemoveFace (face); // notify that face is removed |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 206 | Ptr<Face> |
| 207 | L3Protocol::GetFace (uint32_t index) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 208 | { |
Alexander Afanasyev | aebf5cf | 2012-08-28 17:32:17 -0700 | [diff] [blame] | 209 | NS_ASSERT (0 <= index && index < m_faces.size ()); |
| 210 | return m_faces[index]; |
| 211 | } |
| 212 | |
| 213 | Ptr<Face> |
| 214 | L3Protocol::GetFaceById (uint32_t index) const |
| 215 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 216 | BOOST_FOREACH (const Ptr<Face> &face, m_faces) // this function is not supposed to be called often, so linear search is fine |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 217 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 218 | if (face->GetId () == index) |
| 219 | return face; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 224 | Ptr<Face> |
| 225 | L3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 226 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 227 | BOOST_FOREACH (const Ptr<Face> &face, m_faces) // this function is not supposed to be called often, so linear search is fine |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 228 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 229 | Ptr<NetDeviceFace> netDeviceFace = DynamicCast<NetDeviceFace> (face); |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 230 | if (netDeviceFace == 0) continue; |
| 231 | |
| 232 | if (netDeviceFace->GetNetDevice () == netDevice) |
| 233 | return face; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 238 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 239 | L3Protocol::GetNFaces (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 240 | { |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 241 | return m_faces.size (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 244 | // Callback from lower layer |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 245 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 246 | L3Protocol::Receive (const Ptr<Face> &face, const Ptr<const Packet> &p) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 247 | { |
Alexander Afanasyev | d459ec3 | 2012-04-30 13:58:20 -0700 | [diff] [blame] | 248 | if (!face->IsUp ()) |
| 249 | return; |
| 250 | |
| 251 | NS_LOG_DEBUG (*p); |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 252 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 253 | NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ()); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 255 | Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 256 | try |
| 257 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 258 | HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p); |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 259 | switch (type) |
| 260 | { |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 261 | case HeaderHelper::INTEREST_NDNSIM: |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 262 | { |
Alexander Afanasyev | ff22977 | 2012-09-03 13:30:29 -0700 | [diff] [blame] | 263 | s_interestCounter ++; |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 264 | Ptr<Interest> header = Create<Interest> (); |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 265 | |
| 266 | // Deserialization. Exception may be thrown |
| 267 | packet->RemoveHeader (*header); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame^] | 268 | |
| 269 | // this assert is legitimately failing with CSMA-style devices, when interest size is less then |
| 270 | // minimally required 46 bytes. At the same time, it is safe to ignore the fact |
| 271 | if (packet->GetSize () != 0) |
| 272 | { |
| 273 | NS_LOG_WARN ("Payload size is not zero. Valid only for CSMA (Ethernet) devices"); |
| 274 | } |
| 275 | // NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero"); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 276 | |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 277 | m_forwardingStrategy->OnInterest (face, header, p/*original packet*/); |
| 278 | // if (header->GetNack () > 0) |
| 279 | // OnNack (face, header, p/*original packet*/); |
| 280 | // else |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 281 | // OnInterest (face, header, p/*original packet*/); |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 282 | break; |
| 283 | } |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 284 | case HeaderHelper::CONTENT_OBJECT_NDNSIM: |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 285 | { |
Alexander Afanasyev | ff22977 | 2012-09-03 13:30:29 -0700 | [diff] [blame] | 286 | s_dataCounter ++; |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 287 | Ptr<ContentObject> header = Create<ContentObject> (); |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 288 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 289 | static ContentObjectTail contentObjectTrailer; //there is no data in this object |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 290 | |
| 291 | // Deserialization. Exception may be thrown |
| 292 | packet->RemoveHeader (*header); |
| 293 | packet->RemoveTrailer (contentObjectTrailer); |
| 294 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 295 | m_forwardingStrategy->OnData (face, header, packet/*payload*/, p/*original packet*/); |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 296 | break; |
| 297 | } |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 298 | case HeaderHelper::INTEREST_CCNB: |
| 299 | case HeaderHelper::CONTENT_OBJECT_CCNB: |
| 300 | NS_FATAL_ERROR ("ccnb support is broken in this implementation"); |
| 301 | break; |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 302 | } |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 303 | |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 304 | // exception will be thrown if packet is not recognized |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 305 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 306 | catch (UnknownHeaderException) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 307 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 308 | NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen"); |
| 309 | NS_LOG_ERROR ("Unknown NDN header. Should not happen"); |
Alexander Afanasyev | d459ec3 | 2012-04-30 13:58:20 -0700 | [diff] [blame] | 310 | return; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 311 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 312 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 313 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 315 | } //namespace ndn |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 316 | } //namespace ns3 |