Commit Graph

617 Commits

Author SHA1 Message Date
Xiang Xiao
2c5f653bfd Remove the tail spaces from all files except Documentation
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-02-26 13:24:24 -08:00
ligd
c4ed55c6df socket: divide errno & s_error
Reference:
https: //man7.org/linux/man-pages/man2/connect.2.html

Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-02-23 16:40:19 +01:00
Fotis Panagiotopoulos
8f9dfe0be1 Improvements in TCP connections allocation. 2023-02-20 09:06:46 +08:00
Fotis Panagiotopoulos
85ceb7920e Typo fixes. 2023-02-17 11:17:11 -03:00
chao an
bd7e521871 net/sendfile: adapt sendfile() to support new driver model
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-14 11:21:09 +08:00
zhanghongyu
a2097cfb71 net: modify find device logic
The priorities for finding a network adapter are as follows:
1. if laddr is not ANY, use laddr to find device;
2. if laddr is ANY, and bound index is not 0, use bound index
   to find device;
3. if laddr is ANY and no device is bound, use raddr to find
   device.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-02-12 01:25:34 +08:00
Zhe Weng
d423992988 net/tcp: Regard snd_wnd update as ACKDATA
Receiving an ACK indicating TCP Window Update will not set ACKDATA flag (because tx_unacked is 0) in our TCP stack. Then this ACK won't let us send anything after receiving it, even if it updates snd_wnd. So we need to check whether we can send data immediately when our snd_wnd is updated (especially from 0), otherwise we will only send next data after timer expiry.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-02-03 13:43:39 +02:00
chao an
247b050e5f net/tcp: remove conn check since which can not be NULL
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-02 13:31:06 +08:00
chao an
4c8d244fae sched/getpid: replace syscall getpid/tid/ppid() to kernel version
NuttX kernel should not use the syscall functions, especially after
enabling CONFIG_SCHED_INSTRUMENTATION_SYSCALL, all system functions
will be traced to backend, which will impact system performance.

Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-02 10:33:01 +08:00
Zhe Weng
8819eeaf15 net: Implement shutdown() interface and tcp shutdown
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-01-31 11:15:01 +08:00
梁超众
b33474cb11 free TCP rx buffer immediately in tcp_close
Issue:
TCP rx buffer is freed after 4-way handshake with current design.
3 socket's rx buffer might be consumed during ffmpeg switch music procedure,
and this might cause IOB exhausted.

Solution:
free TCP rx buffer immediately in tcp_close to make sure IOB won't be
exhausted.

Signed-off-by: 梁超众 <liangchaozhong@xiaomi.com>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-30 11:25:36 +08:00
chao an
98e1f9c36d net/tcp: reuse common api to replace some ip select code
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-30 11:25:10 +08:00
zhanghongyu
3e4e416786 tcp/udp_connect: If the remote addr is ANY, change it to LOOPBACK
Behavior alignment to Linux

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-29 13:46:43 +08:00
chao an
85a8249821 Revert "socket:return -EAGAIN if timeout happends in psock_tcp_send"
This reverts commit fbe641a916.

This issue already fixed by below change:

