Skip to contentSkip to conversation

net/filter: Add AF_PACKET support for vhost-net

RFC Superseded Cindy Lu 2 participants 11 messages Mar 30 – Apr 1 Networking

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v3 cover net/filter: Add AF_PACKET support for vhost-net

Hi, All

This series adds AF_PACKET-based capture and injection datapath support for the vhost TAP device in filter

Example Usage(not change with exist upstream code)
=============
Primary VM (mirror incoming packets to secondary via chardev socket):

-netdev tap,id=net0,vhost=on,...
-chardev socket,id=mirror0,host=...,port=...,server=on,wait=off -object filter-redirector,id=vm1redir,netdev=net0,outdev=mirror0...

Secondary VM (receive mirrored packets):

  -netdev tap,id=net0,vhost=on,...
  -chardev socket,id=red0,host=...,port=...,reconnect-ms=..
  -object filter-buffer,id=swbuf,netdev=net0,queue=tx,interval=1000000,status=off.....
  -object filter-redirector,id=r1,netdev=net0,queue=tx,indev=red0,status=off,enable_when
  _stopped=true.... \

changset
===========
change in v2:
1. add support for filter-buffer
2. remove the in_netdev and out_netdev for AF_PACKET bind port, now only use netdev when the vhost=on start use AF_PACKET to capture and inject, when use vhost=off will use the existing code
3. add CAP_NET_RAW check
4. address the comment

change in v3:
1. reuse the exist Capture/inject process

Testing
=======
- Tested with vhost=on/off TAP device on x86_64

known issue:
============
1. compilation should be disabled on non-Linux platforms.will fix in next version.
2. Sometimes the secondary VM receives duplicate packets. will fix this in next version.

Cindy Lu (7):
net/filter-mirror: keep mirroring while stopped
virtio-net: keep tap read polling disabled while vhost owns RX net/filter-redirector: add AF_PACKET netdev setup helpers net/filter-redirector: add AF_PACKET capture path net/filter-redirector: add AF_PACKET inject path net/filter: allow filters on vhost TAP backends net/filter-redirector: check CAP_NET_RAW before creating AF_PACKET

 hw/net/virtio-net.c  |  12 +-
 include/net/filter.h |   6 +
 include/net/net.h    |   6 +
 net/filter-mirror.c  | 379 ++++++++++++++++++++++++++++++++++++++++++-
 net/filter.c         |  17 +-
 net/net.c            |  10 +-
 6 files changed, 417 insertions(+), 13 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

RFC v3 1/7 net/filter-mirror: keep mirroring while stopped

During migration pre-switchover the VM runstate is stopped, which can cause tap read_poll to be disabled (qemu_send_packet_async() returns 0) and incoming tap frames stop reaching the redirector/mirror filter.

Re-enable tap read_poll when swredir is turned on while stopped, and allow the redirector outdev path to keep draining packets while stopped by adding a per-NetClientState allow_send_when_stopped bit used by qemu_can_send_packet().

---
 include/net/net.h   |  6 ++++++
 net/filter-mirror.c | 49 +++++++++++++++++++++++++++++++++++++--------
 net/net.c           | 10 ++++++++-
 3 files changed, 56 insertions(+), 9 deletions(-)
