Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

MACRelayUnitBase Class Reference

#include <MACRelayUnitBase.h>

Inheritance diagram for MACRelayUnitBase:

MACRelayUnitNP MACRelayUnitPP List of all members.

Protected Types

typedef std::map< MACAddress,
AddressEntry, MAC_compare
AddressTable

Protected Methods

void initialize ()
void handleAndDispatchFrame (EtherFrame *frame, int inputport)
void broadcastFrame (EtherFrame *frame, int inputport)
void readAddressTable (const char *fileName)
void updateTableWithAddress (MACAddress &address, int portno)
int getPortForAddress (MACAddress &address)
void printAddressTable ()
void removeAgedEntriesFromTable ()
void removeOldestTableEntry ()
void sendPauseFrame (int portno, int pauseUnits)

Protected Attributes

int numPorts
int addressTableSize
simtime_t agingTime
AddressTable addresstable
int seqNum

Private Methods

 Module_Class_Members (MACRelayUnitBase, cSimpleModule, 0)

Detailed Description

Implements base switching functionality of Ethernet switches. Note that neither activity() nor handleMessage() is redefined here -- active behavior (incl. queueing and performance aspects) must be addressed in subclasses.

Definition at line 37 of file MACRelayUnitBase.h.


Member Typedef Documentation

typedef std::map<MACAddress, AddressEntry, MAC_compare> MACRelayUnitBase::AddressTable [protected]
 

Definition at line 55 of file MACRelayUnitBase.h.


Member Function Documentation

void MACRelayUnitBase::broadcastFrame EtherFrame   frame,
int    inputport
[protected]
 

Utility function: sends the frame on all ports except inputport. The message pointer should not be referenced any more after this call.

Definition at line 112 of file MACRelayUnitBase.cc.

References EtherFrame::dup(), and numPorts.

Referenced by handleAndDispatchFrame().

00113 {
00114     for (int i=0; i<numPorts; ++i)
00115         if (i!=inputport)
00116             send((EtherFrame*)frame->dup(), "lowerLayerOut", i);
00117     delete frame;
00118 }

int MACRelayUnitBase::getPortForAddress MACAddress   address [protected]
 

Returns output port for address, or -1 if unknown.

Definition at line 201 of file MACRelayUnitBase.cc.

References addresstable, agingTime, and EV.

Referenced by handleAndDispatchFrame().

00202 {
00203     AddressTable::iterator iter = addresstable.find(address);
00204     if (iter == addresstable.end())
00205     {
00206         // not found
00207         return -1;
00208     }
00209     if (iter->second.insertionTime + agingTime <= simTime())
00210     {
00211         // don't use (and throw out) aged entries
00212         EV << "Ignoring and deleting aged entry: "<< iter->first << " --> port" << iter->second.portno << "\n";
00213         addresstable.erase(iter);
00214         return -1;
00215     }
00216     return iter->second.portno;
00217 }

void MACRelayUnitBase::handleAndDispatchFrame EtherFrame   frame,
int    inputport
[protected]
 

Updates address table with source address, determines output port and sends out (or broadcasts) frame on ports. Includes calls to updateTableWithAddress() and getPortForAddress().

The message pointer should not be referenced any more after this call.

Definition at line 77 of file MACRelayUnitBase.cc.

References broadcastFrame(), EV, EtherFrame::getDest(), getPortForAddress(), EtherFrame::getSrc(), MACAddress::isBroadcast(), and updateTableWithAddress().

Referenced by MACRelayUnitPP::processFrame().

00078 {
00079     // update address table
00080     updateTableWithAddress(frame->getSrc(), inputport);
00081 
00082     // handle broadcast frames first
00083     if (frame->getDest().isBroadcast())
00084     {
00085         EV << "Broadcasting broadcast frame " << frame << endl;
00086         broadcastFrame(frame, inputport);
00087         return;
00088     }
00089 
00090     // Finds output port of destination address and sends to output port
00091     // if not found then broadcasts to all other ports instead
00092     int outputport = getPortForAddress(frame->getDest());
00093     if (inputport==outputport)
00094     {
00095         EV << "Output port is same as input port, " << frame->fullName() << 
00096               " dest " << frame->getDest() << ", discarding frame\n";
00097         delete frame;
00098         return;
00099     }
00100     if (outputport>=0)
00101     {
00102         EV << "Sending frame " << frame << " with dest address " << frame->getDest() << " to port " << outputport << endl;
00103         send(frame, "lowerLayerOut", outputport);
00104     }
00105     else
00106     {
00107         EV << "Dest address " << frame->getDest() << " unknown, broadcasting frame " << frame << endl;
00108         broadcastFrame(frame, inputport);
00109     }
00110 }

void MACRelayUnitBase::initialize   [protected]
 

Read parameters parameters.

Reimplemented in MACRelayUnitPP.

Definition at line 55 of file MACRelayUnitBase.cc.

References addressTableSize, agingTime, numPorts, readAddressTable(), and seqNum.

