CV-Bowen opened a new pull request, #22045:
URL: https://github.com/apache/nuttx/pull/22045

   ## Summary
   
   Fixes and one new option for rpmsg sockets (`net/rpmsg/rpmsg_sockif.c`),
   continuing #21403:
   
   - `add CONFIG_NET_RPMSG_EPT_PRIORITY`: default endpoint priority of all
     rpmsg sockets, range 0..254 like `SO_PRIORITY`.
   - `refuse a connection when no server is listening`: a stream client
     connecting to a name nobody listens on waited for a listener (with the
     default timeout, forever).  One rpmsg callback registered at
     `net_initialize()` now catches the announcements of all stream clients
     and answers with `NS_DESTROY` when no listening socket matches, so
     `connect()` fails at once.  Datagram announcements still wait in the
     rpmsg bind list for the other end.
   - `send the refusing NS_DESTROY with RPMSG_ADDR_ANY`: so that a Linux
     peer, which compares the address with a still unset `rpdev->dst`,
     matches it.
   - `report a refused nonblocking connect in poll`: the refusal could
     arrive before `poll()` was set up and was then never reported.
   - `fail a blocking connect to an unreachable CPU at once`: with no rpmsg
     device to the target CPU, a blocking `connect()` waited forever; it
     now returns `ECONNREFUSED`.  Nonblocking connect is unchanged.
   - `free the unaccepted connections when a listener closes`: connections
     that completed the handshake but were never accepted leaked.
   - `detach the endpoint when the connection outlives its device`: a
     socket still open when its rpmsg device was destroyed kept a dangling
     `ept.rdev`, and `close()` crashed in freed memory.
   - `do not send the window update / the shutdown / the SYNC to an unbound
     peer`: these called the blocking `rpmsg_send()` after checking the
     endpoint without a lock; a `NS_DESTROY` in between made it wait the
     whole `RPMSG_TICK_COUNT` (15 s), in the SYNC case inside the rx
     callback, stalling every inbound message.  Reserve the tx buffer, then
     check and send with `rpmsg_send_offchannel_nocopy()` under `recvlock`.
   - `make conn->unbind atomic`, `clear a stale unbind flag when SYNC
     arrives`, `allocate the socket name suffix atomically`, `name the
     rejected connection in the backlog log`.
   
   ## Impact
   
   - Behaviour:
     - a stream `connect()` to a name nobody listens on fails (blocking:
       `ECONNRESET`; nonblocking: `poll()` reports `POLLERR | POLLHUP`)
       instead of waiting for a server.  A client that relied on connecting
       before the server listens now has to retry.
     - a blocking `connect()` to a CPU without an rpmsg device returns
       `ECONNREFUSED` instead of waiting for the CPU to come up.
   - Kconfig: new `NET_RPMSG_EPT_PRIORITY` (default 127, the previous
     value).
   - No API change.  `net/rpmsg/rpmsg.h` gains `rpmsg_socket_initialize()`,
     called from `net_initialize()`.
   
   ## Testing
   
   Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 
13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU 
Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205
   
   sim:rpserver + sim:rpproxy, unchanged configs (both enable
   `NET_RPMSG` and `EXAMPLES_RPMSGSOCKET`).  Commands on the proxy console
   (reached with `cu /dev/ttyproxy`) and the server console.  The refusal
   cases use a test-only probe (below, not in this PR) because
   `rpsock_client` busy-loops on `send()` errors once poll returns.
   
   | case | master | this PR |
   |---|---|---|
   | `p111probe block nosuch server` (nobody listens on `nosuch`) | blocks, no 
return | `connect` fails with 104 (`ECONNRESET`) after 9 ms |
   | `p111probe nonblock nosuch2 server`, then `poll(POLLOUT, 5 s)` | 
`EINPROGRESS`, poll times out, revents 0 | `EINPROGRESS`, poll returns revents 
0x18 (`POLLERR \| POLLHUP`) |
   | `p111probe block nosuch3 nocpu` (no such CPU) | blocks, no return | 
