Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
tools: add method to get interface speed
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Maz <[email protected]>
  • Loading branch information
mariomaz committed Jan 20, 2020
1 parent 9e895bc commit 3cf23b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/beerocks/bcl/include/bcl/network/network_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 51 additions & 0 deletions common/beerocks/bcl/source/network/network_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <linux/ethtool.h>
#include <linux/if_bridge.h>
#include <linux/if_ether.h> // ETH_P_ARP = 0x0806
#include <linux/if_packet.h> // struct sockaddr_ll (see man 7 packet)
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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::ip_info> network_utils::get_ip_list()
{
std::vector<network_utils::ip_info> ip_list;
Expand Down

0 comments on commit 3cf23b9

Please sign in to comment.