1
1

opal/util: add function to obtain interface speed

If kernel ethtool_cmd_speed() is not available, use copies if possible.

Signed-off-by: Karol Mroz <mroz.karol@gmail.com>
Этот коммит содержится в:
Karol Mroz 2016-05-04 08:21:25 +02:00 коммит произвёл Karol Mroz
родитель 8f34329efe
Коммит 31e33a64f9
3 изменённых файлов: 105 добавлений и 0 удалений

Просмотреть файл

@ -43,6 +43,7 @@ headers = \
cmd_line.h \
crc.h \
daemon_init.h \
ethtool.h \
error.h \
fd.h \
few.h \
@ -78,6 +79,7 @@ libopalutil_la_SOURCES = \
cmd_line.c \
crc.c \
daemon_init.c \
ethtool.c \
error.c \
fd.c \
few.c \

83
opal/util/ethtool.c Обычный файл
Просмотреть файл

@ -0,0 +1,83 @@
/*
* Copyright (c) 2016 Karol Mroz. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <string.h>
#include <limits.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_LINUX_ETHTOOL_H
#include <linux/ethtool.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LINUX_SOCKIOS_H
#include <linux/sockios.h>
#endif
#include "opal/util/ethtool.h"
#include "opal/util/if.h"
#if !defined(HAVE_DECL_ETHTOOL_CMD_SPEED)
static inline unsigned int
ethtool_cmd_speed(const struct ethtool_cmd *ep)
{
return (ep->speed_hi << 16) | ep->speed;
}
#endif
/*
* Obtain an appropriate bandwidth for the interface if_name. On Linux, we
* get this via an ioctl(). Elsewhere or in the error case, we return the
* speed as 0.
*/
unsigned int
opal_ethtool_get_speed (const char *if_name)
{
unsigned int speed = 0;
#if defined(HAVE_DECL_SIOCETHTOOL) && defined(HAVE_STRUCT_IFREQ) && defined(HAVE_STRUCT_ETHTOOL_CMD)
int sockfd;
struct ifreq ifr;
struct ethtool_cmd edata = {
.cmd = ETHTOOL_GSET,
};
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
goto out;
}
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, if_name, IF_NAMESIZE);
ifr.ifr_data = (char *)&edata;
if (ioctl(sockfd, SIOCETHTOOL, &ifr) < 0) {
goto out;
}
speed = ethtool_cmd_speed(&edata);
if (UINT_MAX == speed) {
speed = 0;
}
out:
close(sockfd);
return speed;
#else
return speed;
#endif
}

20
opal/util/ethtool.h Обычный файл
Просмотреть файл

@ -0,0 +1,20 @@
/*
* Copyright (c) 2016 Karol Mroz. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#ifndef OPAL_ETHTOOL_H
#define OPAL_ETHTOOL_H
/*
* Obtain an appropriate bandwidth for the interface if_name. On Linux, we
* get this via an ioctl(). Elsewhere or in the error case, we return the
* speed as 0.
*/
unsigned int opal_ethtool_get_speed(const char *if_name);
#endif