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

This module handles the mapping between ports and MAC addresses. More...

#include <MACAddressTable.h>

Inheritance diagram for inet::MACAddressTable:
inet::IMACAddressTable

Classes

struct  AddressEntry
 
struct  MAC_compare
 

Public Member Functions

 MACAddressTable ()
 
 ~MACAddressTable ()
 
virtual int getPortForAddress (MACAddress &address, unsigned int vid=0) override
 For a known arriving port, V-TAG and destination MAC. More...
 
virtual bool updateTableWithAddress (int portno, MACAddress &address, unsigned int vid=0) override
 Register a new MAC address at AddressTable. More...
 
virtual void flush (int portno) override
 Clears portno cache. More...
 
virtual void printState () override
 Prints cached data. More...
 
virtual void copyTable (int portA, int portB) override
 Copy cache from portA to portB port. More...
 
virtual void removeAgedEntriesFromVlan (unsigned int vid=0) override
 Remove aged entries from a specified VLAN. More...
 
virtual void removeAgedEntriesFromAllVlans () override
 Remove aged entries from all VLANs. More...
 
virtual void removeAgedEntriesIfNeeded () override
 
virtual void readAddressTable (const char *fileName) override
 Pre-reads in entries for Address Table during initialization. More...
 
virtual void clearTable () override
 For lifecycle: clears all entries from the vlanAddressTable. More...
 
virtual void setAgingTime (simtime_t agingTime) override
 
virtual void resetDefaultAging () override
 

Protected Types

typedef std::map< MACAddress, AddressEntry, MAC_compareAddressTable
 
typedef std::map< unsigned int, AddressTable * > VlanAddressTable
 

Protected Member Functions

virtual void initialize () override
 
virtual void handleMessage (cMessage *msg) override
 
AddressTablegetTableForVid (unsigned int vid)
 Returns a MAC Address Table for a specified VLAN ID. More...
 

Protected Attributes

simtime_t agingTime
 
simtime_t lastPurge
 
AddressTableaddressTable = nullptr
 
VlanAddressTable vlanAddressTable
 

Friends

std::ostream & operator<< (std::ostream &os, const AddressEntry &entry)
 

Detailed Description

This module handles the mapping between ports and MAC addresses.

See the NED definition for details.

Member Typedef Documentation

typedef std::map<unsigned int, AddressTable *> inet::MACAddressTable::VlanAddressTable
protected

Constructor & Destructor Documentation

