]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/netdev: introduce netdev_can_set_mac/mtu() helper functions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Oct 2024 17:01:59 +0000 (02:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 30 Oct 2024 16:06:25 +0000 (01:06 +0900)
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.

src/network/netdev/netdev.c
src/network/netdev/netdev.h

index f7598dc7eac151beda03e8a884b2b6e9e5dc4a41..4d552505694767ac89fa028ca2274f283648a929 100644 (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;
index 10b9d0dd77a0d34ec3a2b3fcf70ea5c207a5c54b..9d8c4c7afdc77c266e5eb5b95a5bb887cbc457df 100644 (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;