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 | |
| 45 | |
| 46 | namespace ns3 { |
| 47 | namespace ndn { |
| 48 | |
| 49 | NS_LOG_COMPONENT_DEFINE("ndn.FibHelper"); |
| 50 | |
| 51 | void |
| 52 | FibHelper::AddNextHop(const ControlParameters& parameters, Ptr<Node> node) |
| 53 | { |
| 54 | NS_LOG_DEBUG("Add Next Hop command was initialized"); |
| 55 | Block encodedParameters(parameters.wireEncode()); |
| 56 | |
| 57 | Name commandName("/localhost/nfd/fib"); |
| 58 | commandName.append("add-nexthop"); |
| 59 | commandName.append(encodedParameters); |
| 60 | |
| 61 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 62 | StackHelper::getKeyChain().sign(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 63 | |
| 64 | Ptr<L3Protocol> l3protocol = node->GetObject<L3Protocol>(); |
| 65 | shared_ptr<nfd::FibManager> fibManager = l3protocol->getFibManager(); |
| 66 | fibManager->onFibRequest(*command); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | FibHelper::RemoveNextHop(const ControlParameters& parameters, Ptr<Node> node) |
| 71 | { |
| 72 | NS_LOG_DEBUG("Remove Next Hop command was initialized"); |
| 73 | Block encodedParameters(parameters.wireEncode()); |
| 74 | |
| 75 | Name commandName("/localhost/nfd/fib"); |
| 76 | commandName.append("remove-nexthop"); |
| 77 | commandName.append(encodedParameters); |
| 78 | |
| 79 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Alexander Afanasyev | dde1e81 | 2015-01-06 14:26:09 -0800 | [diff] [blame] | 80 | StackHelper::getKeyChain().sign(*command); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 81 | |
| 82 | Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>(); |
| 83 | shared_ptr<nfd::FibManager> fibManager = L3protocol->getFibManager(); |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 84 | fibManager->onFibRequest(*command); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face, int32_t metric) |
| 89 | { |
| 90 | NS_LOG_LOGIC("[" << node->GetId() << "]$ route add " << prefix << " via " << face->getLocalUri() |
| 91 | << " metric " << metric); |
| 92 | |
| 93 | // Get L3Protocol object |
| 94 | Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>(); |
| 95 | // Get the forwarder instance |
| 96 | shared_ptr<nfd::Forwarder> m_forwarder = L3protocol->getForwarder(); |
| 97 | |
| 98 | ControlParameters parameters; |
| 99 | parameters.setName(prefix); |
| 100 | parameters.setFaceId(face->getId()); |
| 101 | parameters.setCost(metric); |
| 102 | |
| 103 | AddNextHop(parameters, node); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId, int32_t metric) |
| 108 | { |
| 109 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 110 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 111 | |
| 112 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 113 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 114 | << node->GetId() << "]"); |
| 115 | |
| 116 | AddRoute(node, prefix, face, metric); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | FibHelper::AddRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId, |
| 121 | int32_t metric) |
| 122 | { |
| 123 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 124 | NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist"); |
| 125 | |
| 126 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 127 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 128 | |
| 129 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 130 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName |
| 131 | << "]"); |
| 132 | |
| 133 | AddRoute(node, prefix, face, metric); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode, int32_t metric) |
| 138 | { |
| 139 | for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) { |
| 140 | Ptr<PointToPointNetDevice> netDevice = |
| 141 | DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId)); |
| 142 | if (netDevice == 0) |
| 143 | continue; |
| 144 | |
| 145 | Ptr<Channel> channel = netDevice->GetChannel(); |
| 146 | if (channel == 0) |
| 147 | continue; |
| 148 | |
| 149 | if (channel->GetDevice(0)->GetNode() == otherNode |
| 150 | || channel->GetDevice(1)->GetNode() == otherNode) { |
| 151 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 152 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 153 | |
| 154 | shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice); |
| 155 | NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link"); |
| 156 | |
| 157 | AddRoute(node, prefix, face, metric); |
| 158 | |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | NS_FATAL_ERROR("Cannot add route: Node# " << node->GetId() << " and Node# " << otherNode->GetId() |
| 164 | << " are not connected"); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | FibHelper::AddRoute(const std::string& nodeName, const Name& prefix, |
| 169 | const std::string& otherNodeName, int32_t metric) |
| 170 | { |
| 171 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 172 | NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist"); |
| 173 | |
| 174 | Ptr<Node> otherNode = Names::Find<Node>(otherNodeName); |
| 175 | NS_ASSERT_MSG(otherNode != 0, "Node [" << otherNodeName << "] does not exist"); |
| 176 | |
| 177 | AddRoute(node, prefix, otherNode, metric); |
| 178 | } |
| 179 | |
Yuanzhi Gao | 24a849d | 2015-05-16 14:32:27 -0700 | [diff] [blame] | 180 | void |
| 181 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face) |
| 182 | { |
| 183 | // Get L3Protocol object |
| 184 | Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>(); |
| 185 | // Get the forwarder instance |
| 186 | shared_ptr<nfd::Forwarder> m_forwarder = L3protocol->getForwarder(); |
| 187 | |
| 188 | ControlParameters parameters; |
| 189 | parameters.setName(prefix); |
| 190 | parameters.setFaceId(face->getId()); |
| 191 | |
| 192 | RemoveNextHop(parameters, node); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId) |
| 197 | { |
| 198 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 199 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 200 | |
| 201 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 202 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 203 | << node->GetId() << "]"); |
| 204 | |
| 205 | RemoveRoute(node, prefix, face); |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId) |
| 210 | { |
| 211 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 212 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 213 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 214 | |
| 215 | shared_ptr<Face> face = ndn->getFaceById(faceId); |
| 216 | NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" |
| 217 | << node->GetId() << "]"); |
| 218 | |
| 219 | RemoveRoute(node, prefix, face); |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode) |
| 224 | { |
| 225 | for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) { |
| 226 | Ptr<PointToPointNetDevice> netDevice = |
| 227 | DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId)); |
| 228 | if (netDevice == 0) |
| 229 | continue; |
| 230 | |
| 231 | Ptr<Channel> channel = netDevice->GetChannel(); |
| 232 | if (channel == 0) |
| 233 | continue; |
| 234 | |
| 235 | if (channel->GetDevice(0)->GetNode() == otherNode |
| 236 | || channel->GetDevice(1)->GetNode() == otherNode) { |
| 237 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 238 | NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node"); |
| 239 | |
| 240 | shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice); |
| 241 | NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link"); |
| 242 | |
| 243 | RemoveRoute(node, prefix, face); |
| 244 | |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | NS_FATAL_ERROR("Cannot remove route: Node# " << node->GetId() << " and Node# " << otherNode->GetId() |
| 250 | << " are not connected"); |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix, |
| 255 | const std::string& otherNodeName) |
| 256 | { |
| 257 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 258 | Ptr<Node> otherNode = Names::Find<Node>(otherNodeName); |
| 259 | RemoveRoute(node, prefix, otherNode); |
| 260 | } |
| 261 | |
Spyridon Mastorakis | 588fd10 | 2014-11-20 19:50:02 -0800 | [diff] [blame] | 262 | } // namespace ndn |
| 263 | |
| 264 | } // namespace ns |