net/filter-redirector: Add AF_PACKET support for vhost-net
cover net/filter-redirector: Add AF_PACKET support for vhost-net
This series extends filter-redirector to support vhost-net backends by introducing AF_PACKET based packet capture and injection.
TODO
========
This series introduces basic support for filter-redirector, enabling simple mirroring of VM1's ingress traffic to VM2's mirror port. Additional functionality including filter_buffer and other filter features will be added in following versions.
Example Usage
=============
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,in_netdev=net0,outdev=mirror0
Secondary VM (receive mirrored packets):
-chardev socket,id=red0,host=...,port=...,reconnect-ms=1000 -object filter-redirector,id=vm2redir,netdev=net0,indev=red0,out_netdev=net0
Testing
=======
- Tested with vhost=on/off TAP netdev on x86_64
Cindy Lu (5):
net/filter: Allow redirector on vhost backends net/filter-redirector: add in/out_netdev properties net/filter-redirector: Add AF_PACKET sockets initialization net/filter-redirector: add AF_PACKET redirect datapath
qapi: add in/out_netdev to filter-redirector
net/filter-mirror.c | 460 +++++++++++++++++++++++++++++++++++++++++--- net/filter.c | 6 +- qapi/qom.json | 19 +- 3 files changed, 459 insertions(+), 26 deletions(-)
Signature · 1 line
2.52.0
RFC 1/5 net/filter: Allow redirector on vhost backends
Allow filter redirector on vhost backends
--- net/filter.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/filter.c +4 -2
@@ -255,8 +255,10 @@ static void netfilter_complete(UserCreatable *uc, Error **errp) }if (get_vhost_net (ncs[0 ])) { -error_setg (errp,"Vhost is not supported" ); -return ; +if (!object_dynamic_cast (OBJECT (uc),"filter-redirector" )) { +error_setg (errp,"Vhost is not supported" ); +return ; + } }if (strcmp (nf->position,"head" ) &&strcmp (nf->position,"tail" )) {
Signature · 1 line
2.52.0
Re: [RFC 1/5] net/filter: Allow redirector on vhost backends
Quoted 2 lines · Cindy Lu
Allow filter redirector on vhost backends
This can't work for all type of netfilter I believe unless we implemeting a general shadow virtqueue etc.
Quoted 24 lines
Signed-off-by Cindy Lu <[email protected]>--- net/filter.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/filter.c +4
-3@@ -255,8 +255,10 @@ static void netfilter_complete(UserCreatable *uc, Error **errp) }if (get_vhost_net (ncs[0 ])) { -error_setg (errp,"Vhost is not supported" ); -return ; +if (!object_dynamic_cast (OBJECT (uc),"filter-redirector" )) { +error_setg (errp,"Vhost is not supported" ); +return ; + } }if (strcmp (nf->position,"head" ) &&strcmp (nf->position,"tail" )) { --
Quoted 1 line
2.52.0
Thanks
Quoted 1 line
RFC 2/5 net/filter-redirector: add in/out_netdev properties
In reply to Cindy LuIntroduce two new optional properties for filter-redirector:
- in_netdev: specifies a TAP netdev to capture packets from using AF_PACKET sockets
- out_netdev: specifies a netdev to inject packets into using AF_PACKET sockets
These properties enable filter-redirector to work with vhost-net backends by bypassing the QEMU network stack entirely. Instead of relying on chardev sockets (indev/outdev), the redirector can directly capture/inject packets at the host TAP interface level.
The in_netdev and out_netdev are mutually exclusive with each other and with their chardev counterparts (indev/outdev) on the same direction.
--- net/filter-mirror.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
net/filter-mirror.c +44 -0
@@ -40,6 +40,8 @@ struct MirrorState {NetFilterState parent_obj;char *indev;char *outdev; +char *in_netdev; +char *out_netdev;CharFrontend chr_in;CharFrontend chr_out;SocketReadState rs;
@@ -510,6 +512,40 @@ static void filter_redirector_set_outdev(Object *obj, s->outdev =g_strdup (value); } +static char *filter_redirector_get_in_netdev (Object *obj,Error **errp) +{ +MirrorState *s =FILTER_REDIRECTOR (obj); + +return g_strdup (s->in_netdev); +} + +static void filter_redirector_set_in_netdev (Object *obj, +const char *value, +Error **errp) +{ +MirrorState *s =FILTER_REDIRECTOR (obj); + +g_free (s->in_netdev); + s->in_netdev =g_strdup (value); +} + +static char *filter_redirector_get_out_netdev (Object *obj,Error **errp) +{ +MirrorState *s =FILTER_REDIRECTOR (obj); + +return g_strdup (s->out_netdev); +} + +static void filter_redirector_set_out_netdev (Object *obj, +const char *value, +Error **errp) +{ +MirrorState *s =FILTER_REDIRECTOR (obj); + +g_free (s->out_netdev); + s->out_netdev =g_strdup (value); +} +static bool filter_redirector_get_vnet_hdr (Object *obj,Error **errp) {MirrorState *s =FILTER_REDIRECTOR (obj);
@@ -597,6 +633,12 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data) filter_redirector_set_indev);object_class_property_add_str (oc,"outdev" , filter_redirector_get_outdev, filter_redirector_set_outdev); +object_class_property_add_str (oc,"in_netdev" , + filter_redirector_get_in_netdev, + filter_redirector_set_in_netdev); +object_class_property_add_str (oc,"out_netdev" , + filter_redirector_get_out_netdev, + filter_redirector_set_out_netdev);object_class_property_add_bool (oc,"vnet_hdr_support" , filter_redirector_get_vnet_hdr, filter_redirector_set_vnet_hdr);
@@ -638,6 +680,8 @@ static void filter_redirector_fini(Object *obj)g_free (s->indev);g_free (s->outdev); +g_free (s->in_netdev); +g_free (s->out_netdev); }static const TypeInfo filter_redirector_info = {
Signature · 1 line
2.52.0
RFC 3/5 net/filter-redirector: Add AF_PACKET sockets initialization
In reply to Cindy LuImplement the initialization logic for AF_PACKET based netdev endpoints in filter-redirector:
1. filter_redirector_netdev_setup(): Creates and binds AF_PACKET sockets to the specified TAP interface. For in_netdev, the socket is used to receive packets; for out_netdev, it is used to send.
2. filter_redirector_netdev_read(): Async handler for reading packets from the in_netdev AF_PACKET socket. Packets are forwarded through the redirector chain.
3. Updated cleanup to properly close AF_PACKET sockets and free associated buffers.
4. Modified allow_send_when_stopped logic to consider both chardev and netdev output endpoints, and to only enable when the redirector is active (status=on).
5. VM state change handler now manages the AF_PACKET read handler activation based on VM running state and enable_when_stopped.
--- net/filter-mirror.c | 241 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 224 insertions(+), 17 deletions(-)
net/filter-mirror.c +224 -17
@@ -26,6 +26,13 @@#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,
@@ -42,6 +49,10 @@ struct MirrorState {char *outdev;char *in_netdev;char *out_netdev; +NetClientState *out_net; +int in_netfd; +uint8_t *in_netbuf; +int out_netfd;CharFrontend chr_in;CharFrontend chr_out;SocketReadState rs;
@@ -172,6 +183,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;
@@ -208,6 +230,40 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event) } } +static void filter_redirector_netdev_read (void *opaque) +{ +NetFilterState *nf = opaque; +MirrorState *s =FILTER_REDIRECTOR (nf); +struct sockaddr_ll sll; +socklen_t sll_len; +ssize_t len; + +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 ; + } + +redirector_to_filter (nf, s->in_netbuf, len); + } + +if (len <0 && errno !=EAGAIN && errno !=EWOULDBLOCK && + errno !=EINTR ) { +error_report ("filter redirector read in_netdev failed(%s)" , +strerror (errno)); + } +} + static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,
@@ -268,7 +324,19 @@ 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->vmsentry) { +qemu_del_vm_change_state_handler (s->vmsentry); + s->vmsentry =NULL ; + } +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 ;
@@ -320,13 +388,13 @@ filter_redirector_refresh_allow_send_when_stopped(NetFilterState *nf)/* * Allow sending when stopped if enable_when_stopped is set and we have -* an outdev. This must be independent of nf->on (status) so that packets -* can still flow through the filter chain to other filters even when this -* redirector is disabled. Otherwise, tap_send() will disable read_poll -* when qemu_can_send_packet() returns false, preventing further packet -* processing. +* a redirector output endpoint and the redirector is enabled. +* Keeping this active while redirector status=off can unexpectedly +* drain packets in migration stop windows and perturb vhost ring state. */ - nc->allow_send_when_stopped = (s->enable_when_stopped && s->outdev); + nc->allow_send_when_stopped = (nf->on && + s->enable_when_stopped && + (s->outdev || s->out_netdev)); } static void filter_redirector_vm_state_change(void *opaque, bool running,
@@ -335,8 +403,16 @@ 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_netdev_read :NULL , +NULL , + active ? nf :NULL ); + } -if (!running && s->enable_when_stopped && nc->info->read_poll) { +if (!running && nc && s->enable_when_stopped && nc->info->read_poll) { nc->info->read_poll (nc, true); } }
@@ -362,21 +438,127 @@ static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf) } } +static bool filter_redirector_netdev_setup (MirrorState *s,Error **errp) +{ +struct sockaddr_ll sll = {0 }; +char ifname[IFNAMSIZ ] = {0 }; +int ifindex; +int fd; +NetClientState *nc; + +if (s->in_netdev) { +int tapfd; + nc =qemu_find_netdev (s->in_netdev); +if (!nc) { +error_setg (errp,"in_netdev '%s' not found" , s->in_netdev); +return false; + } + +if (nc->info->type !=NET_CLIENT_DRIVER_TAP ) { +error_setg (errp,"in_netdev '%s' must be a TAP netdev" , + s->in_netdev); +return false; + } + + tapfd =tap_get_fd (nc); +if (tapfd <0 ||tap_fd_get_ifname (tapfd, ifname) !=0 ) { +error_setg (errp,"failed to resolve TAP ifname for in_netdev '%s'" , + s->in_netdev); +return false; + } + }else if (s->out_netdev) { + nc =qemu_find_netdev (s->out_netdev); +if (!nc) { +error_setg (errp,"out_netdev '%s' not found" , s->out_netdev); +return false; + } +/* +* out_netdev always uses AF_PACKET. For TAP netdev we resolve the +* interface name from tap fd; for non-TAP netdev we interpret +* out_netdev string as host interface name. +*/ +if (nc->info->type ==NET_CLIENT_DRIVER_TAP ) { +int tapfd =tap_get_fd (nc); + +if (tapfd <0 ||tap_fd_get_ifname (tapfd, ifname) !=0 ) { +error_setg (errp, +"failed to resolve TAP ifname for out_netdev '%s'" , + s->out_netdev); +return false; + } + }else { +snprintf (ifname,sizeof (ifname),"%s" , s->out_netdev); + } + } + + 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 (s->in_netdev) { + s->in_netfd = fd; +g_free (s->in_netbuf); + s->in_netbuf =g_malloc (REDIRECTOR_MAX_LEN ); + }else { + s->out_netfd = fd; + s->out_net = nc; + } +return true; +} +static void filter_redirector_setup (NetFilterState *nf,Error **errp) {MirrorState *s =FILTER_REDIRECTOR (nf);Chardev *chr; -if (!s->indev && !s->outdev) { -error_setg (errp,"filter redirector needs 'indev' or " -"'outdev' at least one property set" ); +if (!s->indev && !s->outdev && !s->in_netdev && !s->out_netdev) { +error_setg (errp,"filter redirector needs at least one of " +"'indev', 'outdev', 'in_netdev', or 'out_netdev'" ); +return ; + } + +if (s->indev && s->in_netdev) { +error_setg (errp,"'indev' and 'in_netdev' cannot both be set " +"for filter redirector" ); +return ; + } + +if (s->outdev && s->out_netdev) { +error_setg (errp,"'outdev' and 'out_netdev' cannot both be set " +"for filter redirector" ); +return ; + } + +if (s->in_netdev && s->out_netdev) { +error_setg (errp,"'in_netdev' and 'out_netdev' cannot both be set " +"for filter redirector" ); +return ; + } + +if (s->indev && s->outdev && !strcmp (s->indev, s->outdev)) { +error_setg (errp,"'indev' and 'outdev' could not be same " +"for filter redirector" );return ; - }else if (s->indev && s->outdev) { -if (!strcmp (s->indev, s->outdev)) { -error_setg (errp,"'indev' and 'outdev' could not be same " -"for filter redirector" ); -return ; - } }net_socket_rs_init (&s->rs, redirector_rs_finalize, s->vnet_hdr);
@@ -412,9 +594,23 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp) } } +if (s->out_netdev || s->in_netdev) { +if (!filter_redirector_netdev_setup (s, errp)) { +return ; + } + } + s->vmsentry =qemu_add_vm_change_state_handler ( filter_redirector_vm_state_change, nf); +if (s->in_netfd >=0 ) { +bool active =filter_redirector_input_active (nf, nf->on); + +qemu_set_fd_handler (s->in_netfd, + active ? filter_redirector_netdev_read :NULL , +NULL , + active ? nf :NULL ); + }filter_redirector_maybe_enable_read_poll (nf);filter_redirector_refresh_allow_send_when_stopped (nf);
@@ -423,6 +619,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) {
@@ -435,6 +632,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_netdev_read :NULL , +NULL , + active ? nf :NULL ); + } +if (nf->on) {filter_redirector_maybe_enable_read_poll (nf); }
@@ -665,6 +869,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)
@@ -682,6 +888,7 @@ static void filter_redirector_fini(Object *obj)g_free (s->outdev);g_free (s->in_netdev);g_free (s->out_netdev); +g_free (s->in_netbuf); }static const TypeInfo filter_redirector_info = {
Signature · 1 line
2.52.0
Re: [RFC 3/5] net/filter-redirector: Add AF_PACKET sockets initialization
Quoted 189 lines · Cindy Lu
Implement the initialization logic for
AF_PACKETbased netdev endpoints in filter-redirector:1.
filter_redirector_netdev_setup(): Creates and bindsAF_PACKETsockets to the specified TAP interface. Forin_netdev, the socket is used to receive packets; forout_netdev, it is used to send.2.
filter_redirector_netdev_read(): Async handler for reading packets from thein_netdevAF_PACKETsocket. Packets are forwarded through the redirector chain.3. Updated cleanup to properly close
AF_PACKETsockets and free associated buffers.4. Modified
allow_send_when_stoppedlogic to consider both chardev and netdev output endpoints, and to only enable when the redirector is active (status=on).5. VM state change handler now manages the
AF_PACKETread handler activation based on VM running state andenable_when_stopped.Signed-off-by Cindy Lu <[email protected]>--- net/filter-mirror.c | 241 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 224 insertions(+), 17 deletions(-)net/filter-mirror.c +95
-8@@ -26,6 +26,13 @@#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,@@ -42,6 +49,10 @@ struct MirrorState {char *outdev;char *in_netdev;char *out_netdev; +NetClientState *out_net; +int in_netfd; +uint8_t *in_netbuf; +int out_netfd;CharFrontend chr_in;CharFrontend chr_out;SocketReadState rs;@@ -172,6 +183,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;@@ -208,6 +230,40 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event) } } +static void filter_redirector_netdev_read (void *opaque) +{ +NetFilterState *nf = opaque; +MirrorState *s =FILTER_REDIRECTOR (nf); +struct sockaddr_ll sll; +socklen_t sll_len; +ssize_t len; + +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 ; + } + +redirector_to_filter (nf, s->in_netbuf, len); + } + +if (len <0 && errno !=EAGAIN && errno !=EWOULDBLOCK && + errno !=EINTR ) { +error_report ("filter redirector read in_netdev failed(%s)" , +strerror (errno)); + } +} + static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -268,7 +324,19 @@ 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->vmsentry) { +qemu_del_vm_change_state_handler (s->vmsentry); + s->vmsentry =NULL ; + } +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 ;@@ -320,13 +388,13 @@ filter_redirector_refresh_allow_send_when_stopped(NetFilterState *nf)/* * Allow sending when stopped if enable_when_stopped is set and we have -* an outdev. This must be independent of nf->on (status) so that packets -* can still flow through the filter chain to other filters even when this -* redirector is disabled. Otherwise, tap_send() will disable read_poll -* when qemu_can_send_packet() returns false, preventing further packet -* processing. +* a redirector output endpoint and the redirector is enabled. +* Keeping this active while redirector status=off can unexpectedly +* drain packets in migration stop windows and perturb vhost ring state. */ - nc->allow_send_when_stopped = (s->enable_when_stopped && s->outdev); + nc->allow_send_when_stopped = (nf->on && + s->enable_when_stopped && + (s->outdev || s->out_netdev)); } static void filter_redirector_vm_state_change(void *opaque, bool running,@@ -335,8 +403,16 @@ 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_netdev_read :NULL , +NULL , + active ? nf :NULL ); + } -if (!running && s->enable_when_stopped && nc->info->read_poll) { +if (!running && nc && s->enable_when_stopped && nc->info->read_poll) { nc->info->read_poll (nc, true); } }@@ -362,21 +438,127 @@ static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf) } } +static bool filter_redirector_netdev_setup (MirrorState *s,Error **errp) +{ +struct sockaddr_ll sll = {0 }; +char ifname[IFNAMSIZ ] = {0 }; +int ifindex; +int fd; +NetClientState *nc; + +if (s->in_netdev) { +int tapfd; + nc =qemu_find_netdev (s->in_netdev);
net/filter-mirror.c static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf) · line 461
Quoted 10 lines
net/filter-mirror.c +10
-0+if (!nc) { +error_setg (errp,"in_netdev '%s' not found" , s->in_netdev); +return false; + } + +if (nc->info->type !=NET_CLIENT_DRIVER_TAP ) { +error_setg (errp,"in_netdev '%s' must be a TAP netdev" , + s->in_netdev); +return false; + }
This seems inelegant; the netfilter should not be coupled with a specific netdev type. Any reason for doing this?
Quoted 5 lines
net/filter-mirror.c +5
-0+ + tapfd =tap_get_fd (nc); +if (tapfd <0 ||tap_fd_get_ifname (tapfd, ifname) !=0 ) { +error_setg (errp,"failed to resolve TAP ifname for in_netdev '%s'" , + s->in_netdev);
net/filter-mirror.c static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf)
Quoted 10 lines
net/filter-mirror.c +10
-0+return false; + } + }else if (s->out_netdev) { + nc =qemu_find_netdev (s->out_netdev); +if (!nc) { +error_setg (errp,"out_netdev '%s' not found" , s->out_netdev); +return false; + } +/* +* out_netdev always uses AF_PACKET.
I think I don't get this.
Quoted 16 lines
For TAP netdev we resolve the
net/filter-mirror.c +15
-0+* interface name from tap fd; for non-TAP netdev we interpret +* out_netdev string as host interface name. +*/ +if (nc->info->type ==NET_CLIENT_DRIVER_TAP ) { +int tapfd =tap_get_fd (nc); + +if (tapfd <0 ||tap_fd_get_ifname (tapfd, ifname) !=0 ) { +error_setg (errp, +"failed to resolve TAP ifname for out_netdev '%s'" , + s->out_netdev); +return false; + } + }else { +snprintf (ifname,sizeof (ifname),"%s" , s->out_netdev); + }
net/filter-mirror.c static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf) · line 501
Quoted 10 lines
net/filter-mirror.c +10
-0+ } + + 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 ));
There must be a misunderstanding; can we simply reuse the -netdev socket for packet socket?
Another concern, using packet socket requires CAP_NET_ADMIN etc.
Quoted 17 lines
net/filter-mirror.c +17
-0+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 (s->in_netdev) {
net/filter-mirror.c static void filter_redirector_maybe_enable_read_poll(NetFilterState *nf) · line 528
Quoted 10 lines
net/filter-mirror.c +10
-0+ s->in_netfd = fd; +g_free (s->in_netbuf); + s->in_netbuf =g_malloc (REDIRECTOR_MAX_LEN ); + }else { + s->out_netfd = fd; + s->out_net = nc; + } +return true; +} +
Thanks
RFC 4/5 net/filter-redirector: add AF_PACKET redirect datapath
In reply to Cindy LuComplete the AF_PACKET based packet forwarding implementation for filter-redirector:
1. filter_redirector_send_netdev_packet(): Send packets via AF_PACKET socket to out_netdev. Updates netdev_tx statistics.
2. filter_redirector_recv_from_chardev(): Handle packets received from chardev indev. Can forward to either chardev outdev, AF_PACKET out_netdev, or inject into the netfilter chain.
3. filter_redirector_recv_from_netdev(): Handle packets received from AF_PACKET in_netdev. Can forward to chardev outdev or inject into the netfilter chain.
4. Updated filter_redirector_receive_iov() to support out_netdev as an output endpoint. Added logic to skip netdev path consumption when redirector has an input endpoint (indev/in_netdev) to prevent packet loops.
5. Added netdev_rx and netdev_tx counters to query-netfilter-stats output for monitoring AF_PACKET datapath activity.
--- net/filter-mirror.c | 177 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 171 insertions(+), 6 deletions(-)
net/filter-mirror.c +171 -6
@@ -65,6 +65,11 @@ struct MirrorState {uint64_t indev_bytes;uint64_t outdev_packets;uint64_t outdev_bytes; +/* netdev replay/capture statistics for filter-redirector */ +uint64_t netdev_rx_packets; +uint64_t netdev_rx_bytes; +uint64_t netdev_tx_packets; +uint64_t netdev_tx_bytes; }; typedef struct FilterSendCo {
@@ -158,6 +163,59 @@ static int filter_send(MirrorState *s,return data.ret; } +static ssize_t filter_redirector_send_netdev_packet (MirrorState *s, +const struct iovec *iov, +int iovcnt) +{ +ssize_t size =iov_size (iov, iovcnt); + g_autofree uint8_t *buf = NULL; + +if (s->out_netfd <0 ) { +return -ENODEV ; + } +if (size >NET_BUFSIZE ) { +return -EINVAL ; + } + + buf =g_malloc (size); +iov_to_buf (iov, iovcnt,0 , buf, size); + +ssize_t ret =send (s->out_netfd, buf, size,0 ); +if (ret <0 ) { +return -errno; + } +if (ret >0 ) { + s->netdev_tx_packets++; + s->netdev_tx_bytes += ret; + } +return ret; +} +static ssize_t filter_redirector_send_chardev_iov (MirrorState *s, +const struct iovec *iov, +int iovcnt) +{ +if (!s->outdev) { +return -ENODEV ; + } + +if (!qemu_chr_fe_backend_connected (&s->chr_out)) { +return 0 ; + } + +return filter_send (s, iov, iovcnt); +} + +static ssize_t filter_redirector_send_netdev_iov (MirrorState *s, +const struct iovec *iov, +int iovcnt) +{ +if (!s->out_netdev) { +return -ENODEV ; + } + +return filter_redirector_send_netdev_packet (s, iov, iovcnt); +} +static void redirector_to_filter (NetFilterState *nf,const uint8_t *buf,int len)
@@ -230,6 +288,75 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event) } } +static void filter_redirector_recv_from_chardev (NetFilterState *nf, +const uint8_t *buf, +int len) +{ +MirrorState *s =FILTER_REDIRECTOR (nf); +ssize_t ret; +struct iovec iov = { + .iov_base = (void *)buf, + .iov_len = len, + }; + +if (len <=0 ) { +return ; + } + +/* chardev indev */ + s->indev_packets++; + s->indev_bytes += len; + +if (s->out_netdev) { + ret =filter_redirector_send_netdev_iov (s, &iov,1 ); +if (ret <0 ) { +error_report ("filter redirector send failed(%s)" ,strerror (-ret)); + } +return ; + } + +if (s->outdev) { + ret =filter_redirector_send_chardev_iov (s, &iov,1 ); +if (ret <0 ) { +error_report ("filter redirector send failed(%s)" ,strerror (-ret)); + }else if (ret >0 ) { + s->outdev_packets++; + s->outdev_bytes += ret; + } +return ; + } + +redirector_to_filter (nf, buf, len); +} + +static bool filter_redirector_recv_from_netdev (NetFilterState *nf, +const uint8_t *buf, +int len) +{ +MirrorState *s =FILTER_REDIRECTOR (nf); +ssize_t ret; +struct iovec iov = { + .iov_base = (void *)buf, + .iov_len = len, + }; + +if (len <=0 ) { +return false; + } +if (s->outdev) { + ret =filter_redirector_send_chardev_iov (s, &iov,1 ); + }else { +redirector_to_filter (nf, buf, len); +return true; + } + +if (ret <0 ) { +error_report ("filter redirector send failed(%s)" ,strerror (-ret)); +return false; + } +return true; +} +static void filter_redirector_netdev_read (void *opaque) {NetFilterState *nf = opaque;
@@ -254,7 +381,9 @@ static void filter_redirector_netdev_read(void *opaque)continue ; } -redirector_to_filter (nf, s->in_netbuf, len); + s->netdev_rx_packets++; + s->netdev_rx_bytes += len; +filter_redirector_recv_from_netdev (nf, s->in_netbuf, len); } if (len < 0 && errno != EAGAIN && errno != EWOULDBLOCK &&
@@ -296,19 +425,33 @@ static ssize_t filter_redirector_receive_iov(NetFilterState *nf,MirrorState *s =FILTER_REDIRECTOR (nf);int ret; -if (qemu_chr_fe_backend_connected (&s->chr_out)) { - ret =filter_send (s, iov, iovcnt); +/* +* If this redirector has an explicit input endpoint (indev/in_netdev), +* it acts as an injector for that endpoint and must not consume packets +* from the regular netdev data path. Consuming here can create loops when +* out_netdev points back to the same TAP netdev. +*/ +if (s->indev || s->in_netdev) { +return 0 ; + } + +if (s->out_netdev || s->outdev) { +if (s->out_netdev) { + ret =filter_redirector_send_netdev_iov (s, iov, iovcnt); + }else { + ret =filter_redirector_send_chardev_iov (s, iov, iovcnt); + }if (ret <0 ) {error_report ("filter redirector send failed(%s)" ,strerror (-ret)); - }else if (ret >0 ) { + }else if (ret >0 && !s->out_netdev) {/* Update outdev statistics on successful send */ s->outdev_packets++; s->outdev_bytes += ret; }return iov_size (iov, iovcnt); - }else { -return 0 ; } + +return 0 ; }static void filter_mirror_cleanup (NetFilterState *nf)
@@ -369,6 +512,16 @@ static void redirector_rs_finalize(SocketReadState *rs)MirrorState *s =container_of (rs, MirrorState, rs);NetFilterState *nf =NETFILTER (s); +/* +* If redirector has an explicit output endpoint, keep the redirect path +* (e.g. indev=red0 -> out_netdev=net0). +* Fallback to direct netfilter injection only when no output is set. +*/ +if (s->outdev || s->out_netdev) { +filter_redirector_recv_from_chardev (nf, rs->buf, rs->packet_len); +return ; + } +/* Update indev statistics */ s->indev_packets++; s->indev_bytes += rs->packet_len;
@@ -826,6 +979,18 @@ static GList *filter_redirector_get_stats(NetFilterState *nf) counter->bytes = s->outdev_bytes; list =g_list_append (list, counter); + counter =g_new0 (NetFilterCounter,1 ); + counter->name =g_strdup ("netdev_rx" ); + counter->packets = s->netdev_rx_packets; + counter->bytes = s->netdev_rx_bytes; + list =g_list_append (list, counter); + + counter =g_new0 (NetFilterCounter,1 ); + counter->name =g_strdup ("netdev_tx" ); + counter->packets = s->netdev_tx_packets; + counter->bytes = s->netdev_tx_bytes; + list =g_list_append (list, counter); +return list; }
Signature · 1 line
2.52.0
RFC 5/5 qapi: add in/out_netdev to filter-redirector
In reply to Cindy LuDocument the new in_netdev and out_netdev properties in the QAPI schema for FilterRedirectorProperties:
- in_netdev: Name of an input netdev endpoint captured via AF_PACKET. Allows capturing packets directly from a TAP interface, bypassing the QEMU network stack.
- out_netdev: Name of an output netdev endpoint injected via AF_PACKET. Allows injecting packets directly to a TAP interface.
These properties are mutually exclusive with each other and with indev/outdev on the same direction. They enable filter-redirector to work with vhost-net backends for live migration packet mirroring.
--- qapi/qom.json | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
qapi/qom.json +17 -2
@@ -476,8 +476,15 @@ # # Properties for filter-redirector objects. # -# At least one of @indev or @outdev must be present. If both are -# present, they must not refer to the same character device backend. +# At least one of @indev, @outdev, @in_netdev, or @out_netdev must be +# present. +# +# If both @indev and @outdev are present, they must not refer to the +# same character device backend. +# +# @in_netdev and @out_netdev are Linux AF_PACKET based endpoints. +# They are mutually exclusive with each other, and also with +# @indev/@outdev on the same direction. # # @indev: the name of a character device backend from which packets # are received and redirected to the filtered network device
@@ -485,6 +492,12 @@ # @outdev: the name of a character device backend to which all # incoming packets are redirected # +# @in_netdev: the name of an input netdev endpoint captured via +# AF_PACKET +# +# @out_netdev: the name of an output netdev endpoint injected via +# AF_PACKET +# # @vnet_hdr_support: if true, vnet header support is enabled # (default: false) #
@@ -498,6 +511,8 @@ 'base': 'NetfilterProperties', 'data': { '*indev': 'str', '*outdev': 'str', + '*in_netdev': 'str', + '*out_netdev': 'str', '*vnet_hdr_support': 'bool', '*enable_when_stopped': 'bool' } }
Signature · 1 line
2.52.0
Quoted 4 lines · Cindy Lu
This series extends filter-redirector to support vhost-net backends by introducing
AF_PACKETbased packet capture and injection.
Very happy to see this series~
Quoted 11 lines
TODO
========
This series introduces basic support for filter-redirector, enabling simple mirroring of VM1's ingress traffic to VM2's mirror port. Additional functionality includingfilter_bufferand other filter features will be added in following versions.Example Usage
=============
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,in_netdev=net0,outdev=mirror0Secondary VM (receive mirrored packets):
-chardev socket,id=red0,host=...,port=...,reconnect-ms=1000 -object filter-redirector,id=vm2redir,netdev=net0,indev=red0,out_netdev=net0
Whether the receive side need the same setup the "-netdev tap,id=net0,vhost=on" or not?
Thanks
Chen
Quoted 20 lines
Testing
=======
- Tested with vhost=on/off TAP netdev onx86_64Cindy Lu (5):
net/filter: Allow redirector on vhost backendsnet/filter-redirector: add in/out_netdev propertiesnet/filter-redirector: AddAF_PACKETsockets initializationnet/filter-redirector: addAF_PACKETredirect datapath
qapi: add in/out_netdev to filter-redirectornet/filter-mirror.c | 460 +++++++++++++++++++++++++++++++++++++++++--- net/filter.c | 6 +- qapi/qom.json | 19 +- 3 files changed, 459 insertions(+), 26 deletions(-)--
2.52.0
Quoted 9 lines · Zhang Chen
Quoted 4 lines · Cindy Lu
This series extends filter-redirector to support vhost-net backends by introducing
AF_PACKETbased packet capture and injection.Very happy to see this series~
thanks :)
Quoted 17 lines
Quoted 17 lines
TODO
========
This series introduces basic support for filter-redirector, enabling simple mirroring of VM1's ingress traffic to VM2's mirror port. Additional functionality includingfilter_bufferand other filter features will be added in following versions.Example Usage
=============
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,in_netdev=net0,outdev=mirror0Secondary VM (receive mirrored packets):
Quoted 9 lines
Quoted 2 lines
-chardev socket,id=red0,host=...,port=...,reconnect-ms=1000 -object filter-redirector,id=vm2redir,netdev=net0,indev=red0,out_netdev=net0
Whether the receive side need the same setup the "-netdev tap,id=net0,vhost=on" or not?
Thanks
Chen
Hi Chen,
Yes, the receive side still requires the -netdev tap,id=net0,vhost=on setup. I will make this clearer in the next version of the series.
Best regards,
Cindy Lu
Quoted 21 lines
Quoted 20 lines
Testing
=======
- Tested with vhost=on/off TAP netdev onx86_64Cindy Lu (5):
net/filter: Allow redirector on vhost backendsnet/filter-redirector: add in/out_netdev propertiesnet/filter-redirector: AddAF_PACKETsockets initializationnet/filter-redirector: addAF_PACKETredirect datapath
qapi: add in/out_netdev to filter-redirectornet/filter-mirror.c | 460 +++++++++++++++++++++++++++++++++++++++++--- net/filter.c | 6 +- qapi/qom.json | 19 +- 3 files changed, 459 insertions(+), 26 deletions(-)--
2.52.0