include/net/net.h +6 -0
index d2b2e6fc44..bde052acbb 100644
@@ -126,6 +126,12 @@ struct NetClientState {
     char *name;
     char info_str[256];
     unsigned receive_disabled : 1;
+    /*
+     * Allow this sender to keep draining packets while VM is stopped.
+     * Used by COLO-style pre-switchover mirroring, where netfilter needs to
+     * see incoming tap frames even when runstate is not running.
+     */
+    unsigned allow_send_when_stopped:1;
     NetClientDestructor *destructor;
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
net/filter-mirror.c +41 -8

83 lines not on this page · raw message

net/net.c +9 -1
index b15f4db65e..06f54eebb4 100644
@@ -632,7 +632,7 @@ int qemu_can_send_packet(NetClientState *sender)
 {
     int vm_running = runstate_is_running();
 
-    if (!vm_running) {
+    if (!vm_running && !sender->allow_send_when_stopped) {
         return 0;
     }
 
@@ -640,6 +640,14 @@ int qemu_can_send_packet(NetClientState *sender)
         return 1;
     }
 
+    /*
+     * When VM is stopped (e.g. migration pre-switchover), allow draining tap
+     * packets so netfilters (redirector/mirror) can operate.
+     */
+    if (!vm_running && sender->allow_send_when_stopped) {
+        return 1;
+    }
+
     return qemu_can_receive_packet(sender->peer);
 }
 
Signature · 1 line
2.52.0
author
#2 lore raw

RFC v3 2/7 virtio-net: keep tap read polling disabled while vhost owns RX

In reply to Cindy Lu

virtio_net_backend_read_poll_set() re-enables TAP read polling on vmstart even when kernel vhost has already taken over RX.

That lets QEMU userspace and vhost race on the same tap fd and can corrupt the restored virtqueue state during migration switchover.

Keep read_poll disabled for TAP backends with a started vhost_net, while leaving pure userspace backends unchanged.

---
 hw/net/virtio-net.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +11 -1
index d6d2188863..616590fb82 100644
@@ -1322,9 +1322,19 @@ static void virtio_net_backend_read_poll_set(VirtIONet *n, bool enable)
     for (i = 0; i < (int)n->max_ncs; i++) {
         NetClientState *frontend = qemu_get_subqueue(n->nic, i);
         NetClientState *backend = frontend ? frontend->peer : NULL;
+        bool backend_enable = enable;
 
         if (backend && backend->info && backend->info->read_poll) {
-            backend->info->read_poll(backend, enable);
+            /*
+             * When vhost is active, the kernel backend owns the tap RX path.
+             * Re-enabling QEMU read_poll on vmstart makes userspace and vhost
+             * race on the same tap fd, which can corrupt the restored RX ring
+             * during migration switchover replay.
+             */
+            if (enable && get_vhost_net(backend) && n->vhost_started) {
+                backend_enable = false;
+            }
+            backend->info->read_poll(backend, backend_enable);
         }
     }
 }
Signature · 1 line
2.52.0
author
#3 lore raw

RFC v3 3/7 net/filter-redirector: add AF_PACKET netdev setup helpers

In reply to Cindy Lu

Prepare filter-redirector for direct TAP access through AF_PACKET sockets.

Add the setup-side plumbing needed by later capture/inject changes:
- extend MirrorState with dedicated AF_PACKET fds and a capture buffer
- resolve the TAP ifname from the backend fd
- create and bind a nonblocking AF_PACKET socket on that interface
- store the socket as the capture or inject endpoint based on the redirector role
- clean up the AF_PACKET resources from cleanup/finalize paths

This commit only prepares the sockets and object state. The actual capture and inject datapaths are added by follow-up commits.

---
 net/filter-mirror.c | 91 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
net/filter-mirror.c +91 -0
index ab711e8835..d9f6a11d6b 100644
@@ -22,10 +22,18 @@
 #include "qemu/error-report.h"
 #include "trace.h"
 #include "chardev/char-fe.h"
+#include "net/vhost_net.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
 #include "block/aio-wait.h"
 #include "system/runstate.h"
