Introduce filter-redirector
v2 cover Introduce filter-redirector
Filter-redirector is a netfilter plugin.
It gives qemu the ability to redirect net packet. redirector can redirect filter's net packet to outdev. and redirect indev's packet to filter.
filter
+
|
|
redirector |
+-------------------------+
| | |
| | |
| | |
indev +----------------+ +----------------> outdev
| | |
| | |
| | |
+-------------------------+
|
|
v
filter
|
|
v
filter------> guestv2:
- Address Jason's comments.
- Add filter-traffic.h to reuse parts of the codes
- Add unit test case
v1:
initial patch.
Zhang Chen (3):
net/filter-traffic: add filter-traffic.h net/filter-redirector:Add filter-redirector tests/test-filter-redirector: Add unit test for filter-redirector
net/Makefile.objs | 1 + net/filter-mirror.c | 70 ++----------- net/filter-redirector.c | 216 +++++++++++++++++++++++++++++++++++++++++ net/filter-traffic.h | 71 ++++++++++++++ qemu-options.hx | 8 ++ tests/.gitignore | 1 + tests/Makefile | 2 + tests/test-filter-redirector.c | 97 ++++++++++++++++++ vl.c | 3 +- 9 files changed, 407 insertions(+), 62 deletions(-) create mode 100644 net/filter-redirector.c create mode 100644 net/filter-traffic.h create mode 100644 tests/test-filter-redirector.c
Signature · 1 line
1.9.1
PATCH v2 1/3 net/filter-traffic: add filter-traffic.h
We can reuse filter-traffic by filter-mirror, filter-redirector and so on.
--- net/filter-mirror.c | 70 +++++++-------------------------------------------- net/filter-traffic.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 61 deletions(-) create mode 100644 net/filter-traffic.h
net/filter-mirror.c +9 -61
@@ -9,65 +9,13 @@* later. See the COPYING file in the top-level directory. */ -#include "net/filter.h" -#include "net/net.h" -#include "qemu-common.h" -#include "qapi/qmp/qerror.h" -#include "qapi-visit.h" -#include "qom/object.h" -#include "qemu/main-loop.h" -#include "qemu/error-report.h" -#include "trace.h" -#include "sysemu/char.h" -#include "qemu/iov.h" -#include "qemu/sockets.h" +#include "net/filter-traffic.h" #define FILTER_MIRROR (obj) \ - OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_MIRROR)#define TYPE_FILTER_MIRROR "filter-mirror" -typedef struct MirrorState { -NetFilterState parent_obj; -char *outdev; -CharDriverState *chr_out; -}MirrorState ; - -static int filter_mirror_send (NetFilterState *nf, -const struct iovec *iov, -int iovcnt) -{ -MirrorState *s =FILTER_MIRROR (nf); -int ret =0 ; -ssize_t size =0 ; -uint32_t len =0 ; -char *buf; - - size =iov_size (iov, iovcnt); -if (!size) { -return 0 ; - } - - len =htonl (size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); -if (ret !=sizeof (len)) { - goto err; - } - - buf =g_malloc (size); -iov_to_buf (iov, iovcnt,0 , buf, size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); -g_free (buf); -if (ret != size) { - goto err; - } - -return 0 ; - -err: -return ret <0 ? ret : -EIO ; -} - static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,
@@ -77,7 +25,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, {int ret; - ret =filter_mirror_send (nf, iov, iovcnt); + ret =filter_traffic_send (nf, iov, iovcnt);if (ret) {error_report ("filter_mirror_send failed(%s)" ,strerror (-ret)); }
@@ -91,7 +39,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,static void filter_mirror_cleanup (NetFilterState *nf) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (s->chr_out) {qemu_chr_fe_release (s->chr_out);
@@ -100,7 +48,7 @@ static void filter_mirror_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (!s->outdev) { error_setg(errp, "filter filter mirror needs 'outdev' "
@@ -132,7 +80,7 @@ static void filter_mirror_class_init(ObjectClass *oc, void *data)static char *filter_mirror_get_outdev (Object *obj,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);return g_strdup (s->outdev); }
@@ -140,7 +88,7 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)static void filter_mirror_set_outdev (Object *obj,const char *value,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); s->outdev =g_strdup (value);
@@ -159,7 +107,7 @@ static void filter_mirror_init(Object *obj)static void filter_mirror_fini (Object *obj) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); }
@@ -170,7 +118,7 @@ static const TypeInfo filter_mirror_info = { .class_init = filter_mirror_class_init, .instance_init = filter_mirror_init, .instance_finalize = filter_mirror_fini, - .instance_size = sizeof(MirrorState), + .instance_size = sizeof(TrafficState), };static void register_types (void )
net/filter-traffic.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 3 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror, filter-redirector and so on.
Is the main purpose to export filter_traffic_send() ? IMO, define it as a public API would be better.
Using MirrorState and RedirectorState are clearer rather than TrafficState.
Quoted 165 lines
Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-mirror.c | 70 +++++++-------------------------------------------- net/filter-traffic.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 61 deletions(-)create mode 100644
net/filter-traffic.hnet/filter-mirror.c +9
-61@@ -9,65 +9,13 @@* later. See the COPYING file in the top-level directory. */ -#include "net/filter.h" -#include "net/net.h" -#include "qemu-common.h" -#include "qapi/qmp/qerror.h" -#include "qapi-visit.h" -#include "qom/object.h" -#include "qemu/main-loop.h" -#include "qemu/error-report.h" -#include "trace.h" -#include "sysemu/char.h" -#include "qemu/iov.h" -#include "qemu/sockets.h" +#include "net/filter-traffic.h" #define FILTER_MIRROR (obj) \ - OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_MIRROR)#define TYPE_FILTER_MIRROR "filter-mirror" -typedef struct MirrorState { -NetFilterState parent_obj; -char *outdev; -CharDriverState *chr_out; -}MirrorState ; - -static int filter_mirror_send (NetFilterState *nf, -const struct iovec *iov, -int iovcnt) -{ -MirrorState *s =FILTER_MIRROR (nf); -int ret =0 ; -ssize_t size =0 ; -uint32_t len =0 ; -char *buf; - - size =iov_size (iov, iovcnt); -if (!size) { -return 0 ; - } - - len =htonl (size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); -if (ret !=sizeof (len)) { - goto err; - } - - buf =g_malloc (size); -iov_to_buf (iov, iovcnt,0 , buf, size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); -g_free (buf); -if (ret != size) { - goto err; - } - -return 0 ; - -err: -return ret <0 ? ret : -EIO ; -} - static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -77,7 +25,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, {int ret; - ret =filter_mirror_send (nf, iov, iovcnt); + ret =filter_traffic_send (nf, iov, iovcnt);if (ret) {error_report ("filter_mirror_send failed(%s)" ,strerror (-ret)); }@@ -91,7 +39,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,static void filter_mirror_cleanup (NetFilterState *nf) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (s->chr_out) {qemu_chr_fe_release (s->chr_out);@@ -100,7 +48,7 @@ static void filter_mirror_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (!s->outdev) { error_setg(errp, "filter filter mirror needs 'outdev' "@@ -132,7 +80,7 @@ static void filter_mirror_class_init(ObjectClass *oc, void *data)static char *filter_mirror_get_outdev (Object *obj,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);return g_strdup (s->outdev); }@@ -140,7 +88,7 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)static void filter_mirror_set_outdev (Object *obj,const char *value,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); s->outdev =g_strdup (value);@@ -159,7 +107,7 @@ static void filter_mirror_init(Object *obj)static void filter_mirror_fini (Object *obj) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); }@@ -170,7 +118,7 @@ static const TypeInfo filter_mirror_info = { .class_init = filter_mirror_class_init, .instance_init = filter_mirror_init, .instance_finalize = filter_mirror_fini, - .instance_size = sizeof(MirrorState), + .instance_size = sizeof(TrafficState), };static void register_types (void )net/filter-traffic.h new file +16
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h"
net/filter-traffic.h line 26
Quoted 10 lines
net/filter-traffic.h new file +10
-0+#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "trace.h" +#include "sysemu/char.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" + +#ifndef QEMU_FILTER_MIRROR_H +#define QEMU_FILTER_MIRROR_H
I prefer to see this is the first line of a C header code. s/MIRROR/TRAFFIC/
Quoted 5 lines
net/filter-traffic.h new file +5
-0+ +typedef struct TrafficState { + NetFilterState parent_obj; + NetQueue *incoming_queue;/* guest normal net queue */ + char *indev;
net/filter-traffic.h line 41
Quoted 10 lines
net/filter-traffic.h new file +10
-0+char *outdev; +CharDriverState *chr_in; +CharDriverState *chr_out; +} TrafficState; + +static int filter_traffic_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +TrafficState *s = (TrafficState *)nf;
that's dangerous.
Quoted 21 lines
net/filter-traffic.h new file +20
-0+int ret =0 ; +ssize_t size =0 ; +uint32_t len =0 ; +char *buf; + size =iov_size (iov, iovcnt); +if (!size) { +return 0 ; + } + + len =htonl (size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); +if (ret !=sizeof (len)) { + goto err; + } + + buf =g_malloc (size); +iov_to_buf (iov, iovcnt,0 , buf, size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); +g_free (buf); +if (ret != size) {
net/filter-traffic.h line 71
Quoted 10 lines
net/filter-traffic.h new file +10
-0+ goto err; + } + +return 0 ; + +err: +return ret <0 ? ret : -EIO ; +} + +#endif /* QEMU_FILTER_MIRROR_H */
s/MIRROR/TRAFFIC/
Thanks
Li Zhijian
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 12 lines · Li Zhijian
Quoted 3 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror, filter-redirector and so on.
Is the main purpose to export
filter_traffic_send()? IMO, define it as a public API would be better.Using MirrorState and RedirectorState are clearer rather than TrafficState.
OK, I will fix it in next version.
Quoted 176 lines
Quoted 176 lines
Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-mirror.c | 70 +++++++-------------------------------------------- net/filter-traffic.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 61 deletions(-)create mode 100644
net/filter-traffic.hnet/filter-mirror.c +2
-54@@ -9,65 +9,13 @@* later. See the COPYING file in the top-level directory. */ -#include "net/filter.h" -#include "net/net.h" -#include "qemu-common.h" -#include "qapi/qmp/qerror.h" -#include "qapi-visit.h" -#include "qom/object.h" -#include "qemu/main-loop.h" -#include "qemu/error-report.h" -#include "trace.h" -#include "sysemu/char.h" -#include "qemu/iov.h" -#include "qemu/sockets.h" +#include "net/filter-traffic.h" #define FILTER_MIRROR (obj) \ - OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_MIRROR)#define TYPE_FILTER_MIRROR "filter-mirror" -typedef struct MirrorState { -NetFilterState parent_obj; -char *outdev; -CharDriverState *chr_out; -}MirrorState ; - -static int filter_mirror_send (NetFilterState *nf, -const struct iovec *iov, -int iovcnt) -{ -MirrorState *s =FILTER_MIRROR (nf); -int ret =0 ; -ssize_t size =0 ; -uint32_t len =0 ; -char *buf; - - size =iov_size (iov, iovcnt); -if (!size) { -return 0 ; - } - - len =htonl (size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len, sizeof(len)); -if (ret !=sizeof (len)) { - goto err; - } - - buf =g_malloc (size); -iov_to_buf (iov, iovcnt,0 , buf, size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); -g_free (buf); -if (ret != size) { - goto err; - } - -return 0 ; - -err: -return ret <0 ? ret : -EIO ; -} - static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender,unsigned flags,
net/filter-mirror.c +1
-1@@ -77,7 +25,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, { int ret; - ret = filter_mirror_send(nf, iov, iovcnt); + ret = filter_traffic_send(nf, iov, iovcnt); if (ret) { error_report("filter_mirror_send failed(%s)", strerror(-ret));}net/filter-mirror.c +1
-1@@ -91,7 +39,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, static void filter_mirror_cleanup(NetFilterState *nf) { - MirrorState *s = FILTER_MIRROR(nf); + TrafficState *s = FILTER_MIRROR(nf); if (s->chr_out) {qemu_chr_fe_release(s->chr_out);net/filter-mirror.c +2
-2@@ -100,7 +48,7 @@ static void filter_mirror_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (!s->outdev) { error_setg(errp, "filter filter mirror needs 'outdev' "@@ -132,7 +80,7 @@ static void filter_mirror_class_init(ObjectClass *oc, void *data)static char *filter_mirror_get_outdev (Object *obj,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);return g_strdup (s->outdev);}net/filter-mirror.c +1
-1@@ -140,7 +88,7 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)static void filter_mirror_set_outdev (Object *obj,const char *value,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev);s->outdev = g_strdup(value);net/filter-mirror.c +2
-2@@ -159,7 +107,7 @@ static void filter_mirror_init(Object *obj)static void filter_mirror_fini (Object *obj) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); }@@ -170,7 +118,7 @@ static const TypeInfo filter_mirror_info = { .class_init = filter_mirror_class_init, .instance_init = filter_mirror_init, .instance_finalize = filter_mirror_fini, - .instance_size = sizeof(MirrorState), + .instance_size = sizeof(TrafficState), };static void register_types (void )net/filter-traffic.h new file +20
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "trace.h"
Quoted 10 lines
net/filter-traffic.h line 26
Quoted 6 lines
net/filter-traffic.h new file +6
-0+#include "sysemu/char.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" + +#ifndef QEMU_FILTER_MIRROR_H +#define QEMU_FILTER_MIRROR_H I prefer to see this is the first line of a C header code. s/MIRROR/TRAFFIC/
Thanks, will fix it.
Quoted 9 lines
Quoted 9 lines
net/filter-traffic.h new file +9
-0+ +typedef struct TrafficState { +NetFilterState parent_obj; +NetQueue *incoming_queue;/* guest normal net queue */ +char *indev; +char *outdev; +CharDriverState *chr_in; +CharDriverState *chr_out; +}TrafficState ;
Quoted 9 lines
net/filter-traffic.h line 41
Quoted 6 lines
net/filter-traffic.h new file +6
-0+ +static int filter_traffic_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +TrafficState *s = (TrafficState *)nf;that's dangerous.
will fix.
Quoted 26 lines
Quoted 26 lines
net/filter-traffic.h new file +11
-0+int ret =0 ; +ssize_t size =0 ; +uint32_t len =0 ; +char *buf; + size =iov_size (iov, iovcnt); +if (!size) { +return 0 ; + } + + len =htonl (size); + ret = qemu_chr_fe_write_all(s->chr_out, (uint8_t *)&len,sizeof(len));
net/filter-traffic.h new file +13
-0+if (ret !=sizeof (len)) { + goto err; + } + + buf =g_malloc (size); +iov_to_buf (iov, iovcnt,0 , buf, size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); +g_free (buf); +if (ret != size) { + goto err; + } + +return 0 ;
Quoted 9 lines
net/filter-traffic.h line 71
Quoted 7 lines
net/filter-traffic.h new file +6
-0+ +err: + return ret < 0 ? ret : -EIO; +} + +#endif /* QEMU_FILTER_MIRROR_H */ s/MIRROR/TRAFFIC/
will fix
Thanks
zhangchen
Quoted 4 lines
Thanks
Li Zhijian
.
Signature · 2 lines
Thanks zhangchen
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
In reply to Zhang ChenQuoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror, filter-redirector and so on.
I think we could share more than this. E.g just use filter-mirror.c to implement both mirror and redirector.
Quoted 170 lines
Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-mirror.c | 70 +++++++-------------------------------------------- net/filter-traffic.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 61 deletions(-) create mode 100644 net/filter-traffic.hnet/filter-mirror.c +9
-61@@ -9,65 +9,13 @@* later. See the COPYING file in the top-level directory. */ -#include "net/filter.h" -#include "net/net.h" -#include "qemu-common.h" -#include "qapi/qmp/qerror.h" -#include "qapi-visit.h" -#include "qom/object.h" -#include "qemu/main-loop.h" -#include "qemu/error-report.h" -#include "trace.h" -#include "sysemu/char.h" -#include "qemu/iov.h" -#include "qemu/sockets.h" +#include "net/filter-traffic.h" #define FILTER_MIRROR (obj) \ - OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_MIRROR)#define TYPE_FILTER_MIRROR "filter-mirror" -typedef struct MirrorState { -NetFilterState parent_obj; -char *outdev; -CharDriverState *chr_out; -}MirrorState ; - -static int filter_mirror_send (NetFilterState *nf, -const struct iovec *iov, -int iovcnt) -{ -MirrorState *s =FILTER_MIRROR (nf); -int ret =0 ; -ssize_t size =0 ; -uint32_t len =0 ; -char *buf; - - size =iov_size (iov, iovcnt); -if (!size) { -return 0 ; - } - - len =htonl (size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); -if (ret !=sizeof (len)) { - goto err; - } - - buf =g_malloc (size); -iov_to_buf (iov, iovcnt,0 , buf, size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); -g_free (buf); -if (ret != size) { - goto err; - } - -return 0 ; - -err: -return ret <0 ? ret : -EIO ; -} - static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -77,7 +25,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, {int ret; - ret =filter_mirror_send (nf, iov, iovcnt); + ret =filter_traffic_send (nf, iov, iovcnt);if (ret) {error_report ("filter_mirror_send failed(%s)" ,strerror (-ret)); }@@ -91,7 +39,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,static void filter_mirror_cleanup (NetFilterState *nf) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (s->chr_out) {qemu_chr_fe_release (s->chr_out);@@ -100,7 +48,7 @@ static void filter_mirror_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (!s->outdev) { error_setg(errp, "filter filter mirror needs 'outdev' "@@ -132,7 +80,7 @@ static void filter_mirror_class_init(ObjectClass *oc, void *data)static char *filter_mirror_get_outdev (Object *obj,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);return g_strdup (s->outdev); }@@ -140,7 +88,7 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)static void filter_mirror_set_outdev (Object *obj,const char *value,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); s->outdev =g_strdup (value);@@ -159,7 +107,7 @@ static void filter_mirror_init(Object *obj)static void filter_mirror_fini (Object *obj) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); }@@ -170,7 +118,7 @@ static const TypeInfo filter_mirror_info = { .class_init = filter_mirror_class_init, .instance_init = filter_mirror_init, .instance_finalize = filter_mirror_fini, - .instance_size = sizeof(MirrorState), + .instance_size = sizeof(TrafficState), };static void register_types (void )net/filter-traffic.h new file +20
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "trace.h"
net/filter-traffic.h line 30
Quoted 10 lines
net/filter-traffic.h new file +10
-0+#include "sysemu/char.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" + +#ifndef QEMU_FILTER_MIRROR_H +#define QEMU_FILTER_MIRROR_H + +typedef struct TrafficState { + NetFilterState parent_obj; + NetQueue *incoming_queue;/* guest normal net queue */
The comment is wrong here, guest nic net queue should be incoming_queue of NetClientState.
net/filter-traffic.h line 34
Quoted 4 lines
net/filter-traffic.h new file +4
-0+char *indev; +char *outdev; +CharDriverState *chr_in; +CharDriverState *chr_out;
We could just extend current MirrorState.
Quoted 37 lines
net/filter-traffic.h new file +37
-0+} TrafficState; + +static int filter_traffic_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +TrafficState *s = (TrafficState *)nf; +int ret =0 ; +ssize_t size =0 ; +uint32_t len =0 ; +char *buf; + size =iov_size (iov, iovcnt); +if (!size) { +return 0 ; + } + + len =htonl (size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); +if (ret !=sizeof (len)) { + goto err; + } + + buf =g_malloc (size); +iov_to_buf (iov, iovcnt,0 , buf, size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); +g_free (buf); +if (ret != size) { + goto err; + } + +return 0 ; + +err: +return ret <0 ? ret : -EIO ; +} + +#endif /* QEMU_FILTER_MIRROR_H */
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 6 lines · Jason Wang
Quoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror, filter-redirector and so on.
I think we could share more than this. E.g just use
filter-mirror.cto implement both mirror and redirector.
OK, should we change the name to filter-mirror? like filter-traffic? and add redirect=on/off
-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,indev=redirect0,redirect=on,outdev=mirror0
Quoted 174 lines
Quoted 174 lines
Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-mirror.c | 70 +++++++-------------------------------------------- net/filter-traffic.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 61 deletions(-)create mode 100644
net/filter-traffic.hnet/filter-mirror.c +9
-61@@ -9,65 +9,13 @@* later. See the COPYING file in the top-level directory. */ -#include "net/filter.h" -#include "net/net.h" -#include "qemu-common.h" -#include "qapi/qmp/qerror.h" -#include "qapi-visit.h" -#include "qom/object.h" -#include "qemu/main-loop.h" -#include "qemu/error-report.h" -#include "trace.h" -#include "sysemu/char.h" -#include "qemu/iov.h" -#include "qemu/sockets.h" +#include "net/filter-traffic.h" #define FILTER_MIRROR (obj) \ - OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_MIRROR)#define TYPE_FILTER_MIRROR "filter-mirror" -typedef struct MirrorState { -NetFilterState parent_obj; -char *outdev; -CharDriverState *chr_out; -}MirrorState ; - -static int filter_mirror_send (NetFilterState *nf, -const struct iovec *iov, -int iovcnt) -{ -MirrorState *s =FILTER_MIRROR (nf); -int ret =0 ; -ssize_t size =0 ; -uint32_t len =0 ; -char *buf; - - size =iov_size (iov, iovcnt); -if (!size) { -return 0 ; - } - - len =htonl (size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); -if (ret !=sizeof (len)) { - goto err; - } - - buf =g_malloc (size); -iov_to_buf (iov, iovcnt,0 , buf, size); - ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); -g_free (buf); -if (ret != size) { - goto err; - } - -return 0 ; - -err: -return ret <0 ? ret : -EIO ; -} - static ssize_t filter_mirror_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -77,7 +25,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf, {int ret; - ret =filter_mirror_send (nf, iov, iovcnt); + ret =filter_traffic_send (nf, iov, iovcnt);if (ret) {error_report ("filter_mirror_send failed(%s)" ,strerror (-ret)); }@@ -91,7 +39,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,static void filter_mirror_cleanup (NetFilterState *nf) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (s->chr_out) {qemu_chr_fe_release (s->chr_out);@@ -100,7 +48,7 @@ static void filter_mirror_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) { -MirrorState *s =FILTER_MIRROR (nf); +TrafficState *s =FILTER_MIRROR (nf);if (!s->outdev) { error_setg(errp, "filter filter mirror needs 'outdev' "@@ -132,7 +80,7 @@ static void filter_mirror_class_init(ObjectClass *oc, void *data)static char *filter_mirror_get_outdev (Object *obj,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);return g_strdup (s->outdev); }@@ -140,7 +88,7 @@ static char *filter_mirror_get_outdev(Object *obj, Error **errp)static void filter_mirror_set_outdev (Object *obj,const char *value,Error **errp) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); s->outdev =g_strdup (value);@@ -159,7 +107,7 @@ static void filter_mirror_init(Object *obj)static void filter_mirror_fini (Object *obj) { -MirrorState *s =FILTER_MIRROR (obj); +TrafficState *s =FILTER_MIRROR (obj);g_free (s->outdev); }@@ -170,7 +118,7 @@ static const TypeInfo filter_mirror_info = { .class_init = filter_mirror_class_init, .instance_init = filter_mirror_init, .instance_finalize = filter_mirror_fini, - .instance_size = sizeof(MirrorState), + .instance_size = sizeof(TrafficState), };static void register_types (void )net/filter-traffic.h new file +24
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "trace.h" +#include "sysemu/char.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" +
Quoted 8 lines
net/filter-traffic.h line 30
Quoted 6 lines
net/filter-traffic.h new file +6
-0+#ifndef QEMU_FILTER_MIRROR_H +#define QEMU_FILTER_MIRROR_H + +typedef struct TrafficState { + NetFilterState parent_obj; + NetQueue *incoming_queue;/* guest normal net queue */The comment is wrong here, guest nic net queue should be
incoming_queueof NetClientState.
I will fix it next version.
Quoted 5 lines
net/filter-traffic.h line 34
Quoted 4 lines
net/filter-traffic.h new file +4
-0+char *indev; +char *outdev; +CharDriverState *chr_in; +CharDriverState *chr_out;We could just extend current MirrorState.
OK, next version will based on filter-mirror
Thanks
zhangchen
Quoted 41 lines
Quoted 27 lines
net/filter-traffic.h new file +27
-0+} TrafficState; + +static int filter_traffic_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +TrafficState *s = (TrafficState *)nf; +int ret =0 ; +ssize_t size =0 ; +uint32_t len =0 ; +char *buf; + size =iov_size (iov, iovcnt); +if (!size) { +return 0 ; + } + + len =htonl (size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)&len,sizeof (len)); +if (ret !=sizeof (len)) { + goto err; + } + + buf =g_malloc (size); +iov_to_buf (iov, iovcnt,0 , buf, size); + ret =qemu_chr_fe_write_all (s->chr_out, (uint8_t *)buf, size); +g_free (buf); +if (ret != size) {net/filter-traffic.h line 71
Quoted 10 lines
net/filter-traffic.h new file +10
-0+ goto err; + } + +return 0 ; + +err: +return ret <0 ? ret : -EIO ; +} + +#endif /* QEMU_FILTER_MIRROR_H */ .
Signature · 2 lines
Thanks zhangchen
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 7 lines · Zhang Chen
Quoted 4 lines · Jason Wang
Quoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror,
filter-redirector and so on.
Quoted 9 lines
Quoted 2 lines
I think we could share more than this. E.g just use
filter-mirror.cto implement both mirror and redirector.OK, should we change the name to filter-mirror? like filter-traffic? and add redirect=on/off
-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,indev=redirect0,redirect=on,outdev=mirror0
I think we'd better use another type for this.
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 9 lines · Jason Wang
Quoted 7 lines · Zhang Chen
Quoted 5 lines · Jason Wang
Quoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror,
filter-redirector and so on.I think we could share more than this. E.g just use
filter-mirror.cto implement both mirror and redirector.
Quoted 8 lines
Quoted 6 lines
OK, should we change the name to filter-mirror? like filter-traffic? and add redirect=on/off
-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,indev=redirect0,redirect=on,outdev=mirror0
I think we'd better use another type for this.
What is the "another type " ?
please tell me the detail for this.
Thanks
zhangchen
Quoted 2 lines
Signature · 2 lines
Thanks zhangchen
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 19 lines · Zhang Chen
Quoted 16 lines · Jason Wang
Quoted 14 lines · Zhang Chen
Quoted 5 lines · Jason Wang
Quoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror,
filter-redirector and so on.I think we could share more than this. E.g just use
filter-mirror.cto implement both mirror and redirector.OK, should we change the name to filter-mirror? like filter-traffic? and add redirect=on/off
-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,indev=redirect0,redirect=on,outdev=mirror0
Quoted 5 lines
Quoted 2 lines
I think we'd better use another type for this.
What is the "another type " ?
please tell me the detail for this.
E.g. register another type like "filter-redirector" in reister_types() of filter-mirror.c.
Thanks
Quoted 7 lines
Thanks
zhangchenQuoted 2 lines
Re: [Qemu-devel] [PATCH V2 1/3] net/filter-traffic: add filter-traffic.h
Quoted 18 lines · Jason Wang
Quoted 16 lines · Zhang Chen
Quoted 14 lines · Jason Wang
Quoted 13 lines · Zhang Chen
Quoted 5 lines · Jason Wang
Quoted 2 lines · Zhang Chen
We can reuse filter-traffic by filter-mirror,
filter-redirector and so on.I think we could share more than this. E.g just use
filter-mirror.cto implement both mirror and redirector.OK, should we change the name to filter-mirror? like filter-traffic? and add redirect=on/off
-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,indev=redirect0,redirect=on,outdev=mirror0
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I think we'd better use another type for this.
What is the "another type " ?
please tell me the detail for this.E.g. register another type like "filter-redirector" in
reister_types()offilter-mirror.c.Thanks
OK, I get it. will change it in V3
Quoted 8 lines
Quoted 4 lines
Thanks
zhangchenQuoted 1 line
Signature · 2 lines
Thanks zhangchen
PATCH v2 2/3 net/filter-redirector:Add filter-redirector
In reply to Zhang ChenFilter-redirector is a netfilter plugin.
It gives qemu the ability to redirect net packet. redirector can redirect filter's net packet to outdev. and redirect indev's packet to filter.
filter
+
|
|
redirector |
+-------------------------+
| | |
| | |
| | |
indev +----------------+ +----------------> outdev
| | |
| | |
| | |
+-------------------------+
|
|
v
filter
|
|
v
filter------> guestusage:
-netdev tap,id=hn0
-chardev socket,id=s0,host=ip_primary,port=X,server,nowait -chardev socket,id=s1,host=ip_primary,port=Y,server,nowait -filter-redirector,id=r0,netdev=hn0,queue=tx/rx/all,indev=s0,outdev=s1
--- net/Makefile.objs | 1 + net/filter-redirector.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 8 ++ vl.c | 3 +- 4 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 net/filter-redirector.c
net/Makefile.objs +1 -0
@@ -16,3 +16,4 @@ common-obj-$(CONFIG_NETMAP) += netmap.o common-obj-y += filter.o common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o +common-obj-y += filter-redirector.o
net/filter-redirector.c new file +216 -0
216 lines not on this page · raw message
qemu-options.hx +8 -0
@@ -3764,6 +3764,14 @@ queue @var{all|rx|tx} is an option that can be applied to any netfilter. filter-mirror on netdev @var{netdevid},mirror net packet to chardev @var{chardevid} +@item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid}, +outdev=@var{chardevid}[,queue=@var{all|rx|tx}] + +filter-redirector on netdev @var{netdevid},redirect filter's net packet to chardev +@var{chardevid},and redirect indev's packet to filter. +Create a filter-redirector we need to differ outdev id from indev id, id can not +be the same. we can just use indev or outdev, but at least one to be set. + @item -object filter-dump,id=@var{id},netdev=@var{dev},file=@var{filename}][,maxlen=@var{len}] Dump the network traffic on netdev @var{dev} to the file specified by
vl.c +2 -1
@@ -2799,7 +2799,8 @@ static bool object_create_initial(const char *type)*/ if (g_str_equal (type,"filter-buffer" ) ||g_str_equal (type,"filter-dump" ) || -g_str_equal (type,"filter-mirror" )) { +g_str_equal (type,"filter-mirror" ) || +g_str_equal (type,"filter-redirector" )) {return false; }
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH V2 2/3] net/filter-redirector:Add filter-redirector
Quoted 130 lines · Zhang Chen
Filter-redirector is a netfilter plugin.
It gives qemu the ability to redirect net packet. redirector can redirect filter's net packet to outdev. and redirect indev's packet to filter.filter + | | redirector | +-------------------------+ | | | | | | | | | indev +----------------+ +----------------> outdev | | | | | | | | | +-------------------------+ | | v filter | | v filter------> guestusage:
(quoted diff) +0
-4-netdev tap,id=hn0 -chardev socket,id=s0,host=ip_primary,port=X,server,nowait -chardev socket,id=s1,host=ip_primary,port=Y,server,nowait -filter-redirector,id=r0,netdev=hn0,queue=tx/rx/all,indev=s0,outdev=s1Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/Makefile.objs | 1 + net/filter-redirector.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 8 ++ vl.c | 3 +- 4 files changed, 227 insertions(+), 1 deletion(-)create mode 100644
net/filter-redirector.cnet/Makefile.objs +1
-0@@ -16,3 +16,4 @@ common-obj-$(CONFIG_NETMAP) += netmap.o common-obj-y += filter.o common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o +common-obj-y += filter-redirector.onet/filter-redirector.c new file +70
-0@@ -0,0 +1,216 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter-traffic.h" + +#define FILTER_REDIRECTOR (obj) \ + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_REDIRECTOR) + +#define TYPE_FILTER_REDIRECTOR "filter-redirector" +#define REDIRECT_HEADER_LEN sizeof(uint32_t) + +static int filter_redirector_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +return filter_traffic_send (nf, iov, iovcnt); +} + +static ssize_t filter_redirector_receive_iov (NetFilterState *nf, +NetClientState *sender, +unsigned flags, +const struct iovec *iov, +int iovcnt, +NetPacketSent *sent_cb) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); +ssize_t ret =0 ; + +if (s->chr_out) { + ret =filter_redirector_send (nf, iov, iovcnt); +if (ret <0 ) { +error_report ("filter_redirector_send failed" ); + } +return iov_size (iov, iovcnt); + }else { +return 0 ; + } +} + +static void filter_redirector_cleanup (NetFilterState *nf) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); + +if (s->chr_in) { +qemu_chr_fe_release (s->chr_in); + } +if (s->chr_out) { +qemu_chr_fe_release (s->chr_out); + } +} + +static int redirector_chr_can_read (void *opaque) +{ +return REDIRECT_HEADER_LEN ; +} + +static void redirector_chr_read (void *opaque,const uint8_t *buf,int size) +{ +NetFilterState *nf = opaque; +TrafficState *s =FILTER_REDIRECTOR (nf); +uint32_t len; +int ret =0 ;
net/filter-redirector.c line 80
Quoted 10 lines
net/filter-redirector.c new file +10
-0+uint8_t *recv_buf; +int offset = size; + +while (offset <REDIRECT_HEADER_LEN ) { + ret = s->chr_in->chr_sync_read (s->chr_in, (uint8_t *)buf + offset, +REDIRECT_HEADER_LEN - offset); +if (ret ==-1 && errno ==EAGAIN ) { +g_usleep (100 ); + } + offset += ret;
if 'ret' is a nagative value, you will cut 'offset'.
Quoted 52 lines
net/filter-redirector.c new file +52
-0+ } + +memcpy (&len, buf,sizeof (len)); + len =ntohl (len); + +if (len >0 && len <NET_BUFSIZE ) { + recv_buf =g_malloc (len); + ret =qemu_chr_fe_read_all (s->chr_in, recv_buf, len); +if (ret != len) { +error_report ("filter-redirector recv buf failed" ); +g_free (recv_buf); +return ; + } + + ret =qemu_net_queue_send (s->incoming_queue, nf->netdev, +0 , (const uint8_t *)recv_buf, len,NULL ); +g_free (recv_buf); +if (ret <0 ) { +error_report ("filter-redirector out to guest failed" ); + } + }else { +error_report ("filter-redirector recv len failed" ); + } +} + +static void filter_redirector_setup (NetFilterState *nf,Error **errp) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); + +if (!s->indev && !s->outdev) { +error_setg (errp,"filter redirector needs 'indev' or " +"'outdev' at least one property set" ); +return ; + }else if (s->indev == s->outdev) { +error_setg (errp,"filter redirector needs 'indev' and " +"'outdev' are not same" ); +return ; + } + +if (s->indev) { + s->chr_in =qemu_chr_find (s->indev); +if (s->chr_in ==NULL ) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"IN Device '%s' not found" , s->indev); +return ; + } + +qemu_chr_fe_claim_no_fail (s->chr_in); +qemu_chr_add_handlers (s->chr_in, redirector_chr_can_read, + redirector_chr_read,NULL , nf); + } +
net/filter-redirector.c line 142
Quoted 10 lines
net/filter-redirector.c new file +10
-0+if (s->outdev) { + s->chr_out =qemu_chr_find (s->outdev); +if (s->chr_out ==NULL ) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"OUT Device '%s' not found" , s->outdev); +return ; + } +qemu_chr_fe_claim_no_fail (s->chr_out); + } + s->incoming_queue =qemu_new_net_queue (qemu_netfilter_pass_to_next, nf);
Does just 'indev' need an incoming_queue ?
Best regards.
Li Zhijian
Quoted 108 lines
net/filter-redirector.c new file +74
-0+} + +static void filter_redirector_class_init (ObjectClass *oc,void *data) +{ +NetFilterClass *nfc =NETFILTER_CLASS (oc); + + nfc->setup = filter_redirector_setup; + nfc->cleanup = filter_redirector_cleanup; + nfc->receive_iov = filter_redirector_receive_iov; +} + +static char *filter_redirector_get_indev (Object *obj,Error **errp) +{ +TrafficState *s =FILTER_REDIRECTOR (obj); + +return g_strdup (s->indev); +} + +static void +filter_redirector_set_indev (Object *obj,const char *value,Error **errp) +{ +TrafficState *s =FILTER_REDIRECTOR (obj); + +g_free (s->indev); + s->indev =g_strdup (value); +} + +static char *filter_redirector_get_outdev (Object *obj,Error **errp) +{ +TrafficState *s =FILTER_REDIRECTOR (obj); + +return g_strdup (s->outdev); +} + +static void +filter_redirector_set_outdev (Object *obj,const char *value,Error **errp) +{ +TrafficState *s =FILTER_REDIRECTOR (obj); + +g_free (s->outdev); + s->outdev =g_strdup (value); +} + +static void filter_redirector_init (Object *obj) +{ +object_property_add_str (obj,"indev" , filter_redirector_get_indev, + filter_redirector_set_indev,NULL ); +object_property_add_str (obj,"outdev" , filter_redirector_get_outdev, + filter_redirector_set_outdev,NULL ); +} + +static void filter_redirector_fini (Object *obj) +{ +TrafficState *s =FILTER_REDIRECTOR (obj); + +g_free (s->indev); +g_free (s->outdev); +} + +static const TypeInfo filter_redirector_info = { + .name =TYPE_FILTER_REDIRECTOR , + .parent =TYPE_NETFILTER , + .class_init = filter_redirector_class_init, + .instance_init = filter_redirector_init, + .instance_finalize = filter_redirector_fini, + .instance_size =sizeof (TrafficState), +}; + +static void register_types (void ) +{ +type_register_static (&filter_redirector_info); +} + +type_init (register_types);qemu-options.hx +8
-0@@ -3764,6 +3764,14 @@ queue @var{all|rx|tx} is an option that can be applied to any netfilter. filter-mirror on netdev @var{netdevid},mirror net packet to chardev @var{chardevid} +@item -object filter-redirector,id=@var{id},netdev=@var{netdevid},indev=@var{chardevid}, +outdev=@var{chardevid}[,queue=@var{all|rx|tx}] + +filter-redirector on netdev @var{netdevid},redirect filter's net packet to chardev +@var{chardevid},and redirect indev's packet to filter. +Create a filter-redirector we need to differ outdev id from indev id, id can not +be the same. we can just use indev or outdev, but at least one to be set. + @item -object filter-dump,id=@var{id},netdev=@var{dev},file=@var{filename}][,maxlen=@var{len}] Dump the network traffic on netdev @var{dev} to the file specified byvl.c +2
-1@@ -2799,7 +2799,8 @@ static bool object_create_initial(const char *type)*/ if (g_str_equal (type,"filter-buffer" ) ||g_str_equal (type,"filter-dump" ) || -g_str_equal (type,"filter-mirror" )) { +g_str_equal (type,"filter-mirror" ) || +g_str_equal (type,"filter-redirector" )) {return false; }
Re: [Qemu-devel] [PATCH V2 2/3] net/filter-redirector:Add filter-redirector
In reply to Zhang ChenQuoted 19 lines · Zhang Chen
Filter-redirector is a netfilter plugin.
It gives qemu the ability to redirect net packet. redirector can redirect filter's net packet to outdev. and redirect indev's packet to filter.filter + | | redirector | +-------------------------+ | | | | | | | | | indev +----------------+ +----------------> outdev | | | | | | | | | +-------------------------+
Quoted 8 lines
|
|
v
filter
|
|
v
filter------> guestAs we've discussed, the actual destination should depend on "queue" parameter. E.g if queue = rx, the destination should be netdev not guest.
Quoted 103 lines
usage:
(quoted diff) +0
-4-netdev tap,id=hn0 -chardev socket,id=s0,host=ip_primary,port=X,server,nowait -chardev socket,id=s1,host=ip_primary,port=Y,server,nowait -filter-redirector,id=r0,netdev=hn0,queue=tx/rx/all,indev=s0,outdev=s1Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/Makefile.objs | 1 + net/filter-redirector.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 8 ++ vl.c | 3 +- 4 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 net/filter-redirector.cnet/Makefile.objs +1
-0@@ -16,3 +16,4 @@ common-obj-$(CONFIG_NETMAP) += netmap.o common-obj-y += filter.o common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o +common-obj-y += filter-redirector.onet/filter-redirector.c new file +70
-0@@ -0,0 +1,216 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter-traffic.h" + +#define FILTER_REDIRECTOR (obj) \ + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_REDIRECTOR) + +#define TYPE_FILTER_REDIRECTOR "filter-redirector" +#define REDIRECT_HEADER_LEN sizeof(uint32_t) + +static int filter_redirector_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +return filter_traffic_send (nf, iov, iovcnt); +} + +static ssize_t filter_redirector_receive_iov (NetFilterState *nf, +NetClientState *sender, +unsigned flags, +const struct iovec *iov, +int iovcnt, +NetPacketSent *sent_cb) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); +ssize_t ret =0 ; + +if (s->chr_out) { + ret =filter_redirector_send (nf, iov, iovcnt); +if (ret <0 ) { +error_report ("filter_redirector_send failed" ); + } +return iov_size (iov, iovcnt); + }else { +return 0 ; + } +} + +static void filter_redirector_cleanup (NetFilterState *nf) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); + +if (s->chr_in) { +qemu_chr_fe_release (s->chr_in); + } +if (s->chr_out) { +qemu_chr_fe_release (s->chr_out); + } +} + +static int redirector_chr_can_read (void *opaque) +{ +return REDIRECT_HEADER_LEN ; +} + +static void redirector_chr_read (void *opaque,const uint8_t *buf,int size) +{ +NetFilterState *nf = opaque; +TrafficState *s =FILTER_REDIRECTOR (nf); +uint32_t len; +int ret =0 ;
net/filter-redirector.c line 80
Quoted 10 lines
net/filter-redirector.c new file +10
-0+uint8_t *recv_buf; +int offset = size; + +while (offset <REDIRECT_HEADER_LEN ) { + ret = s->chr_in->chr_sync_read (s->chr_in, (uint8_t *)buf + offset, +REDIRECT_HEADER_LEN - offset); +if (ret ==-1 && errno ==EAGAIN ) { +g_usleep (100 ); + } + offset += ret;
Why not use qemu_chr_fe_read_all() here?
And what's better, instead of busy waiting/reading. You can save the partial packet in a temporary buffer, and do not forward it after it has been completely received.
Quoted 6 lines
net/filter-redirector.c new file +6
-0+ } + +memcpy (&len, buf,sizeof (len)); + len =ntohl (len); + +if (len >0 && len <NET_BUFSIZE ) {
net/filter-redirector.c line 96
Quoted 10 lines
net/filter-redirector.c new file +10
-0+ recv_buf =g_malloc (len); + ret =qemu_chr_fe_read_all (s->chr_in, recv_buf, len); +if (ret != len) { +error_report ("filter-redirector recv buf failed" ); +g_free (recv_buf); +return ; + } + + ret =qemu_net_queue_send (s->incoming_queue, nf->netdev, +0 , (const uint8_t *)recv_buf, len,NULL );
As I replied in last version. Sender should be chose depending on 'queues'. When 'queue=rx' sender should be be netdev's peer.
Re: [Qemu-devel] [PATCH V2 2/3] net/filter-redirector:Add filter-redirector
Quoted 29 lines · Jason Wang
Quoted 19 lines · Zhang Chen
Filter-redirector is a netfilter plugin.
It gives qemu the ability to redirect net packet. redirector can redirect filter's net packet to outdev. and redirect indev's packet to filter.filter + | | redirector | +-------------------------+ | | | | | | | | | indev +----------------+ +----------------> outdev | | | | | | | | | +-------------------------+Quoted 8 lines
| | v filter | | v filter------> guest
Quoted 2 lines
As we've discussed, the actual destination should depend on "queue" parameter. E.g if queue = rx, the destination should be netdev not guest.
OK, I will fix it in V3
Quoted 115 lines
Quoted 103 lines
usage:
(quoted diff) +0
-4-netdev tap,id=hn0 -chardev socket,id=s0,host=ip_primary,port=X,server,nowait -chardev socket,id=s1,host=ip_primary,port=Y,server,nowait -filter-redirector,id=r0,netdev=hn0,queue=tx/rx/all,indev=s0,outdev=s1Signed-off-by Zhang Chen <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/Makefile.objs | 1 + net/filter-redirector.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 8 ++ vl.c | 3 +- 4 files changed, 227 insertions(+), 1 deletion(-)create mode 100644
net/filter-redirector.cnet/Makefile.objs +1
-0@@ -16,3 +16,4 @@ common-obj-$(CONFIG_NETMAP) += netmap.o common-obj-y += filter.o common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o +common-obj-y += filter-redirector.onet/filter-redirector.c new file +70
-0@@ -0,0 +1,216 @@ +/* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* Copyright (c) 2016 FUJITSU LIMITED +* Copyright (c) 2016 Intel Corporation +* +* Author: Zhang Chen <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "net/filter-traffic.h" + +#define FILTER_REDIRECTOR (obj) \ + OBJECT_CHECK(TrafficState, (obj), TYPE_FILTER_REDIRECTOR) + +#define TYPE_FILTER_REDIRECTOR "filter-redirector" +#define REDIRECT_HEADER_LEN sizeof(uint32_t) + +static int filter_redirector_send (NetFilterState *nf, +const struct iovec *iov, +int iovcnt) +{ +return filter_traffic_send (nf, iov, iovcnt); +} + +static ssize_t filter_redirector_receive_iov (NetFilterState *nf, +NetClientState *sender, +unsigned flags, +const struct iovec *iov, +int iovcnt, +NetPacketSent *sent_cb) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); +ssize_t ret =0 ; + +if (s->chr_out) { + ret =filter_redirector_send (nf, iov, iovcnt); +if (ret <0 ) { +error_report ("filter_redirector_send failed" ); + } +return iov_size (iov, iovcnt); + }else { +return 0 ; + } +} + +static void filter_redirector_cleanup (NetFilterState *nf) +{ +TrafficState *s =FILTER_REDIRECTOR (nf); + +if (s->chr_in) { +qemu_chr_fe_release (s->chr_in); + } +if (s->chr_out) { +qemu_chr_fe_release (s->chr_out); + } +} + +static int redirector_chr_can_read (void *opaque) +{ +return REDIRECT_HEADER_LEN ; +} + +static void redirector_chr_read (void *opaque,const uint8_t *buf,int size) +{ +NetFilterState *nf = opaque; +TrafficState *s =FILTER_REDIRECTOR (nf); +uint32_t len; +int ret =0 ;net/filter-redirector.c line 80
Quoted 10 lines
net/filter-redirector.c new file +10
-0+uint8_t *recv_buf; +int offset = size; + +while (offset <REDIRECT_HEADER_LEN ) { + ret = s->chr_in->chr_sync_read (s->chr_in, (uint8_t *)buf + offset, +REDIRECT_HEADER_LEN - offset); +if (ret ==-1 && errno ==EAGAIN ) { +g_usleep (100 ); + } + offset += ret;Why not use
qemu_chr_fe_read_all()here?
Quoted 4 lines
And what's better, instead of busy waiting/reading. You can save the partial packet in a temporary buffer, and do not forward it after it has been completely received.
OK, will fix in V3
Thanks
zhangchen
Quoted 23 lines
Quoted 6 lines
net/filter-redirector.c new file +6
-0+ } + +memcpy (&len, buf,sizeof (len)); + len =ntohl (len); + +if (len >0 && len <NET_BUFSIZE ) {net/filter-redirector.c line 96
Quoted 10 lines
net/filter-redirector.c new file +10
-0+ recv_buf =g_malloc (len); + ret =qemu_chr_fe_read_all (s->chr_in, recv_buf, len); +if (ret != len) { +error_report ("filter-redirector recv buf failed" ); +g_free (recv_buf); +return ; + } + + ret =qemu_net_queue_send (s->incoming_queue, nf->netdev, +0 , (const uint8_t *)recv_buf, len,NULL );As I replied in last version. Sender should be chose depending on 'queues'. When 'queue=rx' sender should be be netdev's peer.
.
Signature · 2 lines
Thanks zhangchen
PATCH v2 3/3 tests/test-filter-redirector: Add unit test for filter-redirector
In reply to Zhang ChenIn this unit test,we will test the filter redirector function.
Start qemu with:
"-netdev tap,id=qtest-bn0 "
"-device rtl8139,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0," "queue=tx,indev=redirector1 "
"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=tx,outdev=redirector0 "
We inject packet to -chardev redirector1,then filter-redirector will pass it to filter, another filter-redirector get it and redirect it to redirector0,we read packet from redirector0 and compare to what we inject.
--- tests/.gitignore | 1 + tests/Makefile | 2 + tests/test-filter-redirector.c | 97 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 tests/test-filter-redirector.c
tests/.gitignore +1 -0
@@ -64,5 +64,6 @@ test-x86-cpuid test-xbzrle test-netfilter test-filter-mirror +test-filter-redirector *-test qapi-schema/*.test.*
tests/Makefile +2 -0
@@ -213,6 +213,7 @@ check-qtest-x86_64-$(CONFIG_VHOST_NET_TEST_x86_64) += tests/vhost-user-test$(EXE endif check-qtest-i386-y += tests/test-netfilter$(EXESUF) check-qtest-i386-y += tests/test-filter-mirror$(EXESUF) +check-qtest-i386-y += tests/test-filter-redirector$(EXESUF) check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -565,6 +566,7 @@ tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y) tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y) tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y) tests/test-filter-mirror$(EXESUF): tests/test-filter-mirror.o $(qtest-obj-y) +tests/test-filter-redirector$(EXESUF): tests/test-filter-redirector.o $(qtest-obj-y) tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o contrib/ivshmem-server/ivshmem-server.o $(libqos-pc-obj-y) tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o
tests/test-filter-redirector.c new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH V2 3/3] tests/test-filter-redirector: Add unit test for filter-redirector
Quoted 5 lines · Zhang Chen
In this unit test,we will test the filter redirector function.
Start qemu with:
"-netdev tap,id=qtest-bn0 "
Please don't use tap since it needs
- CAP_NET_ADMIN
- if-up script
Neither of above could not be true for a qtest environment.
Quoted 8 lines
"-device rtl8139,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0," "queue=tx,indev=redirector1 "
"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=tx,outdev=redirector0 "
Quoted 4 lines
We inject packet to -chardev redirector1,then filter-redirector will pass it to filter, another filter-redirector get it and redirect it to redirector0,we read packet from redirector0 and compare to what we inject.
Looks correct but I think queue='rx' should also be tested here. How about:
- using backend
- redirect tx traffic to a chardev, then inject packet from socket and read it from chardev
- redirect from another chardev to rx traffic, then inject packet from chardev and read if from socket?
Re: [Qemu-devel] [PATCH V2 3/3] tests/test-filter-redirector: Add unit test for filter-redirector
Quoted 29 lines · Jason Wang
Quoted 5 lines · Zhang Chen
In this unit test,we will test the filter redirector function.
Start qemu with:
"-netdev tap,id=qtest-bn0 "Please don't use tap since it needs
-
CAP_NET_ADMIN
- if-up scriptNeither of above could not be true for a qtest environment.
Quoted 8 lines
"-device rtl8139,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0," "queue=tx,indev=redirector1 "
"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=tx,outdev=redirector0 "Quoted 4 lines
We inject packet to -chardev redirector1,then filter-redirector will pass it to filter, another filter-redirector get it and redirect it to redirector0,we read packet from redirector0 and compare to what we inject.
Quoted 8 lines
Looks correct but I think queue='rx' should also be tested here. How about:
- using backend
- redirect tx traffic to a chardev, then inject packet from socket and read it from chardev
- redirect from another chardev to rx traffic, then inject packet from chardev and read if from socket?
hi, Jason
IIUC, a full UT for redirector should include following cases. How about they:
rd[n]: redirctor n
Case 1, tx traffic flow:
qemu side | test side
|
+---------+ | +-------+
| backend <---------------+ sock0 |
+----+----+ | +-------+
| |
+----v----+ +-------+ |
| rd0 +->+chardev| |
+---------+ +---+---+ |
| |
+---------+ | |
| rd1 <------+ |
+----+----+ |
| |
+----v----+ | +-------+
| rd2 +--------------->sock1 |
+---------+ | +-------+
+--------------------------------------
Case 2, rx traffic flow
qemu side | test side
|
+---------+ | +-------+
| backend +---------------> sock1 |
+----^----+ | +-------+
| |
+----+----+ +-------+ |
| rd0 +->+chardev| |
+---------+ +---+---+ |
^ |
+---------+ | |
| rd1 +------+ |
+----^----+ |
| |
+----+----+ | +-------+
| rd2 <---------------+sock0 |
+---------+ | +-------+
+Thanks
Li Zhijian