Compare commits

...

11 Commits

Author SHA1 Message Date
Ben Gardiner cb19c9d24b
Merge 63500f8dd0 into 374fecde09 2025-12-04 00:51:46 -08:00
Oliver Hartkopp 374fecde09 cangen: disable generation of unsupported CAN frame types in mixed mode
The mixed mode is able to automatically detect the potential supported
CAN frame types CAN CC/FD/XL by checking the CAN device MTU at startup.

Usually the MTU shows which CAN frame types can be sent but in the case of
CAN XL in CANXL-only mode CC and FD frames can not be sent on the CAN_RAW
socket.

Since this patch [1] the CAN_RAW socket rejects unsupported CAN frames and
returns -EINVAL as error code. With this change in cangen the CC and FD
frame generation can be disabled in mixed mode at runtime.

[1] https://lore.kernel.org/linux-can/20251125123859.3924-17-socketcan@hartkopp.net/T/#u

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2025-12-03 16:09:12 +01:00
Oliver Hartkopp bfbf0c851f cangen: auto enable FD/XL content in mixed mode
Automatically create FD/XL content in mixed mode when the CAN interface
is capable to deal with it.

Suggested-by: Vincent Mailhol <mailhol@kernel.org>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2025-12-03 16:09:12 +01:00
Oliver Hartkopp ed7822c9a2 candump: print CAN XL specific flags as extra message infos
The extra message infos BRS and ESI have been printed when enabled with
the '-x' option. Since CAN XL has similar flags (SEC and RRS) those flags
have been wrongly printed as CAN FD flags.

This patch introduces separate flags for the CAN XL frames and prints them
when a CAN XL frame is processed.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2025-11-26 19:04:50 +01:00
Marc Kleine-Budde e9ea9d1168
Merge pull request #607 from marckleinebudde/cansend-fix-typo
cansend: fix typo in CANFD_FDF flag name
2025-11-07 12:55:39 +01:00
Marc Kleine-Budde 2d793d38d2 cansend: fix typo in CANFD_FDF flag name
Closes: https://github.com/linux-can/can-utils/issues/606
Fixes: 3e0e6ae3a4 ("cansend: print canfd_frame.flags in help text")
2025-11-07 12:51:19 +01:00
Marc Kleine-Budde 340a3b8c92
Merge pull request #605 from marckleinebudde/lib_h-selfcontained
lib.h: include linux/can.h
2025-11-07 12:34:08 +01:00
Marc Kleine-Budde 8838ce80fc
Merge pull request #604 from marckleinebudde/cansend-canfd-flags
cansend: print canfd_frame.flags in help text
2025-11-07 12:33:02 +01:00
Marc Kleine-Budde d6c57240ed lib.h: include linux/can.h
Make lib.h self contained, so that language servers can parse it without
errors.
2025-11-07 12:30:15 +01:00
Marc Kleine-Budde 3e0e6ae3a4 cansend: print canfd_frame.flags in help text 2025-11-07 12:05:04 +01:00
Ben Gardiner 63500f8dd0 canplayer-bisect: introduce a tool to hunt for can packets by bisecting replays of candump logs 2016-04-25 20:14:28 -07:00
5 changed files with 335 additions and 62 deletions

View File

