A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-click-routing.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Lalith Suresh
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Lalith Suresh <suresh.lalith@gmail.com>
18 */
19
20#ifndef IPV4_CLICK_ROUTING_H
21#define IPV4_CLICK_ROUTING_H
22
23#include "ns3/ipv4-routing-protocol.h"
24#include "ns3/ipv4.h"
25#include "ns3/object.h"
26#include "ns3/packet.h"
27#include "ns3/test.h"
28
29#include <map>
30#include <string>
31#include <sys/time.h>
32#include <sys/types.h>
33
37// These are in #include <click/simclick.h>,
38// here we just need a forward declaration.
39struct simclick_node;
40typedef struct simclick_node simclick_node_t;
41
42namespace ns3
43{
44
45/**
46 * \defgroup click Click Routing
47 * This section documents the API of the ns-3 click module. For a generic functional description,
48 * please refer to the ns-3 manual.
49 */
50
51class UniformRandomVariable;
52
53/**
54 * \ingroup click
55 * \brief Class to allow a node to use Click for external routing
56 */
58{
59 public:
60 // Allow test cases to access private members
61 friend class ::ClickTrivialTest;
62 friend class ::ClickIfidFromNameTest;
63 friend class ::ClickIpMacAddressFromNameTest;
64
65 /**
66 * Get type ID.
67 *
68 * \return TypeId.
69 */
70 static TypeId GetTypeId();
71
72 /** Constructor. */
74 ~Ipv4ClickRouting() override;
75
76 /**
77 * Get the uniform random variable.
78 *
79 * \return Uniform random variable.
80 */
82
83 protected:
84 void DoInitialize() override;
85
86 public:
87 void DoDispose() override;
88
89 /**
90 * \brief Click configuration file to be used by the node's Click Instance.
91 * \param clickfile name of .click configuration file
92 */
93 void SetClickFile(std::string clickfile);
94
95 /**
96 * \brief Click defines to be used by the node's Click Instance.
97 * \param defines mapping of defines for .click configuration file parsing
98 */
99 void SetDefines(std::map<std::string, std::string> defines);
100
101 /**
102 * \brief Name of the node as to be used by Click. Required for Click Dumps.
103 * \param name Name to be assigned to the node.
104 */
105 void SetNodeName(std::string name);
106
107 /**
108 * \brief Name of the routing table element being used by Click. Required for RouteOutput ()
109 * \param name Name of the routing table element.
110 */
111 void SetClickRoutingTableElement(std::string name);
112
113 /**
114 * \brief Read Handler interface for a node's Click Elements.
115 * Allows a user to read state information of a Click element.
116 * \param elementName name of the Click element.
117 * \param handlerName name of the handler to be read.
118 * \return String read.
119 */
120 std::string ReadHandler(std::string elementName, std::string handlerName);
121
122 /**
123 * \brief Write Handler interface for a node's Click Elements.
124 * Allows a user to modify state information of a Click element.
125 * \param elementName name of the Click element.
126 * \param handlerName name of the handler to be read.
127 * \param writeString string to be written using the write handler.
128 * \return Write operation status.
129 */
130 int WriteHandler(std::string elementName, std::string handlerName, std::string writeString);
131
132 /**
133 * \brief Sets an interface to run on promiscuous mode.
134 * \param ifid Interface ID.
135 */
136 void SetPromisc(int ifid);
137
138 private:
139 /// Pointer to the simclick node
141
142 /**
143 * \brief Provide a mapping between the node reference used by Click and the corresponding
144 * Ipv4ClickRouting instance.
145 */
146 static std::map<simclick_node_t*, Ptr<Ipv4ClickRouting>> m_clickInstanceFromSimNode;
147
148 public:
149 /**
150 * \brief Allows the Click service methods, which reside outside Ipv4ClickRouting, to get the
151 * required Ipv4ClickRouting instances.
152 * \param simnode The Click simclick_node_t instance for which the Ipv4ClickRouting instance is
153 * required
154 * \return A Ptr to the required Ipv4ClickRouting instance
155 */
157
158 public:
159 /**
160 * \brief Provides for SIMCLICK_GET_DEFINES
161 * \return The defines mapping for .click configuration file parsing
162 */
163 std::map<std::string, std::string> GetDefines();
164
165 /**
166 * \brief Provides for SIMCLICK_IFID_FROM_NAME
167 * \param ifname The name of the interface
168 * \return The interface ID which corresponds to ifname
169 */
170 int GetInterfaceId(const char* ifname);
171
172 /**
173 * \brief Provides for SIMCLICK_IPADDR_FROM_NAME
174 * \param ifid The interface ID for which the IP Address is required
175 * \return The IP Address of the interface in string format
176 */
177 std::string GetIpAddressFromInterfaceId(int ifid);
178
179 /**
180 * \brief Provides for SIMCLICK_IPPREFIX_FROM_NAME
181 * \param ifid The interface ID for which the IP Prefix is required
182 * \return The IP Prefix of the interface in string format
183 */
184 std::string GetIpPrefixFromInterfaceId(int ifid);
185
186 /**
187 * \brief Provides for SIMCLICK_MACADDR_FROM_NAME
188 * \param ifid The interface ID for which the MAC Address is required
189 * \return The MAC Address of the interface in string format
190 */
191 std::string GetMacAddressFromInterfaceId(int ifid);
192
193 /**
194 * \brief Provides for SIMCLICK_GET_NODE_NAME
195 * \return The Node name
196 */
197 std::string GetNodeName();
198
199 /**
200 * \brief Provides for SIMCLICK_IF_READY
201 * \param ifid Interface ID
202 * \return Returns 1, if the interface is ready, -1 if ifid is invalid
203 */
204 bool IsInterfaceReady(int ifid);
205
206 /**
207 * \brief Set the Ipv4 instance to be used
208 * \param ipv4 The Ipv4 instance
209 */
210 void SetIpv4(Ptr<Ipv4> ipv4) override;
211
212 private:
213 /**
214 * \brief Used internally in DoInitialize () to Add a mapping to m_clickInstanceFromSimNode
215 * mapping
216 */
218
219 /**
220 * \brief Get current simulation time as a timeval.
221 * \return Current simulation time as a timeval.
222 */
223 struct timeval GetTimevalFromNow() const;
224
225 /**
226 * \brief This method has to be scheduled every time Click calls SIMCLICK_SCHEDULE
227 */
228 void RunClickEvent();
229
230 public:
231 /**
232 * \brief Schedules simclick_click_run to run at the given time
233 * \param when Time at which the simclick_click_run instance should be run
234 */
235 void HandleScheduleFromClick(const struct timeval* when);
236
237 /**
238 * \brief Receives a packet from Click
239 * \param ifid The interface ID from which the packet is arriving
240 * \param type The type of packet as defined in click/simclick.h
241 * \param data The contents of the packet
242 * \param len The length of the packet
243 */
244 void HandlePacketFromClick(int ifid, int type, const unsigned char* data, int len);
245
246 /**
247 * \brief Sends a packet to Click
248 * \param ifid The interface ID from which the packet is arriving
249 * \param type The type of packet as defined in click/simclick.h
250 * \param data The contents of the packet
251 * \param len The length of the packet
252 */
253 void SendPacketToClick(int ifid, int type, const unsigned char* data, int len);
254
255 /**
256 * \brief Allow a higher layer to send data through Click. (From Ipv4ExtRouting)
257 * \param p The packet to be sent
258 * \param src The source IP Address
259 * \param dest The destination IP Address
260 */
261 void Send(Ptr<Packet> p, Ipv4Address src, Ipv4Address dest);
262
263 /**
264 * \brief Allow a lower layer to send data to Click. (From Ipv4ExtRouting)
265 * \param p The packet to be sent
266 * \param receiverAddr Receiving interface's address
267 * \param dest The Destination MAC address
268 */
269 void Receive(Ptr<Packet> p, Mac48Address receiverAddr, Mac48Address dest);
270
271 // From Ipv4RoutingProtocol
273 const Ipv4Header& header,
274 Ptr<NetDevice> oif,
275 Socket::SocketErrno& sockerr) override;
277 const Ipv4Header& header,
279 const UnicastForwardCallback& ucb,
280 const MulticastForwardCallback& mcb,
281 const LocalDeliverCallback& lcb,
282 const ErrorCallback& ecb) override;
284 Time::Unit unit = Time::S) const override;
285 void NotifyInterfaceUp(uint32_t interface) override;
286 void NotifyInterfaceDown(uint32_t interface) override;
287 void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
288 void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
289
290 private:
291 std::string m_clickFile; //!< Name of .click configuration file
292 std::map<std::string, std::string> m_defines; //!< Defines for .click configuration file parsing
293 std::string m_nodeName; //!< Name of the node
294 std::string m_clickRoutingTableElement; //!< Name of the routing table element
295
296 bool m_clickInitialised; //!< Whether click has been initialized
297 bool m_nonDefaultName; //!< Whether a non-default name has been set
298
299 Ptr<Ipv4> m_ipv4; //!< Pointer to the IPv4 object
300 Ptr<UniformRandomVariable> m_random; //!< Uniform random variable
301};
302
303} // namespace ns3
304
305#endif /* IPV4_CLICK_ROUTING_H */
Click interface ID from name test.
Click IP MAC address from name test.
Click trivial test.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Class to allow a node to use Click for external routing.
void SetDefines(std::map< std::string, std::string > defines)
Click defines to be used by the node's Click Instance.
bool RouteInput(Ptr< const Packet > p, const Ipv4Header &header, Ptr< const NetDevice > idev, const UnicastForwardCallback &ucb, const MulticastForwardCallback &mcb, const LocalDeliverCallback &lcb, const ErrorCallback &ecb) override
Route an input packet (to be forwarded or locally delivered)
std::string GetIpAddressFromInterfaceId(int ifid)
Provides for SIMCLICK_IPADDR_FROM_NAME.
void RunClickEvent()
This method has to be scheduled every time Click calls SIMCLICK_SCHEDULE.
Ipv4ClickRouting()
Constructor.
simclick_node_t * m_simNode
Pointer to the simclick node.
bool IsInterfaceReady(int ifid)
Provides for SIMCLICK_IF_READY.
int WriteHandler(std::string elementName, std::string handlerName, std::string writeString)
Write Handler interface for a node's Click Elements.
void AddSimNodeToClickMapping()
Used internally in DoInitialize () to Add a mapping to m_clickInstanceFromSimNode mapping.
bool m_clickInitialised
Whether click has been initialized.
void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override
void SetPromisc(int ifid)
Sets an interface to run on promiscuous mode.
void DoDispose() override
Destructor implementation.
std::string m_nodeName
Name of the node.
void DoInitialize() override
Initialize() implementation.
std::string GetMacAddressFromInterfaceId(int ifid)
Provides for SIMCLICK_MACADDR_FROM_NAME.
void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override
bool m_nonDefaultName
Whether a non-default name has been set.
void Send(Ptr< Packet > p, Ipv4Address src, Ipv4Address dest)
Allow a higher layer to send data through Click.
Ptr< Ipv4 > m_ipv4
Pointer to the IPv4 object.
void NotifyInterfaceDown(uint32_t interface) override
struct timeval GetTimevalFromNow() const
Get current simulation time as a timeval.
std::map< std::string, std::string > GetDefines()
Provides for SIMCLICK_GET_DEFINES.
Ptr< Ipv4Route > RouteOutput(Ptr< Packet > p, const Ipv4Header &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr) override
Query routing cache for an existing route, for an outbound packet.
void SetClickFile(std::string clickfile)
Click configuration file to be used by the node's Click Instance.
Ptr< UniformRandomVariable > m_random
Uniform random variable.
int GetInterfaceId(const char *ifname)
Provides for SIMCLICK_IFID_FROM_NAME.
void SetIpv4(Ptr< Ipv4 > ipv4) override
Set the Ipv4 instance to be used.
std::map< std::string, std::string > m_defines
Defines for .click configuration file parsing.
void SendPacketToClick(int ifid, int type, const unsigned char *data, int len)
Sends a packet to Click.
static Ptr< Ipv4ClickRouting > GetClickInstanceFromSimNode(simclick_node_t *simnode)
Allows the Click service methods, which reside outside Ipv4ClickRouting, to get the required Ipv4Clic...
void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const override
Print the Routing Table entries.
std::string m_clickFile
Name of .click configuration file.
std::string ReadHandler(std::string elementName, std::string handlerName)
Read Handler interface for a node's Click Elements.
static TypeId GetTypeId()
Get type ID.
void HandleScheduleFromClick(const struct timeval *when)
Schedules simclick_click_run to run at the given time.
static std::map< simclick_node_t *, Ptr< Ipv4ClickRouting > > m_clickInstanceFromSimNode
Provide a mapping between the node reference used by Click and the corresponding Ipv4ClickRouting ins...
std::string GetIpPrefixFromInterfaceId(int ifid)
Provides for SIMCLICK_IPPREFIX_FROM_NAME.
void HandlePacketFromClick(int ifid, int type, const unsigned char *data, int len)
Receives a packet from Click.
void SetClickRoutingTableElement(std::string name)
Name of the routing table element being used by Click.
void SetNodeName(std::string name)
Name of the node as to be used by Click.
Ptr< UniformRandomVariable > GetRandomVariable()
Get the uniform random variable.
void Receive(Ptr< Packet > p, Mac48Address receiverAddr, Mac48Address dest)
Allow a lower layer to send data to Click.
std::string GetNodeName()
Provides for SIMCLICK_GET_NODE_NAME.
void NotifyInterfaceUp(uint32_t interface) override
std::string m_clickRoutingTableElement
Name of the routing table element.
Packet header for IPv4.
Definition: ipv4-header.h:34
a class to store IPv4 address information on an interface
Abstract base class for IPv4 routing protocols.
an EUI-48 address
Definition: mac48-address.h:46
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:84
Unit
The unit to use to interpret a number representing time.
Definition: nstime.h:111
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
struct simclick_node simclick_node_t
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]