From: Richard Whitehouse Date: Sat, 7 May 2011 14:52:59 +0000 (+0100) Subject: Split Ethernet Bridge State into separate file X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=817ff18767bbac88a83076b29087cb28dcb08b20;p=ns-moose.git Split Ethernet Bridge State into separate file --- diff --git a/src/devices/bridge/model/bridge-net-device.cc b/src/devices/bridge/model/bridge-net-device.cc index 167945f..faa125c 100644 --- a/src/devices/bridge/model/bridge-net-device.cc +++ b/src/devices/bridge/model/bridge-net-device.cc @@ -43,21 +43,11 @@ BridgeNetDevice::GetTypeId (void) MakeUintegerAccessor (&BridgeNetDevice::SetMtu, &BridgeNetDevice::GetMtu), MakeUintegerChecker ()) - .AddAttribute ("StateSize", "The size of the State Table", - UintegerValue (8000), - MakeUintegerAccessor (&BridgeNetDevice::SetMaxStateSize, - &BridgeNetDevice::GetMaxStateSize), - MakeUintegerChecker ()) .AddAttribute ("EnableLearning", "Enable the learning mode of the Learning Bridge", BooleanValue (true), MakeBooleanAccessor (&BridgeNetDevice::m_enableLearning), MakeBooleanChecker ()) - .AddAttribute ("ExpirationTime", - "Time it takes for learned MAC state entry to expire.", - TimeValue (Seconds (300)), - MakeTimeAccessor (&BridgeNetDevice::m_expirationTime), - MakeTimeChecker ()) ; return tid; } @@ -69,6 +59,7 @@ BridgeNetDevice::BridgeNetDevice () { NS_LOG_FUNCTION_NOARGS (); m_channel = CreateObject (); + m_state = CreateObject (); } BridgeNetDevice::~BridgeNetDevice() @@ -182,12 +173,7 @@ void BridgeNetDevice::Learn (Address const &src, Ptr port) if (m_enableLearning) { - std::map::iterator it = m_learnState.find(src48); - if(it != m_learnState.end() || m_learnState.size() < m_maxStateSize){ - LearnedState &state = m_learnState[src48]; - state.associatedPort = port; - state.expirationTime = Simulator::Now () + m_expirationTime; - } + m_state->Learn(src48, port); } } @@ -196,21 +182,7 @@ Ptr BridgeNetDevice::GetLearnedState (Mac48Address source) NS_LOG_FUNCTION_NOARGS (); if (m_enableLearning) { - Time now = Simulator::Now (); - std::map::iterator iter = - m_learnState.find (source); - if (iter != m_learnState.end ()) - { - LearnedState &state = iter->second; - if (state.expirationTime > now) - { - return state.associatedPort; - } - else - { - m_learnState.erase (iter); - } - } + return m_state->GetPort(source); } return NULL; } @@ -313,7 +285,7 @@ bool BridgeNetDevice::SetMaxStateSize (const unsigned long maxStateSize ) { NS_LOG_FUNCTION_NOARGS (); - m_maxStateSize = maxStateSize ; + m_state->SetMaxSize(maxStateSize); return true; } @@ -321,14 +293,14 @@ unsigned long BridgeNetDevice::GetMaxStateSize (void) const { NS_LOG_FUNCTION_NOARGS (); - return m_maxStateSize; + return m_state->GetMaxSize(); } unsigned long BridgeNetDevice::GetStateSize (void) const { NS_LOG_FUNCTION_NOARGS (); - return m_learnState.size(); + return m_state->GetSize(); } bool diff --git a/src/devices/bridge/model/bridge-net-device.h b/src/devices/bridge/model/bridge-net-device.h index 2709919..8b0c186 100644 --- a/src/devices/bridge/model/bridge-net-device.h +++ b/src/devices/bridge/model/bridge-net-device.h @@ -20,6 +20,7 @@ #define BRIDGE_NET_DEVICE_H #include "bridge-port-net-device.h" +#include "bridge-state.h" #include "ns3/net-device.h" #include "ns3/mac48-address.h" #include "ns3/nstime.h" @@ -140,22 +141,16 @@ private: NetDevice::PromiscReceiveCallback m_promiscRxCallback; Mac48Address m_address; - Time m_expirationTime; // time it takes for learned MAC state to expire - struct LearnedState - { - Ptr associatedPort; - Time expirationTime; - }; - std::map m_learnState; Ptr m_node; Ptr m_channel; + Ptr m_state; + std::vector > m_ports; uint16_t mPortNumber; uint32_t m_ifIndex; uint16_t m_mtu; - unsigned long m_maxStateSize; bool m_enableLearning; }; diff --git a/src/devices/bridge/model/bridge-state.cc b/src/devices/bridge/model/bridge-state.cc new file mode 100644 index 0000000..ca2ffa4 --- /dev/null +++ b/src/devices/bridge/model/bridge-state.cc @@ -0,0 +1,101 @@ +/* -*- 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 "bridge-state.h" +#include "ns3/log.h" +#include "ns3/boolean.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" + +NS_LOG_COMPONENT_DEFINE ("BridgeState"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (BridgeState); + +TypeId BridgeState::GetTypeId (void){ + +static TypeId tid = TypeId ("ns3::BridgeState") +.SetParent () +.AddConstructor () +.AddAttribute ("ExpirationTime", +"Time it takes for learned MAC state entry to expire.", +TimeValue (Seconds (300)), +MakeTimeAccessor (&BridgeState::m_time), +MakeTimeChecker ()) +.AddAttribute ("StateSize", "The size of the State Table", +UintegerValue (8000), +MakeUintegerAccessor (&BridgeState::m_max), +MakeUintegerChecker ()) +; +return tid; +} + +unsigned long BridgeState::GetMaxSize() const { + return m_max; +} + +BridgeState::BridgeState(){ + +} + +BridgeState::~BridgeState(){ + +} + +void BridgeState::Learn(ns3::Mac48Address addr, Ptr port){ + std::map::iterator it = m_learnState.find(addr); + if(it != m_learnState.end() || m_learnState.size() < m_max){ + Host &state = m_learnState[addr]; + state.associatedPort = port; + state.expirationTime = Simulator::Now () + m_time; + } +} + +void BridgeState::SetMaxSize(unsigned long max){ + m_max = max; +} + +Ptr BridgeState::GetPort(ns3::Mac48Address addr){ + Time now = Simulator::Now (); + std::map::iterator iter = m_learnState.find(addr); + if (iter != m_learnState.end()){ + Host &state = iter->second; + if (state.expirationTime > now){ + return state.associatedPort; + } else { + m_learnState.erase (iter); + } + } +} + +unsigned long BridgeState::GetSize(){ + return m_learnState.size(); +} + +void BridgeState::SetExpirationTime(Time time){ + m_time = time; +} + +Time BridgeState::GetExpirationTime(){ + return m_time; +} + +} + + diff --git a/src/devices/bridge/model/bridge-state.h b/src/devices/bridge/model/bridge-state.h new file mode 100644 index 0000000..d696366 --- /dev/null +++ b/src/devices/bridge/model/bridge-state.h @@ -0,0 +1,65 @@ +/* -*- 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 BRIDGE_STATE_H +#define BRIDGE_STATE_H + +#include "bridge-port-net-device.h" +#include "ns3/object-factory.h" +#include "ns3/mac48-address.h" +#include "ns3/nstime.h" + +namespace ns3 { + +class BridgeState : public Object { + +public: + + static TypeId GetTypeId (void); + BridgeState (); + virtual ~BridgeState (); + + unsigned long GetSize(); + + unsigned long GetMaxSize() const; + void SetMaxSize(const unsigned long max); + + void Learn(Mac48Address addr, Ptr port); + Ptr GetPort(Mac48Address addr); + + void SetExpirationTime(Time time); + + Time GetExpirationTime(); + +private: + + unsigned long m_max; + Time m_time; + + struct Host { + Ptr associatedPort; + Time expirationTime; + }; + + std::map m_learnState; + +}; + +} + +#endif + diff --git a/src/devices/bridge/wscript b/src/devices/bridge/wscript index a8738f9..c612d55 100644 --- a/src/devices/bridge/wscript +++ b/src/devices/bridge/wscript @@ -3,6 +3,7 @@ def build(bld): obj = bld.create_ns3_module('bridge', ['node']) obj.source = [ + 'model/bridge-state.cc', 'model/moose-bridge-port-net-device.cc', 'model/moose-bridge-net-device.cc', 'model/bridge-net-device.cc', @@ -14,6 +15,7 @@ def build(bld): headers = bld.new_task_gen('ns3header') headers.module = 'bridge' headers.source = [ + 'model/bridge-state.h', 'model/moose-bridge-port-net-device.h', 'model/moose-bridge-net-device.h', 'model/bridge-net-device.h',