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 49da3c04b6fa5a0fdde08f3a844b9448fc0e8b93
Author: wangjinjing1 <[email protected]>
AuthorDate: Wed Nov 4 22:18:39 2026 +0800

    drivers/vhost/vhost-can: decouple START/STOP from the bridged netdev
    
    START returns early when the bridged interface is already up, so the
    socket poll is never registered and RX stalls. Register the poll at
    probe and let START/STOP only bring the shared interface up and down
    under netdev_lock; remove no longer takes the interface down.
    
    Signed-off-by: wangjinjing1 <[email protected]>
---
 drivers/vhost/vhost-can.c | 67 +++++++++++++++++++++--------------------------
 1 file changed, 30 insertions(+), 37 deletions(-)

diff --git a/drivers/vhost/vhost-can.c b/drivers/vhost/vhost-can.c
index e93a1ce7123..de154306c6d 100644
--- a/drivers/vhost/vhost-can.c
+++ b/drivers/vhost/vhost-can.c
@@ -552,41 +552,18 @@ static void vhost_can_txq(FAR void *arg)
  * Name: vhost_can_start
  *
  * Description:
- *   Start the controller: drop stale frames queued while stopped,
- *   register the socket poll that drives RX, and bring the bridged
- *   interface up.
+ *   Start the controller: bring the bridged interface up.  The interface
+ *   is shared with local users, so it may already be up.
  *
  ****************************************************************************/
 
 static int vhost_can_start(FAR struct vhost_can_priv_s *priv)
 {
-  struct canfd_frame frame;
   int ret;
 
-  if (IFF_IS_UP(priv->dev->d_flags))
-    {
-      return OK;
-    }
-
-  /* A stopped controller receives nothing. */
-
-  while (psock_recvfrom(&priv->sock, &frame, sizeof(frame),
-                        MSG_DONTWAIT, NULL, NULL) > 0)
-    {
-    }
-
-  ret = psock_poll(&priv->sock, &priv->pfd, true);
-  if (ret < 0)
-    {
-      return ret;
-    }
-
+  netdev_lock(priv->dev);
   ret = netdev_ifup(priv->dev);
-  if (ret < 0)
-    {
-      psock_poll(&priv->sock, &priv->pfd, false);
-    }
-
+  netdev_unlock(priv->dev);
   return ret;
 }
 
@@ -594,15 +571,18 @@ static int vhost_can_start(FAR struct vhost_can_priv_s 
*priv)
  * Name: vhost_can_stop
  *
  * Description:
- *   Stop the controller: unregister the socket poll and bring the
- *   bridged interface down.
+ *   Stop the controller: bring the bridged interface down.
  *
  ****************************************************************************/
 
-static void vhost_can_stop(FAR struct vhost_can_priv_s *priv)
+static int vhost_can_stop(FAR struct vhost_can_priv_s *priv)
 {
-  psock_poll(&priv->sock, &priv->pfd, false);
-  netdev_ifdown(priv->dev);
+  int ret;
+
+  netdev_lock(priv->dev);
+  ret = netdev_ifdown(priv->dev);
+  netdev_unlock(priv->dev);
+  return ret;
 }
 
 /****************************************************************************
@@ -650,8 +630,10 @@ static void vhost_can_ctlq(FAR void *arg)
                     break;
 
                   case VIRTIO_CAN_SET_CTRL_MODE_STOP:
-                    vhost_can_stop(priv);
-                    in->result = VIRTIO_CAN_RESULT_OK;
+                    if (vhost_can_stop(priv) >= 0)
+                      {
+                        in->result = VIRTIO_CAN_RESULT_OK;
+                      }
                     break;
 
                   default:
@@ -926,8 +908,8 @@ static int vhost_can_probe(FAR struct vhost_device *hdev)
   callback[VHOST_CAN_RXQ]  = vhost_can_rxkick;
   callback[VHOST_CAN_CTLQ] = vhost_can_ctlkick;
 
-  /* The socket poll callback registered on controller start drives RX;
-   * there is no RX thread.
+  /* The socket poll callback registered at probe drives RX; there is no
+   * RX thread.
    */
 
   priv->pfd.events = POLLIN;
@@ -941,6 +923,17 @@ static int vhost_can_probe(FAR struct vhost_device *hdev)
       goto err_close;
     }
 
+  /* Registered once, independent of the bridged interface state; a frame
+   * already queued fires the callback, so the RX queue must exist.
+   */
+
+  ret = psock_poll(&priv->sock, &priv->pfd, true);
+  if (ret < 0)
+    {
+      vhost_can_remove(hdev);
+      return ret;
+    }
+
   /* The drain arms the queue callbacks and answers requests the guest
    * queued before the device was probed.
    */
@@ -967,7 +960,7 @@ static void vhost_can_remove(FAR struct vhost_device *hdev)
   virtqueue_disable_cb(hdev->vrings_info[VHOST_CAN_TXQ].vq);
   virtqueue_disable_cb(hdev->vrings_info[VHOST_CAN_RXQ].vq);
   virtqueue_disable_cb(hdev->vrings_info[VHOST_CAN_CTLQ].vq);
-  vhost_can_stop(priv);
+  psock_poll(&priv->sock, &priv->pfd, false);
   work_cancel_sync(HPWORK, &priv->txwork);
   work_cancel_sync(HPWORK, &priv->ctlwork);
   work_cancel_sync(HPWORK, &priv->rxwork);

Reply via email to