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

Computes L2 configuration of the network. More...

#include <L2NetworkConfigurator.h>

Inheritance diagram for inet::L2NetworkConfigurator:

Classes

class  InterfaceInfo
 Represents an interface in the network. More...
 
class  L2Topology
 
class  Link
 
class  Matcher
 
class  Node
 Represents a node in the network. More...
 

Public Types

typedef Ieee8021dInterfaceData::PortInfo PortInfo
 

Public Member Functions

 L2NetworkConfigurator ()
 
virtual void readInterfaceConfiguration (Node *rootNode)
 Reads interface elements from the configuration file and stores result. More...
 
virtual void configureInterface (InterfaceEntry *interfaceEntry)
 Configures the provided interface based on the current network configuration. More...
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void extractTopology (L2Topology &topology)
 Extracts network topology by walking through the module hierarchy. More...
 
virtual void computeConfiguration ()
 Computes the Layer 2 network configuration for all nodes in the network. More...
 
virtual bool linkContainsMatchingHostExcept (InterfaceInfo *currentInfo, Matcher &hostMatcher, cModule *exceptModule)
 
void ensureConfigurationComputed (L2Topology &topology)
 
virtual Topology::LinkOutfindLinkOut (Node *node, int gateId)
 
void configureInterface (InterfaceInfo *interfaceInfo)
 

Protected Attributes

cXMLElement * configuration = nullptr
 
L2Topology topology
 
NoderootNode = nullptr
 

Detailed Description

Computes L2 configuration of the network.

See the NED definition for details.

Member Typedef Documentation

Constructor & Destructor Documentation

inet::L2NetworkConfigurator::L2NetworkConfigurator ( )
inline
40 { }

Member Function Documentation

void inet::L2NetworkConfigurator::computeConfiguration ( )
protectedvirtual

Computes the Layer 2 network configuration for all nodes in the network.

The result of the computation is only stored in the network configurator.

Referenced by ensureConfigurationComputed().

214 {
215  long initializeStartTime = clock();
216  // extract topology into the L2Topology object
218  // read the configuration from XML; it will serve as input for port assignment
220  printElapsedTime("initialize", initializeStartTime);
221 }
Node * rootNode
Definition: L2NetworkConfigurator.h:110
virtual void extractTopology(L2Topology &topology)
Extracts network topology by walking through the module hierarchy.
Definition: L2NetworkConfigurator.cc:60
virtual void readInterfaceConfiguration(Node *rootNode)
Reads interface elements from the configuration file and stores result.
Definition: L2NetworkConfigurator.cc:116
void printElapsedTime(const char *name, long startTime)
Definition: INETDefs.h:94
L2Topology topology
Definition: L2NetworkConfigurator.h:109
#define TIME(CODE)
Definition: INETDefs.h:99
void inet::L2NetworkConfigurator::configureInterface ( InterfaceInfo interfaceInfo)
protected

Referenced by configureInterface(), inet::L2NodeConfigurator::configureNode(), and inet::L2NodeConfigurator::receiveSignal().

279 {
280  InterfaceEntry *interfaceEntry = interfaceInfo->interfaceEntry;
281  Ieee8021dInterfaceData *interfaceData = interfaceEntry->ieee8021dData();
282 
283  interfaceData->setLinkCost(interfaceInfo->portData.linkCost);
284  interfaceData->setPriority(interfaceInfo->portData.priority);
285  interfaceData->setEdge(interfaceInfo->portData.edge);
286 }
void inet::L2NetworkConfigurator::configureInterface ( InterfaceEntry interfaceEntry)
virtual

Configures the provided interface based on the current network configuration.

