INET Framework for OMNeT++/OMNEST
inet::bgp::BgpConfigReader Class Reference

#include <BgpConfigReader.h>

Public Member Functions

 BgpConfigReader (cModule *bgpModule, IInterfaceTable *ift)
 
virtual ~BgpConfigReader ()
 
void loadConfigFromXML (cXMLElement *bgpConfig, BgpRouter *bgpRouter)
 

Private Member Functions

std::vector< const char * > findInternalPeers (cXMLElementList &ASConfig)
 
void loadASConfig (cXMLElementList &ASConfig)
 
void loadEbgpSessionConfig (cXMLElementList &ASConfig, cXMLElementList &sessionList, simtime_t *delayTab)
 
AsId findMyAS (cXMLElementList &ASList, int &outRouterPosition)
 
void loadTimerConfig (cXMLElementList &timerConfig, simtime_t *delayTab)
 
int isInInterfaceTable (IInterfaceTable *ifTable, Ipv4Address addr)
 
int isInInterfaceTable (IInterfaceTable *ifTable, std::string ifName)
 
unsigned int calculateStartDelay (int rtListSize, unsigned char rtPosition, unsigned char rtPeerPosition)
 
bool getBoolAttrOrPar (const cXMLElement &ifConfig, const char *name) const
 
int getIntAttrOrPar (const cXMLElement &ifConfig, const char *name) const
 
const char * getStrAttrOrPar (const cXMLElement &ifConfig, const char *name) const
 

Private Attributes

cModule * bgpModule = nullptr
 
IInterfaceTableift = nullptr
 
BgpRouterbgpRouter = nullptr
 

Constructor & Destructor Documentation

◆ BgpConfigReader()

inet::bgp::BgpConfigReader::BgpConfigReader ( cModule *  bgpModule,
IInterfaceTable ift 
)
16  :
18 {
19 }

◆ ~BgpConfigReader()

virtual inet::bgp::BgpConfigReader::~BgpConfigReader ( )
inlinevirtual
30 {}

Member Function Documentation

◆ calculateStartDelay()

unsigned int inet::bgp::BgpConfigReader::calculateStartDelay ( int  rtListSize,
unsigned char  rtPosition,
unsigned char  rtPeerPosition 
)
private
272 {
273  unsigned int startDelay = 0;
274  if (rtPeerPosition == 1) {
275  if (rtPosition == 1) {
276  startDelay = 1;
277  }
278  else {
279  startDelay = (rtPosition - 1) * 2;
280  }
281  return startDelay;
282  }
283 
284  if (rtPosition < rtPeerPosition) {
285  startDelay = 2;
286  }
287  else if (rtPosition > rtPeerPosition) {
288  startDelay = (rtListSize - 1) * 2 - 2 * (rtPeerPosition - 2);
289  }
290  else {
291  startDelay = (rtListSize - 1) * 2 + 1;
292  }
293  return startDelay;
294 }

Referenced by loadConfigFromXML().

◆ findInternalPeers()

std::vector< const char * > inet::bgp::BgpConfigReader::findInternalPeers ( cXMLElementList &  ASConfig)
private
181 {
182  std::vector<const char *> routerInSameASList;
183  for (auto& elem : ASConfig) {
184  std::string nodeName = elem->getTagName();
185  if (nodeName == "Router") {
186  Ipv4Address internalAddr = Ipv4Address(elem->getAttribute("interAddr"));
187  if (isInInterfaceTable(ift, internalAddr) == -1)
188  routerInSameASList.push_back(elem->getAttribute("interAddr"));
189  else
190  bgpRouter->setInternalAddress(internalAddr);
191  }
192  }
193  return routerInSameASList;
194 }

Referenced by loadConfigFromXML().

◆ findMyAS()

