Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH can-utils] cangen: allow to use -m on classical CAN interfaces
@ 2025-09-07  6:03 Vincent Mailhol
  2025-09-08 11:10 ` Marc Kleine-Budde
  0 siblings, 1 reply; 4+ messages in thread
From: Vincent Mailhol @ 2025-09-07  6:03 UTC (permalink / raw)
  To: Oliver Hartkopp, Marc Kleine-Budde; +Cc: linux-can, Vincent Mailhol

The -m option currently only works on CAN FD or CAN XL interfaces.
There is a logic to add CAN XL to the mix only if supported but CAN FD
is always forced.

Modify the -m logic so that only the options supported by the
interface are added to the mix. This way:

  - a Classical CAN interface only mixes -e and -R
  - a CAN FD interface mixes -e, -R, -f, -b and -E
  - a CAN XL interface mixes -e, -R, -f, -b, -E and -X

This provides a better user experience and also makes -m a good
default option for fuzzing any type of CAN interface.

Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
 cangen.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/cangen.c b/cangen.c
index 95ac32d..5715447 100644
--- a/cangen.c
+++ b/cangen.c
@@ -180,7 +180,7 @@ static void print_usage(char *prg)
 	fprintf(stderr, "         -X            (generate CAN XL CAN frames)\n");
 	fprintf(stderr, "         -R            (generate RTR frames)\n");
 	fprintf(stderr, "         -8            (allow DLC values greater then 8 for Classic CAN frames)\n");
-	fprintf(stderr, "         -m            (mix -e -f -b -E -R -X frames)\n");
+	fprintf(stderr, "         -m            (mix -e -R frames and -f -b -E if FD capable and -X if XL capable)\n");
 	fprintf(stderr, "         -I <mode>     (CAN ID generation mode - see below)\n");
 	fprintf(stderr, "         -L <mode>     (CAN data length code (dlc) generation mode - see below)\n");
 	fprintf(stderr, "         -D <mode>     (CAN data (payload) generation mode - see below)\n");
@@ -574,7 +574,6 @@ int main(int argc, char **argv)
 
 		case 'm':
 			mix = 1;
-			canfd = 1; /* to switch the socket into CAN FD mode */
 			view |= CANLIB_VIEW_INDENT_SFF;
 			break;
 
@@ -777,7 +776,7 @@ int main(int argc, char **argv)
 			   &loopback, sizeof(loopback));
 	}
 
-	if (canfd || canxl) {
+	if (mix || canfd || canxl) {
 
 		/* check if the frame fits into the CAN netdevice */
 		if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
@@ -1084,10 +1083,13 @@ int main(int argc, char **argv)
 		if (mix) {
 			i = random();
 			extended = i & 1;
-			canfd = i & 2;
-			if (canfd) {
-				brs = i & 4;
-				esi = i & 8;
+			/* generate CAN FD traffic if the interface is capable */
+			if (ifr.ifr_mtu >= (int)CANFD_MTU) {
+				canfd = i & 2;
+				if (canfd) {
+					brs = i & 4;
+					esi = i & 8;
+				}
 			}
 			/* generate CAN XL traffic if the interface is capable */
 			if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU)
-- 
2.49.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-09-08 11:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-07  6:03 [PATCH can-utils] cangen: allow to use -m on classical CAN interfaces Vincent Mailhol
2025-09-08 11:10 ` Marc Kleine-Budde
2025-09-08 11:27   ` Oliver Hartkopp
2025-09-08 11:33     ` Marc Kleine-Budde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox