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