From 153806297b832d2dc4a91b6242918e21f3b16d8c Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Sun, 8 May 2011 11:05:11 +0100 Subject: [PATCH] Refactoring to create EthernetBridgeNetDevice as a subclass, like MooseBridgeNetDevice, of ABC BridgeNetDevice --- examples/wireless/wifi-wired-bridging.cc | 4 +- .../bridge/examples/csma-bridge-one-hop.cc | 2 +- src/devices/bridge/examples/csma-bridge.cc | 2 +- .../bridge/examples/moose-csma-bridge.cc | 4 +- ...ge-helper.cc => ethernet-bridge-helper.cc} | 22 +- ...idge-helper.h => ethernet-bridge-helper.h} | 32 +-- src/devices/bridge/model/bridge-net-device.cc | 113 ++-------- src/devices/bridge/model/bridge-net-device.h | 21 +- .../bridge/model/bridge-port-net-device.cc | 45 +--- .../bridge/model/bridge-port-net-device.h | 3 +- .../model/ethernet-bridge-net-device.cc | 200 ++++++++++++++++++ .../bridge/model/ethernet-bridge-net-device.h | 72 +++++++ .../model/ethernet-bridge-port-net-device.cc | 89 ++++++++ .../model/ethernet-bridge-port-net-device.h | 58 +++++ ...idge-state.cc => ethernet-bridge-state.cc} | 38 ++-- ...bridge-state.h => ethernet-bridge-state.h} | 17 +- src/devices/bridge/wscript | 12 +- src/helper/link-layer-helper.h | 5 +- src/test/csma-system-test-suite.cc | 4 +- 19 files changed, 526 insertions(+), 217 deletions(-) rename src/devices/bridge/helper/{bridge-helper.cc => ethernet-bridge-helper.cc} (72%) rename src/devices/bridge/helper/{bridge-helper.h => ethernet-bridge-helper.h} (72%) create mode 100644 src/devices/bridge/model/ethernet-bridge-net-device.cc create mode 100644 src/devices/bridge/model/ethernet-bridge-net-device.h create mode 100644 src/devices/bridge/model/ethernet-bridge-port-net-device.cc create mode 100644 src/devices/bridge/model/ethernet-bridge-port-net-device.h rename src/devices/bridge/model/{bridge-state.cc => ethernet-bridge-state.cc} (67%) rename src/devices/bridge/model/{bridge-state.h => ethernet-bridge-state.h} (82%) diff --git a/examples/wireless/wifi-wired-bridging.cc b/examples/wireless/wifi-wired-bridging.cc index b236356..26ac2e9 100644 --- a/examples/wireless/wifi-wired-bridging.cc +++ b/examples/wireless/wifi-wired-bridging.cc @@ -49,7 +49,7 @@ #include "ns3/helper-module.h" #include "ns3/wifi-module.h" #include "ns3/node-module.h" -#include "ns3/bridge-helper.h" +#include "ns3/ethernet-bridge-helper.h" #include #include #include @@ -108,7 +108,7 @@ int main (int argc, char *argv[]) Ipv4InterfaceContainer staInterface; Ipv4InterfaceContainer apInterface; MobilityHelper mobility; - BridgeHelper bridge; + EthernetBridgeHelper bridge; WifiHelper wifi = WifiHelper::Default (); NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); diff --git a/src/devices/bridge/examples/csma-bridge-one-hop.cc b/src/devices/bridge/examples/csma-bridge-one-hop.cc index 8b4f07f..378b677 100644 --- a/src/devices/bridge/examples/csma-bridge-one-hop.cc +++ b/src/devices/bridge/examples/csma-bridge-one-hop.cc @@ -139,7 +139,7 @@ main (int argc, char *argv[]) // bridge lives on the node bridge1 and bridges together the topBridgeDevices // which are the three CSMA net devices on the node in the diagram above. // - BridgeHelper bridge; + EthernetBridgeHelper bridge; bridge.Install (bridge1, topBridgeDevices); // Add internet stack to the router nodes diff --git a/src/devices/bridge/examples/csma-bridge.cc b/src/devices/bridge/examples/csma-bridge.cc index 994ec6b..bc2d865 100644 --- a/src/devices/bridge/examples/csma-bridge.cc +++ b/src/devices/bridge/examples/csma-bridge.cc @@ -89,7 +89,7 @@ main (int argc, char *argv[]) // Create the bridge netdevice, which will do the packet switching Ptr switchNode = csmaSwitch.Get (0); - BridgeHelper bridge; + EthernetBridgeHelper bridge; bridge.Install (switchNode, switchDevices); // Add internet stack to the terminals diff --git a/src/devices/bridge/examples/moose-csma-bridge.cc b/src/devices/bridge/examples/moose-csma-bridge.cc index 92adc21..1b2ce78 100644 --- a/src/devices/bridge/examples/moose-csma-bridge.cc +++ b/src/devices/bridge/examples/moose-csma-bridge.cc @@ -98,11 +98,11 @@ int main (int argc, char *argv[]) // Create the bridge netdevice, which will do the packet switching Ptr switchANode = switchA.Get(0); - BridgeHelper bridgeA; + EthernetBridgeHelper bridgeA; bridgeA.Install (switchANode, switchADevices); Ptr switchBNode = switchB.Get(0); - BridgeHelper bridgeB; + EthernetBridgeHelper bridgeB; bridgeB.Install (switchBNode, switchBDevices); // Setup the terminals as Internet hosts. diff --git a/src/devices/bridge/helper/bridge-helper.cc b/src/devices/bridge/helper/ethernet-bridge-helper.cc similarity index 72% rename from src/devices/bridge/helper/bridge-helper.cc rename to src/devices/bridge/helper/ethernet-bridge-helper.cc index b74ca8b..afce12e 100644 --- a/src/devices/bridge/helper/bridge-helper.cc +++ b/src/devices/bridge/helper/ethernet-bridge-helper.cc @@ -16,37 +16,37 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "bridge-helper.h" +#include "ethernet-bridge-helper.h" #include "ns3/log.h" -#include "ns3/bridge-net-device.h" +#include "ns3/ethernet-bridge-net-device.h" #include "ns3/node.h" #include "ns3/names.h" -NS_LOG_COMPONENT_DEFINE ("BridgeHelper"); +NS_LOG_COMPONENT_DEFINE ("EthernetBridgeHelper"); namespace ns3 { -BridgeHelper::BridgeHelper () +EthernetBridgeHelper::EthernetBridgeHelper () { NS_LOG_FUNCTION_NOARGS (); - m_deviceFactory.SetTypeId ("ns3::BridgeNetDevice"); + m_deviceFactory.SetTypeId ("ns3::EthernetBridgeNetDevice"); } void -BridgeHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1) +EthernetBridgeHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1) { NS_LOG_FUNCTION_NOARGS (); m_deviceFactory.Set (n1, v1); } NetDeviceContainer -BridgeHelper::Install (Ptr node, NetDeviceContainer c, std::map, bool> portsEnabled) +EthernetBridgeHelper::Install (Ptr node, NetDeviceContainer c, std::map, bool> portsEnabled) { NS_LOG_FUNCTION_NOARGS (); NS_LOG_LOGIC ("**** Install bridge device on node " << node->GetId ()); NetDeviceContainer devs; - Ptr dev = m_deviceFactory.Create (); + Ptr dev = m_deviceFactory.Create (); devs.Add (dev); node->AddDevice (dev); @@ -63,13 +63,13 @@ BridgeHelper::Install (Ptr node, NetDeviceContainer c, std::map node, NetDeviceContainer c) +EthernetBridgeHelper::Install (Ptr node, NetDeviceContainer c) { NS_LOG_FUNCTION_NOARGS (); NS_LOG_LOGIC ("**** Install bridge device on node " << node->GetId ()); NetDeviceContainer devs; - Ptr dev = m_deviceFactory.Create (); + Ptr dev = m_deviceFactory.Create (); devs.Add (dev); node->AddDevice (dev); @@ -82,7 +82,7 @@ BridgeHelper::Install (Ptr node, NetDeviceContainer c) } NetDeviceContainer -BridgeHelper::Install (std::string nodeName, NetDeviceContainer c) +EthernetBridgeHelper::Install (std::string nodeName, NetDeviceContainer c) { NS_LOG_FUNCTION_NOARGS (); Ptr node = Names::Find (nodeName); diff --git a/src/devices/bridge/helper/bridge-helper.h b/src/devices/bridge/helper/ethernet-bridge-helper.h similarity index 72% rename from src/devices/bridge/helper/bridge-helper.h rename to src/devices/bridge/helper/ethernet-bridge-helper.h index bd00d4e..82df72b 100644 --- a/src/devices/bridge/helper/bridge-helper.h +++ b/src/devices/bridge/helper/ethernet-bridge-helper.h @@ -17,33 +17,37 @@ * * Author: Mathieu Lacage * Author: Gustavo Carneiro + * Author: Richard Whitehouse */ -#ifndef BRIDGE_HELPER_H -#define BRIDGE_HELPER_H +#ifndef ETHERNET_BRIDGE_HELPER_H +#define ETHERNET_BRIDGE_HELPER_H #include "ns3/net-device-container.h" #include "ns3/object-factory.h" #include #include +#include namespace ns3 { class Node; class AttributeValue; +class BridgeHelper { public: BridgeHelper(){ assert(false); } void Install(ns3::Ptr, ns3::NetDeviceContainer){ assert(false); } }; + /** * \brief Add capability to bridge multiple LAN segments (IEEE 802.1D bridging) */ -class BridgeHelper +class EthernetBridgeHelper { public: /* - * Construct a BridgeHelper + * Construct a EthernetBridgeHelper */ - BridgeHelper (); + EthernetBridgeHelper (); /** - * Set an attribute on each ns3::BridgeNetDevice created by - * BridgeHelper::Install + * Set an attribute on each ns3::EthernetBridgeNetDevice created by + * EthernetBridgeHelper::Install * * \param n1 the name of the attribute to set * \param v1 the value of the attribute to set @@ -51,8 +55,8 @@ public: void SetDeviceAttribute (std::string n1, const AttributeValue &v1); /** - * This method creates an ns3::BridgeNetDevice with the attributes - * configured by BridgeHelper::SetDeviceAttribute, adds the device + * This method creates an ns3::EthernetBridgeNetDevice with the attributes + * configured by EthernetBridgeHelper::SetDeviceAttribute, adds the device * to the node, and attaches the given NetDevices as ports of the * bridge. * @@ -65,8 +69,8 @@ public: NetDeviceContainer Install (Ptr node, NetDeviceContainer c, std::map, bool> portsEnabled); /** - * This method creates an ns3::BridgeNetDevice with the attributes - * configured by BridgeHelper::SetDeviceAttribute, adds the device + * This method creates an ns3::EthernetBridgeNetDevice with the attributes + * configured by EthernetBridgeHelper::SetDeviceAttribute, adds the device * to the node, and attaches the given NetDevices as ports of the * bridge. * @@ -76,8 +80,8 @@ public: */ NetDeviceContainer Install (Ptr node, NetDeviceContainer c); /** - * This method creates an ns3::BridgeNetDevice with the attributes - * configured by BridgeHelper::SetDeviceAttribute, adds the device + * This method creates an ns3::EthernetBridgeNetDevice with the attributes + * configured by EthernetBridgeHelper::SetDeviceAttribute, adds the device * to the node, and attaches the given NetDevices as ports of the * bridge. * @@ -93,4 +97,4 @@ private: } // namespace ns3 -#endif /* BRIDGE_HELPER_H */ +#endif /* ETHERNET_BRIDGE_HELPER_H */ diff --git a/src/devices/bridge/model/bridge-net-device.cc b/src/devices/bridge/model/bridge-net-device.cc index d42efee..cde43f3 100644 --- a/src/devices/bridge/model/bridge-net-device.cc +++ b/src/devices/bridge/model/bridge-net-device.cc @@ -37,17 +37,11 @@ BridgeNetDevice::GetTypeId (void) { static TypeId tid = TypeId ("ns3::BridgeNetDevice") .SetParent () - .AddConstructor () .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit", UintegerValue (1500), MakeUintegerAccessor (&BridgeNetDevice::SetMtu, &BridgeNetDevice::GetMtu), MakeUintegerChecker ()) - .AddAttribute ("EnableLearning", - "Enable the learning mode of the Learning Bridge", - BooleanValue (true), - MakeBooleanAccessor (&BridgeNetDevice::m_enableLearning), - MakeBooleanChecker ()) ; return tid; } @@ -59,7 +53,6 @@ BridgeNetDevice::BridgeNetDevice () { NS_LOG_FUNCTION_NOARGS (); m_channel = CreateObject (); - m_state = CreateObject (); } BridgeNetDevice::~BridgeNetDevice() @@ -94,7 +87,7 @@ void BridgeNetDevice::Forward (Ptr port, Ptr switch (packetType) { case PACKET_HOST: - if (dst48 == m_address) + if (dst48 == m_macAddress) { m_rxCallback (this, packet, protocol, src); } @@ -107,7 +100,7 @@ void BridgeNetDevice::Forward (Ptr port, Ptr break; case PACKET_OTHERHOST: - if (dst48 == m_address) + if (dst48 == m_macAddress) { m_rxCallback (this, packet, protocol, src); } @@ -119,36 +112,6 @@ void BridgeNetDevice::Forward (Ptr port, Ptr } } -void -BridgeNetDevice::ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) -{ - NS_LOG_FUNCTION_NOARGS (); - - Ptr outPort = GetLearnedState (dst); - if (outPort != NULL && outPort != incomingPort) - { - NS_LOG_LOGIC ("Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () << "'"); - outPort->Send (packet->Copy (), src, dst, protocol); - } - else - { - NS_LOG_LOGIC ("No learned state: send through all ports"); - for (std::vector< Ptr >::iterator iter = m_ports.begin (); - iter != m_ports.end (); iter++) - { - Ptr port = *iter; - if (port != incomingPort) - { - NS_LOG_LOGIC ("LearningBridgeForward (" << src << " => " << dst << "): " - << incomingPort->GetInstanceTypeId ().GetName () - << " --> " << port->GetInstanceTypeId ().GetName () - << " (UID " << packet->GetUid () << ")."); - port->Send (packet->Copy (), src, dst, protocol); - } - } - } -} - void BridgeNetDevice::ForwardBroadcast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) { @@ -165,28 +128,6 @@ BridgeNetDevice::ForwardBroadcast (Ptr incomingPort, Ptr port) -{ - NS_LOG_FUNCTION_NOARGS (); - - Mac48Address src48 = Mac48Address::ConvertFrom (src); - - if (m_enableLearning) - { - m_state->Learn(src48, port); - } -} - -Ptr BridgeNetDevice::GetLearnedState (Mac48Address source) -{ - NS_LOG_FUNCTION_NOARGS (); - if (m_enableLearning) - { - return m_state->GetPort(source); - } - return NULL; -} - uint32_t BridgeNetDevice::GetNBridgePorts (void) const { @@ -210,11 +151,11 @@ BridgeNetDevice::AddBridgePort (Ptr device) NS_ASSERT (device != this); - NS_LOG_DEBUG("AddBridgePort(bridge_addr=" << m_address << ", device_address=" << (Mac48Address::ConvertFrom(device->GetAddress()))); + NS_LOG_DEBUG("AddBridgePort(bridge_addr=" << m_macAddress << ", device_address=" << (Mac48Address::ConvertFrom(device->GetAddress()))); - if (m_address == Mac48Address ()) + if (m_macAddress == Mac48Address ()) { - m_address = Mac48Address::ConvertFrom (device->GetAddress ()); + m_macAddress = Mac48Address::ConvertFrom (device->GetAddress ()); } Ptr port = CreateBridgePort(this, device, m_node); @@ -226,11 +167,6 @@ BridgeNetDevice::AddBridgePort (Ptr device) } -Ptr -BridgeNetDevice::CreateBridgePort(Ptr bridge, Ptr device, Ptr node){ - return CreateObject(bridge, device, node); -} - void BridgeNetDevice::SetIfIndex(const uint32_t index) { @@ -256,14 +192,14 @@ void BridgeNetDevice::SetAddress (Address address) { NS_LOG_FUNCTION_NOARGS (); - m_address = Mac48Address::ConvertFrom (address); + m_macAddress = Mac48Address::ConvertFrom (address); } Address BridgeNetDevice::GetAddress (void) const { NS_LOG_FUNCTION_NOARGS (); - return m_address; + return m_macAddress; } bool @@ -281,28 +217,6 @@ BridgeNetDevice::GetMtu (void) const return m_mtu; } -bool -BridgeNetDevice::SetMaxStateSize (const unsigned long maxStateSize ) -{ - NS_LOG_FUNCTION_NOARGS (); - m_state->SetMaxSize(maxStateSize); - return true; -} - -unsigned long -BridgeNetDevice::GetMaxStateSize (void) const -{ - NS_LOG_FUNCTION_NOARGS (); - return m_state->GetMaxSize(); -} - -unsigned long -BridgeNetDevice::GetStateSize (void) const -{ - NS_LOG_FUNCTION_NOARGS (); - return m_state->GetSize(); -} - bool BridgeNetDevice::IsLinkUp (void) const { @@ -366,7 +280,7 @@ bool BridgeNetDevice::Send (Ptr packet, const Address& dest, uint16_t protocolNumber) { NS_LOG_FUNCTION_NOARGS (); - return SendFrom (packet, m_address, dest, protocolNumber); + return SendFrom (packet, m_macAddress, dest, protocolNumber); } bool @@ -375,6 +289,8 @@ BridgeNetDevice::SendFrom (Ptr packet, const Address& src, const Address NS_LOG_FUNCTION_NOARGS (); Mac48Address dst = Mac48Address::ConvertFrom (dest); + /* + // try to use the learned state if data is unicast if (!dst.IsGroup ()) { @@ -386,6 +302,8 @@ BridgeNetDevice::SendFrom (Ptr packet, const Address& src, const Address } } + */ + // data was not unicast or no state has been learned for that mac // address => flood through all ports. for (std::vector< Ptr >::iterator iter = m_ports.begin (); @@ -450,13 +368,6 @@ Address BridgeNetDevice::GetMulticast (Ipv6Address addr) const return Mac48Address::GetMulticast (addr); } -std::ostream& BridgeNetDevice::Print(std::ostream& file){ - file << 1 << std::endl; - file << m_address << std::endl; - file << *m_state; - return file; -} - std::ostream& operator<<(std::ostream& file, BridgeNetDevice& dev){ return dev.Print(file); } diff --git a/src/devices/bridge/model/bridge-net-device.h b/src/devices/bridge/model/bridge-net-device.h index dd55ab3..3534a73 100644 --- a/src/devices/bridge/model/bridge-net-device.h +++ b/src/devices/bridge/model/bridge-net-device.h @@ -19,8 +19,8 @@ #ifndef BRIDGE_NET_DEVICE_H #define BRIDGE_NET_DEVICE_H -#include "bridge-port-net-device.h" -#include "bridge-state.h" +#include "bridge-net-device.h" +#include "ethernet-bridge-port-net-device.h" #include "ns3/net-device.h" #include "ns3/mac48-address.h" #include "ns3/nstime.h" @@ -102,10 +102,6 @@ public: virtual bool SetMtu (const uint16_t mtu); virtual uint16_t GetMtu (void) const; - virtual bool SetMaxStateSize (const unsigned long maxStateSize ); - virtual unsigned long GetMaxStateSize (void) const; - virtual unsigned long GetStateSize (void) const; - virtual bool IsLinkUp (void) const; virtual void AddLinkChangeCallback (Callback callback); virtual bool IsBroadcast (void) const; @@ -125,29 +121,26 @@ public: virtual Address GetMulticast (Ipv6Address addr) const; virtual void Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType); - virtual void Learn (Address const &src, Ptr port); protected: virtual void DoDispose (void); - virtual void ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); + virtual void ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) = 0; virtual void ForwardBroadcast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); - virtual Ptr GetLearnedState (Mac48Address source); - virtual Ptr CreateBridgePort(Ptr bridge, Ptr device, Ptr node); + virtual Ptr CreateBridgePort(Ptr bridge, Ptr device, Ptr node) = 0; + + virtual std::ostream& Print(std::ostream&) = 0; - virtual std::ostream& Print(std::ostream&); + Mac48Address m_macAddress; private: NetDevice::ReceiveCallback m_rxCallback; NetDevice::PromiscReceiveCallback m_promiscRxCallback; - Mac48Address m_address; Ptr m_node; Ptr m_channel; - Ptr m_state; - std::vector > m_ports; uint16_t mPortNumber; diff --git a/src/devices/bridge/model/bridge-port-net-device.cc b/src/devices/bridge/model/bridge-port-net-device.cc index 6406e85..a8545f7 100644 --- a/src/devices/bridge/model/bridge-port-net-device.cc +++ b/src/devices/bridge/model/bridge-port-net-device.cc @@ -39,6 +39,14 @@ BridgePortNetDevice::GetTypeId (void) return tid; } +void BridgePortNetDevice::Send(Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber){ + NS_LOG_FUNCTION_NOARGS (); + if(m_enabled){ + NS_LOG_LOGIC("SendingPacket(src=" << source << ", dest=" << dest << ")"); + m_device->SendFrom(packet->Copy(), source, dest, protocolNumber); + } +} + BridgePortNetDevice::BridgePortNetDevice(Ptr bridge, Ptr device, Ptr node) : m_bridge(bridge), m_device(device), m_enabled(true){ NS_LOG_FUNCTION_NOARGS (); @@ -66,42 +74,9 @@ void BridgePortNetDevice::SetEnabled(bool enabled){ m_enabled = enabled; } -void BridgePortNetDevice::Send(Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber){ - NS_LOG_FUNCTION_NOARGS (); - if(m_enabled){ - NS_LOG_LOGIC("SendingPacket(src=" << source << ", dest=" << dest << ")"); - m_device->SendFrom(packet->Copy(), source, dest, protocolNumber); - } -} - -Ptr BridgePortNetDevice::GetDevice(){ - return m_device; -} - -/** - * By moving learning to the port, we can have some ports which learn, and some which don't. - **/ - -void -BridgePortNetDevice::Receive (Ptr incomingPort, Ptr packet, uint16_t protocol, - Address const &src, Address const &dst, NetDevice::PacketType packetType) +Ptr BridgePortNetDevice::GetDevice() { - NS_LOG_FUNCTION_NOARGS (); - - if(m_enabled){ - - NS_LOG_LOGIC ("UID is " << packet->GetUid ()); - - NS_LOG_LOGIC ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName () - << ", packet=" << packet << ", protocol="<Learn(src, this); - - m_bridge->Forward(this, packet, protocol, src, dst, packetType); - - } - + return m_device; } } diff --git a/src/devices/bridge/model/bridge-port-net-device.h b/src/devices/bridge/model/bridge-port-net-device.h index 3239454..e9cb1ba 100644 --- a/src/devices/bridge/model/bridge-port-net-device.h +++ b/src/devices/bridge/model/bridge-port-net-device.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace ns3 { @@ -50,7 +51,7 @@ public: virtual void SetEnabled(bool enabled); protected: - virtual void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType); + virtual void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType) = 0; Ptr m_bridge; Ptr m_device; diff --git a/src/devices/bridge/model/ethernet-bridge-net-device.cc b/src/devices/bridge/model/ethernet-bridge-net-device.cc new file mode 100644 index 0000000..35b5b9b --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-net-device.cc @@ -0,0 +1,200 @@ +/* -*- 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 + * Author: Gustavo Carneiro + */ +#include "ethernet-bridge-net-device.h" +#include "ns3/node.h" +#include "ns3/channel.h" +#include "ns3/packet.h" +#include "ns3/log.h" +#include "ns3/boolean.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" + +NS_LOG_COMPONENT_DEFINE ("EthernetBridgeNetDevice"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (EthernetBridgeNetDevice); + + +TypeId +EthernetBridgeNetDevice::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetBridgeNetDevice") + .SetParent () + .AddConstructor () + .AddAttribute ("EnableLearning", + "Enable the learning mode of the Learning Bridge", + BooleanValue (true), + MakeBooleanAccessor (&EthernetBridgeNetDevice::m_enableLearning), + MakeBooleanChecker ()) + ; + return tid; +} + + +EthernetBridgeNetDevice::EthernetBridgeNetDevice () : BridgeNetDevice() +{ + NS_LOG_FUNCTION_NOARGS (); + m_state = CreateObject (); +} + +EthernetBridgeNetDevice::~EthernetBridgeNetDevice() +{ + NS_LOG_FUNCTION_NOARGS (); +} + +void EthernetBridgeNetDevice::Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, NetDevice::PacketType packetType){ + + Mac48Address src48 = Mac48Address::ConvertFrom (src); + Mac48Address dst48 = Mac48Address::ConvertFrom (dst); + +/* if (!m_promiscRxCallback.IsNull ()) + { + m_promiscRxCallback (this, packet, protocol, src, dst, packetType); + } */ + + switch (packetType) + { + case PACKET_HOST: + if (dst48 == m_macAddress) + { +// m_rxCallback (this, packet, protocol, src); + } + break; + + case PACKET_BROADCAST: + case PACKET_MULTICAST: +// m_rxCallback (this, packet, protocol, src); + ForwardBroadcast (port, packet, protocol, src48, dst48); + break; + + case PACKET_OTHERHOST: + if (dst48 == m_macAddress) + { +// m_rxCallback (this, packet, protocol, src); + } + else + { + ForwardUnicast (port, packet, protocol, src48, dst48); + } + break; + } +} + +void +EthernetBridgeNetDevice::ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) +{ + NS_LOG_FUNCTION_NOARGS (); + + Ptr outPort = GetLearnedState (dst); + + if (outPort != NULL && outPort != incomingPort){ + + NS_LOG_LOGIC ("Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () << "'"); + + outPort->Send (packet->Copy (), src, dst, protocol); + + } else { + + NS_LOG_LOGIC ("No learned state: send through all ports"); + + ForwardBroadcast(incomingPort, packet, protocol, src, dst); + + } +} + +void EthernetBridgeNetDevice::ForwardBroadcast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) +{ + NS_LOG_FUNCTION_NOARGS(); + + BridgeNetDevice::ForwardBroadcast(incomingPort, packet, protocol, src, dst); +} + +void EthernetBridgeNetDevice::Learn (Address const &src, Ptr port){ + NS_LOG_FUNCTION_NOARGS (); + + Mac48Address src48 = Mac48Address::ConvertFrom (src); + + if (m_enableLearning) + { + m_state->Learn(src48, port); + } +} + +Ptr EthernetBridgeNetDevice::GetLearnedState (Mac48Address source) +{ + NS_LOG_FUNCTION_NOARGS (); + if (m_enableLearning) + { + return m_state->GetPort(source); + } + return NULL; +} + +Ptr +EthernetBridgeNetDevice::CreateBridgePort(Ptr bridge, Ptr device, Ptr node){ + return CreateObject(bridge, device, node); +} + +/* +bool +EthernetBridgeNetDevice::Send (Ptr packet, const Address& dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION_NOARGS (); + return SendFrom (packet, m_address, dest, protocolNumber); +} + +bool +EthernetBridgeNetDevice::SendFrom (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION_NOARGS (); + Mac48Address dst = Mac48Address::ConvertFrom (dest); + + // try to use the learned state if data is unicast + if (!dst.IsGroup ()) + { + Ptr outPort = GetLearnedState (dst); + if (outPort != NULL) + { + outPort->Send(packet, src, dest, protocolNumber); + return true; + } + } + + // data was not unicast or no state has been learned for that mac + // address => flood through all ports. + for (std::vector< Ptr >::iterator iter = m_ports.begin (); + iter != m_ports.end (); iter++) + { + Ptr port = *iter; + port->Send (packet, src, dest, protocolNumber); + } + + return true; +} +*/ + +std::ostream& EthernetBridgeNetDevice::Print(std::ostream& file){ + file << 1 << std::endl; + file << m_macAddress << std::endl; + file << *m_state; + return file; +} + +} // namespace ns3 diff --git a/src/devices/bridge/model/ethernet-bridge-net-device.h b/src/devices/bridge/model/ethernet-bridge-net-device.h new file mode 100644 index 0000000..fc25bf5 --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-net-device.h @@ -0,0 +1,72 @@ +/* -*- 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: Gustavo Carneiro + * Author: Richard Whitehouse + */ +#ifndef ETHERNET_BRIDGE_NET_DEVICE_H +#define ETHERNET_BRIDGE_NET_DEVICE_H + +#include "bridge-net-device.h" +#include "ethernet-bridge-port-net-device.h" +#include "ethernet-bridge-state.h" +#include "ns3/net-device.h" +#include "ns3/mac48-address.h" +#include "ns3/nstime.h" +#include "ns3/bridge-channel.h" +#include "ns3/object-factory.h" +#include +#include +#include + +namespace ns3 { + +class Node; + +/** + * \ingroup bridge + * \brief a virtual net device that bridges multiple LAN segments + */ +class EthernetBridgeNetDevice : public BridgeNetDevice +{ +public: + static TypeId GetTypeId (void); + EthernetBridgeNetDevice (); + virtual ~EthernetBridgeNetDevice (); + + virtual void Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType); + virtual void Learn (Address const &src, Ptr port); + +protected: + virtual void ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); + virtual void ForwardBroadcast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); + virtual Ptr GetLearnedState (Mac48Address source); + + virtual Ptr CreateBridgePort(Ptr bridge, Ptr device, Ptr node); + + virtual std::ostream& Print(std::ostream&); + +private: + Ptr m_state; + + uint16_t mPortNumber; + bool m_enableLearning; + +}; + +} // namespace ns3 + +#endif /* ETHERNET_BRIDGE_NET_DEVICE_H */ + diff --git a/src/devices/bridge/model/ethernet-bridge-port-net-device.cc b/src/devices/bridge/model/ethernet-bridge-port-net-device.cc new file mode 100644 index 0000000..aa8aa5f --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-port-net-device.cc @@ -0,0 +1,89 @@ +/* -*- 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 "ethernet-bridge-net-device.h" +#include "ns3/node.h" +#include "ns3/channel.h" +#include "ns3/packet.h" +#include "ns3/log.h" +#include "ns3/boolean.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" + +NS_LOG_COMPONENT_DEFINE ("EthernetBridgePortNetDevice"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (EthernetBridgePortNetDevice); + +TypeId +EthernetBridgePortNetDevice::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EthernetBridgePortNetDevice") + .SetParent () + ; + return tid; +} + + +EthernetBridgePortNetDevice::EthernetBridgePortNetDevice(Ptr bridge, Ptr device, Ptr node) : BridgePortNetDevice(bridge,device,node) { + NS_LOG_FUNCTION_NOARGS (); +} + +EthernetBridgePortNetDevice::~EthernetBridgePortNetDevice(){ + NS_LOG_FUNCTION_NOARGS (); +} + +void EthernetBridgePortNetDevice::SetEnabled(bool enabled){ + m_enabled = enabled; +} + +void EthernetBridgePortNetDevice::Send(Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber){ + NS_LOG_FUNCTION_NOARGS (); + BridgePortNetDevice::Send(packet, source, dest, protocolNumber); +} + +/** + * By moving learning to the port, we can have some ports which learn, and some which don't. + **/ + +void +EthernetBridgePortNetDevice::Receive (Ptr incomingPort, Ptr packet, uint16_t protocol, + Address const &src, Address const &dst, NetDevice::PacketType packetType) +{ + NS_LOG_FUNCTION_NOARGS (); + + Ptr bridge = m_bridge->GetObject(); + + if(m_enabled){ + + NS_LOG_LOGIC ("UID is " << packet->GetUid ()); + + NS_LOG_LOGIC ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName () + << ", packet=" << packet << ", protocol="<Learn(src, this); + + bridge->Forward(this, packet, protocol, src, dst, packetType); + + } + +} + +} + diff --git a/src/devices/bridge/model/ethernet-bridge-port-net-device.h b/src/devices/bridge/model/ethernet-bridge-port-net-device.h new file mode 100644 index 0000000..7856e59 --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-port-net-device.h @@ -0,0 +1,58 @@ +/* -*- 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 ETHERNET_BRIDGE_PORT_NET_DEVICE_H +#define ETHERNET_BRIDGE_PORT_NET_DEVICE_H + +#include "bridge-port-net-device.h" +#include "ns3/net-device.h" +#include "ns3/mac48-address.h" +#include "ns3/nstime.h" +#include +#include +#include + +namespace ns3 { + +class Node; +class BridgeNetDevice; + +/** + * \ingroup bridge + * \brief a port on a virtual net device that bridges multiple LAN segments + */ + +class EthernetBridgePortNetDevice : public BridgePortNetDevice { + +public: + EthernetBridgePortNetDevice(Ptr bridge, Ptr device, Ptr node); + virtual ~EthernetBridgePortNetDevice(); + + static TypeId GetTypeId (void); + + virtual void Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber); + + virtual void SetEnabled(bool enabled); + +protected: + virtual void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType); + +}; + +} // namespace ns3 + +#endif /* BRIDGE_PORT_NET_DEVICE_H */ diff --git a/src/devices/bridge/model/bridge-state.cc b/src/devices/bridge/model/ethernet-bridge-state.cc similarity index 67% rename from src/devices/bridge/model/bridge-state.cc rename to src/devices/bridge/model/ethernet-bridge-state.cc index 6c30162..7e8bb61 100644 --- a/src/devices/bridge/model/bridge-state.cc +++ b/src/devices/bridge/model/ethernet-bridge-state.cc @@ -16,49 +16,49 @@ * Author: Richard Whitehouse */ -#include "bridge-state.h" +#include "ethernet-bridge-state.h" #include "ns3/log.h" #include "ns3/boolean.h" #include "ns3/simulator.h" #include "ns3/uinteger.h" -NS_LOG_COMPONENT_DEFINE ("BridgeState"); +NS_LOG_COMPONENT_DEFINE ("EthernetBridgeState"); namespace ns3 { -NS_OBJECT_ENSURE_REGISTERED (BridgeState); +NS_OBJECT_ENSURE_REGISTERED (EthernetBridgeState); -TypeId BridgeState::GetTypeId (void){ +TypeId EthernetBridgeState::GetTypeId (void){ -static TypeId tid = TypeId ("ns3::BridgeState") +static TypeId tid = TypeId ("ns3::EthernetBridgeState") .SetParent () -.AddConstructor () +.AddConstructor () .AddAttribute ("ExpirationTime", "Time it takes for learned MAC state entry to expire.", TimeValue (Seconds (300)), -MakeTimeAccessor (&BridgeState::m_time), +MakeTimeAccessor (&EthernetBridgeState::m_time), MakeTimeChecker ()) .AddAttribute ("StateSize", "The size of the State Table", UintegerValue (8000), -MakeUintegerAccessor (&BridgeState::m_max), +MakeUintegerAccessor (&EthernetBridgeState::m_max), MakeUintegerChecker ()) ; return tid; } -unsigned long BridgeState::GetMaxSize() const { +unsigned long EthernetBridgeState::GetMaxSize() const { return m_max; } -BridgeState::BridgeState(){ +EthernetBridgeState::EthernetBridgeState(){ } -BridgeState::~BridgeState(){ +EthernetBridgeState::~EthernetBridgeState(){ } -void BridgeState::Learn(ns3::Mac48Address addr, Ptr port){ +void EthernetBridgeState::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]; @@ -67,11 +67,11 @@ void BridgeState::Learn(ns3::Mac48Address addr, Ptr port){ } } -void BridgeState::SetMaxSize(unsigned long max){ +void EthernetBridgeState::SetMaxSize(unsigned long max){ m_max = max; } -Ptr BridgeState::GetPort(ns3::Mac48Address addr){ +Ptr EthernetBridgeState::GetPort(ns3::Mac48Address addr){ Time now = Simulator::Now (); std::map::iterator iter = m_learnState.find(addr); if (iter != m_learnState.end()){ @@ -84,22 +84,22 @@ Ptr BridgeState::GetPort(ns3::Mac48Address addr){ } } -unsigned long BridgeState::GetSize(){ +unsigned long EthernetBridgeState::GetSize(){ return m_learnState.size(); } -void BridgeState::SetExpirationTime(Time time){ +void EthernetBridgeState::SetExpirationTime(Time time){ m_time = time; } -Time BridgeState::GetExpirationTime(){ +Time EthernetBridgeState::GetExpirationTime(){ return m_time; } -std::ostream& operator<<(std::ostream& file, BridgeState& state){ +std::ostream& operator<<(std::ostream& file, EthernetBridgeState& state){ // Output state file << state.m_learnState.size(); - std::map::iterator it; + std::map::iterator it; for(it = state.m_learnState.begin(); it != state.m_learnState.end(); ++it){ file << std::endl; file << it->first << std::endl; diff --git a/src/devices/bridge/model/bridge-state.h b/src/devices/bridge/model/ethernet-bridge-state.h similarity index 82% rename from src/devices/bridge/model/bridge-state.h rename to src/devices/bridge/model/ethernet-bridge-state.h index 91b986f..e4c9bbb 100644 --- a/src/devices/bridge/model/bridge-state.h +++ b/src/devices/bridge/model/ethernet-bridge-state.h @@ -15,8 +15,9 @@ * * Author: Richard Whitehouse */ -#ifndef BRIDGE_STATE_H -#define BRIDGE_STATE_H + +#ifndef ETHERNET_BRIDGE_STATE_H +#define ETHERNET_BRIDGE_STATE_H #include "bridge-port-net-device.h" #include "ns3/object-factory.h" @@ -25,13 +26,13 @@ namespace ns3 { -class BridgeState : public Object { +class EthernetBridgeState : public Object { public: static TypeId GetTypeId (void); - BridgeState (); - virtual ~BridgeState (); + EthernetBridgeState (); + virtual ~EthernetBridgeState (); unsigned long GetSize(); @@ -56,12 +57,12 @@ private: }; std::map m_learnState; - - friend std::ostream& operator<<(std::ostream&, BridgeState&); + + friend std::ostream& operator<<(std::ostream&, EthernetBridgeState&); }; -std::ostream& operator<<(std::ostream&, BridgeState&); +std::ostream& operator<<(std::ostream&, EthernetBridgeState&); } diff --git a/src/devices/bridge/wscript b/src/devices/bridge/wscript index 110bd5f..31e9580 100644 --- a/src/devices/bridge/wscript +++ b/src/devices/bridge/wscript @@ -3,7 +3,9 @@ def build(bld): obj = bld.create_ns3_module('bridge', ['node']) obj.source = [ - 'model/bridge-state.cc', + 'model/ethernet-bridge-state.cc', + 'model/ethernet-bridge-port-net-device.cc', + 'model/ethernet-bridge-net-device.cc', 'model/moose-bridge-state.cc', 'model/moose-bridge-port-net-device.cc', 'model/moose-bridge-net-device.cc', @@ -11,12 +13,14 @@ def build(bld): 'model/bridge-port-net-device.cc', 'model/bridge-channel.cc', 'helper/moose-bridge-helper.cc', - 'helper/bridge-helper.cc', + 'helper/ethernet-bridge-helper.cc', ] headers = bld.new_task_gen('ns3header') headers.module = 'bridge' headers.source = [ - 'model/bridge-state.h', + 'model/ethernet-bridge-state.h', + 'model/ethernet-bridge-port-net-device.h', + 'model/ethernet-bridge-net-device.h', 'model/moose-bridge-state.h', 'model/moose-bridge-port-net-device.h', 'model/moose-bridge-net-device.h', @@ -24,7 +28,7 @@ def build(bld): 'model/bridge-port-net-device.h', 'model/bridge-channel.h', 'helper/moose-bridge-helper.h', - 'helper/bridge-helper.h', + 'helper/ethernet-bridge-helper.h', ] if bld.env['ENABLE_EXAMPLES']: diff --git a/src/helper/link-layer-helper.h b/src/helper/link-layer-helper.h index affbfd6..8c87100 100644 --- a/src/helper/link-layer-helper.h +++ b/src/helper/link-layer-helper.h @@ -25,7 +25,8 @@ #include "ns3/node.h" #include "ns3/moose-bridge-net-device.h" #include "ns3/moose-bridge-helper.h" -#include "ns3/bridge-helper.h" +#include "ns3/ethernet-bridge-helper.h" +#include "ns3/ethernet-bridge-net-device.h" #include "internet-stack-helper.h" #include "ipv4-address-helper.h" #include "csma-helper.h" @@ -78,8 +79,8 @@ public: CsmaHelper csma; Ipv4AddressHelper ipv4; InternetStackHelper internet; + EthernetBridgeHelper ethernetHelper; MooseBridgeHelper mooseHelper; - BridgeHelper ethernetHelper; LinkLayerHelper(); diff --git a/src/test/csma-system-test-suite.cc b/src/test/csma-system-test-suite.cc index 4902ec5..a158d6c 100644 --- a/src/test/csma-system-test-suite.cc +++ b/src/test/csma-system-test-suite.cc @@ -23,7 +23,7 @@ #include "ns3/address.h" #include "ns3/application-container.h" -#include "ns3/bridge-helper.h" +#include "ns3/ethernet-bridge-helper.h" #include "ns3/callback.h" #include "ns3/config.h" #include "ns3/csma-helper.h" @@ -116,7 +116,7 @@ CsmaBridgeTestCase::DoRun (void) // Create the bridge netdevice, which will do the packet switching Ptr switchNode = csmaSwitch.Get (0); - BridgeHelper bridge; + EthernetBridgeHelper bridge; bridge.Install (switchNode, switchDevices); InternetStackHelper internet; -- 2.34.1