ethtool-util: introduce ethtool_get_permanent_hw_addr()

And replaces all ethtool_get_permanent_macaddr() with it.
This commit is contained in:
Yu Watanabe
2021-11-05 02:13:37 +09:00
parent 57ae8cd812
commit ed9fa69f1c
6 changed files with 31 additions and 45 deletions

View File

@@ -273,7 +273,7 @@ typedef struct LinkInfo {
int ifindex;
unsigned short iftype;
struct hw_addr_data hw_address;
struct ether_addr permanent_mac_address;
struct hw_addr_data permanent_hw_address;
uint32_t master;
uint32_t mtu;
uint32_t min_mtu;
@@ -348,7 +348,7 @@ typedef struct LinkInfo {
struct ether_addr bssid;
bool has_hw_address:1;
bool has_permanent_mac_address:1;
bool has_permanent_hw_address:1;
bool has_tx_queues:1;
bool has_rx_queues:1;
bool has_stats64:1;
@@ -556,11 +556,10 @@ static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns, b
netlink_message_read_hw_addr(m, IFLA_ADDRESS, &info->hw_address) >= 0 &&
info->hw_address.length > 0;
info->has_permanent_mac_address =
ethtool_get_permanent_macaddr(NULL, info->name, &info->permanent_mac_address) >= 0 &&
!ether_addr_is_null(&info->permanent_mac_address) &&
(info->hw_address.length != sizeof(struct ether_addr) ||
memcmp(&info->permanent_mac_address, info->hw_address.bytes, sizeof(struct ether_addr)) != 0);
info->has_permanent_hw_address =
ethtool_get_permanent_hw_addr(NULL, info->name, &info->permanent_hw_address) >= 0 &&
!hw_addr_is_null(&info->permanent_hw_address) &&
!hw_addr_equal(&info->permanent_hw_address, &info->hw_address);
(void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu);
(void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
@@ -1710,23 +1709,10 @@ static int link_status_one(
return r;
}
if (info->has_permanent_mac_address) {
_cleanup_free_ char *description = NULL;
(void) ieee_oui(hwdb, &info->permanent_mac_address, &description);
r = table_add_many(table,
TABLE_EMPTY,
TABLE_STRING, "Permanent Hardware Address:");
if (info->has_permanent_hw_address) {
r = dump_hw_address(table, hwdb, "Permanent Hardware Address:", &info->permanent_hw_address);
if (r < 0)
return table_log_add_error(r);
r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
ETHER_ADDR_TO_STR(&info->permanent_mac_address),
description ? " (" : "",
strempty(description),
description ? ")" : "");
if (r < 0)
return table_log_add_error(r);
return r;
}
if (info->mtu > 0) {

View File

@@ -1196,8 +1196,8 @@ static int link_get_network(Link *link, Network **ret) {
r = net_match_config(
&network->match,
link->sd_device,
&link->hw_addr.ether,
&link->permanent_mac,
link->hw_addr.length == ETH_ALEN ? &link->hw_addr.ether : NULL,
link->permanent_hw_addr.length == ETH_ALEN ? &link->permanent_hw_addr.ether : NULL,
link->driver,
link->iftype,
link->ifname,
@@ -2400,9 +2400,9 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
if (r < 0)
return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac);
r = ethtool_get_permanent_hw_addr(&manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
if (r < 0)
log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
if (r < 0)

View File

@@ -55,7 +55,7 @@ typedef struct Link {
char *state_file;
struct hw_addr_data hw_addr;
struct hw_addr_data bcast_addr;
struct ether_addr permanent_mac;
struct hw_addr_data permanent_hw_addr;
struct in6_addr ipv6ll_address;
uint32_t mtu;
uint32_t min_mtu;

View File

@@ -339,14 +339,14 @@ int ethtool_get_link_info(
return 0;
}
int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct ether_addr *ret) {
int ethtool_get_permanent_hw_addr(int *ethtool_fd, const char *ifname, struct hw_addr_data *ret) {
_cleanup_close_ int fd = -1;
struct {
struct ethtool_perm_addr addr;
uint8_t space[MAX_ADDR_LEN];
uint8_t space[HW_ADDR_MAX_SIZE];
} epaddr = {
.addr.cmd = ETHTOOL_GPERMADDR,
.addr.size = MAX_ADDR_LEN,
.addr.size = HW_ADDR_MAX_SIZE,
};
struct ifreq ifr = {
.ifr_data = (caddr_t) &epaddr,
@@ -367,17 +367,14 @@ int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct et
if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
return -errno;
if (epaddr.addr.size != 6)
return -EOPNOTSUPP;
if (epaddr.addr.size == 0)
return -ENODATA;
#pragma GCC diagnostic push
#if HAVE_ZERO_LENGTH_BOUNDS
# pragma GCC diagnostic ignored "-Wzero-length-bounds"
#endif
for (size_t i = 0; i < epaddr.addr.size; i++)
ret->ether_addr_octet[i] = epaddr.addr.data[i];
#pragma GCC diagnostic pop
if (epaddr.addr.size > HW_ADDR_MAX_SIZE)
return -EINVAL;
ret->length = epaddr.addr.size;
memcpy(ret->bytes, epaddr.addr.data, epaddr.addr.size);
return 0;
}

View File

@@ -6,6 +6,7 @@
#include <linux/ethtool.h>
#include "conf-parser.h"
#include "ether-addr-util.h"
#define N_ADVERTISE 3
@@ -163,7 +164,7 @@ int ethtool_get_driver(int *ethtool_fd, const char *ifname, char **ret);
int ethtool_get_link_info(int *ethtool_fd, const char *ifname,
int *ret_autonegotiation, uint64_t *ret_speed,
Duplex *ret_duplex, NetDevPort *ret_port);
int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct ether_addr *ret);
int ethtool_get_permanent_hw_addr(int *ethtool_fd, const char *ifname, struct hw_addr_data *ret);
int ethtool_set_wol(int *ethtool_fd, const char *ifname, uint32_t wolopts, const uint8_t password[SOPASS_MAX]);
int ethtool_set_nic_buffer_size(int *ethtool_fd, const char *ifname, const netdev_ring_param *ring);
int ethtool_set_features(int *ethtool_fd, const char *ifname, const int features[static _NET_DEV_FEAT_MAX]);

View File

@@ -349,7 +349,7 @@ bool link_config_should_reload(LinkConfigContext *ctx) {
int link_config_get(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device, LinkConfig **ret) {
unsigned name_assign_type = NET_NAME_UNKNOWN;
struct ether_addr permanent_mac = {};
struct hw_addr_data permanent_hw_addr = {};
unsigned short iftype;
LinkConfig *link;
const char *name;
@@ -377,14 +377,16 @@ int link_config_get(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device
if (flags & IFF_LOOPBACK)
return -ENOENT;
r = ethtool_get_permanent_macaddr(&ctx->ethtool_fd, name, &permanent_mac);
r = ethtool_get_permanent_hw_addr(&ctx->ethtool_fd, name, &permanent_hw_addr);
if (r < 0)
log_device_debug_errno(device, r, "Failed to get permanent MAC address, ignoring: %m");
log_device_debug_errno(device, r, "Failed to get permanent hardware address, ignoring: %m");
(void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type);
LIST_FOREACH(links, link, ctx->links) {
r = net_match_config(&link->match, device, NULL, &permanent_mac, NULL, iftype, NULL, NULL, 0, NULL, NULL);
r = net_match_config(&link->match, device, NULL,
permanent_hw_addr.length == ETH_ALEN ? &permanent_hw_addr.ether : NULL,
NULL, iftype, NULL, NULL, 0, NULL, NULL);
if (r < 0)
return r;
if (r == 0)