From: Michael Tremer Date: Sat, 4 Feb 2023 22:10:56 +0000 (+0000) Subject: networkd: Add a test bus property to set the MTU X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9368a163388c9f8665dbdedaccae70ee168df714;p=network.git networkd: Add a test bus property to set the MTU Signed-off-by: Michael Tremer --- diff --git a/src/networkd/config.c b/src/networkd/config.c index e531ec67..788308f8 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -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); +} diff --git a/src/networkd/config.h b/src/networkd/config.h index 5b9910bd..b8d85550 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -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 */ diff --git a/src/networkd/zone-bus.c b/src/networkd/zone-bus.c index aafbfaf7..e9c2ecc2 100644 --- a/src/networkd/zone-bus.c +++ b/src/networkd/zone-bus.c @@ -18,6 +18,8 @@ # # #############################################################################*/ +#include + #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 }; diff --git a/src/networkd/zone.c b/src/networkd/zone.c index f78334e6..c64706fc 100644 --- a/src/networkd/zone.c +++ b/src/networkd/zone.c @@ -24,6 +24,7 @@ #include #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); +} diff --git a/src/networkd/zone.h b/src/networkd/zone.h index 081a720a..4a004125 100644 --- a/src/networkd/zone.h +++ b/src/networkd/zone.h @@ -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 */