]> git.ipfire.org Git - network.git/commitdiff
networkd: Add a test bus property to set the MTU
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Feb 2023 22:10:56 +0000 (22:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Feb 2023 22:10:56 +0000 (22:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/zone-bus.c
src/networkd/zone.c
src/networkd/zone.h

index e531ec6710600c8c8cba7287d455068e57510b49..788308f8b3db4c59d421e418e9a113163c0a904e 100644 (file)
@@ -273,6 +273,17 @@ int nw_config_del(struct nw_config* config, const char* key) {
        return 0;
 }
 
+const char* nw_config_get(struct nw_config* config, const char* key) {
+       struct nw_config_entry* entry = nw_config_find(config, key);
+
+       // Return the value if found and set
+       if (entry && *entry->value)
+               return entry->value;
+
+       // Otherwise return NULL
+       return NULL;
+}
+
 int nw_config_set(struct nw_config* config, const char* key, const char* value) {
        struct nw_config_entry* entry = NULL;
 
@@ -294,23 +305,24 @@ int nw_config_set(struct nw_config* config, const char* key, const char* value)
        return nw_string_set(entry->value, value);
 }
 
-const char* nw_config_get(struct nw_config* config, const char* key) {
-       struct nw_config_entry* entry = nw_config_find(config, key);
-
-       // Return the value if found and set
-       if (entry && *entry->value)
-               return entry->value;
-
-       // Otherwise return NULL
-       return NULL;
-}
-
-unsigned int nw_config_get_unsigned_int(struct nw_config* config, const char* key) {
+int nw_config_get_int(struct nw_config* config, const char* key, const int __default) {
        const char* value = nw_config_get(config, key);
 
        // Return zero if not set
        if (!value)
-               return 0;
+               return __default;
 
        return strtoul(value, NULL, 10);
 }
+
+int nw_config_set_int(struct nw_config* config, const char* key, const int value) {
+       char __value[1024];
+       int r;
+
+       // Format the value as string
+       r = nw_string_format(__value, "%d\n", value);
+       if (r)
+               return r;
+
+       return nw_config_set(config, key, __value);
+}
index 5b9910bdf2afecb5213858cee360e562090eec85..b8d85550a97c5ddf0c29182d22091be2c40f0020 100644 (file)
@@ -42,9 +42,10 @@ int nw_config_write(struct nw_config* config);
 
 int nw_config_del(struct nw_config* config, const char* key);
 
+const char* nw_config_get(struct nw_config* config, const char* key);
 int nw_config_set(struct nw_config* config, const char* key, const char* value);
 
-const char* nw_config_get(struct nw_config* config, const char* key);
-unsigned int nw_config_get_unsigned_int(struct nw_config* config, const char* key);
+int nw_config_get_int(struct nw_config* config, const char* key, const int __default);
+int nw_config_set_int(struct nw_config* config, const char* key, const int value);
 
 #endif /* NETWORKD_CONFIG_H */
index aafbfaf75e37bdea689eb71c00f462e7ffdbabcd..e9c2ecc2c06a28c344437bf4484a9fae7dd6a508 100644 (file)
@@ -18,6 +18,8 @@
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
+
 #include "bus.h"
 #include "daemon.h"
 #include "logging.h"
@@ -74,9 +76,41 @@ static int nw_zone_object_find(sd_bus* bus, const char* path, const char* interf
        return 1;
 }
 
+/*
+       MTU
+*/
+static int nw_zone_bus_get_mtu(sd_bus* bus, const char *path, const char *interface,
+               const char* property, sd_bus_message* reply, void* data, sd_bus_error *error) {
+       struct nw_zone* zone = (struct nw_zone*)data;
+
+       return sd_bus_message_append(reply, "u", nw_zone_mtu(zone));
+}
+
+static int nw_zone_bus_set_mtu(sd_bus* bus, const char* path, const char* interface,
+               const char* property, sd_bus_message* value, void* data, sd_bus_error* error) {
+       unsigned int mtu = 0;
+       int r;
+
+       struct nw_zone* zone = (struct nw_zone*)data;
+
+       // Parse the value
+       r = sd_bus_message_read(value, "u", &mtu);
+       if (r < 0)
+               return r;
+
+       if (!nw_zone_set_mtu(zone, mtu))
+               return -errno;
+
+       return 0;
+}
+
 static const sd_bus_vtable zone_vtable[] = {
        SD_BUS_VTABLE_START(0),
 
+       // MTU
+       SD_BUS_WRITABLE_PROPERTY("MTU", "u", nw_zone_bus_get_mtu, nw_zone_bus_set_mtu,
+               0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+
        SD_BUS_VTABLE_END
 };
 
index f78334e6e19441e75339a5f84123d89d445e645e..c64706fcc1657bc9a14ef565bbfc9bb0213d2c8d 100644 (file)
@@ -24,6 +24,7 @@
 #include <systemd/sd-bus.h>
 
 #include "config.h"
+#include "logging.h"
 #include "string.h"
 #include "zone.h"
 
@@ -144,3 +145,16 @@ char* nw_zone_bus_path(struct nw_zone* zone) {
 
        return p;
 }
+
+/*
+       MTU
+*/
+unsigned int nw_zone_mtu(struct nw_zone* zone) {
+       return nw_config_get_int(zone->config, "MTU", NETWORK_ZONE_DEFAULT_MTU);
+}
+
+int nw_zone_set_mtu(struct nw_zone* zone, unsigned int mtu) {
+       DEBUG("Change MTU of %s to %u\n", zone->name, mtu);
+
+       return nw_config_set_int(zone->config, "MTU", mtu);
+}
index 081a720a0930865513cd7cb047ff996d793024fe..4a00412503ac096736c2b2046918b837d0828a06 100644 (file)
@@ -22,6 +22,7 @@
 #define NETWORKD_ZONE_H
 
 #define NETWORK_ZONE_NAME_MAX_LENGTH           16
+#define NETWORK_ZONE_DEFAULT_MTU                       1500
 
 struct nw_zone;
 
@@ -34,4 +35,10 @@ const char* nw_zone_name(struct nw_zone* zone);
 
 char* nw_zone_bus_path(struct nw_zone* zone);
 
+/*
+       MTU
+*/
+unsigned int nw_zone_mtu(struct nw_zone* zone);
+int nw_zone_set_mtu(struct nw_zone* zone, unsigned int mtu);
+
 #endif /* NETWORKD_ZONE_H */