]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: can: introduce a config parser function for bitrates
authorRichard Petri <git@rpls.de>
Mon, 30 Mar 2020 21:05:56 +0000 (23:05 +0200)
committerRichard Petri <git@rpls.de>
Wed, 1 Apr 2020 18:07:20 +0000 (20:07 +0200)
For now, this function is nearly equivalent to the si_uint64 parser, except for
an additional range check as Linux only takes 32-bit values as bitrates. In
future, this may also be used to introduce fancier bitrate config formats.

man/systemd.network.xml
src/network/networkd-can.c
src/network/networkd-can.h
src/network/networkd-network-gperf.gperf
src/network/networkd-network.h

index bb6c35f9babf49e4a7a9b3f6f8df59cc8e55e08f..be0756513ff2ca3522dee7ca2a0d743dd03d78d9 100644 (file)
           <term><varname>BitRate=</varname></term>
           <listitem>
             <para>The bitrate of CAN device in bits per second. The usual SI prefixes (K, M) with the base of 1000 can
-            be used here.</para>
+            be used here. Takes a number in the range 1..4294967295.</para>
           </listitem>
         </varlistentry>
         <varlistentry>
index 18533843e30c9226078dc766432bf3ecf6f76faa..4118fcf859b17bd4033d1f29c3cc153b2f000ea4 100644 (file)
@@ -7,10 +7,51 @@
 #include "networkd-can.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
+#include "parse-util.h"
 #include "string-util.h"
 
 #define CAN_TERMINATION_OHM_VALUE 120
 
+int config_parse_can_bitrate(
+                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 *br = data;
+        uint64_t sz;
+        int r;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = parse_size(rvalue, 1000, &sz);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, r,
+                           "Failed to parse can bitrate '%s', ignoring: %m", rvalue);
+                return 0;
+        }
+
+        /* Linux uses __u32 for bitrates, so the value should not exceed that. */
+        if (sz <= 0 || sz > UINT32_MAX) {
+                log_syntax(unit, LOG_ERR, filename, line, 0,
+                           "Bit rate out of permitted range 1...4294967295");
+                return 0;
+        }
+
+        *br = (uint32_t) sz;
+
+        return 0;
+}
+
 static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
@@ -103,11 +144,6 @@ static int link_set_can(Link *link) {
                         .sample_point = link->network->can_sample_point,
                 };
 
-                if (link->network->can_bitrate > UINT32_MAX) {
-                        log_link_error(link, "bitrate (%" PRIu64 ") too big.", link->network->can_bitrate);
-                        return -ERANGE;
-                }
-
                 log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate);
                 if (link->network->can_sample_point > 0)
                         log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
index c744bdfea72c9a8c3108df4535be357180f31b72..30e99b189d3def7491bf873991c6b0ce1d076de9 100644 (file)
@@ -1,6 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 #pragma once
 
+#include "conf-parser.h"
+
 typedef struct Link Link;
 
 int link_configure_can(Link *link);
+
+CONFIG_PARSER_PROTOTYPE(config_parse_can_bitrate);
index 18ba23bfc8bcb6af4497bfe8447c94bd8295396f..ee50da098d67b0429a0940e090e90d953bb2f6be 100644 (file)
@@ -6,6 +6,7 @@ _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
 #include "conf-parser.h"
 #include "netem.h"
 #include "network-internal.h"
+#include "networkd-can.h"
 #include "networkd-conf.h"
 #include "networkd-dhcp-common.h"
 #include "networkd-dhcp-server.h"
@@ -257,7 +258,7 @@ IPv6Prefix.PreferredLifetimeSec,             config_parse_prefix_lifetime,
 IPv6Prefix.Assign,                           config_parse_prefix_assign,                               0,                             0
 IPv6RoutePrefix.Route,                       config_parse_route_prefix,                                0,                             0
 IPv6RoutePrefix.LifetimeSec,                 config_parse_route_prefix_lifetime,                       0,                             0
-CAN.BitRate,                                 config_parse_si_uint64,                                   0,                             offsetof(Network, can_bitrate)
+CAN.BitRate,                                 config_parse_can_bitrate,                                 0,                             offsetof(Network, can_bitrate)
 CAN.SamplePoint,                             config_parse_permille,                                    0,                             offsetof(Network, can_sample_point)
 CAN.RestartSec,                              config_parse_sec,                                         0,                             offsetof(Network, can_restart_us)
 CAN.TripleSampling,                          config_parse_tristate,                                    0,                             offsetof(Network, can_triple_sampling)
index 66ee01d7f3fbaac34461fb0ce5d3628238807956..7acb4a5461dfb4c76f36422cf3393006a3aeeb35 100644 (file)
@@ -205,7 +205,7 @@ struct Network {
         uint32_t br_untagged_bitmap[BRIDGE_VLAN_BITMAP_LEN];
 
         /* CAN support */
-        uint64_t can_bitrate;
+        uint32_t can_bitrate;
         unsigned can_sample_point;
         usec_t can_restart_us;
         int can_triple_sampling;