From: Richard Whitehouse Date: Tue, 25 Jan 2011 01:07:33 +0000 (+0000) Subject: Implemented MOOSE Ethernet Address Rewriting, Forwarding and Caching. Removed friend... X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=8d726f012f253bab8583c0a0af377987f7fa16f1;p=ns-moose.git Implemented MOOSE Ethernet Address Rewriting, Forwarding and Caching. Removed friend class from bridge port and made necessary changes. Tidied up interface for Bridge / Bridge Port. --- diff --git a/code/src/devices/bridge/model/bridge-net-device.cc b/code/src/devices/bridge/model/bridge-net-device.cc index 602426d..14d8d81 100644 --- a/code/src/devices/bridge/model/bridge-net-device.cc +++ b/code/src/devices/bridge/model/bridge-net-device.cc @@ -214,13 +214,13 @@ BridgeNetDevice::GetNBridgePorts (void) const return m_ports.size (); } -/** Dislike this. */ +/** Dislike this slightly less now. */ Ptr BridgeNetDevice::GetBridgePort (uint32_t n) const { NS_LOG_FUNCTION_NOARGS (); - return m_ports[n]->m_device; + return m_ports[n]->GetDevice(); } void diff --git a/code/src/devices/bridge/model/bridge-net-device.h b/code/src/devices/bridge/model/bridge-net-device.h index a005f1a..6fe9e99 100644 --- a/code/src/devices/bridge/model/bridge-net-device.h +++ b/code/src/devices/bridge/model/bridge-net-device.h @@ -118,13 +118,14 @@ public: virtual bool SupportsSendFrom () const; 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 Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType); 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 void Learn (Address const &src, Ptr port); virtual Ptr GetLearnedState (Mac48Address source); virtual Ptr CreateBridgePort(Ptr bridge, Ptr device, Ptr node); @@ -151,8 +152,6 @@ private: uint16_t m_mtu; bool m_enableLearning; - friend class BridgePortNetDevice; - }; } // namespace ns3 diff --git a/code/src/devices/bridge/model/bridge-port-net-device.cc b/code/src/devices/bridge/model/bridge-port-net-device.cc index 29093e6..5a68c8f 100644 --- a/code/src/devices/bridge/model/bridge-port-net-device.cc +++ b/code/src/devices/bridge/model/bridge-port-net-device.cc @@ -68,6 +68,10 @@ void BridgePortNetDevice::Send(Ptr packet, const Address& source, const 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. **/ diff --git a/code/src/devices/bridge/model/bridge-port-net-device.h b/code/src/devices/bridge/model/bridge-port-net-device.h index ccc3da0..58b05f1 100644 --- a/code/src/devices/bridge/model/bridge-port-net-device.h +++ b/code/src/devices/bridge/model/bridge-port-net-device.h @@ -43,17 +43,16 @@ public: static TypeId GetTypeId (void); + Ptr GetDevice(); + + virtual void Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber); + protected: - void Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber); - void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType); - -private: + virtual void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType); + Ptr m_bridge; Ptr m_device; - - friend class BridgeNetDevice; - }; } // namespace ns3 diff --git a/code/src/devices/bridge/model/moose-bridge-net-device.cc b/code/src/devices/bridge/model/moose-bridge-net-device.cc index f066c3b..48b47eb 100644 --- a/code/src/devices/bridge/model/moose-bridge-net-device.cc +++ b/code/src/devices/bridge/model/moose-bridge-net-device.cc @@ -10,7 +10,7 @@ * 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 + * 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 @@ -21,6 +21,8 @@ #include "moose-bridge-port-net-device.h" #include "ns3/log.h" #include "ns3/node.h" +#include "ns3/simulator.h" +#include "ns3/packet.h" NS_LOG_COMPONENT_DEFINE ("MooseBridgeNetDevice"); @@ -52,16 +54,202 @@ Ptr MooseBridgeNetDevice::CreateBridgePort(Ptr(bridge, device, node); } -/* -//bool Send (Ptr packet, const Address& dest, uint16_t protocolNumber); -//bool SendFrom (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber); +MooseAddress MooseBridgeNetDevice::FromMoose(MooseAddress const& addr){ + NS_LOG_FUNCTION_NOARGS(); + + if(addr.GetMooseType() != MooseAddress::MOOSE){ + return MooseAddress(Mac48Address::GetBroadcast()); + } + + // Don't translate none local MOOSE addrs. + + if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ + return addr; + } + + std::map::iterator iter = m_ethernetState.find(addr.GetMooseSuffix()); + + if(iter == m_ethernetState.end()){ + + // Expired state - must broadcast. + + return MooseAddress(Mac48Address::GetBroadcast()); + + } else { -void Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType){ - + Time now = Simulator::Now(); + + SuffixState* state = iter->second; + + if(state->expirationTime > now){ + m_ethernetState.erase(iter); + m_suffixState.erase(iter->second->ethernet); + return MooseAddress(Mac48Address::GetBroadcast()); + } else { + return MooseAddress(iter->second->ethernet); + } + + } + } -void ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); -void ForwardBroadcast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); +MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){ + NS_LOG_FUNCTION_NOARGS(); + + Time now = Simulator::Now(); + + MooseAddress::MooseType mt = addr.GetMooseType(); + + if(mt == MooseAddress::MOOSE){ + return addr; + } else if(mt == MooseAddress::HOST){ + MooseAddress moose; + + Mac48Address addr48 = addr.GetMacAddress(); + + // Fix Suffix. + std::map::iterator iter = m_suffixState.find(addr48); + if(iter == m_suffixState.end()){ + // Allocate New Suffix + + moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); + + SuffixState &state = m_suffixState[addr48]; + state.ethernet = addr48; + state.suffix = moose.GetMooseSuffix(); + state.expirationTime = now + m_expirationTime; + + m_ethernetState[state.suffix] = &state; + + } else { + SuffixState &state = iter->second; + if(state.expirationTime > now){ + moose = MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), state.suffix); + state.expirationTime = now + m_expirationTime; + } else { + // Delete old and alloc new + + m_ethernetState.erase(state.suffix); + m_suffixState.erase(iter); + + // Allocate New + + SuffixState &state = m_suffixState[addr48]; + + moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); + state.ethernet = addr48; + state.suffix = moose.GetMooseSuffix(); + state.expirationTime = now + m_expirationTime; + + m_ethernetState[state.suffix] = &state; + + } + } + + return moose; + + } else { + NS_ASSERT( (mt == MooseAddress::HOST) || (mt == MooseAddress::MOOSE) ); + return addr; + } + +} + +void MooseBridgeNetDevice::Learn(MooseAddress const& addr, Ptr port){ + NS_LOG_FUNCTION_NOARGS (); + + Time now = Simulator::Now(); + + NS_ASSERT(addr.GetMooseType() == MooseAddress::MOOSE); + + if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ + + PrefixState &state = m_prefixState[addr.GetMoosePrefix()]; + state.associatedPort = port; + state.expirationTime = now + m_expirationTime; + + } else { + + PortState &state = m_portState[addr.GetMooseSuffix()]; + state.associatedPort = port; + state.expirationTime = now + m_expirationTime; + + } + +} + +Ptr MooseBridgeNetDevice::GetLearnedPort(MooseAddress const&addr){ + NS_LOG_FUNCTION_NOARGS(); + + Time now = Simulator::Now(); + + if(addr.GetMooseType() != MooseAddress::MOOSE){ + return NULL; + } + + if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){ + std::map::iterator iter = m_prefixState.find(addr.GetMoosePrefix()); + + if(iter != m_prefixState.end()){ + return iter->second.associatedPort; + } + } else { + std::map::iterator iter = m_portState.find(addr.GetMooseSuffix()); + + if(iter != m_portState.end()){ + return iter->second.associatedPort; + } + } + + return NULL; +} + + +void +MooseBridgeNetDevice::ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst) +{ + NS_LOG_FUNCTION_NOARGS (); + + // Get Port + + MooseAddress mdst = MooseAddress(dst); + + Ptr outPort = GetLearnedPort(mdst); + + if (outPort != NULL && outPort != incomingPort){ + NS_LOG_LOGIC ("Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () << "'"); + + Mac48Address newdst = FromMoose(mdst).GetMacAddress(); + + if(!newdst.IsBroadcast()){ + + NS_LOG_LOGIC("MOOSE bridge says MAC Address is `" << newdst << "'"); + + outPort->Send (packet->Copy (), src, newdst, protocol); + return; + } + } else { + Mac48Address newdst = FromMoose(mdst).GetMacAddress(); + + if(!newdst.IsBroadcast()){ + + NS_LOG_LOGIC("MOOSE bridge says MAC Address is `" << newdst << "' - no port data, so broadcasting."); + + ForwardBroadcast(incomingPort, packet, protocol, src, newdst); + + return; + } + } + + NS_LOG_LOGIC("Not enough learned state. Dropping packet"); + +} + + + +/* +//bool Send (Ptr packet, const Address& dest, uint16_t protocolNumber); +//bool SendFrom (Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber); void Learn (Address const &src, Ptr port); Ptr GetLearnedState (Mac48Address source);*/ diff --git a/code/src/devices/bridge/model/moose-bridge-net-device.h b/code/src/devices/bridge/model/moose-bridge-net-device.h index 9f39dcb..91fd1d7 100644 --- a/code/src/devices/bridge/model/moose-bridge-net-device.h +++ b/code/src/devices/bridge/model/moose-bridge-net-device.h @@ -39,8 +39,16 @@ public: MooseBridgeNetDevice (); virtual ~MooseBridgeNetDevice (); + void Learn(MooseAddress const& addr, Ptr port); + Ptr GetLearnedPort(MooseAddress const& addr); + MooseAddress ToMoose(MooseAddress const& addr); + MooseAddress FromMoose(MooseAddress const& addr); + + protected: + virtual void ForwardUnicast (Ptr incomingPort, Ptr packet, uint16_t protocol, Mac48Address src, Mac48Address dst); + // From NetDevice // Unconvinced we need these... @@ -53,7 +61,6 @@ protected: /* virtual void Forward (Ptr port, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType); - 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 void Learn (Address const &src, Ptr port); virtual Ptr GetLearnedState (Mac48Address source); */ @@ -64,18 +71,6 @@ private: // Need more types of state -/** - Local Port Mappings - -- Implemented in parent. - - struct LearnedState - { - Ptr associatedPort; - Time expirationTime; - }; - std::map m_learnState; -**/ - MooseAddress m_mooseAddress; // Remote Moose State @@ -84,17 +79,29 @@ private: { Ptr associatedPort; Time expirationTime; - }; - std::map m_remoteState; + }; // Local Moose State struct SuffixState { - Mac48Address ethernetAddr; + Mac48Address ethernet; + MooseSuffixAddress suffix; + Time expirationTime; + }; + + struct PortState + { + Ptr associatedPort; Time expirationTime; }; - std::map m_localState; + + Time m_expirationTime; + + std::map m_prefixState; + std::map m_suffixState; + std::map m_ethernetState; + std::map m_portState; }; diff --git a/code/src/devices/bridge/model/moose-bridge-port-net-device.cc b/code/src/devices/bridge/model/moose-bridge-port-net-device.cc index 59a74ca..e16bc0c 100644 --- a/code/src/devices/bridge/model/moose-bridge-port-net-device.cc +++ b/code/src/devices/bridge/model/moose-bridge-port-net-device.cc @@ -20,6 +20,7 @@ #include "moose-bridge-net-device.h" #include "bridge-port-net-device.h" #include "moose-bridge-port-net-device.h" +#include "ns3/moose-address.h" #include "ns3/node.h" #include "ns3/channel.h" #include "ns3/packet.h" @@ -27,6 +28,8 @@ #include "ns3/boolean.h" #include "ns3/simulator.h" #include "ns3/uinteger.h" +#include "ns3/arp-l3-protocol.h" +#include NS_LOG_COMPONENT_DEFINE ("MooseBridgePortNetDevice"); @@ -51,6 +54,60 @@ MooseBridgePortNetDevice::~MooseBridgePortNetDevice(){ NS_LOG_FUNCTION_NOARGS (); } +void MooseBridgePortNetDevice::Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber){ + NS_LOG_FUNCTION_NOARGS(); + NS_LOG_DEBUG("Send(device=" << m_device << ",packet=" << packet << ",src=" << src << ",dest=" << dest << ",port=" << this << ",protocol=" << protocolNumber); + + BridgePortNetDevice::Send(packet, src, dest, protocolNumber); } +void MooseBridgePortNetDevice::Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, NetDevice::PacketType packetType){ + NS_LOG_FUNCTION_NOARGS (); + NS_LOG_DEBUG("Receive(device=" << device << ",packet=" << packet << ",src=" << src << ",dst=" << dst << ",type=" << packetType << ",port=" << this << ",protocol=" << protocol << ")"); + + Mac48Address src48 = Mac48Address::ConvertFrom(src); + + MooseAddress msrc(src48); + + Ptr bridge = m_bridge->GetObject(); + + NS_ASSERT(bridge != NULL); + + MooseAddress::MooseType mt = msrc.GetMooseType(); + + if(mt == MooseAddress::MOOSE){ + // We have a MOOSE Packet. Let it continue. + + NS_LOG_LOGIC("MOOSE Packet Received"); + + bridge->Learn(msrc, this); + + bridge->Forward(this, packet, protocol, src, dst, packetType); + + } else if(mt == MooseAddress::HOST) { + + NS_LOG_LOGIC("Unicast Packet Received"); + + MooseAddress newsrc = bridge->ToMoose(msrc); + + packet->Print(std::cout); std::cout << std::endl; + if(protocol == ArpL3Protocol::PROT_NUMBER){ + // Need to do ARP rewriting. + + } + + bridge->Learn(newsrc, this); + + bridge->Forward(this, packet, protocol, newsrc.GetMacAddress(), dst, packetType); + + } else { + + NS_LOG_LOGIC("Multicast Packet Recieved"); + + bridge->Forward(this, packet, protocol, src, dst, packetType); + + } +} + +} diff --git a/code/src/devices/bridge/model/moose-bridge-port-net-device.h b/code/src/devices/bridge/model/moose-bridge-port-net-device.h index f0dfa78..a1a2b42 100644 --- a/code/src/devices/bridge/model/moose-bridge-port-net-device.h +++ b/code/src/devices/bridge/model/moose-bridge-port-net-device.h @@ -45,6 +45,9 @@ public: MooseBridgePortNetDevice(Ptr bridge, Ptr device, Ptr node); virtual ~MooseBridgePortNetDevice(); + virtual void Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber); + virtual void Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType); + static TypeId GetTypeId (void); }; @@ -52,3 +55,4 @@ public: } #endif + diff --git a/code/src/node/moose-address.cc b/code/src/node/moose-address.cc index ab300a0..e7802b1 100644 --- a/code/src/node/moose-address.cc +++ b/code/src/node/moose-address.cc @@ -57,9 +57,15 @@ const Mac48Address MooseAddress::GetMacAddress() const return mac; } -bool MooseAddress::IsMoose () const +MooseAddress::MooseType MooseAddress::GetMooseType () const { - return (m_address[0] & 0x02) == 0x02; + if((m_address[0] & 0x01) == 0x01){ + return GROUP; + } else if((m_address[0] & 0x02) == 0x02){ + return MOOSE; + } else { + return HOST; + } } MooseAddress MooseAddress::Allocate() @@ -82,7 +88,7 @@ MooseAddress MooseAddress::Allocate(MoosePrefixAddress prefix){ uint16_t key = prefix.GetInt(); - uint16_t val = ++ids[key]; + uint32_t val = ++ids[key]; MooseAddress moose; moose.m_address[0] = 0 | 0x02; // MOOSE Address @@ -98,5 +104,26 @@ MooseAddress MooseAddress::Allocate(MoosePrefixAddress prefix){ } +MooseAddress MooseAddress::Combine(MoosePrefixAddress prefix, MooseSuffixAddress suffix){ + + uint16_t key = prefix.GetInt(); + uint32_t val = suffix.GetInt(); + + MooseAddress moose; + + moose.m_address[0] = 0 | 0x02; // MOOSE Address + + moose.m_address[1] = (key >> 8) & 0xff; // Bridge + moose.m_address[2] = (key >> 0) & 0xff; + + moose.m_address[3] = (val >> 16) & 0xff; // Port + moose.m_address[4] = (val >> 8) & 0xff; + moose.m_address[5] = (val >> 0) & 0xff; + + return moose; + +} + + } diff --git a/code/src/node/moose-address.h b/code/src/node/moose-address.h index 74253de..1a0d7ef 100644 --- a/code/src/node/moose-address.h +++ b/code/src/node/moose-address.h @@ -42,11 +42,17 @@ public: MooseAddress (const char *str); MooseAddress (const Mac48Address &mac); + enum MooseType { + MOOSE, + HOST, + GROUP + }; + /** - * \returns true if this is a MOOSE address, false otherwise. + * \returns the MooseType of this address. */ - bool IsMoose() const; + MooseType GetMooseType() const; /** * \returns the MOOSE prefix @@ -76,6 +82,12 @@ public: */ static MooseAddress Allocate (MoosePrefixAddress prefix); + /** + * Form a Moose Address from Parts. + */ + + static MooseAddress Combine (MoosePrefixAddress prefix, MooseSuffixAddress suffix); + private: uint8_t m_address[6]; diff --git a/code/src/node/moose-suffix-address.cc b/code/src/node/moose-suffix-address.cc index 49dc814..512ffab 100644 --- a/code/src/node/moose-suffix-address.cc +++ b/code/src/node/moose-suffix-address.cc @@ -30,6 +30,12 @@ MooseSuffixAddress::MooseSuffixAddress(const uint8_t buffer[3]){ memcpy(m_address, buffer, 3); } - +uint32_t MooseSuffixAddress::GetInt() const { + uint32_t val = 0; + val = m_address[0]; + val += m_address[1] << 8; + val += m_address[2] << 16; + return val; +} } diff --git a/code/src/node/moose-suffix-address.h b/code/src/node/moose-suffix-address.h index 6827101..5b4e2b3 100644 --- a/code/src/node/moose-suffix-address.h +++ b/code/src/node/moose-suffix-address.h @@ -32,6 +32,8 @@ public: MooseSuffixAddress(); MooseSuffixAddress(const uint8_t buffer[3]); + uint32_t GetInt() const; + private: uint8_t m_address[6];