Referenced by MACRelayUnitPP::initialize().

00056 {
00057     // number of ports
00058     numPorts = gate("lowerLayerOut",0)->size();
00059     if (gate("lowerLayerIn",0)->size()!=numPorts)
00060         error("the sizes of the lowerLayerIn[] and lowerLayerOut[] gate vectors must be the same");
00061 
00062     // other parameters
00063     addressTableSize = par("addressTableSize");
00064     addressTableSize = addressTableSize >= 0 ? addressTableSize : 0;
00065 
00066     agingTime = par("agingTime");
00067     agingTime = agingTime > 0 ? agingTime : 10;
00068 
00069     // Option to pre-read in Address Table. To turn ot off, set addressTableFile to empty string
00070     const char *addressTableFile = par("addressTableFile");
00071     if (addressTableFile && *addressTableFile)
00072         readAddressTable(addressTableFile);
00073 
00074     seqNum = 0;
00075 }

MACRelayUnitBase::Module_Class_Members MACRelayUnitBase   ,
cSimpleModule   ,
 
[private]
 

void MACRelayUnitBase::printAddressTable   [protected]
 

Prints contents of address table on ev.

Definition at line 120 of file MACRelayUnitBase.cc.

References addresstable, agingTime, and EV.

Referenced by MACRelayUnitPP::processFrame().

00121 {
00122     AddressTable::iterator iter;
00123     EV << "Address Table (" << addresstable.size() << " entries):\n";
00124     for (iter = addresstable.begin(); iter!=addresstable.end(); ++iter)
00125     {
00126         EV << "  " << iter->first << " --> port" << iter->second.portno <<
00127               (iter->second.insertionTime+agingTime <= simTime() ? " (aged)" : "") << endl;
00128     }
00129 }

void MACRelayUnitBase::readAddressTable const char *    fileName [protected]
 

Pre-reads in entries for Address Table during initialization.

Definition at line 220 of file MACRelayUnitBase.cc.

References addresstable, fgetline(), MACRelayUnitBase::AddressEntry::insertionTime, and MACRelayUnitBase::AddressEntry::portno.

Referenced by initialize().

00221 {
00222     FILE *fp = fopen(fileName, "r");
00223     if (fp == NULL)
00224         error("cannot open address table file `%s'", fileName);
00225 
00226     //  Syntax of the file goes as:
00227     //  Address in hexadecimal representation, Portno
00228     //  ffffffff    1
00229     //  ffffeed1    2
00230     //  aabcdeff    3
00231     //
00232     //  etc...
00233     //
00234     //  Each iteration of the loop reads in an entire line i.e. up to '\n' or EOF characters
00235     //  and uses strtok to extract tokens from the resulting string
00236     char *line;
00237     int lineno = 0;
00238     while ((line = fgetline(fp)) != NULL)
00239     {
00240         lineno++;
00241 
00242         // lines beginning with '#' are treated as comments
00243         if (line[0]=='#')
00244             continue;
00245 
00246         // scan in hexaddress
00247         char *hexaddress = strtok(line, " \t");
00248         // scan in port number
00249         char *portno = strtok(NULL, " \t");
00250 
00251         // empty line?
00252         if (!hexaddress)
00253             continue;
00254 
00255         // broken line?
00256         if (!portno)
00257             error("line %d invalid in address table file `%s'", lineno, fileName);
00258 
00259         // Create an entry with address and portno and insert into table
00260         AddressEntry entry;
00261         entry.insertionTime = 0;
00262         entry.portno = atoi(portno);
00263         addresstable[MACAddress(hexaddress)] = entry;
00264 
00265         // Garbage collection before next iteration
00266         delete [] line;
00267     }
00268     fclose(fp);
00269 }

void MACRelayUnitBase::removeAgedEntriesFromTable   [protected]
 

Utility function: throws out all aged entries from table.

Definition at line 131 of file MACRelayUnitBase.cc.

References addresstable, agingTime, EV, and MACRelayUnitBase::AddressEntry::insertionTime.

Referenced by updateTableWithAddress().

00132 {
00133     for (AddressTable::iterator iter = addresstable.begin(); iter != addresstable.end();)
00134     {
00135         AddressTable::iterator cur = iter++; // iter will get invalidated after erase()
00136         AddressEntry& entry = cur->second;
00137         if (entry.insertionTime + agingTime <= simTime())
00138         {
00139             EV << "Removing aged entry from Address Table: " <<
00140                   cur->first << " --> port" << cur->second.portno << "\n";
00141             addresstable.erase(cur);
00142         }
00143     }
00144 }

void MACRelayUnitBase::removeOldestTableEntry   [protected]
 

Utility function: throws out oldest (not necessarily aged) entry from table.

Definition at line 146 of file MACRelayUnitBase.cc.

References addresstable, and EV.

Referenced by updateTableWithAddress().

