Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-l3-protocol.hpp" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 21 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 22 | #include "ns3/packet.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 23 | #include "ns3/node.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
| 25 | #include "ns3/callback.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 26 | #include "ns3/uinteger.h" |
| 27 | #include "ns3/trace-source-accessor.h" |
| 28 | #include "ns3/object-vector.h" |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 29 | #include "ns3/pointer.h" |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 30 | #include "ns3/simulator.h" |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 31 | #include "ns3/random-variable.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 32 | |
Spyridon Mastorakis | e4f0d3c | 2014-10-29 13:20:03 -0700 | [diff] [blame] | 33 | #include "ndn-face.hpp" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 35 | #include "ndn-net-device-face.hpp" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 36 | #include "../helper/ndn-stack-helper.hpp" |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 37 | #include "cs/ndn-content-store.hpp" |
Alexander Afanasyev | 52e9aa9 | 2011-11-15 20:23:20 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 39 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 40 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 41 | #include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp" |
| 42 | #include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp" |
| 43 | #include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp" |
| 44 | #include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp" |
| 45 | #include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp" |
| 46 | #include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.hpp" |
| 47 | |
| 48 | #include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp" |
| 49 | #include "ns3/ndnSIM/NFD/core/config-file.hpp" |
| 50 | #include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp" |
| 51 | #include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp" |
| 52 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 54 | |
| 55 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 56 | namespace ndn { |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 58 | const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777; |
Alexander Afanasyev | 016a5d8 | 2013-07-15 10:41:29 -0700 | [diff] [blame] | 59 | const uint16_t L3Protocol::IP_STACK_PORT = 9695; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | NS_OBJECT_ENSURE_REGISTERED(L3Protocol); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 63 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | L3Protocol::GetTypeId(void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 65 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | static TypeId tid = |
| 67 | TypeId("ns3::ndn::L3Protocol") |
| 68 | .SetGroupName("ndn") |
| 69 | .SetParent<Object>() |
| 70 | .AddConstructor<L3Protocol>() |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 71 | |
| 72 | .AddTraceSource("OutInterests", "OutInterests", |
| 73 | MakeTraceSourceAccessor(&L3Protocol::m_outInterests)) |
| 74 | .AddTraceSource("InInterests", "InInterests", |
| 75 | MakeTraceSourceAccessor(&L3Protocol::m_inInterests)) |
| 76 | |
| 77 | //////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData)) |
| 80 | .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData)); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 81 | return tid; |
| 82 | } |
| 83 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 84 | class L3Protocol::Impl { |
| 85 | private: |
| 86 | friend class L3Protocol; |
| 87 | |
| 88 | shared_ptr<nfd::Forwarder> m_forwarder; |
| 89 | |
| 90 | shared_ptr<nfd::InternalFace> m_internalFace; |
| 91 | shared_ptr<nfd::FibManager> m_fibManager; |
| 92 | shared_ptr<nfd::FaceManager> m_faceManager; |
| 93 | shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager; |
| 94 | shared_ptr<nfd::StatusServer> m_statusServer; |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 95 | |
| 96 | Ptr<ContentStore> m_csFromNdnSim; |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | L3Protocol::L3Protocol() |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 100 | : m_impl(new Impl()) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 101 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | L3Protocol::~L3Protocol() |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 106 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 107 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 110 | void |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 111 | L3Protocol::initialize(bool shouldUseNfdCs) |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 112 | { |
| 113 | m_impl->m_forwarder = make_shared<nfd::Forwarder>(); |
| 114 | |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 115 | initializeManagement(shouldUseNfdCs); |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 116 | |
| 117 | m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL); |
| 118 | m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>( |
| 119 | FaceUri("contentstore://")), |
| 120 | nfd::FACEID_CONTENT_STORE); |
| 121 | } |
| 122 | |
| 123 | void |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 124 | L3Protocol::initializeManagement(bool shouldUseNfdCs) |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 125 | { |
| 126 | m_impl->m_internalFace = make_shared<nfd::InternalFace>(); |
| 127 | |
| 128 | m_impl->m_fibManager = |
| 129 | make_shared<nfd::FibManager>(ref(m_impl->m_forwarder->getFib()), |
| 130 | bind(&nfd::Forwarder::getFace, m_impl->m_forwarder.get(), _1), |
| 131 | m_impl->m_internalFace, std::ref(StackHelper::getKeyChain())); |
| 132 | |
| 133 | m_impl->m_faceManager = |
| 134 | make_shared<nfd::FaceManager>(ref(m_impl->m_forwarder->getFaceTable()), m_impl->m_internalFace, |
| 135 | std::ref(StackHelper::getKeyChain())); |
| 136 | |
| 137 | m_impl->m_strategyChoiceManager = |
| 138 | make_shared<nfd::StrategyChoiceManager>(ref(m_impl->m_forwarder->getStrategyChoice()), |
| 139 | m_impl->m_internalFace, |
| 140 | std::ref(StackHelper::getKeyChain())); |
| 141 | |
| 142 | m_impl->m_statusServer = |
| 143 | make_shared<nfd::StatusServer>(m_impl->m_internalFace, ref(*m_impl->m_forwarder), |
| 144 | std::ref(StackHelper::getKeyChain())); |
| 145 | |
| 146 | nfd::TablesConfigSection tablesConfig(m_impl->m_forwarder->getCs(), |
| 147 | m_impl->m_forwarder->getPit(), |
| 148 | m_impl->m_forwarder->getFib(), |
| 149 | m_impl->m_forwarder->getStrategyChoice(), |
| 150 | m_impl->m_forwarder->getMeasurements()); |
| 151 | |
| 152 | m_impl->m_forwarder->getFaceTable().addReserved(m_impl->m_internalFace, |
| 153 | nfd::FACEID_INTERNAL_FACE); |
| 154 | |
| 155 | // add FIB entry for NFD Management Protocol |
| 156 | shared_ptr<::nfd::fib::Entry> entry = |
| 157 | m_impl->m_forwarder->getFib().insert("/localhost/nfd").first; |
| 158 | entry->addNextHop(m_impl->m_internalFace, 0); |
| 159 | } |
| 160 | |
| 161 | shared_ptr<nfd::Forwarder> |
| 162 | L3Protocol::getForwarder() |
| 163 | { |
| 164 | return m_impl->m_forwarder; |
| 165 | } |
| 166 | |
| 167 | shared_ptr<nfd::FibManager> |
| 168 | L3Protocol::getFibManager() |
| 169 | { |
| 170 | return m_impl->m_fibManager; |
| 171 | } |
| 172 | |
| 173 | shared_ptr<nfd::StrategyChoiceManager> |
| 174 | L3Protocol::getStrategyChoiceManager() |
| 175 | { |
| 176 | return m_impl->m_strategyChoiceManager; |
| 177 | } |
| 178 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 179 | /* |
| 180 | * This method is called by AddAgregate and completes the aggregation |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 181 | * by setting the node in the ndn stack |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 182 | */ |
| 183 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 184 | L3Protocol::NotifyNewAggregate() |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 185 | { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 186 | if (m_node == nullptr) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | m_node = GetObject<Node>(); |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 188 | if (m_node != nullptr) { |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 189 | NS_ASSERT(m_impl->m_forwarder != nullptr); |
| 190 | m_impl->m_csFromNdnSim = GetObject<ContentStore>(); |
| 191 | if (m_impl->m_csFromNdnSim != nullptr) { |
| 192 | m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim); |
| 193 | } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 194 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 195 | } |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 196 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 197 | Object::NotifyNewAggregate(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 200 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 201 | L3Protocol::DoDispose(void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 202 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 203 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 204 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 205 | m_node = 0; |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 207 | Object::DoDispose(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 210 | nfd::FaceId |
| 211 | L3Protocol::addFace(shared_ptr<Face> face) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 212 | { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 213 | NS_LOG_FUNCTION(this << face.get()); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 214 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 215 | m_impl->m_forwarder->addFace(face); |
| 216 | |
| 217 | // Connect Signals to TraceSource |
| 218 | face->onReceiveInterest += |
| 219 | [this, face](const Interest& interest) { this->m_inInterests(interest, *face); }; |
| 220 | |
| 221 | face->onSendInterest += |
| 222 | [this, face](const Interest& interest) { this->m_outInterests(interest, *face); }; |
| 223 | |
| 224 | face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); }; |
| 225 | |
| 226 | face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); }; |
| 227 | |
| 228 | return face->getId(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 231 | // void |
| 232 | // L3Protocol::removeFace(shared_ptr<Face> face) |
| 233 | // { |
| 234 | // NS_LOG_FUNCTION(this << std::cref(*face)); |
| 235 | // } |
| 236 | |
| 237 | shared_ptr<Face> |
| 238 | L3Protocol::getFaceById(nfd::FaceId id) const |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 239 | { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 240 | return m_impl->m_forwarder->getFaceTable().get(id); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Spyridon Mastorakis | e4f0d3c | 2014-10-29 13:20:03 -0700 | [diff] [blame] | 243 | shared_ptr<Face> |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 244 | L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 245 | { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 246 | for (const auto& i : m_impl->m_forwarder->getFaceTable()) { |
| 247 | shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i); |
| 248 | if (netDeviceFace == nullptr) |
| 249 | continue; |
Alexander Afanasyev | aebf5cf | 2012-08-28 17:32:17 -0700 | [diff] [blame] | 250 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 251 | if (netDeviceFace->GetNetDevice() == netDevice) |
| 252 | return i; |
| 253 | } |
Spyridon Mastorakis | e4f0d3c | 2014-10-29 13:20:03 -0700 | [diff] [blame] | 254 | return nullptr; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 257 | } // namespace ndn |
| 258 | } // namespace ns3 |