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