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> |
| 19 | */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 21 | #include "ccnx-l3-protocol.h" |
| 22 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | #include "ns3/packet.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 24 | #include "ns3/net-device.h" |
| 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 | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 31 | #include "ns3/boolean.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 33 | #include "ns3/ccnx-header-helper.h" |
| 34 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 35 | #include "ccnx-face.h" |
| 36 | #include "ccnx-route.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 37 | #include "ccnx-forwarding-strategy.h" |
| 38 | #include "ccnx-interest-header.h" |
| 39 | #include "ccnx-content-object-header.h" |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 40 | #include "ccnx-content-store.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 42 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 43 | |
| 44 | NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol"); |
| 45 | |
| 46 | namespace ns3 { |
| 47 | |
Alexander Afanasyev | 7112f48 | 2011-08-17 14:05:57 -0700 | [diff] [blame] | 48 | const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 49 | |
| 50 | NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol); |
| 51 | |
| 52 | TypeId |
| 53 | CcnxL3Protocol::GetTypeId (void) |
| 54 | { |
| 55 | static TypeId tid = TypeId ("ns3::CcnxL3Protocol") |
| 56 | .SetParent<Ccnx> () |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 57 | .SetGroupName ("Ccnx") |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 58 | .AddConstructor<CcnxL3Protocol> () |
Alexander Afanasyev | 7112f48 | 2011-08-17 14:05:57 -0700 | [diff] [blame] | 59 | // .AddTraceSource ("Tx", "Send ccnx packet to outgoing interface.", |
| 60 | // MakeTraceSourceAccessor (&CcnxL3Protocol::m_txTrace)) |
| 61 | // .AddTraceSource ("Rx", "Receive ccnx packet from incoming interface.", |
| 62 | // MakeTraceSourceAccessor (&CcnxL3Protocol::m_rxTrace)) |
| 63 | // .AddTraceSource ("Drop", "Drop ccnx packet", |
| 64 | // MakeTraceSourceAccessor (&CcnxL3Protocol::m_dropTrace)) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 65 | .AddAttribute ("InterfaceList", "The set of Ccnx interfaces associated to this Ccnx stack.", |
| 66 | ObjectVectorValue (), |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 67 | MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces), |
| 68 | MakeObjectVectorChecker<CcnxFace> ()) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 7112f48 | 2011-08-17 14:05:57 -0700 | [diff] [blame] | 70 | // .AddTraceSource ("SendOutgoing", "A newly-generated packet by this node is about to be queued for transmission", |
| 71 | // MakeTraceSourceAccessor (&CcnxL3Protocol::m_sendOutgoingTrace)) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 72 | |
| 73 | ; |
| 74 | return tid; |
| 75 | } |
| 76 | |
| 77 | CcnxL3Protocol::CcnxL3Protocol() |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 78 | : m_faceCounter (0) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 79 | { |
| 80 | NS_LOG_FUNCTION (this); |
| 81 | } |
| 82 | |
| 83 | CcnxL3Protocol::~CcnxL3Protocol () |
| 84 | { |
| 85 | NS_LOG_FUNCTION (this); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | CcnxL3Protocol::SetNode (Ptr<Node> node) |
| 90 | { |
| 91 | m_node = node; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * This method is called by AddAgregate and completes the aggregation |
| 96 | * by setting the node in the ccnx stack |
| 97 | */ |
| 98 | void |
| 99 | CcnxL3Protocol::NotifyNewAggregate () |
| 100 | { |
| 101 | if (m_node == 0) |
| 102 | { |
| 103 | Ptr<Node>node = this->GetObject<Node>(); |
| 104 | // verify that it's a valid node and that |
| 105 | // the node has not been set before |
| 106 | if (node != 0) |
| 107 | { |
| 108 | this->SetNode (node); |
| 109 | } |
| 110 | } |
| 111 | Object::NotifyNewAggregate (); |
| 112 | } |
| 113 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 114 | void |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 115 | CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 116 | { |
| 117 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 118 | m_forwardingStrategy = forwardingStrategy; |
| 119 | m_forwardingStrategy->SetCcnx (this); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 122 | Ptr<CcnxForwardingStrategy> |
| 123 | CcnxL3Protocol::GetForwardingStrategy (void) const |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 124 | { |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 125 | return m_forwardingStrategy; |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 126 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 127 | |
| 128 | void |
| 129 | CcnxL3Protocol::DoDispose (void) |
| 130 | { |
| 131 | NS_LOG_FUNCTION (this); |
| 132 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 133 | for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 134 | { |
| 135 | *i = 0; |
| 136 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 137 | m_faces.clear (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 138 | m_node = 0; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 139 | // m_forwardingStrategy = 0; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 140 | Object::DoDispose (); |
| 141 | } |
| 142 | |
| 143 | uint32_t |
Alexander Afanasyev | 7112f48 | 2011-08-17 14:05:57 -0700 | [diff] [blame] | 144 | CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 145 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 146 | NS_LOG_FUNCTION (this << &face); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 148 | face->SetNode (m_node); |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 149 | 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] | 150 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 151 | face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this)); |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 152 | // if (face->GetDevice() != 0) |
| 153 | // { |
| 154 | // m_node->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::ReceiveFromLower, this), |
| 155 | // CcnxL3Protocol::ETHERNET_FRAME_TYPE, face->GetDevice(), true/*promiscuous mode*/); |
| 156 | // } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 157 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 158 | m_faces.push_back (face); |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 159 | m_faceCounter ++; |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 160 | return face->GetId (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 163 | Ptr<CcnxFace> |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 164 | CcnxL3Protocol::GetFace (uint32_t index) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 165 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 166 | BOOST_FOREACH (const Ptr<CcnxFace> &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] | 167 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 168 | if (face->GetId () == index) |
| 169 | return face; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | uint32_t |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 175 | CcnxL3Protocol::GetNFaces (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 176 | { |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 177 | return m_faces.size (); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 180 | // Callback from lower layer |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 181 | void |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 182 | CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 183 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 184 | if (face->IsUp ()) |
| 185 | { |
| 186 | NS_LOG_LOGIC ("Dropping received packet -- interface is down"); |
| 187 | // m_dropTrace (packet, DROP_INTERFACE_DOWN, m_node->GetObject<Ccnx> (), incomingFace); |
| 188 | return; |
| 189 | } |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 190 | 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] | 191 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 192 | 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] | 193 | try |
| 194 | { |
| 195 | Ptr<Header> header = CcnxHeaderHelper::CreateCorrectCcnxHeader (p); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 196 | ReceiveAndProcess (face, header, packet); // header should serve as overloaded method selector... not sure whether it works with this "smart" pointers... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 197 | } |
| 198 | catch (CcnxUnknownHeaderException) |
| 199 | { |
| 200 | NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen"); |
| 201 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 202 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 204 | // Processing Interests |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 205 | void CcnxL3Protocol::ReceiveAndProcess (const Ptr<CcnxFace> &incomingFace, |
| 206 | const Ptr<CcnxInterestHeader> &header, |
| 207 | const Ptr<Packet> &packet) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 209 | NS_LOG_LOGIC ("Receiving interest from " << &incomingFace); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 210 | // m_rxTrace (packet, m_node->GetObject<Ccnx> (), incomingFace); |
| 211 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 212 | /// \todo Processing of Interest packets |
| 213 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 214 | // NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets"); |
| 215 | // if (!m_forwardingStrategy->RouteInput (packet, incomingFace, |
| 216 | // MakeCallback (&CcnxL3Protocol::Send, this), |
| 217 | // MakeCallback (&CcnxL3Protocol::RouteInputError, this) |
| 218 | // )) |
| 219 | // { |
| 220 | // NS_LOG_WARN ("No route found for forwarding packet. Drop."); |
| 221 | // m_dropTrace (packet, DROP_NO_ROUTE, m_node->GetObject<Ccnx> (), incomingFace); |
| 222 | // } |
| 223 | } |
| 224 | |
| 225 | // Processing ContentObjects |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 226 | void CcnxL3Protocol::ReceiveAndProcess (const Ptr<CcnxFace> &incomingFace, |
| 227 | const Ptr<CcnxContentObjectHeader> &header, |
| 228 | const Ptr<Packet> &packet) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 229 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 230 | NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 231 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 232 | /// \todo Processing of ContentObject packets |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // fake method |
| 236 | void |
| 237 | CcnxL3Protocol::ReceiveAndProcess (Ptr<CcnxFace> face, Ptr<Header> header, Ptr<Packet> p) |
| 238 | { |
| 239 | NS_ASSERT_MSG (false, "This function should never be called"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | void |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 244 | CcnxL3Protocol::Send (const Ptr<CcnxFace> &face, const Ptr<Packet> &packet) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 245 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 246 | NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face); // |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 247 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 248 | NS_ASSERT_MSG (face != 0, "Face should never be NULL"); |
| 249 | |
| 250 | if (face->IsUp ()) |
| 251 | { |
| 252 | NS_LOG_LOGIC ("Sending via face " << &face); // |
| 253 | m_txTrace (packet, m_node->GetObject<Ccnx> (), face); |
| 254 | face->Send (packet); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face); |
| 259 | m_dropTrace (packet, DROP_INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face); |
| 260 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 264 | } //namespace ns3 |