AsId inet::bgp::BgpConfigReader::findMyAS ( cXMLElementList &  ASList,
int &  outRouterPosition 
)
private
98 {
99  // find my own Ipv4 address in the configuration file and return the AS id under which it is configured
100  // and also the 1 based position of the entry inside the AS config element
101  for (auto& elem : asList) {
102  cXMLElementList routerList = (elem)->getChildrenByTagName("Router");
103  outRouterPosition = 1;
104  for (auto& routerList_routerListIt : routerList) {
105  Ipv4Address routerAddr = Ipv4Address((routerList_routerListIt)->getAttribute("interAddr"));
106  for (int i = 0; i < ift->getNumInterfaces(); i++) {
107  if (ift->getInterface(i)->getProtocolData<Ipv4InterfaceData>()->getIPAddress() == routerAddr)
108  return atoi((routerList_routerListIt)->getParentNode()->getAttribute("id"));
109  }
110  outRouterPosition++;
111  }
112  }
113 
114  return 0;
115 }

Referenced by loadConfigFromXML().

◆ getBoolAttrOrPar()

bool inet::bgp::BgpConfigReader::getBoolAttrOrPar ( const cXMLElement &  ifConfig,
const char *  name 
) const
private
297 {
298  const char *attrStr = ifConfig.getAttribute(name);
299  if (attrStr && *attrStr) {
300  if (strcmp(attrStr, "true") == 0 || strcmp(attrStr, "1") == 0)
301  return true;
302  if (strcmp(attrStr, "false") == 0 || strcmp(attrStr, "0") == 0)
303  return false;
304  throw cRuntimeError("Invalid boolean attribute %s = '%s' at %s", name, attrStr, ifConfig.getSourceLocation());
305  }
306  return bgpModule->par(name);
307 }

Referenced by loadASConfig(), and loadEbgpSessionConfig().

◆ getIntAttrOrPar()

int inet::bgp::BgpConfigReader::getIntAttrOrPar ( const cXMLElement &  ifConfig,
const char *  name 
) const
private
310 {
311  const char *attrStr = ifConfig.getAttribute(name);
312  if (attrStr && *attrStr)
313  return atoi(attrStr);
314  return bgpModule->par(name);
315 }

Referenced by loadASConfig(), and loadEbgpSessionConfig().

◆ getStrAttrOrPar()

const char * inet::bgp::BgpConfigReader::getStrAttrOrPar ( const cXMLElement &  ifConfig,
const char *  name 
) const
private
318 {
319  const char *attrStr = ifConfig.getAttribute(name);
320  if (attrStr && *attrStr)
321  return attrStr;
322  return bgpModule->par(name);
323 }

Referenced by loadASConfig().

◆ isInInterfaceTable() [1/2]

int inet::bgp::BgpConfigReader::isInInterfaceTable ( IInterfaceTable ifTable,
Ipv4Address  addr 
)
private
252 {
253  for (int i = 0; i < ifTable->getNumInterfaces(); i++) {
254  if (ifTable->getInterface(i)->getProtocolData<Ipv4InterfaceData>()->getIPAddress() == addr) {
255  return i;
256  }
257  }
258  return -1;
259 }

Referenced by findInternalPeers(), loadASConfig(), and loadEbgpSessionConfig().

◆ isInInterfaceTable() [2/2]

int inet::bgp::BgpConfigReader::isInInterfaceTable ( IInterfaceTable ifTable,
std::string  ifName 
)
private
262 {
263  for (int i = 0; i < ifTable->getNumInterfaces(); i++) {
264  if (std::string(ifTable->getInterface(i)->getInterfaceName()) == ifName) {
265  return i;
266  }
267  }
268  return -1;
269 }

◆ loadASConfig()

