This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 9f2d3bb0855803b6ac669726d7deffd15b13334c Author: raiden00pl <[email protected]> AuthorDate: Fri Nov 27 09:50:34 2026 +0100 canutils/isotpperf: Add an ISO-TP throughput and integrity test tool. Sends or receives ISO-TP messages of a given length over a SocketCAN interface, checks their sequence and contents, and reports the message and data rate, lost and corrupted messages. The receiver can set the flow control block size and STmin. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: raiden00pl <[email protected]> --- canutils/isotpperf/CMakeLists.txt | 35 +++ canutils/isotpperf/Kconfig | 32 +++ canutils/isotpperf/Make.defs | 25 ++ canutils/isotpperf/Makefile | 32 +++ canutils/isotpperf/isotpperf_main.c | 487 ++++++++++++++++++++++++++++++++++++ 5 files changed, 611 insertions(+) diff --git a/canutils/isotpperf/CMakeLists.txt b/canutils/isotpperf/CMakeLists.txt new file mode 100644 index 000000000..10f123b14 --- /dev/null +++ b/canutils/isotpperf/CMakeLists.txt @@ -0,0 +1,35 @@ +# ############################################################################## +# apps/canutils/isotpperf/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_CANUTILS_ISOTPPERF) + nuttx_add_application( + NAME + ${CONFIG_CANUTILS_ISOTPPERF_PROGNAME} + PRIORITY + ${CONFIG_CANUTILS_ISOTPPERF_PRIORITY} + STACKSIZE + ${CONFIG_CANUTILS_ISOTPPERF_STACKSIZE} + MODULE + ${CONFIG_CANUTILS_ISOTPPERF} + SRCS + isotpperf_main.c) +endif() diff --git a/canutils/isotpperf/Kconfig b/canutils/isotpperf/Kconfig new file mode 100644 index 000000000..aa2130a2d --- /dev/null +++ b/canutils/isotpperf/Kconfig @@ -0,0 +1,32 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config CANUTILS_ISOTPPERF + tristate "ISO-TP throughput and integrity test tool" + default n + depends on NET_CAN_ISOTP + ---help--- + Send or receive ISO-TP messages of a given length over a + SocketCAN interface, check their sequence and contents, and + report the message rate, data rate, lost and corrupted messages. + +if CANUTILS_ISOTPPERF + +config CANUTILS_ISOTPPERF_PROGNAME + string "Program name" + default "isotpperf" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config CANUTILS_ISOTPPERF_PRIORITY + int "isotpperf task priority" + default 100 + +config CANUTILS_ISOTPPERF_STACKSIZE + int "isotpperf stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/canutils/isotpperf/Make.defs b/canutils/isotpperf/Make.defs new file mode 100644 index 000000000..d94b4f887 --- /dev/null +++ b/canutils/isotpperf/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/canutils/isotpperf/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_CANUTILS_ISOTPPERF),) +CONFIGURED_APPS += $(APPDIR)/canutils/isotpperf +endif diff --git a/canutils/isotpperf/Makefile b/canutils/isotpperf/Makefile new file mode 100644 index 000000000..20d35e2eb --- /dev/null +++ b/canutils/isotpperf/Makefile @@ -0,0 +1,32 @@ +############################################################################ +# apps/canutils/isotpperf/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = $(CONFIG_CANUTILS_ISOTPPERF_PROGNAME) +PRIORITY = $(CONFIG_CANUTILS_ISOTPPERF_PRIORITY) +STACKSIZE = $(CONFIG_CANUTILS_ISOTPPERF_STACKSIZE) +MODULE = $(CONFIG_CANUTILS_ISOTPPERF) + +MAINSRC = isotpperf_main.c + +include $(APPDIR)/Application.mk diff --git a/canutils/isotpperf/isotpperf_main.c b/canutils/isotpperf/isotpperf_main.c new file mode 100644 index 000000000..51febdc9a --- /dev/null +++ b/canutils/isotpperf/isotpperf_main.c @@ -0,0 +1,487 @@ +/**************************************************************************** + * apps/canutils/isotpperf/isotpperf_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <inttypes.h> +#include <poll.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <nuttx/can.h> +#include <nuttx/can/isotp.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ISOTPPERF_SEQLEN 4 /* Sequence number bytes in a message */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct isotpperf_s +{ + bool tx; /* Sender, else receiver */ + FAR const char *ifname; /* SocketCAN interface */ + uint32_t txid; /* CAN ID of sent frames */ + uint32_t rxid; /* CAN ID of received frames */ + uint32_t count; /* Messages to send or receive */ + uint32_t len; /* Message length */ + uint32_t timeout; /* RX idle timeout in ms */ + uint8_t bs; /* RX flow control block size */ + uint8_t stmin; /* RX flow control STmin in ms */ + + /* Results */ + + uint32_t msgs; /* Messages sent or received */ + uint64_t bytes; /* Bytes sent or received */ + uint32_t lost; /* Sequence numbers skipped */ + uint32_t ooo; /* Sequence went backwards */ + uint32_t bad; /* Wrong length or contents */ + uint64_t t0; /* First message, us */ + uint64_t t1; /* Last message, us */ + bool haveseq; /* nextseq is valid */ + uint32_t nextseq; /* Expected sequence number */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static uint8_t g_buf[ISOTP_MAX_MSG_LEN]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: isotpperf_now + * + * Description: + * Get the monotonic time. + * + * Input Parameters: + * None + * + * Returned Value: + * Time in microseconds. + * + ****************************************************************************/ + +static uint64_t isotpperf_now(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000000ull + ts.tv_nsec / 1000; +} + +/**************************************************************************** + * Name: isotpperf_fill + * + * Description: + * Fill a message: a little-endian sequence number in the first + * ISOTPPERF_SEQLEN bytes, then a pattern. + * + * Input Parameters: + * buf - Message buffer + * len - Message length + * seq - Sequence number + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void isotpperf_fill(FAR uint8_t *buf, uint32_t len, uint32_t seq) +{ + uint32_t i; + + for (i = 0; i < len; i++) + { + buf[i] = i < ISOTPPERF_SEQLEN ? (uint8_t)(seq >> (8 * i)) : + (uint8_t)(seq + i); + } +} + +/**************************************************************************** + * Name: isotpperf_check + * + * Description: + * Check the pattern of a received message. + * + * Input Parameters: + * buf - Message + * len - Message length + * seq - Sequence number + * + * Returned Value: + * True if the pattern is correct. + * + ****************************************************************************/ + +static bool isotpperf_check(FAR const uint8_t *buf, uint32_t len, + uint32_t seq) +{ + uint32_t i; + + for (i = ISOTPPERF_SEQLEN; i < len; i++) + { + if (buf[i] != (uint8_t)(seq + i)) + { + return false; + } + } + + return true; +} + +/**************************************************************************** + * Name: isotpperf_report + * + * Description: + * Print the results. + * + * Input Parameters: + * p - Test state + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void isotpperf_report(FAR struct isotpperf_s *p) +{ + uint64_t us = p->t1 - p->t0; + uint64_t bps = us > 0 ? p->bytes * 1000000 / us : 0; + + printf("isotpperf: mode=%s msgs=%" PRIu32 " bytes=%" PRIu64 + " us=%" PRIu64 " Bps=%" PRIu64 " lost=%" PRIu32 " ooo=%" PRIu32 + " bad=%" PRIu32 "\n", p->tx ? "tx" : "rx", p->msgs, p->bytes, us, + bps, p->lost, p->ooo, p->bad); +} + +/**************************************************************************** + * Name: isotpperf_tx + * + * Description: + * Send messages. + * + * Input Parameters: + * p - Test state + * sock - Bound ISO-TP socket + * + * Returned Value: + * Zero on success, a negated errno value on failure. + * + ****************************************************************************/ + +static int isotpperf_tx(FAR struct isotpperf_s *p, int sock) +{ + uint32_t seq; + ssize_t ret; + + p->t0 = isotpperf_now(); + for (seq = 0; seq < p->count; seq++) + { + isotpperf_fill(g_buf, p->len, seq); + ret = write(sock, g_buf, p->len); + if (ret != (ssize_t)p->len) + { + fprintf(stderr, "isotpperf: write %" PRIu32 " failed: %zd\n", + seq, ret < 0 ? errno : ret); + return ret < 0 ? -errno : -EIO; + } + + p->msgs++; + p->bytes += p->len; + } + + p->t1 = isotpperf_now(); + return OK; +} + +/**************************************************************************** + * Name: isotpperf_rx + * + * Description: + * Receive and check messages until the count or the idle timeout. + * + * Input Parameters: + * p - Test state + * sock - Bound ISO-TP socket + * + * Returned Value: + * Zero. + * + ****************************************************************************/ + +static int isotpperf_rx(FAR struct isotpperf_s *p, int sock) +{ + struct pollfd pfd; + ssize_t ret; + uint32_t seq; + + pfd.fd = sock; + pfd.events = POLLIN; + + while (p->msgs + p->lost < p->count) + { + if (poll(&pfd, 1, p->timeout) <= 0) + { + break; + } + + ret = read(sock, g_buf, sizeof(g_buf)); + if (ret < 0) + { + fprintf(stderr, "isotpperf: read failed: %d\n", errno); + p->bad++; + continue; + } + + if (p->msgs == 0) + { + p->t0 = isotpperf_now(); + } + + p->t1 = isotpperf_now(); + p->msgs++; + p->bytes += ret; + + if (ret != (ssize_t)p->len || ret < ISOTPPERF_SEQLEN) + { + p->bad++; + continue; + } + + seq = (uint32_t)g_buf[0] | (uint32_t)g_buf[1] << 8 | + (uint32_t)g_buf[2] << 16 | (uint32_t)g_buf[3] << 24; + if (!isotpperf_check(g_buf, p->len, seq)) + { + p->bad++; + } + + if (p->haveseq) + { + if (seq > p->nextseq) + { + p->lost += seq - p->nextseq; + } + else if (seq < p->nextseq) + { + p->ooo++; + } + } + + p->haveseq = true; + p->nextseq = seq + 1; + } + + if (p->haveseq && p->nextseq < p->count) + { + p->lost += p->count - p->nextseq; + } + + return OK; +} + +/**************************************************************************** + * Name: isotpperf_usage + * + * Description: + * Print the usage. + * + * Input Parameters: + * progname - Program name + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void isotpperf_usage(FAR const char *progname) +{ + fprintf(stderr, + "Usage: %s -m tx|rx -i <ifname> -t <txid> -r <rxid> [options]\n" + " -n <count> messages (default 100)\n" + " -l <len> message length, %d..%d (default 256)\n" + " -T <ms> RX idle timeout (default 3000)\n" + " -b <bs> RX flow control block size (default 0)\n" + " -s <ms> RX flow control STmin (default 0)\n", + progname, ISOTPPERF_SEQLEN, ISOTP_MAX_MSG_LEN); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: isotpperf_main + * + * Description: + * Entry point of the isotpperf tool. + * + * Input Parameters: + * argc - Number of arguments + * argv - Arguments + * + * Returned Value: + * EXIT_SUCCESS or EXIT_FAILURE. + * + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + struct isotpperf_s p; + struct sockaddr_can addr; + struct ifreq ifr; + bool havemode = false; + int option; + int sock; + int ret; + + memset(&p, 0, sizeof(p)); + p.count = 100; + p.len = 256; + p.timeout = 3000; + p.txid = CAN_SFF_MASK + 1; + p.rxid = CAN_SFF_MASK + 1; + + while ((option = getopt(argc, argv, "m:i:t:r:n:l:T:b:s:h")) != ERROR) + { + switch (option) + { + case 'm': + p.tx = strcmp(optarg, "tx") == 0; + havemode = p.tx || strcmp(optarg, "rx") == 0; + break; + + case 'i': + p.ifname = optarg; + break; + + case 't': + p.txid = strtoul(optarg, NULL, 0); + break; + + case 'r': + p.rxid = strtoul(optarg, NULL, 0); + break; + + case 'n': + p.count = strtoul(optarg, NULL, 0); + break; + + case 'l': + p.len = strtoul(optarg, NULL, 0); + break; + + case 'T': + p.timeout = strtoul(optarg, NULL, 0); + break; + + case 'b': + p.bs = strtoul(optarg, NULL, 0); + break; + + case 's': + p.stmin = strtoul(optarg, NULL, 0); + break; + + default: + isotpperf_usage(argv[0]); + return EXIT_FAILURE; + } + } + + if (!havemode || p.ifname == NULL || p.txid > CAN_SFF_MASK || + p.rxid > CAN_SFF_MASK || p.len < ISOTPPERF_SEQLEN || + p.len > ISOTP_MAX_MSG_LEN) + { + isotpperf_usage(argv[0]); + return EXIT_FAILURE; + } + + sock = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP); + if (sock < 0) + { + fprintf(stderr, "isotpperf: socket failed: %d\n", errno); + return EXIT_FAILURE; + } + + strlcpy(ifr.ifr_name, p.ifname, IFNAMSIZ); + if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) + { + fprintf(stderr, "isotpperf: no interface %s\n", p.ifname); + ret = -errno; + goto out; + } + + if (!p.tx) + { + struct can_isotp_fc_options fc; + + memset(&fc, 0, sizeof(fc)); + fc.bs = p.bs; + fc.stmin = p.stmin; + if (setsockopt(sock, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fc, + sizeof(fc)) < 0) + { + fprintf(stderr, "isotpperf: RECV_FC failed: %d\n", errno); + } + } + + memset(&addr, 0, sizeof(addr)); + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + addr.can_addr.tp.tx_id = p.txid; + addr.can_addr.tp.rx_id = p.rxid; + if (bind(sock, (FAR struct sockaddr *)&addr, sizeof(addr)) < 0) + { + fprintf(stderr, "isotpperf: bind failed: %d\n", errno); + ret = -errno; + goto out; + } + + ret = p.tx ? isotpperf_tx(&p, sock) : isotpperf_rx(&p, sock); + isotpperf_report(&p); + +out: + close(sock); + return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +}
