INET Framework for OMNeT++/OMNEST
inet::Ipv4FlatNetworkConfigurator Class Reference

Configures Ipv4 addresses and routing tables for a "flat" network, "flat" meaning that all hosts and routers will have the same network address. More...

#include <Ipv4FlatNetworkConfigurator.h>

Inheritance diagram for inet::Ipv4FlatNetworkConfigurator:

Classes

class  NodeInfo
 

Protected Types

typedef std::vector< NodeInfoNodeInfoVector
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void extractTopology (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void assignAddresses (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void addDefaultRoutes (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void fillRoutingTables (cTopology &topo, NodeInfoVector &nodeInfo)
 
virtual void setDisplayString (cTopology &topo, NodeInfoVector &nodeInfo)
 

Detailed Description

Configures Ipv4 addresses and routing tables for a "flat" network, "flat" meaning that all hosts and routers will have the same network address.

For more info please see the NED file.

Member Typedef Documentation

◆ NodeInfoVector

Member Function Documentation

◆ addDefaultRoutes()

void inet::Ipv4FlatNetworkConfigurator::addDefaultRoutes ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual
103 {
104  // add default route to nodes with exactly one (non-loopback) interface
105  for (int i = 0; i < topo.getNumNodes(); i++) {
106  cTopology::Node *node = topo.getNode(i);
107 
108  // skip bus types
109  if (!nodeInfo[i].isIPNode)
110  continue;
111 
112  IInterfaceTable *ift = nodeInfo[i].ift;
113  IIpv4RoutingTable *rt = nodeInfo[i].rt;
114 
115  // count non-loopback interfaces
116  int numIntf = 0;
117  NetworkInterface *ie = nullptr;
118  for (int k = 0; k < ift->getNumInterfaces(); k++)
119  if (!ift->getInterface(k)->isLoopback()) {
120  ie = ift->getInterface(k);
121  numIntf++;
122  }
123 
124  nodeInfo[i].usesDefaultRoute = (numIntf == 1);
125  if (numIntf != 1)
126  continue; // only deal with nodes with one interface plus loopback
127 
128  EV_INFO << " " << node->getModule()->getFullName() << "=" << nodeInfo[i].address
129  << " has only one (non-loopback) interface, adding default route\n";
130 
131  // add route
132  Ipv4Route *e = new Ipv4Route();
133  e->setDestination(Ipv4Address());
134  e->setNetmask(Ipv4Address());
135  e->setInterface(ie);
136  e->setSourceType(IRoute::MANUAL);
137 // e->getMetric() = 1;
138  rt->addRoute(e);
139  }
140 }

Referenced by initialize().

◆ assignAddresses()

void inet::Ipv4FlatNetworkConfigurator::assignAddresses ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual
72 {
73  // assign Ipv4 addresses
74  uint32_t networkAddress = Ipv4Address(par("networkAddress").stringValue()).getInt();
75  uint32_t netmask = Ipv4Address(par("netmask").stringValue()).getInt();
76  int maxNodes = (~netmask) - 1; // 0 and ffff have special meaning and cannot be used
77  if (topo.getNumNodes() > maxNodes)
78  throw cRuntimeError("netmask too large, not enough addresses for all %d nodes", topo.getNumNodes());
79 
80  int numIPNodes = 0;
81  for (int i = 0; i < topo.getNumNodes(); i++) {
82  // skip bus types
83  if (!nodeInfo[i].isIPNode)
84  continue;
85 
86  uint32_t addr = networkAddress | uint32_t(++numIPNodes);
87  nodeInfo[i].address.set(addr);
88 
89  // find interface table and assign address to all (non-loopback) interfaces
90  IInterfaceTable *ift = nodeInfo[i].ift;
91  for (int k = 0; k < ift->getNumInterfaces(); k++) {
92  NetworkInterface *ie = ift->getInterface(k);
93  if (!ie->isLoopback()) {
94  auto ipv4Data = ie->getProtocolDataForUpdate<Ipv4InterfaceData>();
95  ipv4Data->setIPAddress(Ipv4Address(addr));
96  ipv4Data->setNetmask(Ipv4Address::ALLONES_ADDRESS); // full address must match for local delivery
97  }
98  }
99  }
100 }

Referenced by initialize().

◆ extractTopology()

void inet::Ipv4FlatNetworkConfigurator::extractTopology ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual
52 {
53  // extract topology
54  topo.extractByProperty("networkNode");
55  EV_DEBUG << "cTopology found " << topo.getNumNodes() << " nodes\n";
56 
57  // fill in isIPNode, ift and rt members in nodeInfo[]
58  nodeInfo.resize(topo.getNumNodes());
59  for (int i = 0; i < topo.getNumNodes(); i++) {
60  cModule *mod = topo.getNode(i)->getModule();
61  nodeInfo[i].isIPNode = L3AddressResolver().findIpv4RoutingTableOf(mod) != nullptr && L3AddressResolver().findInterfaceTableOf(mod) != nullptr;
62  if (nodeInfo[i].isIPNode) {
63  nodeInfo[i].ift = L3AddressResolver().interfaceTableOf(mod);
64  nodeInfo[i].rt = L3AddressResolver().getIpv4RoutingTableOf(mod);
65  nodeInfo[i].ipForwardEnabled = mod->hasPar("forwarding") ? mod->par("forwarding") : false;
66  topo.getNode(i)->setWeight(nodeInfo[i].ipForwardEnabled ? 0.0 : INFINITY);
67  }
68  }
69 }

Referenced by initialize().

◆ fillRoutingTables()

void inet::Ipv4FlatNetworkConfigurator::fillRoutingTables ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual
143 {
144  // fill in routing tables with static routes
145  for (int i = 0; i < topo.getNumNodes(); i++) {
146  cTopology::Node *destNode = topo.getNode(i);
147 
148  // skip bus types
149  if (!nodeInfo[i].isIPNode)
150  continue;
151 
152  Ipv4Address destAddr = nodeInfo[i].address;
153  std::string destModName = destNode->getModule()->getFullName();
154 
155  // calculate shortest paths from everywhere towards destNode
156  topo.calculateWeightedSingleShortestPathsTo(destNode);
157 
158  // add route (with host=destNode) to every routing table in the network
159  // (excepting nodes with only one interface -- there we'll set up a default route)
160  for (int j = 0; j < topo.getNumNodes(); j++) {
161  if (i == j)
162  continue;
163  if (!nodeInfo[j].isIPNode)
164  continue;
165 
166  cTopology::Node *atNode = topo.getNode(j);
167  if (atNode->getNumPaths() == 0)
168  continue; // not connected
169  if (nodeInfo[j].usesDefaultRoute)
170  continue; // already added default route here
171 
172  Ipv4Address atAddr = nodeInfo[j].address;
173 
174  IInterfaceTable *ift = nodeInfo[j].ift;
175 
176  int outputGateId = atNode->getPath(0)->getLocalGate()->getId();
177  NetworkInterface *ie = ift->findInterfaceByNodeOutputGateId(outputGateId);
178  if (!ie)
179  throw cRuntimeError("%s has no interface for output gate id %d", ift->getFullPath().c_str(), outputGateId);
180 
181  EV_INFO << " from " << atNode->getModule()->getFullName() << "=" << Ipv4Address(atAddr);
182  EV_INFO << " towards " << destModName << "=" << Ipv4Address(destAddr) << " interface " << ie->getInterfaceName() << endl;
183 
184  // add route
185  IIpv4RoutingTable *rt = nodeInfo[j].rt;
186  Ipv4Route *e = new Ipv4Route();
187  e->setDestination(destAddr);
188  e->setNetmask(Ipv4Address(255, 255, 255, 255)); // full match needed
189  e->setInterface(ie);
190  e->setSourceType(IRoute::MANUAL);
191 // e->getMetric() = 1;
192  rt->addRoute(e);
193  }
194  }
195 }

Referenced by initialize().

◆ handleMessage()

void inet::Ipv4FlatNetworkConfigurator::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
198 {
199  throw cRuntimeError("this module doesn't handle messages, it runs only in initialize()");
200 }

◆ initialize()

void inet::Ipv4FlatNetworkConfigurator::initialize ( int  stage)
overrideprotectedvirtual
25 {
26  cSimpleModule::initialize(stage);
27 
28  if (stage == INITSTAGE_NETWORK_CONFIGURATION) {
29  cTopology topo("topo");
30  NodeInfoVector nodeInfo; // will be of size topo.nodes[]
31 
32  // extract topology into the cTopology object, then fill in
33  // isIPNode, rt and ift members of nodeInfo[]
34  extractTopology(topo, nodeInfo);
35 
36  // assign addresses to Ipv4 nodes, and also store result in nodeInfo[].address
37  assignAddresses(topo, nodeInfo);
38 
39  // add default routes to hosts (nodes with a single attachment);
40  // also remember result in nodeInfo[].usesDefaultRoute
41  addDefaultRoutes(topo, nodeInfo);
42 
43  // calculate shortest paths, and add corresponding static routes
44  fillRoutingTables(topo, nodeInfo);
45 
46  // update display string
47  setDisplayString(topo, nodeInfo);
48  }
49 }

◆ numInitStages()

virtual int inet::Ipv4FlatNetworkConfigurator::numInitStages ( ) const
inlineoverrideprotectedvirtual
40 { return NUM_INIT_STAGES; }

◆ setDisplayString()

void inet::Ipv4FlatNetworkConfigurator::setDisplayString ( cTopology &  topo,
NodeInfoVector nodeInfo 
)
protectedvirtual
203 {
204  int numIPNodes = 0;
205  for (int i = 0; i < topo.getNumNodes(); i++)
206  if (nodeInfo[i].isIPNode)
207  numIPNodes++;
208 
209  // update display string
210  char buf[80];
211  sprintf(buf, "%d Ipv4 nodes\n%d non-Ipv4 nodes", numIPNodes, topo.getNumNodes() - numIPNodes);
212  getDisplayString().setTagArg("t", 0, buf);
213 }

Referenced by initialize().


The documentation for this class was generated from the following files:
inet::math::mod
double mod(double dividend, double divisor)
Returns the rest of a whole-numbered division.
Definition: INETMath.h:96
inet::INITSTAGE_NETWORK_CONFIGURATION
INET_API InitStage INITSTAGE_NETWORK_CONFIGURATION
Initialization of network configuration (e.g.
inet::IRoute::MANUAL
@ MANUAL
manually added static route
Definition: IRoute.h:29
inet::Ipv4FlatNetworkConfigurator::NodeInfoVector
std::vector< NodeInfo > NodeInfoVector
Definition: Ipv4FlatNetworkConfigurator.h:37
inet::units::constants::e
const value< double, units::C > e(1.602176487e-19)
inet::Ipv4FlatNetworkConfigurator::assignAddresses
virtual void assignAddresses(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: Ipv4FlatNetworkConfigurator.cc:71
inet::Ipv4Address::ALLONES_ADDRESS
static const Ipv4Address ALLONES_ADDRESS
255.255.255.255
Definition: Ipv4Address.h:94
INFINITY
#define INFINITY
Definition: Topology.h:20
inet::Ipv4FlatNetworkConfigurator::fillRoutingTables
virtual void fillRoutingTables(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: Ipv4FlatNetworkConfigurator.cc:142
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::physicallayer::k
const double k
Definition: Qam1024Modulation.cc:14
inet::Ipv4FlatNetworkConfigurator::extractTopology
virtual void extractTopology(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: Ipv4FlatNetworkConfigurator.cc:51
inet::Ipv4FlatNetworkConfigurator::addDefaultRoutes
virtual void addDefaultRoutes(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: Ipv4FlatNetworkConfigurator.cc:102
inet::Ipv4FlatNetworkConfigurator::setDisplayString
virtual void setDisplayString(cTopology &topo, NodeInfoVector &nodeInfo)
Definition: Ipv4FlatNetworkConfigurator.cc:202