]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-can.c
network: use request queue to configure CAN interfaces
[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 "networkd-can.h"
7 #include "networkd-link.h"
8 #include "networkd-network.h"
9 #include "networkd-setlink.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 int can_set_netlink_message(Link *link, sd_netlink_message *m) {
56 struct can_ctrlmode cm = {};
57 int r;
58
59 assert(link);
60 assert(link->network);
61 assert(m);
62
63 r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK);
64 if (r < 0)
65 return log_link_debug_errno(link, r, "Could not set netlink flags: %m");
66
67 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
68 if (r < 0)
69 return log_link_debug_errno(link, r, "Failed to open IFLA_LINKINFO container: %m");
70
71 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind);
72 if (r < 0)
73 return log_link_debug_errno(link, r, "Could not open IFLA_INFO_DATA container: %m");
74
75 if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) {
76 struct can_bittiming bt = {
77 .bitrate = link->network->can_bitrate,
78 .sample_point = link->network->can_sample_point,
79 };
80
81 log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate);
82 if (link->network->can_sample_point > 0)
83 log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
84 else
85 log_link_debug(link, "Using default sample point");
86
87 r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
88 if (r < 0)
89 return log_link_debug_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m");
90 }
91
92 if (link->network->can_data_bitrate > 0 || link->network->can_data_sample_point > 0) {
93 struct can_bittiming bt = {
94 .bitrate = link->network->can_data_bitrate,
95 .sample_point = link->network->can_data_sample_point,
96 };
97
98 log_link_debug(link, "Setting data bitrate = %d bit/s", bt.bitrate);
99 if (link->network->can_data_sample_point > 0)
100 log_link_debug(link, "Setting data sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
101 else
102 log_link_debug(link, "Using default data sample point");
103
104 r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt));
105 if (r < 0)
106 return log_link_debug_errno(link, r, "Could not append IFLA_CAN_DATA_BITTIMING attribute: %m");
107 }
108
109 if (link->network->can_restart_us > 0) {
110 char time_string[FORMAT_TIMESPAN_MAX];
111 uint64_t restart_ms;
112
113 if (link->network->can_restart_us == USEC_INFINITY)
114 restart_ms = 0;
115 else
116 restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC);
117
118 format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC);
119
120 if (restart_ms > UINT32_MAX)
121 return log_link_debug_errno(link, SYNTHETIC_ERRNO(ERANGE), "restart timeout (%s) too big.", time_string);
122
123 log_link_debug(link, "Setting restart = %s", time_string);
124
125 r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms);
126 if (r < 0)
127 return log_link_debug_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
128 }
129
130 if (link->network->can_fd_mode >= 0) {
131 cm.mask |= CAN_CTRLMODE_FD;
132 SET_FLAG(cm.flags, CAN_CTRLMODE_FD, link->network->can_fd_mode);
133 log_link_debug(link, "Setting FD mode to '%s'.", yes_no(link->network->can_fd_mode));
134 }
135
136 if (link->network->can_non_iso >= 0) {
137 cm.mask |= CAN_CTRLMODE_FD_NON_ISO;
138 SET_FLAG(cm.flags, CAN_CTRLMODE_FD_NON_ISO, link->network->can_non_iso);
139 log_link_debug(link, "Setting FD non-ISO mode to '%s'.", yes_no(link->network->can_non_iso));
140 }
141
142 if (link->network->can_triple_sampling >= 0) {
143 cm.mask |= CAN_CTRLMODE_3_SAMPLES;
144 SET_FLAG(cm.flags, CAN_CTRLMODE_3_SAMPLES, link->network->can_triple_sampling);
145 log_link_debug(link, "Setting triple-sampling to '%s'.", yes_no(link->network->can_triple_sampling));
146 }
147
148 if (link->network->can_berr_reporting >= 0) {
149 cm.mask |= CAN_CTRLMODE_BERR_REPORTING;
150 SET_FLAG(cm.flags, CAN_CTRLMODE_BERR_REPORTING, link->network->can_berr_reporting);
151 log_link_debug(link, "Setting bus error reporting to '%s'.", yes_no(link->network->can_berr_reporting));
152 }
153
154 if (link->network->can_listen_only >= 0) {
155 cm.mask |= CAN_CTRLMODE_LISTENONLY;
156 SET_FLAG(cm.flags, CAN_CTRLMODE_LISTENONLY, link->network->can_listen_only);
157 log_link_debug(link, "Setting listen-only mode to '%s'.", yes_no(link->network->can_listen_only));
158 }
159
160 if (cm.mask != 0) {
161 r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
162 if (r < 0)
163 return log_link_debug_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
164 }
165
166 if (link->network->can_termination >= 0) {
167 log_link_debug(link, "Setting can-termination to '%s'.", yes_no(link->network->can_termination));
168
169 r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION,
170 link->network->can_termination ? CAN_TERMINATION_OHM_VALUE : 0);
171 if (r < 0)
172 return log_link_debug_errno(link, r, "Could not append IFLA_CAN_TERMINATION attribute: %m");
173 }
174
175 r = sd_netlink_message_close_container(m);
176 if (r < 0)
177 return log_link_debug_errno(link, r, "Failed to close IFLA_INFO_DATA container: %m");
178
179 r = sd_netlink_message_close_container(m);
180 if (r < 0)
181 return log_link_debug_errno(link, r, "Failed to close IFLA_LINKINFO container: %m");
182
183 return 0;
184 }