void inet::bgp::BgpConfigReader::loadASConfig ( cXMLElementList &  ASConfig)
private
197 {
198  // set the default values
200 
201  for (auto& elem : ASConfig) {
202  std::string nodeName = elem->getTagName();
203  if (nodeName == "Router") {
204  Ipv4Address internalAddr = Ipv4Address(elem->getAttribute("interAddr"));
205  if (isInInterfaceTable(ift, internalAddr) != -1) {
206  bgpRouter->setRedistributeInternal(getBoolAttrOrPar(*elem, "redistributeInternal"));
207  bgpRouter->setRedistributeOspf(getStrAttrOrPar(*elem, "redistributeOspf"));
208  bgpRouter->setRedistributeRip(getBoolAttrOrPar(*elem, "redistributeRip"));
209 
210  for (auto& entry : elem->getChildren()) {
211  std::string nodeName = entry->getTagName();
212  if (nodeName == "Network") {
213  const char *address = entry->getAttribute("address");
214  if (address && *address)
215  bgpRouter->addToAdvertiseList(Ipv4Address(address));
216  else
217  throw cRuntimeError("BGP Error: attribute 'address' is mandatory in 'Network'");
218  }
219  else if (nodeName == "Neighbor") {
220  const char *peer = entry->getAttribute("address");
221  if (peer && *peer) {
222  bool nextHopSelf = getBoolAttrOrPar(*entry, "nextHopSelf");
223  bgpRouter->setNextHopSelf(Ipv4Address(peer), nextHopSelf);
224 
225  int localPreference = getIntAttrOrPar(*entry, "localPreference");
226  bgpRouter->setLocalPreference(Ipv4Address(peer), localPreference);
227  }
228  else
229  throw cRuntimeError("BGP Error: attribute 'address' is mandatory in 'Neighbor'");
230  }
231  else
232  throw cRuntimeError("BGP Error: attribute '%s' is invalid in 'Router'", nodeName.c_str());
233  }
234  }
235  }
236  else if (nodeName == "DenyRoute" || nodeName == "DenyRouteIN" || nodeName == "DenyRouteOUT") {
237  BgpRoutingTableEntry *entry = new BgpRoutingTableEntry(); // FIXME Who will delete this entry?
238  entry->setDestination(Ipv4Address((elem)->getAttribute("Address")));
239  entry->setNetmask(Ipv4Address((elem)->getAttribute("Netmask")));
240  bgpRouter->addToPrefixList(nodeName, entry);
241  }
242  else if (nodeName == "DenyAS" || nodeName == "DenyASIN" || nodeName == "DenyASOUT") {
243  AsId ASCur = atoi((elem)->getNodeValue());
244  bgpRouter->addToAsList(nodeName, ASCur);
245  }
246  else
247  throw cRuntimeError("BGP Error: unknown element named '%s' for AS %u", nodeName.c_str(), bgpRouter->getAsId());
248  }
249 }

Referenced by loadConfigFromXML().

◆ loadConfigFromXML()

void inet::bgp::BgpConfigReader::loadConfigFromXML ( cXMLElement *  bgpConfig,
BgpRouter bgpRouter 
)
22 {
23  this->bgpRouter = bgpRouter;
24 
25  if (strcmp(bgpConfig->getTagName(), "BGPConfig"))
26  throw cRuntimeError("Cannot read BGP configuration, unaccepted '%s' node at %s", bgpConfig->getTagName(), bgpConfig->getSourceLocation());
27 
28  // load bgp timer parameters informations
29  cXMLElement *paramNode = bgpConfig->getElementByPath("TimerParams");
30  if (paramNode == nullptr)
31  throw cRuntimeError("BGP Error: No configuration for BGP timer parameters");
32  cXMLElementList timerConfig = paramNode->getChildren();
33  simtime_t delayTab[NB_TIMERS];
34  loadTimerConfig(timerConfig, delayTab);
35 
36  // find my AS
37  cXMLElementList ASList = bgpConfig->getElementsByTagName("AS");
38  int routerPosition;
39  AsId myAsId = findMyAS(ASList, routerPosition);
40  if (myAsId == 0)
41  throw cRuntimeError("BGP Error: No AS configuration for Router ID: %s", bgpRouter->getRouterId().str().c_str());
42 
43  bgpRouter->setAsId(myAsId);
44 
45  // load AS information
46  char ASXPath[32];
47  sprintf(ASXPath, "AS[@id='%d']", myAsId);
48  cXMLElement *ASNode = bgpConfig->getElementByPath(ASXPath);
49  if (ASNode == nullptr)
50  throw cRuntimeError("BGP Error: No configuration for AS ID: %d", myAsId);
51  cXMLElementList ASConfig = ASNode->getChildren();
52 
53  // load EGP Session informations
54  cXMLElementList sessionList = bgpConfig->getElementsByTagName("Session");
55  simtime_t saveStartDelay = delayTab[3];
56  loadEbgpSessionConfig(ASConfig, sessionList, delayTab);
57  delayTab[3] = saveStartDelay;
58 
59  // get all BGP speakers in my AS
60  auto routerInSameASList = findInternalPeers(ASConfig);
61 
62  // create an IGP Session with each BGP speaker in my AS
63  if (routerInSameASList.size()) {
64  unsigned int routerPeerPosition = 1;
65  delayTab[3] += sessionList.size() * 2;
66  for (auto it = routerInSameASList.begin(); it != routerInSameASList.end(); it++, routerPeerPosition++) {
67  SessionId newSessionID = bgpRouter->createIbgpSession(*it /*peer address*/);
68  delayTab[3] += calculateStartDelay(routerInSameASList.size(), routerPosition, routerPeerPosition);
69  bgpRouter->setTimer(newSessionID, delayTab);
70  bgpRouter->setSocketListen(newSessionID);
71  }
72  }
73 
74  // should be called after all (E-BGP/I-BGP) sessions are created
75  loadASConfig(ASConfig);
76 }

