Commit Graph

286 Commits

Author SHA1 Message Date
Zhe Weng 0e018308a3 net/pkt: Add readahead queue for pkt, call input for tx on sim
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-03-08 23:09:25 +02:00
chao an fcefd16586 net/devif: fix null pointer reference found out by coverity
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-21 00:44:07 +08:00
Fotis Panagiotopoulos 9b4d784307 Improvements in sockets 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
chao an e3a6c16944 net/ip: fix compile break if disable NET_TCP
1.
ipfrag/ipv4_frag.c: In function ‘ipv4_fragin’:
ipfrag/ipv4_frag.c:184:22: warning: ‘head’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  184 |   ipv4->len[1] = head->io_pktlen & 0xff;
      |                  ~~~~^~~~~~~~~~~
ipfrag/ipv4_frag.c:123:21: note: ‘head’ was declared here
  123 |   FAR struct iob_s *head;
      |                     ^~~~

2.
devif/ipv6_input.c: In function ‘ipv6_in’:
devif/ipv6_input.c:60:33: error: ‘TCPIPv6BUF’ undeclared (first use in this function); did you mean ‘UDPIPv6BUF’?
   60 | #define PAYLOAD ((FAR uint8_t *)TCPIPv6BUF)
      |                                 ^~~~~~~~~~

