This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 6b50d0c8de4af19a9db2d89d45b79a5045fa03ee Author: zhanghongyu <[email protected]> AuthorDate: Tue Nov 10 19:19:46 2026 +0800 net/local: send a DGRAM datagram as one atomic frame pathlen, pktlen, path and payload went out as four FIFO writes, so a reader woken by POLLIN could consume the preamble and hit -EAGAIN on a payload not yet written. No threshold fixes this: the receiver cannot know pathlen. local_send_frame() submits the whole frame as one atomic file_writev(), and the size check now covers the whole frame rather than the payload alone. The whole frame, preamble plus path plus payload, is measured against the peer's lc_rcvsize before anything is written. The sender FIFO is put into PIPEIOC_ATOMICWRITE mode so the frame lands all-or-nothing, and the POLLIN threshold workaround goes away. Signed-off-by: zhanghongyu <[email protected]> --- net/local/local.h | 28 +++++----- net/local/local_fifo.c | 30 +++++++---- net/local/local_sendmsg.c | 19 ++----- net/local/local_sendpacket.c | 120 +++++++++++++++++++++++++++++-------------- 4 files changed, 118 insertions(+), 79 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index 94280886ae0..4f98da73e3c 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -492,28 +492,30 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR const struct msghdr *msg, int flags); /**************************************************************************** - * Name: local_send_preamble + * Name: local_send_frame * * Description: - * Send a packet on the write-only FIFO. + * Send one datagram on the write-only FIFO as a single atomic writev, so + * that the reader is never woken on a preamble whose payload has not been + * written yet. * * Input Parameters: - * conn A reference to local connection structure - * filep File structure of write-only FIFO. - * buf Data to send - * len Length of data to send - * preamble preamble Flag to indicate the preamble sync header assembly + * conn A reference to local connection structure + * buf Payload to send + * len Number of payload iovec segments + * rcvsize Receive buffer size the peer advertised, preamble room + * included * * Returned Value: - * Packet length is returned on success; a negated errno value is returned - * on any failure. + * Payload length is returned on success; a negated errno value is + * returned on any failure. -EMSGSIZE means the whole frame, preamble + * included, does not fit in rcvsize. * ****************************************************************************/ -int local_send_preamble(FAR struct local_conn_s *conn, - FAR struct file *filep, - FAR const struct iovec *buf, - size_t len, size_t rcvsize); +ssize_t local_send_frame(FAR struct local_conn_s *conn, + FAR const struct iovec *buf, size_t len, + size_t rcvsize); /**************************************************************************** * Name: local_send_packet diff --git a/net/local/local_fifo.c b/net/local/local_fifo.c index 5f331667612..c69c3349590 100644 --- a/net/local/local_fifo.c +++ b/net/local/local_fifo.c @@ -39,6 +39,9 @@ #include <assert.h> #include <nuttx/debug.h> +#include <nuttx/fs/fs.h> +#include <nuttx/fs/ioctl.h> + #include "local/local.h" /**************************************************************************** @@ -425,7 +428,7 @@ int local_set_pollthreshold(FAR struct local_conn_s *conn, ret = local_set_pollinthreshold(&conn->lc_infile, threshold); if (ret > 0) { - ret = local_set_polloutthreshold(&conn->lc_outfile, threshold); + ret = local_set_polloutthreshold(&conn->lc_outfile, threshold); } return ret; @@ -699,16 +702,6 @@ int local_open_receiver(FAR struct local_conn_s *conn, bool nonblock) /* Policy: Free FIFO resources when the buffer is empty. */ ret = local_set_policy(&conn->lc_infile, 1); - if (ret == 0) - { - /* Set POLLIN threshold bigger than preamble len. - * This is to avoid non-blocking read failed with -EAGAIN when - * only preamble len is sent and read by reader. - */ - - ret = local_set_pollinthreshold(&conn->lc_infile, - 2 * sizeof(lc_size_t)); - } } return ret; @@ -752,6 +745,21 @@ int local_open_sender(FAR struct local_conn_s *conn, FAR const char *path, ret = local_set_polloutthreshold(&conn->lc_outfile, 2 * sizeof(lc_size_t)); } + + if (ret == 0) + { + /* Require the FIFO to publish each writev all-or-nothing. A + * datagram is a preamble plus a payload, so a reader woken + * between the two would consume a header whose payload does not + * exist yet and the FIFO would lose framing from then on. + */ + + ret = file_ioctl(&conn->lc_outfile, PIPEIOC_ATOMICWRITE, 1); + if (ret < 0) + { + nerr("ERROR: Failed to set FIFO atomic write mode: %d\n", ret); + } + } } return ret; diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index 0211ac642a4..acbb6971127 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -295,27 +295,14 @@ static ssize_t local_sendto(FAR struct socket *psock, goto errout_with_halfduplex; } - /* Send the preamble */ + /* Send preamble and payload as one atomic frame */ - ret = local_send_preamble(conn, &conn->lc_outfile, buf, len, - server->lc_rcvsize); + ret = local_send_frame(conn, buf, len, server->lc_rcvsize); if (ret < 0) { - nerr("ERROR: Failed to send the preamble: %zd\n", ret); - goto errout_with_sender; + nerr("ERROR: Failed to send the frame: %zd\n", ret); } - /* Send the packet */ - - ret = local_send_packet(&conn->lc_outfile, buf, len); - if (ret < 0) - { - nerr("ERROR: Failed to send the packet: %zd\n", ret); - goto errout_with_sender; - } - -errout_with_sender: - /* Now we can close the write-only socket descriptor */ file_close(&conn->lc_outfile); diff --git a/net/local/local_sendpacket.c b/net/local/local_sendpacket.c index 3b49810423d..6b8a4c85d3b 100644 --- a/net/local/local_sendpacket.c +++ b/net/local/local_sendpacket.c @@ -28,6 +28,7 @@ #include <sys/types.h> #include <stdint.h> +#include <string.h> #include <unistd.h> #include <errno.h> #include <assert.h> @@ -38,6 +39,17 @@ #include "local/local.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Preamble and path segments plus this many payload segments are gathered + * on the stack. A datagram never needs more than this, which the sender + * asserts before it builds the frame. + */ + +#define LOCAL_FRAME_NIOV 8 + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -97,72 +109,102 @@ static ssize_t local_fifo_write(FAR struct file *filep, ****************************************************************************/ /**************************************************************************** - * Name: local_send_preamble + * Name: local_send_frame * * Description: - * Send a packet on the write-only FIFO. + * Send one datagram on the write-only FIFO as a single atomic writev, so + * that the reader is never woken on a preamble whose payload has not been + * written yet. * * Input Parameters: - * conn A reference to local connection structure - * filep File structure of write-only FIFO. - * buf Data to send - * len Length of data to send + * conn A reference to local connection structure + * buf Payload to send + * len Number of payload iovec segments + * rcvsize Receive buffer size the peer advertised, preamble room + * included * * Returned Value: - * Packet length is returned on success; a negated errno value is returned - * on any failure. + * Payload length is returned on success; a negated errno value is + * returned on any failure. -EMSGSIZE means the whole frame, preamble + * included, does not fit in rcvsize. * ****************************************************************************/ -int local_send_preamble(FAR struct local_conn_s *conn, - FAR struct file *filep, - FAR const struct iovec *buf, - size_t len, size_t rcvsize) +ssize_t local_send_frame(FAR struct local_conn_s *conn, + FAR const struct iovec *buf, size_t len, + size_t rcvsize) { - FAR const struct iovec *end = buf + len; - FAR const struct iovec *iov; - ssize_t ret; + struct iovec stackiov[LOCAL_FRAME_NIOV]; + lc_size_t preamble[2]; lc_size_t pathlen; - lc_size_t pktlen; - size_t total = 0; + size_t niov = len + 2; + size_t pktlen; + ssize_t ret; + size_t i; - if (rcvsize <= sizeof(lc_size_t)) + /* The frame is the preamble plus the path plus every payload segment. + * The on-stack iovec holds at most LOCAL_FRAME_NIOV segments, two of + * which are reserved for the preamble and the path. len comes straight + * from the caller's msg_iovlen (bounded only by IOV_MAX), so reject a + * request that would not fit instead of overrunning stackiov. + */ + + if (niov > LOCAL_FRAME_NIOV) { + nerr("ERROR: Too many iovec segments: %zu > %d\n", + niov, LOCAL_FRAME_NIOV); return -EMSGSIZE; } - /* Send the packet length */ - - for (iov = buf; iov != end; iov++) + for (pktlen = 0, i = 0; i < len; i++) { - if (iov->iov_len > rcvsize - sizeof(lc_size_t) - total) - { - nerr("ERROR: Packet is too big\n"); - return -EMSGSIZE; - } - - total += iov->iov_len; + pktlen += buf[i].iov_len; } pathlen = strlen(conn->lc_path); - ret = local_fifo_write(&conn->lc_outfile, (FAR const uint8_t *)&pathlen, - sizeof(lc_size_t)); - if (ret != sizeof(lc_size_t)) + + /* Hold the frame to the capacity the receiver advertised. The driver + * refuses only what its FIFO can never hold, which is a different limit: + * the FIFO may have been created or resized for another rcvsize. + */ + + if (pktlen + sizeof(preamble) + pathlen > rcvsize) { - nerr("ERROR: local send path length failed ret: %zd\n", ret); - return ret; + nerr("ERROR: Frame is too big: %zu > %zu\n", + pktlen + sizeof(preamble) + (size_t)pathlen, rcvsize); + return -EMSGSIZE; } - pktlen = total; - ret = local_fifo_write(filep, (FAR const uint8_t *)&pktlen, - sizeof(lc_size_t)); - if (ret != sizeof(lc_size_t)) + /* Wire format is [pathlen][pktlen][path][payload]; the receiver parses + * the two lengths before it knows how much of either follows. + */ + + preamble[0] = pathlen; + preamble[1] = pktlen; + + stackiov[0].iov_base = preamble; + stackiov[0].iov_len = sizeof(preamble); + stackiov[1].iov_base = conn->lc_path; + stackiov[1].iov_len = pathlen; + memcpy(&stackiov[2], buf, len * sizeof(struct iovec)); + + /* The FIFO is in atomic mode, so this either publishes the whole frame + * or writes nothing at all. + */ + + ret = file_writev(&conn->lc_outfile, stackiov, niov); + + if (ret < 0) { + if (ret != -EAGAIN) + { + nerr("ERROR: local send frame failed ret: %zd\n", ret); + } + return ret; } - return local_fifo_write(&conn->lc_outfile, (uint8_t *)conn->lc_path, - pathlen); + return pktlen; } /****************************************************************************