Referenced by inet::bgp::Bgp::createBgpRouter().

◆ loadEbgpSessionConfig()

void inet::bgp::BgpConfigReader::loadEbgpSessionConfig ( cXMLElementList &  ASConfig,
cXMLElementList &  sessionList,
simtime_t *  delayTab 
)
private
118 {
119  simtime_t saveStartDelay = delayTab[3];
120  for (auto sessionListIt = sessionList.begin(); sessionListIt != sessionList.end(); sessionListIt++, delayTab[3] = saveStartDelay) {
121  auto numRouters = (*sessionListIt)->getChildren();
122  if (numRouters.size() != 2)
123  throw cRuntimeError("BGP Error: Number of routers is invalid for session ID : %s", (*sessionListIt)->getAttribute("id"));
124 
125  Ipv4Address routerAddr1 = Ipv4Address((*sessionListIt)->getFirstChild()->getAttribute("exterAddr"));
126  Ipv4Address routerAddr2 = Ipv4Address((*sessionListIt)->getLastChild()->getAttribute("exterAddr"));
127  if (isInInterfaceTable(ift, routerAddr1) == -1 && isInInterfaceTable(ift, routerAddr2) == -1)
128  continue;
129 
130  Ipv4Address peerAddr;
131  Ipv4Address myAddr;
132  if (isInInterfaceTable(ift, routerAddr1) != -1) {
133  peerAddr = routerAddr2;
134  myAddr = routerAddr1;
135  delayTab[3] += atoi((*sessionListIt)->getAttribute("id"));
136  }
137  else {
138  peerAddr = routerAddr1;
139  myAddr = routerAddr2;
140  delayTab[3] += atoi((*sessionListIt)->getAttribute("id")) + bgpModule->par("ExternalPeerStartDelayOffset").doubleValue();
141  }
142 
143  if (peerAddr.isUnspecified())
144  throw cRuntimeError("BGP Error: No valid external address for session ID : %s", (*sessionListIt)->getAttribute("id"));
145 
146  SessionInfo externalInfo;
147 
148  externalInfo.myAddr = myAddr;
149  externalInfo.checkConnection = bgpModule->par("connectedCheck").boolValue();
150  externalInfo.ebgpMultihop = bgpModule->par("ebgpMultihop").intValue();
151  if (externalInfo.ebgpMultihop < 1)
152  throw cRuntimeError("BGP Error: ebgpMultihop parameter must be >= 1");
153  else if (externalInfo.ebgpMultihop > 1) // if E-BGP multi-hop is enabled, then turn off checkConnection
154  externalInfo.checkConnection = false;
155 
156  for (auto& elem : ASConfig) {
157  if (std::string(elem->getTagName()) == "Router") {
158  if (isInInterfaceTable(ift, Ipv4Address(elem->getAttribute("interAddr"))) != -1) {
159  for (auto& entry : elem->getChildren()) {
160  if (std::string(entry->getTagName()) == "Neighbor") {
161  const char *peer = entry->getAttribute("address");
162  if (peer && *peer && peerAddr.equals(Ipv4Address(peer))) {
163  externalInfo.checkConnection = getBoolAttrOrPar(*entry, "connectedCheck");
164  externalInfo.ebgpMultihop = getIntAttrOrPar(*entry, "ebgpMultihop");
165  if (externalInfo.ebgpMultihop > 1) // if E-BGP multi-hop is enabled, then turn off checkConnection
166  externalInfo.checkConnection = false;
167  }
168  }
169  }
170  }
171  }
172  }
173 
174  SessionId newSessionID = bgpRouter->createEbgpSession(peerAddr.str().c_str(), externalInfo);
175  bgpRouter->setTimer(newSessionID, delayTab);
176  bgpRouter->setSocketListen(newSessionID);
177  }
178 }

Referenced by loadConfigFromXML().

◆ loadTimerConfig()

void inet::bgp::BgpConfigReader::loadTimerConfig ( cXMLElementList &  timerConfig,
simtime_t *  delayTab 
)
private
79 {
80  for (auto& elem : timerConfig) {
81  std::string nodeName = (elem)->getTagName();
82  if (nodeName == "connectRetryTime") {
83  delayTab[0] = (double)atoi((elem)->getNodeValue());
84  }
85  else if (nodeName == "holdTime") {
86  delayTab[1] = (double)atoi((elem)->getNodeValue());
87  }
88  else if (nodeName == "keepAliveTime") {
89  delayTab[2] = (double)atoi((elem)->getNodeValue());
90  }
91  else if (nodeName == "startDelay") {
92  delayTab[3] = (double)atoi((elem)->getNodeValue());
93  }
94  }
95 }

Referenced by loadConfigFromXML().

Member Data Documentation

◆ bgpModule

cModule* inet::bgp::BgpConfigReader::bgpModule = nullptr
private

◆ bgpRouter

BgpRouter* inet::bgp::BgpConfigReader::bgpRouter = nullptr
private

◆ ift

IInterfaceTable* inet::bgp::BgpConfigReader::ift = nullptr
private

The documentation for this class was generated from the following files:
inet::bgp::SessionId
unsigned long SessionId
Definition: BgpCommon.h:52
inet::bgp::BgpRouter::setSocketListen
void setSocketListen(SessionId id)
Definition: BgpRouter.cc:143
inet::bgp::BgpRouter::setRedistributeOspf
void setRedistributeOspf(std::string x)
Definition: BgpRouter.cc:254
inet::bgp::BgpConfigReader::findInternalPeers
std::vector< const char * > findInternalPeers(cXMLElementList &ASConfig)
Definition: BgpConfigReader.cc:180
inet::bgp::BgpRouter::setInternalAddress
void setInternalAddress(Ipv4Address x)
Definition: BgpRouter.h:72
inet::bgp::BgpRouter::setRedistributeInternal
void setRedistributeInternal(bool x)
Definition: BgpRouter.h:74
inet::NetworkInterface::getProtocolData
const InterfaceProtocolData * getProtocolData(int index) const
Returns the protocol data at the given index.
Definition: NetworkInterface.h:287
inet::bgp::BgpConfigReader::getIntAttrOrPar
int getIntAttrOrPar(const cXMLElement &ifConfig, const char *name) const
Definition: BgpConfigReader.cc:309
inet::bgp::BgpConfigReader::getBoolAttrOrPar
bool getBoolAttrOrPar(const cXMLElement &ifConfig, const char *name) const
Definition: BgpConfigReader.cc:296
inet::bgp::BgpRouter::setDefaultConfig
void setDefaultConfig()
Definition: BgpRouter.cc:149
inet::bgp::BgpConfigReader::loadEbgpSessionConfig
void loadEbgpSessionConfig(cXMLElementList &ASConfig, cXMLElementList &sessionList, simtime_t *delayTab)
Definition: BgpConfigReader.cc:117
inet::bgp::BgpConfigReader::isInInterfaceTable
int isInInterfaceTable(IInterfaceTable *ifTable, Ipv4Address addr)
Definition: BgpConfigReader.cc:251
inet::bgp::BgpConfigReader::bgpModule
cModule * bgpModule
Definition: BgpConfigReader.h:24
inet::bgp::BgpRouter::setRedistributeRip
void setRedistributeRip(bool x)
Definition: BgpRouter.h:76
inet::bgp::BgpRouter::createIbgpSession
SessionId createIbgpSession(const char *peerAddr)
Definition: BgpRouter.cc:81
inet::bgp::BgpRouter::addToAsList
void addToAsList(std::string nodeName, AsId id)
Definition: BgpRouter.cc:212
inet::bgp::BgpRouter::setAsId
void setAsId(AsId myAsId)
Definition: BgpRouter.h:65
inet::bgp::BgpConfigReader::bgpRouter
BgpRouter * bgpRouter
Definition: BgpConfigReader.h:26
inet::bgp::BgpRouter::setTimer
void setTimer(SessionId id, simtime_t *delayTab)
Definition: BgpRouter.cc:138
inet::bgp::BgpConfigReader::calculateStartDelay
unsigned int calculateStartDelay(int rtListSize, unsigned char rtPosition, unsigned char rtPeerPosition)
Definition: BgpConfigReader.cc:271
inet::bgp::BgpConfigReader::findMyAS
AsId findMyAS(cXMLElementList &ASList, int &outRouterPosition)
Definition: BgpConfigReader.cc:97
inet::bgp::BgpRouter::addToAdvertiseList
void addToAdvertiseList(Ipv4Address address)
Definition: BgpRouter.cc:168
inet::bgp::BgpRouter::getAsId
AsId getAsId()
Definition: BgpRouter.h:66
inet::Ipv4Address::str
std::string str(bool printUnspec=true) const
Returns the string representation of the address (e.g.
Definition: Ipv4Address.cc:98
inet::bgp::BgpRouter::createEbgpSession
SessionId createEbgpSession(const char *peerAddr, SessionInfo &externalInfo)
Definition: BgpRouter.cc:103
inet::bgp::BgpRouter::setNextHopSelf
void setNextHopSelf(Ipv4Address peer, bool nextHopSelf)
Definition: BgpRouter.cc:226
inet::bgp::BgpConfigReader::loadASConfig
void loadASConfig(cXMLElementList &ASConfig)
Definition: BgpConfigReader.cc:196
inet::IInterfaceTable::getNumInterfaces
virtual int getNumInterfaces() const =0
Returns the number of interfaces.
inet::bgp::BgpConfigReader::ift
IInterfaceTable * ift
Definition: BgpConfigReader.h:25
inet::bgp::NB_TIMERS
const unsigned char NB_TIMERS
Definition: BgpCommon.h:27
inet::bgp::BgpRouter::getRouterId
RouterId getRouterId()
Definition: BgpRouter.h:64
inet::IInterfaceTable::getInterface
virtual NetworkInterface * getInterface(int pos) const =0
Returns the NetworkInterface specified by an index 0..numInterfaces-1.
inet::bgp::AsId
unsigned short AsId
Definition: BgpCommon.h:51
inet::bgp::BgpConfigReader::getStrAttrOrPar
const char * getStrAttrOrPar(const cXMLElement &ifConfig, const char *name) const
Definition: BgpConfigReader.cc:317
inet::bgp::BgpRouter::setLocalPreference
void setLocalPreference(Ipv4Address peer, int localPref)
Definition: BgpRouter.cc:240
inet::bgp::BgpConfigReader::loadTimerConfig
void loadTimerConfig(cXMLElementList &timerConfig, simtime_t *delayTab)
Definition: BgpConfigReader.cc:78
inet::bgp::BgpRouter::addToPrefixList
void addToPrefixList(std::string nodeName, BgpRoutingTableEntry *entry)
Definition: BgpRouter.cc:195