INET Framework for OMNeT++/OMNEST
inet::ospf::InterfaceState Class Referenceabstract

#include <OSPFInterfaceState.h>

Inheritance diagram for inet::ospf::InterfaceState:
inet::ospf::InterfaceStateBackup inet::ospf::InterfaceStateDesignatedRouter inet::ospf::InterfaceStateDown inet::ospf::InterfaceStateLoopback inet::ospf::InterfaceStateNotDesignatedRouter inet::ospf::InterfaceStatePointToPoint inet::ospf::InterfaceStateWaiting

Public Member Functions

virtual ~InterfaceState ()
 
virtual void processEvent (Interface *intf, Interface::InterfaceEventType event)=0
 
virtual Interface::InterfaceStateType getState () const =0
 

Protected Member Functions

void changeState (Interface *intf, InterfaceState *newState, InterfaceState *currentState)
 
void calculateDesignatedRouter (Interface *intf)
 

Constructor & Destructor Documentation

virtual inet::ospf::InterfaceState::~InterfaceState ( )
inlinevirtual
35 {}

Member Function Documentation

void inet::ospf::InterfaceState::calculateDesignatedRouter ( Interface intf)
protected

Referenced by inet::ospf::InterfaceStateBackup::processEvent(), inet::ospf::InterfaceStateWaiting::processEvent(), inet::ospf::InterfaceStateNotDesignatedRouter::processEvent(), and inet::ospf::InterfaceStateDesignatedRouter::processEvent().