262 {
264  cModule *networkNodeModule = findContainingNode(interfaceEntry->getInterfaceModule());
265  // TODO: avoid linear search
266  for (int i = 0; i < topology.getNumNodes(); i++) {
267  Node *node = (Node *)topology.getNode(i);
268  if (node->module == networkNodeModule) {
269  for (auto & elem : node->interfaceInfos) {
270  InterfaceInfo *interfaceInfo = elem;
271  if (interfaceInfo->interfaceEntry == interfaceEntry)
272  return configureInterface(interfaceInfo);
273  }
274  }
275  }
276 }
Node * getNode(int i)
Returns pointer to the ith node in the graph.
Definition: Topology.cc:356
int getNumNodes() const
Returns the number of nodes in the graph.
Definition: Topology.h:522
void configureInterface(InterfaceInfo *interfaceInfo)
Definition: L2NetworkConfigurator.cc:278
void ensureConfigurationComputed(L2Topology &topology)
Definition: L2NetworkConfigurator.cc:223
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
L2Topology topology
Definition: L2NetworkConfigurator.h:109
void inet::L2NetworkConfigurator::ensureConfigurationComputed ( L2Topology topology)
protected

Referenced by configureInterface(), and initialize().

224 {
225  if (topology.getNumNodes() == 0)
227 }
int getNumNodes() const
Returns the number of nodes in the graph.
Definition: Topology.h:522
virtual void computeConfiguration()
Computes the Layer 2 network configuration for all nodes in the network.
Definition: L2NetworkConfigurator.cc:213
L2Topology topology
Definition: L2NetworkConfigurator.h:109
void inet::L2NetworkConfigurator::extractTopology ( L2Topology topology)
protectedvirtual

Extracts network topology by walking through the module hierarchy.

Creates vertices from modules having property. Creates edges from connections (wired and wireless) between network interfaces.

Referenced by computeConfiguration().

61 {
62  topology.extractByProperty("networkNode");
63  EV_DEBUG << "Topology found " << topology.getNumNodes() << " nodes\n";
64 
65  if (topology.getNumNodes() == 0)
66  throw cRuntimeError("Empty network!");
67 
68  // extract nodes, fill in interfaceTable and routingTable members in node
69  for (int i = 0; i < topology.getNumNodes(); i++) {
70  Node *node = (Node *)topology.getNode(i);
71  node->module = node->getModule();
72  cModule *ifTable = node->module->getSubmodule("interfaceTable");
73 
74  // EtherHost has no InterfaceTable
75  if (ifTable) //todo:
76  node->interfaceTable = dynamic_cast<IInterfaceTable *>(ifTable);
77  }
78 
79  // extract links and interfaces
80  std::set<InterfaceEntry *> interfacesSeen;
81  std::queue<Node *> Q; // unvisited nodes in the graph
82 
83  rootNode = (Node *)topology.getNode(0);
84  Q.push(rootNode);
85 
86  while (!Q.empty()) {
87  Node *node = Q.front();
88  Q.pop();
89  IInterfaceTable *interfaceTable = node->interfaceTable;
90 
91  if (interfaceTable) {
92  // push neighbors to the queue
93  for (int i = 0; i < interfaceTable->getNumInterfaces(); i++) {
94  InterfaceEntry *interfaceEntry = interfaceTable->getInterface(i);
95  if (interfacesSeen.count(interfaceEntry) == 0) {
96  // visiting this interface
97  interfacesSeen.insert(interfaceEntry);
98 
99  Topology::LinkOut *linkOut = findLinkOut(node, interfaceEntry->getNodeOutputGateId());
100 
101  Node *childNode = nullptr;
102 
103  if (linkOut) {
104  childNode = (Node *)linkOut->getRemoteNode();
105  Q.push(childNode);
106  }
107 
108  InterfaceInfo *info = new InterfaceInfo(node, childNode, interfaceEntry);
109  node->interfaceInfos.push_back(info);
110  }
111  }
112  }
113  }
114 }
void extractByProperty(const char *propertyName, const char *value=nullptr)
Extracts model topology by a module property.
Definition: Topology.cc:159
Node * rootNode
Definition: L2NetworkConfigurator.h:110
Node * getNode(int i)
Returns pointer to the ith node in the graph.
Definition: Topology.cc:356
int getNumNodes() const
Returns the number of nodes in the graph.
Definition: Topology.h:522
cModule * module
Definition: L2NetworkConfigurator.h:52
virtual Topology::LinkOut * findLinkOut(Node *node, int gateId)
Definition: L2NetworkConfigurator.cc:229
IInterfaceTable * interfaceTable
Definition: L2NetworkConfigurator.h:53
L2Topology topology
Definition: L2NetworkConfigurator.h:109
virtual InterfaceEntry * getInterface(int pos) const =0
Returns the InterfaceEntry specified by an index 0..numInterfaces-1.
Topology::LinkOut * inet::L2NetworkConfigurator::findLinkOut ( Node node,
int  gateId 
)
protectedvirtual

