A Discrete-Event Network Simulator
API
nix-vector-routing.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 The Georgia Institute of Technology
3  * Copyright (c) 2021 NITK Surathkal
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This file is adapted from the old ipv4-nix-vector-routing.h.
19  *
20  * Authors: Josh Pelkey <jpelkey@gatech.edu>
21  *
22  * Modified by: Ameya Deshpande <ameyanrd@outlook.com>
23  */
24 
25 #ifndef NIX_VECTOR_ROUTING_H
26 #define NIX_VECTOR_ROUTING_H
27 
28 #include "ns3/bridge-net-device.h"
29 #include "ns3/channel.h"
30 #include "ns3/ipv4-interface.h"
31 #include "ns3/ipv4-l3-protocol.h"
32 #include "ns3/ipv4-route.h"
33 #include "ns3/ipv4-routing-protocol.h"
34 #include "ns3/ipv6-interface.h"
35 #include "ns3/ipv6-l3-protocol.h"
36 #include "ns3/ipv6-route.h"
37 #include "ns3/ipv6-routing-protocol.h"
38 #include "ns3/net-device-container.h"
39 #include "ns3/nix-vector.h"
40 #include "ns3/node-container.h"
41 #include "ns3/node-list.h"
42 #include "ns3/nstime.h"
43 
44 #include <map>
45 #include <unordered_map>
46 
47 // NOLINTBEGIN(modernize-use-override)
48 
49 namespace ns3
50 {
51 
68 template <typename T>
69 class NixVectorRouting : public std::enable_if_t<std::is_same_v<Ipv4RoutingProtocol, T> ||
70  std::is_same_v<Ipv6RoutingProtocol, T>,
71  T>
72 {
74  static constexpr bool IsIpv4 = std::is_same_v<Ipv4RoutingProtocol, T>;
75 
77  using Ip = typename std::conditional_t<IsIpv4, Ipv4, Ipv6>;
78 
80  using IpAddress = typename std::conditional_t<IsIpv4, Ipv4Address, Ipv6Address>;
81 
83  using IpRoute = typename std::conditional_t<IsIpv4, Ipv4Route, Ipv6Route>;
84 
86  using IpAddressHash = typename std::conditional_t<IsIpv4, Ipv4AddressHash, Ipv6AddressHash>;
87 
89  using IpHeader = typename std::conditional_t<IsIpv4, Ipv4Header, Ipv6Header>;
90 
93  typename std::conditional_t<IsIpv4, Ipv4InterfaceAddress, Ipv6InterfaceAddress>;
94 
96  using IpInterface = typename std::conditional_t<IsIpv4, Ipv4Interface, Ipv6Interface>;
97 
99  using IpL3Protocol = typename std::conditional_t<IsIpv4, Ipv4L3Protocol, Ipv6L3Protocol>;
100 
101  public:
109  static TypeId GetTypeId();
116  void SetNode(Ptr<Node> node);
117 
128  void FlushGlobalNixRoutingCache() const;
129 
140  void PrintRoutingPath(Ptr<Node> source,
141  IpAddress dest,
143  Time::Unit unit) const;
144 
145  private:
150  void FlushNixCache() const;
151 
156  void FlushIpRouteCache() const;
157 
164 
176 
183  Ptr<NixVector> GetNixVectorInCache(const IpAddress& address, bool& foundInCache) const;
184 
191 
199  void GetAdjacentNetDevices(Ptr<NetDevice> netDevice,
201  NetDeviceContainer& netDeviceContainer) const;
202 
209  Ptr<Node> GetNodeByIp(IpAddress dest) const;
210 
218 
227  bool BuildNixVector(const std::vector<Ptr<Node>>& parentVector,
228  uint32_t source,
229  uint32_t dest,
230  Ptr<NixVector> nixVector) const;
231 
238  uint32_t FindTotalNeighbors(Ptr<Node> node) const;
239 
246 
255  uint32_t FindNetDeviceForNixIndex(Ptr<Node> node,
256  uint32_t nodeIndex,
257  IpAddress& gatewayIp) const;
258 
268  bool BFS(uint32_t numberOfNodes,
269  Ptr<Node> source,
270  Ptr<Node> dest,
271  std::vector<Ptr<Node>>& parentVector,
272  Ptr<NetDevice> oif) const;
273 
278  void DoInitialize();
279 
284  void DoDispose();
285 
287  typedef std::map<IpAddress, Ptr<NixVector>> NixMap_t;
289  typedef std::map<IpAddress, Ptr<IpRoute>> IpRouteMap_t;
290 
294 
298 
300  typedef typename std::conditional_t<IsIpv4, UnicastForwardCallbackv4, UnicastForwardCallbackv6>
302 
306 
308  typedef Callback<void,
312  const IpHeader&>
314 
316  typedef
317  typename std::conditional_t<IsIpv4, MulticastForwardCallbackv4, MulticastForwardCallbackv6>
319 
322 
325 
326  /* From Ipv4RoutingProtocol and Ipv6RoutingProtocol */
342  const IpHeader& header,
343  Ptr<NetDevice> oif,
344  Socket::SocketErrno& sockerr);
345 
365  virtual bool RouteInput(Ptr<const Packet> p,
366  const IpHeader& header,
368  const UnicastForwardCallback& ucb,
369  const MulticastForwardCallback& mcb,
370  const LocalDeliverCallback& lcb,
371  const ErrorCallback& ecb);
372 
379  virtual void NotifyInterfaceUp(uint32_t interface);
380 
387  virtual void NotifyInterfaceDown(uint32_t interface);
388 
396  virtual void NotifyAddAddress(uint32_t interface, IpInterfaceAddress address);
397 
405  virtual void NotifyRemoveAddress(uint32_t interface, IpInterfaceAddress address);
406 
416  virtual void PrintRoutingTable(Ptr<OutputStreamWrapper> stream,
417  Time::Unit unit = Time::S) const;
418 
419  /* From IPv4RoutingProtocol */
427  virtual void SetIpv4(Ptr<Ip> ipv4);
428 
429  /* From IPv6RoutingProtocol */
437  virtual void SetIpv6(Ptr<Ip> ipv6);
438 
450  virtual void NotifyAddRoute(IpAddress dst,
451  Ipv6Prefix mask,
452  IpAddress nextHop,
453  uint32_t interface,
454  IpAddress prefixToUse = IpAddress::GetZero());
455 
467  virtual void NotifyRemoveRoute(IpAddress dst,
468  Ipv6Prefix mask,
469  IpAddress nextHop,
470  uint32_t interface,
471  IpAddress prefixToUse = IpAddress::GetZero());
472 
476  void CheckCacheStateAndFlush() const;
477 
481  void BuildIpAddressToNodeMap() const;
482 
487  static bool g_isCacheDirty;
488 
492  static uint32_t g_epoch;
493 
496 
499 
502 
505 
513  typedef std::unordered_map<IpAddress, ns3::Ptr<ns3::Node>, IpAddressHash> IpAddressToNodeMap;
515 
517  typedef std::unordered_map<Ptr<NetDevice>, Ptr<IpInterface>> NetDeviceToIpInterfaceMap;
520 };
521 
529 
535 } // namespace ns3
536 
537 // NOLINTEND(modernize-use-override)
538 
539 #endif /* NIX_VECTOR_ROUTING_H */
Callback template class.
Definition: callback.h:438
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
holds a vector of ns3::NetDevice pointers
Nix-vector routing protocol.
Callback< void, Ptr< IpRoute >, Ptr< const Packet >, const IpHeader & > UnicastForwardCallbackv4
Callback for IPv4 unicast packets to be forwarded.
std::map< IpAddress, Ptr< IpRoute > > IpRouteMap_t
Map of IpAddress to IpRoute.
void PrintRoutingPath(Ptr< Node > source, IpAddress dest, Ptr< OutputStreamWrapper > stream, Time::Unit unit) const
Print the Routing Path according to Nix Routing.
void BuildIpAddressToNodeMap() const
Build map from IP Address to Node for faster lookup.
Ptr< IpRoute > GetIpRouteInCache(IpAddress address)
Checks the cache based on dest IP for the IpRoute.
virtual void NotifyRemoveRoute(IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse=IpAddress::GetZero())
Notify route removing.
virtual void NotifyAddRoute(IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse=IpAddress::GetZero())
Notify a new route.
typename std::conditional_t< IsIpv4, Ipv4InterfaceAddress, Ipv6InterfaceAddress > IpInterfaceAddress
Alias for Ipv4InterfaceAddress and Ipv6InterfaceAddress classes.
static uint32_t g_epoch
Nix Epoch, incremented each time a flush is performed.
virtual void NotifyInterfaceUp(uint32_t interface)
void GetAdjacentNetDevices(Ptr< NetDevice > netDevice, Ptr< Channel > channel, NetDeviceContainer &netDeviceContainer) const
Given a net-device returns all the adjacent net-devices, essentially getting the neighbors on that ch...
std::conditional_t< IsIpv4, UnicastForwardCallbackv4, UnicastForwardCallbackv6 > UnicastForwardCallback
Callback for unicast packets to be forwarded.
typename std::conditional_t< IsIpv4, Ipv4AddressHash, Ipv6AddressHash > IpAddressHash
Alias for Ipv4AddressHash and Ipv6AddressHash classes.
std::map< IpAddress, Ptr< NixVector > > NixMap_t
Map of IpAddress to NixVector.
NixMap_t m_nixCache
Cache stores nix-vectors based on destination ip.
typename std::conditional_t< IsIpv4, Ipv4Route, Ipv6Route > IpRoute
Alias for Ipv4Route and Ipv6Route classes.
virtual void NotifyInterfaceDown(uint32_t interface)
typename std::conditional_t< IsIpv4, Ipv4Address, Ipv6Address > IpAddress
Alias for Ipv4Address and Ipv6Address classes.
static IpAddressToNodeMap g_ipAddressToNodeMap
Address to node map.
void FlushGlobalNixRoutingCache() const
Called when run-time link topology change occurs which iterates through the node list and flushes any...
Callback< void, Ptr< Ipv4MulticastRoute >, Ptr< const Packet >, const IpHeader & > MulticastForwardCallbackv4
Callback for IPv4 multicast packets to be forwarded.
virtual void SetIpv6(Ptr< Ip > ipv6)
Typically, invoked directly or indirectly from ns3::Ipv6::SetRoutingProtocol.
Callback< void, Ptr< const Packet >, const IpHeader &, Socket::SocketErrno > ErrorCallback
Callback for routing errors (e.g., no route found)
uint32_t m_totalNeighbors
Total neighbors used for nix-vector to determine number of bits.
virtual Ptr< IpRoute > RouteOutput(Ptr< Packet > p, const IpHeader &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr)
Query routing cache for an existing route, for an outbound packet.
virtual void SetIpv4(Ptr< Ip > ipv4)
Typically, invoked directly or indirectly from ns3::Ipv4::SetRoutingProtocol.
Ptr< IpInterface > GetInterfaceByNetDevice(Ptr< NetDevice > netDevice) const
Iterates through the node list and finds the one corresponding to the given IpAddress.
uint32_t FindTotalNeighbors(Ptr< Node > node) const
Simply iterates through the nodes net-devices and determines how many neighbors the node has.
virtual void NotifyAddAddress(uint32_t interface, IpInterfaceAddress address)
std::conditional_t< IsIpv4, MulticastForwardCallbackv4, MulticastForwardCallbackv6 > MulticastForwardCallback
Callback for multicast packets to be forwarded.
void FlushIpRouteCache() const
Flushes the cache which stores the Ip route based on the destination IP.
virtual bool RouteInput(Ptr< const Packet > p, const IpHeader &header, Ptr< const NetDevice > idev, const UnicastForwardCallback &ucb, const MulticastForwardCallback &mcb, const LocalDeliverCallback &lcb, const ErrorCallback &ecb)
Route an input packet (to be forwarded or locally delivered)
Ptr< Ip > m_ip
IP object.
Ptr< NixVector > GetNixVector(Ptr< Node > source, IpAddress dest, Ptr< NetDevice > oif) const
Takes in the source node and dest IP and calls GetNodeByIp, BFS, accounting for any output interface ...
typename std::conditional_t< IsIpv4, Ipv4Interface, Ipv6Interface > IpInterface
Alias for Ipv4Interface and Ipv6Interface classes.
typename std::conditional_t< IsIpv4, Ipv4, Ipv6 > Ip
Alias for Ipv4 and Ipv6 classes.
typename std::conditional_t< IsIpv4, Ipv4Header, Ipv6Header > IpHeader
Alias for Ipv4Header and Ipv6Header classes.
Callback< void, Ptr< const NetDevice >, Ptr< Ipv6MulticastRoute >, Ptr< const Packet >, const IpHeader & > MulticastForwardCallbackv6
Callback for IPv6 multicast packets to be forwarded.
Ptr< NixVector > GetNixVectorInCache(const IpAddress &address, bool &foundInCache) const
Checks the cache based on dest IP for the nix-vector.
uint32_t FindNetDeviceForNixIndex(Ptr< Node > node, uint32_t nodeIndex, IpAddress &gatewayIp) const
Nix index is with respect to the neighbors.
static NetDeviceToIpInterfaceMap g_netdeviceToIpInterfaceMap
NetDevice pointer to IpInterface pointer map.
void SetNode(Ptr< Node > node)
Set the Node pointer of the node for which this routing protocol is to be placed.
virtual void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print the Routing Table entries.
typename std::conditional_t< IsIpv4, Ipv4L3Protocol, Ipv6L3Protocol > IpL3Protocol
Alias for Ipv4L3Protocol and Ipv4L3Protocol classes.
void ResetTotalNeighbors()
Upon a run-time topology change caches are flushed and the total number of neighbors is reset to zero...
virtual void NotifyRemoveAddress(uint32_t interface, IpInterfaceAddress address)
Callback< void, Ptr< const Packet >, const IpHeader &, uint32_t > LocalDeliverCallback
Callback for packets to be locally delivered.
static TypeId GetTypeId()
The Interface ID of the Global Router interface.
static bool g_isCacheDirty
Flag to mark when caches are dirty and need to be flushed.
Ptr< Node > m_node
Node object.
void CheckCacheStateAndFlush() const
Flushes routing caches if required.
Callback< void, Ptr< const NetDevice >, Ptr< IpRoute >, Ptr< const Packet >, const IpHeader & > UnicastForwardCallbackv6
Callback for IPv6 unicast packets to be forwarded.
std::unordered_map< IpAddress, ns3::Ptr< ns3::Node >, IpAddressHash > IpAddressToNodeMap
Mapping of IP address to ns-3 node.
std::unordered_map< Ptr< NetDevice >, Ptr< IpInterface > > NetDeviceToIpInterfaceMap
Mapping of Ptr<NetDevice> to Ptr<IpInterface>.
Ptr< BridgeNetDevice > NetDeviceIsBridged(Ptr< NetDevice > nd) const
Determine if the NetDevice is bridged.
bool BFS(uint32_t numberOfNodes, Ptr< Node > source, Ptr< Node > dest, std::vector< Ptr< Node >> &parentVector, Ptr< NetDevice > oif) const
Breadth first search algorithm.
static constexpr bool IsIpv4
Alias for determining whether the parent is Ipv4RoutingProtocol or Ipv6RoutingProtocol.
IpRouteMap_t m_ipRouteCache
Cache stores IpRoutes based on destination ip.
void FlushNixCache() const
Flushes the cache which stores nix-vector based on destination IP.
bool BuildNixVector(const std::vector< Ptr< Node >> &parentVector, uint32_t source, uint32_t dest, Ptr< NixVector > nixVector) const
Recurses the T vector, created by BFS and actually builds the nixvector.
Ptr< Node > GetNodeByIp(IpAddress dest) const
Iterates through the node list and finds the one corresponding to the given IpAddress.
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
NixVectorRouting< Ipv6RoutingProtocol > Ipv6NixVectorRouting
Create the typedef Ipv6NixVectorRouting with T as Ipv6RoutingProtocol.
NixVectorRouting< Ipv4RoutingProtocol > Ipv4NixVectorRouting
Create the typedef Ipv4NixVectorRouting with T as Ipv4RoutingProtocol.
address
Definition: first.py:47
typename std::enable_if< B, T >::type enable_if_t
Definition: json.h:2746
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:88