network/netdev: introduce netdev_can_set_mac/mtu() helper functions

Several netdevs cannot set IFLA_ADDRESS or IFLA_MTU attribute on update.
Currently, the vtable field is unused, as we do not support updating
existing netdevs. Preparation for later commits.
This commit is contained in:
Yu Watanabe
2024-10-31 01:06:25 +09:00
parent 58ada3eab2
commit 17c5337f7b
2 changed files with 33 additions and 2 deletions
+27 -2
View File
@@ -611,6 +611,31 @@ finalize:
return 0;
}
static bool netdev_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) {
assert(netdev);
assert(hw_addr);
if (hw_addr->length <= 0)
return false;
if (!NETDEV_VTABLE(netdev)->can_set_mac)
return true;
return NETDEV_VTABLE(netdev)->can_set_mac(netdev, hw_addr);
}
static bool netdev_can_set_mtu(NetDev *netdev, uint32_t mtu) {
assert(netdev);
if (mtu <= 0)
return false;
if (!NETDEV_VTABLE(netdev)->can_set_mtu)
return true;
return NETDEV_VTABLE(netdev)->can_set_mtu(netdev, mtu);
}
static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message *m) {
int r;
@@ -623,14 +648,14 @@ static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message
if (r < 0)
return r;
if (hw_addr.length > 0) {
if (netdev_can_set_mac(netdev, &hw_addr)) {
log_netdev_debug(netdev, "Using MAC address: %s", HW_ADDR_TO_STR(&hw_addr));
r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &hw_addr);
if (r < 0)
return r;
}
if (netdev->mtu != 0) {
if (netdev_can_set_mtu(netdev, netdev->mtu)) {
r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
if (r < 0)
return r;
+6
View File
@@ -179,6 +179,12 @@ typedef struct NetDevVTable {
/* get ifindex of the netdev. */
int (*get_ifindex)(NetDev *netdev, const char *name);
/* provides if MAC address can be set. If this is not set, assumed to be yes. */
bool (*can_set_mac)(NetDev *netdev, const struct hw_addr_data *hw_addr);
/* provides if MTU can be set. If this is not set, assumed to be yes. */
bool (*can_set_mtu)(NetDev *netdev, uint32_t mtu);
/* expected iftype, e.g. ARPHRD_ETHER. */
uint16_t iftype;