Commit graph

752 commits

Author SHA1 Message Date
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
wenquan1
d2887ef15f net/tcp: fix send RST bug when receive syn retransmit in syn-rcvd state
Fix send RST bug when receive syn retransmit in syn-rcvd state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
476e193812 net/tcp: ignore fin packet in syn-sent state
Ignore fin packet in syn-sent state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
e3c2ab8267 net/tcp: drop the packet with neither SYN or RST flag set in syn-sent state
Drop the packet with neither SYN or RST flag set in syn-sent state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
b09b5dfc9c net/tcp: ignore a RST control message in syn-send state
Ignore a RST control message in syn-send state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
12de7e6dd5 net/tcp: ignore a segment carrying an unacceptable ACK and RST in syn-send state
Ignore a segment carrying an unacceptable ACK and RST in syn-send state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
62f8c400b6 net/tcp: send a reset if the SYN is in the window buf error
According rfc793 p71, after step1 check OTW seq, and step2 check RST flag, then check syn flag.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
c4494c714c net/tcp: support OTW SEQ number check except SYN-SEND state
According rfc793 p69, from state SYN-RECEIVED to state TIME-WAIT, if the incoming segment is not acceptable, an acknowledgment should be sent in reply (unless the RST bit is set):
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
758b6dd847 net/tcp: support OTW SEQ number check in SYN-RCVD state
According rfc793 p69, In SYN-RCVD state, if the incoming segment is not acceptable, an acknowledgment should be sent in reply (unless the RST bit is set):
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
ee5009295c tcp_reset: Modify RST packet responses to comply with RFC 793.
According to RFC793, the flags of TCP reply packet should be as follows:
1. `RST` if flags of request packet has `ACK`
2. `RST|ACK` if flags of request packet has no `ACK`, at the same time, the sequence should be set to zero.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:44:38 +08:00
wenquan1
c0fa2cae3d net/tcp: fix conn->work use after free in worker queue list
in multi-core cpu, tcp_free and tcp_timer_expiry->tcp_timer will work in parallel, after tcp_free call work_cancle, tcp->timer will call tcp_update_timer to re-add work to worker queue, then tcp_free free conn, in this condition, it will result use after free.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:42:49 +08:00
wenquan1
cc0cc552fc net/tcp: discard fin packet in LISTEN state
Discard fin packet in LISTEN state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:42:23 +08:00
wenquan1
fb7dcc3d89 net/tcp: fix error check of OTW ACK sequence of zero-window probe ack
Due to illegal sndseq_max increase when send zero-window probe or keealive probe

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
wenquan1
74ab7816a1 net/tcp: send a reset if ack is not acceptable in syn-received state
In SYN-RCVD state, if the ACK is not acceptable, send a reset.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
wenquan1
8ead446ee4 net/tcp: support OTW check and response with an empty acknowledgment segment
According RFC793,p72~p73 In states from ESTABLISHED to LASTACK:"If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an ACK, drop the segment, and return"

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
liqinhui
facb9d4e65 net/tcp: Initialize the sndseq_max.
Initialize conn->sndseq_max when tcp conn alloc

Signed-off-by: liqinhui <liqinhui@xiaomi.com>
2025-12-29 22:39:56 +08:00
zhanghongyu
a91c50243e net/tcp: send reset when retransmitted number greater than TCP_MAXRTX
in the current process, if tcpstate is at TCP_FIN_WAIT_1, TCP_CLOSING,
and TCP_LAST_ACK, and the peer does not reply TCP_ACK, it will be
retransmitted permanently. to avoid continuous occupation of resources,
now when the retransmission equals TCP_MAXRTX, a reset message will be
sent and the conn resources will be reclaimed in tcp_close_work.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 22:38:25 +08:00
wenquan1
11ea1de01d net/tcp: fix tcp conn lock leak when port in use in tcp_selectport
Fix tcp conn lock leak when port in use in tcp_selectport.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 10:33:18 -03:00
zhanghongyu
6addf24fa2 net/tcp: replace TCP_CLOSE with TCP_ABORT
since we added TCP_TXCLOSE and TCP_RXCLOSE, only RST-like exception
flows now trigger TCP_CLOSE events, so replace the remaining
TCP_CLOSE with TCP_ABORT.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
fa47ab7bd2 net/tcp: add TCP_RXCLOSE to handle rx is shutdown
notify the local waiting recv process; otherwise, after the peer TX
is closed, the wait of the recv process will continue to wait.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
2ad01e2aab net/tcp: support recv packet in the TCP_FIN_WAIT_1/2 state
shutdown should send TCP_FIN packet.