| commit 715785245c
| Author: chao an <anchao@xiaomi.com>
| Date:   Mon Jan 16 12:37:44 2023 +0800
|
|     net/tcp: fix potential busy loop in tcp_send_buffered.c
|
|     if the wrbuffer does not have enough space to send the rest of
|     the data, then the send function will loop infinitely in nonblock
|     mode, add send timeout check to avoid this issue.
|
|     Signed-off-by: chao an <anchao@xiaomi.com>

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-29 13:46:01 +08:00
zhanghongyu
26286fd19e tcp: modify errno when connect raddr is ANY for ltp
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-29 13:45:49 +08:00
zhanghongyu
65e08b750c tcp_reset: check the conn is exist when tcp_reset
tcp_input:
If we didn't find an active connection that expected the packet,
If the SYN flag isn't set,
It is an old packet and we send a RST.
conn is NULL when this case

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-29 13:43:44 +08:00
梁超众
5012195bde support ipv4 ToS and ipv6 TrafficClass
Signed-off-by: 梁超众 <liangchaozhong@xiaomi.com>
2023-01-29 13:43:44 +08:00
zhanghongyu
86d602f734 tcp_netpoll: add assert into tcp_pollsetup when pollinfo invalid
In order to expose similar problems quickly in the future

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-28 23:27:14 +02:00
zhanghongyu
c1521b2ef2 tcp: add TCP_ACKDATA flag to close event callback
The close package cannot be sent in time without TCP_ACKDATA

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-28 09:02:42 +02:00
zhanghongyu
3725fe28fb tcp: fix trans data error when fast retrans
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2023-01-28 09:02:14 +02:00
liangchaozhong
fbe641a916 socket:return -EAGAIN if timeout happends in psock_tcp_send
psock_tcp_send will enter busyloop state when no IOB keeps at unavailable state
when the socket is blocking socket and this will cause WDT.
According to socket's manpage, -1 should be returned while errno set to EAGAIN
if send/recv timeout was set.

Here's the description:
SO_RCVTIMEO and SO_SNDTIMEO
Specify the receiving or sending timeouts until reporting an error. The argument is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as if the socket was specified to be nonblocking. If the timeout is set to zero (the default) then the operation will never timeout. Timeouts only have effect for system calls that perform socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no effect for select(2), poll(2), epoll_wait(2), and so on.

Signed-off-by: liangchaozhong <liangchaozhong@xiaomi.com>
2023-01-28 08:06:57 +02:00
chao an
64dd7e6376 net/tcp: add Selective-ACK support
Reference:
https://datatracker.ietf.org/doc/html/rfc2018

Iperf2 client/server test on esp32c3:

Drop(1/50):
CONFIG_NET_TCP_DEBUG_DROP_SEND=y
CONFIG_NET_TCP_DEBUG_DROP_SEND_PROBABILITY=50  // Drop probability: 1/50
CONFIG_NET_TCP_DEBUG_DROP_RECV=y
CONFIG_NET_TCP_DEBUG_DROP_RECV_PROBABILITY=50  // Drop probability: 1/50

Drop(1/50) + OFO/SACK:
CONFIG_NET_TCP_DEBUG_DROP_SEND=y
CONFIG_NET_TCP_DEBUG_DROP_SEND_PROBABILITY=50  // Drop probability: 1/50
CONFIG_NET_TCP_DEBUG_DROP_RECV=y
CONFIG_NET_TCP_DEBUG_DROP_RECV_PROBABILITY=50  // Drop probability: 1/50

CONFIG_NET_TCP_OUT_OF_ORDER=y
CONFIG_NET_TCP_SELECTIVE_ACK=y

---------------------------------------------------------
|  TCP Config            | Server | Client |            |
|-------------------------------------------------------|
|  Original              |   12   |     9  |  Mbits/sec |
|  Drop(1/50)            |  0.6   |   0.3  |  Mbits/sec |
|  Drop(1/50) + OFO/SACK |    8   |     8  |  Mbits/sec |
---------------------------------------------------------

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 16:24:09 +08:00
chao an
c581cc5f9b net/tcp: parse tcp options in common function
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 16:24:09 +08:00
chao an
d175f50f01 net/tcp: add out-of-order segment support
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 16:24:09 +08:00
chao an
0e7d397553 net/tcp: new api tcp_dataconcat() to concatenate/pack iob chain
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 16:24:09 +08:00
Zhe Weng
1cf3147626 net/netdev: Avoid hardcoded guardsize when using d_iob
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-01-18 14:41:07 +08:00
chao an
1f75d02bb5 net/tcp: correct behavior of SO_LINGER
1. Remove tcp_txdrain() from close() to avoid indefinitely block
2. Send TCP_RST immediately if linger timeout

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 02:47:12 +08:00
chao an
fa6ba05097 net/tcp: move drop send source code to correct place
Merge conflicts lead to code being placed in thre wrong place
The debug code should placed in tcp_send() not tcp_synack()

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 02:12:43 +08:00
chao an
715785245c net/tcp: fix potential busy loop in tcp_send_buffered.c
if the wrbuffer does not have enough space to send the rest of
the data, then the send function will loop infinitely in nonblock
mode, add send timeout check to avoid this issue.

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 02:11:33 +08:00
Xiang Xiao
a851ad84c3 net: consistent the net sem wait naming conversion
to prepare the new mutex wait function

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-15 12:31:30 -03:00
chao an
22348c890b net/tcp: debug feature to drop the tx/rx packet
Add 2 configurations
1. Config to drop recived packet
CONFIG_NET_TCP_DEBUG_DROP_RECV=y
CONFIG_NET_TCP_DEBUG_DROP_RECV_PROBABILITY=50 /* Default drop probability is 1/50 */

