|
Lines 131-137
TapBridge::TapBridge ()
|
Link Here
|
|---|
|
| 131 |
m_startEvent (), |
131 |
m_startEvent (), |
| 132 |
m_stopEvent (), |
132 |
m_stopEvent (), |
| 133 |
m_readThread (0), |
133 |
m_readThread (0), |
| 134 |
m_learnedMac (Mac48Address ("ff:ff:ff:ff:ff:ff")) |
134 |
m_ns3AddressRewritten (false) |
| 135 |
{ |
135 |
{ |
| 136 |
NS_LOG_FUNCTION_NOARGS (); |
136 |
NS_LOG_FUNCTION_NOARGS (); |
| 137 |
Start (m_tStart); |
137 |
Start (m_tStart); |
|
Lines 641-647
TapBridge::ReadThread (void)
|
Link Here
|
|---|
|
| 641 |
return; |
641 |
return; |
| 642 |
} |
642 |
} |
| 643 |
|
643 |
|
| 644 |
NS_LOG_INFO ("TapBridge::ReadThread(): Received packet"); |
644 |
NS_LOG_INFO ("TapBridge::ReadThread(): Received packet on node " << m_node->GetId ()); |
| 645 |
NS_LOG_INFO ("TapBridge::ReadThread(): Scheduling handler"); |
645 |
NS_LOG_INFO ("TapBridge::ReadThread(): Scheduling handler"); |
| 646 |
DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow ( |
646 |
DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow ( |
| 647 |
MakeEvent (&TapBridge::ForwardToBridgedDevice, this, buf, len)); |
647 |
MakeEvent (&TapBridge::ForwardToBridgedDevice, this, buf, len)); |
|
Lines 713-733
TapBridge::ForwardToBridgedDevice (uint8
|
Link Here
|
|---|
|
| 713 |
// |
713 |
// |
| 714 |
NS_ASSERT_MSG (Mac48Address::ConvertFrom (src) != Mac48Address ("ff:ff:ff:ff:ff:ff"), |
714 |
NS_ASSERT_MSG (Mac48Address::ConvertFrom (src) != Mac48Address ("ff:ff:ff:ff:ff:ff"), |
| 715 |
"TapBridge::ForwardToBridgedDevice: Source addr is broadcast"); |
715 |
"TapBridge::ForwardToBridgedDevice: Source addr is broadcast"); |
| 716 |
// |
716 |
if (m_ns3AddressRewritten == false) |
| 717 |
// Remember the Mac address since we are going to spoof it when we go |
717 |
{ |
| 718 |
// the other way. |
718 |
// |
| 719 |
// |
719 |
// Set the ns-3 device's mac address to the overlying container's |
| 720 |
m_learnedMac = Mac48Address::ConvertFrom (src); |
720 |
// mac address |
| 721 |
NS_LOG_LOGIC ("Learned MacAddr is " << m_learnedMac); |
721 |
// |
| 722 |
|
722 |
Mac48Address learnedMac = Mac48Address::ConvertFrom (src); |
|
|
723 |
NS_LOG_LOGIC ("Learned MacAddr is " << learnedMac << ": setting ns-3 device to use this address"); |
| 724 |
m_bridgedDevice->SetAddress (Mac48Address::ConvertFrom (learnedMac)); |
| 725 |
m_ns3AddressRewritten = true; |
| 726 |
} |
| 723 |
// |
727 |
// |
| 724 |
// If we are operating in USE_LOCAL mode, we may be attached to an ns-3 |
728 |
// If we are operating in USE_LOCAL mode, we may be attached to an ns-3 |
| 725 |
// device that does not support bridging (SupportsSendFrom returns false). |
729 |
// device that does not support bridging (SupportsSendFrom returns false). |
| 726 |
// The whole point of this mode is really to support this case. We allow |
730 |
// But, since the mac addresses are now aligned, we can call Send() |
| 727 |
// only packets from one source MAC to flow across the TapBridge in this |
|
|
| 728 |
// mode and will spoof that address when packets flow the other way. |
| 729 |
// Since we will be doing this spoofing, we can relax the normal bridged |
| 730 |
// device requirement to support SendFrom and use Send. |
| 731 |
// |
731 |
// |
| 732 |
NS_LOG_LOGIC ("Forwarding packet to ns-3 device via Send()"); |
732 |
NS_LOG_LOGIC ("Forwarding packet to ns-3 device via Send()"); |
| 733 |
m_bridgedDevice->Send (packet, dst, type); |
733 |
m_bridgedDevice->Send (packet, dst, type); |
|
Lines 864-883
TapBridge::SetBridgedNetDevice (Ptr<NetD
|
Link Here
|
|---|
|
| 864 |
} |
864 |
} |
| 865 |
|
865 |
|
| 866 |
// |
866 |
// |
| 867 |
// Tell the bridged device to forward its received packets here. We use the |
867 |
// We need to disconnect the bridged device from the internet stack on our |
| 868 |
// promiscuous mode hook to get both the source and destination addresses. |
868 |
// node to ensure that only one stack responds to packets inbound over the |
|
|
869 |
// bridged device. That one stack lives outside ns-3 so we just blatantly |
| 870 |
// steal the device callbacks. |
| 869 |
// |
871 |
// |
| 870 |
m_node->RegisterProtocolHandler (MakeCallback (&TapBridge::ReceiveFromBridgedDevice, this), 0, bridgedDevice, true); |
872 |
// N.B This can be undone if someone does a RegisterProtocolHandler later |
|
|
873 |
// on this node. |
| 874 |
// |
| 875 |
bridgedDevice->SetReceiveCallback (MakeCallback (&TapBridge::DiscardFromBridgedDevice, this)); |
| 876 |
bridgedDevice->SetPromiscReceiveCallback (MakeCallback (&TapBridge::ReceiveFromBridgedDevice, this)); |
| 871 |
m_bridgedDevice = bridgedDevice; |
877 |
m_bridgedDevice = bridgedDevice; |
| 872 |
} |
878 |
} |
| 873 |
|
879 |
|
| 874 |
void |
880 |
bool |
|
|
881 |
TapBridge::DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, const Address &src) |
| 882 |
{ |
| 883 |
NS_LOG_FUNCTION (device << packet << protocol << src); |
| 884 |
NS_LOG_LOGIC ("Discarding packet stolen from bridged device " << device); |
| 885 |
return true; |
| 886 |
} |
| 887 |
|
| 888 |
bool |
| 875 |
TapBridge::ReceiveFromBridgedDevice ( |
889 |
TapBridge::ReceiveFromBridgedDevice ( |
| 876 |
Ptr<NetDevice> device, |
890 |
Ptr<NetDevice> device, |
| 877 |
Ptr<const Packet> packet, |
891 |
Ptr<const Packet> packet, |
| 878 |
uint16_t protocol, |
892 |
uint16_t protocol, |
| 879 |
Address const &src, |
893 |
const Address &src, |
| 880 |
Address const &dst, |
894 |
const Address &dst, |
| 881 |
PacketType packetType) |
895 |
PacketType packetType) |
| 882 |
{ |
896 |
{ |
| 883 |
NS_LOG_FUNCTION (device << packet << protocol << src << dst << packetType); |
897 |
NS_LOG_FUNCTION (device << packet << protocol << src << dst << packetType); |
|
Lines 913-947
TapBridge::ReceiveFromBridgedDevice (
|
Link Here
|
|---|
|
| 913 |
// we want to act like a bridge and forward these PACKET_OTHERHOST |
927 |
// we want to act like a bridge and forward these PACKET_OTHERHOST |
| 914 |
// packets. |
928 |
// packets. |
| 915 |
// |
929 |
// |
| 916 |
return; |
930 |
return true; |
| 917 |
} |
931 |
} |
| 918 |
|
932 |
|
| 919 |
// |
|
|
| 920 |
// We have received a packet from the ns-3 net device that has been associated |
| 921 |
// with this bridge. We want to take these bits and send them off to the tap |
| 922 |
// device on the Linux host. The only question we have to answer is, what |
| 923 |
// should the destination address be? |
| 924 |
// |
| 925 |
// If we are in CONFIGURE_LOCAL mode, then the destination address is just |
| 926 |
// left alone since it can only be the shared single MAC address, broadcast |
| 927 |
// or multicast. |
| 928 |
// |
| 929 |
// If we are in USE_LOCAL mode, then we need to spoof the destination |
| 930 |
// address with the one we saved. |
| 931 |
// |
| 932 |
// If we are in USE_BRIDGE mode, then we need to do the equvalent of a |
| 933 |
// SendFrom and leave the source and destination alone. |
| 934 |
// |
| 935 |
Mac48Address from = Mac48Address::ConvertFrom (src); |
933 |
Mac48Address from = Mac48Address::ConvertFrom (src); |
| 936 |
Mac48Address to; |
934 |
Mac48Address to = Mac48Address::ConvertFrom (dst); |
| 937 |
if (m_mode == USE_LOCAL) |
|
|
| 938 |
{ |
| 939 |
to = Mac48Address::ConvertFrom (m_learnedMac); |
| 940 |
} |
| 941 |
else |
| 942 |
{ |
| 943 |
to = Mac48Address::ConvertFrom (dst); |
| 944 |
} |
| 945 |
|
935 |
|
| 946 |
Ptr<Packet> p = packet->Copy (); |
936 |
Ptr<Packet> p = packet->Copy (); |
| 947 |
EthernetHeader header = EthernetHeader (false); |
937 |
EthernetHeader header = EthernetHeader (false); |
|
Lines 956-964
TapBridge::ReceiveFromBridgedDevice (
|
Link Here
|
|---|
|
| 956 |
NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ()); |
946 |
NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ()); |
| 957 |
NS_LOG_LOGIC ("Pkt LengthType is " << header.GetLengthType ()); |
947 |
NS_LOG_LOGIC ("Pkt LengthType is " << header.GetLengthType ()); |
| 958 |
NS_LOG_LOGIC ("Pkt size is " << p->GetSize ()); |
948 |
NS_LOG_LOGIC ("Pkt size is " << p->GetSize ()); |
|
|
949 |
NS_LOG_LOGIC ("End of receive packet handling on node " << m_node->GetId ()); |
| 959 |
|
950 |
|
| 960 |
uint32_t bytesWritten = write (m_sock, p->PeekData (), p->GetSize ()); |
951 |
uint32_t bytesWritten = write (m_sock, p->PeekData (), p->GetSize ()); |
| 961 |
NS_ABORT_MSG_IF (bytesWritten != p->GetSize (), "TapBridge::ReceiveFromBridgedDevice(): Write error."); |
952 |
NS_ABORT_MSG_IF (bytesWritten != p->GetSize (), "TapBridge::ReceiveFromBridgedDevice(): Write error."); |
|
|
953 |
return true; |
| 962 |
} |
954 |
} |
| 963 |
|
955 |
|
| 964 |
void |
956 |
void |
|
Lines 980-985
TapBridge::GetChannel (void) const
|
Link Here
|
|---|
|
| 980 |
{ |
972 |
{ |
| 981 |
NS_LOG_FUNCTION_NOARGS (); |
973 |
NS_LOG_FUNCTION_NOARGS (); |
| 982 |
return 0; |
974 |
return 0; |
|
|
975 |
} |
| 976 |
|
| 977 |
void |
| 978 |
TapBridge::SetAddress (Address address) |
| 979 |
{ |
| 980 |
NS_LOG_FUNCTION (address); |
| 981 |
m_address = Mac48Address::ConvertFrom (address); |
| 983 |
} |
982 |
} |
| 984 |
|
983 |
|
| 985 |
Address |
984 |
Address |