123 {
124  RouterID routerID = intf->parentArea->getRouter()->getRouterID();
125  DesignatedRouterID currentDesignatedRouter = intf->designatedRouter;
126  DesignatedRouterID currentBackupRouter = intf->backupDesignatedRouter;
127 
128  unsigned int neighborCount = intf->neighboringRouters.size();
129  unsigned char repeatCount = 0;
130  unsigned int i;
131 
132  DesignatedRouterID declaredBackup;
133  unsigned char declaredBackupPriority;
134  RouterID declaredBackupID;
135  bool backupDeclared;
136 
137  DesignatedRouterID declaredDesignatedRouter;
138  unsigned char declaredDesignatedRouterPriority;
139  RouterID declaredDesignatedRouterID;
140  bool designatedRouterDeclared;
141 
142  do {
143  // calculating backup designated router
144  declaredBackup = NULL_DESIGNATEDROUTERID;
145  declaredBackupPriority = 0;
146  declaredBackupID = NULL_ROUTERID;
147  backupDeclared = false;
148 
149  DesignatedRouterID highestRouter = NULL_DESIGNATEDROUTERID;
150  unsigned char highestPriority = 0;
151  RouterID highestID = NULL_ROUTERID;
152 
153  for (i = 0; i < neighborCount; i++) {
154  Neighbor *neighbor = intf->neighboringRouters[i];
155  unsigned char neighborPriority = neighbor->getPriority();
156 
157  if (neighbor->getState() < Neighbor::TWOWAY_STATE) {
158  continue;
159  }
160  if (neighborPriority == 0) {
161  continue;
162  }
163 
164  RouterID neighborID = neighbor->getNeighborID();
165  DesignatedRouterID neighborsDesignatedRouter = neighbor->getDesignatedRouter();
166  DesignatedRouterID neighborsBackupDesignatedRouter = neighbor->getBackupDesignatedRouter();
167 
168  if (neighborsDesignatedRouter.routerID != neighborID) {
169  if (neighborsBackupDesignatedRouter.routerID == neighborID) {
170  if ((neighborPriority > declaredBackupPriority) ||
171  ((neighborPriority == declaredBackupPriority) &&
172  (neighborID > declaredBackupID)))
173  {
174  declaredBackup = neighborsBackupDesignatedRouter;
175  declaredBackupPriority = neighborPriority;
176  declaredBackupID = neighborID;
177  backupDeclared = true;
178  }
179  }
180  if (!backupDeclared) {
181  if ((neighborPriority > highestPriority) ||
182  ((neighborPriority == highestPriority) &&
183  (neighborID > highestID)))
184  {
185  highestRouter.routerID = neighborID;
186  highestRouter.ipInterfaceAddress = neighbor->getAddress();
187  highestPriority = neighborPriority;
188  highestID = neighborID;
189  }
190  }
191  }
192  }
193  // also include the router itself in the calculations
194  if (intf->routerPriority > 0) {
195  if (currentDesignatedRouter.routerID != routerID) {
196  if (currentBackupRouter.routerID == routerID) {
197  if ((intf->routerPriority > declaredBackupPriority) ||
198  ((intf->routerPriority == declaredBackupPriority) &&
199  (routerID > declaredBackupID)))
200  {
201  declaredBackup.routerID = routerID;
202  declaredBackup.ipInterfaceAddress = intf->interfaceAddressRange.address;
203  declaredBackupPriority = intf->routerPriority;
204  declaredBackupID = routerID;
205  backupDeclared = true;
206  }
207  }
208  if (!backupDeclared) {
209  if ((intf->routerPriority > highestPriority) ||
210  ((intf->routerPriority == highestPriority) &&
211  (routerID > highestID)))
212  {
213  declaredBackup.routerID = routerID;
214  declaredBackup.ipInterfaceAddress = intf->interfaceAddressRange.address;
215  declaredBackupPriority = intf->routerPriority;
216  declaredBackupID = routerID;
217  backupDeclared = true;
218  }
219  else {
220  declaredBackup = highestRouter;
221  declaredBackupPriority = highestPriority;
222  declaredBackupID = highestID;
223  backupDeclared = true;
224  }
225  }
226  }
227  }
228 
229  // calculating backup designated router
230  declaredDesignatedRouter = NULL_DESIGNATEDROUTERID;
231  declaredDesignatedRouterPriority = 0;
232  declaredDesignatedRouterID = NULL_ROUTERID;
233  designatedRouterDeclared = false;
234 
235  for (i = 0; i < neighborCount; i++) {
236  Neighbor *neighbor = intf->neighboringRouters[i];
237  unsigned char neighborPriority = neighbor->getPriority();
238 
239  if (neighbor->getState() < Neighbor::TWOWAY_STATE) {
240  continue;
241  }
242  if (neighborPriority == 0) {
243  continue;
244  }
245 
246  RouterID neighborID = neighbor->getNeighborID();
247  DesignatedRouterID neighborsDesignatedRouter = neighbor->getDesignatedRouter();
248  DesignatedRouterID neighborsBackupDesignatedRouter = neighbor->getBackupDesignatedRouter();
249 
250  if (neighborsDesignatedRouter.routerID == neighborID) {
251  if ((neighborPriority > declaredDesignatedRouterPriority) ||
252  ((neighborPriority == declaredDesignatedRouterPriority) &&
253  (neighborID > declaredDesignatedRouterID)))
254  {
255  declaredDesignatedRouter = neighborsDesignatedRouter;
256  declaredDesignatedRouterPriority = neighborPriority;
257  declaredDesignatedRouterID = neighborID;
258  designatedRouterDeclared = true;
259  }
260  }
261  }
262  // also include the router itself in the calculations
263  if (intf->routerPriority > 0) {
264  if (currentDesignatedRouter.routerID == routerID) {
265  if ((intf->routerPriority > declaredDesignatedRouterPriority) ||
266  ((intf->routerPriority == declaredDesignatedRouterPriority) &&
267  (routerID > declaredDesignatedRouterID)))
268  {
269  declaredDesignatedRouter.routerID = routerID;
270  declaredDesignatedRouter.ipInterfaceAddress = intf->interfaceAddressRange.address;
271  declaredDesignatedRouterPriority = intf->routerPriority;
272  declaredDesignatedRouterID = routerID;
273  designatedRouterDeclared = true;
274  }
275  }
276  }
277  if (!designatedRouterDeclared) {
278  declaredDesignatedRouter = declaredBackup;
279  declaredDesignatedRouterPriority = declaredBackupPriority;
280  declaredDesignatedRouterID = declaredBackupID;
281  designatedRouterDeclared = true;
282  }
283 
284  // if the router is any kind of DR or is no longer one of them, then repeat
285  if (
286  (
287  (declaredDesignatedRouter.routerID != NULL_ROUTERID) &&
288  ((
289  (currentDesignatedRouter.routerID == routerID) &&
290  (declaredDesignatedRouter.routerID != routerID)
291  ) ||
292  (
293  (currentDesignatedRouter.routerID != routerID) &&
294  (declaredDesignatedRouter.routerID == routerID)
295  ))
296  ) ||
297  (
298  (declaredBackup.routerID != NULL_ROUTERID) &&
299  ((
300  (currentBackupRouter.routerID == routerID) &&
301  (declaredBackup.routerID != routerID)
302  ) ||
303  (
304  (currentBackupRouter.routerID != routerID) &&
305  (declaredBackup.routerID == routerID)
306  ))
307  )
308  )
309  {
310  currentDesignatedRouter = declaredDesignatedRouter;
311  currentBackupRouter = declaredBackup;
312  repeatCount++;
313  }
314  else {
315  repeatCount += 2;
316  }
317  } while (repeatCount < 2);
318 
319  RouterID routersOldDesignatedRouterID = intf->designatedRouter.routerID;
320  RouterID routersOldBackupID = intf->backupDesignatedRouter.routerID;
321 
322  intf->designatedRouter = declaredDesignatedRouter;
323  intf->backupDesignatedRouter = declaredBackup;
324 
325  bool wasBackupDesignatedRouter = (routersOldBackupID == routerID);
326  bool wasDesignatedRouter = (routersOldDesignatedRouterID == routerID);
327  bool wasOther = (intf->getState() == Interface::NOT_DESIGNATED_ROUTER_STATE);
328  bool wasWaiting = (!wasBackupDesignatedRouter && !wasDesignatedRouter && !wasOther);
329  bool isBackupDesignatedRouter = (declaredBackup.routerID == routerID);
330  bool isDesignatedRouter = (declaredDesignatedRouter.routerID == routerID);
331  bool isOther = (!isBackupDesignatedRouter && !isDesignatedRouter);
332 
333  if (wasBackupDesignatedRouter) {
334  if (isDesignatedRouter) {
335  changeState(intf, new InterfaceStateDesignatedRouter, this);
336  }
337  if (isOther) {
338  changeState(intf, new InterfaceStateNotDesignatedRouter, this);
339  }
340  }
341  if (wasDesignatedRouter) {
342  if (isBackupDesignatedRouter) {
343  changeState(intf, new InterfaceStateBackup, this);
344  }
345  if (isOther) {
346  changeState(intf, new InterfaceStateNotDesignatedRouter, this);
347  }
348  }
349  if (wasOther) {
350  if (isDesignatedRouter) {
351  changeState(intf, new InterfaceStateDesignatedRouter, this);
352  }
353  if (isBackupDesignatedRouter) {
354  changeState(intf, new InterfaceStateBackup, this);
355  }
356  }
357  if (wasWaiting) {
358  if (isDesignatedRouter) {
359  changeState(intf, new InterfaceStateDesignatedRouter, this);
360  }
361  if (isBackupDesignatedRouter) {
362  changeState(intf, new InterfaceStateBackup, this);
363  }
364  if (isOther) {
365  changeState(intf, new InterfaceStateNotDesignatedRouter, this);
366  }
367  }
368 
369  for (i = 0; i < neighborCount; i++) {
370  if ((intf->interfaceType == Interface::NBMA) &&
371  ((!wasBackupDesignatedRouter && isBackupDesignatedRouter) ||
372  (!wasDesignatedRouter && isDesignatedRouter)))
373  {
374  if (intf->neighboringRouters[i]->getPriority() == 0) {
375  intf->neighboringRouters[i]->processEvent(Neighbor::START);
376  }
377  }
378  if ((declaredDesignatedRouter.routerID != routersOldDesignatedRouterID) ||
379  (declaredBackup.routerID != routersOldBackupID))
380  {
381  if (intf->neighboringRouters[i]->getState() >= Neighbor::TWOWAY_STATE) {
382  intf->neighboringRouters[i]->processEvent(Neighbor::IS_ADJACENCY_OK);
383  }
384  }
385  }
386 }
Definition: OSPFNeighbor.h:65
Definition: OSPFInterface.h:58
const DesignatedRouterID NULL_DESIGNATEDROUTERID
Definition: OSPFcommon.h:164
Definition: OSPFNeighbor.h:43
void changeState(Interface *intf, InterfaceState *newState, InterfaceState *currentState)
Definition: OSPFInterfaceState.cc:33
const RouterID NULL_ROUTERID(0, 0, 0, 0)
Definition: OSPFNeighbor.h:49
IPv4Address RouterID
Definition: OSPFcommon.h:137
void inet::ospf::InterfaceState::changeState ( Interface intf,
InterfaceState newState,
InterfaceState currentState 
)
protected

