136 Commits

Author SHA1 Message Date
Piyush Patle 0dccc8ba21 include/debug.h: Move to include/nuttx/debug.h
debug.h is a NuttX-specific, non-POSIX header. Placing it in the
top-level include/ directory creates naming conflicts with external
projects that define their own debug.h.
This commit moves the canonical header to include/nuttx/debug.h,
following the NuttX convention for non-POSIX/non-standard headers,
and updates all in-tree references.

A backward-compatibility shim is left at include/debug.h that
emits a deprecation #warning and re-includes <nuttx/debug.h>,
allowing out-of-tree code to continue building while migrating.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
2026-04-07 07:50:06 -03:00
zhanghongyu 27214321be net/can,udp: fix conn unlock position in callback
Move conn_unlock() after the data event handling in can_callback()
and udp_callback(). Previously, the connection lock was released
immediately after devif_conn_event(), leaving the subsequent
can_data_event()/net_dataevent() and read-ahead buffer operations
unprotected. This could lead to a race condition where another
context modifies the connection state while data is being stored.

Hold the lock until the entire callback processing is complete to
ensure thread safety.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-04-02 20:55:27 +08:00
wangjinjing1 18820ef07d net/can: allow errmask to be set to CAN_ERR_FLAG;
For the LIN protocol, the error frame ID can be 0. This change is to ensure that all LIN error frames can pass the filter.

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2026-01-19 15:13:14 +08:00
guoshichao 3d45550da9 can_sendmsg_buffered: fix can_sendmsg param type mismatch error
Update the can_sendmsg() signature from "struct msghdr *msg" to
"const struct msghdr *msg" to match the updated sendmsg() prototype
and resolve compilation errors due to parameter type inconsistency.

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-16 21:11:39 +08:00
guoshichao 57637e5927 net: make the sendmsg param type consistent with posix specification
To ensure consistency, in all places where the "sendmsg" function is used
either directly or indirectly, the type of the "struct msghdr *msg" parameter
needs to be modified to "const struct msghdr *msg".

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2026-01-16 21:11:39 +08:00
OceanfromXiaomi f53b986fb2 can: propagate iob_tryadd_queue() failure in can_datahandler
The can_datahandler() function calls iob_tryadd_queue() to enqueue
received CAN data. Since iob_tryadd_queue() may return a negative
value on failure, the error was previously not properly handled.

Update can_datahandler() to propagate the return value so callers
can detect and handle queueing failures correctly.

Signed-off-by: zhaohaiyang1 <zhaohaiyang1@xiaomi.com>
2026-01-07 08:31:06 +01:00
zhanghongyu 8f41613374 net: replace net_sem*wait with conn_dev_sem*wait to simplify code logic
optimize the current code format according to the previous net_xxx_wait
implementation to reduce multiple calls of similar code

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-01-02 07:48:06 -03:00
zhanghongyu c481374fb8 devif_poll: only call the corresponding xxx_poll when there is data to be sent
reduce the execution consumption of irrelevant code

testing the TX rates of TCP and UDP based on the Infineon board can
increase them by 13%.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-01-02 00:04:16 +08:00
zhanghongyu a0b847bf1b net/devif_callback: change flags type from uint16_t to uint32_t
increase the size of the flag to prepare for precise xxx_POLL.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-01-02 00:04:16 +08:00
zhanghongyu d377f6b788 net: use netdev_lock to protect netdev_txnotify_dev
since the type of sconn lock is rmutex, there is no need to release
sconn lock before TX

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-01-02 00:04:16 +08:00
dongjiuzhu1 b8585b9b0c net/can: fix poll setup to properly find and use free pollfd slots
Bug Description:
The original code always used conn->pollinfo[0] (the first element) to store
new poll setup context, regardless of whether that slot was already in use.
This caused multiple poll operations on the same CAN socket to overwrite each
other's context, leading to:
- Lost poll waiters when multiple threads poll the same socket
- Memory corruption in pollfd structures
- Undefined behavior when poll_teardown tries to clean up

