]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-can.c
network: can: add support for bus error reporting
[thirdparty/systemd.git] / src / network / networkd-can.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <linux/can/netlink.h>
5
6 #include "netlink-util.h"
7 #include "networkd-can.h"
8 #include "networkd-link.h"
9 #include "networkd-manager.h"
10 #include "parse-util.h"
11 #include "string-util.h"
12
13 #define CAN_TERMINATION_OHM_VALUE 120
14
15 int config_parse_can_bitrate(
16 const char* unit,
17 const char *filename,
18 unsigned line,
19 const char *section,
20 unsigned section_line,
21 const char *lvalue,
22 int ltype,
23 const char *rvalue,
24 void *data,
25 void *userdata) {
26
27 uint32_t *br = data;
28 uint64_t sz;
29 int r;
30
31 assert(filename);
32 assert(lvalue);
33 assert(rvalue);
34 assert(data);
35
36 r = parse_size(rvalue, 1000, &sz);
37 if (r < 0) {
38 log_syntax(unit, LOG_WARNING, filename, line, r,
39 "Failed to parse can bitrate '%s', ignoring: %m", rvalue);
40 return 0;
41 }
42
43 /* Linux uses __u32 for bitrates, so the value should not exceed that. */
44 if (sz <= 0 || sz > UINT32_MAX) {
45 log_syntax(unit, LOG_WARNING, filename, line, 0,
46 "Bit rate out of permitted range 1...4294967295");
47 return 0;
48 }
49
50 *br = (uint32_t) sz;
51
52 return 0;
53 }
54
55 static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
56 int r;
57
58 assert(link);
59
60 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
61 return 1;
62
63 r = sd_netlink_message_get_errno(m);
64 if (r < 0)
65 /* we warn but don't fail the link, as it may be brought up later */
66 log_link_message_warning_errno(link, m, r, "Could not bring up interface");
67
68 return 1;
69 }
70
71 static int link_up_can(Link *link) {
72 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
73 int r;
74
75 assert(link);
76
77 log_link_debug(link, "Bringing CAN link up");
78
79 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
80 if (r < 0)
81 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
82
83 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
84 if (r < 0)
85 return log_link_error_errno(link, r, "Could not set link flags: %m");
86
87 r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
88 link_netlink_destroy_callback, link);
89 if (r < 0)
90 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
91
92 link_ref(link);
93
94 return 0;
95 }
96
97 static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
98 int r;
99
100 assert(link);
101
102 log_link_debug(link, "Set link");
103
104 r = sd_netlink_message_get_errno(m);
105 if (r < 0 && r != -EEXIST) {
106 log_link_message_warning_errno(link, m, r, "Failed to configure CAN link");
107 link_enter_failed(link);
108 }
109
110 return 1;
111 }
112
113 static int link_set_can(Link *link) {
114 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
115 struct can_ctrlmode cm = {};
116 int r;
117
118 assert(link);
119 assert(link->network);
120 assert(link->manager);
121 assert(link->manager->rtnl);
122
123 log_link_debug(link, "Configuring CAN link.");
124
125 r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex);
126 if (r < 0)
127 return log_link_error_errno(link, r, "Failed to allocate netlink message: %m");
128
129 r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK);
130 if (r < 0)
131 return log_link_error_errno(link, r, "Could not set netlink flags: %m");
132
133 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
134 if (r < 0)
135 return log_link_error_errno(link, r, "Failed to open netlink container: %m");
136
137 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind);
138 if (r < 0)
139 return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m");
140
141 if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) {
142 struct can_bittiming bt = {
143 .bitrate = link->network->can_bitrate,
144 .sample_point = link->network->can_sample_point,
145 };
146
147 log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate);
148 if (link->network->can_sample_point > 0)
149 log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
150 else
151 log_link_debug(link, "Using default sample point");
152
153 r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
154 if (r < 0)
155 return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m");
156 }
157
158 if (link->network->can_data_bitrate > 0 || link->network->can_data_sample_point > 0) {
159 struct can_bittiming bt = {
160 .bitrate = link->network->can_data_bitrate,
161 .sample_point = link->network->can_data_sample_point,
162 };
163
164 log_link_debug(link, "Setting data bitrate = %d bit/s", bt.bitrate);
165 if (link->network->can_data_sample_point > 0)
166 log_link_debug(link, "Setting data sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
167 else
168 log_link_debug(link, "Using default data sample point");
169
170 r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt));
171 if (r < 0)
172 return log_link_error_errno(link, r, "Could not append IFLA_CAN_DATA_BITTIMING attribute: %m");
173 }
174
175 if (link->network->can_fd_mode >= 0) {
176 cm.mask |= CAN_CTRLMODE_FD;
177 SET_FLAG(cm.flags, CAN_CTRLMODE_FD, link->network->can_fd_mode);
178 log_link_debug(link, "Setting FD mode to '%s'.", yes_no(link->network->can_fd_mode));
179 }
180
181 if (link->network->can_non_iso >= 0) {
182 cm.mask |= CAN_CTRLMODE_FD_NON_ISO;
183 SET_FLAG(cm.flags, CAN_CTRLMODE_FD_NON_ISO, link->network->can_non_iso);
184 log_link_debug(link, "Setting FD non-ISO mode to '%s'.", yes_no(link->network->can_non_iso));
185 }
186
187 if (link->network->can_restart_us > 0) {
188 char time_string[FORMAT_TIMESPAN_MAX];
189 uint64_t restart_ms;
190
191 if (link->network->can_restart_us == USEC_INFINITY)
192 restart_ms = 0;
193 else
194 restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC);
195
196 format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC);
197
198 if (restart_ms > UINT32_MAX)
199 return log_link_error_errno(link, SYNTHETIC_ERRNO(ERANGE), "restart timeout (%s) too big.", time_string);
200
201 log_link_debug(link, "Setting restart = %s", time_string);
202
203 r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms);
204 if (r < 0)
205 return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
206 }
207
208 if (link->network->can_triple_sampling >= 0) {
209 cm.mask |= CAN_CTRLMODE_3_SAMPLES;
210 SET_FLAG(cm.flags, CAN_CTRLMODE_3_SAMPLES, link->network->can_triple_sampling);
211 log_link_debug(link, "Setting triple-sampling to '%s'.", yes_no(link->network->can_triple_sampling));
212 }
213
214 if (link->network->can_berr_reporting >= 0) {
215 cm.mask |= CAN_CTRLMODE_BERR_REPORTING;
216 SET_FLAG(cm.flags, CAN_CTRLMODE_BERR_REPORTING, link->network->can_berr_reporting);
217 log_link_debug(link, "Setting bus error reporting to '%s'.", yes_no(link->network->can_berr_reporting));
218 }
219
220 if (link->network->can_listen_only >= 0) {
221 cm.mask |= CAN_CTRLMODE_LISTENONLY;
222 SET_FLAG(cm.flags, CAN_CTRLMODE_LISTENONLY, link->network->can_listen_only);
223 log_link_debug(link, "Setting listen-only mode to '%s'.", yes_no(link->network->can_listen_only));
224 }
225
226 if (cm.mask != 0) {
227 r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
228 if (r < 0)
229 return log_link_error_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
230 }
231
232 if (link->network->can_termination >= 0) {
233
234 log_link_debug(link, "Setting can-termination to '%s'.", yes_no(link->network->can_termination));
235
236 r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION,
237 link->network->can_termination ? CAN_TERMINATION_OHM_VALUE : 0);
238 if (r < 0)
239 return log_link_error_errno(link, r, "Could not append IFLA_CAN_TERMINATION attribute: %m");
240
241 }
242
243 r = sd_netlink_message_close_container(m);
244 if (r < 0)
245 return log_link_error_errno(link, r, "Failed to close netlink container: %m");
246
247 r = sd_netlink_message_close_container(m);
248 if (r < 0)
249 return log_link_error_errno(link, r, "Failed to close netlink container: %m");
250
251 r = netlink_call_async(link->manager->rtnl, NULL, m, link_set_handler,
252 link_netlink_destroy_callback, link);
253 if (r < 0)
254 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
255
256 link_ref(link);
257
258 if (!(link->flags & IFF_UP))
259 return link_up_can(link);
260
261 return 0;
262 }
263
264 static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
265 int r;
266
267 assert(link);
268
269 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
270 return 1;
271
272 r = sd_netlink_message_get_errno(m);
273 if (r < 0) {
274 log_link_message_warning_errno(link, m, r, "Could not bring down interface");
275 link_enter_failed(link);
276 return 1;
277 }
278
279 r = link_set_can(link);
280 if (r < 0)
281 link_enter_failed(link);
282
283 return 1;
284 }
285
286 int link_configure_can(Link *link) {
287 int r;
288
289 link_set_state(link, LINK_STATE_CONFIGURING);
290
291 if (streq_ptr(link->kind, "can")) {
292 /* The CAN interface must be down to configure bitrate, etc... */
293 if ((link->flags & IFF_UP)) {
294 r = link_down(link, link_down_handler);
295 if (r < 0) {
296 link_enter_failed(link);
297 return r;
298 }
299 } else {
300 r = link_set_can(link);
301 if (r < 0) {
302 link_enter_failed(link);
303 return r;
304 }
305 }
306
307 return 0;
308 }
309
310 if (!(link->flags & IFF_UP)) {
311 r = link_up_can(link);
312 if (r < 0) {
313 link_enter_failed(link);
314 return r;
315 }
316 }
317
318 return 0;
319 }