network: can: make Termination= optionally take a raw resistor value

Note that this slightly breaks backward compatibility when
Termination=1. Previously, this is handled as boolean true, then 120 ohm
was used. But now with this commit, it is handled as 1 ohm.
This commit is contained in:
Yu Watanabe 2021-08-16 02:16:53 +09:00
parent 6dd84c9e86
commit 239f91f71c
5 changed files with 58 additions and 9 deletions

View file

@ -2996,8 +2996,10 @@ IPv6Token=prefixstable:2002:da8:1::</programlisting></para>
<varlistentry>
<term><varname>Termination=</varname></term>
<listitem>
<para>Takes a boolean. When <literal>yes</literal>, the termination resistor will be selected for
the bias network. When unset, the kernel's default will be used.</para>
<para>Takes a boolean or a termination resistor value in ohm in the range 065535. When
<literal>yes</literal>, the termination resistor is set to 120 ohm. When
<literal>no</literal> or <literal>0</literal> is set, the termination resistor is disabled.
When unset, the kernel's default will be used.</para>
</listitem>
</varlistentry>
<varlistentry>

View file

@ -10,7 +10,7 @@
#include "parse-util.h"
#include "string-util.h"
#define CAN_TERMINATION_OHM_VALUE 120
#define CAN_TERMINATION_DEFAULT_OHM_VALUE 120
int can_set_netlink_message(Link *link, sd_netlink_message *m) {
int r;
@ -90,11 +90,10 @@ int can_set_netlink_message(Link *link, sd_netlink_message *m) {
return log_link_debug_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
}
if (link->network->can_termination >= 0) {
log_link_debug(link, "Setting can-termination to '%s'.", yes_no(link->network->can_termination));
if (link->network->can_termination_set) {
log_link_debug(link, "Setting can-termination to '%u'.", link->network->can_termination);
r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION,
link->network->can_termination ? CAN_TERMINATION_OHM_VALUE : 0);
r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION, link->network->can_termination);
if (r < 0)
return log_link_debug_errno(link, r, "Could not append IFLA_CAN_TERMINATION attribute: %m");
}
@ -227,3 +226,49 @@ int config_parse_can_control_mode(
SET_FLAG(network->can_control_mode_flags, mask, r);
return 0;
}
int config_parse_can_termination(
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) {
Network *network = userdata;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if (isempty(rvalue)) {
network->can_termination_set = false;
return 0;
}
/* Note that 0 termination ohm value means no termination resistor, and there is no conflict
* between parse_boolean() and safe_atou16() when Termination=0. However, Termination=1 must be
* treated as 1 ohm, instead of true (and then the default ohm value). So, we need to parse the
* string with safe_atou16() at first. */
r = safe_atou16(rvalue, &network->can_termination);
if (r < 0) {
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse CAN termination value, ignoring: %s", rvalue);
return 0;
}
network->can_termination = r ? CAN_TERMINATION_DEFAULT_OHM_VALUE : 0;
}
network->can_termination_set = true;
return 0;
}

View file

@ -14,3 +14,4 @@ int can_set_netlink_message(Link *link, sd_netlink_message *m);
CONFIG_PARSER_PROTOTYPE(config_parse_can_bitrate);
CONFIG_PARSER_PROTOTYPE(config_parse_can_restart_usec);
CONFIG_PARSER_PROTOTYPE(config_parse_can_control_mode);
CONFIG_PARSER_PROTOTYPE(config_parse_can_termination);

View file

@ -358,7 +358,7 @@ CAN.FDMode, config_parse_can_control_mode,
CAN.PresumeACK, config_parse_can_control_mode, CAN_CTRLMODE_PRESUME_ACK, 0
CAN.FDNonISO, config_parse_can_control_mode, CAN_CTRLMODE_FD_NON_ISO, 0
CAN.ClassicDataLengthCode, config_parse_can_control_mode, CAN_CTRLMODE_CC_LEN8_DLC, 0
CAN.Termination, config_parse_tristate, 0, offsetof(Network, can_termination)
CAN.Termination, config_parse_can_termination, 0, 0
QDisc.Parent, config_parse_qdisc_parent, _QDISC_KIND_INVALID, 0
QDisc.Handle, config_parse_qdisc_handle, _QDISC_KIND_INVALID, 0
BFIFO.Parent, config_parse_qdisc_parent, QDISC_KIND_BFIFO, 0

View file

@ -270,7 +270,8 @@ struct Network {
usec_t can_restart_us;
uint32_t can_control_mode_mask;
uint32_t can_control_mode_flags;
int can_termination;
uint16_t can_termination;
bool can_termination_set;
/* sysctl settings */
AddressFamily ip_forward;