+#include "net/tap.h"
+#include "net/tap_int.h"
+
+#include <sys/socket.h>
+#include <net/if.h>
+#include <linux/if_packet.h>
+#include <netinet/if_ether.h>
 
 typedef struct MirrorState MirrorState;
 DECLARE_INSTANCE_CHECKER(MirrorState, FILTER_MIRROR,
@@ -40,6 +48,9 @@ struct MirrorState {
     NetFilterState parent_obj;
     char *indev;
     char *outdev;
+    int in_netfd;
+    uint8_t *in_netbuf;
+    int out_netfd;
     CharFrontend chr_in;
     CharFrontend chr_out;
     SocketReadState rs;
@@ -267,6 +278,15 @@ static void filter_redirector_cleanup(NetFilterState *nf)
     qemu_chr_fe_deinit(&s->chr_in, false);
     qemu_chr_fe_deinit(&s->chr_out, false);
     qemu_del_vm_change_state_handler(s->vmsentry);
+    if (s->in_netfd >= 0) {
+        qemu_set_fd_handler(s->in_netfd, NULL, NULL, NULL);
+        close(s->in_netfd);
+        s->in_netfd = -1;
+    }
+    if (s->out_netfd >= 0) {
+        close(s->out_netfd);
+        s->out_netfd = -1;
+    }
 
     if (nf->netdev) {
         nf->netdev->allow_send_when_stopped = 0;
@@ -360,6 +380,70 @@ static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf)
     }
 }
 
+static bool filter_redirector_netdev_setup(NetFilterState *nf, Error **errp)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+    struct sockaddr_ll sll = { 0 };
+    char ifname[IFNAMSIZ] = { 0 };
+    bool capture_role;
+    bool inject_role;
+    int ifindex;
+    int fd;
+    NetClientState *nc = nf->netdev;
+    int tapfd;
+
+    if (!nc || nc->info->type != NET_CLIENT_DRIVER_TAP) {
+        return true;
+    }
+
+    capture_role = s->outdev && !s->indev && get_vhost_net(nc);
+    inject_role = s->indev && !s->outdev && get_vhost_net(nc);
+    if (!capture_role && !inject_role) {
+        return true;
+    }
+
+    tapfd = tap_get_fd(nc);
+    if (tapfd < 0 || tap_fd_get_ifname(tapfd, ifname) != 0) {
+        error_setg(errp, "failed to resolve TAP ifname for netdev '%s'",
+                   nf->netdev_id);
+        return false;
+    }
+
+    ifindex = if_nametoindex(ifname);
+    if (!ifindex) {
+        error_setg_errno(errp, errno,
+                         "failed to resolve ifindex for '%s'", ifname);
+        return false;
+    }
+
+    fd = qemu_socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ALL));
+    if (fd < 0) {
+        error_setg_errno(errp, errno, "failed to create AF_PACKET socket");
+        return false;
+    }
+
+    sll.sll_family = AF_PACKET;
+    sll.sll_ifindex = ifindex;
+    sll.sll_protocol = htons(ETH_P_ALL);
+    if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
+        error_setg_errno(errp, errno,
+                         "failed to bind AF_PACKET socket for ifname '%s'",
+                         ifname);
+        close(fd);
+        return false;
+    }
+
+    if (capture_role) {
+        s->in_netfd = fd;
+        g_free(s->in_netbuf);
+        s->in_netbuf = g_malloc(REDIRECTOR_MAX_LEN);
+    } else {
+        s->out_netfd = fd;
+    }
+
+    return true;
+}
+
 static void filter_redirector_setup(NetFilterState *nf, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(nf);
@@ -410,6 +494,10 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
         }
     }
 
+    if (!filter_redirector_netdev_setup(nf, errp)) {
+        return;
+    }
+
     s->vmsentry = qemu_add_vm_change_state_handler(
         filter_redirector_vm_state_change, nf);
 
@@ -623,6 +711,8 @@ static void filter_redirector_init(Object *obj)
     MirrorState *s = FILTER_REDIRECTOR(obj);
 
     s->vnet_hdr = false;
+    s->in_netfd = -1;
+    s->out_netfd = -1;
 }
 
 static void filter_mirror_fini(Object *obj)
@@ -638,6 +728,7 @@ static void filter_redirector_fini(Object *obj)
 
     g_free(s->indev);
     g_free(s->outdev);
