From 3cf23b94e162f152b3973bfce6ac466a69ff9fcf Mon Sep 17 00:00:00 2001 From: Mario Maz Date: Mon, 20 Jan 2020 17:49:53 +0100 Subject: [PATCH] tools: add method to get interface speed Signed-off-by: Mario Maz --- .../bcl/include/bcl/network/network_utils.h | 1 + .../bcl/source/network/network_utils.cpp | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/common/beerocks/bcl/include/bcl/network/network_utils.h b/common/beerocks/bcl/include/bcl/network/network_utils.h index a9a102e30d..d2a0336342 100644 --- a/common/beerocks/bcl/include/bcl/network/network_utils.h +++ b/common/beerocks/bcl/include/bcl/network/network_utils.h @@ -115,6 +115,7 @@ class network_utils { static bool linux_iface_exists(const std::string &iface); static bool linux_iface_is_up(const std::string &iface); static bool linux_iface_is_up_and_running(const std::string &iface); + static bool linux_iface_get_speed(const std::string &iface, uint32_t &speed); static bool arp_send(const std::string &iface, const std::string &dst_ip, const std::string &src_ip, sMacAddr dst_mac, sMacAddr src_mac, int count, diff --git a/common/beerocks/bcl/source/network/network_utils.cpp b/common/beerocks/bcl/source/network/network_utils.cpp index 9acc5f8fc9..58caa20a28 100644 --- a/common/beerocks/bcl/source/network/network_utils.cpp +++ b/common/beerocks/bcl/source/network/network_utils.cpp @@ -14,12 +14,15 @@ #include #include #include +#include #include #include // ETH_P_ARP = 0x0806 #include // struct sockaddr_ll (see man 7 packet) #include #include +#include #include +#include #include #include #include @@ -767,6 +770,54 @@ bool network_utils::linux_iface_is_up_and_running(const std::string &iface) return (false); } +bool network_utils::linux_iface_get_speed(const std::string &iface, uint32_t &speed) +{ + int sock; + bool result = false; + + speed = SPEED_UNKNOWN; + + sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + if (sock < 0) { + LOG(ERROR) << "Can't create SOCK_DGRAM socket: " << strerror(errno); + } else { + struct ifreq ifr; + struct ethtool_link_settings settings; + int rc; + + beerocks::string_utils::copy_string(ifr.ifr_name, iface.c_str(), sizeof(ifr.ifr_name)); + ifr.ifr_data = (char *)&settings; + + settings.cmd = ETHTOOL_GSET; + + rc = ioctl(sock, SIOCETHTOOL, &ifr); + if (0 == rc) { + speed = settings.speed; + } else if (EOPNOTSUPP == errno) { + struct ethtool_cmd edata; + + ifr.ifr_data = (char *)&edata; + + edata.cmd = ETHTOOL_GSET; + + rc = ioctl(sock, SIOCETHTOOL, &ifr); + if (0 == rc) { + speed = ethtool_cmd_speed(&edata); + } + } + + if (rc < 0) { + LOG(ERROR) << "ioctl failed: " << strerror(errno); + } else { + result = true; + } + + close(sock); + } + + return result; +} + std::vector network_utils::get_ip_list() { std::vector ip_list;