Root Cause:
The code directly assigned `info = conn->pollinfo` without checking if the
slot was available, effectively always using pollinfo[0]. When a second
thread called poll() on the same socket, it would overwrite the first
thread's poll context.

Solution:
1. Initialize info to NULL instead of conn->pollinfo
2. Before setting up poll, iterate through all CONFIG_NET_CAN_NPOLLWAITERS
   slots to find the first free slot (where fds == NULL)
3. Return -EBUSY if no free slots are available
4. During teardown, properly mark the slot as free by setting fds = NULL

Additional Changes:
- Added CONFIG_NET_CAN_NPOLLWAITERS Kconfig option (default 4) to make the
  maximum number of concurrent poll waiters configurable
- Changed hardcoded array size from 4 to CONFIG_NET_CAN_NPOLLWAITERS
- Fixed lock ordering in teardown to ensure fds is cleared before unlock

Impact:
- Enables multiple threads to safely poll the same CAN socket concurrently
- Prevents poll context corruption in multi-threaded applications
- Provides proper resource management with -EBUSY when all slots are full
- Makes the number of supported concurrent pollers configurable per use case

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-31 02:35:42 +08:00
gaohedong 7e6138f258 net/ioctl: add a socket interface for ioctl
Add a socket family for ioctl.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 14:38:15 +08:00
zhanghongyu 0900b21217 net: change conn lock from mutex to rmutex
Build Documentation / build-html (push) Has been cancelled
UDP and PKT will hold the dev lock and conn lock when calling back the
protocol stack from the network dirver. At this time, if the poll_notify
notifies usrsock_server and executes poll_teardown, a conn double lock
will appear. so we changed the lock to a recursive lock