Referenced by extractTopology().

230 {
231  for (int i = 0; i < node->getNumOutLinks(); i++)
232  if (node->getLinkOut(i)->getLocalGateId() == gateId)
233  return node->getLinkOut(i);
234 
235 
236  return nullptr;
237 }
virtual void inet::L2NetworkConfigurator::handleMessage ( cMessage *  msg)
inlineoverrideprotectedvirtual
115 { throw cRuntimeError("this module doesn't handle messages, it runs only in initialize()"); }
void inet::L2NetworkConfigurator::initialize ( int  stage)
overrideprotectedvirtual
46 {
47  if (stage == INITSTAGE_LOCAL)
48  configuration = par("config");
49  else if (stage == INITSTAGE_LINK_LAYER)
51 }
void ensureConfigurationComputed(L2Topology &topology)
Definition: L2NetworkConfigurator.cc:223
Local initializations.
Definition: InitStages.h:35
Initialization of link-layer protocols.
Definition: InitStages.h:59
cXMLElement * configuration
Definition: L2NetworkConfigurator.h:108
L2Topology topology
Definition: L2NetworkConfigurator.h:109
bool inet::L2NetworkConfigurator::linkContainsMatchingHostExcept ( InterfaceInfo currentInfo,
Matcher hostMatcher,
cModule *  exceptModule 
)
protectedvirtual

Referenced by readInterfaceConfiguration().

241 {
242  Node *childNode = currentInfo->childNode;
243 
244  if (childNode == nullptr)
245  return false;
246 
247  cModule *hostModule = childNode->module;
248 
249  std::string hostFullPath = hostModule->getFullPath();
250  std::string hostShortenedFullPath = hostFullPath.substr(hostFullPath.find('.') + 1);
251 
252  if (hostModule == exceptModule)
253  return false;
254 
255  if (hostMatcher.matches(hostShortenedFullPath.c_str()) || hostMatcher.matches(hostFullPath.c_str()))
256  return true;
257 
258  return false;
259 }
virtual int inet::L2NetworkConfigurator::numInitStages ( ) const
inlineoverrideprotectedvirtual
114 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::L2NetworkConfigurator::readInterfaceConfiguration ( Node rootNode)
virtual

Reads interface elements from the configuration file and stores result.

Referenced by computeConfiguration().