2. Config to drop sent packet
CONFIG_NET_TCP_DEBUG_DROP_SEND=y
CONFIG_NET_TCP_DEBUG_DROP_SEND_PROBABILITY=50 /* Default drop probability is 1/50 */

Iperf2 client/server test on esp32c3:

---------------------------------------------------------
|  TCP Config            | Server | Client |            |
|-------------------------------------------------------|
|  Original              |   12   |     9  |  Mbits/sec |
|  Drop(1/50)            |  0.6   |   0.3  |  Mbits/sec |
|  Drop(1/50) + OFO/SACK |    8   |     8  |  Mbits/sec |
---------------------------------------------------------

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-14 14:05:26 +08:00
chao an
8a63d29c6e net/devif_poll: optimize device buffer alloc in txpoll
Allocate the device buffer only if the protocol really need to send data.
not all protocols require the driver to prepare additional iob before
sending, especially UDP, each iob reserves l2/l3 header in advance
after prepare write buffer, net device could reuse this entry to send directly

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-13 16:41:10 +08:00
chao an
ffd81e63be net/tcp: reprepare response buffer from unthrottle pool
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-06 16:33:13 +08:00
chao an
3af47d1913 net/tcp: remove unnecessary clear for device buffer
which has been cleared in tcp_datahandler()

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-06 16:33:13 +08:00
chao an
1510dbee68 net/tcp: Do not trigger retransmission if the new data has not been consumed.
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-03 16:28:30 +08:00
chao an
6f15b32e34 net/tcp: rename NET_TCP_RECV_CONTIG to NET_TCP_RECV_PACK
Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-19 01:32:05 +08:00
chao an
794adc5814 net/tcp: contig received data to reducing iob consumption
Fragmentation of network data will intensify iob consumption, if
the device receives a message storm of fragmented packets, the iob
cache will not be effectively used, this is not allowed on iot devices
since the resources of such devices are limited. Of course, this
also takes some disadvantages: data needs to be copied.
This option will brings some balance on resource-constrained devices,
enable this config to reduce the consumption of iob, the received iob
buffers will be merged into the contiguous iob chain.

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-18 21:15:47 +08:00
Xiang Xiao
d5689e070b net/arp: Remove nuttx/net/arp.h
1.move ARPHRD_ETHER to netinet/arp.h
1.move arp_entry_s to net/arp/arp.h
2.move arp_input to nuttx/net/netdev.h

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-12-16 22:10:59 +02:00
chao an
11de08de27 net/devif_send: replace all block send to nonblock mode
Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-08 09:53:15 +01:00
chao an
62004a28a6 net/d_buf: remove d_buf reference from l3/l4
l3/l4 stack will decouple the reference of d_buf gradually, Only legacy
devices still retain d_buf support, new net devices will use d_iob

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-04 20:37:14 +08:00
chao an
34d2cde8a8 net/l2/l3/l4: add support of iob offload
1. Add new config CONFIG_NET_LL_GUARDSIZE to isolation of l2 stack,
   which will benefit l3(IP) layer for multi-MAC(l2) implementation,
   especially in some NICs such as celluler net driver.

