]> 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 63e54db56eba3ba77c4feb36db38cce00ac0f7eb..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 "missing.h"
-#include "link-config.h"
+#include "alloc-util.h"
+#include "conf-files.h"
+#include "conf-parser.h"
 #include "ethtool-util.h"
-
+#include "fd-util.h"
 #include "libudev-private.h"
-#include "sd-netlink.h"
-#include "util.h"
+#include "link-config.h"
 #include "log.h"
-#include "strv.h"
-#include "path-util.h"
-#include "conf-parser.h"
-#include "conf-files.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"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -164,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",
@@ -186,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;
-        }
-
-        FOREACH_WORD_QUOTED(word, l, line, state)
-                if (strneq(word, "net.ifnames=0", l))
-                        return false;
+        bool b;
 
-        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);
 
@@ -348,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 */
@@ -367,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);
@@ -383,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");
@@ -460,6 +467,7 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
                                 mac = &generated_mac;
                         }
                         break;
+                case MACPOLICY_NONE:
                 default:
                         mac = config->mac;
         }
@@ -492,7 +500,8 @@ int link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret
 
 static const char* const mac_policy_table[_MACPOLICY_MAX] = {
         [MACPOLICY_PERSISTENT] = "persistent",
-        [MACPOLICY_RANDOM] = "random"
+        [MACPOLICY_RANDOM] = "random",
+        [MACPOLICY_NONE] = "none"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);