blob: 19536bf5e7637392d0dcd86aeee7d086aa467226 [file] [log] [blame]
Spyridon Mastorakis588fd102014-11-20 19:50:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08003 * Copyright (c) 2011-2015 Regents of the University of California.
Spyridon Mastorakis588fd102014-11-20 19:50:02 -08004 *
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
46namespace ns3 {
47namespace ndn {
48
49NS_LOG_COMPONENT_DEFINE("ndn.FibHelper");
50
51void
52FibHelper::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 Afanasyevdde1e812015-01-06 14:26:09 -080062 StackHelper::getKeyChain().sign(*command);
Spyridon Mastorakis588fd102014-11-20 19:50:02 -080063
64 Ptr<L3Protocol> l3protocol = node->GetObject<L3Protocol>();
65 shared_ptr<nfd::FibManager> fibManager = l3protocol->getFibManager();
66 fibManager->onFibRequest(*command);
67}
68
69void
70FibHelper::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 Afanasyevdde1e812015-01-06 14:26:09 -080080 StackHelper::getKeyChain().sign(*command);
Spyridon Mastorakis588fd102014-11-20 19:50:02 -080081
82 Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>();
83 shared_ptr<nfd::FibManager> fibManager = L3protocol->getFibManager();
84 // fibManager->addInterestRule(commandName.toUri(), key, *keyChain.getPublicKey (key));
85 fibManager->onFibRequest(*command);
86}
87
88void
89FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face, int32_t metric)
90{
91 NS_LOG_LOGIC("[" << node->GetId() << "]$ route add " << prefix << " via " << face->getLocalUri()
92 << " metric " << metric);
93
94 // Get L3Protocol object
95 Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>();
96 // Get the forwarder instance
97 shared_ptr<nfd::Forwarder> m_forwarder = L3protocol->getForwarder();
98
99 ControlParameters parameters;
100 parameters.setName(prefix);
101 parameters.setFaceId(face->getId());
102 parameters.setCost(metric);
103
104 AddNextHop(parameters, node);
105}
106
107void
108FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId, int32_t metric)
109{
110 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
111 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
112
113 shared_ptr<Face> face = ndn->getFaceById(faceId);
114 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node ["
115 << node->GetId() << "]");
116
117 AddRoute(node, prefix, face, metric);
118}
119
120void
121FibHelper::AddRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId,
122 int32_t metric)
123{
124 Ptr<Node> node = Names::Find<Node>(nodeName);
125 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
126
127 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
128 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
129
130 shared_ptr<Face> face = ndn->getFaceById(faceId);
131 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName
132 << "]");
133
134 AddRoute(node, prefix, face, metric);
135}
136
137void
138FibHelper::AddRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode, int32_t metric)
139{
140 for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) {
141 Ptr<PointToPointNetDevice> netDevice =
142 DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId));
143 if (netDevice == 0)
144 continue;
145
146 Ptr<Channel> channel = netDevice->GetChannel();
147 if (channel == 0)
148 continue;
149
150 if (channel->GetDevice(0)->GetNode() == otherNode
151 || channel->GetDevice(1)->GetNode() == otherNode) {
152 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
153 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
154
155 shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice);
156 NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link");
157
158 AddRoute(node, prefix, face, metric);
159
160 return;
161 }
162 }
163
164 NS_FATAL_ERROR("Cannot add route: Node# " << node->GetId() << " and Node# " << otherNode->GetId()
165 << " are not connected");
166}
167
168void
169FibHelper::AddRoute(const std::string& nodeName, const Name& prefix,
170 const std::string& otherNodeName, int32_t metric)
171{
172 Ptr<Node> node = Names::Find<Node>(nodeName);
173 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
174
175 Ptr<Node> otherNode = Names::Find<Node>(otherNodeName);
176 NS_ASSERT_MSG(otherNode != 0, "Node [" << otherNodeName << "] does not exist");
177
178 AddRoute(node, prefix, otherNode, metric);
179}
180
Yuanzhi Gao24a849d2015-05-16 14:32:27 -0700181void
182FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, shared_ptr<Face> face)
183{
184 // Get L3Protocol object
185 Ptr<L3Protocol> L3protocol = node->GetObject<L3Protocol>();
186 // Get the forwarder instance
187 shared_ptr<nfd::Forwarder> m_forwarder = L3protocol->getForwarder();
188
189 ControlParameters parameters;
190 parameters.setName(prefix);
191 parameters.setFaceId(face->getId());
192
193 RemoveNextHop(parameters, node);
194}
195
196void
197FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, uint32_t faceId)
198{
199 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
200 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
201
202 shared_ptr<Face> face = ndn->getFaceById(faceId);
203 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node ["
204 << node->GetId() << "]");
205
206 RemoveRoute(node, prefix, face);
207}
208
209void
210FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix, uint32_t faceId)
211{
212 Ptr<Node> node = Names::Find<Node>(nodeName);
213 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
214 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
215
216 shared_ptr<Face> face = ndn->getFaceById(faceId);
217 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node ["
218 << node->GetId() << "]");
219
220 RemoveRoute(node, prefix, face);
221}
222
223void
224FibHelper::RemoveRoute(Ptr<Node> node, const Name& prefix, Ptr<Node> otherNode)
225{
226 for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) {
227 Ptr<PointToPointNetDevice> netDevice =
228 DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId));
229 if (netDevice == 0)
230 continue;
231
232 Ptr<Channel> channel = netDevice->GetChannel();
233 if (channel == 0)
234 continue;
235
236 if (channel->GetDevice(0)->GetNode() == otherNode
237 || channel->GetDevice(1)->GetNode() == otherNode) {
238 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
239 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
240
241 shared_ptr<Face> face = ndn->getFaceByNetDevice(netDevice);
242 NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link");
243
244 RemoveRoute(node, prefix, face);
245
246 return;
247 }
248 }
249
250 NS_FATAL_ERROR("Cannot remove route: Node# " << node->GetId() << " and Node# " << otherNode->GetId()
251 << " are not connected");
252}
253
254void
255FibHelper::RemoveRoute(const std::string& nodeName, const Name& prefix,
256 const std::string& otherNodeName)
257{
258 Ptr<Node> node = Names::Find<Node>(nodeName);
259 Ptr<Node> otherNode = Names::Find<Node>(otherNodeName);
260 RemoveRoute(node, prefix, otherNode);
261}
262
Spyridon Mastorakis588fd102014-11-20 19:50:02 -0800263} // namespace ndn
264
265} // namespace ns