sched_dumpstack
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/Libs/libc/sched/sched_dumpstack.c:71
__assert
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/libs/libc/assert/lib_assert.c:49
nxsem_wait_slow
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/semaphore/sem_wait.c:102 (discriminator 1)
nxmutex_lock
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/include/nuttx/mutex.h:514
udp_pollteardown
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_netpoll.c:278
usrsock_rpmsg_poll_setup
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/usrsock/usrsock_rpmsg_server.c:1232
usrsock_rpmsg_poll_cb
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/usrsock/usrsock_rpmsg_server.c:1299
poll_notify
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/fs/vfs/fs_poll.c:296
udp_poll_eventhandler
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_netpoll.c:105
devif_conn_event
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/devif_callback.c:481
udp_callback
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_callback.c:324
udp_input_conn
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_input.c:150
ipv4_in
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/ipv4_input.c:425
ipv4_input
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/ipv4_input.c:533
tun_net_receive_tun
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/net/tun.c:574
tun_net_receive_tun
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/net/tun.c:574
file_writev_compat
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/fs/vfs/fs_write.c:89
rpmsgdev_write_handler
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/misc/rpmsgdev_server.c:354
rpmsg_virtio_process_rx_buffer
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/rpmsg/rpmsg_virtio.c:256
rpmsg_virtio_rx_worker
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/rpmsg/rpmsg_virtio.c:291
work_dispatch
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/wqueue/kwork_thread.c:233
work_thread
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/wqueue/kwork_thread.c:293
nxtask_start
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/task/task_start.c:99

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu c8713a1b48 net: protect the older network cards driver's tx and rx process
Add netdev_lock in xxx_input and txavail to adapt to drivers that do not
use upperhalf.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu 02e7ed7cc4 net/can: replace net_lock with conn_lock and conn_lock_dev
Protect can resources through netdev_lock, conn_lock, and can_list_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1 4fa74e4d18 net/can: add write_q free for callback alloc error handling
Release the corresponding resources held when the alloc fails

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1 99584588fc net/can: modify net_unlock location before txnotify for can_sendmsg_buffered.c
Release the lock early to reduce the number of thread switches.

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1 3bd4748bc3 net/can: repair callback alloc error handling
Release the corresponding resources held when the alloc fails

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1 a22f2a6123 net/socketcan: move rx filter down to can_input;
rx filter from recvmsg down to can_input, before packet delivery;

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu 3e025b5a03 net/can: can protocol uses a separate buffer to cache can data
To avoid memory waste, can add support for using an independent iob buffer
The IP protocol often configures CONFIG_IOB_BUFSIZE to be relatively large,
such as 512, for higher throughput. However, the buffer required by CAN is
fixed at a length of 16 or 64. If the system's IOB is directly used,
a lot of memory will be wasted.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-25 08:56:12 -03:00
zhanghongyu 908596dde3 net/bufpool: fix typo
A previous spelling mistake has been detected. fix it

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-25 08:33:13 -03:00
wangjinjing1 7b528d903c net/can: fix compile error
variable "conn" not define if CONFIG_NET_SEND_BUFSIZE > 0

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-25 10:04:42 +08:00
zhanghongyu 31e0ec8a39 net/can: fix the typo
Otherwise, we can only manually enable NET_WRITE_BUFFERS

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-25 10:04:42 +08:00
zhanghongyu aba8cd4d86 net/net_initialize: remove the empty functions that are not needed
streamline the code structure

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-25 09:50:40 +08:00
zhanghongyu 2b092adf48 net/can: fix possible IOB resource leak when can close
If there is data in readahead, it needs to be released; otherwise,
it will cause the leakage of IOB resources

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-15 10:19:15 +08:00
wangjinjing1 232ef950c6 net/can,udp,tcp: add sem destroy for udp/tcp sndsem;
standardize the usage of semphore

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-15 10:19:15 +08:00
wangjinjing1 8c92be874f net/can: add write buffer to support nonblock send
The sending of can socket can support more scenarios

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-15 10:19:15 +08:00
zhanghongyu 1c0a3a8d63 net/can: support can receive packets from all can devices
after the socket calls bind with ifindex is 0, it can receive packets
from all can devices

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-15 10:19:15 +08:00
zhanghongyu b7b9aebab9 net/can: return -ENODEV if the device is not found when bind
correction return value

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-15 10:19:15 +08:00
zhanghongyu e87082b195 net/utils/net_bufpool: add lock to struct net_bufpool_s and bufpool_navail
Add a lock to net_bufpool to simplify the protocol stack code.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-07 11:11:21 +08:00
Jukka Laitinen f4142626b9 net/can/can_recvmsg.c: Cleanup can_readahead
Build Documentation / build-html (push) Has been cancelled
It is better to remove the iob from the readahead queue first,
and only after that work on the iob.

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2025-08-26 20:42:01 +08:00
haitomatic 144ac23647 net/can/can_recvmsg.c: Remove IOB trimming as useless, can frames can always fit in one IOB
Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2025-08-26 20:42:01 +08:00
haitomatic 8fc2ad20a7 net/can/can_callback.c: Take the timestamp size into account in "buflen"
In case CONFIG_NET_TIMESTAMP is enabled, subtract the timestamp size from the buflen

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2025-08-26 20:42:01 +08:00
Lars Kruse 3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
Javier Casas 9c04b9ed4a net/can: Fix NULL dereference for I/O Block
In a recent patch the socketcan timestamp generation was moved
to add it also when there is an active reader (previously the timestamp
was only added before storing the CAN frame in the read-ahead list).

It was working fine but I realized that the can_callback function is
also called when sending frames. In this case, the IO block is not allocated
yet and the code breaks.

This PR is to only generate the timestamp if the IO block is not NULL
(reading path) and skip it when it is NULL (sending path)

Signed-off-by: Javier Casas <javiercasas@geotab.com>
2025-03-13 09:37:55 +01:00
Javier Casas 73f9ef00e2 net/can: fix timestamp
Fix timestamp in socket CAN. Right now the timestamp is only generated
if there is no reader and the frame is stored in the read ahead list.
This is solved by moving the timestamp generation before the code flow
branch.

