InetUnderlayConfigurator.cc

Go to the documentation of this file.
00001 //
00002 // This program is free software; you can redistribute it and/or
00003 // modify it under the terms of the GNU General Public License
00004 // as published by the Free Software Foundation; either version 2
00005 // of the License, or (at your option) any later version.
00006 //
00007 // This program is distributed in the hope that it will be useful,
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License
00013 // along with this program; if not, write to the Free Software
00014 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00015 //
00016 
00022 #include "InetUnderlayConfigurator.h"
00023 
00024 #include <GlobalNodeList.h>
00025 #include <TransportAddress.h>
00026 
00027 #include <AccessNet.h>
00028 #include <IRoutingTable.h>
00029 #include <IInterfaceTable.h>
00030 #include <IPAddressResolver.h>
00031 #include <IPv4InterfaceData.h>
00032 #include <NotificationBoard.h>
00033 
00034 
00035 #include <InetInfo.h>
00036 
00037 Define_Module(InetUnderlayConfigurator);
00038 
00039 void InetUnderlayConfigurator::initializeUnderlay(int stage)
00040 {
00041     //backbone configuration
00042     if (stage == MIN_STAGE_UNDERLAY) {
00043         // Find all router modules.
00044         cTopology topo("topo");
00045         topo.extractByProperty("node");
00046 
00047         // Assign IP addresses to all router modules.
00048         std::vector<uint32> nodeAddresses;
00049         nodeAddresses.resize(topo.getNumNodes());
00050 
00051         // IP addresses for backbone
00052         // Take start IP from config file
00053         // FIXME: Make Netmask for Routers configurable!
00054         uint32 lowIPBoundary = IPAddress(par("startIP").stringValue()).getInt();
00055 
00056         // uint32 lowIPBoundary = uint32((1 << 24) + 1);
00057         int numIPNodes = 0;
00058 
00059         for (int i = 0; i < topo.getNumNodes(); i++) {
00060             ++numIPNodes;
00061 
00062             uint32 addr = lowIPBoundary + uint32(numIPNodes << 16);
00063             nodeAddresses[i] = addr;
00064 
00065             // update ip display string
00066             if (ev.isGUI()) {
00067                 topo.getNode(i)->getModule()->getDisplayString().insertTag("t", 0);
00068                 topo.getNode(i)->getModule()->getDisplayString().setTagArg("t", 0,
00069                                         const_cast<char*>(IPAddress(addr).str().c_str()));
00070                 topo.getNode(i)->getModule()->getDisplayString().setTagArg("t", 1, "l");
00071                 topo.getNode(i)->getModule()->getDisplayString().setTagArg("t", 2, "red");
00072             }
00073 
00074             // find interface table and assign address to all (non-loopback) interfaces
00075             IInterfaceTable* ift = IPAddressResolver().interfaceTableOf(topo.getNode(i)->getModule());
00076 
00077             for ( int k = 0; k < ift->getNumInterfaces(); k++ ) {
00078                 InterfaceEntry* ie = ift->getInterface(k);
00079                 if (!ie->isLoopback()) {
00080                     ie->ipv4Data()->setIPAddress(IPAddress(addr));
00081                     // full address must match for local delivery
00082                     ie->ipv4Data()->setNetmask(IPAddress::ALLONES_ADDRESS);
00083                 }
00084             }
00085         }
00086 
00087         // Fill in routing tables.
00088         for (int i = 0; i < topo.getNumNodes(); i++) {
00089             cTopology::Node* destNode = topo.getNode(i);
00090             uint32 destAddr = nodeAddresses[i];
00091 
00092             // calculate shortest paths from everywhere towards destNode
00093             topo.calculateUnweightedSingleShortestPathsTo(destNode);
00094 
00095             // add overlayAccessRouters and overlayBackboneRouters
00096             // to the GlobalNodeList
00097             if ((strcmp(destNode->getModule()->getName(), "overlayBackboneRouter") == 0) ||
00098                     (strcmp(destNode->getModule()->getName(), "overlayAccessRouter") == 0)) {
00099                 //add node to bootstrap oracle
00100                 PeerInfo* info = new PeerInfo(0, destNode->getModule()->getId());
00101                 globalNodeList->addPeer(IPvXAddress(nodeAddresses[i]), info);
00102             }
00103 
00104 
00105             // If destNode is the outRouter, add a default route
00106             // to outside network via the TunOutDevice and a route to the
00107             // Gateway
00108             if ( strcmp(destNode->getModule()->getName(), "outRouter" ) == 0 ) {
00109                 IPRoute* defRoute = new IPRoute();
00110                 defRoute->setHost(IPAddress::UNSPECIFIED_ADDRESS);
00111                 defRoute->setNetmask(IPAddress::UNSPECIFIED_ADDRESS);
00112                 defRoute->setGateway(IPAddress(par("gatewayIP").stringValue()));
00113                 defRoute->setInterface(IPAddressResolver().interfaceTableOf(destNode->getModule())->getInterfaceByName("tunDev"));
00114                 defRoute->setType(IPRoute::REMOTE);
00115                 defRoute->setSource(IPRoute::MANUAL);
00116                 IPAddressResolver().routingTableOf(destNode->getModule())->addRoute(defRoute);
00117 
00118                 IPRoute* gwRoute = new IPRoute();
00119                 gwRoute->setHost(IPAddress(par("gatewayIP").stringValue()));
00120                 gwRoute->setNetmask(IPAddress(255, 255, 255, 255));
00121                 gwRoute->setInterface(IPAddressResolver().interfaceTableOf(destNode->getModule())->getInterfaceByName("tunDev"));
00122                 gwRoute->setType(IPRoute::DIRECT);
00123                 gwRoute->setSource(IPRoute::MANUAL);
00124                 IPAddressResolver().routingTableOf(destNode->getModule())->addRoute(gwRoute);
00125             }
00126 
00127             // add route (with host=destNode) to every routing table in the network
00128             for (int j = 0; j < topo.getNumNodes(); j++) {
00129                 // continue if same node
00130                 if (i == j)
00131                     continue;
00132 
00133                 // cancel simulation if node is not connected with destination
00134                 cTopology::Node* atNode = topo.getNode(j);
00135 
00136                 if (atNode->getNumPaths() == 0) {
00137                     error((std::string(atNode->getModule()->getName()) + ": Network is not entirely connected."
00138                             "Please increase your value for the "
00139                             "connectivity parameter").c_str());
00140                 }
00141 
00142                 //
00143                 // Add routes at the atNode.
00144                 //
00145 
00146                 // find atNode's interface and routing table
00147                 IInterfaceTable* ift = IPAddressResolver().interfaceTableOf(atNode->getModule());
00148                 IRoutingTable* rt = IPAddressResolver().routingTableOf(atNode->getModule());
00149 
00150                 // find atNode's interface entry for the next hop node
00151                 int outputGateId = atNode->getPath(0)->getLocalGate()->getId();
00152                 InterfaceEntry *ie = ift->getInterfaceByNodeOutputGateId(outputGateId);
00153 
00154                 // find the next hop node on the path towards destNode
00155                 cModule* next_hop = atNode->getPath(0)->getRemoteNode()->getModule();
00156                 IPAddress next_hop_ip = IPAddressResolver().addressOf(next_hop).get4();
00157 
00158 
00159                 // Requirement 1: Each router has exactly one routing entry
00160                 // (netmask 255.255.0.0) to each other router
00161                 IPRoute* re = new IPRoute();
00162 
00163                 re->setHost(IPAddress(destAddr));
00164                 re->setInterface(ie);
00165                 re->setSource(IPRoute::MANUAL);
00166                 re->setNetmask(IPAddress(255, 255, 0, 0));
00167                 re->setGateway(IPAddress(next_hop_ip));
00168                 re->setType(IPRoute::REMOTE);
00169 
00170                 rt->addRoute(re);
00171 
00172                 // Requirement 2: Each router has a point-to-point routing
00173                 // entry (netmask 255.255.255.255) for each immediate neighbour
00174                 if (atNode->getDistanceToTarget() == 1) {
00175                     IPRoute* re2 = new IPRoute();
00176 
00177                     re2->setHost(IPAddress(destAddr));
00178                     re2->setInterface(ie);
00179                     re2->setSource(IPRoute::MANUAL);
00180                     re2->setNetmask(IPAddress(255, 255, 255, 255));
00181                     re2->setType(IPRoute::DIRECT);
00182 
00183                     rt->addRoute(re2);
00184                 }
00185 
00186                 // If destNode is the outRouter, add a default route
00187                 // to the next hop in the direction of the outRouter
00188                 if (strcmp(destNode->getModule()->getName(), "outRouter" ) == 0) {
00189                     IPRoute* defRoute = new IPRoute();
00190                     defRoute->setHost(IPAddress::UNSPECIFIED_ADDRESS);
00191                     defRoute->setNetmask(IPAddress::UNSPECIFIED_ADDRESS);
00192                     defRoute->setGateway(IPAddress(next_hop_ip));
00193                     defRoute->setInterface(ie);
00194                     defRoute->setType(IPRoute::REMOTE);
00195                     defRoute->setSource(IPRoute::MANUAL);
00196 
00197                     rt->addRoute(defRoute);
00198                 }
00199             }
00200         }
00201     }
00202     //access net configuration
00203     else if(stage == MAX_STAGE_UNDERLAY) {
00204         // fetch some parameters
00205         accessRouterNum = getParentModule()->par("accessRouterNum");
00206         overlayAccessRouterNum = getParentModule()->par("overlayAccessRouterNum");
00207 
00208         // count the overlay clients
00209         overlayTerminalCount = 0;
00210 
00211         numCreated = 0;
00212         numKilled = 0;
00213 
00214         // add access node modules to access node vector
00215         // and assign the channel type to be used by the access node
00216         cModule* node;
00217         AccessNet* nodeAccess;
00218 
00219         for (int i = 0; i < accessRouterNum; i++) {
00220             node = getParentModule()->getSubmodule("accessRouter", i);
00221             accessNode.push_back( node );
00222             nodeAccess = check_and_cast<AccessNet*>( node->getSubmodule("accessNet") );
00223             nodeAccess->selectChannel( channelTypes[intuniform(0, channelTypes.size()-1)] );
00224         }
00225 
00226         for (int i = 0; i < overlayAccessRouterNum; i++) {
00227             node = getParentModule()->getSubmodule("overlayAccessRouter", i);
00228             accessNode.push_back( node );
00229             nodeAccess = check_and_cast<AccessNet*>( node->getSubmodule("accessNet") );
00230             nodeAccess->selectChannel( channelTypes[intuniform(0, channelTypes.size()-1)] );
00231         }
00232 
00233         // debug stuff
00234         WATCH_PTRVECTOR(accessNode);
00235     }
00236 }
00237 
00238 TransportAddress* InetUnderlayConfigurator::createNode(NodeType type, bool initialize)
00239 {
00240     Enter_Method_Silent();
00241     // derive overlay node from ned
00242     cModuleType* moduleType = cModuleType::get(type.terminalType.c_str());
00243     cModule* node = moduleType->create("overlayTerminal", getParentModule(),
00244                                        numCreated + 1, numCreated);
00245 
00246     node->par("overlayType").setStringValue(type.overlayType.c_str());
00247     node->par("tier1Type").setStringValue(type.tier1Type.c_str());
00248     node->par("tier2Type").setStringValue(type.tier2Type.c_str());
00249     node->par("tier3Type").setStringValue(type.tier3Type.c_str());
00250 
00251     node->setGateSize("pppg", 1);
00252 
00253     std::string displayString;
00254 
00255     if ((type.typeID > 1) && (type.typeID <= (NUM_COLORS + 1))) {
00256         ((displayString += "i=device/wifilaptop_l,")
00257                         += colorNames[type.typeID - 2])
00258                         += ",40;i2=block/circle_s";
00259     } else {
00260         displayString = "i=device/wifilaptop_l;i2=block/circle_s";
00261     }
00262 
00263     node->finalizeParameters();
00264     node->setDisplayString(displayString.c_str());
00265 
00266     node->buildInside();
00267     node->scheduleStart(simTime());
00268 
00269     // create meta information
00270     InetInfo* info = new InetInfo(type.typeID, node->getId());
00271     AccessNet* accessNet= check_and_cast<AccessNet*>
00272         (accessNode[intuniform(0, accessNode.size() - 1)]
00273                 ->getSubmodule("accessNet"));
00274     info->setAccessNetModule(accessNet);
00275     // add node to a randomly chosen access net
00276     info->setNodeID(accessNet->addOverlayNode(node));
00277 
00278     // add node to bootstrap oracle
00279     globalNodeList->addPeer(IPAddressResolver().addressOf(node), info);
00280 
00281     // if the node was not created during startup we have to
00282     // finish the initialization process manually
00283     if (!initialize) {
00284         for (int i = MAX_STAGE_UNDERLAY + 1; i < NUM_STAGES_ALL; i++) {
00285             node->callInitialize(i);
00286         }
00287     }
00288 
00289     overlayTerminalCount++;
00290     numCreated++;
00291 
00292     churnGenerator[type.typeID - 1]->terminalCount++;
00293 
00294     TransportAddress *address = new TransportAddress(
00295                                        IPAddressResolver().addressOf(node));
00296 
00297     // update display
00298     setDisplayString();
00299 
00300     return address;
00301 }
00302 
00303 //TODO: getRandomNode()
00304 void InetUnderlayConfigurator::preKillNode(NodeType type, TransportAddress* addr)
00305 {
00306     Enter_Method_Silent();
00307 
00308     AccessNet* accessNetModule = NULL;
00309     int nodeID;
00310     InetInfo* info;
00311 
00312     // If no address given, get random node
00313     if (addr == NULL) {
00314         addr = globalNodeList->getRandomAliveNode(type.typeID);
00315 
00316         if (addr == NULL) {
00317             // all nodes are already prekilled
00318             std::cout << "all nodes are already prekilled" << std::endl;
00319             return;
00320         }
00321     }
00322 
00323     // get node information
00324     info = dynamic_cast<InetInfo*>(globalNodeList->getPeerInfo(*addr));
00325 
00326     if (info != NULL) {
00327         accessNetModule = info->getAccessNetModule();
00328         nodeID = info->getNodeID();
00329     } else {
00330         opp_error("IPv4UnderlayConfigurator: Trying to pre kill node "
00331                   "with nonexistant TransportAddress!");
00332     }
00333 
00334     uint32_t effectiveType = info->getTypeID();
00335 
00336     // do not kill node that is already scheduled
00337     if(scheduledID.count(nodeID))
00338         return;
00339 
00340     cModule* node = accessNetModule->getOverlayNode(nodeID);
00341     globalNodeList->removePeer(IPAddressResolver().addressOf(node));
00342 
00343     //put node into the kill list and schedule a message for final removal of the node
00344     killList.push_front(IPAddressResolver().addressOf(node));
00345     scheduledID.insert(nodeID);
00346 
00347     overlayTerminalCount--;
00348     numKilled++;
00349 
00350     churnGenerator[effectiveType - 1]->terminalCount--;
00351 
00352     // update display
00353     setDisplayString();
00354 
00355     // inform the notification board about the removal
00356     NotificationBoard* nb = check_and_cast<NotificationBoard*>(
00357             node->getSubmodule("notificationBoard"));
00358     nb->fireChangeNotification(NF_OVERLAY_NODE_LEAVE);
00359 
00360     double random = uniform(0, 1);
00361 
00362     if (random < gracefulLeaveProbability) {
00363         nb->fireChangeNotification(NF_OVERLAY_NODE_GRACEFUL_LEAVE);
00364     }
00365 
00366     cMessage* msg = new cMessage();
00367     scheduleAt(simTime() + gracefulLeaveDelay, msg);
00368 
00369 }
00370 
00371 void InetUnderlayConfigurator::migrateNode(NodeType type, TransportAddress* addr)
00372 {
00373     Enter_Method_Silent();
00374 
00375     AccessNet* accessNetModule = NULL;
00376     int nodeID = -1;
00377     InetInfo* info;
00378 
00379     // If no address given, get random node
00380     if(addr == NULL) {
00381         info = dynamic_cast<InetInfo*>(globalNodeList->getRandomPeerInfo(type.typeID));
00382     } else {
00383         // get node information
00384         info = dynamic_cast<InetInfo*>(globalNodeList->getPeerInfo(*addr));
00385     }
00386 
00387     if(info != NULL) {
00388         accessNetModule = info->getAccessNetModule();
00389         nodeID = info->getNodeID();
00390     } else {
00391         opp_error("IPv4UnderlayConfigurator: Trying to pre kill node with nonexistant TransportAddress!");
00392     }
00393 
00394     // do not migrate node that is already scheduled
00395     if(scheduledID.count(nodeID))
00396         return;
00397 
00398     cModule* node = accessNetModule->removeOverlayNode(nodeID);//intuniform(0, accessNetModule->size() - 1));
00399 
00400     if(node == NULL)
00401         opp_error("IPv4UnderlayConfigurator: Trying to remove node which is nonexistant in AccessNet!");
00402 
00403     //remove node from bootstrap oracle
00404     globalNodeList->killPeer(IPAddressResolver().addressOf(node));
00405 
00406     node->bubble("I am migrating!");
00407 
00408     // connect the node to another access net
00409     AccessNet* newAccessNetModule;
00410 
00411     do {
00412         newAccessNetModule = check_and_cast<AccessNet*>(accessNode[intuniform(0, accessNode.size() - 1)]->getSubmodule("accessNet"));
00413     } while((newAccessNetModule == accessNetModule) && (accessNode.size() != 1));
00414 
00415     // create meta information
00416     InetInfo* newinfo = new InetInfo(type.typeID, node->getId());
00417     // add node to a randomly chosen access net
00418     newinfo->setAccessNetModule(newAccessNetModule);
00419     newinfo->setNodeID(newAccessNetModule->addOverlayNode(node, true));
00420 
00421     //add node to bootstrap oracle
00422     globalNodeList->addPeer(IPAddressResolver().addressOf(node), newinfo);
00423 
00424     // inform the notification board about the migration
00425     NotificationBoard* nb = check_and_cast<NotificationBoard*>(node->getSubmodule("notificationBoard"));
00426     nb->fireChangeNotification(NF_OVERLAY_TRANSPORTADDRESS_CHANGED);
00427 }
00428 
00429 void InetUnderlayConfigurator::handleTimerEvent(cMessage* msg)
00430 {
00431     Enter_Method_Silent();
00432 
00433     // get next scheduled node from the kill list
00434     IPvXAddress addr = killList.back();
00435     killList.pop_back();
00436 
00437     AccessNet* accessNetModule = NULL;
00438     int nodeID = -1;
00439 
00440     InetInfo* info = dynamic_cast<InetInfo*>(globalNodeList->getPeerInfo(addr));
00441     if(info != NULL) {
00442         accessNetModule = info->getAccessNetModule();
00443         nodeID = info->getNodeID();
00444     } else {
00445         opp_error("IPv4UnderlayConfigurator: Trying to kill node with nonexistant TransportAddress!");
00446     }
00447 
00448     scheduledID.erase(nodeID);
00449     globalNodeList->killPeer(addr);
00450 
00451     cModule* node = accessNetModule->removeOverlayNode(nodeID);
00452 
00453     if(node == NULL)
00454         opp_error("IPv4UnderlayConfigurator: Trying to remove node which is nonexistant in AccessNet!");
00455 
00456     node->callFinish();
00457     node->deleteModule();
00458 
00459     delete msg;
00460 }
00461 
00462 void InetUnderlayConfigurator::setDisplayString()
00463 {
00464     char buf[80];
00465     sprintf(buf, "%i overlay terminals\n%i access router\n%i overlay access router",
00466             overlayTerminalCount, accessRouterNum, overlayAccessRouterNum);
00467     getDisplayString().setTagArg("t", 0, buf);
00468 }
00469 
00470 void InetUnderlayConfigurator::finishUnderlay()
00471 {
00472     // statistics
00473     recordScalar("Terminals added", numCreated);
00474     recordScalar("Terminals removed", numKilled);
00475 
00476     if (!isInInitPhase()) {
00477         struct timeval now, diff;
00478         gettimeofday(&now, NULL);
00479         timersub(&now, &initFinishedTime, &diff);
00480         printf("Simulation time: %li.%06li\n", diff.tv_sec, diff.tv_usec);
00481     }
00482 }

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5