Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 15 | * |
| 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 | **/ |
| 19 | |
| 20 | #include "ndn-fib-helper.hpp" |
| 21 | |
| 22 | #include "ns3/assert.h" |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/object.h" |
| 25 | #include "ns3/names.h" |
| 26 | #include "ns3/packet-socket-factory.h" |
| 27 | #include "ns3/config.h" |
| 28 | #include "ns3/simulator.h" |
| 29 | #include "ns3/string.h" |
| 30 | #include "ns3/net-device.h" |
| 31 | #include "ns3/channel.h" |
| 32 | #include "ns3/callback.h" |
| 33 | #include "ns3/node.h" |
| 34 | #include "ns3/core-config.h" |
| 35 | #include "ns3/point-to-point-net-device.h" |
| 36 | #include "ns3/point-to-point-helper.h" |
| 37 | #include "ns3/callback.h" |
| 38 | #include "ns3/node-list.h" |
| 39 | #include "ns3/data-rate.h" |
| 40 | |
| 41 | #include "daemon/mgmt/fib-manager.hpp" |
| 42 | #include "ns3/ndnSIM/model/ndn-l3-protocol.hpp" |
| 43 | #include "ns3/ndnSIM/helper/ndn-stack-helper.hpp" |
| 44 | |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 45 | namespace ns3 { |
| 46 | namespace ndn { |
| 47 | |
| 48 | NS_LOG_COMPONENT_DEFINE("ndn.FibHelper"); |
| 49 | |
| 50 | void |
| 51 | FibHelper::AddNextHop(const ControlParameters& parameters, Ptr<Node> node) |
| 52 | { |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 53 | Block encodedParameters(parameters.wireEncode()); |
| 54 | |
| 55 | Name commandName("/localhost/nfd/fib"); |
| 56 | commandName.append("add-nexthop"); |
| 57 | commandName.append(encodedParameters); |
| 58 | |
| 59 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 60 | command->setCanBePrefix(false); |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 61 | StackHelper::getKeyChain().sign(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 62 | |
| 63 | Ptr<L3Protocol> l3protocol = node->GetObject<L3Protocol>(); |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 64 | l3protocol->injectInterest(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | FibHelper::RemoveNextHop(const ControlParameters& parameters, Ptr<Node> node) |
| 69 | { |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 70 | Block encodedParameters(parameters.wireEncode()); |
| 71 | |
| 72 | Name commandName("/localhost/nfd/fib"); |
| 73 | commandName.append("remove-nexthop"); |
| 74 | commandName.append(encodedParameters); |
| 75 | |
| 76 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 77 | command->setCanBePrefix(false); |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 78 | StackHelper::getKeyChain().sign(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 80 | Ptr<L3Protocol> l3protocol = node->GetObject<L3Protocol>(); |
| 81 | l3protocol->injectInterest(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face, int32_t metric) |
| 86 | { |
| 87 | NS_LOG_LOGIC("[" << node->GetId() << "]$ route add " << prefix << " via " << face->getLocalUri() |
| 88 | << " metric " << metric); |
| 89 | |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 90 | ControlParameters parameters; |
| 91 | parameters.setName(prefix); |
| 92 | parameters.setFaceId(face->getId()); |
| 93 | parameters.setCost(metric); |
| 94 | |
| 95 | AddNextHop(parameters, node); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId, int32_t metric) |
| 100 | { |
| 101 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 102 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 103 | |
| 104 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 105 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 106 | << node->GetId() << "]"); |
| 107 | |
| 108 | AddRoute(node, prefix, face, metric); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | FibHelper::AddRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId, |
| 113 | int32_t metric) |
| 114 | { |
| 115 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 116 | NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist"); |
| 117 | |
| 118 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 119 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 120 | |
| 121 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 122 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName |
| 123 | << "]"); |
| 124 | |
| 125 | AddRoute(node, prefix, face, metric); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode, int32_t metric) |
| 130 | { |
| 131 | for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) { |
| 132 | Ptr<PointToPointNetDevice> netDevice = |
| 133 | DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId)); |
| 134 | if (netDevice == 0) |
| 135 | continue; |
| 136 | |
| 137 | Ptr<Channel> channel = netDevice->GetChannel(); |
| 138 | if (channel == 0) |
| 139 | continue; |
| 140 | |
| 141 | if (channel->GetDevice(0)->GetNode() == otherNode |
| 142 | || channel->GetDevice(1)->GetNode() == otherNode) { |
| 143 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 144 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 145 | |
| 146 | shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice); |
| 147 | NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link"); |
| 148 | |
| 149 | AddRoute(node, prefix, face, metric); |
| 150 | |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | NS_FATAL_ERROR("Cannot add route: Node# " << node->GetId() << " and Node# " << otherNode->GetId() |
| 156 | << " are not connected"); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | FibHelper::AddRoute(const std::string& nodeName, const Name& prefix, |
| 161 | const std::string& otherNodeName, int32_t metric) |
| 162 | { |
| 163 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 164 | NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist"); |
| 165 | |
| 166 | Ptr<Node> otherNode = Names::Find<Node>(otherNodeName); |
| 167 | NS_ASSERT_MSG(otherNode != 0, "Node [" << otherNodeName << "] does not exist"); |
| 168 | |
| 169 | AddRoute(node, prefix, otherNode, metric); |
| 170 | } |
| 171 | |
Yuanzhi Gao | 24a849d | 2015-05-16 14:32:27 -0700 | [diff] [blame] | 172 | void |
| 173 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face) |
| 174 | { |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 175 | NS_LOG_LOGIC("[" << node->GetId() << "]$ route del " << prefix << " via " << face->getLocalUri()); |
| 176 | |
Yuanzhi Gao | 24a849d | 2015-05-16 14:32:27 -0700 | [diff] [blame] | 177 | // Get L3Protocol object |
| 178 | Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>(); |
| 179 | // Get the forwarder instance |
| 180 | shared_ptr<nfd::Forwarder> m_forwarder = L3protocol->getForwarder(); |
| 181 | |
| 182 | ControlParameters parameters; |
| 183 | parameters.setName(prefix); |
| 184 | parameters.setFaceId(face->getId()); |
| 185 | |
| 186 | RemoveNextHop(parameters, node); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId) |
| 191 | { |
| 192 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 193 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 194 | |
| 195 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 196 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 197 | << node->GetId() << "]"); |
| 198 | |
| 199 | RemoveRoute(node, prefix, face); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId) |
| 204 | { |
| 205 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 206 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 207 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 208 | |
| 209 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 210 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 211 | << node->GetId() << "]"); |
| 212 | |
| 213 | RemoveRoute(node, prefix, face); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode) |
| 218 | { |
| 219 | for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) { |
| 220 | Ptr<PointToPointNetDevice> netDevice = |
| 221 | DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId)); |
| 222 | if (netDevice == 0) |
| 223 | continue; |
| 224 | |
| 225 | Ptr<Channel> channel = netDevice->GetChannel(); |
| 226 | if (channel == 0) |
| 227 | continue; |
| 228 | |
| 229 | if (channel->GetDevice(0)->GetNode() == otherNode |
| 230 | || channel->GetDevice(1)->GetNode() == otherNode) { |
| 231 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 232 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 233 | |
| 234 | shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice); |
| 235 | NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link"); |
| 236 | |
| 237 | RemoveRoute(node, prefix, face); |
| 238 | |
| 239 | return; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | NS_FATAL_ERROR("Cannot remove route: Node# " << node->GetId() << " and Node# " << otherNode->GetId() |
| 244 | << " are not connected"); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix, |
| 249 | const std::string& otherNodeName) |
| 250 | { |
| 251 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 252 | Ptr<Node> otherNode = Names::Find<Node>(otherNodeName); |
| 253 | RemoveRoute(node, prefix, otherNode); |
| 254 | } |
| 255 | |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 256 | } // namespace ndn |
| 257 | |
| 258 | } // namespace ns |