]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_can.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / iplink_can.c
1 /*
2 * iplink_can.c CAN device support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Wolfgang Grandegger <wg@grandegger.com>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <linux/can/netlink.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "ip_common.h"
21
22 static void print_usage(FILE *f)
23 {
24 fprintf(f,
25 "Usage: ip link set DEVICE type can\n"
26 "\t[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |\n"
27 "\t[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1\n \t phase-seg2 PHASE-SEG2 [ sjw SJW ] ]\n"
28 "\n"
29 "\t[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |\n"
30 "\t[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1\n \t dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]\n"
31 "\n"
32 "\t[ loopback { on | off } ]\n"
33 "\t[ listen-only { on | off } ]\n"
34 "\t[ triple-sampling { on | off } ]\n"
35 "\t[ one-shot { on | off } ]\n"
36 "\t[ berr-reporting { on | off } ]\n"
37 "\t[ fd { on | off } ]\n"
38 "\t[ fd-non-iso { on | off } ]\n"
39 "\t[ presume-ack { on | off } ]\n"
40 "\n"
41 "\t[ restart-ms TIME-MS ]\n"
42 "\t[ restart ]\n"
43 "\n"
44 "\t[ termination { 0..65535 } ]\n"
45 "\n"
46 "\tWhere: BITRATE := { 1..1000000 }\n"
47 "\t SAMPLE-POINT := { 0.000..0.999 }\n"
48 "\t TQ := { NUMBER }\n"
49 "\t PROP-SEG := { 1..8 }\n"
50 "\t PHASE-SEG1 := { 1..8 }\n"
51 "\t PHASE-SEG2 := { 1..8 }\n"
52 "\t SJW := { 1..4 }\n"
53 "\t RESTART-MS := { 0 | NUMBER }\n"
54 );
55 }
56
57 static void usage(void)
58 {
59 print_usage(stderr);
60 }
61
62 static int get_float(float *val, const char *arg)
63 {
64 float res;
65 char *ptr;
66
67 if (!arg || !*arg)
68 return -1;
69 res = strtof(arg, &ptr);
70 if (!ptr || ptr == arg || *ptr)
71 return -1;
72 *val = res;
73 return 0;
74 }
75
76 static void set_ctrlmode(char *name, char *arg,
77 struct can_ctrlmode *cm, __u32 flags)
78 {
79 if (strcmp(arg, "on") == 0) {
80 cm->flags |= flags;
81 } else if (strcmp(arg, "off") != 0) {
82 fprintf(stderr,
83 "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n",
84 name, arg);
85 exit(-1);
86 }
87 cm->mask |= flags;
88 }
89
90 static void print_ctrlmode(FILE *f, __u32 cm)
91 {
92 fprintf(f, "<");
93 #define _PF(cmflag, cmname) \
94 if (cm & cmflag) { \
95 cm &= ~cmflag; \
96 fprintf(f, "%s%s", cmname, cm ? "," : ""); \
97 }
98 _PF(CAN_CTRLMODE_LOOPBACK, "LOOPBACK");
99 _PF(CAN_CTRLMODE_LISTENONLY, "LISTEN-ONLY");
100 _PF(CAN_CTRLMODE_3_SAMPLES, "TRIPLE-SAMPLING");
101 _PF(CAN_CTRLMODE_ONE_SHOT, "ONE-SHOT");
102 _PF(CAN_CTRLMODE_BERR_REPORTING, "BERR-REPORTING");
103 _PF(CAN_CTRLMODE_FD, "FD");
104 _PF(CAN_CTRLMODE_FD_NON_ISO, "FD-NON-ISO");
105 _PF(CAN_CTRLMODE_PRESUME_ACK, "PRESUME-ACK");
106 #undef _PF
107 if (cm)
108 fprintf(f, "%x", cm);
109 fprintf(f, "> ");
110 }
111
112 static int can_parse_opt(struct link_util *lu, int argc, char **argv,
113 struct nlmsghdr *n)
114 {
115 struct can_bittiming bt = {}, dbt = {};
116 struct can_ctrlmode cm = {0, 0};
117
118 while (argc > 0) {
119 if (matches(*argv, "bitrate") == 0) {
120 NEXT_ARG();
121 if (get_u32(&bt.bitrate, *argv, 0))
122 invarg("invalid \"bitrate\" value\n", *argv);
123 } else if (matches(*argv, "sample-point") == 0) {
124 float sp;
125
126 NEXT_ARG();
127 if (get_float(&sp, *argv))
128 invarg("invalid \"sample-point\" value\n",
129 *argv);
130 bt.sample_point = (__u32)(sp * 1000);
131 } else if (matches(*argv, "tq") == 0) {
132 NEXT_ARG();
133 if (get_u32(&bt.tq, *argv, 0))
134 invarg("invalid \"tq\" value\n", *argv);
135 } else if (matches(*argv, "prop-seg") == 0) {
136 NEXT_ARG();
137 if (get_u32(&bt.prop_seg, *argv, 0))
138 invarg("invalid \"prop-seg\" value\n", *argv);
139 } else if (matches(*argv, "phase-seg1") == 0) {
140 NEXT_ARG();
141 if (get_u32(&bt.phase_seg1, *argv, 0))
142 invarg("invalid \"phase-seg1\" value\n", *argv);
143 } else if (matches(*argv, "phase-seg2") == 0) {
144 NEXT_ARG();
145 if (get_u32(&bt.phase_seg2, *argv, 0))
146 invarg("invalid \"phase-seg2\" value\n", *argv);
147 } else if (matches(*argv, "sjw") == 0) {
148 NEXT_ARG();
149 if (get_u32(&bt.sjw, *argv, 0))
150 invarg("invalid \"sjw\" value\n", *argv);
151 } else if (matches(*argv, "dbitrate") == 0) {
152 NEXT_ARG();
153 if (get_u32(&dbt.bitrate, *argv, 0))
154 invarg("invalid \"dbitrate\" value\n", *argv);
155 } else if (matches(*argv, "dsample-point") == 0) {
156 float sp;
157
158 NEXT_ARG();
159 if (get_float(&sp, *argv))
160 invarg("invalid \"dsample-point\" value\n", *argv);
161 dbt.sample_point = (__u32)(sp * 1000);
162 } else if (matches(*argv, "dtq") == 0) {
163 NEXT_ARG();
164 if (get_u32(&dbt.tq, *argv, 0))
165 invarg("invalid \"dtq\" value\n", *argv);
166 } else if (matches(*argv, "dprop-seg") == 0) {
167 NEXT_ARG();
168 if (get_u32(&dbt.prop_seg, *argv, 0))
169 invarg("invalid \"dprop-seg\" value\n", *argv);
170 } else if (matches(*argv, "dphase-seg1") == 0) {
171 NEXT_ARG();
172 if (get_u32(&dbt.phase_seg1, *argv, 0))
173 invarg("invalid \"dphase-seg1\" value\n", *argv);
174 } else if (matches(*argv, "dphase-seg2") == 0) {
175 NEXT_ARG();
176 if (get_u32(&dbt.phase_seg2, *argv, 0))
177 invarg("invalid \"dphase-seg2\" value\n", *argv);
178 } else if (matches(*argv, "dsjw") == 0) {
179 NEXT_ARG();
180 if (get_u32(&dbt.sjw, *argv, 0))
181 invarg("invalid \"dsjw\" value\n", *argv);
182 } else if (matches(*argv, "loopback") == 0) {
183 NEXT_ARG();
184 set_ctrlmode("loopback", *argv, &cm,
185 CAN_CTRLMODE_LOOPBACK);
186 } else if (matches(*argv, "listen-only") == 0) {
187 NEXT_ARG();
188 set_ctrlmode("listen-only", *argv, &cm,
189 CAN_CTRLMODE_LISTENONLY);
190 } else if (matches(*argv, "triple-sampling") == 0) {
191 NEXT_ARG();
192 set_ctrlmode("triple-sampling", *argv, &cm,
193 CAN_CTRLMODE_3_SAMPLES);
194 } else if (matches(*argv, "one-shot") == 0) {
195 NEXT_ARG();
196 set_ctrlmode("one-shot", *argv, &cm,
197 CAN_CTRLMODE_ONE_SHOT);
198 } else if (matches(*argv, "berr-reporting") == 0) {
199 NEXT_ARG();
200 set_ctrlmode("berr-reporting", *argv, &cm,
201 CAN_CTRLMODE_BERR_REPORTING);
202 } else if (matches(*argv, "fd") == 0) {
203 NEXT_ARG();
204 set_ctrlmode("fd", *argv, &cm,
205 CAN_CTRLMODE_FD);
206 } else if (matches(*argv, "fd-non-iso") == 0) {
207 NEXT_ARG();
208 set_ctrlmode("fd-non-iso", *argv, &cm,
209 CAN_CTRLMODE_FD_NON_ISO);
210 } else if (matches(*argv, "presume-ack") == 0) {
211 NEXT_ARG();
212 set_ctrlmode("presume-ack", *argv, &cm,
213 CAN_CTRLMODE_PRESUME_ACK);
214 } else if (matches(*argv, "restart") == 0) {
215 __u32 val = 1;
216
217 addattr32(n, 1024, IFLA_CAN_RESTART, val);
218 } else if (matches(*argv, "restart-ms") == 0) {
219 __u32 val;
220
221 NEXT_ARG();
222 if (get_u32(&val, *argv, 0))
223 invarg("invalid \"restart-ms\" value\n", *argv);
224 addattr32(n, 1024, IFLA_CAN_RESTART_MS, val);
225 } else if (matches(*argv, "termination") == 0) {
226 __u16 val;
227
228 NEXT_ARG();
229 if (get_u16(&val, *argv, 0))
230 invarg("invalid \"termination\" value\n",
231 *argv);
232 addattr16(n, 1024, IFLA_CAN_TERMINATION, val);
233 } else if (matches(*argv, "help") == 0) {
234 usage();
235 return -1;
236 } else {
237 fprintf(stderr, "can: unknown option \"%s\"\n", *argv);
238 usage();
239 return -1;
240 }
241 argc--, argv++;
242 }
243
244 if (bt.bitrate || bt.tq)
245 addattr_l(n, 1024, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
246 if (dbt.bitrate || dbt.tq)
247 addattr_l(n, 1024, IFLA_CAN_DATA_BITTIMING, &dbt, sizeof(dbt));
248 if (cm.mask)
249 addattr_l(n, 1024, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
250
251 return 0;
252 }
253
254 static const char *can_state_names[] = {
255 [CAN_STATE_ERROR_ACTIVE] = "ERROR-ACTIVE",
256 [CAN_STATE_ERROR_WARNING] = "ERROR-WARNING",
257 [CAN_STATE_ERROR_PASSIVE] = "ERROR-PASSIVE",
258 [CAN_STATE_BUS_OFF] = "BUS-OFF",
259 [CAN_STATE_STOPPED] = "STOPPED",
260 [CAN_STATE_SLEEPING] = "SLEEPING"
261 };
262
263 static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
264 {
265 if (!tb)
266 return;
267
268 if (tb[IFLA_CAN_CTRLMODE]) {
269 struct can_ctrlmode *cm = RTA_DATA(tb[IFLA_CAN_CTRLMODE]);
270
271 if (cm->flags)
272 print_ctrlmode(f, cm->flags);
273 }
274
275 if (tb[IFLA_CAN_STATE]) {
276 uint32_t state = rta_getattr_u32(tb[IFLA_CAN_STATE]);
277
278 fprintf(f, "state %s ", state <= CAN_STATE_MAX ?
279 can_state_names[state] : "UNKNOWN");
280 }
281
282 if (tb[IFLA_CAN_BERR_COUNTER]) {
283 struct can_berr_counter *bc =
284 RTA_DATA(tb[IFLA_CAN_BERR_COUNTER]);
285
286 fprintf(f, "(berr-counter tx %d rx %d) ", bc->txerr, bc->rxerr);
287 }
288
289 if (tb[IFLA_CAN_RESTART_MS]) {
290 __u32 *restart_ms = RTA_DATA(tb[IFLA_CAN_RESTART_MS]);
291
292 fprintf(f, "restart-ms %d ", *restart_ms);
293 }
294
295 /* bittiming is irrelevant if fixed bitrate is defined */
296 if (tb[IFLA_CAN_BITTIMING] && !tb[IFLA_CAN_BITRATE_CONST]) {
297 struct can_bittiming *bt = RTA_DATA(tb[IFLA_CAN_BITTIMING]);
298
299 fprintf(f, "\n bitrate %d sample-point %.3f ",
300 bt->bitrate, (float)bt->sample_point / 1000.);
301 fprintf(f, "\n tq %d prop-seg %d phase-seg1 %d phase-seg2 %d sjw %d",
302 bt->tq, bt->prop_seg, bt->phase_seg1, bt->phase_seg2,
303 bt->sjw);
304 }
305
306 /* bittiming const is irrelevant if fixed bitrate is defined */
307 if (tb[IFLA_CAN_BITTIMING_CONST] && !tb[IFLA_CAN_BITRATE_CONST]) {
308 struct can_bittiming_const *btc =
309 RTA_DATA(tb[IFLA_CAN_BITTIMING_CONST]);
310
311 fprintf(f, "\n %s: tseg1 %d..%d tseg2 %d..%d "
312 "sjw 1..%d brp %d..%d brp-inc %d",
313 btc->name, btc->tseg1_min, btc->tseg1_max,
314 btc->tseg2_min, btc->tseg2_max, btc->sjw_max,
315 btc->brp_min, btc->brp_max, btc->brp_inc);
316 }
317
318 if (tb[IFLA_CAN_BITRATE_CONST]) {
319 __u32 *bitrate_const = RTA_DATA(tb[IFLA_CAN_BITRATE_CONST]);
320 int bitrate_cnt = RTA_PAYLOAD(tb[IFLA_CAN_BITRATE_CONST]) /
321 sizeof(*bitrate_const);
322 int i;
323 __u32 bitrate = 0;
324
325 if (tb[IFLA_CAN_BITTIMING]) {
326 struct can_bittiming *bt =
327 RTA_DATA(tb[IFLA_CAN_BITTIMING]);
328 bitrate = bt->bitrate;
329 }
330
331 fprintf(f, "\n bitrate %u", bitrate);
332 fprintf(f, "\n [");
333
334 for (i = 0; i < bitrate_cnt - 1; ++i) {
335 /* This will keep lines below 80 signs */
336 if (!(i % 6) && i)
337 fprintf(f, "\n ");
338
339 fprintf(f, "%8u, ", bitrate_const[i]);
340 }
341
342 if (!(i % 6) && i)
343 fprintf(f, "\n ");
344 fprintf(f, "%8u ]", bitrate_const[i]);
345 }
346
347 /* data bittiming is irrelevant if fixed bitrate is defined */
348 if (tb[IFLA_CAN_DATA_BITTIMING] && !tb[IFLA_CAN_DATA_BITRATE_CONST]) {
349 struct can_bittiming *dbt =
350 RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING]);
351
352 fprintf(f, "\n dbitrate %d dsample-point %.3f ",
353 dbt->bitrate, (float)dbt->sample_point / 1000.);
354 fprintf(f, "\n dtq %d dprop-seg %d dphase-seg1 %d "
355 "dphase-seg2 %d dsjw %d",
356 dbt->tq, dbt->prop_seg, dbt->phase_seg1,
357 dbt->phase_seg2, dbt->sjw);
358 }
359
360 /* data bittiming const is irrelevant if fixed bitrate is defined */
361 if (tb[IFLA_CAN_DATA_BITTIMING_CONST] &&
362 !tb[IFLA_CAN_DATA_BITRATE_CONST]) {
363 struct can_bittiming_const *dbtc =
364 RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING_CONST]);
365
366 fprintf(f, "\n %s: dtseg1 %d..%d dtseg2 %d..%d "
367 "dsjw 1..%d dbrp %d..%d dbrp-inc %d",
368 dbtc->name, dbtc->tseg1_min, dbtc->tseg1_max,
369 dbtc->tseg2_min, dbtc->tseg2_max, dbtc->sjw_max,
370 dbtc->brp_min, dbtc->brp_max, dbtc->brp_inc);
371 }
372
373 if (tb[IFLA_CAN_DATA_BITRATE_CONST]) {
374 __u32 *dbitrate_const =
375 RTA_DATA(tb[IFLA_CAN_DATA_BITRATE_CONST]);
376 int dbitrate_cnt =
377 RTA_PAYLOAD(tb[IFLA_CAN_DATA_BITRATE_CONST]) /
378 sizeof(*dbitrate_const);
379 int i;
380 __u32 dbitrate = 0;
381
382 if (tb[IFLA_CAN_DATA_BITTIMING]) {
383 struct can_bittiming *dbt =
384 RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING]);
385 dbitrate = dbt->bitrate;
386 }
387
388 fprintf(f, "\n dbitrate %u", dbitrate);
389 fprintf(f, "\n [");
390
391 for (i = 0; i < dbitrate_cnt - 1; ++i) {
392 /* This will keep lines below 80 signs */
393 if (!(i % 6) && i)
394 fprintf(f, "\n ");
395
396 fprintf(f, "%8u, ", dbitrate_const[i]);
397 }
398
399 if (!(i % 6) && i)
400 fprintf(f, "\n ");
401 fprintf(f, "%8u ]", dbitrate_const[i]);
402 }
403
404 if (tb[IFLA_CAN_TERMINATION_CONST] && tb[IFLA_CAN_TERMINATION]) {
405 __u16 *trm = RTA_DATA(tb[IFLA_CAN_TERMINATION]);
406 __u16 *trm_const = RTA_DATA(tb[IFLA_CAN_TERMINATION_CONST]);
407 int trm_cnt = RTA_PAYLOAD(tb[IFLA_CAN_TERMINATION_CONST]) /
408 sizeof(*trm_const);
409 int i;
410
411 fprintf(f, "\n termination %hu [ ", *trm);
412
413 for (i = 0; i < trm_cnt - 1; ++i)
414 fprintf(f, "%hu, ", trm_const[i]);
415
416 fprintf(f, "%hu ]", trm_const[i]);
417 }
418
419 if (tb[IFLA_CAN_CLOCK]) {
420 struct can_clock *clock = RTA_DATA(tb[IFLA_CAN_CLOCK]);
421
422 fprintf(f, "\n clock %d", clock->freq);
423 }
424
425 }
426
427 static void can_print_xstats(struct link_util *lu,
428 FILE *f, struct rtattr *xstats)
429 {
430 struct can_device_stats *stats;
431
432 if (xstats && RTA_PAYLOAD(xstats) == sizeof(*stats)) {
433 stats = RTA_DATA(xstats);
434 fprintf(f, "\n re-started bus-errors arbit-lost "
435 "error-warn error-pass bus-off");
436 fprintf(f, "\n %-10d %-10d %-10d %-10d %-10d %-10d",
437 stats->restarts, stats->bus_error,
438 stats->arbitration_lost, stats->error_warning,
439 stats->error_passive, stats->bus_off);
440 }
441 }
442
443 static void can_print_help(struct link_util *lu, int argc, char **argv,
444 FILE *f)
445 {
446 print_usage(f);
447 }
448
449 struct link_util can_link_util = {
450 .id = "can",
451 .maxattr = IFLA_CAN_MAX,
452 .parse_opt = can_parse_opt,
453 .print_opt = can_print_opt,
454 .print_xstats = can_print_xstats,
455 .print_help = can_print_help,
456 };