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