+    g_free(s->in_netbuf);
 }
 
 static const TypeInfo filter_redirector_info = {
Signature · 1 line
2.52.0
#4 lore raw

Re: [RFC v3 3/7] net/filter-redirector: add AF_PACKET netdev setup helpers

Quoted 11 lines · Cindy Lu

Prepare filter-redirector for direct TAP access through AF_PACKET sockets.

Add the setup-side plumbing needed by later capture/inject changes:
- extend MirrorState with dedicated AF_PACKET fds and a capture buffer
- resolve the TAP ifname from the backend fd
- create and bind a nonblocking AF_PACKET socket on that interface
- store the socket as the capture or inject endpoint based on the redirector role
- clean up the AF_PACKET resources from cleanup/finalize paths

Quoted 4 lines

This commit only prepares the sockets and object state. The actual capture and inject datapaths are added by follow-up commits.

I think I have the same question as version 1, could we simply reuse

1) netdev socket
or
2) chardev

for packet socket instead of having this?

Thanks

author
#5 lore raw

RFC v3 4/7 net/filter-redirector: add AF_PACKET capture path

In reply to Cindy Lu

Wire the capture-side AF_PACKET socket into filter-redirector.

When the redirector owns an AF_PACKET capture socket, install a read handler that drains PACKET_OUTGOING frames from the TAP device and forwards them into the existing redirector chardev path. Reuse the normal chardev packet framing so downstream code sees the same transport format as it already handles today, and keep redirector statistics updated through the same helper.

AF_PACKET delivers a raw Ethernet frame while the redirector chardev path can carry packets with an empty vnet header wrapper. When the backend has a vnet header length, prepend an empty legacy virtio-net header before the frame so the captured packet can move through the same chardev transport as regular redirector traffic.

Hook the fd handler up during setup, status changes and VM state changes so capture is only active when the redirector is enabled and allowed to run in the current VM state.

---
 net/filter-mirror.c | 121 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
net/filter-mirror.c +121 -0
index d9f6a11d6b..e114ddb7d1 100644
@@ -43,6 +43,7 @@ DECLARE_INSTANCE_CHECKER(MirrorState, FILTER_REDIRECTOR,
                          TYPE_FILTER_REDIRECTOR)
 
 #define REDIRECTOR_MAX_LEN NET_BUFSIZE
+#define REDIRECTOR_AF_PACKET_WRAP_LEN sizeof(struct virtio_net_hdr)
 
 struct MirrorState {
     NetFilterState parent_obj;
@@ -181,6 +182,17 @@ static int redirector_chr_can_read(void *opaque)
     return REDIRECTOR_MAX_LEN;
 }
 
+static bool filter_redirector_input_active(NetFilterState *nf, bool enable)
+{
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+
+    if (!enable) {
+        return false;
+    }
+
+    return runstate_is_running() || s->enable_when_stopped;
+}
+
 static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
 {
     NetFilterState *nf = opaque;
@@ -217,6 +229,91 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)
     }
 }
 