117 {
118  std::set<InterfaceEntry *> matchedBefore;
119  cXMLElementList interfaceElements = configuration->getChildrenByTagName("interface");
120 
121  for (auto & interfaceElements_i : interfaceElements) {
122  std::set<InterfaceEntry *> interfacesSeen;
123  cXMLElement *interfaceElement = interfaceElements_i;
124 
125  const char *hostAttr = interfaceElement->getAttribute("hosts"); // "host* router[0..3]"
126  const char *interfaceAttr = interfaceElement->getAttribute("names"); // i.e. interface names, like "eth* ppp0"
127  const char *towardsAttr = interfaceElement->getAttribute("towards"); // neighbor host names, like "ap switch"
128  const char *amongAttr = interfaceElement->getAttribute("among"); // neighbor host names, like "host[*] router1"
129  const char *portsAttr = interfaceElement->getAttribute("ports"); // switch gate indices, like "0 1 2"
130 
131  // Begin RSTP properties, for more information see RSTP module
132  const char *cost = interfaceElement->getAttribute("cost");
133  const char *priority = interfaceElement->getAttribute("priority");
134  const char *edge = interfaceElement->getAttribute("edge");
135  // End RSTP properties
136 
137  if (amongAttr && *amongAttr) { // among="X Y Z" means hosts = "X Y Z" towards = "X Y Z"
138  if ((hostAttr && *hostAttr) || (towardsAttr && *towardsAttr))
139  throw cRuntimeError("The 'hosts'/'towards' and 'among' attributes are mutually exclusive, at %s",
140  interfaceElement->getSourceLocation());
141  towardsAttr = hostAttr = amongAttr;
142  }
143 
144  try {
145  // parse host/interface/towards expressions
146  Matcher hostMatcher(hostAttr);
147  Matcher interfaceMatcher(interfaceAttr);
148  Matcher towardsMatcher(towardsAttr);
149  Matcher portsMatcher(portsAttr);
150 
151  std::queue<Node *> Q;
152  Q.push(rootNode);
153 
154  // configure port type/cost/priority constraints on matching interfaces
155  while (!Q.empty()) {
156  Node *currentNode = Q.front();
157  Q.pop();
158 
159  for (unsigned int i = 0; i < currentNode->interfaceInfos.size(); i++) {
160  InterfaceEntry *ifEntry = currentNode->interfaceInfos[i]->interfaceEntry;
161  if (interfacesSeen.count(ifEntry) == 0 && matchedBefore.count(ifEntry) == 0) {
162  cModule *hostModule = currentNode->module;
163  std::string hostFullPath = hostModule->getFullPath();
164  std::string hostShortenedFullPath = hostFullPath.substr(hostFullPath.find('.') + 1);
165 
166  // loopback interfaces
167  if (ifEntry->getNodeInputGateId() == -1) {
168  interfacesSeen.insert(ifEntry);
169  continue;
170  }
171 
172  cGate *gate = hostModule->gate(ifEntry->getNodeInputGateId());
173  std::stringstream ss;
174  ss << gate->getIndex();
175  std::string port = ss.str();
176 
177  // Note: "hosts", "interfaces" and "towards" must ALL match on the interface for the rule to apply
178  if ((hostMatcher.matchesAny() || hostMatcher.matches(hostShortenedFullPath.c_str()) || hostMatcher.matches(hostFullPath.c_str()))
179  && (interfaceMatcher.matchesAny() || interfaceMatcher.matches(ifEntry->getFullName()))
180  && (towardsMatcher.matchesAny() || linkContainsMatchingHostExcept(currentNode->interfaceInfos[i], towardsMatcher, hostModule))
181  && (portsMatcher.matchesAny() || portsMatcher.matches(port.c_str())))
182  {
183  // cost
184  if (isNotEmpty(cost))
185  currentNode->interfaceInfos[i]->portData.linkCost = atoi(cost);
186 
187  // priority
188  if (isNotEmpty(priority))
189  currentNode->interfaceInfos[i]->portData.priority = atoi(priority);
190 
191  //edge
192  if (isNotEmpty(edge))
193  currentNode->interfaceInfos[i]->portData.edge = strcmp(edge, "true") ? false : true;
194  EV_DEBUG << hostModule->getFullPath() << ":" << ifEntry->getFullName() << endl;
195 
196  matchedBefore.insert(ifEntry);
197  }
198 
199  interfacesSeen.insert(ifEntry);
200  if (currentNode->interfaceInfos[i]->childNode)
201  Q.push(currentNode->interfaceInfos[i]->childNode);
202  }
203  }
204  }
205  }
206  catch (std::exception& e) {
207  throw cRuntimeError("Error in XML <interface> element at %s: %s", interfaceElement->getSourceLocation(),
208  e.what());
209  }
210  }
211 }
Node * rootNode
Definition: L2NetworkConfigurator.h:110
bool isNotEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:40
const value< double, units::C > e(1.602176487e-19)
virtual bool linkContainsMatchingHostExcept(InterfaceInfo *currentInfo, Matcher &hostMatcher, cModule *exceptModule)
Definition: L2NetworkConfigurator.cc:239
cXMLElement * configuration
Definition: L2NetworkConfigurator.h:108

Member Data Documentation

cXMLElement* inet::L2NetworkConfigurator::configuration = nullptr
protected
Node* inet::L2NetworkConfigurator::rootNode = nullptr
protected
L2Topology inet::L2NetworkConfigurator::topology
protected

The documentation for this class was generated from the following files: