19#include <netlink-private/netlink.h>
20#include <netlink/netlink.h>
21#include <netlink/attr.h>
22#include <netlink/utils.h>
23#include <netlink/object.h>
24#include <netlink/route/rtnl.h>
25#include <netlink-private/route/link/api.h>
26#include <netlink/route/link/can.h>
28#include <linux/can/netlink.h>
31#define CAN_HAS_BITTIMING (1<<0)
32#define CAN_HAS_BITTIMING_CONST (1<<1)
33#define CAN_HAS_CLOCK (1<<2)
34#define CAN_HAS_STATE (1<<3)
35#define CAN_HAS_CTRLMODE (1<<4)
36#define CAN_HAS_RESTART_MS (1<<5)
37#define CAN_HAS_RESTART (1<<6)
38#define CAN_HAS_BERR_COUNTER (1<<7)
39#define CAN_HAS_DATA_BITTIMING (1<<8)
40#define CAN_HAS_DATA_BITTIMING_CONST (1<<9)
45 uint32_t ci_restart_ms;
46 struct can_ctrlmode ci_ctrlmode;
47 struct can_bittiming ci_bittiming;
48 struct can_bittiming_const ci_bittiming_const;
49 struct can_clock ci_clock;
50 struct can_berr_counter ci_berr_counter;
52 struct can_bittiming ci_data_bittiming;
53 struct can_bittiming_const ci_data_bittiming_const;
58static struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
60 [IFLA_CAN_CTRLMODE] = { .minlen =
sizeof(
struct can_ctrlmode) },
61 [IFLA_CAN_RESTART_MS] = { .type =
NLA_U32 },
62 [IFLA_CAN_RESTART] = { .type =
NLA_U32 },
63 [IFLA_CAN_BITTIMING] = { .minlen =
sizeof(
struct can_bittiming) },
64 [IFLA_CAN_BITTIMING_CONST]
65 = { .minlen =
sizeof(
struct can_bittiming_const) },
66 [IFLA_CAN_CLOCK] = { .minlen =
sizeof(
struct can_clock) },
67 [IFLA_CAN_BERR_COUNTER] = { .minlen =
sizeof(
struct can_berr_counter) },
68 [IFLA_CAN_DATA_BITTIMING]
69 = { .minlen =
sizeof(
struct can_bittiming) },
70 [IFLA_CAN_DATA_BITTIMING_CONST]
71 = { .minlen =
sizeof(
struct can_bittiming_const) },
74static int can_alloc(
struct rtnl_link *link)
79 memset(link->l_info, 0,
sizeof(*ci));
81 ci = calloc(1,
sizeof(*ci));
91static int can_parse(
struct rtnl_link *link,
struct nlattr *data,
92 struct nlattr *xstats)
94 struct nlattr *tb[IFLA_CAN_MAX+1];
98 NL_DBG(3,
"Parsing CAN link info\n");
103 if ((err = can_alloc(link)) < 0)
108 if (tb[IFLA_CAN_STATE]) {
110 ci->ci_mask |= CAN_HAS_STATE;
113 if (tb[IFLA_CAN_RESTART]) {
114 ci->ci_restart =
nla_get_u32(tb[IFLA_CAN_RESTART]);
115 ci->ci_mask |= CAN_HAS_RESTART;
118 if (tb[IFLA_CAN_RESTART_MS]) {
119 ci->ci_restart_ms =
nla_get_u32(tb[IFLA_CAN_RESTART_MS]);
120 ci->ci_mask |= CAN_HAS_RESTART_MS;
123 if (tb[IFLA_CAN_CTRLMODE]) {
124 nla_memcpy(&ci->ci_ctrlmode, tb[IFLA_CAN_CTRLMODE],
125 sizeof(ci->ci_ctrlmode));
126 ci->ci_mask |= CAN_HAS_CTRLMODE;
129 if (tb[IFLA_CAN_BITTIMING]) {
130 nla_memcpy(&ci->ci_bittiming, tb[IFLA_CAN_BITTIMING],
131 sizeof(ci->ci_bittiming));
132 ci->ci_mask |= CAN_HAS_BITTIMING;
135 if (tb[IFLA_CAN_BITTIMING_CONST]) {
137 tb[IFLA_CAN_BITTIMING_CONST],
138 sizeof(ci->ci_bittiming_const));
139 ci->ci_mask |= CAN_HAS_BITTIMING_CONST;
142 if (tb[IFLA_CAN_CLOCK]) {
144 sizeof(ci->ci_clock));
145 ci->ci_mask |= CAN_HAS_CLOCK;
148 if (tb[IFLA_CAN_BERR_COUNTER]) {
149 nla_memcpy(&ci->ci_berr_counter, tb[IFLA_CAN_BERR_COUNTER],
150 sizeof(ci->ci_berr_counter));
151 ci->ci_mask |= CAN_HAS_BERR_COUNTER;
154 if (tb[IFLA_CAN_DATA_BITTIMING]) {
155 nla_memcpy(&ci->ci_data_bittiming, tb[IFLA_CAN_DATA_BITTIMING],
156 sizeof(ci->ci_data_bittiming));
157 ci->ci_mask |= CAN_HAS_DATA_BITTIMING;
160 if (tb[IFLA_CAN_DATA_BITTIMING_CONST]) {
161 nla_memcpy(&ci->ci_data_bittiming_const, tb[IFLA_CAN_DATA_BITTIMING_CONST],
162 sizeof(ci->ci_data_bittiming_const));
163 ci->ci_mask |= CAN_HAS_DATA_BITTIMING_CONST;
171static void can_free(
struct rtnl_link *link)
173 struct can_info *ci = link->l_info;
179static char *print_can_state (uint32_t state)
185 case CAN_STATE_ERROR_ACTIVE:
186 text =
"error active";
188 case CAN_STATE_ERROR_WARNING:
189 text =
"error warning";
191 case CAN_STATE_ERROR_PASSIVE:
192 text =
"error passive";
194 case CAN_STATE_BUS_OFF:
197 case CAN_STATE_STOPPED:
200 case CAN_STATE_SLEEPING:
204 text =
"unknown state";
212 struct can_info *ci = link->l_info;
215 rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf,
sizeof(buf));
216 nl_dump(p,
"bitrate %d %s <%s>",
217 ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
222 struct can_info *ci = link->l_info;
225 rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf,
sizeof(buf));
226 nl_dump(p,
" bitrate %d %s <%s>",
227 ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
229 if (ci->ci_mask & CAN_HAS_RESTART) {
231 nl_dump_line(p,
" restarting\n");
234 if (ci->ci_mask & CAN_HAS_RESTART_MS) {
235 nl_dump_line(p,
" restart interval %d ms\n",
239 if (ci->ci_mask & CAN_HAS_BITTIMING) {
240 nl_dump_line(p,
" sample point %f %%\n",
241 ((
float) ci->ci_bittiming.sample_point)/10);
242 nl_dump_line(p,
" time quanta %d ns\n",
243 ci->ci_bittiming.tq);
244 nl_dump_line(p,
" propagation segment %d tq\n",
245 ci->ci_bittiming.prop_seg);
246 nl_dump_line(p,
" phase buffer segment1 %d tq\n",
247 ci->ci_bittiming.phase_seg1);
248 nl_dump_line(p,
" phase buffer segment2 %d tq\n",
249 ci->ci_bittiming.phase_seg2);
250 nl_dump_line(p,
" synchronisation jump width %d tq\n",
251 ci->ci_bittiming.sjw);
252 nl_dump_line(p,
" bitrate prescaler %d\n",
253 ci->ci_bittiming.brp);
256 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST) {
257 nl_dump_line(p,
" minimum tsig1 %d tq\n",
258 ci->ci_bittiming_const.tseg1_min);
259 nl_dump_line(p,
" maximum tsig1 %d tq\n",
260 ci->ci_bittiming_const.tseg1_max);
261 nl_dump_line(p,
" minimum tsig2 %d tq\n",
262 ci->ci_bittiming_const.tseg2_min);
263 nl_dump_line(p,
" maximum tsig2 %d tq\n",
264 ci->ci_bittiming_const.tseg2_max);
265 nl_dump_line(p,
" maximum sjw %d tq\n",
266 ci->ci_bittiming_const.sjw_max);
267 nl_dump_line(p,
" minimum brp %d\n",
268 ci->ci_bittiming_const.brp_min);
269 nl_dump_line(p,
" maximum brp %d\n",
270 ci->ci_bittiming_const.brp_max);
271 nl_dump_line(p,
" brp increment %d\n",
272 ci->ci_bittiming_const.brp_inc);
275 if (ci->ci_mask & CAN_HAS_CLOCK) {
276 nl_dump_line(p,
" base freq %u Hz\n", ci->ci_clock.freq);
280 if (ci->ci_mask & CAN_HAS_BERR_COUNTER) {
281 nl_dump_line(p,
" bus error RX %d\n",
282 ci->ci_berr_counter.rxerr);
283 nl_dump_line(p,
" bus error TX %d\n",
284 ci->ci_berr_counter.txerr);
292 struct can_info *cdst, *csrc = src->l_info;
300 cdst = malloc(
sizeof(*cdst));
310static int can_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
312 struct can_info *ci = link->l_info;
319 if (ci->ci_mask & CAN_HAS_RESTART)
320 NLA_PUT_U32(msg, IFLA_CAN_RESTART, ci->ci_restart);
322 if (ci->ci_mask & CAN_HAS_RESTART_MS)
323 NLA_PUT_U32(msg, IFLA_CAN_RESTART_MS, ci->ci_restart_ms);
325 if (ci->ci_mask & CAN_HAS_CTRLMODE)
326 NLA_PUT(msg, IFLA_CAN_CTRLMODE,
sizeof(ci->ci_ctrlmode),
329 if (ci->ci_mask & CAN_HAS_BITTIMING)
330 NLA_PUT(msg, IFLA_CAN_BITTIMING,
sizeof(ci->ci_bittiming),
333 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
334 NLA_PUT(msg, IFLA_CAN_BITTIMING_CONST,
335 sizeof(ci->ci_bittiming_const),
336 &ci->ci_bittiming_const);
338 if (ci->ci_mask & CAN_HAS_CLOCK)
339 NLA_PUT(msg, IFLA_CAN_CLOCK,
sizeof(ci->ci_clock),
342 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING)
343 NLA_PUT(msg, IFLA_CAN_DATA_BITTIMING,
sizeof(ci->ci_data_bittiming),
344 &ci->ci_data_bittiming);
346 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING_CONST)
347 NLA_PUT(msg, IFLA_CAN_DATA_BITTIMING_CONST,
sizeof(ci->ci_data_bittiming_const),
348 &ci->ci_data_bittiming_const);
357static struct rtnl_link_info_ops can_info_ops = {
359 .io_alloc = can_alloc,
360 .io_parse = can_parse,
365 .io_clone = can_clone,
366 .io_put_attrs = can_put_attrs,
371#define IS_CAN_LINK_ASSERT(link) \
372 if ((link)->l_info_ops != &can_info_ops) { \
373 APPBUG("Link is not a CAN link. set type \"can\" first."); \
374 return -NLE_OPNOTSUPP; \
391 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"can");
402 struct can_info *ci = link->l_info;
404 IS_CAN_LINK_ASSERT(link);
407 ci->ci_restart |= CAN_HAS_RESTART;
421 struct can_info *ci = link->l_info;
423 IS_CAN_LINK_ASSERT(link);
427 if (ci->ci_mask & CAN_HAS_CLOCK)
428 *freq = ci->ci_clock.freq;
443 struct can_info *ci = link->l_info;
445 IS_CAN_LINK_ASSERT(link);
449 *state = ci->ci_state;
462 struct can_info *ci = link->l_info;
464 IS_CAN_LINK_ASSERT(link);
466 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
467 return ci->ci_berr_counter.rxerr;
480 struct can_info *ci = link->l_info;
482 IS_CAN_LINK_ASSERT(link);
484 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
485 return ci->ci_berr_counter.txerr;
499 struct can_info *ci = link->l_info;
501 IS_CAN_LINK_ASSERT(link);
505 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
506 *berr = ci->ci_berr_counter;
521 struct can_bittiming_const *bt_const)
523 struct can_info *ci = link->l_info;
525 IS_CAN_LINK_ASSERT(link);
529 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
530 *bt_const = ci->ci_bittiming_const;
545 struct can_bittiming *bit_timing)
547 struct can_info *ci = link->l_info;
549 IS_CAN_LINK_ASSERT(link);
553 if (ci->ci_mask & CAN_HAS_BITTIMING)
554 *bit_timing = ci->ci_bittiming;
569 const struct can_bittiming *bit_timing)
571 struct can_info *ci = link->l_info;
573 IS_CAN_LINK_ASSERT(link);
577 ci->ci_bittiming = *bit_timing;
578 ci->ci_mask |= CAN_HAS_BITTIMING;
592 struct can_info *ci = link->l_info;
594 IS_CAN_LINK_ASSERT(link);
598 if (ci->ci_mask & CAN_HAS_BITTIMING)
599 *bitrate = ci->ci_bittiming.bitrate;
615 struct can_info *ci = link->l_info;
617 IS_CAN_LINK_ASSERT(link);
619 ci->ci_bittiming.bitrate = bitrate;
620 ci->ci_mask |= CAN_HAS_BITTIMING;
634 struct can_info *ci = link->l_info;
636 IS_CAN_LINK_ASSERT(link);
640 if (ci->ci_mask & CAN_HAS_BITTIMING)
641 *sp = ci->ci_bittiming.sample_point;
657 struct can_info *ci = link->l_info;
659 IS_CAN_LINK_ASSERT(link);
661 ci->ci_bittiming.sample_point = sp;
662 ci->ci_mask |= CAN_HAS_BITTIMING;
676 struct can_info *ci = link->l_info;
678 IS_CAN_LINK_ASSERT(link);
682 if (ci->ci_mask & CAN_HAS_RESTART_MS)
683 *interval = ci->ci_restart_ms;
699 struct can_info *ci = link->l_info;
701 IS_CAN_LINK_ASSERT(link);
703 ci->ci_restart_ms = interval;
704 ci->ci_mask |= CAN_HAS_RESTART_MS;
718 struct can_info *ci = link->l_info;
720 IS_CAN_LINK_ASSERT(link);
724 if (ci->ci_mask & CAN_HAS_CTRLMODE)
725 *ctrlmode = ci->ci_ctrlmode.flags;
741 struct can_info *ci = link->l_info;
743 IS_CAN_LINK_ASSERT(link);
745 ci->ci_ctrlmode.flags |= ctrlmode;
746 ci->ci_ctrlmode.mask |= ctrlmode;
747 ci->ci_mask |= CAN_HAS_CTRLMODE;
761 struct can_info *ci = link->l_info;
763 IS_CAN_LINK_ASSERT(link);
765 ci->ci_ctrlmode.flags &= ~ctrlmode;
766 ci->ci_ctrlmode.mask |= ctrlmode;
767 ci->ci_mask |= CAN_HAS_CTRLMODE;
780 struct can_bittiming_const *data_bt_const)
782 struct can_info *ci = link->l_info;
784 IS_CAN_LINK_ASSERT(link);
788 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING_CONST)
789 *data_bt_const = ci->ci_data_bittiming_const;
804 const struct can_bittiming_const *data_bt_const)
806 struct can_info *ci = link->l_info;
808 IS_CAN_LINK_ASSERT(link);
812 ci->ci_data_bittiming_const = *data_bt_const;
813 ci->ci_mask |= CAN_HAS_DATA_BITTIMING_CONST;
826 struct can_bittiming *data_bit_timing)
828 struct can_info *ci = link->l_info;
830 IS_CAN_LINK_ASSERT(link);
831 if (!data_bit_timing)
834 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING)
835 *data_bit_timing = ci->ci_data_bittiming;
850 const struct can_bittiming *data_bit_timing)
852 struct can_info *ci = link->l_info;
854 IS_CAN_LINK_ASSERT(link);
855 if (!data_bit_timing)
858 ci->ci_data_bittiming = *data_bit_timing;
859 ci->ci_mask |= CAN_HAS_DATA_BITTIMING;
871static const struct trans_tbl can_ctrlmode[] = {
872 __ADD(CAN_CTRLMODE_LOOPBACK, loopback),
873 __ADD(CAN_CTRLMODE_LISTENONLY, listen-only),
874 __ADD(CAN_CTRLMODE_3_SAMPLES, triple-sampling),
875 __ADD(CAN_CTRLMODE_ONE_SHOT, one-shot),
876 __ADD(CAN_CTRLMODE_BERR_REPORTING, berr-reporting),
877 __ADD(CAN_CTRLMODE_FD, fd),
878 __ADD(CAN_CTRLMODE_PRESUME_ACK, presume-ack),
879 __ADD(CAN_CTRLMODE_FD_NON_ISO, fd-non-iso),
882char *rtnl_link_can_ctrlmode2str(
int ctrlmode,
char *buf,
size_t len)
884 return __flags2str(ctrlmode, buf, len, can_ctrlmode,
885 ARRAY_SIZE(can_ctrlmode));
888int rtnl_link_can_str2ctrlmode(
const char *name)
890 return __str2flags(name, can_ctrlmode, ARRAY_SIZE(can_ctrlmode));
895static void __init can_init(
void)
900static void __exit can_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int rtnl_link_can_berr_rx(struct rtnl_link *link)
Get CAN RX bus error count.
int rtnl_link_can_set_restart_ms(struct rtnl_link *link, uint32_t interval)
Set CAN device restart intervall.
int rtnl_link_can_get_restart_ms(struct rtnl_link *link, uint32_t *interval)
Get CAN device restart intervall.
int rtnl_link_can_get_ctrlmode(struct rtnl_link *link, uint32_t *ctrlmode)
Get CAN control mode.
int rtnl_link_can_freq(struct rtnl_link *link, uint32_t *freq)
Get CAN base frequency.
int rtnl_link_can_get_bittiming(struct rtnl_link *link, struct can_bittiming *bit_timing)
Get CAN device bit-timing.
int rtnl_link_can_unset_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
Unset a CAN Control Mode.
int rtnl_link_can_set_data_bittiming_const(struct rtnl_link *link, const struct can_bittiming_const *data_bt_const)
Set CAN FD device data bit-timing-const.
int rtnl_link_is_can(struct rtnl_link *link)
Check if link is a CAN link.
int rtnl_link_can_get_data_bittiming(struct rtnl_link *link, struct can_bittiming *data_bit_timing)
Get CAN FD device data bit-timing.
int rtnl_link_can_restart(struct rtnl_link *link)
Restart CAN device.
int rtnl_link_can_set_sample_point(struct rtnl_link *link, uint32_t sp)
Set CAN device sample point.
int rtnl_link_can_set_data_bittiming(struct rtnl_link *link, const struct can_bittiming *data_bit_timing)
Set CAN FD device data bit-timing.
int rtnl_link_can_get_sample_point(struct rtnl_link *link, uint32_t *sp)
Get CAN device sample point.
int rtnl_link_can_set_bittiming(struct rtnl_link *link, const struct can_bittiming *bit_timing)
Set CAN device bit-timing.
int rtnl_link_can_set_bitrate(struct rtnl_link *link, uint32_t bitrate)
Set CAN device bit-rate.
int rtnl_link_can_get_data_bittiming_const(struct rtnl_link *link, struct can_bittiming_const *data_bt_const)
Get CAN FD hardware-dependent data bit-timing constant.
int rtnl_link_can_get_bitrate(struct rtnl_link *link, uint32_t *bitrate)
Get CAN device bit-timing.
int rtnl_link_can_berr_tx(struct rtnl_link *link)
Get CAN TX bus error count.
int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state)
Get CAN state.
int rtnl_link_can_get_bt_const(struct rtnl_link *link, struct can_bittiming_const *bt_const)
Get CAN hardware-dependent bit-timing constant.
int rtnl_link_can_set_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
Set a CAN Control Mode.
int rtnl_link_can_berr(struct rtnl_link *link, struct can_berr_counter *berr)
Get CAN bus error count.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.