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 | 45b92d4 | 2011-08-14 23:11:38 -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 | 45b92d4 | 2011-08-14 23:11:38 -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 | 45b92d4 | 2011-08-14 23:11:38 -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 | 45b92d4 | 2011-08-14 23:11:38 -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 | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-stack-helper.hpp" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 21 | |
| 22 | #include "ns3/log.h" |
| 23 | #include "ns3/names.h" |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 24 | #include "ns3/string.h" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 25 | #include "ns3/point-to-point-net-device.h" |
Alexander Afanasyev | a9d889b | 2016-09-08 18:34:25 -0700 | [diff] [blame] | 26 | #include "ns3/point-to-point-channel.h" |
Chavoosh Ghasemi | 58ef861 | 2018-09-12 15:12:49 -0700 | [diff] [blame] | 27 | #include "ns3/node-list.h" |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 28 | #include "ns3/simulator.h" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 30 | #if HAVE_NS3_VISUALIZER |
| 31 | #include "../../visualizer/model/visual-simulator-impl.h" |
| 32 | #endif // HAVE_NS3_VISUALIZER |
| 33 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 34 | #include "model/ndn-l3-protocol.hpp" |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 35 | #include "model/ndn-net-device-transport.hpp" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 36 | #include "utils/ndn-time.hpp" |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 37 | #include "utils/dummy-keychain.hpp" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 38 | |
| 39 | #include <limits> |
| 40 | #include <map> |
Alexander Afanasyev | b762684 | 2012-01-12 13:43:33 -0800 | [diff] [blame] | 41 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 42 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 43 | #include "ns3/ndnSIM/NFD/daemon/face/generic-link-service.hpp" |
spirosmastorakis | 34eed98 | 2016-11-02 15:20:50 -0700 | [diff] [blame] | 44 | #include "ns3/ndnSIM/NFD/daemon/table/cs-policy-priority-fifo.hpp" |
| 45 | #include "ns3/ndnSIM/NFD/daemon/table/cs-policy-lru.hpp" |
| 46 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 47 | NS_LOG_COMPONENT_DEFINE("ndn.StackHelper"); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 48 | |
| 49 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 50 | namespace ndn { |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | StackHelper::StackHelper() |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 53 | : m_isForwarderStatusManagerDisabled(false) |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 54 | , m_isStrategyChoiceManagerDisabled(false) |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 55 | , m_needSetDefaultRoutes(false) |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 56 | { |
Spyridon Mastorakis | 86edf6f | 2014-11-14 19:27:18 -0800 | [diff] [blame] | 57 | setCustomNdnCxxClocks(); |
| 58 | |
spirosmastorakis | 34eed98 | 2016-11-02 15:20:50 -0700 | [diff] [blame] | 59 | m_csPolicies.insert({"nfd::cs::lru", [] { return make_unique<nfd::cs::LruPolicy>(); }}); |
| 60 | m_csPolicies.insert({"nfd::cs::priority_fifo", [] () { return make_unique<nfd::cs::PriorityFifoPolicy>(); }}); |
| 61 | |
| 62 | m_csPolicyCreationFunc = m_csPolicies["nfd::cs::lru"]; |
| 63 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol"); |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | m_netDeviceCallbacks.push_back( |
| 67 | std::make_pair(PointToPointNetDevice::GetTypeId(), |
| 68 | MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this))); |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 69 | // default callback will be fired if non of others callbacks fit or did the job |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 70 | } |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | StackHelper::~StackHelper() |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 73 | { |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 76 | KeyChain& |
| 77 | StackHelper::getKeyChain() |
| 78 | { |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 79 | static ::ndn::KeyChain keyChain("pib-dummy", "tpm-dummy"); |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 80 | return keyChain; |
| 81 | } |
| 82 | |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 83 | void |
Spyridon Mastorakis | 86edf6f | 2014-11-14 19:27:18 -0800 | [diff] [blame] | 84 | StackHelper::setCustomNdnCxxClocks() |
| 85 | { |
| 86 | ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(), |
| 87 | make_shared<ns3::ndn::time::CustomSystemClock>()); |
| 88 | } |
| 89 | |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 90 | void |
| 91 | StackHelper::SetDefaultRoutes(bool needSet) |
| 92 | { |
| 93 | NS_LOG_FUNCTION(this << needSet); |
| 94 | m_needSetDefaultRoutes = needSet; |
| 95 | } |
| 96 | |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 97 | void |
| 98 | StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1, |
| 99 | const std::string& attr2, const std::string& value2, |
| 100 | const std::string& attr3, const std::string& value3, |
| 101 | const std::string& attr4, const std::string& value4) |
| 102 | { |
| 103 | if (attr1 != "") |
| 104 | m_ndnFactory.Set(attr1, StringValue(value1)); |
| 105 | if (attr2 != "") |
| 106 | m_ndnFactory.Set(attr2, StringValue(value2)); |
| 107 | if (attr3 != "") |
| 108 | m_ndnFactory.Set(attr3, StringValue(value3)); |
| 109 | if (attr4 != "") |
| 110 | m_ndnFactory.Set(attr4, StringValue(value4)); |
| 111 | } |
| 112 | |
| 113 | void |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 114 | StackHelper::setCsSize(size_t maxSize) |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 115 | { |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 116 | m_maxCsSize = maxSize; |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 117 | } |
| 118 | |
spirosmastorakis | 34eed98 | 2016-11-02 15:20:50 -0700 | [diff] [blame] | 119 | void |
| 120 | StackHelper::setPolicy(const std::string& policy) |
| 121 | { |
| 122 | auto found = m_csPolicies.find(policy); |
| 123 | if (found != m_csPolicies.end()) { |
| 124 | m_csPolicyCreationFunc = found->second; |
| 125 | } |
| 126 | else { |
| 127 | NS_FATAL_ERROR("Cache replacement policy " << policy << " not found"); |
| 128 | NS_LOG_DEBUG("Available cache replacement policies: "); |
| 129 | for (auto it = m_csPolicies.begin(); it != m_csPolicies.end(); it++) { |
| 130 | NS_LOG_DEBUG(" " << it->first); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 135 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 136 | StackHelper::Install(const NodeContainer& c) const |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 137 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 138 | for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) { |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 139 | Install(*i); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | } |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 143 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 144 | StackHelper::InstallAll() const |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 145 | { |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 146 | Install(NodeContainer::GetGlobal()); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 149 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 150 | StackHelper::Install(Ptr<Node> node) const |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 151 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 152 | if (node->GetObject<L3Protocol>() != 0) { |
Spyridon Mastorakis | 21226ae | 2015-03-31 18:03:11 -0700 | [diff] [blame] | 153 | NS_FATAL_ERROR("Cannot re-install NDN stack on node " |
| 154 | << node->GetId()); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 155 | return; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | } |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 157 | Simulator::ScheduleWithContext(node->GetId(), Seconds(0), &StackHelper::doInstall, this, node); |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 158 | ProcessWarmupEvents(); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 159 | } |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 161 | void |
| 162 | StackHelper::doInstall(Ptr<Node> node) const |
| 163 | { |
| 164 | // async install to ensure proper context |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 165 | Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>(); |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 167 | if (m_isForwarderStatusManagerDisabled) { |
| 168 | ndn->getConfig().put("ndnSIM.disable_forwarder_status_manager", true); |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (m_isStrategyChoiceManagerDisabled) { |
| 172 | ndn->getConfig().put("ndnSIM.disable_strategy_choice_manager", true); |
| 173 | } |
| 174 | |
Alexander Afanasyev | 25e4d8f | 2019-07-29 13:44:04 -0400 | [diff] [blame] | 175 | ndn->getConfig().put("tables.cs_max_packets", m_maxCsSize); |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 8fbcc64 | 2019-07-27 16:53:37 -0400 | [diff] [blame] | 177 | ndn->setCsReplacementPolicy(m_csPolicyCreationFunc); |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 178 | |
| 179 | // Aggregate L3Protocol on node (must be after setting ndnSIM CS) |
| 180 | node->AggregateObject(ndn); |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 182 | for (uint32_t index = 0; index < node->GetNDevices(); index++) { |
| 183 | Ptr<NetDevice> device = node->GetDevice(index); |
| 184 | // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed, |
| 185 | // Normally, ndnSIM works without IP stack, so no reason to check |
| 186 | // if (DynamicCast<LoopbackNetDevice> (device) != 0) |
| 187 | // continue; // don't create face for a LoopbackNetDevice |
Alexander Afanasyev | 1145314 | 2011-11-25 16:13:33 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 189 | this->createAndRegisterFace(node, ndn, device); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 193 | void |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 194 | StackHelper::AddFaceCreateCallback(TypeId netDeviceType, |
| 195 | StackHelper::FaceCreateCallback callback) |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 196 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 197 | m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback)); |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 200 | void |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 201 | StackHelper::UpdateFaceCreateCallback(TypeId netDeviceType, |
| 202 | FaceCreateCallback callback) |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 203 | { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 204 | for (auto& i : m_netDeviceCallbacks) { |
| 205 | if (i.first == netDeviceType) { |
| 206 | i.second = callback; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 207 | return; |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 208 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 209 | } |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 213 | StackHelper::RemoveFaceCreateCallback(TypeId netDeviceType, |
| 214 | FaceCreateCallback callback) |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 215 | { |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 216 | m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, FaceCreateCallback>& i) { |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 217 | return (i.first == netDeviceType); |
| 218 | }); |
Alexander Afanasyev | 2a269f7 | 2013-06-06 22:59:33 -0700 | [diff] [blame] | 219 | } |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 220 | |
Alexander Afanasyev | a9d889b | 2016-09-08 18:34:25 -0700 | [diff] [blame] | 221 | std::string |
| 222 | constructFaceUri(Ptr<NetDevice> netDevice) |
| 223 | { |
| 224 | std::string uri = "netdev://"; |
| 225 | Address address = netDevice->GetAddress(); |
| 226 | if (Mac48Address::IsMatchingType(address)) { |
| 227 | uri += "[" + boost::lexical_cast<std::string>(Mac48Address::ConvertFrom(address)) + "]"; |
| 228 | } |
| 229 | |
| 230 | return uri; |
| 231 | } |
| 232 | |
| 233 | |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 234 | shared_ptr<Face> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 235 | StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn, |
| 236 | Ptr<NetDevice> netDevice) const |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 237 | { |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 238 | NS_LOG_DEBUG("Creating default Face on node " << node->GetId()); |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 239 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 240 | // Create an ndnSIM-specific transport instance |
| 241 | ::nfd::face::GenericLinkService::Options opts; |
| 242 | opts.allowFragmentation = true; |
| 243 | opts.allowReassembly = true; |
schneiderklaus | 5f5c992 | 2018-07-25 20:35:03 -0700 | [diff] [blame] | 244 | opts.allowCongestionMarking = true; |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 245 | |
| 246 | auto linkService = make_unique<::nfd::face::GenericLinkService>(opts); |
| 247 | |
| 248 | auto transport = make_unique<NetDeviceTransport>(node, netDevice, |
| 249 | constructFaceUri(netDevice), |
| 250 | "netdev://[ff:ff:ff:ff:ff:ff]"); |
| 251 | |
| 252 | auto face = std::make_shared<Face>(std::move(linkService), std::move(transport)); |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 253 | face->setMetric(1); |
| 254 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 255 | ndn->addFace(face); |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 256 | NS_LOG_LOGIC("Node " << node->GetId() << ": added Face as face #" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 257 | << face->getLocalUri()); |
Alexander Afanasyev | c17e4bd | 2013-02-17 14:31:56 -0800 | [diff] [blame] | 258 | |
| 259 | return face; |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 262 | shared_ptr<Face> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 263 | StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn, |
| 264 | Ptr<NetDevice> device) const |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 265 | { |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 266 | NS_LOG_DEBUG("Creating point-to-point Face on node " << node->GetId()); |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 267 | |
Alexander Afanasyev | a9d889b | 2016-09-08 18:34:25 -0700 | [diff] [blame] | 268 | Ptr<PointToPointNetDevice> netDevice = DynamicCast<PointToPointNetDevice>(device); |
| 269 | NS_ASSERT(netDevice != nullptr); |
| 270 | |
| 271 | // access the other end of the link |
| 272 | Ptr<PointToPointChannel> channel = DynamicCast<PointToPointChannel>(netDevice->GetChannel()); |
| 273 | NS_ASSERT(channel != nullptr); |
| 274 | |
| 275 | Ptr<NetDevice> remoteNetDevice = channel->GetDevice(0); |
| 276 | if (remoteNetDevice->GetNode() == node) |
| 277 | remoteNetDevice = channel->GetDevice(1); |
| 278 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 279 | // Create an ndnSIM-specific transport instance |
| 280 | ::nfd::face::GenericLinkService::Options opts; |
| 281 | opts.allowFragmentation = true; |
| 282 | opts.allowReassembly = true; |
schneiderklaus | 5f5c992 | 2018-07-25 20:35:03 -0700 | [diff] [blame] | 283 | opts.allowCongestionMarking = true; |
Alexander Afanasyev | a9d889b | 2016-09-08 18:34:25 -0700 | [diff] [blame] | 284 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 285 | auto linkService = make_unique<::nfd::face::GenericLinkService>(opts); |
| 286 | |
| 287 | auto transport = make_unique<NetDeviceTransport>(node, netDevice, |
| 288 | constructFaceUri(netDevice), |
| 289 | constructFaceUri(remoteNetDevice)); |
| 290 | |
| 291 | auto face = std::make_shared<Face>(std::move(linkService), std::move(transport)); |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 292 | face->setMetric(1); |
| 293 | |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 294 | ndn->addFace(face); |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 295 | NS_LOG_LOGIC("Node " << node->GetId() << ": added Face as face #" |
Spyridon Mastorakis | 9760bd0 | 2014-11-12 13:32:55 -0800 | [diff] [blame] | 296 | << face->getLocalUri()); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | 122f378 | 2013-02-02 00:04:40 -0800 | [diff] [blame] | 298 | return face; |
| 299 | } |
| 300 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 301 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 302 | StackHelper::Install(const std::string& nodeName) const |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 303 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 304 | Ptr<Node> node = Names::Find<Node>(nodeName); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 305 | Install(node); |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Spyridon Mastorakis | 21226ae | 2015-03-31 18:03:11 -0700 | [diff] [blame] | 308 | void |
| 309 | StackHelper::Update(Ptr<Node> node) |
| 310 | { |
| 311 | if (node->GetObject<L3Protocol>() == 0) { |
| 312 | Install(node); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 317 | |
| 318 | for (uint32_t index = 0; index < node->GetNDevices(); index++) { |
| 319 | |
| 320 | Ptr<NetDevice> device = node->GetDevice(index); |
| 321 | |
| 322 | if (ndn->getFaceByNetDevice(device) == nullptr) { |
| 323 | this->createAndRegisterFace(node, ndn, device); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | StackHelper::Update(const NodeContainer& c) |
| 330 | { |
| 331 | for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) { |
| 332 | Update(*i); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | StackHelper::Update(const std::string& nodeName) |
| 338 | { |
| 339 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 340 | Update(node); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | StackHelper::UpdateAll() |
| 345 | { |
| 346 | Update(NodeContainer::GetGlobal()); |
| 347 | } |
| 348 | |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 349 | shared_ptr<Face> |
Spyridon Mastorakis | 21226ae | 2015-03-31 18:03:11 -0700 | [diff] [blame] | 350 | StackHelper::createAndRegisterFace(Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> device) const |
| 351 | { |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 352 | shared_ptr<Face> face; |
Spyridon Mastorakis | 21226ae | 2015-03-31 18:03:11 -0700 | [diff] [blame] | 353 | |
| 354 | for (const auto& item : m_netDeviceCallbacks) { |
| 355 | if (device->GetInstanceTypeId() == item.first || |
| 356 | device->GetInstanceTypeId().IsChildOf(item.first)) { |
| 357 | face = item.second(node, ndn, device); |
| 358 | if (face != 0) |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (face == 0) { |
| 364 | face = DefaultNetDeviceCallback(node, ndn, device); |
| 365 | } |
| 366 | |
| 367 | if (m_needSetDefaultRoutes) { |
| 368 | // default route with lowest priority possible |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 369 | FibHelper::AddRoute(node, "/", face, std::numeric_limits<int32_t>::max()); |
Spyridon Mastorakis | 21226ae | 2015-03-31 18:03:11 -0700 | [diff] [blame] | 370 | } |
| 371 | return face; |
| 372 | } |
| 373 | |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 374 | void |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 375 | StackHelper::disableStrategyChoiceManager() |
| 376 | { |
| 377 | m_isStrategyChoiceManagerDisabled = true; |
| 378 | } |
| 379 | |
| 380 | void |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 381 | StackHelper::disableForwarderStatusManager() |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 382 | { |
Alexander Afanasyev | a91aab4 | 2016-09-08 15:47:38 -0700 | [diff] [blame] | 383 | m_isForwarderStatusManagerDisabled = true; |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Chavoosh Ghasemi | 58ef861 | 2018-09-12 15:12:49 -0700 | [diff] [blame] | 386 | void |
| 387 | StackHelper::SetLinkDelayAsFaceMetric() |
| 388 | { |
| 389 | for (uint32_t i = 0; i < NodeList::GetNNodes(); ++i) { |
| 390 | auto ndn = NodeList::GetNode(i)->GetObject<L3Protocol>(); |
| 391 | if (ndn == nullptr) |
| 392 | continue; |
| 393 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 394 | for (auto& face : ndn->getFaceTable()) { |
Chavoosh Ghasemi | 58ef861 | 2018-09-12 15:12:49 -0700 | [diff] [blame] | 395 | auto transport = dynamic_cast<NetDeviceTransport*>(face.getTransport()); |
| 396 | if (transport == nullptr) |
| 397 | continue; |
| 398 | auto p2p = dynamic_cast<PointToPointChannel*>(&(*(transport->GetNetDevice()->GetChannel()))); |
| 399 | TimeValue currentDelay; |
| 400 | p2p->GetAttribute("Delay", currentDelay); |
| 401 | face.setMetric((currentDelay.Get().ToDouble(Time::S)) * 1000); |
| 402 | |
| 403 | std::cout << "Node " << i << ": Face " << face.getId() |
| 404 | << " with metric " << face.getMetric() << "\n"; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 409 | void |
| 410 | StackHelper::ProcessWarmupEvents() |
| 411 | { |
| 412 | Simulator::Stop(Seconds(0)); |
| 413 | #if HAVE_NS3_VISUALIZER |
| 414 | auto impl = DynamicCast<VisualSimulatorImpl>(Simulator::GetImplementation()); |
| 415 | if (impl != nullptr) { |
| 416 | impl->RunRealSimulator(); |
| 417 | } |
| 418 | else { |
| 419 | Simulator::Run(); |
| 420 | } |
| 421 | #else |
| 422 | Simulator::Run(); |
| 423 | #endif // HAVE_NS3_VISUALIZER |
| 424 | } |
| 425 | |
| 426 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 427 | } // namespace ndn |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 428 | } // namespace ns3 |