]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/net/link-config.c
systemd-link: add support to configure the device port (#6153)
[thirdparty/systemd.git] / src / udev / net / link-config.c
index e9577930e326636e0fc5d01f837144aa56aa2d08..e65a8794dcc1a89ee174c9c643a2558bd97a0d30 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
  This file is part of systemd.
 
 ***/
 
 #include <netinet/ether.h>
-#include <linux/netdevice.h>
 
 #include "sd-netlink.h"
 
+#include "alloc-util.h"
 #include "conf-files.h"
 #include "conf-parser.h"
 #include "ethtool-util.h"
 #include "missing.h"
 #include "netlink-util.h"
 #include "network-internal.h"
+#include "parse-util.h"
 #include "path-util.h"
+#include "proc-cmdline.h"
 #include "random-util.h"
+#include "stat-util.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
 #include "util.h"
@@ -165,6 +167,10 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         link->mac_policy = _MACPOLICY_INVALID;
         link->wol = _WOL_INVALID;
         link->duplex = _DUP_INVALID;
+        link->port = _NET_DEV_PORT_INVALID;
+        link->autonegotiation = -1;
+
+        memset(&link->features, -1, sizeof(link->features));
 
         r = config_parse(NULL, filename, file,
                          "Match\0Link\0Ethernet\0",
@@ -187,28 +193,15 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
 }
 
 static bool enable_name_policy(void) {
-        _cleanup_free_ char *line = NULL;
-        const char *word, *state;
-        int r;
-        size_t l;
-
-        r = proc_cmdline(&line);
-        if (r < 0) {
-                log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
-                return true;
-        }
+        bool b;
 
-        FOREACH_WORD_QUOTED(word, l, line, state)
-                if (strneq(word, "net.ifnames=0", l))
-                        return false;
-
-        return true;
+        return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b;
 }
 
 int link_config_load(link_config_ctx *ctx) {
-        int r;
         _cleanup_strv_free_ char **files;
         char **f;
+        int r;
 
         link_configs_free(ctx);
 
@@ -349,14 +342,14 @@ static int get_mac(struct udev_device *device, bool want_random,
         if (want_random)
                 random_bytes(mac->ether_addr_octet, ETH_ALEN);
         else {
-                uint8_t result[8];
+                uint64_t result;
 
-                r = net_get_unique_predictable_data(device, result);
+                r = net_get_unique_predictable_data(device, &result);
                 if (r < 0)
                         return r;
 
                 assert_cc(ETH_ALEN <= sizeof(result));
-                memcpy(mac->ether_addr_octet, result, ETH_ALEN);
+                memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
         }
 
         /* see eth_random_addr in the kernel */
@@ -368,11 +361,12 @@ static int get_mac(struct udev_device *device, bool want_random,
 
 int link_config_apply(link_config_ctx *ctx, link_config *config,
                       struct udev_device *device, const char **name) {
-        const char *old_name;
-        const char *new_name = NULL;
+        bool respect_predictable = false;
         struct ether_addr generated_mac;
         struct ether_addr *mac = NULL;
-        bool respect_predictable = false;
+        const char *new_name = NULL;
+        const char *old_name;
+        unsigned speed;
         int r, ifindex;
 
         assert(ctx);
@@ -384,17 +378,29 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
         if (!old_name)
                 return -EINVAL;
 
-        r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
-        if (r < 0)
-                log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
-                                  old_name, config->speed / 1024,
-                                  duplex_to_string(config->duplex));
+
+        speed = DIV_ROUND_UP(config->speed, 1000000);
+
+        r = ethtool_set_glinksettings(&ctx->ethtool_fd, old_name, config);
+        if (r < 0) {
+
+                if (r == -EOPNOTSUPP)
+                        r = ethtool_set_speed(&ctx->ethtool_fd, old_name, speed, config->duplex);
+
+                if (r < 0)
+                        log_warning_errno(r, "Could not set speed, duplex or port (%s) of %s to %u Mbps (%s): %m",
+                                          port_to_string(config->port), old_name, speed, duplex_to_string(config->duplex));
+        }
 
         r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
         if (r < 0)
                 log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
                                   old_name, wol_to_string(config->wol));
 
+        r = ethtool_set_features(&ctx->ethtool_fd, old_name, config->features);
+        if (r < 0)
+                log_warning_errno(r, "Could not set offload features of %s: %m", old_name);
+
         ifindex = udev_device_get_ifindex(device);
         if (ifindex <= 0) {
                 log_warning("Could not find ifindex");