blob: ce3f52917f95e945e6ac37f0792278ab1b969f2c [file] [log] [blame]
Spyridon Mastorakis588fd102014-11-20 19:50:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2014 Regents of the University of California.
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
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));
62 StackHelper::getKeyChain().signWithSha256(*command);
63
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));
80 StackHelper::getKeyChain().signWithSha256(*command);
81
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
181} // namespace ndn
182
183} // namespace ns