new configuration options: CONFIG_NET_LL_GUARDSIZE

CONFIG_NET_LL_GUARDSIZE will reserved l2 buffer header size of
network buffer to isolate the L2/L3 (MAC/IP) data on network layer,
which will be beneficial to L3 network layer protocol transparent
transmission and forwarding

------------------------------------------------------------
Layout of frist iob entry:

        iob_data (aligned by CONFIG_IOB_ALIGNMENT)
            |
            |                  io_offset(CONFIG_NET_LL_GUARDSIZE)
            |                                |
            -------------------------------------------------
      iob   |            Reserved            |    io_len    |
            -------------------------------------------------

-------------------------------------------------------------
Layout of different NICs implementation:

        iob_data (aligned by CONFIG_IOB_ALIGNMENT)
            |
            |                 io_offset(CONFIG_NET_LL_GUARDSIZE)
            |                                |
            -------------------------------------------------
 Ethernet   |       Reserved    | ETH_HDRLEN |    io_len    |
            ---------------------------------|---------------
 8021Q      |   Reserved  | ETH_8021Q_HDRLEN |    io_len    |
            ---------------------------------|---------------
 ipforward  |            Reserved            |    io_len    |
            -------------------------------------------------

--------------------------------------------------------------------

2. Support iob offload to l2 driver to avoid unnecessary memory copy

Support send/receive iob vectors directly between the NICs and l3/l4
stack to avoid unnecessary memory copies, especially on hardware that
supports Scatter/gather, which can greatly improve performance.

new interface to support iob offload:

  ------------------------------------------
  |    IOB version     |     original      |
  |----------------------------------------|
  |  devif_iob_poll()  |   devif_poll()    |
  |       ...          |       ...         |
  ------------------------------------------

--------------------------------------------------------------------

1> NIC hardware support Scatter/gather transfer

TX:

                tcp_poll()/udp_poll()/pkt_poll()/...(l3|l4)
                           /              \
                          /                \
devif_poll_[l3|l4]_connections()     devif_iob_send() (nocopy:udp/icmp/...)
           /                                   \      (copy:tcp)
          /                                     \
  devif_iob_poll("NIC"_txpoll)                callback() // "NIC"_txpoll
                                                  |
                            dev->d_iob:           |
                                                ---------------         ---------------
                             io_data       iob1 |  |          |    iob3 |  |          |
                                    \           ---------------         ---------------
                                  ---------------  |       --------------- |
                             iob0 |  |          |  |  iob2 |  |          | |
                                  ---------------  |       --------------- |
                                     \             |          /           /
                                        \          |       /           /
                                   ----------------------------------------------
                    NICs io vector |    |    |    |    |    |    |    |    |    |
                                   ----------------------------------------------

RX:

  [tcp|udp|icmp|...]ipv[4|6]_data_handler()(iob_concat/append to readahead)
                    |
                    |
      [tcp|udp|icmp|...]_ipv[4|6]_in()/...
                    |
                    |
          pkt/ipv[4/6]_input()/...
                    |
                    |
     NICs io vector receive(iov_base to each iobs)

--------------------------------------------------------------------

2> CONFIG_IOB_BUFSIZE is greater than MTU:

TX:

"(CONFIG_IOB_BUFSIZE) > (MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE + CONFIG_NET_LL_GUARDSIZE)"

                tcp_poll()/udp_poll()/pkt_poll()/...(l3|l4)
                           /              \
                          /                \
devif_poll_[l3|l4]_connections()     devif_iob_send() (nocopy:udp/icmp/...)
           /                                   \      (copy:tcp)
          /                                     \
  devif_iob_poll("NIC"_txpoll)                callback() // "NIC"_txpoll
                                                  |
                                             "NIC"_send()
                          (dev->d_iob->io_data[CONFIG_NET_LL_GUARDSIZE - NET_LL_HDRLEN(dev)])

