|
INET Framework for OMNeT++/OMNEST
|
00001 00002 #include "vlanTable.h" 00003 00004 #include "vlanTableXMLparser.h" 00005 00006 00007 00008 Define_Module(VLANTable); 00009 00010 VLANTable::VLANTable() { 00011 return; 00012 } 00013 VLANTable::~VLANTable() { 00014 return; 00015 } 00016 00017 /* --- PUBLIC --- */ 00018 00019 /* MGMT */ 00020 const VLANTable::VIDTable * VLANTable::getTaggedTable() { 00021 return &vidTable; 00022 } 00023 const VLANTable::PortVIDTable * VLANTable::getUntaggedTable(){ 00024 return &portVIDTable; 00025 } 00026 00027 void VLANTable::initDefault() { 00028 regVLAN(1); 00029 extendTable(1); 00030 for (unsigned int i = 0; i < (unsigned int)portCount; i++) { 00031 addPortVID(i,1); 00032 } 00033 } 00034 00035 /* PUBLIC ACCESS METHODS */ 00036 VLANTable::tVIDPortList VLANTable::getPorts(int VID) { 00037 if (vidTable.size() < VID+1) { 00038 return empty; 00039 } 00040 if (vidTable.at(VID).VID != VID) {// VLAN is not active 00041 return empty; 00042 } 00043 return vidTable.at(VID).portList; 00044 } 00045 00046 int VLANTable::getVID(int Port) { 00047 if (Port < 0) { 00048 error("negative port number"); 00049 simulation.endRun(); 00050 } 00051 if (Port > portCount) { 00052 error("Portnumber is exceeds port count in getVID"); 00053 simulation.endRun(); 00054 } 00055 return portVIDTable.at(Port).VID; 00056 } 00057 00058 bool VLANTable::isAllowed(int VID, int _port) { 00059 if (vidTable.size() < VID+1) { 00060 return false; 00061 } 00062 tVIDRecord tmp = vidTable.at(VID); 00063 if (tmp.VID != VID) { // VLAN is not active 00064 return false; 00065 } 00066 00067 for (unsigned int i = 0; i < tmp.portList.size(); i++) { 00068 if (tmp.portList.at(i).port == _port) { 00069 return true; 00070 } 00071 } 00072 00073 return false; 00074 } 00075 00076 VLANTable::tTagAction VLANTable::getTag(int VID, int _port) { 00077 tVIDRecord tmp = vidTable.at(VID); 00078 if (tmp.VID != VID) { // VLAN is not active 00079 error("query for includeTag on non active vlan"); 00080 simulation.endRun(); 00081 return NONE; 00082 } 00083 00084 for (unsigned int i = 0; i < tmp.portList.size(); i++) { 00085 if (tmp.portList.at(i).port == _port) { 00086 return tmp.portList.at(i).action; 00087 } 00088 } 00089 00090 error("query for includeTag on non existing (VID, PORT)"); 00091 simulation.endRun(); 00092 return NONE; 00093 } 00094 00095 /* --- PRIVATE --- */ 00096 00097 void VLANTable::add(int VID, tVIDPortList& _portList) { 00098 vidTable.at(VID).VID = VID; 00099 vidTable.at(VID).portList = _portList; 00100 } 00101 00102 void VLANTable::addTagged(int VID, std::vector<int>& ports) { 00103 tVIDPortList& currentList = vidTable.at(VID).portList; 00104 vidTable.at(VID).VID = VID; 00105 00106 tVIDPort tmpPort; 00107 tmpPort.action = INCLUDE; 00108 00109 unsigned int i; 00110 unsigned int c; 00111 for(i = 0; i < ports.size(); i++) { // for all input port number 00112 for(c = 0; c < currentList.size(); c++) { // search through whole port vector 00113 if (currentList.at(c).port == ports.at(i)) { // if port number match 00114 currentList.at(c).action = INCLUDE; // modify action 00115 break; 00116 } 00117 } 00118 if (c == currentList.size()) { // if port record not found 00119 tmpPort.port = ports.at(i); // set temporary port number 00120 currentList.push_back(tmpPort); // insert new 00121 } 00122 00123 } 00124 } 00125 00126 void VLANTable::addUntagged(int VID, std::vector<int>& ports) { 00127 tVIDPortList& currentList = vidTable.at(VID).portList; 00128 vidTable.at(VID).VID = VID; 00129 00130 tVIDPort tmpPort; 00131 tmpPort.action = REMOVE; 00132 00133 unsigned int i; 00134 unsigned int c; 00135 for(i = 0; i < ports.size(); i++) { // for all input port number 00136 for(c = 0; c < currentList.size(); c++) { // search through whole port vector 00137 if (currentList.at(c).port == ports.at(i)) { // if port number match 00138 currentList.at(c).action = REMOVE; // modify action 00139 break; 00140 } 00141 } 00142 if (c == currentList.size()) { // if port record not found 00143 tmpPort.port = ports.at(i); // set temporary port number 00144 currentList.push_back(tmpPort); // insert new 00145 } 00146 00147 } 00148 } 00149 00150 void VLANTable::setVLANName(int _VID, std::string& _name) { 00151 vidTable.at(_VID).name = _name; 00152 } 00153 00154 void VLANTable::addPortVID(int _port, int _VID) { 00155 portVIDTable.at(_port).port = _port; 00156 portVIDTable.at(_port).VID = _VID; 00157 00158 std::vector<int> tmp; 00159 tmp.push_back(_port); 00160 extendTable(_VID); 00161 addUntagged(_VID, tmp); 00162 00163 } 00164 00165 void VLANTable::setPortVID(int _port, int _VID) { 00166 portVIDTable.at(_port).port = _port; 00167 portVIDTable.at(_port).VID = _VID; 00168 } 00169 00170 void VLANTable::delPort(int _port, int _VID) { 00171 tVIDPortList::iterator it; 00172 for (it = vidTable.at(_VID).portList.begin(); it != vidTable.at(_VID).portList.end(); it++) { 00173 if (it->port == _port) { 00174 vidTable.at(_VID).portList.erase(it); 00175 return; 00176 } 00177 } 00178 } 00179 00180 00181 /* --- PROTECTED --- */ 00182 00183 void VLANTable::initialize(int stage) { 00184 if (stage == 0) { 00185 portCount = par("portCount"); //TODO from CFG/simulation 00186 00187 emptyVID.VID = 0; 00188 00189 tPortVIDRecord empty2; 00190 empty2.port = 0; 00191 empty2.VID = 0; 00192 00193 //vidTable.insert(vidTable.begin(), VLANCOUNT, empty); 00194 portVIDTable.insert(portVIDTable.begin(), portCount, empty2); 00195 00196 00197 00198 const char *filename = par("configFile"); 00199 const char *switchID = par("switchID"); 00200 00201 if (*filename == '\0' || *switchID == '\0') { 00202 EV << "Warning: " << this->getParentModule()->getName() << ": Could not config, config filename or switchID is not set, using Default." << std::endl; 00203 initDefault(); 00204 } else { 00205 VLANTableXMLparser config(this); 00206 config.parse(filename, switchID); 00207 } 00208 00209 WATCH(portCount); 00210 WATCH_VECTOR(vidTable); 00211 WATCH_VECTOR(portVIDTable); 00212 } 00213 00214 } 00215 00216 void VLANTable::finish() { 00217 00218 } 00219 00220 void VLANTable::regVLAN(unsigned int _vlan) { 00221 for (unsigned int i = 0; i < vlanList.size(); i++) { 00222 if (vlanList.at(i) == _vlan) { 00223 return; 00224 } 00225 } 00226 vlanList.push_back(_vlan); 00227 } 00228 std::vector<unsigned int> VLANTable::getVLANList() { 00229 return vlanList; 00230 } 00231 00232 void VLANTable::extendTable(int VLAN) { 00233 00234 EV <<"KECYYYYYY !!!!!!!!!!!!!!!!!!" << "\n"; 00235 while (vidTable.size() < (unsigned int) VLAN+2) { // +1 is for index compensation 00236 vidTable.push_back(emptyVID); 00237 00238 } 00239 EV << VLAN << "/" << vidTable.size() << "\n"; 00240 } 00241 00242