blob: 70a31718fc2d024326f48533e2a80fda009cdd1c [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-link-control-helper.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070021
22#include "ns3/assert.h"
23#include "ns3/names.h"
24#include "ns3/point-to-point-net-device.h"
25#include "ns3/point-to-point-channel.h"
26#include "ns3/channel.h"
27#include "ns3/log.h"
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080028#include "ns3/error-model.h"
29#include "ns3/string.h"
30#include "ns3/boolean.h"
31#include "ns3/double.h"
32#include "ns3/pointer.h"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070033
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080034#include "model/ndn-l3-protocol.hpp"
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070035#include "model/ndn-net-device-link-service.hpp"
36#include "NFD/daemon/face/face.hpp"
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080037
38#include "fw/forwarder.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070039
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper");
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070041
42namespace ns3 {
43namespace ndn {
44
45void
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080046LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070047{
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080048 NS_LOG_FUNCTION(node1 << node2 << errorRate);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070049
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080050 NS_ASSERT(node1 != nullptr && node2 != nullptr);
51 NS_ASSERT(errorRate <= 1.0);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052
53 Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
54 Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
55
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080056 NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070057
58 // iterate over all faces to find the right one
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080059 for (const auto& face : ndn1->getForwarder()->getFaceTable()) {
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070060 auto linkService = dynamic_cast<NetDeviceLinkService*>(face->getLinkService());
61 if (linkService == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070063
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070064 Ptr<PointToPointNetDevice> nd1 = linkService->GetNetDevice()->GetObject<PointToPointNetDevice>();
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080065 if (nd1 == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070067
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 Ptr<Channel> channel = nd1->GetChannel();
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080069 if (channel == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070071
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070073
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
75 if (nd2->GetNode() == node1)
76 nd2 = ppChannel->GetDevice(1);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070077
Spyridon Mastorakisda325912015-05-28 22:45:33 -070078 if (nd2->GetNode() == node2) {
79 ObjectFactory errorFactory("ns3::RateErrorModel");
80 errorFactory.Set("ErrorUnit", StringValue("ERROR_UNIT_PACKET"));
81 errorFactory.Set("ErrorRate", DoubleValue(errorRate));
82 if (errorRate <= 0) {
83 errorFactory.Set("IsEnabled", BooleanValue(false));
84 }
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080085
Spyridon Mastorakisda325912015-05-28 22:45:33 -070086 nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
87 nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
88 return;
89 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 }
Spyridon Mastorakisda325912015-05-28 22:45:33 -070091 NS_FATAL_ERROR("There is no link to fail between the requested nodes");
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070092}
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080093
94void
95LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
96{
97 setErrorRate(node1, node2, 1.0);
98}
99
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700100void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700102{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700104}
105
106void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700108{
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -0800109 setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700110}
111
112void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700114{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700116}
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -0800117
118} // namespace ndn
119} // namespace ns3