summaryrefslogtreecommitdiffstats
path: root/src/network/networkd-can.c
blob: b8a1871a35016b1bbbc1518a021b84c61b8ff972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <net/if.h>
#include <linux/can/netlink.h>

#include "networkd-can.h"
#include "networkd-link.h"
#include "networkd-network.h"
#include "networkd-setlink.h"
#include "parse-util.h"
#include "string-util.h"

#define CAN_TERMINATION_DEFAULT_OHM_VALUE 120

int can_set_netlink_message(Link *link, sd_netlink_message *m) {
        int r;

        assert(link);
        assert(link->network);
        assert(m);

        r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK);
        if (r < 0)
                return r;

        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
        if (r < 0)
                return r;

        r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind);
        if (r < 0)
                return r;

        if (link->network->can_bitrate > 0) {
                struct can_bittiming bt = {
                        .bitrate = link->network->can_bitrate,
                        .sample_point = link->network->can_sample_point,
                        .sjw = link->network->can_sync_jump_width,
                };

                log_link_debug(link, "Setting bitrate = %u bit/s", bt.bitrate);
                if (link->network->can_sample_point > 0)
                        log_link_debug(link, "Setting sample point = %u.%u%%", bt.sample_point / 10, bt.sample_point % 10);
                else
                        log_link_debug(link, "Using default sample point");

                r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
                if (r < 0)
                        return r;
        } else if (link->network->can_time_quanta_ns > 0) {
                struct can_bittiming bt = {
                        .tq = link->network->can_time_quanta_ns,
                        .prop_seg = link->network->can_propagation_segment,
                        .phase_seg1 = link->network->can_phase_buffer_segment_1,
                        .phase_seg2 = link->network->can_phase_buffer_segment_2,
                        .sjw = link->network->can_sync_jump_width,
                };

                log_link_debug(link, "Setting time quanta = %"PRIu32" nsec", bt.tq);
                r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
                if (r < 0)
                        return r;
        }

        if (link->network->can_data_bitrate > 0) {
                struct can_bittiming bt = {
                        .bitrate = link->network->can_data_bitrate,
                        .sample_point = link->network->can_data_sample_point,
                        .sjw = link->network->can_data_sync_jump_width,
                };

                log_link_debug(link, "Setting data bitrate = %u bit/s", bt.bitrate);
                if (link->network->can_data_sample_point > 0)
                        log_link_debug(link, "Setting data sample point = %u.%u%%", bt.sample_point / 10, bt.sample_point % 10);
                else
                        log_link_debug(link, "Using default data sample point");

                r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt));
                if (r < 0)
                        return r;
        } else if (link->network->can_data_time_quanta_ns > 0) {
                struct can_bittiming bt = {
                        .tq = link->network->can_data_time_quanta_ns,
                        .prop_seg = link->network->can_data_propagation_segment,
                        .phase_seg1 = link->network->can_data_phase_buffer_segment_1,
                        .phase_seg2 = link->network->can_data_phase_buffer_segment_2,
                        .sjw = link->network->can_data_sync_jump_width,
                };

                log_link_debug(link, "Setting data time quanta = %"PRIu32" nsec", bt.tq);
                r = sd_netlink_message_append_data(m, IFLA_CAN_DATA_BITTIMING, &bt, sizeof(bt));
                if (r < 0)
                        return r;
        }

        if (link->network->can_restart_us > 0) {
                uint64_t restart_ms;

                if (link->network->can_restart_us == USEC_INFINITY)
                        restart_ms = 0;
                else
                        restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC);

                log_link_debug(link, "Setting restart = %s", FORMAT_TIMESPAN(restart_ms * 1000, MSEC_PER_SEC));
                r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms);
                if (r < 0)
                        return r;
        }

        if (link->network->can_control_mode_mask != 0) {
                struct can_ctrlmode cm = {
                        .mask = link->network->can_control_mode_mask,
                        .flags = link->network->can_control_mode_flags,
                };

                r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
                if (r < 0)
                        return r;
        }

        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);
                if (r < 0)
                        return r;
        }

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return r;

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return r;

        return 0;
}

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 = ASSERT_PTR(data);
        uint64_t sz;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        r = parse_size(rvalue, 1000, &sz);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, 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_WARNING, filename, line, 0,
                           "Bit rate out of permitted range 1...4294967295");
                return 0;
        }

        *br = (uint32_t) sz;

        return 0;
}

int config_parse_can_time_quanta(
                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) {

        nsec_t val, *tq = ASSERT_PTR(data);
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        r = parse_nsec(rvalue, &val);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse can time quanta '%s', ignoring: %m", rvalue);
                return 0;
        }

        /* Linux uses __u32 for bitrates, so the value should not exceed that. */
        if (val <= 0 || val > UINT32_MAX) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Time quanta out of permitted range 1...4294967295");
                return 0;
        }

        *tq = val;
        return 0;
}

int config_parse_can_restart_usec(
                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) {

        usec_t usec, *restart_usec = ASSERT_PTR(data);
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        r = parse_sec(rvalue, &usec);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse CAN restart sec '%s', ignoring: %m", rvalue);
                return 0;
        }

        if (usec != USEC_INFINITY &&
            DIV_ROUND_UP(usec, USEC_PER_MSEC) > UINT32_MAX) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "CAN RestartSec= must be in the range 0...%"PRIu32"ms, ignoring: %s", UINT32_MAX, rvalue);
                return 0;
        }

        *restart_usec = usec;
        return 0;
}

int config_parse_can_control_mode(
                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 = ASSERT_PTR(userdata);
        uint32_t mask = ltype;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(mask != 0);

        if (isempty(rvalue)) {
                network->can_control_mode_mask &= ~mask;
                network->can_control_mode_flags &= ~mask;
                return 0;
        }

        r = parse_boolean(rvalue);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse CAN control mode '%s', ignoring: %s", lvalue, rvalue);
                return 0;
        }

        network->can_control_mode_mask |= mask;
        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;
}