Signed-off-by: Javier Casas <javiercasas@geotab.com>
2025-03-07 12:31:40 -03:00
Javier Casas 52dbfda355 bug fix: can_datahandler wrong return value
can_datahandler returns an unsigned value that represent the
number of bytes actually bufffered, but the call to iob_tryadd_queue
returns a negative value when it fails so that value can't be used as
can_datahandler return value because it is interpreted as unsigned so
a big number is wrongly returned when iob_tryadd_queue fails.

The fix is just setting the return value to zero when iob_tryadd_queue
fails, reflecting the actual meaning that no bytes have been buffered.
2025-01-30 11:37:29 -03:00
Javier Casas d52ff33e78 net/can/: add statistics for recv, sent and drop
Add support for network statistics for CAN.
It includes counters for receive, sent
and drop frames.

Signed-off-by: Javier Casas <javiercasas@geotab.com>
2025-01-30 11:37:29 -03:00
Zhe Weng 87a7714103 net: Split msg_iovlen check in recvmsg into each protocol
To prepare for supporting multiple iov in each protocol.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2025-01-20 17:12:14 +08:00
Zhe Weng 50b3ab7671 net/bufpool: Call init automatically on alloc
Note: Initialize function of protocols (tcp, udp, pkt, etc.) are kept empty.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2025-01-07 22:00:21 +08:00
Zhe Weng 1fe07d0838 net: Add buffer pool to replace connection allocation
Our net socket connection allocations are powerful but redundant
because they're implemented once in each protocol.  This is not good for
further optimizing and extending to other allocations, so maybe we can
add a common implementation for the usage.

Impact:
1. We add a `struct net_bufpool_s` as pool descriptor, which may use a
   little bit more memory than previous implementation (~28Bytes).
2. We share same functions between pools, so code size may shrink under
   some scenarios.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2024-12-23 16:57:19 -03:00
Jukka Laitinen ef827e88a7 net: Copy out also can cmsg data into the end of packet
This has been broken at some point. Just fix it by copying the can frame and
the cmsg data into IOB, and fix devif_poll to copy out the full data.

The can drivers expect to find the timeout timestamp in the end of the frame.

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2024-10-16 15:39:11 +08:00
Xiang Xiao 8ea51b3efc net/can: Save simple options to socket_conn_s
like other protocols(e.g. ip, tcp, udp)

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2024-10-13 11:24:31 +08:00
Xiang Xiao f76c2ed83b can: Add g_ prefix to can_dlc_to_len and len_to_can_dlc.
detail: Add g_ prefix to can_dlc_to_len and len_to_can_dlc to
follow NuttX coding style conventions for global symbols,
improving code readability and maintainability.

Signed-off-by: zhaohaiyang1 <zhaohaiyang1@xiaomi.com>
2024-09-18 23:50:11 +08:00
Alin Jerpelea 67d02a45eb net: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-09-12 01:08:11 +08:00
hujun5 e1b9144e71 can: Before we use pstate, we should check if it is NULL.
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-05 21:25:22 +08:00
Xiang Xiao fcb3e84c24 can: Merge netpacket/can.h into nuttx/can.h
To align with the layout of Linux can header file.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2024-08-26 05:05:31 -04:00
Petro Karashchenko d499ac9d58 nuttx: fix multiple 'FAR', 'CODE' and style issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
gaohedong dc651e090e net/can: Add SO_RCVBUF option for can socket
If the CAN stack receiving packets fast, but the application layer reading packets slow. Then `conn->readahead` will continue to grow, leading to memory leaks. Finally CAN stack potentially starve out all IOB buffers. To prevent memory leaks, users can restrict can socket buffer length.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2024-06-14 19:54:07 +08:00
zhanghongyu a9bff735e7 net/can: deliver data into multiple CAN conn bound to same dev
Because CAN is a broadcast protocol, each conn needs to be given independent data to avoid mutual interference.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2024-04-09 00:25:44 +08:00