]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/net: allow to set number of SR-IOV virtual functions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 14 Jan 2022 18:35:27 +0000 (03:35 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Jan 2022 06:00:53 +0000 (15:00 +0900)
This adds SR-IOVVirtualFunctions= setting in [Link] section.

man/systemd.link.xml
src/network/networkd-network.c
src/shared/netif-sriov.c
src/shared/netif-sriov.h
src/udev/net/link-config-gperf.gperf
src/udev/net/link-config.c
src/udev/net/link-config.h
test/fuzz/fuzz-link-parser/directives.link

index 60d2c11e3cd7043d2919e93d6888c813699e204d..700defeda6b86045e9f4daf7a302d5c1a0c41f65 100644 (file)
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><varname>SR-IOVVirtualFunctions=</varname></term>
+        <listitem>
+          <para>Specifies the number of SR-IOV virtual functions. Takes an integer in the range
+          0…2147483647. Defaults to unset, and automatically determined from the values specified in
+          the <varname>VirtualFunction=</varname> settings in the [SR-IOV] sections.</para>
+        </listitem>
+      </varlistentry>
+
     </variablelist>
   </refsect1>
 
index df4b6f23ad5a8f89685516e8017d227e76093dd9..3142be471f8b8f29608c8cfae98ff8bf50c00500 100644 (file)
@@ -321,7 +321,7 @@ int network_verify(Network *network) {
         network_drop_invalid_route_prefixes(network);
         network_drop_invalid_routing_policy_rules(network);
         network_drop_invalid_traffic_control(network);
-        r = sr_iov_drop_invalid_sections(network->sr_iov_by_section);
+        r = sr_iov_drop_invalid_sections(UINT32_MAX, network->sr_iov_by_section);
         if (r < 0)
                 return r;
         network_drop_invalid_static_leases(network);
index fc40ccbbb6c7d0974d9e0a40576f0c3b029a00e2..720aa65f4c0598dc9662a546379ded9a45639524 100644 (file)
@@ -1,10 +1,12 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "alloc-util.h"
+#include "device-util.h"
 #include "netlink-util.h"
 #include "netif-sriov.h"
 #include "parse-util.h"
 #include "set.h"
+#include "stdio-util.h"
 #include "string-util.h"
 
 static int sr_iov_new(SRIOV **ret) {
@@ -179,7 +181,100 @@ int sr_iov_set_netlink_message(SRIOV *sr_iov, sd_netlink_message *req) {
         return 0;
 }
 
-static int sr_iov_section_verify(SRIOV *sr_iov) {
+int sr_iov_get_num_vfs(sd_device *device, uint32_t *ret) {
+        const char *str;
+        uint32_t n;
+        int r;
+
+        assert(device);
+        assert(ret);
+
+        r = sd_device_get_sysattr_value(device, "device/sriov_numvfs", &str);
+        if (r < 0)
+                return r;
+
+        r = safe_atou32(str, &n);
+        if (r < 0)
+                return r;
+
+        *ret = n;
+        return 0;
+}
+
+int sr_iov_set_num_vfs(sd_device *device, uint32_t num_vfs, OrderedHashmap *sr_iov_by_section) {
+        char val[DECIMAL_STR_MAX(uint32_t)];
+        const char *str;
+        int r;
+
+        assert(device);
+
+        if (num_vfs == UINT32_MAX) {
+                uint32_t current_num_vfs;
+                SRIOV *sr_iov;
+
+                /* If the number of virtual functions is not specified, then use the maximum number of VF + 1. */
+
+                num_vfs = 0;
+                ORDERED_HASHMAP_FOREACH(sr_iov, sr_iov_by_section)
+                        num_vfs = MAX(num_vfs, sr_iov->vf + 1);
+
+                if (num_vfs == 0) /* No VF is configured. */
+                        return 0;
+
+                r = sr_iov_get_num_vfs(device, &current_num_vfs);
+                if (r < 0)
+                        return log_device_debug_errno(device, r, "Failed to get the current number of SR-IOV virtual functions: %m");
+
+                /* Enough VFs already exist. */
+                if (num_vfs <= current_num_vfs)
+                        return 0;
+
+        } else if (num_vfs == 0) {
+                r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", "0");
+                if (r < 0)
+                        log_device_debug_errno(device, r, "Failed to write device/sriov_numvfs sysfs attribute, ignoring: %m");
+
+                /* Gracefully handle the error in disabling VFs when the interface does not support SR-IOV. */
+                return r == -ENOENT ? 0 : r;
+        }
+
+        /* So, the interface does not have enough VFs. Before increasing the number of VFs, check the
+         * maximum allowed number of VFs from the sriov_totalvfs sysattr. Note that the sysattr
+         * currently exists only for PCI drivers. Hence, ignore -ENOENT.
+         * TODO: netdevsim provides the information in debugfs. */
+        r = sd_device_get_sysattr_value(device, "device/sriov_totalvfs", &str);
+        if (r >= 0) {
+                uint32_t max_num_vfs;
+
+                r = safe_atou32(str, &max_num_vfs);
+                if (r < 0)
+                        return log_device_debug_errno(device, r, "Failed to parse device/sriov_totalvfs sysfs attribute '%s': %m", str);
+
+                if (num_vfs > max_num_vfs)
+                        return log_device_debug_errno(device, SYNTHETIC_ERRNO(ERANGE),
+                                                      "Specified number of virtual functions is out of range. "
+                                                      "The maximum allowed value is %"PRIu32".",
+                                                      max_num_vfs);
+
+        } else if (r != -ENOENT) /* Currently, only PCI driver has the attribute. */
+                return log_device_debug_errno(device, r, "Failed to read device/sriov_totalvfs sysfs attribute: %m");
+
+        xsprintf(val, "%"PRIu32, num_vfs);
+        r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", val);
+        if (r == -EBUSY) {
+                /* Some devices e.g. netdevsim refuse to set sriov_numvfs if it has non-zero value. */
+                r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", "0");
+                if (r >= 0)
+                        r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", val);
+        }
+        if (r < 0)
+                return log_device_debug_errno(device, r, "Failed to write device/sriov_numvfs sysfs attribute: %m");
+
+        log_device_debug(device, "device/sriov_numvfs sysfs attribute set to '%s'.", val);
+        return 0;
+}
+
+static int sr_iov_section_verify(uint32_t num_vfs, SRIOV *sr_iov) {
         assert(sr_iov);
 
         if (section_is_invalid(sr_iov->section))
@@ -191,10 +286,16 @@ static int sr_iov_section_verify(SRIOV *sr_iov) {
                                          "Ignoring [SR-IOV] section from line %u.",
                                          sr_iov->section->filename, sr_iov->section->line);
 
+        if (sr_iov->vf >= num_vfs)
+                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
+                                         "%s: VirtualFunction= must be smaller than the value specified in SR-IOVVirtualFunctions=. "
+                                         "Ignoring [SR-IOV] section from line %u.",
+                                         sr_iov->section->filename, sr_iov->section->line);
+
         return 0;
 }
 
-int sr_iov_drop_invalid_sections(OrderedHashmap *sr_iov_by_section) {
+int sr_iov_drop_invalid_sections(uint32_t num_vfs, OrderedHashmap *sr_iov_by_section) {
         _cleanup_hashmap_free_ Hashmap *hashmap = NULL;
         SRIOV *sr_iov;
         int r;
@@ -202,7 +303,7 @@ int sr_iov_drop_invalid_sections(OrderedHashmap *sr_iov_by_section) {
         ORDERED_HASHMAP_FOREACH(sr_iov, sr_iov_by_section) {
                 SRIOV *dup;
 
-                if (sr_iov_section_verify(sr_iov) < 0) {
+                if (sr_iov_section_verify(num_vfs, sr_iov) < 0) {
                         sr_iov_free(sr_iov);
                         continue;
                 }
@@ -485,3 +586,46 @@ int config_parse_sr_iov_mac(
         TAKE_PTR(sr_iov);
         return 0;
 }
+
+int config_parse_sr_iov_num_vfs(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        uint32_t n, *num_vfs = data;
+        int r;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        if (isempty(rvalue)) {
+                *num_vfs = UINT32_MAX;
+                return 0;
+        }
+
+        r = safe_atou32(rvalue, &n);
+        if (r < 0) {
+                log_syntax(unit, LOG_WARNING, filename, line, r,
+                           "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
+                return 0;
+        }
+
+        if (n > INT_MAX) {
+                log_syntax(unit, LOG_WARNING, filename, line, 0,
+                           "The number of SR-IOV virtual functions is too large. It must be equal to "
+                           "or smaller than 2147483647. Ignoring assignment: %"PRIu32, n);
+                return 0;
+        }
+
+        *num_vfs = n;
+        return 0;
+}
index 871496287f9eae7044e7a68b0e6397704513158f..4c85f101c7b3de0e48ae73cfd1f4c542ad20d7cc 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <linux/if_link.h>
 
+#include "sd-device.h"
+
 #include "conf-parser.h"
 #include "ether-addr-util.h"
 #include "hashmap.h"
@@ -32,7 +34,9 @@ typedef struct SRIOV {
 
 SRIOV *sr_iov_free(SRIOV *sr_iov);
 int sr_iov_set_netlink_message(SRIOV *sr_iov, sd_netlink_message *req);
-int sr_iov_drop_invalid_sections(OrderedHashmap *sr_iov_by_section);
+int sr_iov_get_num_vfs(sd_device *device, uint32_t *ret);
+int sr_iov_set_num_vfs(sd_device *device, uint32_t num_vfs, OrderedHashmap *sr_iov_by_section);
+int sr_iov_drop_invalid_sections(uint32_t num_vfs, OrderedHashmap *sr_iov_by_section);
 
 DEFINE_SECTION_CLEANUP_FUNCTIONS(SRIOV, sr_iov_free);
 
@@ -41,3 +45,4 @@ CONFIG_PARSER_PROTOTYPE(config_parse_sr_iov_boolean);
 CONFIG_PARSER_PROTOTYPE(config_parse_sr_iov_link_state);
 CONFIG_PARSER_PROTOTYPE(config_parse_sr_iov_vlan_proto);
 CONFIG_PARSER_PROTOTYPE(config_parse_sr_iov_mac);
+CONFIG_PARSER_PROTOTYPE(config_parse_sr_iov_num_vfs);
index 3732fd53ef2feeb0359611697a42728c8bc4f0a2..7dde4ed59f5287b8a4ffc34e8b6aaf3b8c8d58fb 100644 (file)
@@ -102,6 +102,7 @@ Link.RxMaxCoalescedHighFrames,             config_parse_coalesce_u32,
 Link.TxCoalesceHighSec,                    config_parse_coalesce_sec,             0,                             offsetof(LinkConfig, coalesce.tx_coalesce_usecs_high)
 Link.TxMaxCoalescedHighFrames,             config_parse_coalesce_u32,             0,                             offsetof(LinkConfig, coalesce.tx_max_coalesced_frames_high)
 Link.CoalescePacketRateSampleIntervalSec,  config_parse_coalesce_sec,             0,                             offsetof(LinkConfig, coalesce.rate_sample_interval)
+Link.SR-IOVVirtualFunctions,               config_parse_sr_iov_num_vfs,           0,                             offsetof(LinkConfig, sr_iov_num_vfs)
 SR-IOV.VirtualFunction,                    config_parse_sr_iov_uint32,            0,                             offsetof(LinkConfig, sr_iov_by_section)
 SR-IOV.VLANId,                             config_parse_sr_iov_uint32,            0,                             offsetof(LinkConfig, sr_iov_by_section)
 SR-IOV.QualityOfService,                   config_parse_sr_iov_uint32,            0,                             offsetof(LinkConfig, sr_iov_by_section)
index 3e8b6aaaf29b7fcb8bb423c861d67963b0eaa808..5bd029ecc50b97e17db04e0144d4714261f9df4c 100644 (file)
@@ -250,6 +250,7 @@ int link_load_one(LinkConfigContext *ctx, const char *filename) {
                 .txqueuelen = UINT32_MAX,
                 .coalesce.use_adaptive_rx_coalesce = -1,
                 .coalesce.use_adaptive_tx_coalesce = -1,
+                .sr_iov_num_vfs = UINT32_MAX,
         };
 
         for (i = 0; i < ELEMENTSOF(config->features); i++)
@@ -290,7 +291,7 @@ int link_load_one(LinkConfigContext *ctx, const char *filename) {
         if (r < 0)
                 return r;
 
-        r = sr_iov_drop_invalid_sections(config->sr_iov_by_section);
+        r = sr_iov_drop_invalid_sections(config->sr_iov_num_vfs, config->sr_iov_by_section);
         if (r < 0)
                 return r;
 
@@ -874,6 +875,11 @@ static int link_apply_sr_iov_config(Link *link, sd_netlink **rtnl) {
 
         assert(link);
         assert(link->config);
+        assert(link->device);
+
+        r = sr_iov_set_num_vfs(link->device, link->config->sr_iov_num_vfs, link->config->sr_iov_by_section);
+        if (r < 0)
+                log_link_warning_errno(link, r, "Failed to set the number of SR-IOV virtual functions, ignoring: %m");
 
         ORDERED_HASHMAP_FOREACH(sr_iov, link->config->sr_iov_by_section) {
                 r = sr_iov_configure(link, rtnl, sr_iov);
index e71738cfbf0e72a4ec5b2e0101b05f794069907c..0d1d117f2e1a2ab5b590115de21278285d54d6cb 100644 (file)
@@ -77,6 +77,7 @@ struct LinkConfig {
         int autoneg_flow_control;
         netdev_coalesce_param coalesce;
 
+        uint32_t sr_iov_num_vfs;
         OrderedHashmap *sr_iov_by_section;
 
         LIST_FIELDS(LinkConfig, configs);
index 87b435c745ab66eabf74fadcfdce38fe0057c72a..a5773826c583f320b59eebcece7c166e372d2ca6 100644 (file)
@@ -80,6 +80,7 @@ RxMaxCoalescedHighFrames=
 TxCoalesceHighSec=
 TxMaxCoalescedHighFrames=
 CoalescePacketRateSampleIntervalSec=
+SR-IOVVirtualFunctions=
 [SR-IOV]
 VirtualFunction=
 MACSpoofCheck=