A Discrete-Event Network Simulator
API
nix-vector-routing.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 The Georgia Institute of Technology
4  * Copyright (c) 2021 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * This file is adapted from the old ipv4-nix-vector-routing.h.
20  *
21  * Authors: Josh Pelkey <jpelkey@gatech.edu>
22  *
23  * Modified by: Ameya Deshpande <ameyanrd@outlook.com>
24  */
25 
26 #ifndef NIX_VECTOR_ROUTING_H
27 #define NIX_VECTOR_ROUTING_H
28 
29 #include "ns3/channel.h"
30 #include "ns3/node-container.h"
31 #include "ns3/node-list.h"
32 #include "ns3/net-device-container.h"
33 #include "ns3/ipv4-routing-protocol.h"
34 #include "ns3/ipv4-route.h"
35 #include "ns3/ipv6-route.h"
36 #include "ns3/ipv6-routing-protocol.h"
37 #include "ns3/nix-vector.h"
38 #include "ns3/bridge-net-device.h"
39 #include "ns3/nstime.h"
40 #include "ns3/ipv4-interface.h"
41 #include "ns3/ipv6-interface.h"
42 #include "ns3/ipv4-l3-protocol.h"
43 #include "ns3/ipv6-l3-protocol.h"
44 
45 #include <map>
46 #include <unordered_map>
47 
48 namespace ns3 {
49 
66 template <typename T>
67 class NixVectorRouting : public std::enable_if<std::is_same<Ipv4RoutingProtocol, T>::value || std::is_same<Ipv6RoutingProtocol, T>::value, T>::type
68 {
70  using IsIpv4 = std::is_same <Ipv4RoutingProtocol, T>;
71 
73  using Ip = typename std::conditional <IsIpv4::value, Ipv4, Ipv6>::type;
74 
76  using IpAddress = typename std::conditional<IsIpv4::value, Ipv4Address, Ipv6Address>::type;
77 
79  using IpRoute = typename std::conditional<IsIpv4::value, Ipv4Route, Ipv6Route>::type;
80 
82  using IpAddressHash = typename std::conditional<IsIpv4::value, Ipv4AddressHash, Ipv6AddressHash>::type;
83 
85  using IpHeader = typename std::conditional<IsIpv4::value, Ipv4Header, Ipv6Header>::type;
86 
88  using IpInterfaceAddress = typename std::conditional<IsIpv4::value, Ipv4InterfaceAddress, Ipv6InterfaceAddress>::type;
89 
91  using IpInterface = typename std::conditional<IsIpv4::value, Ipv4Interface, Ipv6Interface>::type;
92 
94  using IpL3Protocol = typename std::conditional<IsIpv4::value, Ipv4L3Protocol, Ipv6L3Protocol>::type;
95 
96 public:
104  static TypeId GetTypeId (void);
111  void SetNode (Ptr<Node> node);
112 
123  void FlushGlobalNixRoutingCache (void) const;
124 
135  void PrintRoutingPath (Ptr<Node> source, IpAddress dest, Ptr<OutputStreamWrapper> stream, Time::Unit unit) const;
136 
137 
138 private:
139 
144  void FlushNixCache (void) const;
145 
150  void FlushIpRouteCache (void) const;
151 
157  void ResetTotalNeighbors (void);
158 
170 
177  Ptr<NixVector> GetNixVectorInCache (const IpAddress &address, bool &foundInCache) const;
178 
185 
193  void GetAdjacentNetDevices (Ptr<NetDevice> netDevice, Ptr<Channel> channel, NetDeviceContainer & netDeviceContainer) const;
194 
201  Ptr<Node> GetNodeByIp (IpAddress dest) const;
202 
210 
219  bool BuildNixVector (const std::vector< Ptr<Node> > & parentVector, uint32_t source, uint32_t dest, Ptr<NixVector> nixVector) const;
220 
226  bool BuildNixVectorLocal (Ptr<NixVector> nixVector);
227 
234  uint32_t FindTotalNeighbors (Ptr<Node> node) const;
235 
236 
243 
244 
253  uint32_t FindNetDeviceForNixIndex (Ptr<Node> node, uint32_t nodeIndex, IpAddress & gatewayIp) const;
254 
264  bool BFS (uint32_t numberOfNodes,
265  Ptr<Node> source,
266  Ptr<Node> dest,
267  std::vector< Ptr<Node> > & parentVector,
268  Ptr<NetDevice> oif) const;
269 
274  void DoInitialize ();
275 
280  void DoDispose (void);
281 
283  typedef std::map<IpAddress, Ptr<NixVector> > NixMap_t;
285  typedef std::map<IpAddress, Ptr<IpRoute> > IpRouteMap_t;
286 
289 
292 
294  typedef typename std::conditional<IsIpv4::value, UnicastForwardCallbackv4, UnicastForwardCallbackv6>::type UnicastForwardCallback;
295 
298 
301 
303  typedef typename std::conditional<IsIpv4::value, MulticastForwardCallbackv4, MulticastForwardCallbackv6>::type MulticastForwardCallback;
304 
307 
310 
311  /* From Ipv4RoutingProtocol and Ipv6RoutingProtocol */
326  virtual Ptr<IpRoute> RouteOutput (Ptr<Packet> p, const IpHeader &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
327 
347  virtual bool RouteInput (Ptr<const Packet> p, const IpHeader &header, Ptr<const NetDevice> idev,
350 
357  virtual void NotifyInterfaceUp (uint32_t interface);
358 
365  virtual void NotifyInterfaceDown (uint32_t interface);
366 
374  virtual void NotifyAddAddress (uint32_t interface, IpInterfaceAddress address);
375 
383  virtual void NotifyRemoveAddress (uint32_t interface, IpInterfaceAddress address);
384 
394  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit = Time::S) const;
395 
396  /* From IPv4RoutingProtocol */
404  virtual void SetIpv4 (Ptr<Ip> ipv4);
405 
406  /* From IPv6RoutingProtocol */
414  virtual void SetIpv6 (Ptr<Ip> ipv6);
415 
427  virtual void NotifyAddRoute (IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse = IpAddress::GetZero ());
428 
440  virtual void NotifyRemoveRoute (IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse = IpAddress::GetZero ());
441 
445  void CheckCacheStateAndFlush (void) const;
446 
450  void BuildIpAddressToNodeMap (void) const;
451 
456  static bool g_isCacheDirty;
457 
460 
463 
466 
469 
470 
478  typedef std::unordered_map<IpAddress, ns3::Ptr<ns3::Node>, IpAddressHash > IpAddressToNodeMap;
480 
482  typedef std::unordered_map<Ptr<NetDevice>, Ptr<IpInterface>> NetDeviceToIpInterfaceMap;
484 };
485 
486 
494 
500 } // namespace ns3
501 
502 #endif /* NIX_VECTOR_ROUTING_H */
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::NixVectorRouting::ErrorCallback
Callback< void, Ptr< const Packet >, const IpHeader &, Socket::SocketErrno > ErrorCallback
Callback for routing errors (e.g., no route found)
Definition: nix-vector-routing.h:309
ns3::NixVectorRouting::FindNetDeviceForNixIndex
uint32_t FindNetDeviceForNixIndex(Ptr< Node > node, uint32_t nodeIndex, IpAddress &gatewayIp) const
Nix index is with respect to the neighbors.
Definition: nix-vector-routing.cc:653
ns3::NixVectorRouting::NotifyAddRoute
virtual void NotifyAddRoute(IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse=IpAddress::GetZero())
Notify a new route.
Definition: nix-vector-routing.cc:1054
ns3::NixVectorRouting::ResetTotalNeighbors
void ResetTotalNeighbors(void)
Upon a run-time topology change caches are flushed and the total number of neighbors is reset to zero...
ns3::NixVectorRouting::g_ipAddressToNodeMap
static IpAddressToNodeMap g_ipAddressToNodeMap
Address to node map.
Definition: nix-vector-routing.h:479
ns3::NixVectorRouting::~NixVectorRouting
~NixVectorRouting()
Definition: nix-vector-routing.cc:75
ns3::NixVectorRouting::NixMap_t
std::map< IpAddress, Ptr< NixVector > > NixMap_t
Map of IpAddress to NixVector.
Definition: nix-vector-routing.h:283
ns3::NixVectorRouting::GetInterfaceByNetDevice
Ptr< IpInterface > GetInterfaceByNetDevice(Ptr< NetDevice > netDevice) const
Iterates through the node list and finds the one corresponding to the given IpAddress.
Definition: nix-vector-routing.cc:551
ns3::NixVectorRouting::m_ipRouteCache
IpRouteMap_t m_ipRouteCache
Cache stores IpRoutes based on destination ip.
Definition: nix-vector-routing.h:462
ns3::Socket::SocketErrno
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
ns3::Callback
Callback template class.
Definition: callback.h:1279
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::NixVectorRouting::MulticastForwardCallbackv6
Callback< void, Ptr< const NetDevice >, Ptr< Ipv6MulticastRoute >, Ptr< const Packet >, const IpHeader & > MulticastForwardCallbackv6
Callback for IPv6 multicast packets to be forwarded.
Definition: nix-vector-routing.h:300
ns3::NixVectorRouting::m_totalNeighbors
uint32_t m_totalNeighbors
Total neighbors used for nix-vector to determine number of bits.
Definition: nix-vector-routing.h:468
ns3::NixVectorRouting::GetNodeByIp
Ptr< Node > GetNodeByIp(IpAddress dest) const
Iterates through the node list and finds the one corresponding to the given IpAddress.
Definition: nix-vector-routing.cc:522
third.channel
channel
Definition: third.py:92
ns3::NixVectorRouting::SetNode
void SetNode(Ptr< Node > node)
Set the Node pointer of the node for which this routing protocol is to be placed.
Definition: nix-vector-routing.cc:130
ns3::NixVectorRouting::IpAddressHash
typename std::conditional< IsIpv4::value, Ipv4AddressHash, Ipv6AddressHash >::type IpAddressHash
Alias for Ipv4AddressHash and Ipv6AddressHash classes.
Definition: nix-vector-routing.h:82
ns3::NixVectorRouting::GetNixVectorInCache
Ptr< NixVector > GetNixVectorInCache(const IpAddress &address, bool &foundInCache) const
Checks the cache based on dest IP for the nix-vector.
Definition: nix-vector-routing.cc:234
ns3::NixVectorRouting::NotifyAddAddress
virtual void NotifyAddAddress(uint32_t interface, IpInterfaceAddress address)
Definition: nix-vector-routing.cc:1042
ns3::NixVectorRouting::IpL3Protocol
typename std::conditional< IsIpv4::value, Ipv4L3Protocol, Ipv6L3Protocol >::type IpL3Protocol
Alias for Ipv4L3Protocol and Ipv4L3Protocol classes.
Definition: nix-vector-routing.h:94
ns3::NixVectorRouting::NotifyRemoveAddress
virtual void NotifyRemoveAddress(uint32_t interface, IpInterfaceAddress address)
Definition: nix-vector-routing.cc:1048
ns3::NixVectorRouting::SetIpv4
virtual void SetIpv4(Ptr< Ip > ipv4)
Typically, invoked directly or indirectly from ns3::Ipv4::SetRoutingProtocol.
Definition: nix-vector-routing.cc:82
ns3::NixVectorRouting::CheckCacheStateAndFlush
void CheckCacheStateAndFlush(void) const
Flushes routing caches if required.
Definition: nix-vector-routing.cc:1358
ns3::NixVectorRouting::FlushGlobalNixRoutingCache
void FlushGlobalNixRoutingCache(void) const
Called when run-time link topology change occurs which iterates through the node list and flushes any...
Definition: nix-vector-routing.cc:139
ns3::NixVectorRouting::FlushIpRouteCache
void FlushIpRouteCache(void) const
Flushes the cache which stores the Ip route based on the destination IP.
Definition: nix-vector-routing.cc:173
ns3::NixVectorRouting::g_netdeviceToIpInterfaceMap
static NetDeviceToIpInterfaceMap g_netdeviceToIpInterfaceMap
NetDevice pointer to IpInterface pointer map.
Definition: nix-vector-routing.h:483
ns3::NixVectorRouting::IpInterface
typename std::conditional< IsIpv4::value, Ipv4Interface, Ipv6Interface >::type IpInterface
Alias for Ipv4Interface and Ipv6Interface classes.
Definition: nix-vector-routing.h:91
ns3::NixVectorRouting::NotifyRemoveRoute
virtual void NotifyRemoveRoute(IpAddress dst, Ipv6Prefix mask, IpAddress nextHop, uint32_t interface, IpAddress prefixToUse=IpAddress::GetZero())
Notify route removing.
Definition: nix-vector-routing.cc:1060
ns3::Ptr< Node >
ns3::NixVectorRouting::RouteInput
virtual bool RouteInput(Ptr< const Packet > p, const IpHeader &header, Ptr< const NetDevice > idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)
Route an input packet (to be forwarded or locally delivered)
Definition: nix-vector-routing.cc:841
ns3::NixVectorRouting::NetDeviceIsBridged
Ptr< BridgeNetDevice > NetDeviceIsBridged(Ptr< NetDevice > nd) const
Determine if the NetDevice is bridged.
Definition: nix-vector-routing.cc:612
ns3::NixVectorRouting::IpAddressToNodeMap
std::unordered_map< IpAddress, ns3::Ptr< ns3::Node >, IpAddressHash > IpAddressToNodeMap
Mapping of IP address to ns-3 node.
Definition: nix-vector-routing.h:478
ns3::NixVectorRouting::UnicastForwardCallback
std::conditional< IsIpv4::value, UnicastForwardCallbackv4, UnicastForwardCallbackv6 >::type UnicastForwardCallback
Callback for unicast packets to be forwarded.
Definition: nix-vector-routing.h:294
ns3::NixVectorRouting::MulticastForwardCallback
std::conditional< IsIpv4::value, MulticastForwardCallbackv4, MulticastForwardCallbackv6 >::type MulticastForwardCallback
Callback for multicast packets to be forwarded.
Definition: nix-vector-routing.h:303
ns3::NixVectorRouting::NetDeviceToIpInterfaceMap
std::unordered_map< Ptr< NetDevice >, Ptr< IpInterface > > NetDeviceToIpInterfaceMap
Mapping of Ptr<NetDevice> to Ptr<IpInterface>.
Definition: nix-vector-routing.h:482
first.address
address
Definition: first.py:44
ns3::NixVectorRouting::IsIpv4
std::is_same< Ipv4RoutingProtocol, T > IsIpv4
Alias for determining whether the parent is Ipv4RoutingProtocol or Ipv6RoutingProtocol.
Definition: nix-vector-routing.h:70
ns3::NixVectorRouting::Ip
typename std::conditional< IsIpv4::value, Ipv4, Ipv6 >::type Ip
Alias for Ipv4 and Ipv6 classes.
Definition: nix-vector-routing.h:73
ns3::NixVectorRouting
Nix-vector routing protocol.
Definition: nix-vector-routing.h:68
ns3::NixVectorRouting::RouteOutput
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.
Definition: nix-vector-routing.cc:699
ns3::NixVectorRouting::MulticastForwardCallbackv4
Callback< void, Ptr< Ipv4MulticastRoute >, Ptr< const Packet >, const IpHeader & > MulticastForwardCallbackv4
Callback for IPv4 multicast packets to be forwarded.
Definition: nix-vector-routing.h:297
ns3::NixVectorRouting::IpHeader
typename std::conditional< IsIpv4::value, Ipv4Header, Ipv6Header >::type IpHeader
Alias for Ipv4Header and Ipv6Header classes.
Definition: nix-vector-routing.h:85
ns3::NixVectorRouting::BuildIpAddressToNodeMap
void BuildIpAddressToNodeMap(void) const
Build map from IP Address to Node for faster lookup.
Definition: nix-vector-routing.cc:476
ns3::NixVectorRouting::BFS
bool BFS(uint32_t numberOfNodes, Ptr< Node > source, Ptr< Node > dest, std::vector< Ptr< Node > > &parentVector, Ptr< NetDevice > oif) const
Breadth first search algorithm.
Definition: nix-vector-routing.cc:1067
ns3::NixVectorRouting::GetAdjacentNetDevices
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...
Definition: nix-vector-routing.cc:377
ns3::NixVectorRouting::g_isCacheDirty
static bool g_isCacheDirty
Flag to mark when caches are dirty and need to be flushed.
Definition: nix-vector-routing.h:456
ns3::NixVectorRouting::PrintRoutingPath
void PrintRoutingPath(Ptr< Node > source, IpAddress dest, Ptr< OutputStreamWrapper > stream, Time::Unit unit) const
Print the Routing Path according to Nix Routing.
Definition: nix-vector-routing.cc:1226
ns3::NixVectorRouting::FindTotalNeighbors
uint32_t FindTotalNeighbors(Ptr< Node > node) const
Simply iterates through the nodes net-devices and determines how many neighbors the node has.
Definition: nix-vector-routing.cc:578
ns3::NixVectorRouting::GetTypeId
static TypeId GetTypeId(void)
The Interface ID of the Global Router interface.
Definition: nix-vector-routing.cc:55
ns3::NixVectorRouting::m_node
Ptr< Node > m_node
Node object.
Definition: nix-vector-routing.h:465
ns3::NixVectorRouting::IpAddress
typename std::conditional< IsIpv4::value, Ipv4Address, Ipv6Address >::type IpAddress
Alias for Ipv4Address and Ipv6Address classes.
Definition: nix-vector-routing.h:76
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::NixVectorRouting::DoInitialize
void DoInitialize()
Definition: nix-vector-routing.cc:104
ns3::NixVectorRouting::IpRouteMap_t
std::map< IpAddress, Ptr< IpRoute > > IpRouteMap_t
Map of IpAddress to IpRoute.
Definition: nix-vector-routing.h:285
ns3::NixVectorRouting::m_ip
Ptr< Ip > m_ip
IP object.
Definition: nix-vector-routing.h:464
ns3::NixVectorRouting::BuildNixVector
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.
Definition: nix-vector-routing.cc:303
ns3::NixVectorRouting::UnicastForwardCallbackv6
Callback< void, Ptr< const NetDevice >, Ptr< IpRoute >, Ptr< const Packet >, const IpHeader & > UnicastForwardCallbackv6
Callback for IPv6 unicast packets to be forwarded.
Definition: nix-vector-routing.h:291
ns3::Ipv4NixVectorRouting
NixVectorRouting< Ipv4RoutingProtocol > Ipv4NixVectorRouting
Create the typedef Ipv4NixVectorRouting with T as Ipv4RoutingProtocol.
Definition: nix-vector-routing.h:493
ns3::Ipv6NixVectorRouting
NixVectorRouting< Ipv6RoutingProtocol > Ipv6NixVectorRouting
Create the typedef Ipv6NixVectorRouting with T as Ipv6RoutingProtocol.
Definition: nix-vector-routing.h:499
ns3::NixVectorRouting::UnicastForwardCallbackv4
Callback< void, Ptr< IpRoute >, Ptr< const Packet >, const IpHeader & > UnicastForwardCallbackv4
Callback for IPv4 unicast packets to be forwarded.
Definition: nix-vector-routing.h:288
ns3::NixVectorRouting::IpInterfaceAddress
typename std::conditional< IsIpv4::value, Ipv4InterfaceAddress, Ipv6InterfaceAddress >::type IpInterfaceAddress
Alias for Ipv4InterfaceAddress and Ipv6InterfaceAddress classes.
Definition: nix-vector-routing.h:88
ns3::NixVectorRouting::m_nixCache
NixMap_t m_nixCache
Cache stores nix-vectors based on destination ip.
Definition: nix-vector-routing.h:459
ns3::NixVectorRouting::SetIpv6
virtual void SetIpv6(Ptr< Ip > ipv6)
Typically, invoked directly or indirectly from ns3::Ipv6::SetRoutingProtocol.
Definition: nix-vector-routing.cc:93
ns3::NixVectorRouting::PrintRoutingTable
virtual void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print the Routing Table entries.
Definition: nix-vector-routing.cc:960
ns3::NixVectorRouting::DoDispose
void DoDispose(void)
Definition: nix-vector-routing.cc:118
ns3::NixVectorRouting::FlushNixCache
void FlushNixCache(void) const
Flushes the cache which stores nix-vector based on destination IP.
Definition: nix-vector-routing.cc:165
ns3::NixVectorRouting::IpRoute
typename std::conditional< IsIpv4::value, Ipv4Route, Ipv6Route >::type IpRoute
Alias for Ipv4Route and Ipv6Route classes.
Definition: nix-vector-routing.h:79
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::NixVectorRouting::GetNixVector
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 ...
Definition: nix-vector-routing.cc:181
ns3::NixVectorRouting::NotifyInterfaceDown
virtual void NotifyInterfaceDown(uint32_t interface)
Definition: nix-vector-routing.cc:1036
ns3::NixVectorRouting::LocalDeliverCallback
Callback< void, Ptr< const Packet >, const IpHeader &, uint32_t > LocalDeliverCallback
Callback for packets to be locally delivered.
Definition: nix-vector-routing.h:306
ns3::NixVectorRouting::NotifyInterfaceUp
virtual void NotifyInterfaceUp(uint32_t interface)
Definition: nix-vector-routing.cc:1030
ns3::NixVectorRouting::NixVectorRouting
NixVectorRouting()
Definition: nix-vector-routing.cc:68
ns3::Time::Unit
Unit
The unit to use to interpret a number representing time.
Definition: nstime.h:110
ns3::NixVectorRouting::GetIpRouteInCache
Ptr< IpRoute > GetIpRouteInCache(IpAddress address)
Checks the cache based on dest IP for the IpRoute.
Definition: nix-vector-routing.cc:255
ns3::NixVectorRouting::BuildNixVectorLocal
bool BuildNixVectorLocal(Ptr< NixVector > nixVector)
Special variation of BuildNixVector for when a node is sending to itself.
Definition: nix-vector-routing.cc:274