@ -113,7 +113,8 @@ static const int canfx_on = 1;
#define MAXANI 4
static const char anichar[MAXANI] = { '|', '/', '-', '\\' };
static const char extra_m_info[4][4] = { "- -", "B -", "- E", "B E" };
static const char extra_fd_info[4][4] = { "- -", "B -", "- E", "B E" };
static const char extra_xl_info[4][4] = { "- -", "S -", "- R", "S R" };
extern int optind, opterr, optopt;
@ -143,7 +144,7 @@ static void print_usage(void)
fprintf(stderr, " -d (monitor dropped CAN frames)\n");
fprintf(stderr, " -e (dump CAN error frames in human-readable format)\n");
fprintf(stderr, " -8 (display raw DLC values in {} for Classical CAN)\n");
fprintf(stderr, " -x (print extra message infos, rx/tx brs esi)\n");
fprintf(stderr, " -x (print extra message infos, rx/tx [brs esi|sec rrs])\n");
fprintf(stderr, " -T <msecs> (terminate after <msecs> if no frames were received)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
@ -892,11 +893,16 @@ int main(int argc, char **argv)
if (extra_msg_info) {
if (msg.msg_flags & MSG_DONTROUTE)
alen += sprintf(afrbuf + alen, " TX %s",
extra_m_info[cu.fd.flags & 3]);
alen += sprintf(afrbuf + alen, " TX");
else
alen += sprintf(afrbuf + alen, " RX %s",
extra_m_info[cu.fd.flags & 3]);
alen += sprintf(afrbuf + alen, " RX");
if (cu.xl.flags & CANXL_XLF)
alen += sprintf(afrbuf + alen, " %s",
extra_xl_info[cu.xl.flags & 3]);
else
alen += sprintf(afrbuf + alen, " %s",
extra_fd_info[cu.fd.flags & 3]);
}
alen += sprintf(afrbuf + alen, "%s ", (color == 1) ? col_off : "");

164
cangen.c
View File

@ -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 CC [-e -R] FD [-f -b -E] XL [-X] on capable devices)\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");
@ -339,10 +339,9 @@ resend:
nbytes = sendmsg(fd, &msg, 0);
if (nbytes < 0) {
ret = -errno;
if (ret != -ENOBUFS) {
perror("write");
if (ret != -ENOBUFS)
return ret;
}
if (!ignore_enobufs && !timeout) {
perror("write");
return ret;
@ -455,6 +454,9 @@ int main(int argc, char **argv)
unsigned char extended = 0;
unsigned char canfd = 0;
unsigned char canxl = 0;
unsigned char mixcc = 1; /* mix default */
unsigned char mixfd = 1; /* mix default */
unsigned char mixxl = 1; /* mix default */
unsigned char brs = 0;
unsigned char esi = 0;
unsigned char mix = 0;
@ -574,7 +576,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,14 +778,58 @@ int main(int argc, char **argv)
&loopback, sizeof(loopback));
}
if (canfd || canxl) {
/* get CAN netdevice MTU for frame type capabilities */
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
perror("SIOCGIFMTU");
return 1;
}
/* check if the frame fits into the CAN netdevice */
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
perror("SIOCGIFMTU");
/* check CAN XL support */
if (ifr.ifr_mtu < (int)CANXL_MIN_MTU) {
mixxl = 0;
if (canxl) {
printf("CAN interface not CAN XL capable - sorry.\n");
return 1;
}
}
/* check CAN FD support */
if (ifr.ifr_mtu < (int)CANFD_MTU) {
mixfd = 0;
if (canfd) {
printf("CAN interface not CAN FD capable - sorry.\n");
return 1;
}
}
/* enable CAN FD on the socket */
if (mixfd) {
/* interface is ok - try to switch the socket into CAN FD mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN FD support\n");
return 1;
}
}
/* enable CAN XL on the socket */
if (mixxl) {
/* interface is ok - try to switch the socket into CAN XL mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN XL support\n");
return 1;
}
/* try to enable the CAN XL VCID pass through mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
&vcid_opts, sizeof(vcid_opts))) {
printf("error when enabling CAN XL VCID pass through\n");
return 1;
}
}
/* sanitize given values */
if (canfd || canxl) {
if (canfd) {
/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
cu.fd.len = can_fd_dlc2len(can_fd_len2dlc(cu.fd.len));
@ -793,41 +838,6 @@ int main(int argc, char **argv)
if (cu.fd.len > CANFD_MAX_DLEN)
cu.fd.len = CANFD_MAX_DLEN;
}
if (canxl && (ifr.ifr_mtu < (int)CANXL_MIN_MTU)) {
printf("CAN interface not CAN XL capable - sorry.\n");
return 1;
}
if (canfd && (ifr.ifr_mtu < (int)CANFD_MTU)) {
printf("CAN interface not CAN FD capable - sorry.\n");
return 1;
}
if (ifr.ifr_mtu == (int)CANFD_MTU) {
/* interface is ok - try to switch the socket into CAN FD mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN FD support\n");
return 1;
}
}
if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU) {
/* interface is ok - try to switch the socket into CAN XL mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_FRAMES,
&enable_canfx, sizeof(enable_canfx))){
printf("error when enabling CAN XL support\n");
return 1;
}
/* try to enable the CAN XL VCID pass through mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
&vcid_opts, sizeof(vcid_opts))) {
printf("error when enabling CAN XL VCID pass through\n");
return 1;
}
}
} else {
/* sanitize Classical CAN 2.0 frame length */
if (len8_dlc) {
@ -1040,8 +1050,31 @@ int main(int argc, char **argv)
}
ret = do_send_one(s, &cu, mtu, polltimeout);
if (ret)
if ((ret == -EINVAL) && mix) {
/* mix mode: disable unsupported CAN frame type */
switch (mtu) {
case CAN_MTU:
mixcc = 0;
break;
case CANFD_MTU:
mixfd = 0;
break;
case CANXL_MTU:
mixxl = 0;
break;
default:
printf ("mix mode: unknown MTU");
return 1;
}
if (!mixcc && !mixfd && !mixxl) {
printf ("mix mode: no valid CAN frame types\n");
return 1;
}
} else if (ret) {
/* other error than -ENOBUFS and -EINVAL */
perror("write");
return 1;
}
if (burst_sent_count >= burst_count)
burst_sent_count = 0;
@ -1082,18 +1115,41 @@ int main(int argc, char **argv)
}
if (mix) {
canfd = 0;
canxl = 0;
i = random();
extended = i & 1;
canfd = i & 2;
if (canfd) {
brs = i & 4;
esi = i & 8;
if (mixfd) {
canfd = i & 2;
if (canfd) {
brs = i & 4;
esi = i & 8;
}
}
if (mixxl) {
if (mixfd)
canxl = ((i & 96) == 96); /* 1/4 */
else
canxl = ((i & 32) == 32); /* 1/2 */
}
if (mixcc) {
rtr_frame = ((i & 24) == 24); /* reduce RTR to 1/4 */
} else {
/* no CC frames allowed - CAN XL-only mode? */
if (!canxl && !canfd) {
/* force XL or FD frames */
if (mixxl)
canxl = 1;
else if (mixfd) {
canfd = 1;
brs = i & 4;
esi = i & 8;
} else {
printf ("mix mode: no valid CAN frame types\n");
return 1;
}
}
}
/* generate CAN XL traffic if the interface is capable */
if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU)
canxl = ((i & 96) == 96);
rtr_frame = ((i & 24) == 24); /* reduce RTR frames to 1/4 */
}
}

205
canplayer-bisect 100755
View File

@ -0,0 +1,205 @@
#!/bin/bash
if [ -z "${CANPLAYER}" ]; then
CANPLAYER="canplayer"
fi
die() {
echo "$*" > /dev/stderr
exit 1
}
usage() {
echo "canplayer-bisect <start|stop|clean|good|yes|bad|no|again|where|undo> <logfile> <canplayer options>"
}
is_ready() {
if [ ! -d .canplayer-bisect ]; then
usage
exit 1
fi
return 0
}
setup() {
is_ready
LOGFILE=$(cat .canplayer-bisect/logfile |head -n 1)
SAVED_LEN="$(cat .canplayer-bisect/len|tail -n 1)"
LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')"
if [ "$LEN" != "$SAVED_LEN" ]; then
die "logfile has changed size. restart"
fi
CANPLAYER_ARGS=$(cat .canplayer-bisect/args |head -n 1)
HEAD="$(cat .canplayer-bisect/head |tail -n 1)"
TAIL="$(cat .canplayer-bisect/tail |tail -n 1)"
}
back() {
HEAD="$(cat .canplayer-bisect/head |tail -n 2 |head -n1)"
TAIL="$(cat .canplayer-bisect/tail |tail -n 2 |head -n1)"
}
do_undo() {
sed -i '$ d' .canplayer-bisect/head
sed -i '$ d' .canplayer-bisect/tail
}
teardown() {
mkdir -p .canplayer-bisect
echo $LEN > .canplayer-bisect/len
echo $LOGFILE > .canplayer-bisect/logfile
echo $CANPLAYER_ARGS > .canplayer-bisect/args
echo $HEAD >> .canplayer-bisect/head
echo $TAIL >> .canplayer-bisect/tail
}
show() {
cat $LOGFILE | sed -n ${HEAD},${TAIL}p
}
play() {
#we *could* pipe directly to canplayer, but then the user can't add -l i to CANPLAYER_ARGS to hunt for packets using looped playback
the_show="$(mktemp)"
trap "rm -rf \"${the_show}\"" EXIT
show > "${the_show}"
"${CANPLAYER}" ${CANPLAYER_ARGS} -I "${the_show}"
}
do_show() {
setup
show
}
check_heads_n_tails() {
if [ $HEAD -eq $TAIL ]; then
do_stop
fi
}
do_good() {
setup
check_heads_n_tails
if [ $(( $HEAD + 1 )) -eq $TAIL ]; then
TAIL=$HEAD
else
TAIL=$(( ( $TAIL - $HEAD ) / 2 + $HEAD - 1 ))
fi
teardown
play
}
do_bad() {
setup
check_heads_n_tails
back
if [ $(( $HEAD + 1 )) -eq $TAIL ]; then
HEAD=$TAIL
else
HEAD=$(( ( $TAIL - $HEAD ) / 2 + $HEAD ))
fi
teardown
play
}
do_again() {
setup
play
}
do_start() {
do_clean
LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')"
HEAD=1
TAIL=$LEN
echo "assuming logfile contains the packets you seek... bisecting to first half"
teardown
play
}
do_where() {
setup
echo "between $HEAD and $TAIL (+$(( $TAIL - $HEAD ))) of $LOGFILE"
}
do_stop() {
setup
if [ "$COMMAND" == "no" ]; then
echo "failed to find what you were looking for"
exit 1
else
echo "the packets you seek are:"
do_where
exit 0
fi
}
do_clean() {
rm -rf .canplayer-bisect
}
if [ -z "$1" ]; then
usage
exit 1
fi
COMMAND=$1
if [ ! -d .canplayer-bisect ] && [ ! -z "$2" ] && [ ! -e "$2" ]; then
usage
exit 1
fi
LOGFILE="$2"
shift
shift
CANPLAYER_ARGS="$*"
case "$COMMAND" in
start)
do_start
;;
stop)
do_stop
;;
clean)
do_clean
;;
good|yes)
do_good
;;
bad|no)
do_bad
;;
again)
do_again
;;
where)
do_where
;;
undo)
do_undo
;;
show)
do_show
;;
esac

View File

@ -80,7 +80,10 @@ static void print_usage(char *prg)
"_{dlc}:\n"
" an optional 9..F data length code value when payload length is 8\n"
"<flags>:\n"
" a single ASCII Hex value (0 .. F) which defines canfd_frame.flags\n"
" a single ASCII Hex value (0 .. F) which defines canfd_frame.flags:\n"
" %x CANFD_BRS\n"
" %x CANFD_ESI\n"
" %x CANFD_FDF\n"
"\n"
"<vcid>:\n"
" 2 hex chars - virtual CAN network identifier (00 .. FF)\n"
@ -100,7 +103,8 @@ static void print_usage(char *prg)
" 1F334455#1122334455667788_B / 123#R / 00000123#R3 / 333#R8_E /\n"
" 45123#81:00:12345678#11223344.556677 / 00242#81:07:40000123#112233\n"
"\n",
prg, prg);
prg, prg,
CANFD_BRS, CANFD_ESI, CANFD_FDF);
}
int main(int argc, char **argv)

2
lib.h
View File

@ -49,6 +49,8 @@
#include <stddef.h>
#include <stdio.h>
#include <linux/can.h>
#ifdef DEBUG
#define pr_debug(fmt, args...) printf(fmt, ##args)
#else