inet::MACAddressTable::MACAddressTable ( )
33 {
34  addressTable = new AddressTable();
35  // Set addressTable for VLAN ID 0
37 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
AddressTable * addressTable
Definition: MACAddressTable.h:53
inet::MACAddressTable::~MACAddressTable ( )
326 {
327  for (auto & elem : vlanAddressTable)
328  delete elem.second;
329 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54

Member Function Documentation

void inet::MACAddressTable::clearTable ( )
overridevirtual

For lifecycle: clears all entries from the vlanAddressTable.

Implements inet::IMACAddressTable.

317 {
318  for (auto & elem : vlanAddressTable)
319  delete elem.second;
320 
321  vlanAddressTable.clear();
322  addressTable = nullptr;
323 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
AddressTable * addressTable
Definition: MACAddressTable.h:53
void inet::MACAddressTable::copyTable ( int  portA,
int  portB 
)
overridevirtual

Copy cache from portA to portB port.

Implements inet::IMACAddressTable.

209 {
210  for (auto & elem : vlanAddressTable) {
211  AddressTable *table = elem.second;
212  for (auto & table_j : *table)
213  if (table_j.second.portno == portA)
214  table_j.second.portno = portB;
215 
216  }
217 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
void inet::MACAddressTable::flush ( int  portno)
overridevirtual

Clears portno cache.

Implements inet::IMACAddressTable.

181 {
182  Enter_Method("MACAddressTable::flush(): Clearing gate %d cache", portno);
183  for (auto & elem : vlanAddressTable) {
184  AddressTable *table = elem.second;
185  for (auto j = table->begin(); j != table->end(); ) {
186  auto cur = j++;
187  if (cur->second.portno == portno)
188  table->erase(cur);
189  }
190  }
191 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
int inet::MACAddressTable::getPortForAddress ( MACAddress address,
unsigned int  vid = 0 
)
overridevirtual

For a known arriving port, V-TAG and destination MAC.

It finds out the port where relay component should deliver the message

Parameters
addressMAC destination
vidVLAN ID
Returns
Output port for address, or -1 if unknown.

Implements inet::IMACAddressTable.

107 {
108  Enter_Method("MACAddressTable::getPortForAddress()");
109 
110  AddressTable *table = getTableForVid(vid);
111  // VLAN ID vid does not exist
112  if (table == nullptr)
113  return -1;
114 
115  auto iter = table->find(address);
116 
117  if (iter == table->end()) {
118  // not found
119  return -1;
120  }
121  if (iter->second.insertionTime + agingTime <= simTime()) {
122  // don't use (and throw out) aged entries
123  EV << "Ignoring and deleting aged entry: " << iter->first << " --> port" << iter->second.portno << "\n";
124  table->erase(iter);
125  return -1;
126  }
127  return iter->second.portno;
128 }
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
AddressTable * getTableForVid(unsigned int vid)
Returns a MAC Address Table for a specified VLAN ID.
Definition: MACAddressTable.cc:89
simtime_t agingTime
Definition: MACAddressTable.h:51
MACAddressTable::AddressTable * inet::MACAddressTable::getTableForVid ( unsigned int  vid)
protected

Returns a MAC Address Table for a specified VLAN ID.

Referenced by getPortForAddress(), readAddressTable(), removeAgedEntriesFromVlan(), and updateTableWithAddress().

90 {
91  if (vid == 0)
92  return addressTable;
93 
94  auto iter = vlanAddressTable.find(vid);
95  if (iter != vlanAddressTable.end())
96  return iter->second;
97  return nullptr;
98 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
AddressTable * addressTable
Definition: MACAddressTable.h:53
void inet::MACAddressTable::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
79 {
80  throw cRuntimeError("This module doesn't process messages");
81 }
void inet::MACAddressTable::initialize ( )
overrideprotectedvirtual
40 {
41  agingTime = par("agingTime");
42  lastPurge = SIMTIME_ZERO;
43 
44  // Option to pre-read in Address Table. To turn it off, set addressTableFile to empty string
45  const char *addressTableFile = par("addressTableFile");
46  if (addressTableFile && *addressTableFile)
47  readAddressTable(addressTableFile);
48 
49  AddressTable& addressTable = *this->addressTable; // magic to hide the '*' from the name of the watch below
50  WATCH_MAP(addressTable);
51 }
virtual void readAddressTable(const char *fileName) override
Pre-reads in entries for Address Table during initialization.
Definition: MACAddressTable.cc:263
simtime_t lastPurge
Definition: MACAddressTable.h:52
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
simtime_t agingTime
Definition: MACAddressTable.h:51
AddressTable * addressTable
Definition: MACAddressTable.h:53
void inet::MACAddressTable::printState ( )
overridevirtual

Prints cached data.

Implements inet::IMACAddressTable.

198 {
199  EV << endl << "MAC Address Table" << endl;
200  EV << "VLAN ID MAC Port Inserted" << endl;
201  for (auto & elem : vlanAddressTable) {
202  AddressTable *table = elem.second;
203  for (auto & table_j : *table)
204  EV << table_j.second.vid << " " << table_j.first << " " << table_j.second.portno << " " << table_j.second.insertionTime << endl;
205  }
206 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
void inet::MACAddressTable::readAddressTable ( const char *  fileName)
overridevirtual

Pre-reads in entries for Address Table during initialization.

Implements inet::IMACAddressTable.

Referenced by initialize().

264 {
265  FILE *fp = fopen(fileName, "r");
266  if (fp == nullptr)
267  throw cRuntimeError("cannot open address table file `%s'", fileName);
268 
269  // Syntax of the file goes as:
270  // VLAN ID, address in hexadecimal representation, portno
271  // 1 ffffffff 1
272  // 1 ffffeed1 2
273  // 2 aabcdeff 3
274  //
275  // etc...
276  //
277  // Each iteration of the loop reads in an entire line i.e. up to '\n' or EOF characters
278  // and uses strtok to extract tokens from the resulting string
279  char *line;
280  for (int lineno = 0; (line = fgetline(fp)) != nullptr; delete [] line) {
281  lineno++;
282 
283  // lines beginning with '#' are treated as comments
284  if (line[0] == '#')
285  continue;
286 
287  // scan in VLAN ID
288  char *vlanID = strtok(line, " \t");
289  // scan in hexaddress
290  char *hexaddress = strtok(nullptr, " \t");
291  // scan in port number
292  char *portno = strtok(nullptr, " \t");
293 
294  // empty line?
295  if (!vlanID)
296  continue;
297 
298  // broken line?
299  if (!portno || !hexaddress)
300  throw cRuntimeError("line %d invalid in address table file `%s'", lineno, fileName);
301 
302  // Create an entry with address and portno and insert into table
303  AddressEntry entry(atoi(vlanID), atoi(portno), 0);
304  AddressTable *table = getTableForVid(entry.vid);
305 
306  if (table == nullptr) {
307  table = new AddressTable();
308  vlanAddressTable[entry.vid] = table;
309  }
310 
311  (*table)[MACAddress(hexaddress)] = entry;
312  }
313  fclose(fp);
314 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
AddressTable * getTableForVid(unsigned int vid)
Returns a MAC Address Table for a specified VLAN ID.
Definition: MACAddressTable.cc:89
void inet::MACAddressTable::removeAgedEntriesFromAllVlans ( )
overridevirtual

Remove aged entries from all VLANs.

Implements inet::IMACAddressTable.

Referenced by removeAgedEntriesIfNeeded().

237 {
238  for (auto & elem : vlanAddressTable) {
239  AddressTable *table = elem.second;
240  // TODO: this part could be factored out
241  for (auto j = table->begin(); j != table->end(); ) {
242  auto cur = j++; // iter will get invalidated after erase()
243  AddressEntry& entry = cur->second;
244  if (entry.insertionTime + agingTime <= simTime()) {
245  EV << "Removing aged entry from Address Table: "
246  << cur->first << " --> port" << cur->second.portno << "\n";
247  table->erase(cur);
248  }
249  }
250  }
251 }
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
simtime_t agingTime
Definition: MACAddressTable.h:51
void inet::MACAddressTable::removeAgedEntriesFromVlan ( unsigned int  vid = 0)
overridevirtual

Remove aged entries from a specified VLAN.

Implements inet::IMACAddressTable.

220 {
221  AddressTable *table = getTableForVid(vid);
222  if (table == nullptr)
223  return;
224  // TODO: this part could be factored out
225  for (auto iter = table->begin(); iter != table->end(); ) {
226  auto cur = iter++; // iter will get invalidated after erase()
227  AddressEntry& entry = cur->second;
228  if (entry.insertionTime + agingTime <= simTime()) {
229  EV << "Removing aged entry from Address Table: "
230  << cur->first << " --> port" << cur->second.portno << "\n";
231  table->erase(cur);
232  }
233  }
234 }
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
AddressTable * getTableForVid(unsigned int vid)
Returns a MAC Address Table for a specified VLAN ID.
Definition: MACAddressTable.cc:89
simtime_t agingTime
Definition: MACAddressTable.h:51
void inet::MACAddressTable::removeAgedEntriesIfNeeded ( )
overridevirtual

Implements inet::IMACAddressTable.

Referenced by updateTableWithAddress().

254 {
255  simtime_t now = simTime();
256 
257  if (now >= lastPurge + 1)
259 
260  lastPurge = simTime();
261 }
simtime_t lastPurge
Definition: MACAddressTable.h:52
virtual void removeAgedEntriesFromAllVlans() override
Remove aged entries from all VLANs.
Definition: MACAddressTable.cc:236
void inet::MACAddressTable::resetDefaultAging ( )
overridevirtual

Implements inet::IMACAddressTable.

337 {
338  agingTime = par("agingTime");
339 }
simtime_t agingTime
Definition: MACAddressTable.h:51
void inet::MACAddressTable::setAgingTime ( simtime_t  agingTime)
overridevirtual

Implements inet::IMACAddressTable.

332 {
333  this->agingTime = agingTime;
334 }
simtime_t agingTime
Definition: MACAddressTable.h:51
bool inet::MACAddressTable::updateTableWithAddress ( int  portno,
MACAddress address,
unsigned int  vid = 0 
)
overridevirtual

Register a new MAC address at AddressTable.

Returns
True if refreshed. False if it is new.

Implements inet::IMACAddressTable.

136 {
137  Enter_Method("MACAddressTable::updateTableWithAddress()");
138  if (address.isBroadcast())
139  return false;
140 
141  AddressTable::iterator iter;
142  AddressTable *table = getTableForVid(vid);
143 
144  if (table == nullptr) {
145  // MAC Address Table does not exist for VLAN ID vid, so we create it
146  table = new AddressTable();
147 
148  // set 'the addressTable' to VLAN ID 0
149  if (vid == 0)
150  addressTable = table;
151 
152  vlanAddressTable[vid] = table;
153  iter = table->end();
154  }
155  else
156  iter = table->find(address);
157 
158  if (iter == table->end()) {
160 
161  // Add entry to table
162  EV << "Adding entry to Address Table: " << address << " --> port" << portno << "\n";
163  (*table)[address] = AddressEntry(vid, portno, simTime());
164  return false;
165  }
166  else {
167  // Update existing entry
168  EV << "Updating entry in Address Table: " << address << " --> port" << portno << "\n";
169  AddressEntry& entry = iter->second;
170  entry.insertionTime = simTime();
171  entry.portno = portno;
172  }
173  return true;
174 }
virtual void removeAgedEntriesIfNeeded() override
Definition: MACAddressTable.cc:253
VlanAddressTable vlanAddressTable
Definition: MACAddressTable.h:54
std::map< MACAddress, AddressEntry, MAC_compare > AddressTable
Definition: MACAddressTable.h:48
AddressTable * getTableForVid(unsigned int vid)
Returns a MAC Address Table for a specified VLAN ID.
Definition: MACAddressTable.cc:89
AddressTable * addressTable
Definition: MACAddressTable.h:53

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const AddressEntry entry 
)
friend
27 {
28  os << "{VID=" << entry.vid << ", port=" << entry.portno << ", insertionTime=" << entry.insertionTime << "}";
29  return os;
30 }

Member Data Documentation

AddressTable* inet::MACAddressTable::addressTable = nullptr
protected
simtime_t inet::MACAddressTable::agingTime
protected
simtime_t inet::MACAddressTable::lastPurge
protected

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