+static int filter_redirector_send_chardev_iov(MirrorState *s,
+                                              const struct iovec *iov,
+                                              int iovcnt)
+{
+    int ret;
+
+    if (!qemu_chr_fe_backend_connected(&s->chr_out)) {
+        return 0;
+    }
+
+    ret = filter_send(s, iov, iovcnt);
+    if (ret > 0) {
+        s->outdev_packets++;
+        s->outdev_bytes += ret;
+    }
+
+    return ret;
+}
+
+static void filter_redirector_capture_netdev_read(void *opaque)
+{
+    NetFilterState *nf = opaque;
+    MirrorState *s = FILTER_REDIRECTOR(nf);
+    char vnet_hdr[REDIRECTOR_AF_PACKET_WRAP_LEN] = { 0 };
+    struct iovec iov[2];
+    struct sockaddr_ll sll;
+    socklen_t sll_len;
+    ssize_t len;
+    size_t wrap_vnet_hdr_len;
+    int iovcnt;
+    int ret;
+
+    if (!s->in_netbuf || s->in_netfd < 0) {
+        return;
+    }
+
+    for (;;) {
+        sll_len = sizeof(sll);
+        len = recvfrom(s->in_netfd, s->in_netbuf, REDIRECTOR_MAX_LEN, 0,
+                       (struct sockaddr *)&sll, &sll_len);
+        if (len <= 0) {
+            break;
+        }
+
+        if (sll.sll_pkttype != PACKET_OUTGOING) {
+            continue;
+        }
+
+        /*
+         * AF_PACKET gives us a raw Ethernet frame. Wrap it as a regular
+         * redirector payload by prepending an empty legacy virtio-net header,
+         * so the downstream chardev path can treat it like a normal packet.
+         */
+        wrap_vnet_hdr_len = qemu_get_vnet_hdr_len(nf->netdev) ?
+                            REDIRECTOR_AF_PACKET_WRAP_LEN : 0;
+        if (len + wrap_vnet_hdr_len > REDIRECTOR_MAX_LEN) {
+            error_report("filter redirector packet too large after wrap(%zd)",
+                         len);
+            continue;
+        }
+
+        iov[0].iov_base = s->in_netbuf;
+        iov[0].iov_len = len;
+        iovcnt = 1;
+        if (wrap_vnet_hdr_len) {
+            iov[0].iov_base = vnet_hdr;
+            iov[0].iov_len = wrap_vnet_hdr_len;
+            iov[1].iov_base = s->in_netbuf;
+            iov[1].iov_len = len;
+            iovcnt = 2;
+        }
+
+        ret = filter_redirector_send_chardev_iov(s, iov, iovcnt);
+        if (ret < 0) {
+            error_report("filter redirector send failed(%s)", strerror(-ret));
+        }
+    }
+
+    if (len < 0 && errno != EAGAIN && errno != EWOULDBLOCK &&
+        errno != EINTR) {
+        error_report("filter redirector read netdev failed(%s)",
+                     strerror(errno));
+    }
+}
+
 static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
                                          NetClientState *sender,
                                          unsigned flags,