00147 {
00148     AddressTable::iterator oldest = addresstable.end();
00149     simtime_t oldestInsertTime = simTime()+1;
00150     for (AddressTable::iterator iter = addresstable.begin(); iter != addresstable.end(); iter++)
00151     {
00152         if (iter->second.insertionTime < oldestInsertTime)
00153         {
00154             oldest = iter;
00155             oldestInsertTime = iter->second.insertionTime;
00156         }
00157     }
00158     if (oldest != addresstable.end())
00159     {
00160         EV << "Table full, removing oldest entry: " <<
00161               oldest->first << " --> port" << oldest->second.portno << "\n";
00162         addresstable.erase(oldest);
00163     }
00164 }

void MACRelayUnitBase::sendPauseFrame int    portno,
int    pauseUnits
[protected]
 

Utility function (for use by subclasses) to send a flow control PAUSE frame on the given port.

Definition at line 272 of file MACRelayUnitBase.cc.

References ETH_PAUSE, ETHER_MAC_FRAME_BYTES, ETHER_PAUSE_COMMAND_BYTES, EV, MIN_ETHERNET_FRAME, seqNum, and EtherPauseFrame::setPauseTime().

Referenced by MACRelayUnitPP::handleIncomingFrame().

00273 {
00274     EV << "Creating and sending PAUSE frame on port " << portno << " with duration=" << pauseUnits << " units\n";
00275 
00276     // create Ethernet frame
00277     char framename[40];
00278     sprintf(framename, "pause-%d-%d", id(), seqNum++);
00279     EtherPauseFrame *frame = new EtherPauseFrame(framename, ETH_PAUSE);
00280     frame->setPauseTime(pauseUnits);
00281 
00282     frame->setLength(8*(ETHER_MAC_FRAME_BYTES+ETHER_PAUSE_COMMAND_BYTES));
00283     if (frame->length() < 8*MIN_ETHERNET_FRAME)
00284         frame->setLength(8*MIN_ETHERNET_FRAME);
00285 
00286     send(frame, "lowerLayerOut", portno);
00287 }

void MACRelayUnitBase::updateTableWithAddress MACAddress   address,
int    portno
[protected]
 

Enters address into table.

Definition at line 166 of file MACRelayUnitBase.cc.

References addresstable, addressTableSize, EV, MACRelayUnitBase::AddressEntry::insertionTime, MACRelayUnitBase::AddressEntry::portno, removeAgedEntriesFromTable(), and removeOldestTableEntry().

Referenced by handleAndDispatchFrame().

00167 {
00168     AddressTable::iterator iter;
00169 
00170     iter = addresstable.find(address);
00171     if (iter == addresstable.end())
00172     {
00173         // Observe finite table size
00174         if (addressTableSize!=0 && addresstable.size() == (unsigned int)addressTableSize)
00175         {
00176             // lazy removal of aged entries: only if table gets full (this step is not strictly needed)
00177             EV << "Making room in Address Table by throwing out aged entries.\n";
00178             removeAgedEntriesFromTable();
00179 
00180             if (addresstable.size() == (unsigned int)addressTableSize)
00181                 removeOldestTableEntry();
00182         }
00183 
00184         // Add entry to table
00185         EV << "Adding entry to Address Table: "<< address << " --> port" << portno << "\n";
00186         AddressEntry entry;
00187         entry.portno = portno;
00188         entry.insertionTime = simTime();
00189         addresstable[address] = entry;
00190     }
00191     else
00192     {
00193         // Update existing entry
00194         EV << "Updating entry in Address Table: "<< address << " --> port" << portno << "\n";
00195         AddressEntry& entry = iter->second;
00196         entry.insertionTime = simTime();
00197         entry.portno = portno;
00198     }
00199 }


Member Data Documentation

AddressTable MACRelayUnitBase::addresstable [protected]
 

Definition at line 62 of file MACRelayUnitBase.h.

Referenced by getPortForAddress(), printAddressTable(), readAddressTable(), removeAgedEntriesFromTable(), removeOldestTableEntry(), and updateTableWithAddress().

int MACRelayUnitBase::addressTableSize [protected]
 

Definition at line 59 of file MACRelayUnitBase.h.

Referenced by MACRelayUnitPP::initialize(), initialize(), and updateTableWithAddress().

simtime_t MACRelayUnitBase::agingTime [protected]
 

Definition at line 60 of file MACRelayUnitBase.h.

Referenced by getPortForAddress(), MACRelayUnitPP::initialize(), initialize(), printAddressTable(), and removeAgedEntriesFromTable().

int MACRelayUnitBase::numPorts [protected]
 

Definition at line 58 of file MACRelayUnitBase.h.

Referenced by broadcastFrame(), MACRelayUnitPP::handleIncomingFrame(), MACRelayUnitPP::initialize(), and initialize().

int MACRelayUnitBase::seqNum [protected]
 

Definition at line 64 of file MACRelayUnitBase.h.

Referenced by initialize(), and sendPauseFrame().


The documentation for this class was generated from the following files:
Generated on Sat May 15 20:30:48 2004 for Ethernet by doxygen1.2.17