[+] INetAdapter::GetTransmitBytesPerSec()

[+] INetAdapter::GetReceiveBytesPerSec()
This commit is contained in:
Reece Wilson 2023-12-01 13:22:44 +00:00
parent eceb037e70
commit 81871ed0b8
5 changed files with 80 additions and 5 deletions

View File

@ -35,6 +35,8 @@ namespace Aurora::IO::Net
virtual bool HasDHCP() = 0;
virtual AuUInt32 GetMTU() = 0;
virtual AuUInt64 GetTransmitBytesPerSec() = 0;
virtual AuUInt64 GetReceiveBytesPerSec() = 0;
virtual AuList<IPAddress> GetDNS() = 0;
};

View File

@ -9,10 +9,15 @@
#include "AuNetAdapter.hpp"
#include "AuNetEndpoint.hpp"
#include <asm/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_arp.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/ethtool.h>
namespace Aurora::IO::Net
{
@ -375,6 +380,45 @@ namespace Aurora::IO::Net
return ret;
}
static void PatchAdapterSpeed(int family, NetAdapter &adapter)
{
struct ethtool_cmd edata;
int iSocket {};
{
iSocket = ::socket(PF_INET,
SOCK_DGRAM | SOCK_CLOEXEC,
IPPROTO_IP);
if (iSocket < 0)
{
return;
}
}
{
struct ifreq ifr;
strncpy(ifr.ifr_name, adapter.device.c_str(), sizeof(ifr.ifr_name));
ifr.ifr_data = &edata;
edata.cmd = ETHTOOL_GSET;
auto iRet = ::ioctl(sock, SIOCETHTOOL, &ifr);
if (iRet < 0)
{
::close(iSocket);
return;
}
}
{
auto uBytesPerSecond =
((AuUInt)(ethtool_cmd_speed(&edata))) * 1000 / 8;
adapter.uTransmitBytesPerSec = uBytesPerSecond;
adapter.uReceiveBytesPerSec = uBytesPerSecond;
}
::close(iSocket);
}
void NetlinkDevice::UpdateAdapterInfo(int family, NetAdapter &adapter)
{
static const auto kAddrBufferSize = 12 * 1024;
@ -487,20 +531,32 @@ namespace Aurora::IO::Net
{
adapter.device = (const char *)RTA_DATA(attr);
}
if (attr->rta_type == IFLA_MTU)
else if (attr->rta_type == IFLA_MTU)
{
adapter.mtu = *(unsigned int *)RTA_DATA(attr);
}
if (attr->rta_type == IFLA_ADDRESS)
else if (attr->rta_type == IFLA_ADDRESS)
{
if (dstlen <= adapter.mac.size())
{
AuMemcpy(adapter.mac.begin(), (const char *)RTA_DATA(attr), dstlen);
}
}
#if 0
else if (attr->rta_type == IFLA_STATS)
{
auto pStats = (struct rtnl_link_stats *)RTA_DATA(attr);
adapter.uTransmitBytesPerSec = pStats->tx_bytes;
adapter.uReceiveBytesPerSec = pStats->rx_bytes;
}
else if (attr->rta_type == IFLA_STATS64)
{
auto pStats64 = (struct rtnl_link_stats64 *)RTA_DATA(attr);
adapter.uTransmitBytesPerSec = pStats64->tx_bytes;
adapter.uReceiveBytesPerSec = pStats64->rx_bytes;
}
}
#endif
}
}
}
@ -557,6 +613,7 @@ namespace Aurora::IO::Net
continue;
}
PatchAdapterSpeed(family, *pAdapter.get());
ret.push_back(pAdapter);
}

View File

@ -235,6 +235,8 @@ namespace Aurora::IO::Net
}
adapter.dhcp = pCurrAddresses->Dhcpv4Enabled;
adapter.uTransmitBytesPerSec = pCurrAddresses->TransmitLinkSpeed / 8;
adapter.uReceiveBytesPerSec = pCurrAddresses->ReceiveLinkSpeed / 8;
}
if (adapter.address.ip == EIPProtocol::eIPProtocolV4)

View File

@ -87,4 +87,14 @@ namespace Aurora::IO::Net
{
return this->mtu;
}
AuUInt64 NetAdapter::GetTransmitBytesPerSec()
{
return this->uTransmitBytesPerSec;
}
AuUInt64 NetAdapter::GetReceiveBytesPerSec()
{
return this->uReceiveBytesPerSec;
}
}

View File

@ -34,6 +34,8 @@ namespace Aurora::IO::Net
bool HasDHCP() override;
AuUInt32 GetMTU() override;
AuUInt64 GetTransmitBytesPerSec() override;
AuUInt64 GetReceiveBytesPerSec() override;
// FriendlyName
@ -53,6 +55,8 @@ namespace Aurora::IO::Net
ENetworkAdapterStatus eNetworkStatus { ENetworkAdapterStatus::eUp };
bool dhcp {};
AuArray<AuUInt8, 8> mac;
AuUInt64 uTransmitBytesPerSec { 10 * 1000 * 1000 / 8 };
AuUInt64 uReceiveBytesPerSec { 10 * 1000 * 1000 / 8 };
AuUInt32 mtu;
AuList<IPAddress> dns;