`connect` fails with 111 (`ECONNREFUSED`) at once |
   | `rpsock_server stream block test1` / `rpsock_client stream block test1 
server` | pass | pass |
   | `rpsock_server dgram block test2 proxy` / `rpsock_client dgram block test2 
server` | pass | pass |
   
   qemu-armv8a:rpserver + qemu-armv8a:rpproxy (ivshmem and rpmsg_port_uart
   channels, built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, see
   below), same cases: identical results to sim on master and with this PR
   (blocking probe: no return vs 104; nonblocking poll: revents 0 vs 0x18;
   unknown CPU: no return vs 111; stream and dgram pass on both).
   
   <details><summary>this PR, sim</summary>
   
   ```
   proxy> p111probe block nosuch server &
   PROBE connect ret -1 errno 104 after 9 ms
   proxy> p111probe nonblock nosuch2 server &
   PROBE connect ret -1 errno 115 after 0 ms
   PROBE poll ret 1 revents 0x18
   proxy> p111probe block nosuch3 nocpu &
   PROBE connect ret -1 errno 111 after 0 ms
   ```
   </details>
   
   <details><summary>master, sim</summary>
   
   ```
   proxy> p111probe block nosuch server &
   proxy> p111probe nonblock nosuch2 server &
   PROBE connect ret -1 errno 115 after 0 ms
   PROBE poll ret 0 revents 0x0
   proxy> p111probe block nosuch3 nocpu &
   ```
   (the two blocking probes never return)
   </details>
   
   <details><summary>test-only probe</summary>
   
   ```c
   /* Test only: probe rpmsg socket connect behaviour.
    * p111probe <block|nonblock> <name> <cpu>
    */
   
   #include <nuttx/config.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <poll.h>
   #include <stdio.h>
   #include <string.h>
   #include <sys/socket.h>
   #include <time.h>
   #include <unistd.h>
   #include <netpacket/rpmsg.h>
   
   int main(int argc, FAR char *argv[])
   {
     struct sockaddr_rpmsg addr;
     struct timespec t0;
     struct timespec t1;
     struct pollfd pfd;
     int nonblock = strcmp(argv[1], "nonblock") == 0;
     int fd;
     int ret;
   
     fd = socket(PF_RPMSG, SOCK_STREAM | (nonblock ? SOCK_NONBLOCK : 0), 0);
     memset(&addr, 0, sizeof(addr));
     addr.rp_family = AF_RPMSG;
     strlcpy(addr.rp_name, argv[2], sizeof(addr.rp_name));
     strlcpy(addr.rp_cpu, argv[3], sizeof(addr.rp_cpu));
   
     clock_gettime(CLOCK_MONOTONIC, &t0);
     ret = connect(fd, (FAR struct sockaddr *)&addr, sizeof(addr));
     clock_gettime(CLOCK_MONOTONIC, &t1);
     printf("PROBE connect ret %d errno %d after %ld ms\n", ret,
            ret < 0 ? errno : 0,
            (long)((t1.tv_sec - t0.tv_sec) * 1000 +
                   (t1.tv_nsec - t0.tv_nsec) / 1000000));
   
     if (ret < 0 && errno == EINPROGRESS)
       {
         memset(&pfd, 0, sizeof(pfd));
         pfd.fd = fd;
         pfd.events = POLLOUT;
         ret = poll(&pfd, 1, 5000);
         printf("PROBE poll ret %d revents 0x%x\n", ret, pfd.revents);
       }
   
     close(fd);
     printf("PROBE done\n");
     return 0;
   }
   ```
   </details>
   
   Every commit builds sim:rpproxy, sim:rpserver and qemu-armv8a:rpproxy
   without warnings and the sim pair boots; qemu-armv7a:rpproxy builds at
   the last commit.
   
   Not tested: the races fixed by the reserve-then-send changes, the stale
   unbind flag and the atomic name suffix (no deterministic reproduction);
   a listener closed with unaccepted connections; a socket outliving its
   rpmsg device; a Linux peer.
   
   No regression in rpmsg: sim:rpserver/rpproxy and 
qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, 
rpmsgfs; 25/25). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC 
disabled, which current master needs for nxinit to start nsh there; unrelated 
to this change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to