]> git.ipfire.org Git - network.git/commitdiff
networkd: Read link MTU
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 10:13:37 +0000 (10:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 10:13:37 +0000 (10:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/link.c

index 40f809f532344013d5f13249819338ca42468465..62be89371a6f9e5ba9f55648d68ec4358777a197 100644 (file)
@@ -40,6 +40,11 @@ struct nw_link {
 
        // Interface Name
        char ifname[IF_NAMESIZE];
+
+       // MTU
+       uint32_t mtu;
+       uint32_t min_mtu;
+       uint32_t max_mtu;
 };
 
 int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex) {
@@ -115,6 +120,54 @@ static int nw_link_update_ifname(struct nw_link* link, sd_netlink_message* messa
        return 0;
 }
 
+static int nw_link_update_mtu(struct nw_link* link, sd_netlink_message* message) {
+       uint32_t mtu = 0;
+       uint32_t min_mtu = 0;
+       uint32_t max_mtu = 0;
+       int r;
+
+       // Read the MTU
+       r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
+       if (r < 0) {
+               ERROR("Could not read MTU for link %d: %m\n", link->ifindex);
+               return r;
+       }
+
+       // Read the minimum MTU
+       r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
+       if (r < 0) {
+               ERROR("Could not read the minimum MTU for link %d: %m\n", link->ifindex);
+               return r;
+       }
+
+       // Read the maximum MTU
+       r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
+       if (r < 0) {
+               ERROR("Could not read the maximum MTU for link %d: %m\n", link->ifindex);
+               return r;
+       }
+
+       // Set the maximum MTU to infinity
+       if (!max_mtu)
+               max_mtu = UINT32_MAX;
+
+       // Store min/max MTU
+       link->min_mtu = min_mtu;
+       link->max_mtu = max_mtu;
+
+       // End here, if the MTU has not been changed
+       if (link->mtu == mtu)
+               return 0;
+
+       DEBUG("Link %d: MTU has changed to %" PRIu32 " (min: %" PRIu32 ", max: %" PRIu32 ")\n",
+               link->ifindex, link->mtu, link->min_mtu, link->max_mtu);
+
+       // Store MTU
+       link->mtu = mtu;
+
+       return 0;
+}
+
 /*
        This function is called whenever anything changes, so that we can
        update our internal link object.
@@ -127,6 +180,11 @@ static int nw_link_update(struct nw_link* link, sd_netlink_message* message) {
        if (r)
                return r;
 
+       // Update the MTU
+       r = nw_link_update_mtu(link, message);
+       if (r)
+               return r;
+
        return 0;
 }