close should send TCP_RST packet when the data in readahead has not been
                                 read and NEW_DATA has arrived.
             send TCP_FIN packet when in other cases.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
0588d08d75 net/tcp: remove SYN_RCVD state conn by listener created when free listener
after the listener fd is closed, the half-open connections associated
with the listener are not reclaimed. According to the protocol standard,
they should be reclaimed in a timely manner.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:20:01 +08:00
zhanghongyu
93cba3da64 net/tcp: add tcp_conn_cmp to compare tcp connection
encapsulate similar code into a function to simplify code logic.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:20:01 +08:00
zhanghongyu
0900b21217 net: change conn lock from mutex to rmutex
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
5032377c34 net/tcp: replace net_lock with conn_lock and conn_lock_dev
Protect tcp resources through netdev_lock, conn_lock, and tcp_list_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangchen
6196550f8f tcp:add net_lock to protect tcp resource
add net_lock in the tcp_callback_cleanup & Set the devif_callback_s of
tcp_callback_s to a two-dimensional pointer.

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
0af74a54bb net/netdev: separate netdev_list_lock from net_lock
use netdev_list_lock to protect all network card traversal, registration,
and deregistration operations.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
19dcd452ff net/tcp: add kconfig to support retransmission at a fixed time
the maximum retransmission interval allowed for car projects
cannot exceed 6 seconds, and allows for fixed retransmission intervals.
according to the previous algorithm, the retransmission interval
may be 0.5 * 2 ^ 4 = 8 seconds, which does not meet the requirements.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-27 21:35:00 +08:00
zhanghongyu
746d68916f net/tcp: add support for the CLOSE_WAIT state
CLOSE-WAIT - represents waiting for a connection termination request
             from the local user.
      TCP A                                                TCP B

  1.  ESTABLISHED                                          ESTABLISHED
  2.  (Close)
      FIN-WAIT-1  --> <SEQ=100><ACK=300><CTL=FIN,ACK>  --> CLOSE-WAIT
  3.  FIN-WAIT-2  <-- <SEQ=300><ACK=101><CTL=ACK>      <-- CLOSE-WAIT
  4.                                                       (Close)
      TIME-WAIT   <-- <SEQ=300><ACK=101><CTL=FIN,ACK>  <-- LAST-ACK
  5.  TIME-WAIT   --> <SEQ=101><ACK=301><CTL=ACK>      --> CLOSED
  6.  (2 MSL)
      CLOSED

