Write canfd_frame to can interface
Hi there!
I have been working on getting my device compatible with both, CAN and CAN FD.
For receiving this is working straight forward. My physical interface is CAN FD capable
and no matter whether I set it up as
ip link set can0 type can bitrate 500000 fd off
or
ip link set can0 type can bitrate 500000 fd dbitrate 2000000 off
in code I can always just use the canfd_frame struct and set the CAN_RAW_FD_FRAMES
option. Doing this I can receive CAN and CAN FD frames in both modes without having
to fall back to the can_frame struct (as explained in the docs).
For sending I expected a similar behavior. I set the CAN_RAW_FD_FRAMES option and
always sent using the canfd_frame struct. Sadly, this fails while writing on the interface
when it is not in FD-mode with an Invalid Argument error. To make this work without
falling back to the can_frame struct I just do
write(sock, &canfdf, sizeof(struct can_frame));
where canfdf is a canfd_frame. Not setting CAN_RAW_FD_FRAMES when the interface
is in CAN mode but sending using the full canfd_frame won't work too.
Is this expected behavior? Shouldn't the error only be returned if the canfd_frame I
pass has more than 8 bytes when the interface is not in FD-mode?
Regards,
Thomas
Marc Kleine-Budde
maintainerQuoted 23 lines · On 29.07.2021 12:03:56, [email protected] wrote:
I have been working on getting my device compatible with both, CAN and CAN FD.
For receiving this is working straight forward. My physical interface is CAN FD capable and no matter whether I set it up as
ip link set can0 type can bitrate 500000 fd off
or
ip link set can0 type can bitrate 500000 fd dbitrate 2000000 off
in code I can always just use thecanfd_framestruct and set theCAN_RAW_FD_FRAMESoption. Doing this I can receive CAN and CAN FD frames in both modes without having to fall back to thecan_framestruct (as explained in the docs).For sending I expected a similar behavior. I set the
CAN_RAW_FD_FRAMESoption and always sent using thecanfd_framestruct. Sadly, this fails while writing on the interface when it is not in FD-mode with an Invalid Argument error. To make this work without falling back to thecan_framestruct I just do
write(sock, &canfdf, sizeof(struct can_frame));
where canfdf is acanfd_frame. Not settingCAN_RAW_FD_FRAMESwhen the interface is in CAN mode but sending using the fullcanfd_framewon't work too.
Quoted 1 line
Is this expected behavior?
Yes.
Quoted 3 lines
Shouldn't the error only be returned if the
canfd_frameI pass has more than 8 bytes when the interface is not in FD-mode?
A CAN-2.0 frame with 8 bytes is something different than a CAN-FD frame with 8 bytes. The kernel uses the length of the frame to decide if it is a CAN-2.0 or CAN-FD frame. If your CAN controller has switched CAN-FD off, it cannot send CAN-FD frames, thus you get an error.
Does that make sense?
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung West/Dortmund | Phone: +49-231-2826-924 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Hello Marc,
Quoted 11 lines · Marc Kleine-Budde
Quoted 3 lines · On 29.07.2021 12:03:56, [email protected] wrote:
Shouldn't the error only be returned if the
canfd_frameI pass has more than 8 bytes when the interface is not in FD-mode?A CAN-2.0 frame with 8 bytes is something different than a CAN-FD frame with 8 bytes. The kernel uses the length of the frame to decide if it is a CAN-2.0 or CAN-FD frame. If your CAN controller has switched CAN-FD off, it cannot send CAN-FD frames, thus you get an error.
Does that make sense?
Sure!
I see how a CAN-2.0 frame with 8 bytes differs from a CAN-FD frame with 8-bytes, but when I receive into a canfd_frame I can't differentiate like that anymore. In userspace an 8B CAN-2.0 frame and an 8B CAN-FD frame look just the same, no matter the interface running with FD on or off.
... which is wrong as I just noticed. Paying attention to the actual bytes read by the socket I can see the 16 vs. 72B that make up a can_frame vs. a canfd_frame respectively. Even when always writing into a canfd_frame. The same differentiation I must make when sending...
Thanks for the quick reply!
Best regards
Thomas Wagner
Marc Kleine-Budde
maintainerQuoted 17 lines · Thomas Wagner
Hello Marc,
Quoted 11 lines · Marc Kleine-Budde
Quoted 3 lines · On 29.07.2021 12:03:56, [email protected] wrote:
Shouldn't the error only be returned if the
canfd_frameI pass has more than 8 bytes when the interface is not in FD-mode?A CAN-2.0 frame with 8 bytes is something different than a CAN-FD frame with 8 bytes. The kernel uses the length of the frame to decide if it is a CAN-2.0 or CAN-FD frame. If your CAN controller has switched CAN-FD off, it cannot send CAN-FD frames, thus you get an error.
Does that make sense?
Sure!
Quoted 8 lines
I see how a CAN-2.0 frame with 8 bytes differs from a CAN-FD frame with 8-bytes, but when I receive into a
canfd_frameI can't differentiate like that anymore. In userspace an 8B CAN-2.0 frame and an 8B CAN-FD frame look just the same, no matter the interface running with FD on or off.... which is wrong as I just noticed. Paying attention to the actual bytes read by the socket I can see the 16 vs. 72B that make up a
can_framevs. acanfd_framerespectively. Even when always writing into acanfd_frame.
With the C language the type information of your read buffer doesn't leave the scope of your function (write() uses a void * for the buffer). The length information is checked in the kernel, but only if it's large enough:
https://elixir.bootlin.com/linux/v5.13/source/net/can/raw.c#L850
Quoted 1 line
The same differentiation I must make when sending...
ACK - If you an idea how to improve the documentation, let me know!
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung West/Dortmund | Phone: +49-231-2826-924 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |