From: Richard Whitehouse Date: Sat, 7 May 2011 14:58:27 +0000 (+0100) Subject: Split MOOSE Bridge State into separate class X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=a4ae074e47296efd616b65ac39afa093fff5b3ff;p=ns-moose.git Split MOOSE Bridge State into separate class --- diff --git a/src/devices/bridge/model/moose-bridge-net-device.cc b/src/devices/bridge/model/moose-bridge-net-device.cc index 2612911..6ed97dd 100644 --- a/src/devices/bridge/model/moose-bridge-net-device.cc +++ b/src/devices/bridge/model/moose-bridge-net-device.cc @@ -41,11 +41,7 @@ MooseBridgeNetDevice::GetTypeId (void) MooseAddressValue(), MakeMooseAddressAccessor (&MooseBridgeNetDevice::m_mooseAddress), MakeMooseAddressChecker ()) - .AddAttribute ("MooseExpirationTime", - "Time it takes for learned MOOSE state entry to expire.", - TimeValue (Seconds (30)), - MakeTimeAccessor (&MooseBridgeNetDevice::m_expirationTime), - MakeTimeChecker ()); + ; return tid; } @@ -53,6 +49,7 @@ MooseBridgeNetDevice::MooseBridgeNetDevice() : BridgeNetDevice() { NS_LOG_FUNCTION_NOARGS (); m_mooseAddress = MooseAddress::Allocate(); + m_state = CreateObject (); } @@ -76,38 +73,14 @@ MooseAddress MooseBridgeNetDevice::FromMoose(MooseAddress const& addr){ if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ return addr; } - - std::map::iterator iter = m_ethernetState.find(addr.GetMooseSuffix()); - - if(iter == m_ethernetState.end()){ - - // Expired state - must broadcast. - - return MooseAddress(Mac48Address::GetBroadcast()); - } else { - - Time now = Simulator::Now(); - - SuffixState* state = iter->second; - - if(state->expirationTime > now){ - return MooseAddress(iter->second->ethernet); - } else { - m_ethernetState.erase(iter); - m_suffixState.erase(iter->second->ethernet); - return MooseAddress(Mac48Address::GetBroadcast()); - } - - } - + return m_state->GetMac(addr.GetMooseSuffix()); + } MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){ NS_LOG_FUNCTION_NOARGS(); - Time now = Simulator::Now(); - MooseAddress::MooseType mt = addr.GetMooseType(); if(mt == MooseAddress::MOOSE){ @@ -117,55 +90,9 @@ MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){ Mac48Address addr48 = addr.GetMacAddress(); - // Fix Suffix. - std::map::iterator iter = m_suffixState.find(addr48); - if(iter == m_suffixState.end()){ - // Allocate New Suffix - - moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); - - SuffixState &state = m_suffixState[addr48]; - state.ethernet = addr48; - state.suffix = moose.GetMooseSuffix(); - state.expirationTime = now + m_expirationTime; - - NS_LOG_LOGIC("Allocating New MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ") for " << addr48); + MooseSuffixAddress suffix = m_state->GetSuffix(addr48, m_mooseAddress.GetMoosePrefix()); - m_ethernetState[state.suffix] = &m_suffixState[addr48]; - - } else { - SuffixState &state = iter->second; - if(state.expirationTime > now){ - moose = MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), state.suffix); - - NS_LOG_LOGIC("Using Allocated MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")"); - - if(now + m_expirationTime > state.expirationTime){ - state.expirationTime = now + m_expirationTime; - } - } else { - // Delete old and alloc new - - m_ethernetState.erase(state.suffix); - m_suffixState.erase(iter); - - // Allocate New - - SuffixState &state = m_suffixState[addr48]; - - moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); - state.ethernet = addr48; - state.suffix = moose.GetMooseSuffix(); - state.expirationTime = now + m_expirationTime; - - NS_LOG_LOGIC("Expired MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")"); - - m_ethernetState[state.suffix] = &m_suffixState[addr48]; - - } - } - - return moose; + return MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), suffix); } else { NS_ASSERT( (mt == MooseAddress::HOST) || (mt == MooseAddress::MOOSE) ); @@ -179,11 +106,7 @@ void MooseBridgeNetDevice::AddRoutes(std::map >::iterator it = routes.begin(); it != routes.end(); it ++){ - NS_LOG_LOGIC("Adding Prefix State: (port: " << it->second << ", time: " << Simulator::GetMaximumSimulationTime() << ")"); - - PrefixState &state = m_prefixState[it->first]; - state.associatedPort = it->second; - state.expirationTime = Simulator::GetMaximumSimulationTime(); + m_state->Fixed(it->first, it->second); } @@ -191,29 +114,16 @@ void MooseBridgeNetDevice::AddRoutes(std::map port){ NS_LOG_FUNCTION_NOARGS (); - - Time now = Simulator::Now(); NS_ASSERT(addr.GetMooseType() == MooseAddress::MOOSE); if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ - PrefixState &state = m_prefixState[addr.GetMoosePrefix()]; - if(now + m_expirationTime > state.expirationTime){ - NS_LOG_LOGIC("Updating Learned Prefix State: (port: " << port << ", time: " << now + m_expirationTime << ", address: " << addr << ") - Previous State: (port: " << state.associatedPort << ", time: " << state.expirationTime << ")"); - - state.associatedPort = port; // Only update if the expiration is not in the future - state.expirationTime = now + m_expirationTime; - } + m_state->Learn(addr.GetMoosePrefix(), port); } else { - PortState &state = m_portState[addr.GetMooseSuffix()]; - if(now + m_expirationTime > state.expirationTime){ - NS_LOG_LOGIC("Updating Learned Suffix State: (port: " << port << ", time: " << now + m_expirationTime << ", address: " << addr << ") - Previous State: (port: " << state.associatedPort << ", time: " << state.expirationTime << ")"); - state.associatedPort = port; - state.expirationTime = now + m_expirationTime; - } + m_state->Learn(addr.GetMooseSuffix(), port); } @@ -222,24 +132,14 @@ void MooseBridgeNetDevice::Learn(MooseAddress const& addr, Ptr MooseBridgeNetDevice::GetLearnedPort(MooseAddress const&addr){ NS_LOG_FUNCTION_NOARGS(); - Time now = Simulator::Now(); - if(addr.GetMooseType() != MooseAddress::MOOSE){ return NULL; } if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ - std::map::iterator iter = m_prefixState.find(addr.GetMoosePrefix()); - - if(iter != m_prefixState.end()){ - return iter->second.associatedPort; - } + return m_state->GetPort(addr.GetMoosePrefix()); } else { - std::map::iterator iter = m_portState.find(addr.GetMooseSuffix()); - - if(iter != m_portState.end()){ - return iter->second.associatedPort; - } + return m_state->GetPort(addr.GetMooseSuffix()); } return NULL; diff --git a/src/devices/bridge/model/moose-bridge-net-device.h b/src/devices/bridge/model/moose-bridge-net-device.h index b22ade3..8e37932 100644 --- a/src/devices/bridge/model/moose-bridge-net-device.h +++ b/src/devices/bridge/model/moose-bridge-net-device.h @@ -19,6 +19,7 @@ #define MOOSE_BRIDGE_NET_DEVICE_H #include "bridge-net-device.h" +#include "moose-bridge-state.h" #include "ns3/moose-address.h" #include "ns3/moose-prefix-address.h" #include "ns3/moose-suffix-address.h" @@ -58,39 +59,9 @@ protected: private: -// Need more types of state - MooseAddress m_mooseAddress; - // Remote Moose State - - struct PrefixState - { - Ptr associatedPort; - Time expirationTime; - }; - - // Local Moose State - - struct SuffixState - { - Mac48Address ethernet; - MooseSuffixAddress suffix; - Time expirationTime; - }; - - struct PortState - { - Ptr associatedPort; - Time expirationTime; - }; - - Time m_expirationTime; - - std::map m_prefixState; - std::map m_suffixState; - std::map m_ethernetState; - std::map m_portState; + Ptr m_state; }; diff --git a/src/devices/bridge/model/moose-bridge-state.cc b/src/devices/bridge/model/moose-bridge-state.cc new file mode 100644 index 0000000..4bc77a5 --- /dev/null +++ b/src/devices/bridge/model/moose-bridge-state.cc @@ -0,0 +1,213 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation; +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Richard Whitehouse +*/ + +#include "moose-bridge-state.h" +#include "ns3/log.h" +#include "ns3/boolean.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" + +NS_LOG_COMPONENT_DEFINE ("MooseBridgeState"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (MooseBridgeState); + +TypeId MooseBridgeState::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::MooseBridgeState") + .SetParent () + .AddConstructor () + .AddAttribute ( "ExpirationTime", + "Time it takes for learned state entry to expire.", + TimeValue (Seconds (300)), + MakeTimeAccessor (&MooseBridgeState::m_time), + MakeTimeChecker ()) + .AddAttribute ( "StateSize", "The size of the State Table", + UintegerValue (8000), + MakeUintegerAccessor (&MooseBridgeState::m_max), + MakeUintegerChecker ()) + ; + return tid; +} + +MooseBridgeState::MooseBridgeState(){ + +} + +MooseBridgeState::~MooseBridgeState(){ + +} + +void MooseBridgeState::Learn(MoosePrefixAddress addr, Ptr port){ + + Time now = Simulator::Now (); + + Prefix &state = m_prefixState[addr]; + // Only update if the expiration is not in the future + if(now + m_time > state.time){ + state.port = port; + state.time = now + m_time; + } +} + +void MooseBridgeState::Learn(MooseSuffixAddress addr, Ptr port){ + + Time now = Simulator::Now (); + + Port &state = m_portState[addr]; + // Only update if the expiration is not in the future + if(now + m_time > state.time){ + state.port = port; + state.time = now + m_time; + } + +} + +void MooseBridgeState::Fixed(MoosePrefixAddress addr, Ptr port){ + + + Prefix &state = m_prefixState[addr]; + state.port = port; + state.time = Simulator::GetMaximumSimulationTime(); +} + +Mac48Address MooseBridgeState::GetMac(MooseSuffixAddress addr){ + + std::map::iterator iter = m_sufSuffixState.find(addr); + + if(iter == m_sufSuffixState.end()){ + + // Not found. + + return Mac48Address::GetBroadcast(); + + } else { + + Time now = Simulator::Now(); + + Suffix* state = iter->second; + + if(state->time > now){ + return iter->second->ethernet; + } else { + m_sufSuffixState.erase(iter); + m_ethSuffixState.erase(iter->second->ethernet); + return Mac48Address::GetBroadcast(); + } + } + +} + +MooseSuffixAddress MooseBridgeState::GetSuffix(Mac48Address addr, MoosePrefixAddress prefix){ + + Time now = Simulator::Now(); + + std::map::iterator iter = m_ethSuffixState.find(addr); + + if(iter == m_ethSuffixState.end()){ + // Allocate New Suffix + MooseAddress moose = MooseAddress::Allocate(prefix); + Suffix &state = m_ethSuffixState[addr]; + state.ethernet = addr; + state.suffix = moose.GetMooseSuffix(); + state.time = now + m_time; + NS_LOG_LOGIC("Allocating New MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ") for " << addr); + m_sufSuffixState[state.suffix] = &m_ethSuffixState[addr]; + + + return moose.GetMooseSuffix(); + + } else { + Suffix &state = iter->second; + + // Valid Entry + + if(state.time > now){ + MooseAddress moose = MooseAddress::Combine(prefix, state.suffix); + NS_LOG_LOGIC("Using Allocated MOOSE Suffix: (" << prefix.GetInt() << "," << state.suffix.GetInt() << ")"); + + if(now + m_time > state.time){ + state.time = now + m_time; + } + + + return moose.GetMooseSuffix(); + + } else { + + // Expired Entry - Delete old and alloc new + + m_sufSuffixState.erase(state.suffix); + m_ethSuffixState.erase(iter); + + // Allocate New + + Suffix &state = m_ethSuffixState[addr]; + MooseAddress moose = MooseAddress::Allocate(prefix); + state.ethernet = addr; + state.suffix = moose.GetMooseSuffix(); + state.time = now + m_time; + + NS_LOG_LOGIC("Expired MOOSE Suffix: (" << prefix.GetInt() << "," << state.suffix.GetInt() << ")"); + + m_sufSuffixState[state.suffix] = &m_ethSuffixState[addr]; + + + return moose.GetMooseSuffix(); + + } + } +} + +Ptr MooseBridgeState::GetPort(MoosePrefixAddress addr){ + + Time now = Simulator::Now(); + + std::map::iterator iter = m_prefixState.find(addr); + + if(iter != m_prefixState.end() && iter->second.time > now){ + + return iter->second.port; + } + + return NULL; + +} + +Ptr MooseBridgeState::GetPort(MooseSuffixAddress addr){ + + Time now = Simulator::Now(); + + std::map::iterator iter = m_portState.find(addr); + + if(iter != m_portState.end() && iter->second.time > now){ + + return iter->second.port; + } + + return NULL; + +} + +unsigned long MooseBridgeState::GetSize(){ + return m_prefixState.size() + m_portState.size(); +} + +} + diff --git a/src/devices/bridge/model/moose-bridge-state.h b/src/devices/bridge/model/moose-bridge-state.h new file mode 100644 index 0000000..d7cfb29 --- /dev/null +++ b/src/devices/bridge/model/moose-bridge-state.h @@ -0,0 +1,94 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation; +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Richard Whitehouse +*/ + +#ifndef MOOSE_BRIDGE_STATE_H +#define MOOSE_BRIDGE_STATE_H + +#include "bridge-port-net-device.h" +#include "ns3/object-factory.h" +#include "ns3/mac48-address.h" +#include "ns3/nstime.h" +#include "ns3/moose-address.h" +#include "ns3/moose-prefix-address.h" +#include "ns3/moose-suffix-address.h" + +namespace ns3 { + +class MooseBridgeState : public Object { + +public: + + static TypeId GetTypeId (void); + MooseBridgeState(); + ~MooseBridgeState(); + + unsigned long GetSize(); + + unsigned long GetMaxSize() const; + bool SetMaxSize(const unsigned long max); + + void Learn(MooseSuffixAddress suffix, Ptr port); + void Learn(MoosePrefixAddress prefix, Ptr port); + + void Fixed(MoosePrefixAddress prefix, Ptr port); + + MooseSuffixAddress GetSuffix(Mac48Address addr, MoosePrefixAddress prefix); + + Ptr GetPort(MoosePrefixAddress addr); + Ptr GetPort(MooseSuffixAddress addr); + + Mac48Address GetMac(MooseSuffixAddress addr); + +private: + + unsigned long m_max; + + // Remote Moose State + + struct Prefix { + Ptr port; + Time time; + }; + + // Local Moose State + + struct Suffix { + Mac48Address ethernet; + MooseSuffixAddress suffix; + Time time; + }; + + struct Port { + Ptr port; + Time time; + }; + + Time m_time; + + std::map m_prefixState; + std::map m_ethSuffixState; + std::map m_sufSuffixState; + std::map m_portState; + + +}; + +} + +#endif + diff --git a/src/devices/bridge/wscript b/src/devices/bridge/wscript index c612d55..110bd5f 100644 --- a/src/devices/bridge/wscript +++ b/src/devices/bridge/wscript @@ -4,6 +4,7 @@ def build(bld): obj = bld.create_ns3_module('bridge', ['node']) obj.source = [ 'model/bridge-state.cc', + 'model/moose-bridge-state.cc', 'model/moose-bridge-port-net-device.cc', 'model/moose-bridge-net-device.cc', 'model/bridge-net-device.cc', @@ -16,6 +17,7 @@ def build(bld): headers.module = 'bridge' headers.source = [ 'model/bridge-state.h', + 'model/moose-bridge-state.h', 'model/moose-bridge-port-net-device.h', 'model/moose-bridge-net-device.h', 'model/bridge-net-device.h',