in the current state, we can continue to send data until the user
calls shutdown or close, then directly enter the TCP_LAST_ACK state

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-27 21:06:23 +08:00
wangchen
c5bb75441c net/tcp: modify the return value when dev is down in tcp_pollsetup
when dev is down, tcp_pollsetup returns the value 'OK' and sets the connection status to 'down' and eventset to POLLERR & POLLUP

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-25 10:06:49 +08:00
wangchen
f10fc888fe net/tcp:resolve reconnection issues when connection failed due to rejection
After first connection refused happend,The user use the same connection failed,becasue the tcpstateflags is not TCP_ALLOCATED

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-25 09:56:09 +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
wangchen
6cf4c95517 tcp_input: Add condition to check sndseq and ackseq equality
When triggering TCP retransmission through a timer, conn->unacked_q will save more packets, which may result in sndseq being equal to ackseq. Previous implementations may cause conn->nrtx to not be cleared, leading to an increase in conn->rto errors and a longer timer time

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-22 21:14:53 +08:00
wangchen
eae67ef9a5 tcp: Reset wb_nack to zero when it hits TCP fast retransmit threshold
The modification is to solve the problem that the opposite end cannot receive the retransmission message, and the protocol stack no longer sends retransmission messages

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-22 21:14:53 +08:00
zhanghongyu
5c61672185 net/tcp/tcp_connect.c: move txnotify after setup_callback
otherwise, when tx is modified to direct mode, wait will not be awakened

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-20 10:58:16 +08:00
zhanghongyu
9ce35caf4e net/tcp: fix the problem of nested tx in the rxcb process
since the current txnotify is executed synchronously at the location of
the current call without switching threads, resources such as d_iob need
to be cached first.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-20 10:58:16 +08:00
zhanghongyu
9869181761 tcp.h: add TCP_CORK definition
TCP_CORK and TCP_NODELAY behave almost exactly the opposite,
so first provide simple support for TCP_CORK.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-18 15:38:17 +08:00
zhangshuai39
560aed72a4 net: Check listening connections when selecting ports
Added check for port number on listen queue

Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
2025-12-17 01:30:11 +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
wangjianyu3
c1aa625bdb net: enable tcp backlog by default
If the NET_TCPBACKLOG configuration is not enabled, no I/O event will be
triggered for the socket file descriptor in poll/epoll. As a result, the
connection will not be accepted, leading to a failure in the TCP handshake.

The problem is that users may not have noticed this configuration,
no event will be received when polling the socket fd.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-06-26 09:32:54 -03:00
Matteo Golin
ab756e8dd7 net/tcp: Fix keep-alive implementation
The TCP keep-alive implementation would send the probes and abort the
connection correctly, but without waking up the affected socket and
marking the connection as aborted. This patch from zhhyu7 in #16447
resolves the issue.

Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
2025-05-28 10:18:17 -03:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
nuttxs
da74a5e368 net/tcp/tcp_conn.c: optimize the port conflict detection rules
for tcp_listener(). fixed potential conflict between implicit
binding and port conflict detection.

Implicit binding automatically allocates an interface address during
connection,but port conflict detection occurs before binding/connecting.
This can cause issues when creating multiple TCP connections in succession.
If the first connection uses a random port via implicit binding, subsequent
connections might reuse the same port under current filtering rules,
leading to connect() failures.

Signed-off-by: nuttxs <zhaoqing.zhang@sony.com>
2025-04-24 14:10:27 +08:00
chenrun1
99a1f0dc65 inet_sockif:support multiple iov recv
Summary:
  Implement multiple iovlen recvmsg in inet_sockif, only TCP mode
  support at current.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2025-01-20 17:12:14 +08:00
zhanghongyu
70489fe56b net/tcp_timer: remove tcp_callback(TIMEOUT) when accept conn timeout
when accept conn timeout, only accept conn itself should handle the event
(send TCP_RESET), and there is no need to notify the listening conn.
otherwise, the server's poll will return with POLLERR and POLLHUP.
this may cause some applications to execute incorrect logic in the future.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-01-10 02:04:37 +08:00
Huang Qi
cfd0cc0d1a Remove duplicate includes across multiple files
This commit cleans up redundant header file includes throughout the codebase.
 The changes include:

 - Removing duplicate #include directives that were present in the same file
 - Consolidating includes that were split across multiple lines unnecessarily
 - Removing unused includes that were no longer needed
 - Fixing some formatting issues with includes

 The changes improve code organization and maintainability by:
 - Reducing unnecessary dependencies
 - Making include dependencies more explicit
 - Following consistent include patterns
 - Removing dead code

 No functional changes are made - this is purely a code cleanup commit.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2025-01-09 23:30:23 +08:00
Zhe Weng
3e62be8361 net: Enable dynamic allocation of tcp/udp/ipfwd buffer by default
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2025-01-07 22:00:21 +08:00