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 5d886800a66be2b384cd2fe6d5a81c5612b3e756
Author: wangjinjing1 <[email protected]>
AuthorDate: Fri Nov 6 10:23:26 2026 +0800

    drivers/vhost/vhost-can: add static RX filters for the bridged socket
    
    The vhost CAN backend passes every frame of the bridged interface to the
    peer. Carry an optional CAN_RAW filter list in the NuttX-private device
    config after the ifname, offered through a private feature bit
    VIRTIO_CAN_F_RX_FILTER, and apply it to the bridge socket at probe. With
    no list, or one that cannot be applied, every frame is still received.
    
    Bit 4, VIRTIO_CAN_F_IFNAME, is reserved for a driver that takes its
    interface name from the same private config.
    
    Signed-off-by: wangjinjing1 <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/vhost/vhost-can.c         | 49 +++++++++++++++++++++++++++++++++++++++
 include/nuttx/virtio/virtio-can.h | 10 ++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/drivers/vhost/vhost-can.c b/drivers/vhost/vhost-can.c
index de154306c6d..9093f61b9f3 100644
--- a/drivers/vhost/vhost-can.c
+++ b/drivers/vhost/vhost-can.c
@@ -27,6 +27,7 @@
 #include <nuttx/config.h>
 
 #include <assert.h>
+#include <debug.h>
 #include <errno.h>
 #include <poll.h>
 #include <string.h>
@@ -831,6 +832,47 @@ static void vhost_can_rxkick(FAR struct virtqueue *vq)
   vhost_can_rxready(priv);
 }
 
+/****************************************************************************
+ * Name: vhost_can_setfilter
+ *
+ * Description:
+ *   Apply the RX filters of the device config to the bridge socket.  A list
+ *   that cannot be applied is logged and every frame is received.
+ *
+ ****************************************************************************/
+
+static void vhost_can_setfilter(FAR struct vhost_can_priv_s *priv)
+{
+  struct can_filter filters[CONFIG_NET_CAN_RAW_FILTER_MAX];
+  uint16_t num = 0;
+  int ret;
+
+  vhost_read_config_member(priv->hdev, struct vhost_can_config_s,
+                           filter_num, &num);
+  if (num == 0)
+    {
+      return;
+    }
+
+  if (num > nitems(filters))
+    {
+      vhosterr("%s: %u RX filters exceed %d, receiving all\n",
+               priv->dev->d_ifname, num, CONFIG_NET_CAN_RAW_FILTER_MAX);
+      return;
+    }
+
+  memset(filters, 0, sizeof(filters));
+  vhost_read_config(priv->hdev, offsetof(struct vhost_can_config_s, filters),
+                    filters, num * sizeof(filters[0]));
+  ret = psock_setsockopt(&priv->sock, SOL_CAN_RAW, CAN_RAW_FILTER,
+                         filters, num * sizeof(filters[0]));
+  if (ret < 0)
+    {
+      vhosterr("%s: set RX filters failed %d, receiving all\n",
+               priv->dev->d_ifname, ret);
+    }
+}
+
 /****************************************************************************
  * Name: vhost_can_probe
  ****************************************************************************/
@@ -843,6 +885,7 @@ static int vhost_can_probe(FAR struct vhost_device *hdev)
   char ifname[VIRTIO_CAN_IFNAME_SIZE];
   FAR struct net_driver_s *dev;
   struct sockaddr_can addr;
+  uint64_t features;
   int on = 1;
   int ret;
   int i;
@@ -892,6 +935,12 @@ static int vhost_can_probe(FAR struct vhost_device *hdev)
   psock_setsockopt(&priv->sock, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
                    &on, sizeof(on));
 
+  vhost_get_features(hdev, &features);
+  if (vhost_has_feature(hdev, VIRTIO_CAN_F_RX_FILTER))
+    {
+      vhost_can_setfilter(priv);
+    }
+
   memset(&addr, 0, sizeof(addr));
   addr.can_family  = AF_CAN;
   addr.can_ifindex = dev->d_ifindex;
diff --git a/include/nuttx/virtio/virtio-can.h 
b/include/nuttx/virtio/virtio-can.h
index ded3f629925..01337f06555 100644
--- a/include/nuttx/virtio/virtio-can.h
+++ b/include/nuttx/virtio/virtio-can.h
@@ -45,6 +45,8 @@
 #define VIRTIO_CAN_F_CAN_FD            1
 #define VIRTIO_CAN_F_RTR_FRAMES        2
 #define VIRTIO_CAN_F_LATE_TX_ACK       3
+#define VIRTIO_CAN_F_IFNAME            4
+#define VIRTIO_CAN_F_RX_FILTER         5
 
 #define VIRTIO_CAN_RESULT_OK           0
 #define VIRTIO_CAN_RESULT_NOT_OK       1
@@ -55,6 +57,7 @@
 #define VIRTIO_CAN_FLAGS_BRS           0x1000
 
 #define VIRTIO_CAN_IFNAME_SIZE         16
+#define VIRTIO_CAN_FILTER_MAX          32
 
 #define VIRTIO_CAN_TX                  0x0001
 #define VIRTIO_CAN_RX                  0x0101
@@ -79,14 +82,17 @@ begin_packed_struct struct virtio_can_config_s
 } end_packed_struct;
 
 /* NuttX-private device config: the OASIS config followed by the name of
- * the local CAN interface the vhost backend bridges to.  A standard driver
- * reads only the leading spec config.
+ * the local CAN interface the vhost backend bridges to, and the RX filters
+ * the backend applies to it. A standard driver reads only the leading
+ * spec config.
  */
 
 begin_packed_struct struct vhost_can_config_s
 {
   struct virtio_can_config_s config;
   char ifname[VIRTIO_CAN_IFNAME_SIZE];
+  uint16_t filter_num; /* Valid entries of filters */
+  struct can_filter filters[VIRTIO_CAN_FILTER_MAX];
 } end_packed_struct;
 
 begin_packed_struct struct virtio_can_tx_out_s

Reply via email to