@@ -353,6 +450,14 @@ static void filter_redirector_vm_state_change(void *opaque, bool running,
     NetFilterState *nf = opaque;
     MirrorState *s = FILTER_REDIRECTOR(nf);
     NetClientState *nc = nf->netdev;
+    bool active = filter_redirector_input_active(nf, nf->on);
+
+    if (s->in_netfd >= 0) {
+        qemu_set_fd_handler(s->in_netfd,
+                            active ?
+                            filter_redirector_capture_netdev_read : NULL,
+                            NULL, active ? nf : NULL);
+    }
 
     if (!running && s->enable_when_stopped && nc->info->read_poll) {
         nc->info->read_poll(nc, true);
@@ -448,6 +553,7 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(nf);
     Chardev *chr;
+    bool active = filter_redirector_input_active(nf, nf->on);
 
     if (!s->indev && !s->outdev) {
         error_setg(errp, "filter redirector needs 'indev' or "
@@ -501,6 +607,13 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
     s->vmsentry = qemu_add_vm_change_state_handler(
         filter_redirector_vm_state_change, nf);
 
+    if (s->in_netfd >= 0) {
+        qemu_set_fd_handler(s->in_netfd,
+                            active ?
+                            filter_redirector_capture_netdev_read : NULL,
+                            NULL, active ? nf : NULL);
+    }
+
     filter_redirector_maybe_enable_read_poll(nf);
 
     filter_redirector_refresh_allow_send_when_stopped(nf);
@@ -509,6 +622,7 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
 static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(nf);
+    bool active = filter_redirector_input_active(nf, nf->on);
 
     if (s->indev) {
         if (nf->on) {
@@ -521,6 +635,13 @@ static void filter_redirector_status_changed(NetFilterState *nf, Error **errp)
         }
     }
 
+    if (s->in_netfd >= 0) {
+        qemu_set_fd_handler(s->in_netfd,
+                            active ?
+                            filter_redirector_capture_netdev_read : NULL,
+                            NULL, active ? nf : NULL);
+    }
+
     if (nf->on) {
         filter_redirector_maybe_enable_read_poll(nf);
     }
Signature · 1 line
2.52.0
author
#6 lore raw

RFC v3 5/7 net/filter-redirector: add AF_PACKET inject path

In reply to Cindy Lu

Allow redirector packets to be injected back into a vhost-backed TAP backend without going through the normal peer queue.

Export a redirector helper that sends an iov directly through the redirector's AF_PACKET inject socket, and teach qemu_netfilter_pass_to_next() to use it when the sender is backed by vhost. This lets the redirector reinject traffic into the TAP device even when the regular userspace path is bypassed by vhost.

The redirector chardev transport may carry an empty vnet header wrapper before the Ethernet frame. Detect the longest supported all-zero wrapper and strip it before sendmsg() so the AF_PACKET inject side always writes a raw L2 frame back to TAP.

Propagate sendmsg() failures through ssize_t so the caller can report partial/failed injection cleanly.

---
 include/net/filter.h |  6 +++
 net/filter-mirror.c  | 94 ++++++++++++++++++++++++++++++++++++++++++++
 net/filter.c         | 11 +++++-
 3 files changed, 110 insertions(+), 1 deletion(-)
include/net/filter.h +6 -0
index f0673c9163..68076a66ad 100644
@@ -91,6 +91,12 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
                                     int iovcnt,
                                     void *opaque);
 
+bool qemu_netfilter_redirector_inject_netdev(NetFilterState *nf,
+                                             NetClientState *sender,
+                                             const struct iovec *iov,
+                                             int iovcnt,
+                                             ssize_t *retp);
+
 void colo_notify_filters_event(int event, Error **errp);
 
 #endif /* QEMU_NET_FILTER_H */
net/filter-mirror.c +94 -0

100 lines not on this page · raw message

net/filter.c +10 -1
index 76345c1a9d..847dd34698 100644
@@ -81,7 +81,7 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
                                     int iovcnt,
                                     void *opaque)
 {
-    int ret = 0;
+    ssize_t ret = 0;
     int direction;
     NetFilterState *nf = opaque;
     NetFilterState *next = NULL;
@@ -124,6 +124,15 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
      * deleted while we go through filters.
      */
     if (sender && sender->peer) {
+        if (get_vhost_net(sender) &&
+            qemu_netfilter_redirector_inject_netdev(nf, sender,
+                                                    iov, iovcnt, &ret)) {
+            if (ret < 0) {
+                error_report("filter redirector AF_PACKET send failed(%s)",
+                             strerror(-ret));
+            }
+            goto out;
+        }
         qemu_net_queue_send_iov(sender->peer->incoming_queue,
                                 sender, flags, iov, iovcnt, NULL);
     }
Signature · 1 line
2.52.0
author
#7 lore raw

RFC v3 6/7 net/filter: allow filters on vhost TAP backends

In reply to Cindy Lu

netfilter_complete() currently rejects any filter attached to a vhost-backed netdev. The AF_PACKET redirector series needs to attach filters to vhost-backed TAP devices, so relax the blanket vhost rejection and keep only the backend-type requirement.

---
 net/filter.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/filter.c +4 -2
index 847dd34698..636586e1f3 100644
@@ -264,8 +264,10 @@ static void netfilter_complete(UserCreatable *uc, Error **errp)
     }
 
     if (get_vhost_net(ncs[0])) {
-        error_setg(errp, "Vhost is not supported");
-        return;
+        if (ncs[0]->info->type != NET_CLIENT_DRIVER_TAP) {
+            error_setg(errp, "Vhost filter support requires a TAP backend");
+            return;
+        }
     }
 
     if (strcmp(nf->position, "head") && strcmp(nf->position, "tail")) {
Signature · 1 line
2.52.0
author
#8 lore raw

RFC v3 7/7 net/filter-redirector: check CAP_NET_RAW before creating AF_PACKET

In reply to Cindy Lu

Creating an AF_PACKET SOCK_RAW socket requires the CAP_NET_RAW capability. Without it the qemu_socket() call fails with EPERM, producing a generic error that gives no hint about the missing capability.

Add an explicit capget()-based check in filter_redirector_netdev_setup() before the socket call.

---
 net/filter-mirror.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
net/filter-mirror.c +24 -0
index 165a7ee1ea..a521eec02b 100644
@@ -34,6 +34,8 @@
 #include <net/if.h>
 #include <linux/if_packet.h>
 #include <netinet/if_ether.h>
+#include <linux/capability.h>
+#include <sys/syscall.h>
 
 typedef struct MirrorState MirrorState;
 DECLARE_INSTANCE_CHECKER(MirrorState, FILTER_MIRROR,
@@ -579,6 +581,21 @@ static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf)
     }
 }
 
+static bool filter_redirector_has_cap_net_raw(void)
+{
+    struct __user_cap_header_struct hdr = {
+        .version = _LINUX_CAPABILITY_VERSION_3,
+        .pid = 0,
+    };
+    struct __user_cap_data_struct data[2] = {};
+
+    if (syscall(SYS_capget, &hdr, data) < 0) {
+        return false;
+    }
+
+    return data[CAP_NET_RAW >> 5].effective & (1u << (CAP_NET_RAW & 31));
+}
+
 static bool filter_redirector_netdev_setup(NetFilterState *nf, Error **errp)
 {
     MirrorState *s = FILTER_REDIRECTOR(nf);
@@ -615,6 +632,13 @@ static bool filter_redirector_netdev_setup(NetFilterState *nf, Error **errp)
         return false;
     }
 
+    if (!filter_redirector_has_cap_net_raw()) {
+        error_setg(errp,
+                   "AF_PACKET raw socket requires CAP_NET_RAW; "
+                   "run with 'setcap cap_net_raw+ep <qemu-binary>' or as root");
+        return false;
+    }
+
     fd = qemu_socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ALL));
     if (fd < 0) {
         error_setg_errno(errp, errno, "failed to create AF_PACKET socket");
Signature · 1 line
2.52.0
#9 lore raw
In reply to Cindy Lu
Quoted 10 lines · Cindy Lu

Hi, All

This series adds AF_PACKET-based capture and injection datapath support for the vhost TAP device in filter

Example Usage(not change with exist upstream code)
=============
Primary VM (mirror incoming packets to secondary via chardev socket):

Quoted 8 lines

-netdev tap,id=net0,vhost=on,...
-chardev socket,id=mirror0,host=...,port=...,server=on,wait=off -object filter-redirector,id=vm1redir,netdev=net0,outdev=mirror0...

Secondary VM (receive mirrored packets):

-netdev tap,id=net0,vhost=on,...
-chardev socket,id=red0,host=...,port=...,reconnect-ms=..

Is this for packet socket?

Quoted 44 lines
  -object filter-buffer,id=swbuf,netdev=net0,queue=tx,interval=1000000,status=off.....
  -object filter-redirector,id=r1,netdev=net0,queue=tx,indev=red0,status=off,enable_when
  _stopped=true.... \

changset
===========
change in v2:
1. add support for filter-buffer
2. remove the in_netdev and out_netdev for AF_PACKET bind port, now only use netdev when the vhost=on start use AF_PACKET to capture and inject, when use vhost=off will use the existing code
3. add CAP_NET_RAW check
4. address the comment

change in v3:
1. reuse the exist Capture/inject process

Testing
=======
- Tested with vhost=on/off TAP device on x86_64

known issue:
============
1. compilation should be disabled on non-Linux platforms.will fix in next version.
2. Sometimes the secondary VM receives duplicate packets. will fix this in next version.

Cindy Lu (7):
net/filter-mirror: keep mirroring while stopped
virtio-net: keep tap read polling disabled while vhost owns RX net/filter-redirector: add AF_PACKET netdev setup helpers net/filter-redirector: add AF_PACKET capture path net/filter-redirector: add AF_PACKET inject path net/filter: allow filters on vhost TAP backends net/filter-redirector: check CAP_NET_RAW before creating AF_PACKET

 hw/net/virtio-net.c  |  12 +-
 include/net/filter.h |   6 +
 include/net/net.h    |   6 +
 net/filter-mirror.c  | 379 ++++++++++++++++++++++++++++++++++++++++++-
 net/filter.c         |  17 +-
 net/net.c            |  10 +-
 6 files changed, 417 insertions(+), 13 deletions(-)
Quoted 3 lines

--
2.52.0

Thanks

author
#10 lore raw
Quoted 15 lines · Jason Wang
Quoted 14 lines · Cindy Lu

Hi, All

This series adds AF_PACKET-based capture and injection datapath support for the vhost TAP device in filter

Example Usage(not change with exist upstream code)
=============
Primary VM (mirror incoming packets to secondary via chardev socket):

-netdev tap,id=net0,vhost=on,...
-chardev socket,id=mirror0,host=...,port=...,server=on,wait=off -object filter-redirector,id=vm1redir,netdev=net0,outdev=mirror0...

Quoted 8 lines
Quoted 4 lines

Secondary VM (receive mirrored packets):

-netdev tap,id=net0,vhost=on,...
-chardev socket,id=red0,host=...,port=...,reconnect-ms=..

Is this for packet socket?

this is connect to the -chardev

socket,id=red0,host=127.0.0.1,port=9003,reconnect-ms=1000 \ in the Primary
VM to mirror the packet. should I also change this into packet socket ?
I didn't change the inside filter process in this code and only change the capture and inject
Thanks
cindy

Quoted 57 lines
Quoted 1 line

-object

filter-buffer,id=swbuf,netdev=net0,queue=tx,interval=1000000,status=off.....

Quoted 1 line

-object

filter-redirector,id=r1,netdev=net0,queue=tx,indev=red0,status=off,enable_when

Quoted 7 lines
  _stopped=true.... \

changset
===========
change in v2:
1. add support for filter-buffer
2. remove the in_netdev and out_netdev for AF_PACKET bind port, now only

use netdev

Quoted 1 line

when the vhost=on start use AF_PACKET to capture and inject, when use

vhost=off will use

Quoted 7 lines

the existing code
3. add CAP_NET_RAW check
4. address the comment

change in v3:
1. reuse the exist Capture/inject process

Quoted 7 lines

Testing
=======
- Tested with vhost=on/off TAP device on x86_64

known issue:
============
1. compilation should be disabled on non-Linux platforms.will fix in

next version.

Quoted 1 line

2. Sometimes the secondary VM receives duplicate packets. will fix this

in next version.

Quoted 19 lines

Cindy Lu (7):
net/filter-mirror: keep mirroring while stopped
virtio-net: keep tap read polling disabled while vhost owns RX net/filter-redirector: add AF_PACKET netdev setup helpers net/filter-redirector: add AF_PACKET capture path net/filter-redirector: add AF_PACKET inject path net/filter: allow filters on vhost TAP backends net/filter-redirector: check CAP_NET_RAW before creating AF_PACKET

 hw/net/virtio-net.c  |  12 +-
 include/net/filter.h |   6 +
 include/net/net.h    |   6 +
 net/filter-mirror.c  | 379 ++++++++++++++++++++++++++++++++++++++++++-
 net/filter.c         |  17 +-
 net/net.c            |  10 +-
 6 files changed, 417 insertions(+), 13 deletions(-)
Quoted 3 lines

--
2.52.0

Thanks