Referenced by calculateDesignatedRouter(), inet::ospf::InterfaceStateWaiting::processEvent(), inet::ospf::InterfaceStatePointToPoint::processEvent(), inet::ospf::InterfaceStateNotDesignatedRouter::processEvent(), inet::ospf::InterfaceStateLoopback::processEvent(), inet::ospf::InterfaceStateDown::processEvent(), inet::ospf::InterfaceStateDesignatedRouter::processEvent(), and inet::ospf::InterfaceStateBackup::processEvent().

34 {
35  Interface::InterfaceStateType oldState = currentState->getState();
36  Interface::InterfaceStateType nextState = newState->getState();
37  Interface::OSPFInterfaceType intfType = intf->getType();
38  bool shouldRebuildRoutingTable = false;
39 
40  intf->changeState(newState, currentState);
41 
42  if ((oldState == Interface::DOWN_STATE) ||
43  (nextState == Interface::DOWN_STATE) ||
44  (oldState == Interface::LOOPBACK_STATE) ||
45  (nextState == Interface::LOOPBACK_STATE) ||
46  (oldState == Interface::DESIGNATED_ROUTER_STATE) ||
47  (nextState == Interface::DESIGNATED_ROUTER_STATE) ||
48  ((intfType == Interface::POINTTOPOINT) &&
49  ((oldState == Interface::POINTTOPOINT_STATE) ||
50  (nextState == Interface::POINTTOPOINT_STATE))) ||
51  (((intfType == Interface::BROADCAST) ||
52  (intfType == Interface::NBMA)) &&
53  ((oldState == Interface::WAITING_STATE) ||
54  (nextState == Interface::WAITING_STATE))))
55  {
56  RouterLSA *routerLSA = intf->getArea()->findRouterLSA(intf->getArea()->getRouter()->getRouterID());
57 
58  if (routerLSA != nullptr) {
59  long sequenceNumber = routerLSA->getHeader().getLsSequenceNumber();
60  if (sequenceNumber == MAX_SEQUENCE_NUMBER) {
61  routerLSA->getHeader().setLsAge(MAX_AGE);
62  intf->getArea()->floodLSA(routerLSA);
63  routerLSA->incrementInstallTime();
64  }
65  else {
66  RouterLSA *newLSA = intf->getArea()->originateRouterLSA();
67 
68  newLSA->getHeader().setLsSequenceNumber(sequenceNumber + 1);
69  shouldRebuildRoutingTable |= routerLSA->update(newLSA);
70  delete newLSA;
71 
72  intf->getArea()->floodLSA(routerLSA);
73  }
74  }
75  else { // (lsa == nullptr) -> This must be the first time any interface is up...
76  RouterLSA *newLSA = intf->getArea()->originateRouterLSA();
77 
78  shouldRebuildRoutingTable |= intf->getArea()->installRouterLSA(newLSA);
79 
80  routerLSA = intf->getArea()->findRouterLSA(intf->getArea()->getRouter()->getRouterID());
81 
82  intf->getArea()->setSPFTreeRoot(routerLSA);
83  intf->getArea()->floodLSA(newLSA);
84  delete newLSA;
85  }
86  }
87 
88  if (nextState == Interface::DESIGNATED_ROUTER_STATE) {
89  NetworkLSA *newLSA = intf->getArea()->originateNetworkLSA(intf);
90  if (newLSA != nullptr) {
91  shouldRebuildRoutingTable |= intf->getArea()->installNetworkLSA(newLSA);
92 
93  intf->getArea()->floodLSA(newLSA);
94  delete newLSA;
95  }
96  else { // no neighbors on the network -> old NetworkLSA must be flushed
97  NetworkLSA *oldLSA = intf->getArea()->findNetworkLSA(intf->getAddressRange().address);
98 
99  if (oldLSA != nullptr) {
100  oldLSA->getHeader().setLsAge(MAX_AGE);
101  intf->getArea()->floodLSA(oldLSA);
102  oldLSA->incrementInstallTime();
103  }
104  }
105  }
106 
107  if (oldState == Interface::DESIGNATED_ROUTER_STATE) {
108  NetworkLSA *networkLSA = intf->getArea()->findNetworkLSA(intf->getAddressRange().address);
109 
110  if (networkLSA != nullptr) {
111  networkLSA->getHeader().setLsAge(MAX_AGE);
112  intf->getArea()->floodLSA(networkLSA);
113  networkLSA->incrementInstallTime();
114  }
115  }
116 
117  if (shouldRebuildRoutingTable) {
118  intf->getArea()->getRouter()->rebuildRoutingTable();
119  }
120 }
Definition: OSPFInterface.h:66
Definition: OSPFInterface.h:65
Definition: OSPFInterface.h:64
Definition: OSPFInterface.h:56
InterfaceStateType
Definition: OSPFInterface.h:63
Definition: OSPFInterface.h:58
#define MAX_SEQUENCE_NUMBER
Definition: OSPFcommon.h:43
Definition: OSPFInterface.h:57
#define MAX_AGE
Definition: OSPFcommon.h:36
Definition: OSPFInterface.h:67
OSPFInterfaceType
Definition: OSPFInterface.h:54

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