3.
nat/ipv4_nat.c: In function ‘ipv4_nat_inbound_icmp’:
nat/ipv4_nat.c:67:30: error: ‘TCP_HDRLEN’ undeclared (first use in this function); did you mean ‘UDP_HDRLEN’?
   67 |   ((proto) == IP_PROTO_TCP ? TCP_HDRLEN : \
      |                              ^~~~~~~~~~
nat/ipv4_nat.c:323:47: note: in expansion of macro ‘L4_HDRLEN’
  323 |             inner_l4hdrlen = MIN(inner_l4len, L4_HDRLEN(inner->proto));
      |                                               ^~~~~~~~~

Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-07 12:46:40 +02:00
chao an 30aa0ee700 net/devif: correct the judgment condition in devif_send()
Regression by:

| commit 7fce145b30
| Author: chao an <anchao@xiaomi.com>
| Date:   Mon Jan 30 21:36:39 2023 +0800
|
|     net/devif: check the net device before use
|
|     Signed-off-by: chao an <anchao@xiaomi.com>

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-31 22:42:58 +08:00
chao an 7fce145b30 net/devif: check the net device before use
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-31 18:41:50 +08:00
Zhe Weng be89bcc044 mm/iob: Support negative offset when copyin/out.
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-01-31 00:39:15 +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 9ae7119a39 net/devif: bypass send length check if ip fragment enabled
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-18 02:12:02 +08:00
luojun1234 ff3733b5b5 Support fragmentation and reassembly
Signed-off-by: luojun1 <luojun1@xiaomi.com>
2023-01-17 14:01:37 +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 0cbbbb9215 net/devif: reuse devif_send() for can/pkt/icmp stack
Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-13 16:41:10 +08:00
Zhe Weng 34ade7a0b6 net/devif: fix devif_poll loop logic
Previously, the devif_poll works in this flow:
devif_poll_xx -> bstop = callback -> continue if !bstop -> poll another

Now we split the polling and callback, so it should work like:
devif_poll_connections -> return true if callback need to be called
   -> bstop = callback -> loop if !bstop -> poll again

Conditions:
poll_connections == 0, d_len == 0, break, return 0
poll_connections == 1, d_len == 0, break, return 1 (other stop case,
                                                    don't callback)
poll_connections == 1, d_len > 0, callback == 1, break, return 1
poll_connections == 1, d_len > 0, callback == 0, loop

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-30 21:25:59 +08:00
Zhe Weng 7f9cf184e5 net/devif_loopback: Add robustness to avoid infinite loop
When ipv4_input/ipv6_input called by devif_loopback writes wrong data into buffer (another bug we're fixing), the else block does nothing but only record the 'dropped' statistic, then infinite loop happens.

Refers to previous lo device with dropping logic:
https://github.com/apache/nuttx/blob/releases/11.0/drivers/net/loopback.c#L178-L180

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-24 16:20:17 +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 5492e961bc net/devif: replace redundant code to iob_clone_partial()
Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-14 21:37:20 +08:00
Zhe Weng 2f3e237380 net/ipfwd: limit number of ipforward struct to be less than number of IOB
If ipforward consumes all the IOB, devif_poll will not poll any more. Without polling, ipforward will not release any IOB, then all the network stack may fail with no IOB available.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-09 11:20:16 +08: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 d295b4e1f1 net/devif: Use the device ll header length to calculate send limit
Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-08 03:16:42 +08:00
chao an 960b4bb2a7 net/devif: initialize d_len to 0 before polling connections
Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-08 03:16:42 +08:00
Zhe Weng 2a780e0467 net/ipfwd: fix `devif_forward` after IOB offload.
Problem:
- `iob_copyout` to `d_buf` doesn't set `io_len` of the IOB, so `devif_poll` failed to copy the data into `buf` by `iob_copyout`

Modification:
- Just Move the IOB in `devif_forward`.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-06 17:03:47 +08:00
chao an e002edf87c net/devif: reprepare iob buffer before polling connections
Loopback device will consume the d_iob packet in upper layer protocol,
in order to avoid null pointer access, reprepare iob buffer before polling connections

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-05 17:32:07 +08:00
chao an 8fc1e524ab net/ip: return success if the packet was forwarded.
The incoming packet should not be input to ip layer if the packet has been forwarded

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-04 15:59:43 -03:00
Xiang Xiao 43bda3282f net/arp: Move arp_ipin and arp_out to private header file
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-12-04 20:39:21 +08: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 82d67b201a net/offload: add offload support for pkt/arp
1. add offload support for pkt/arp
2. Reset the d_buf to NULL if d_iob has released

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-04 01:34:30 +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
Zhe Weng da6ab79d65 net/ipv4_input: Set IPv4 flag at the same place as ipv6_input
Set IPv4 flag before processing ipforward, otherwise the ICMP packet responded by ipforward may sometimes be regarded as IPv6.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-02 15:26:45 +08:00
Zhe Weng 3aac6a1a3b net/devif: fix devif loopback
devif_loopback needs to be applied on l2 data, and was broken by commit below, now fix it.

| commit 8850dee746
| Author: chao an <anchao@xiaomi.com>
| Date:   Sun Nov 27 03:31:07 2022 +0800
|
|     net/devif: move preprocess of txpoll into common code
|
|     Signed-off-by: chao an <anchao@xiaomi.com>

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-12-01 19:48:23 +08:00
chao an a0bce18192 net/ipv4: fix shadows declaration
devif/ipv4_input.c: In function ‘ipv4_in’:
devif/ipv4_input.c:305:15: warning: declaration of ‘ret’ shadows a previous local [-Wshadow]
  305 |           int ret = ipv4_forward(dev, ipv4);
      |               ^~~
devif/ipv4_input.c:151:7: note: shadowed declaration is here
  151 |   int ret = OK;
      |       ^~~

Signed-off-by: chao an <anchao@xiaomi.com>
2022-12-01 09:38:33 +08:00
chao an 0de9e387a7 net/loopback: reuse devif_loopback() logic for device lo(loopback)
TX poll callback in device lo(loopback) can be replaced by devif_loopback()
from devif_poll() hook, remove duplicate code to reuse this logic

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-30 10:07:14 +08:00
chao an 823252261f net/devif: remove link layer length from loopback
Add link layer length in loopback is unnecessary after below change checkin:

 | commit 6fa60627eb
 | Author: chao an <anchao@xiaomi.com>
 | Date:   Sun Nov 27 02:13:21 2022 +0800
 |
 |     net/devif/ip: build l2 header on the IP layer
 |
 |     Signed-off-by: chao an <anchao@xiaomi.com>

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-30 10:07:14 +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 615845d9a6 net/devif: add common interface to build L2 headers
So we can hide the implementation details of L2 NICs.

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-28 14:44:15 +01:00
Xiang Xiao 4663987e66 Fix devif/ipv4_input.c:405:1: warning: label ‘done’ defined but not used [-Wunused-label]
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-27 18:01:38 +08:00
chao an 6fa60627eb net/devif/ip: build l2 header on the IP layer
Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-27 12:13:45 +08:00
chao an 8850dee746 net/devif: move preprocess of txpoll into common code
Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-27 12:11:12 +08:00
chao an db88554d7d net/devif: add common funtion to build L2 headers before sending
Generic callback before device output to build L2 headers before sending

Signed-off-by: chao an <anchao@xiaomi.com>
2022-11-27 12:11:12 +08:00
Zhe Weng b595430578 net: add basic NAT workflow
Add basic functions for NAT (NAPT), remaining some logic unimplemented (UDP, ICMP, port assignment, etc). NAT for TCP can work now (unless port conflicts).
Outbound: LAN ->  Forward  ->  NAT(only if targeting at WAN)  -> WAN
Inbound:  WAN ->  NAT(only from WAN, change dest) -> Forward  -> LAN

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2022-11-11 14:36:55 +08:00
chao an a8d3286258 net: move device buffer define to common header
Signed-off-by: chao an <anchao@xiaomi.com>
2022-10-28 00:32:16 -04:00
Fotis Panagiotopoulos 143d1322ea Added handling of MSG_WAITALL flag in TCP recv. 2022-10-13 18:22:05 +08:00
chao.an 162fcd10ca net: cleanup pvconn reference to avoid confuse
More reference:
https://github.com/apache/incubator-nuttx/pull/5252
https://github.com/apache/incubator-nuttx/pull/5434

Signed-off-by: chao.an <anchao@xiaomi.com>
2022-08-26 20:58:11 +08:00
chao.an 9cb17841d8 net/sockopt: move BINDTODEVICE to socket level
rename the UDP_BINDTODEVICE to SO_BINDTODEVICE to follow the linux
style to be compatible with non-UDP protocol binding requirements

Signed-off-by: chao.an <anchao@xiaomi.com>
2022-08-25 17:56:52 +08:00
Sebastien Lorquet 6bd36d64e3 Packet dropped in IPv4/v6 input is now an info, not a warning 2022-06-09 16:07:34 +02:00
zhanghongyu 035d925864 devif: remove all devif_timer
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2022-06-02 20:11:50 -03:00
zhanghongyu 3f8b71924f tcp: move wd_timer from wifi driver to tcp stack
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2022-05-28 16:29:51 +08:00
zhanghongyu 931a64717a fix:Temporarily remove udp send large pkt assert
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2022-04-02 10:40:22 +08:00
chao.an 61fa5d70f5 devif/ipv6_input.c: fix compile warning
devif/ipv6_input.c: In function ‘ipv6_input’:
devif/ipv6_input.c:344:32: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
  344 |       if ((ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff00)) &&
      |                                ^
devif/ipv6_input.c:345:32: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
  345 |           (ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff01)) &&
      |                                ^
devif/ipv6_input.c:346:32: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
  346 |           (ipv6->destipaddr[0] & HTONS(0xff0f) != HTONS(0xff02)))
      |                                ^

Signed-off-by: chao.an <anchao@xiaomi.com>
2022-02-18 16:05:20 +08:00