| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/can/netlink.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "networkd-can.h" |
| 8 |
#include "networkd-link.h" |
| 9 |
#include "networkd-network.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
|
| 13 |
#define CAN_TERMINATION_DEFAULT_OHM_VALUE 120 |
| 14 |
|
| 15 |
int can_set_netlink_message(Link *link, sd_netlink_message *m) { |
| 16 |
int r; |
| 17 |
|
| 18 |
assert(link); |
| 19 |
assert(link->network); |
| 20 |
assert(m); |
| 21 |
|
| 22 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 23 |
if (r < 0) |
| 24 |
return r; |
| 25 |
|
| 26 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind); |
| 27 |
if (r < 0) |
| 28 |
return r; |
| 29 |
|
| 30 |
if (link->network->can_bitrate > 0) { |
| 31 |
struct can_bittiming bt = { |
| 32 |
.bitrate = link->network->can_bitrate, |
| 33 |
.sample_point = link->network->can_sample_point, |
| 34 |
.sjw = link->network->can_sync_jump_width, |
| 35 |
}; |
| 36 |
|
| 37 |
log_link_debug(link, "Setting bitrate = %u bit/s", bt.bitrate); |
| 38 |
if (link->network->can_sample_point > 0) |
| 39 |
log_link_debug(link, "Setting sample point = %u.%u%%", bt.sample_point / 10, bt.sample_point % 10); |
| 40 |
else |
| 41 |
log_link_debug(link, "Using default sample point"); |
| 42 |
|
| 43 |
r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
} else if (link->network->can_time_quanta_ns > 0) { |
| 47 |
struct can_bittiming bt = { |
| 48 |
.tq = link->network->can_time_quanta_ns, |
| 49 |
.prop_seg = link->network->can_propagation_segment, |
| 50 |
.phase_seg1 = link->network->can_phase_buffer_segment_1, |
| 51 |
.phase_seg2 = link->network->can_phase_buffer_segment_2, |
| 52 |
.sjw = link->network->can_sync_jump_width, |
| 53 |
}; |
| 54 |
|
| 55 |
log_link_debug(link, "Setting time quanta = %"PRIu32" nsec", bt.tq); |
| 56 |
r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
} |
| 60 |
|
| 61 |
if (link->network->can_data_bitrate > 0) { |
| 62 |
struct can_bittiming bt = { |
| 63 |
.bitrate = link->network->can_data_bitrate, |
| 64 |
.sample_point = link->network->can_data_sample_point, |
| 65 |
.sjw = link->network->can_data_sync_jump_width, |
| 66 |
}; |
| 67 |
|
| 68 |
log_link_debug(link, "Setting data bitrate = %u bit/s", bt.bitrate); |
| 69 |
if (link->network->can_data_sample_point > 0) |
| 70 |
log_link_debug(link, "Setting data sample point = %u.%u%%", bt.sample_point / 10, bt.sample_point % 10); |
| 71 |
else |
| 72 |
log_link_debug(link, "Using default data sample point"); |
| 73 |
|
| 74 |
r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt)); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
} else if (link->network->can_data_time_quanta_ns > 0) { |
| 78 |
struct can_bittiming bt = { |
| 79 |
.tq = link->network->can_data_time_quanta_ns, |
| 80 |
.prop_seg = link->network->can_data_propagation_segment, |
| 81 |
.phase_seg1 = link->network->can_data_phase_buffer_segment_1, |
| 82 |
.phase_seg2 = link->network->can_data_phase_buffer_segment_2, |
| 83 |
.sjw = link->network->can_data_sync_jump_width, |
| 84 |
}; |
| 85 |
|
| 86 |
log_link_debug(link, "Setting data time quanta = %"PRIu32" nsec", bt.tq); |
| 87 |
r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt)); |
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
} |
| 91 |
|
| 92 |
uint64_t restart_ms; |
| 93 |
|
| 94 |
if (link->network->can_restart_us == USEC_INFINITY) |
| 95 |
restart_ms = 0; |
| 96 |
else |
| 97 |
restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC); |
| 98 |
|
| 99 |
log_link_debug(link, "Setting restart = %s", FORMAT_TIMESPAN(restart_ms * 1000, MSEC_PER_SEC)); |
| 100 |
r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms); |
| 101 |
if (r < 0) |
| 102 |
return r; |
| 103 |
|
| 104 |
if (link->network->can_control_mode_mask != 0) { |
| 105 |
struct can_ctrlmode cm = { |
| 106 |
.mask = link->network->can_control_mode_mask, |
| 107 |
.flags = link->network->can_control_mode_flags, |
| 108 |
}; |
| 109 |
|
| 110 |
r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm)); |
| 111 |
if (r < 0) |
| 112 |
return r; |
| 113 |
} |
| 114 |
|
| 115 |
if (link->network->can_termination_set) { |
| 116 |
log_link_debug(link, "Setting can-termination to '%u'.", link->network->can_termination); |
| 117 |
|
| 118 |
r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION, link->network->can_termination); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
} |
| 122 |
|
| 123 |
r = sd_netlink_message_close_container(m); |
| 124 |
if (r < 0) |
| 125 |
return r; |
| 126 |
|
| 127 |
r = sd_netlink_message_close_container(m); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
int config_parse_can_bitrate( |
| 135 |
const char *unit, |
| 136 |
const char *filename, |
| 137 |
unsigned line, |
| 138 |
const char *section, |
| 139 |
unsigned section_line, |
| 140 |
const char *lvalue, |
| 141 |
int ltype, |
| 142 |
const char *rvalue, |
| 143 |
void *data, |
| 144 |
void *userdata) { |
| 145 |
|
| 146 |
uint32_t *br = ASSERT_PTR(data); |
| 147 |
uint64_t sz; |
| 148 |
int r; |
| 149 |
|
| 150 |
assert(filename); |
| 151 |
assert(lvalue); |
| 152 |
assert(rvalue); |
| 153 |
|
| 154 |
r = parse_size(rvalue, 1000, &sz); |
| 155 |
if (r < 0) { |
| 156 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 157 |
"Failed to parse can bitrate '%s', ignoring: %m", rvalue); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
/* Linux uses __u32 for bitrates, so the value should not exceed that. */ |
| 162 |
if (sz <= 0 || sz > UINT32_MAX) { |
| 163 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 164 |
"Bit rate out of permitted range 1...4294967295"); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
*br = (uint32_t) sz; |
| 169 |
|
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
int config_parse_can_time_quanta( |
| 174 |
const char *unit, |
| 175 |
const char *filename, |
| 176 |
unsigned line, |
| 177 |
const char *section, |
| 178 |
unsigned section_line, |
| 179 |
const char *lvalue, |
| 180 |
int ltype, |
| 181 |
const char *rvalue, |
| 182 |
void *data, |
| 183 |
void *userdata) { |
| 184 |
|
| 185 |
nsec_t val, *tq = ASSERT_PTR(data); |
| 186 |
int r; |
| 187 |
|
| 188 |
assert(filename); |
| 189 |
assert(lvalue); |
| 190 |
assert(rvalue); |
| 191 |
|
| 192 |
r = parse_nsec(rvalue, &val); |
| 193 |
if (r < 0) { |
| 194 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 195 |
"Failed to parse can time quanta '%s', ignoring: %m", rvalue); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
/* Linux uses __u32 for bitrates, so the value should not exceed that. */ |
| 200 |
if (val <= 0 || val > UINT32_MAX) { |
| 201 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 202 |
"Time quanta out of permitted range 1...4294967295"); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
*tq = val; |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
int config_parse_can_restart_usec( |
| 211 |
const char *unit, |
| 212 |
const char *filename, |
| 213 |
unsigned line, |
| 214 |
const char *section, |
| 215 |
unsigned section_line, |
| 216 |
const char *lvalue, |
| 217 |
int ltype, |
| 218 |
const char *rvalue, |
| 219 |
void *data, |
| 220 |
void *userdata) { |
| 221 |
|
| 222 |
usec_t usec, *restart_usec = ASSERT_PTR(data); |
| 223 |
int r; |
| 224 |
|
| 225 |
assert(filename); |
| 226 |
assert(lvalue); |
| 227 |
assert(rvalue); |
| 228 |
|
| 229 |
r = parse_sec(rvalue, &usec); |
| 230 |
if (r < 0) { |
| 231 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 232 |
"Failed to parse CAN restart sec '%s', ignoring: %m", rvalue); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
if (usec != USEC_INFINITY && |
| 237 |
DIV_ROUND_UP(usec, USEC_PER_MSEC) > UINT32_MAX) { |
| 238 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 239 |
"CAN RestartSec= must be in the range 0...%"PRIu32"ms, ignoring: %s", UINT32_MAX, rvalue); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
*restart_usec = usec; |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
int config_parse_can_control_mode( |
| 248 |
const char *unit, |
| 249 |
const char *filename, |
| 250 |
unsigned line, |
| 251 |
const char *section, |
| 252 |
unsigned section_line, |
| 253 |
const char *lvalue, |
| 254 |
int ltype, |
| 255 |
const char *rvalue, |
| 256 |
void *data, |
| 257 |
void *userdata) { |
| 258 |
|
| 259 |
Network *network = ASSERT_PTR(userdata); |
| 260 |
uint32_t mask = ltype; |
| 261 |
int r; |
| 262 |
|
| 263 |
assert(filename); |
| 264 |
assert(lvalue); |
| 265 |
assert(rvalue); |
| 266 |
assert(mask != 0); |
| 267 |
|
| 268 |
if (isempty(rvalue)) { |
| 269 |
network->can_control_mode_mask &= ~mask; |
| 270 |
network->can_control_mode_flags &= ~mask; |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
r = parse_boolean(rvalue); |
| 275 |
if (r < 0) { |
| 276 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 277 |
"Failed to parse CAN control mode '%s', ignoring: %s", lvalue, rvalue); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
network->can_control_mode_mask |= mask; |
| 282 |
SET_FLAG(network->can_control_mode_flags, mask, r); |
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
int config_parse_can_termination( |
| 287 |
const char *unit, |
| 288 |
const char *filename, |
| 289 |
unsigned line, |
| 290 |
const char *section, |
| 291 |
unsigned section_line, |
| 292 |
const char *lvalue, |
| 293 |
int ltype, |
| 294 |
const char *rvalue, |
| 295 |
void *data, |
| 296 |
void *userdata) { |
| 297 |
|
| 298 |
Network *network = userdata; |
| 299 |
int r; |
| 300 |
|
| 301 |
assert(filename); |
| 302 |
assert(lvalue); |
| 303 |
assert(rvalue); |
| 304 |
assert(data); |
| 305 |
|
| 306 |
if (isempty(rvalue)) { |
| 307 |
network->can_termination_set = false; |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
/* Note that 0 termination ohm value means no termination resistor, and there is no conflict |
| 312 |
* between parse_boolean() and safe_atou16() when Termination=0. However, Termination=1 must be |
| 313 |
* treated as 1 ohm, instead of true (and then the default ohm value). So, we need to parse the |
| 314 |
* string with safe_atou16() at first. */ |
| 315 |
|
| 316 |
r = safe_atou16(rvalue, &network->can_termination); |
| 317 |
if (r < 0) { |
| 318 |
r = parse_boolean(rvalue); |
| 319 |
if (r < 0) { |
| 320 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 321 |
"Failed to parse CAN termination value, ignoring: %s", rvalue); |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
network->can_termination = r ? CAN_TERMINATION_DEFAULT_OHM_VALUE : 0; |
| 326 |
} |
| 327 |
|
| 328 |
network->can_termination_set = true; |
| 329 |
return 0; |
| 330 |
} |