casaroli commented on code in PR #20702:
URL: https://github.com/apache/nuttx/pull/20702#discussion_r4415757793


##########
net/inet/inet_sockif.c:
##########
@@ -1839,20 +1839,60 @@ static ssize_t inet_sendto(FAR struct socket *psock, 
FAR const void *buf,
 static ssize_t inet_sendmsg(FAR struct socket *psock,
                             FAR const struct msghdr *msg, int flags)
 {
-  FAR void *buf = msg->msg_iov->iov_base;
-  size_t len = msg->msg_iov->iov_len;
+  FAR void *buf;
+  size_t len;
   FAR const struct sockaddr *to = msg->msg_name;
   socklen_t tolen = msg->msg_namelen;
   FAR const struct iovec *iov;
   FAR const struct iovec *end;
   int ret;
 
+  if (msg->msg_iovlen == 0)
+    {
+      return -EINVAL;
+    }
+
+  buf = msg->msg_iov->iov_base;
+  len = msg->msg_iov->iov_len;
+
   if (msg->msg_iovlen == 1)
     {
       return to ? inet_sendto(psock, buf, len, flags, to, tolen) :
                   inet_send(psock, buf, len, flags);
     }
 
+  /* If this is a connected stream socket (TCP), we can send each iov chunk
+   * sequentially without allocating a temporary memory buffer.
+   */
+
+  if (psock->s_type == SOCK_STREAM && to == NULL)
+    {
+      ssize_t total = 0;
+
+      end = &msg->msg_iov[msg->msg_iovlen];
+      for (iov = msg->msg_iov; iov != end; iov++)
+        {
+          if (iov->iov_len == 0 || iov->iov_base == NULL)
+            {
+              continue;
+            }
+
+          ret = inet_send(psock, iov->iov_base, iov->iov_len, flags);

Review Comment:
   With CONFIG_NET_TCP_WRITE_BUFFERS=y each iov is still a separate 
inet_send(), and psock_tcp_send() takes the connection lock for each call. A 
send() from another thread can thus still put its data between the iovs of one 
sendmsg(); before this PR the whole message went into one psock_tcp_send().
   
   Fix: keep the gather path for TCP also with write buffers, or queue all iovs 
under one lock.
   



-- 
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