From: Richard Whitehouse Date: Sun, 8 May 2011 10:05:11 +0000 (+0100) Subject: Refactoring to create EthernetBridgeNetDevice as a subclass, like MooseBridgeNetDevic... X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=153806297b832d2dc4a91b6242918e21f3b16d8c;p=ns-moose.git Refactoring to create EthernetBridgeNetDevice as a subclass, like MooseBridgeNetDevice, of ABC BridgeNetDevice --- 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/bridge-helper.cc deleted file mode 100644 index b74ca8b..0000000 --- a/src/devices/bridge/helper/bridge-helper.cc +++ /dev/null @@ -1,92 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) - * - * 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 - */ - -#include "bridge-helper.h" -#include "ns3/log.h" -#include "ns3/bridge-net-device.h" -#include "ns3/node.h" -#include "ns3/names.h" - -NS_LOG_COMPONENT_DEFINE ("BridgeHelper"); - -namespace ns3 { - -BridgeHelper::BridgeHelper () -{ - NS_LOG_FUNCTION_NOARGS (); - m_deviceFactory.SetTypeId ("ns3::BridgeNetDevice"); -} - -void -BridgeHelper::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) -{ - NS_LOG_FUNCTION_NOARGS (); - NS_LOG_LOGIC ("**** Install bridge device on node " << node->GetId ()); - - NetDeviceContainer devs; - Ptr dev = m_deviceFactory.Create (); - devs.Add (dev); - node->AddDevice (dev); - - for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i) - { - NS_LOG_LOGIC ("**** Add BridgePort "<< *i); - Ptr port = dev->AddBridgePort (*i); - if(portsEnabled.find(*i) != portsEnabled.end()){ - port->SetEnabled(portsEnabled[*i]); - } - } - return devs; -} - - -NetDeviceContainer -BridgeHelper::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 (); - devs.Add (dev); - node->AddDevice (dev); - - for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i) - { - NS_LOG_LOGIC ("**** Add BridgePort "<< *i); - dev->AddBridgePort (*i); - } - return devs; -} - -NetDeviceContainer -BridgeHelper::Install (std::string nodeName, NetDeviceContainer c) -{ - NS_LOG_FUNCTION_NOARGS (); - Ptr node = Names::Find (nodeName); - return Install (node, c); -} - -} // namespace ns3 diff --git a/src/devices/bridge/helper/bridge-helper.h b/src/devices/bridge/helper/bridge-helper.h deleted file mode 100644 index bd00d4e..0000000 --- a/src/devices/bridge/helper/bridge-helper.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2008 INRIA - * - * 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: Mathieu Lacage - * Author: Gustavo Carneiro - */ -#ifndef BRIDGE_HELPER_H -#define BRIDGE_HELPER_H - -#include "ns3/net-device-container.h" -#include "ns3/object-factory.h" -#include -#include - -namespace ns3 { - -class Node; -class AttributeValue; - -/** - * \brief Add capability to bridge multiple LAN segments (IEEE 802.1D bridging) - */ -class BridgeHelper -{ -public: - /* - * Construct a BridgeHelper - */ - BridgeHelper (); - /** - * Set an attribute on each ns3::BridgeNetDevice created by - * BridgeHelper::Install - * - * \param n1 the name of the attribute to set - * \param v1 the value of the attribute to set - */ - void SetDeviceAttribute (std::string n1, const AttributeValue &v1); - - /** - * This method creates an ns3::BridgeNetDevice with the attributes - * configured by BridgeHelper::SetDeviceAttribute, adds the device - * to the node, and attaches the given NetDevices as ports of the - * bridge. - * - * This version uses static spanning tree to disable loops. - * - * \param node The node to install the device in - * \param c Container of NetDevices to add as bridge ports - * \returns A container holding the added net device. - */ - 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 - * to the node, and attaches the given NetDevices as ports of the - * bridge. - * - * \param node The node to install the device in - * \param c Container of NetDevices to add as bridge ports - * \returns A container holding the added net device. - */ - NetDeviceContainer Install (Ptr node, NetDeviceContainer c); - /** - * This method creates an ns3::BridgeNetDevice with the attributes - * configured by BridgeHelper::SetDeviceAttribute, adds the device - * to the node, and attaches the given NetDevices as ports of the - * bridge. - * - * \param nodeName The name of the node to install the device in - * \param c Container of NetDevices to add as bridge ports - * \returns A container holding the added net device. - */ - NetDeviceContainer Install (std::string nodeName, NetDeviceContainer c); -private: - ObjectFactory m_deviceFactory; -}; - -} // namespace ns3 - - -#endif /* BRIDGE_HELPER_H */ diff --git a/src/devices/bridge/helper/ethernet-bridge-helper.cc b/src/devices/bridge/helper/ethernet-bridge-helper.cc new file mode 100644 index 0000000..afce12e --- /dev/null +++ b/src/devices/bridge/helper/ethernet-bridge-helper.cc @@ -0,0 +1,92 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) + * + * 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 + */ + +#include "ethernet-bridge-helper.h" +#include "ns3/log.h" +#include "ns3/ethernet-bridge-net-device.h" +#include "ns3/node.h" +#include "ns3/names.h" + +NS_LOG_COMPONENT_DEFINE ("EthernetBridgeHelper"); + +namespace ns3 { + +EthernetBridgeHelper::EthernetBridgeHelper () +{ + NS_LOG_FUNCTION_NOARGS (); + m_deviceFactory.SetTypeId ("ns3::EthernetBridgeNetDevice"); +} + +void +EthernetBridgeHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1) +{ + NS_LOG_FUNCTION_NOARGS (); + m_deviceFactory.Set (n1, v1); +} + +NetDeviceContainer +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 (); + devs.Add (dev); + node->AddDevice (dev); + + for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i) + { + NS_LOG_LOGIC ("**** Add BridgePort "<< *i); + Ptr port = dev->AddBridgePort (*i); + if(portsEnabled.find(*i) != portsEnabled.end()){ + port->SetEnabled(portsEnabled[*i]); + } + } + return devs; +} + + +NetDeviceContainer +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 (); + devs.Add (dev); + node->AddDevice (dev); + + for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i) + { + NS_LOG_LOGIC ("**** Add BridgePort "<< *i); + dev->AddBridgePort (*i); + } + return devs; +} + +NetDeviceContainer +EthernetBridgeHelper::Install (std::string nodeName, NetDeviceContainer c) +{ + NS_LOG_FUNCTION_NOARGS (); + Ptr node = Names::Find (nodeName); + return Install (node, c); +} + +} // namespace ns3 diff --git a/src/devices/bridge/helper/ethernet-bridge-helper.h b/src/devices/bridge/helper/ethernet-bridge-helper.h new file mode 100644 index 0000000..82df72b --- /dev/null +++ b/src/devices/bridge/helper/ethernet-bridge-helper.h @@ -0,0 +1,100 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 INRIA + * + * 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: Mathieu Lacage + * Author: Gustavo Carneiro + * Author: Richard Whitehouse + */ +#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 EthernetBridgeHelper +{ +public: + /* + * Construct a EthernetBridgeHelper + */ + EthernetBridgeHelper (); + /** + * 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 + */ + void SetDeviceAttribute (std::string n1, const AttributeValue &v1); + + /** + * 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. + * + * This version uses static spanning tree to disable loops. + * + * \param node The node to install the device in + * \param c Container of NetDevices to add as bridge ports + * \returns A container holding the added net device. + */ + NetDeviceContainer Install (Ptr node, NetDeviceContainer c, std::map, bool> portsEnabled); + + /** + * 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. + * + * \param node The node to install the device in + * \param c Container of NetDevices to add as bridge ports + * \returns A container holding the added net device. + */ + NetDeviceContainer Install (Ptr node, NetDeviceContainer c); + /** + * 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. + * + * \param nodeName The name of the node to install the device in + * \param c Container of NetDevices to add as bridge ports + * \returns A container holding the added net device. + */ + NetDeviceContainer Install (std::string nodeName, NetDeviceContainer c); +private: + ObjectFactory m_deviceFactory; +}; + +} // namespace ns3 + + +#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/bridge-state.cc b/src/devices/bridge/model/bridge-state.cc deleted file mode 100644 index 6c30162..0000000 --- a/src/devices/bridge/model/bridge-state.cc +++ /dev/null @@ -1,114 +0,0 @@ -/* -*- 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; -} - -std::ostream& operator<<(std::ostream& file, BridgeState& state){ - // Output state - file << state.m_learnState.size(); - std::map::iterator it; - for(it = state.m_learnState.begin(); it != state.m_learnState.end(); ++it){ - file << std::endl; - file << it->first << std::endl; - file << it->second.associatedPort << std::endl; - file << it->second.expirationTime; - } - return file; -} - -} - - diff --git a/src/devices/bridge/model/bridge-state.h b/src/devices/bridge/model/bridge-state.h deleted file mode 100644 index 91b986f..0000000 --- a/src/devices/bridge/model/bridge-state.h +++ /dev/null @@ -1,69 +0,0 @@ -/* -*- 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; - - friend std::ostream& operator<<(std::ostream&, BridgeState&); - -}; - -std::ostream& operator<<(std::ostream&, BridgeState&); - -} - -#endif - 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/ethernet-bridge-state.cc b/src/devices/bridge/model/ethernet-bridge-state.cc new file mode 100644 index 0000000..7e8bb61 --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-state.cc @@ -0,0 +1,114 @@ +/* -*- 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-state.h" +#include "ns3/log.h" +#include "ns3/boolean.h" +#include "ns3/simulator.h" +#include "ns3/uinteger.h" + +NS_LOG_COMPONENT_DEFINE ("EthernetBridgeState"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (EthernetBridgeState); + +TypeId EthernetBridgeState::GetTypeId (void){ + +static TypeId tid = TypeId ("ns3::EthernetBridgeState") +.SetParent () +.AddConstructor () +.AddAttribute ("ExpirationTime", +"Time it takes for learned MAC state entry to expire.", +TimeValue (Seconds (300)), +MakeTimeAccessor (&EthernetBridgeState::m_time), +MakeTimeChecker ()) +.AddAttribute ("StateSize", "The size of the State Table", +UintegerValue (8000), +MakeUintegerAccessor (&EthernetBridgeState::m_max), +MakeUintegerChecker ()) +; +return tid; +} + +unsigned long EthernetBridgeState::GetMaxSize() const { + return m_max; +} + +EthernetBridgeState::EthernetBridgeState(){ + +} + +EthernetBridgeState::~EthernetBridgeState(){ + +} + +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]; + state.associatedPort = port; + state.expirationTime = Simulator::Now () + m_time; + } +} + +void EthernetBridgeState::SetMaxSize(unsigned long max){ + m_max = max; +} + +Ptr EthernetBridgeState::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 EthernetBridgeState::GetSize(){ + return m_learnState.size(); +} + +void EthernetBridgeState::SetExpirationTime(Time time){ + m_time = time; +} + +Time EthernetBridgeState::GetExpirationTime(){ + return m_time; +} + +std::ostream& operator<<(std::ostream& file, EthernetBridgeState& state){ + // Output state + file << state.m_learnState.size(); + std::map::iterator it; + for(it = state.m_learnState.begin(); it != state.m_learnState.end(); ++it){ + file << std::endl; + file << it->first << std::endl; + file << it->second.associatedPort << std::endl; + file << it->second.expirationTime; + } + return file; +} + +} + + diff --git a/src/devices/bridge/model/ethernet-bridge-state.h b/src/devices/bridge/model/ethernet-bridge-state.h new file mode 100644 index 0000000..e4c9bbb --- /dev/null +++ b/src/devices/bridge/model/ethernet-bridge-state.h @@ -0,0 +1,70 @@ +/* -*- 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_STATE_H +#define ETHERNET_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 EthernetBridgeState : public Object { + +public: + + static TypeId GetTypeId (void); + EthernetBridgeState (); + virtual ~EthernetBridgeState (); + + 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; + + friend std::ostream& operator<<(std::ostream&, EthernetBridgeState&); + +}; + +std::ostream& operator<<(std::ostream&, EthernetBridgeState&); + +} + +#endif + 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;