return m_ports.size ();
}
-/** Dislike this. */
+/** Dislike this slightly less now. */
Ptr<NetDevice>
BridgeNetDevice::GetBridgePort (uint32_t n) const
{
NS_LOG_FUNCTION_NOARGS ();
- return m_ports[n]->m_device;
+ return m_ports[n]->GetDevice();
}
void
virtual bool SupportsSendFrom () const;
virtual Address GetMulticast (Ipv6Address addr) const;
+ virtual void Forward (Ptr<BridgePortNetDevice> port, Ptr<const Packet> packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType);
+ virtual void Learn (Address const &src, Ptr<BridgePortNetDevice> port);
+
protected:
virtual void DoDispose (void);
- virtual void Forward (Ptr<BridgePortNetDevice> port, Ptr<const Packet> packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType);
virtual void ForwardUnicast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
virtual void ForwardBroadcast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
- virtual void Learn (Address const &src, Ptr<BridgePortNetDevice> port);
virtual Ptr<BridgePortNetDevice> GetLearnedState (Mac48Address source);
virtual Ptr<BridgePortNetDevice> CreateBridgePort(Ptr<BridgeNetDevice> bridge, Ptr<NetDevice> device, Ptr<Node> node);
uint16_t m_mtu;
bool m_enableLearning;
- friend class BridgePortNetDevice;
-
};
} // namespace ns3
m_device->SendFrom(packet->Copy(), source, dest, protocolNumber);
}
+Ptr<NetDevice> BridgePortNetDevice::GetDevice(){
+ return m_device;
+}
+
/**
* By moving learning to the port, we can have some ports which learn, and some which don't.
**/
static TypeId GetTypeId (void);
+ Ptr<NetDevice> GetDevice();
+
+ virtual void Send (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber);
+
protected:
- void Send (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber);
- void Receive (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType);
-
-private:
+ virtual void Receive (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType);
+
Ptr<BridgeNetDevice> m_bridge;
Ptr<NetDevice> m_device;
-
- friend class BridgeNetDevice;
-
};
} // namespace ns3
* 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 <ns3@richardwhiuk.com>
#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");
return CreateObject<MooseBridgePortNetDevice>(bridge, device, node);
}
-/*
-//bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
-//bool SendFrom (Ptr<Packet> 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<MooseSuffixAddress, SuffixState*>::iterator iter = m_ethernetState.find(addr.GetMooseSuffix());
+
+ if(iter == m_ethernetState.end()){
+
+ // Expired state - must broadcast.
+
+ return MooseAddress(Mac48Address::GetBroadcast());
+
+ } else {
-void Forward (Ptr<BridgePortNetDevice> port, Ptr<const Packet> 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<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
-void ForwardBroadcast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> 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<Mac48Address, SuffixState>::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<BridgePortNetDevice> 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<BridgePortNetDevice> 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<MoosePrefixAddress, PrefixState>::iterator iter = m_prefixState.find(addr.GetMoosePrefix());
+
+ if(iter != m_prefixState.end()){
+ return iter->second.associatedPort;
+ }
+ } else {
+ std::map<MooseSuffixAddress, PortState>::iterator iter = m_portState.find(addr.GetMooseSuffix());
+
+ if(iter != m_portState.end()){
+ return iter->second.associatedPort;
+ }
+ }
+
+ return NULL;
+}
+
+
+void
+MooseBridgeNetDevice::ForwardUnicast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
+{
+ NS_LOG_FUNCTION_NOARGS ();
+
+ // Get Port
+
+ MooseAddress mdst = MooseAddress(dst);
+
+ Ptr<BridgePortNetDevice> 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> packet, const Address& dest, uint16_t protocolNumber);
+//bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
void Learn (Address const &src, Ptr<BridgePortNetDevice> port);
Ptr<BridgePortNetDevice> GetLearnedState (Mac48Address source);*/
MooseBridgeNetDevice ();
virtual ~MooseBridgeNetDevice ();
+ void Learn(MooseAddress const& addr, Ptr<BridgePortNetDevice> port);
+ Ptr<BridgePortNetDevice> GetLearnedPort(MooseAddress const& addr);
+ MooseAddress ToMoose(MooseAddress const& addr);
+ MooseAddress FromMoose(MooseAddress const& addr);
+
+
protected:
+ virtual void ForwardUnicast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
+
// From NetDevice
// Unconvinced we need these...
/*
virtual void Forward (Ptr<BridgePortNetDevice> port, Ptr<const Packet> packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType);
- virtual void ForwardUnicast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
virtual void ForwardBroadcast (Ptr<BridgePortNetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol, Mac48Address src, Mac48Address dst);
virtual void Learn (Address const &src, Ptr<BridgePortNetDevice> port);
virtual Ptr<BridgePortNetDevice> GetLearnedState (Mac48Address source); */
// Need more types of state
-/**
- Local Port Mappings
- -- Implemented in parent.
-
- struct LearnedState
- {
- Ptr<BridgePortNetDevice> associatedPort;
- Time expirationTime;
- };
- std::map<Mac48Address, LearnedState> m_learnState;
-**/
-
MooseAddress m_mooseAddress;
// Remote Moose State
{
Ptr<BridgePortNetDevice> associatedPort;
Time expirationTime;
- };
- std::map<MoosePrefixAddress, PrefixState> m_remoteState;
+ };
// Local Moose State
struct SuffixState
{
- Mac48Address ethernetAddr;
+ Mac48Address ethernet;
+ MooseSuffixAddress suffix;
+ Time expirationTime;
+ };
+
+ struct PortState
+ {
+ Ptr<BridgePortNetDevice> associatedPort;
Time expirationTime;
};
- std::map<MooseSuffixAddress, PrefixState> m_localState;
+
+ Time m_expirationTime;
+
+ std::map<MoosePrefixAddress, PrefixState> m_prefixState;
+ std::map<Mac48Address, SuffixState> m_suffixState;
+ std::map<MooseSuffixAddress, SuffixState*> m_ethernetState;
+ std::map<MooseSuffixAddress, PortState> m_portState;
};
#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"
#include "ns3/boolean.h"
#include "ns3/simulator.h"
#include "ns3/uinteger.h"
+#include "ns3/arp-l3-protocol.h"
+#include <iostream>
NS_LOG_COMPONENT_DEFINE ("MooseBridgePortNetDevice");
NS_LOG_FUNCTION_NOARGS ();
}
+void MooseBridgePortNetDevice::Send (Ptr<Packet> 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<NetDevice> device, Ptr<const Packet> 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<MooseBridgeNetDevice> bridge = m_bridge->GetObject<MooseBridgeNetDevice>();
+
+ 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);
+
+ }
+}
+
+}
MooseBridgePortNetDevice(Ptr<BridgeNetDevice> bridge, Ptr<NetDevice> device, Ptr<Node> node);
virtual ~MooseBridgePortNetDevice();
+ virtual void Send (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber);
+ virtual void Receive (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &source, Address const &destination, NetDevice::PacketType packetType);
+
static TypeId GetTypeId (void);
};
}
#endif
+
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()
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
}
+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;
+
+}
+
+
}
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
*/
static MooseAddress Allocate (MoosePrefixAddress prefix);
+ /**
+ * Form a Moose Address from Parts.
+ */
+
+ static MooseAddress Combine (MoosePrefixAddress prefix, MooseSuffixAddress suffix);
+
private:
uint8_t m_address[6];
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;
+}
}
MooseSuffixAddress();
MooseSuffixAddress(const uint8_t buffer[3]);
+ uint32_t GetInt() const;
+
private:
uint8_t m_address[6];