Compare commits

...

7 Commits

Author SHA1 Message Date
GGZ8 8858c1c413
Merge 7815be8741 into 374fecde09 2025-12-05 13:58:55 +01: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
GGZ8 7815be8741 WIP on pr-realcan: 277048e RealCAN implementation 2024-09-30 12:41:39 +02:00
GGZ8 dae324dbd5 index on pr-realcan: 277048e RealCAN implementation 2024-09-30 12:41:39 +02:00
GZZ8 277048eba6 RealCAN implementation
Replay pre-recorded CAN Bus dumps respecting the original relative timestamps.

Fix code style

Conform to codebase style as per maintainer's suggestions.

Refactor code logic according to suggestions for PR #521
2024-05-27 13:52:19 +02:00
3 changed files with 178 additions and 64 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 : "");

171
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) {
@ -857,7 +867,14 @@ int main(int argc, char **argv)
if (ret)
return 1;
int k[] = {1,2};
int j = 0;
while (running) {
ts_gap = double_to_timespec((k[j%2])/1000.0);
// printf("%lu, %lu\n", ts_gap.tv_sec, ts_gap.tv_nsec);
setsockopt_txtime(s);
setup_time();
j++;
/* clear values but preserve cu.fd.len */
cu.fd.flags = 0;
cu.fd.__res0 = 0;
@ -1040,8 +1057,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 +1122,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 */
}
}

View File

@ -48,6 +48,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/can.h>
#include <linux/can/raw.h>
@ -89,6 +90,12 @@ const int canfx_on = 1;
extern int optind, opterr, optopt;
struct sleep {
struct timeval *sleep_vector;
size_t idx;
size_t size;
};
static void print_usage(char *prg)
{
fprintf(stderr, "%s - replay a compact CAN frame logfile to CAN devices.\n", prg);
@ -117,6 +124,8 @@ static void print_usage(char *prg)
"loopback of sent CAN frames)\n");
fprintf(stderr, " -v (verbose: print "
"sent CAN frames)\n");
fprintf(stderr, " -r (real-time: send "
"CAN frames in real-time)\n");
fprintf(stderr, " -h (show "
"this help message)\n\n");
fprintf(stderr, "Interface assignment:\n");
@ -280,8 +289,11 @@ int main(int argc, char **argv)
int eof, txmtu, i, j;
char *fret;
unsigned long long sec, usec;
bool gap_from_file = false;
struct sleep timestamps;
struct timeval send_time, act_time, init_trace, init_time;
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvh")) != -1) {
while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvrh")) != -1) {
switch (opt) {
case 'I':
infile = fopen(optarg, "r");
@ -336,6 +348,17 @@ int main(int argc, char **argv)
verbose++;
break;
case 'r':
if (isatty(fileno(infile))) {
fprintf(stderr, "Specify an input file for option -r !\n");
exit(EXIT_FAILURE);
}
gap_from_file = true; /* using time delta from file */
init_trace.tv_sec = 0;
init_trace.tv_usec = 0;
timestamps.idx = 0; /*to avoid warning accessing idx variable*/
break;
case 'h':
print_usage(basename(argv[0]));
exit(EXIT_SUCCESS);
@ -368,8 +391,10 @@ int main(int argc, char **argv)
printf("interactive mode: press ENTER to process next CAN frame ...\n");
}
sleep_ts.tv_sec = gap / 1000;
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
if (!gap_from_file) {
sleep_ts.tv_sec = gap / 1000;
sleep_ts.tv_nsec = (gap % 1000) * 1000000;
}
/* open socket */
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
@ -553,6 +578,26 @@ int main(int argc, char **argv)
}
log_tv.tv_sec = sec;
if (gap_from_file){
if (timestamps.idx == 0){
gettimeofday(&init_time, NULL);
if (log_tv.tv_sec > 0 || log_tv.tv_usec > 0)
init_trace = log_tv;
}
timersub(&log_tv, &init_trace, &send_time);
if (timestamps.idx > 0){
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);
while (timercmp(&act_time, &send_time, <)){
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);
}
}
timestamps.idx++;
}
/*
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
* 3rd party or handcrafted logfiles that treat the timestamp as float
@ -582,7 +627,7 @@ int main(int argc, char **argv)
} /* while frames_to_send ... */
if (nanosleep(&sleep_ts, NULL))
if (!gap_from_file && nanosleep(&sleep_ts, NULL))
return 1;
delay_loops++; /* private statistics */