bug 1777: TapBridge address learning patch (modified by Tom Goff)

This commit is contained in:
Piotr Jurkiewicz
2013-11-19 06:50:18 -08:00
parent a3f979febf
commit adb677eddd
2 changed files with 37 additions and 21 deletions

View File

@@ -39,29 +39,13 @@
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <cerrno>
#include <limits>
#include <cstdlib>
#include <unistd.h>
//
// Sometimes having a tap-creator is actually more trouble than solution. In
// these cases you can uncomment the define of TAP_CREATOR below and the
// simulation will just use a device you have preconfigured. This is useful
// if you are running in an environment where you have got to run as root,
// such as ORBIT or CORE.
//
// #define NO_CREATOR
#ifdef NO_CREATOR
#include <fcntl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#endif
NS_LOG_COMPONENT_DEFINE ("TapBridge");
namespace ns3 {
@@ -631,7 +615,7 @@ TapBridge::CreateTap (void)
int *rawSocket = (int*)CMSG_DATA (cmsg);
NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket);
m_sock = *rawSocket;
return;
break;
}
else
{
@@ -639,8 +623,40 @@ TapBridge::CreateTap (void)
}
}
}
NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
if (cmsg == NULL)
{
NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
}
if (m_mode == USE_LOCAL || m_mode == USE_BRIDGE)
{
//
// Set the ns-3 device's mac address to the overlying container's
// mac address
//
struct ifreq s;
strncpy (s.ifr_name, m_tapDeviceName.c_str (), sizeof (s.ifr_name));
NS_LOG_INFO ("Trying to get MacAddr of " << m_tapDeviceName);
int ioctlResult = ioctl (sock, SIOCGIFHWADDR, &s);
if (ioctlResult == 0)
{
Mac48Address learnedMac;
learnedMac.CopyFrom ((uint8_t *)s.ifr_hwaddr.sa_data);
NS_LOG_INFO ("Learned Tap device MacAddr is " << learnedMac << ": setting ns-3 device to use this address");
m_bridgedDevice->SetAddress (learnedMac);
m_ns3AddressRewritten = true;
}
if (!m_ns3AddressRewritten)
{
NS_LOG_INFO ("Cannot get MacAddr of Tap device: " << m_tapDeviceName << " while in USE_LOCAL/USE_BRIDGE mode: " << std::strerror (errno));
NS_LOG_INFO ("Underlying ns-3 device will continue to use default address, what can lead to connectivity errors");
}
}
}
close (sock);
}
void

View File

@@ -448,7 +448,7 @@ private:
* \internal
*
* Whether the MAC address of the underlying ns-3 device has already been
* rewritten is stored in this variable (for UseLocal mode only).
* rewritten is stored in this variable (for UseLocal/UseBridge mode only).
*/
bool m_ns3AddressRewritten;