RX:

  [tcp|udp|icmp|...]ipv[4|6]_data_handler()(iob_concat/append to readahead)
                    |
                    |
      [tcp|udp|icmp|...]_ipv[4|6]_in()/...
                    |
                    |
          pkt/ipv[4/6]_input()/...
                    |
                    |
     NICs io vector receive(iov_base to io_data)

--------------------------------------------------------------------

3> Compatible with all old flat buffer NICs

TX:
                tcp_poll()/udp_poll()/pkt_poll()/...(l3|l4)
                           /              \
                          /                \
devif_poll_[l3|l4]_connections()     devif_iob_send() (nocopy:udp/icmp/...)
           /                                   \      (copy:tcp)
          /                                     \
  devif_iob_poll(devif_poll_callback())  devif_poll_callback() /* new interface, gather iobs to flat buffer */
       /                                           \
      /                                             \
 devif_poll("NIC"_txpoll)                     "NIC"_send()(dev->d_buf)

RX:

  [tcp|udp|icmp|...]ipv[4|6]_data_handler()(iob_concat/append to readahead)
                    |
                    |
      [tcp|udp|icmp|...]_ipv[4|6]_in()/...
                    |
                    |
               netdev_input()  /* new interface, Scatter/gather flat/iob buffer */
                    |
                    |
          pkt/ipv[4|6]_input()/...
                    |
                    |
    NICs io vector receive(Orignal flat buffer)

3. Iperf passthrough on NuttX simulator:

  -------------------------------------------------
  |  Protocol      | Server | Client |            |
  |-----------------------------------------------|
  |  TCP           |  813   |   834  |  Mbits/sec |
  |  TCP(Offload)  | 1720   |  1100  |  Mbits/sec |
  |  UDP           |   22   |   757  |  Mbits/sec |
  |  UDP(Offload)  |   25   |  1250  |  Mbits/sec |
  -------------------------------------------------

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-03 11:47:04 +08:00
liyi
391b501639 net: extract l3 header build code into new functions
Signed-off-by: liyi <liyi25@xiaomi.com>
2022-11-29 18:36:15 +08:00
chao an
d54a20b393 net/tcp/udp: remove all domain assertion
fix build break if enable CONFIG_NET_IPv6 only

In file included from tcp/tcp_sendfile.c:38:
tcp/tcp_sendfile.c: In function ‘sendfile_eventhandler’:
tcp/tcp_sendfile.c:173:27: error: ‘struct tcp_conn_s’ has no member named ‘domain’
  173 |           DEBUGASSERT(conn->domain == PF_INET6);
      |                           ^~

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-29 17:28:36 +08:00
chao an
3caa551ff4 net: remove psock reference from connect
Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-24 22:57:42 +08:00
chao an
f2a7711ef8 net/soerr: add new _SO_CONN_SETERRNO() macro
support so error code set from conn instance

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-24 22:57:42 +08:00
liangchaozhong
0ec4b0a149 tcp: recv returns 0 without set errno
Issue:
recv return 0  means peer side has already closed the connection according to
man page's description.
0 is returned without set errno when TCP rx timeout happens with current
design.

Solution:
return result instead of ir_result when ir_result is 0, then -1 will be
returned with errno set to EAGAIN for tcp rx buffer empty case.

Signed-off-by: liangchaozhong <liangchaozhong@xiaomi.com>
2022-11-23 23:23:44 +08:00
Xiang Xiao
0d516c7ff6 net/tcp: Remove the dependence on SCHED_WORKQUEUE
since it isn't required in case of NET_TCP_NO_STACK

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-11-22 20:45:26 +09:00
chao an
ae4b5b5f50 net/tcp: TCP_WAITALL should use state flag
The feature of MSG_WAITALL is broken since the wrong flag is used

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-21 17:21:10 +08:00
anjiahao
d07792a343 Initialize global mutext/sem by NXMUTEX_INITIALIZER and SEM_INITIALIZER
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-11-14 09:34:04 +09:00
Alan Carvalho de Assis
8286a558c8 net/tcp: Avoid starting TCP sequence number 0 2022-11-13 09:09:36 +08:00