|
VLANs(802.1Q)forOMNet++
0.5Beta
|
A NetworkConfigurator to handle vlans on initialization. More...
#include <VlanNetConfig.h>
Public Member Functions | |
| uint16_t | getVlanFromIfName (std::string ifname) |
| uint32_t | getNetAddrForVlan (uint16_t vlan) |
| virtual void | hostRemoved (cModule *h) |
| Handle Hosts that are dynamically removed. | |
| virtual void | hostAdded (cModule *h) |
| Handle Hosts that are dynamically added. | |
Protected Member Functions | |
| virtual void | initialize (int stage) |
| virtual void | assignAddresses (cTopology &topo, NodeInfoVector &nodeInfo) |
| virtual void | setDisplayString (cTopology &topo, NodeInfoVector &nodeInfo) |
| virtual void | initializeNextAvailableIPz () |
Protected Attributes | |
| uint32 | networkAddress |
| uint32 | netmask |
| VlanIPInfo | vlanInfo |
A NetworkConfigurator to handle vlans on initialization.
Not many parameters available... What it actually does is to take the 10.x.x.0/24 subnet for each vlan. 10.0.0.0/24 is always going to be the real device. the x.x is replaced by the vlan ID. DrawBack: This means that each vlan is allowed 254 machines(hosts+network devices).
| void VlanNetConfig::assignAddresses | ( | cTopology & | topo, |
| NodeInfoVector & | nodeInfo | ||
| ) | [protected, virtual] |
{
int numIPNodes = 0;
for (int i=0; i<topo.getNumNodes(); i++)
{
// skip bus types
if (!nodeInfo[i].isIPNode)
continue;
uint32 addr = networkAddress | uint32(++numIPNodes);
if (numIPNodes==255) error("To many nodes, we are out of IPs");
nodeInfo[i].address.set(addr);
// find interface table and assign address to all (non-loopback) interfaces
IInterfaceTable *ift = nodeInfo[i].ift;
for (int k=0; k<ift->getNumInterfaces(); k++)
{
InterfaceEntry *ie = ift->getInterface(k);
if (ie->isLoopback()) continue;
uint16_t vlan = getVlanFromIfName( ie->getFullName() );
VlanIPInfo::iterator it = vlanInfo.find(vlan);
if (it == vlanInfo.end()){
vlanInfo[vlan].numHosts = 1;
}else{
if (vlanInfo[vlan].numHosts == 254)
error("netmask too large, not enough addresses for all nodes "
"for vlan %d", vlan);
vlanInfo[vlan].numHosts++;
}
addr=getNetAddrForVlan(vlan)|vlanInfo[vlan].numHosts;
ie->ipv4Data()->setIPAddress(IPAddress(addr));
ie->ipv4Data()->setNetmask(netmask);
// full address must match for local delivery
}
}
}
| uint32_t VlanNetConfig::getNetAddrForVlan | ( | uint16_t | vlan | ) |
{
uint32_t vl = vlan&0x00FFL;
uint32_t vh = vlan>>8;
vh=vh<<8*2;
vl=vl<<8;
return (networkAddress|vh|vl);
}
| uint16_t VlanNetConfig::getVlanFromIfName | ( | std::string | ifname | ) |
{
uint subif = ifname.find('.');
uint16_t vlan=0;
if (subif!=string::npos){
vlan = atoi(ifname.substr(subif+1).c_str());
}
return vlan;
}
| void VlanNetConfig::hostAdded | ( | cModule * | h | ) | [virtual] |
Handle Hosts that are dynamically added.
{
Enter_Method_Silent("hostAdded");
IInterfaceTable * ift=IPAddressResolver().interfaceTableOf(h);
for (int k=0; k<ift->getNumInterfaces(); k++)
{
InterfaceEntry *ie = ift->getInterface(k);
if (ie->isLoopback()) continue;
uint16_t vlan = getVlanFromIfName( ie->getFullName() );
// Check that there is at least 1
if (vlanInfo[vlan].nextAvailIP.size() == 0)
error("No more IPs for vlan %d", vlan);
// Assign it
uint32_t nip = vlanInfo[vlan].nextAvailIP.top();
vlanInfo[vlan].nextAvailIP.pop();
ie->ipv4Data()->setIPAddress(IPAddress(nip));
ie->ipv4Data()->setNetmask(netmask);
vlanInfo[vlan].numHosts++;
// Now, if the stack is empty it means that we pulled the
// IP with the maximum number (last one assigned on initialization).
// We have to push the next ip (++) into the stack (if available)
if (vlanInfo[vlan].nextAvailIP.size() == 0){
if (vlanInfo[vlan].numHosts == 254) continue; // Skip if no more IPs
nip = getNetAddrForVlan(vlan)|(vlanInfo[vlan].numHosts+1);
vlanInfo[vlan].nextAvailIP.push(nip);
}
}
}
| void VlanNetConfig::hostRemoved | ( | cModule * | h | ) | [virtual] |
Handle Hosts that are dynamically removed.
{
Enter_Method_Silent("hostRemoved");
IInterfaceTable * ift=IPAddressResolver().interfaceTableOf(h);
for (int k=0; k<ift->getNumInterfaces(); k++)
{
InterfaceEntry *ie = ift->getInterface(k);
if (ie->isLoopback()) continue;
uint16_t vlan = getVlanFromIfName( ie->getFullName() );
IPAddress freedIP = ie->ipv4Data()->getIPAddress();
vlanInfo[vlan].nextAvailIP.push(freedIP.getInt());
vlanInfo[vlan].numHosts--;
}
}
| void VlanNetConfig::initialize | ( | int | stage | ) | [protected, virtual] |
{
if (stage==0){
// assign IP addresses
networkAddress = IPAddress("10.0.0.0").getInt();
netmask = IPAddress("255.255.255.0").getInt();
}
FlatNetworkConfigurator::initialize(stage);
if (stage==numInitStages()-1){
// Initialize Stacks
initializeNextAvailableIPz();
WATCH_MAP(vlanInfo);
}
}
| void VlanNetConfig::initializeNextAvailableIPz | ( | ) | [protected, virtual] |
{
VlanIPInfo::iterator it = vlanInfo.begin();
for (;it!=vlanInfo.end(); ++it){
// It means we do not have more ips... but we actually don't
// need more at this stage. The error occurs on host addition
if (it->second.numHosts == 254) continue;
uint32_t nip = getNetAddrForVlan(it->first)|(it->second.numHosts+1);
EV << "Next Available IP for vlan "<<it->first<<" is: "<<IPAddress(nip)<<endl;
it->second.nextAvailIP.push(nip);
}
}
| void VlanNetConfig::setDisplayString | ( | cTopology & | topo, |
| NodeInfoVector & | nodeInfo | ||
| ) | [protected, virtual] |
{
int numIPNodes = 0;
for (int i=0; i<topo.getNumNodes(); i++)
if (nodeInfo[i].isIPNode)
numIPNodes++;
// update display string
char buf[100];
sprintf(buf, "%d IP nodes\nin %d vlans\n(%d non-IP nodes)", numIPNodes, (int)vlanInfo.size(),topo.getNumNodes()-numIPNodes);
getDisplayString().setTagArg("t",0,buf);
}
uint32 VlanNetConfig::netmask [protected] |
uint32 VlanNetConfig::networkAddress [protected] |
VlanIPInfo VlanNetConfig::vlanInfo [protected] |