#include <FlatNetworkConfigurator.h>
For more info please see the NED file.
Protected Types | |
typedef std::vector< NodeInfo > | NodeInfoVector |
typedef std::vector< std::string > | StringVector |
Protected Member Functions | |
virtual int | numInitStages () const |
virtual void | initialize (int stage) |
virtual void | handleMessage (cMessage *msg) |
void | extractTopology (cTopology &topo, NodeInfoVector &nodeInfo) |
void | assignAddresses (cTopology &topo, NodeInfoVector &nodeInfo) |
void | addDefaultRoutes (cTopology &topo, NodeInfoVector &nodeInfo) |
void | fillRoutingTables (cTopology &topo, NodeInfoVector &nodeInfo) |
void | setDisplayString (cTopology &topo, NodeInfoVector &nodeInfo) |
Classes | |
struct | NodeInfo |
typedef std::vector<NodeInfo> FlatNetworkConfigurator::NodeInfoVector [protected] |
typedef std::vector<std::string> FlatNetworkConfigurator::StringVector [protected] |
void FlatNetworkConfigurator::addDefaultRoutes | ( | cTopology & | topo, | |
NodeInfoVector & | nodeInfo | |||
) | [protected] |
00120 { 00121 // add default route to nodes with exactly one (non-loopback) interface 00122 for (int i=0; i<topo.nodes(); i++) 00123 { 00124 cTopology::Node *node = topo.node(i); 00125 00126 // skip bus types 00127 if (!nodeInfo[i].isIPNode) 00128 continue; 00129 00130 InterfaceTable *ift = nodeInfo[i].ift; 00131 RoutingTable *rt = nodeInfo[i].rt; 00132 00133 // count non-loopback interfaces 00134 int numIntf = 0; 00135 InterfaceEntry *ie = NULL; 00136 for (int k=0; k<ift->numInterfaces(); k++) 00137 if (!ift->interfaceAt(k)->isLoopback()) 00138 {ie = ift->interfaceAt(k); numIntf++;} 00139 00140 nodeInfo[i].usesDefaultRoute = (numIntf==1); 00141 if (numIntf!=1) 00142 continue; // only deal with nodes with one interface plus loopback 00143 00144 EV << " " << node->module()->fullName() << "=" << nodeInfo[i].address 00145 << " has only one (non-loopback) interface, adding default route\n"; 00146 00147 // add route 00148 RoutingEntry *e = new RoutingEntry(); 00149 e->host = IPAddress(); 00150 e->netmask = IPAddress(); 00151 e->interfaceName = ie->name(); 00152 e->interfacePtr = ie; 00153 e->type = RoutingEntry::REMOTE; 00154 e->source = RoutingEntry::MANUAL; 00155 //e->metric() = 1; 00156 rt->addRoutingEntry(e); 00157 } 00158 }
void FlatNetworkConfigurator::assignAddresses | ( | cTopology & | topo, | |
NodeInfoVector & | nodeInfo | |||
) | [protected] |
00087 { 00088 // assign IP addresses 00089 uint32 networkAddress = IPAddress(par("networkAddress").stringValue()).getInt(); 00090 uint32 netmask = IPAddress(par("netmask").stringValue()).getInt(); 00091 int maxNodes = (~netmask)-1; // 0 and ffff have special meaning and cannot be used 00092 if (topo.nodes()>maxNodes) 00093 error("netmask too large, not enough addresses for all %d nodes", topo.nodes()); 00094 00095 int numIPNodes = 0; 00096 for (int i=0; i<topo.nodes(); i++) 00097 { 00098 // skip bus types 00099 if (!nodeInfo[i].isIPNode) 00100 continue; 00101 00102 uint32 addr = networkAddress | uint32(++numIPNodes); 00103 nodeInfo[i].address.set(addr); 00104 00105 // find interface table and assign address to all (non-loopback) interfaces 00106 InterfaceTable *ift = nodeInfo[i].ift; 00107 for (int k=0; k<ift->numInterfaces(); k++) 00108 { 00109 InterfaceEntry *ie = ift->interfaceAt(k); 00110 if (!ie->isLoopback()) 00111 { 00112 ie->ipv4()->setInetAddress(IPAddress(addr)); 00113 ie->ipv4()->setNetmask(IPAddress::ALLONES_ADDRESS); // full address must match for local delivery 00114 } 00115 } 00116 } 00117 }
void FlatNetworkConfigurator::extractTopology | ( | cTopology & | topo, | |
NodeInfoVector & | nodeInfo | |||
) | [protected] |
00061 { 00062 // FIXME eliminate nonIPModuleTypes, like in NetworkConfigurator 00063 StringVector types = cStringTokenizer(par("moduleTypes"), " ").asVector(); 00064 StringVector nonIPTypes = cStringTokenizer(par("nonIPModuleTypes"), " ").asVector(); 00065 for (unsigned int i=0; i<nonIPTypes.size(); i++) 00066 types.push_back(nonIPTypes[i]); 00067 00068 // extract topology 00069 topo.extractByModuleType(types); 00070 EV << "cTopology found " << topo.nodes() << " nodes\n"; 00071 00072 // fill in isIPNode, ift and rt members in nodeInfo[] 00073 nodeInfo.resize(topo.nodes()); 00074 for (int i=0; i<topo.nodes(); i++) 00075 { 00076 cModule *mod = topo.node(i)->module(); 00077 nodeInfo[i].isIPNode = std::find(nonIPTypes.begin(),nonIPTypes.end(), mod->className())==nonIPTypes.end(); 00078 if (nodeInfo[i].isIPNode) 00079 { 00080 nodeInfo[i].ift = IPAddressResolver().interfaceTableOf(mod); 00081 nodeInfo[i].rt = IPAddressResolver().routingTableOf(mod); 00082 } 00083 } 00084 }
void FlatNetworkConfigurator::fillRoutingTables | ( | cTopology & | topo, | |
NodeInfoVector & | nodeInfo | |||
) | [protected] |
00161 { 00162 // fill in routing tables with static routes 00163 for (int i=0; i<topo.nodes(); i++) 00164 { 00165 cTopology::Node *destNode = topo.node(i); 00166 00167 // skip bus types 00168 if (!nodeInfo[i].isIPNode) 00169 continue; 00170 00171 IPAddress destAddr = nodeInfo[i].address; 00172 std::string destModName = destNode->module()->fullName(); 00173 00174 // calculate shortest paths from everywhere towards destNode 00175 topo.unweightedSingleShortestPathsTo(destNode); 00176 00177 // add route (with host=destNode) to every routing table in the network 00178 // (excepting nodes with only one interface -- there we'll set up a default route) 00179 for (int j=0; j<topo.nodes(); j++) 00180 { 00181 if (i==j) continue; 00182 if (!nodeInfo[j].isIPNode) 00183 continue; 00184 00185 cTopology::Node *atNode = topo.node(j); 00186 if (atNode->paths()==0) 00187 continue; // not connected 00188 if (nodeInfo[j].usesDefaultRoute) 00189 continue; // already added default route here 00190 00191 IPAddress atAddr = nodeInfo[j].address; 00192 00193 InterfaceTable *ift = nodeInfo[j].ift; 00194 00195 int outputGateId = atNode->path(0)->localGate()->id(); 00196 InterfaceEntry *ie = ift->interfaceByNodeOutputGateId(outputGateId); 00197 if (!ie) 00198 error("%s has no interface for output gate id %d", ift->fullPath().c_str(), outputGateId); 00199 00200 EV << " from " << atNode->module()->fullName() << "=" << IPAddress(atAddr); 00201 EV << " towards " << destModName << "=" << IPAddress(destAddr) << " interface " << ie->name() << endl; 00202 00203 // add route 00204 RoutingTable *rt = nodeInfo[j].rt; 00205 RoutingEntry *e = new RoutingEntry(); 00206 e->host = destAddr; 00207 e->netmask = IPAddress(255,255,255,255); // full match needed 00208 e->interfaceName = ie->name(); 00209 e->interfacePtr = ie; 00210 e->type = RoutingEntry::DIRECT; 00211 e->source = RoutingEntry::MANUAL; 00212 //e->metric() = 1; 00213 rt->addRoutingEntry(e); 00214 } 00215 } 00216 }
void FlatNetworkConfigurator::handleMessage | ( | cMessage * | msg | ) | [protected, virtual] |
void FlatNetworkConfigurator::initialize | ( | int | stage | ) | [protected, virtual] |
00035 { 00036 if (stage==2) 00037 { 00038 cTopology topo("topo"); 00039 NodeInfoVector nodeInfo; // will be of size topo.nodes[] 00040 00041 // extract topology into the cTopology object, then fill in 00042 // isIPNode, rt and ift members of nodeInfo[] 00043 extractTopology(topo, nodeInfo); 00044 00045 // assign addresses to IP nodes, and also store result in nodeInfo[].address 00046 assignAddresses(topo, nodeInfo); 00047 00048 // add default routes to hosts (nodes with a single attachment); 00049 // also remember result in nodeInfo[].usesDefaultRoute 00050 addDefaultRoutes(topo, nodeInfo); 00051 00052 // calculate shortest paths, and add corresponding static routes 00053 fillRoutingTables(topo, nodeInfo); 00054 00055 // update display string 00056 setDisplayString(topo, nodeInfo); 00057 } 00058 }
virtual int FlatNetworkConfigurator::numInitStages | ( | ) | const [inline, protected, virtual] |
void FlatNetworkConfigurator::setDisplayString | ( | cTopology & | topo, | |
NodeInfoVector & | nodeInfo | |||
) | [protected] |
00224 { 00225 int numIPNodes = 0; 00226 for (int i=0; i<topo.nodes(); i++) 00227 if (nodeInfo[i].isIPNode) 00228 numIPNodes++; 00229 00230 // update display string 00231 char buf[80]; 00232 sprintf(buf, "%d IP nodes\n%d non-IP nodes", numIPNodes, topo.nodes()-numIPNodes); 00233 displayString().setTagArg("t",0,buf); 00234 }