VCAN and CAN-FD DLC versus data length
Hi all.
I just ran into some curious behaviour with the vcan driver in regards to CAN-FD and the DLC.
If I send a CAN-FD frame with a length, which is not directly representable by the DLC field, the vcan driver conserves this value. E.g. a Length of 11 is sent and received in userspace, even though there is no DLC representation for 11.
I talked to a colleague about this and we could not think of a case, in which this behaviour presents a problem. It is just a small discrepancy from how a real CAN would behave.
cansend from the can-utils package actually makes sure not to use a data length, which is not directly representable. This is the output of a candump oagainst a patched version, which does not "fix" the length.
$ candump any & ./patched-cansend vcan0 123##011223344556677889900AA vcan0 123 [11] 11 22 33 44 55 66 77 88 99 00 AA
Does anyone have a real case, which would warrant a patch to the vcan driver to change this? Or should the CAN stack even return EINVAL on a data length, that is not representable?
Regards
Andre
Hi Andre,
Quoted 3 lines · Andre Naujoks
If I send a CAN-FD frame with a length, which is not directly representable by the DLC field, the vcan driver conserves this value. E.g. a Length of 11 is sent and received in userspace, even though there is no DLC representation for 11.
I talked to a colleague about this and we could not think of a case, in which this behaviour presents a problem. It is just a small discrepancy from how a real CAN would behave.
Right.
Last time when we changed the can-gw we had a discussion about this "sanitizing" of CAN specific data length here:
https://marc.info/?l=linux-can&m=156537620228658
1. When we get a CAN skb from the real CAN interface this is correct (in CAN length)
2. When we send stuff to the real CAN interface the value is fixed before writing the content into the CAN controllers registers.
Quoted 4 lines
cansend from the can-utils package actually makes sure not to use a data length, which is not directly representable. This is the output of a candump oagainst a patched version, which does not "fix" the length.
$ candump any & ./patched-cansend vcan0 123##011223344556677889900AA vcan0 123 [11] 11 22 33 44 55 66 77 88 99 00 AA
Yes. vcan is just forwarding frames without sanitizing. And even the can-gw can be used to (intentionally) create weird values in the length field - but makes sure that the length boundaries of CAN (0 .. 8) and CAN FD (0 .. 64) are met.
So using the len value e.g. in a for-statement never lets you write outside the CAN(FD) frame data[].
Quoted 1 line
Does anyone have a real case, which would warrant a patch to the vcan driver to change this? Or should the CAN stack even return EINVAL on a data length, that is not representable?
As we had that discussion in the URL above and I can't see anything breaking I would tend to leave it as-is.
When we would sanitize the vcan skb flow we might end up with many skb_copy() cases instead of skb_clone() (permormance!). And as people may create skbs with PF_PACKET we are also not able to fix-up the length until it reaches the driver layer anyway.
Does that fit for you?
Best,
Oliver
Am 17.01.20 um 16:06 schrieb Oliver Hartkopp:
Quoted 6 lines
Hi Andre,
Quoted 2 lines · Andre Naujoks
If I send a CAN-FD frame with a length, which is not directly representable by the DLC field, the vcan driver conserves this value. E.g. a Length of 11 is sent and received in userspace, even though there is no DLC representation for 11.
Quoted 7 lines
Quoted 1 line
I talked to a colleague about this and we could not think of a case, in which this behaviour presents a problem. It is just a small discrepancy from how a real CAN would behave.
Right.
Last time when we changed the can-gw we had a discussion about this "sanitizing" of CAN specific data length here:
Thanks for the link!
Quoted 14 lines
1. When we get a CAN skb from the real CAN interface this is correct (in CAN length)
2. When we send stuff to the real CAN interface the value is fixed before writing the content into the CAN controllers registers.
Quoted 4 lines
cansend from the can-utils package actually makes sure not to use a data length, which is not directly representable. This is the output of a candump oagainst a patched version, which does not "fix" the length.
$ candump any & ./patched-cansend vcan0 123##011223344556677889900AA vcan0 123 [11] 11 22 33 44 55 66 77 88 99 00 AAYes. vcan is just forwarding frames without sanitizing. And even the can-gw can be used to (intentionally) create weird values in the length field - but makes sure that the length boundaries of CAN (0 .. 8) and CAN FD (0 .. 64) are met.
So using the len value e.g. in a for-statement never lets you write outside the CAN(FD) frame data[].
Quoted 7 lines
Quoted 1 line
Does anyone have a real case, which would warrant a patch to the vcan driver to change this? Or should the CAN stack even return EINVAL on a data length, that is not representable?
As we had that discussion in the URL above and I can't see anything breaking I would tend to leave it as-is.
When we would sanitize the vcan skb flow we might end up with many
skb_copy()cases instead ofskb_clone()(permormance!). And as people may create skbs withPF_PACKETwe are also not able to fix-up the length until it reaches the driver layer anyway.Does that fit for you?
Yes, it makes sense. This was kind of what I was looking for: Somebody else, who thinks this is not an issue.
Thanks again for the detailed explanation! I think I will take care of the lengths in my application then. Just to have consistent behaviour on CAN vs VCAN. In the end it is a cosmetic issue.
Best Regars
Andre
Quoted 3 lines
Best,
Oliver