filter-rewriter: introduce filter-rewriter
v2 cover filter-rewriter: introduce filter-rewriter
Filter-rewriter is a part of COLO project.
So this patch set depend on colo-compare.
It will rewrite some of secondary packet to make secondary guest's connection established successfully.
v2:
- add more comments about packet flows
- add some trace-event
- add seq offset ( = secondary_seq - primary_seq)
v1:
- initial patch
Zhang Chen (3):
filter-rewriter: introduce filter-rewriter initialization
filter-rewriter: track connection and parse packet
filter-rewriter: rewrite tcp packet to keep secondary connection
net/Makefile.objs | 1 + net/colo-base.h | 2 + net/filter-rewriter.c | 270 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 10 ++ trace-events | 5 + vl.c | 3 +- 6 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 net/filter-rewriter.c
Signature · 1 line
2.7.4
RFC PATCH v2 1/3 filter-rewriter: introduce filter-rewriter initialization
Filter-rewriter is a part of COLO project.
It will rewrite some of secondary packet to make secondary guest's connection established successfully.
usage:
colo secondary:
-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 -object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 -object filter-rewriter,id=rew0,netdev=hn0,queue=all
--- net/Makefile.objs | 1 + net/filter-rewriter.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 10 +++++ vl.c | 3 +- 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 net/filter-rewriter.c
net/Makefile.objs +1 -0
@@ -18,3 +18,4 @@ common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o common-obj-y += colo-compare.o common-obj-y += colo-base.o +common-obj-y += filter-rewriter.o
net/filter-rewriter.c new file +112 -0
112 lines not on this page · raw message
qemu-options.hx +10 -0
@@ -3859,6 +3859,16 @@ 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 of indev or outdev need to be specified. +@item -object filter-rewriter,id=@var{id},netdev=@var{netdevid},rewriter-mode=@var{mode}[,queue=@var{all|rx|tx}] + +Filter-rewriter is a part of COLO project.It will rewrite some of secondary packet. + +usage: +colo secondary: +-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 +-object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 +-object filter-rewriter,id=rew0,netdev=hn0,queue=all + @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
@@ -2866,7 +2866,8 @@ static bool object_create_initial(const char *type) g_str_equal(type, "filter-dump") || g_str_equal(type, "filter-mirror") || g_str_equal(type, "filter-redirector") || - g_str_equal(type, "colo-compare")) { + g_str_equal(type, "colo-compare") || + g_str_equal(type, "filter-rewriter")) {return false; }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [RFC PATCH V2 1/3] filter-rewriter: introduce filter-rewriter initialization
Quoted 3 lines · Zhang Chen
Filter-rewriter is a part of COLO project.
It will rewrite some of secondary packet to make secondary guest's connection established successfully.
Probably need to be more verbose here. E.g we only care about tcp and only rewrite ack now.
Quoted 151 lines
usage:
colo secondary:
(quoted diff) +0
-3-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 -object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 -object filter-rewriter,id=rew0,netdev=hn0,queue=allSigned-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/Makefile.objs | 1 + net/filter-rewriter.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 10 +++++ vl.c | 3 +- 4 files changed, 125 insertions(+), 1 deletion(-)create mode 100644
net/filter-rewriter.cnet/Makefile.objs +1
-0@@ -18,3 +18,4 @@ common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o common-obj-y += colo-compare.o common-obj-y += colo-base.o +common-obj-y += filter-rewriter.onet/filter-rewriter.c new file +112
-0@@ -0,0 +1,112 @@ +/* +* 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 "qemu/osdep.h" +#include "net/colo-base.h" +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/iov.h" +#include "net/checksum.h" + +#define FILTER_COLO_REWRITER (obj) \ + OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER) + +#define TYPE_FILTER_REWRITER "filter-rewriter" + +enum { +PRIMARY =0 , +SECONDARY , +}; + +typedef struct RewriterState { +NetFilterState parent_obj; +/* connection list: the connections belonged to this NIC could be found +* in this list. +* element type: Connection +*/ +GQueue conn_list; +NetQueue *incoming_queue; +/* to protect conn_list */ +QemuMutex conn_list_lock; +/* hashtable to save connection */ +GHashTable *connection_track_table; +/* to save unprocessed_connections */ +GQueue unprocessed_connections; +/* current hash size */ +uint32_t hashtable_size; +}RewriterState ; + +static ssize_t colo_rewriter_receive_iov (NetFilterState *nf, +NetClientState *sender, +unsigned flags, +const struct iovec *iov, +int iovcnt, +NetPacketSent *sent_cb) +{ +/* +* if we get tcp packet +* we will rewrite it to make secondary guest's +* connection established successfully +*/ +return 0 ; +} + +static void colo_rewriter_cleanup (NetFilterState *nf) +{ +RewriterState *s =FILTER_COLO_REWRITER (nf); + +qemu_mutex_destroy (&s->conn_list_lock); +g_queue_free (&s->conn_list); +} + +static void colo_rewriter_setup (NetFilterState *nf,Error **errp) +{ +RewriterState *s =FILTER_COLO_REWRITER (nf); + +g_queue_init (&s->conn_list); +qemu_mutex_init (&s->conn_list_lock); + s->hashtable_size =0 ; + + s->connection_track_table =g_hash_table_new_full (connection_key_hash, + connection_key_equal, + g_free, + connection_destroy); + s->incoming_queue =qemu_new_net_queue (qemu_netfilter_pass_to_next, nf); +} + +static void colo_rewriter_class_init (ObjectClass *oc,void *data) +{ +NetFilterClass *nfc =NETFILTER_CLASS (oc); + + nfc->setup = colo_rewriter_setup; + nfc->cleanup = colo_rewriter_cleanup; + nfc->receive_iov = colo_rewriter_receive_iov; +} + +static const TypeInfo colo_rewriter_info = { + .name =TYPE_FILTER_REWRITER , + .parent =TYPE_NETFILTER , + .class_init = colo_rewriter_class_init, + .instance_size =sizeof (RewriterState), +}; + +static void register_types (void ) +{ +type_register_static (&colo_rewriter_info); +} + +type_init (register_types);qemu-options.hx +0
-0
qemu-options.hx Create a filter-redirector we need to differ outdev id from indev id, id can not · line 3864
Quoted 6 lines
qemu-options.hx +3
-0@@ -3859,6 +3859,16 @@ 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 of indev or outdev need to be specified. +@item -object filter-rewriter,id=@var{id},netdev=@var{netdevid},rewriter-mode=@var{mode}[,queue=@var{all|rx|tx}] + +Filter-rewriter is a part of COLO project.It will rewrite some of secondary packet.
Need to be more verbose here.
Quoted 15 lines
qemu-options.hx +7
-0+ +usage: +colo secondary: +-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 +-object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 +-object filter-rewriter,id=rew0,netdev=hn0,queue=all + @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 +0
-0
vl.c static bool object_create_initial(const char *type) · line 2870
Quoted 6 lines
vl.c +2
-1@@ -2866,7 +2866,8 @@ static bool object_create_initial(const char *type)g_str_equal (type,"filter-dump" ) ||g_str_equal (type,"filter-mirror" ) ||g_str_equal (type,"filter-redirector" ) || - g_str_equal(type, "colo-compare")) { +g_str_equal (type,"colo-compare" ) || + g_str_equal(type, "filter-rewriter")) {
So this makes the patch can't be applied cleanly on master.
And I think we don't want to manually compare 100 kinds of filters in the future. How about doing something better, e.g:
- introduce something like filter_register_type()
- recored the name of the type in a list
- iterate it here
?
Quoted 3 lines
vl.c +0
-0return false; }
Re: [Qemu-devel] [RFC PATCH V2 1/3] filter-rewriter: introduce filter-rewriter initialization
Quoted 9 lines · Jason Wang
Quoted 3 lines · Zhang Chen
Filter-rewriter is a part of COLO project.
It will rewrite some of secondary packet to make secondary guest's connection established successfully.Probably need to be more verbose here. E.g we only care about tcp and only rewrite ack now.
ok~ I will add more comments in next version.
Quoted 158 lines
Quoted 158 lines
usage:
colo secondary:
(quoted diff) +0
-3-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 -object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 -object filter-rewriter,id=rew0,netdev=hn0,queue=allSigned-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/Makefile.objs | 1 + net/filter-rewriter.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 10 +++++ vl.c | 3 +- 4 files changed, 125 insertions(+), 1 deletion(-)create mode 100644
net/filter-rewriter.cnet/Makefile.objs +1
-0@@ -18,3 +18,4 @@ common-obj-y += filter-buffer.o common-obj-y += filter-mirror.o common-obj-y += colo-compare.o common-obj-y += colo-base.o +common-obj-y += filter-rewriter.onet/filter-rewriter.c new file +37
-0@@ -0,0 +1,112 @@ +/* +* 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 "qemu/osdep.h" +#include "net/colo-base.h" +#include "net/filter.h" +#include "net/net.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "qapi/qmp/qerror.h" +#include "qapi-visit.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/iov.h" +#include "net/checksum.h" + +#define FILTER_COLO_REWRITER (obj) \ + OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER) + +#define TYPE_FILTER_REWRITER "filter-rewriter" + +enum { +PRIMARY =0 , +SECONDARY , +}; + +typedef struct RewriterState { + NetFilterState parent_obj; + /* connection list: the connections belonged to this NIC couldbe found
net/filter-rewriter.c new file +47
-0+* in this list. +* element type: Connection +*/ +GQueue conn_list; +NetQueue *incoming_queue; +/* to protect conn_list */ +QemuMutex conn_list_lock; +/* hashtable to save connection */ +GHashTable *connection_track_table; +/* to save unprocessed_connections */ +GQueue unprocessed_connections; +/* current hash size */ +uint32_t hashtable_size; +} RewriterState; + +static ssize_t colo_rewriter_receive_iov (NetFilterState *nf, +NetClientState *sender, +unsigned flags, +const struct iovec *iov, +int iovcnt, +NetPacketSent *sent_cb) +{ +/* +* if we get tcp packet +* we will rewrite it to make secondary guest's +* connection established successfully +*/ +return 0 ; +} + +static void colo_rewriter_cleanup (NetFilterState *nf) +{ +RewriterState *s =FILTER_COLO_REWRITER (nf); + +qemu_mutex_destroy (&s->conn_list_lock); +g_queue_free (&s->conn_list); +} + +static void colo_rewriter_setup (NetFilterState *nf,Error **errp) +{ +RewriterState *s =FILTER_COLO_REWRITER (nf); + +g_queue_init (&s->conn_list); +qemu_mutex_init (&s->conn_list_lock); + s->hashtable_size =0 ; + + s->connection_track_table =g_hash_table_new_full(connection_key_hash,
+connection_key_equal,net/filter-rewriter.c new file +3
-0+ g_free, + connection_destroy); + s->incoming_queue =
qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);net/filter-rewriter.c new file +24
-0+} + +static void colo_rewriter_class_init (ObjectClass *oc,void *data) +{ +NetFilterClass *nfc =NETFILTER_CLASS (oc); + + nfc->setup = colo_rewriter_setup; + nfc->cleanup = colo_rewriter_cleanup; + nfc->receive_iov = colo_rewriter_receive_iov; +} + +static const TypeInfo colo_rewriter_info = { + .name =TYPE_FILTER_REWRITER , + .parent =TYPE_NETFILTER , + .class_init = colo_rewriter_class_init, + .instance_size =sizeof (RewriterState), +}; + +static void register_types (void ) +{ +type_register_static (&colo_rewriter_info); +} + +type_init (register_types);qemu-options.hx +0
-0@@ -3859,6 +3859,16 @@ Create a filter-redirector we need to differoutdev id from indev id, id can not
be the same. we can just use indev or outdev, but at least one of
indev or outdev
Quoted 8 lines
Quoted 6 lines
need to be specified.
+@item -object
filter-rewriter,id=@var{id},netdev=@var{netdevid},rewriter-mode=@var{mode}[,queue=@var{all|rx|tx}]qemu-options.hx +2
-0+ +Filter-rewriter is a part of COLO project.It will rewrite some ofsecondary packet.
Need to be more verbose here.
OK~
Quoted 18 lines
Quoted 18 lines
qemu-options.hx +7
-0+ +usage: +colo secondary: +-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 +-object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1 +-object filter-rewriter,id=rew0,netdev=hn0,queue=all + @item -objectfilter-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 +0
-0@@ -2866,7 +2866,8 @@ static bool object_create_initial(const char *type)
Quoted 8 lines
vl.c static bool object_create_initial(const char *type) · line 2870
Quoted 6 lines
vl.c +2
-1g_str_equal (type,"filter-dump" ) ||g_str_equal (type,"filter-mirror" ) ||g_str_equal (type,"filter-redirector" ) || - g_str_equal(type, "colo-compare")) { +g_str_equal (type,"colo-compare" ) || + g_str_equal(type, "filter-rewriter")) {So this makes the patch can't be applied cleanly on master.
Yes,filter-rewriter based on colo-base in colo-compare... So, should we make colo-base be a independent patch set? Now, colo-base used by colo-compare and filter-rewriter.
Quoted 9 lines
And I think we don't want to manually compare 100 kinds of filters in the future. How about doing something better, e.g:
- introduce something like
filter_register_type()
- recored the name of the type in a list
- iterate it here?
Maybe we need do this after colo-compare and filter-rewriter be merged. and make filter-dump and other filter all in this type.
Thanks
Zhang Chen
Quoted 7 lines
vl.c static bool object_create_initial(const char *type) · line 2872
Quoted 2 lines
vl.c +0
-0return false; }.
Signature · 2 lines
Thanks zhangchen
Re: [Qemu-devel] [RFC PATCH V2 1/3] filter-rewriter: introduce filter-rewriter initialization
Quoted 20 lines · Zhang Chen
Quoted 19 lines
Quoted 10 lines
@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 +0
-0vl.c static bool object_create_initial(const char · line 2871
Quoted 7 lines
vl.c +2
-1@@ -2866,7 +2866,8 @@ static bool object_create_initial(const char *type) g_str_equal(type, "filter-dump") || g_str_equal(type, "filter-mirror") || g_str_equal(type, "filter-redirector") || - g_str_equal(type, "colo-compare")) { + g_str_equal(type, "colo-compare") || + g_str_equal(type, "filter-rewriter")) {So this makes the patch can't be applied cleanly on master.
Quoted 4 lines
Yes,filter-rewriter based on colo-base in colo-compare... So, should we make colo-base be a independent patch set? Now, colo-base used by colo-compare and filter-rewriter.
At least the codes in this series is pretty independent, so there's no need I think.
Quoted 7 lines
Quoted 7 lines
And I think we don't want to manually compare 100 kinds of filters in the future. How about doing something better, e.g:
- introduce something like
filter_register_type()
- recored the name of the type in a list
- iterate it here
Quoted 8 lines
Quoted 2 lines
?
Maybe we need do this after colo-compare and filter-rewriter be merged. and make filter-dump and other filter all in this type.
Thanks
Zhang Chen
Yes.
Quoted 7 lines
Quoted 7 lines
vl.c static bool object_create_initial(const char *type) · line 2872
Quoted 3 lines
vl.c +0
-0return false; }.
RFC PATCH v2 2/3 filter-rewriter: track connection and parse packet
In reply to Zhang ChenWe use colo-base.h to track connection and parse packet
--- net/filter-rewriter.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
net/filter-rewriter.c +52 -0
@@ -50,6 +50,20 @@ typedef struct RewriterState {uint32_t hashtable_size; } RewriterState; +/* +* Return 1 on success, if return 0 means the pkt +* is not TCP packet +*/ +static int is_tcp_packet (Packet *pkt) +{ +if (!parse_packet_early (pkt) && + pkt->ip->ip_p ==IPPROTO_TCP ) { +return 1 ; + }else { +return 0 ; + } +} + static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,
@@ -57,11 +71,49 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,int iovcnt, NetPacketSent *sent_cb) { + RewriterState *s = FILTER_COLO_REWRITER(nf); +Connection *conn; +ConnectionKey key = {{0 } }; +Packet *pkt; +ssize_t size =iov_size (iov, iovcnt); +char *buf =g_malloc0 (size); + +iov_to_buf (iov, iovcnt,0 , buf, size); + pkt =packet_new (buf, size); +/* * if we get tcp packet * we will rewrite it to make secondary guest's * connection established successfully */ +if (is_tcp_packet (pkt)) { +if (sender == nf->netdev) { +fill_connection_key (pkt, &key,SECONDARY ); + }else { +fill_connection_key (pkt, &key,PRIMARY ); + } + + conn =connection_get (s->connection_track_table, + &key, + &s->hashtable_size); +if (!conn->processing) { +qemu_mutex_lock (&s->conn_list_lock); +g_queue_push_tail (&s->conn_list, conn); +qemu_mutex_unlock (&s->conn_list_lock); + conn->processing = true; + } + +if (sender == nf->netdev) { +/* NET_FILTER_DIRECTION_TX */ +/* handle_primary_tcp_pkt */ + }else { +/* NET_FILTER_DIRECTION_RX */ +/* handle_secondary_tcp_pkt */ + } + } + +packet_destroy (pkt,NULL ); + pkt =NULL ;return 0 ; }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [RFC PATCH V2 2/3] filter-rewriter: track connection and parse packet
Quoted 57 lines · Zhang Chen
We use
colo-base.hto track connection and parse packetSigned-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-rewriter.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)net/filter-rewriter.c +28
-0@@ -50,6 +50,20 @@ typedef struct RewriterState {uint32_t hashtable_size; } RewriterState; +/* +* Return 1 on success, if return 0 means the pkt +* is not TCP packet +*/ +static int is_tcp_packet (Packet *pkt) +{ +if (!parse_packet_early (pkt) && + pkt->ip->ip_p ==IPPROTO_TCP ) { +return 1 ; + }else { +return 0 ; + } +} + static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -57,11 +71,49 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,int iovcnt, NetPacketSent *sent_cb) { + RewriterState *s = FILTER_COLO_REWRITER(nf); +Connection *conn; +ConnectionKey key = {{0 } }; +Packet *pkt; +ssize_t size =iov_size (iov, iovcnt); +char *buf =g_malloc0 (size); + +iov_to_buf (iov, iovcnt,0 , buf, size); + pkt =packet_new (buf, size); +/* * if we get tcp packet * we will rewrite it to make secondary guest's * connection established successfully */ +if (is_tcp_packet (pkt)) { +if (sender == nf->netdev) { +fill_connection_key (pkt, &key,SECONDARY ); + }else {
net/filter-rewriter.c static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, · line 102
Quoted 10 lines
net/filter-rewriter.c +10
-0+fill_connection_key (pkt, &key,PRIMARY ); + } + + conn =connection_get (s->connection_track_table, + &key, + &s->hashtable_size); +if (!conn->processing) { +qemu_mutex_lock (&s->conn_list_lock); +g_queue_push_tail (&s->conn_list, conn); +qemu_mutex_unlock (&s->conn_list_lock);
conn_list was never used in this series, and I fail to understand why conn_list is needed?
Quoted 17 lines
net/filter-rewriter.c +14
-0+ conn->processing = true; + } + +if (sender == nf->netdev) { +/* NET_FILTER_DIRECTION_TX */ +/* handle_primary_tcp_pkt */ + }else { +/* NET_FILTER_DIRECTION_RX */ +/* handle_secondary_tcp_pkt */ + } + } + +packet_destroy (pkt,NULL ); + pkt =NULL ;return 0 ; }
Re: [Qemu-devel] [RFC PATCH V2 2/3] filter-rewriter: track connection and parse packet
Quoted 65 lines · Jason Wang
Quoted 62 lines · Zhang Chen
We use
colo-base.hto track connection and parse packetSigned-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/filter-rewriter.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)net/filter-rewriter.c +31
-0@@ -50,6 +50,20 @@ typedef struct RewriterState {uint32_t hashtable_size; } RewriterState; +/* +* Return 1 on success, if return 0 means the pkt +* is not TCP packet +*/ +static int is_tcp_packet(Packet *pkt) +{ +if (!parse_packet_early (pkt) && + pkt->ip->ip_p ==IPPROTO_TCP ) { +return 1 ; + }else { +return 0 ; + } +} + static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, NetClientState *sender, unsigned flags,@@ -57,11 +71,49 @@ static ssize_tcolo_rewriter_receive_iov (NetFilterState *nf, int iovcnt, NetPacketSent *sent_cb) { +RewriterState *s =FILTER_COLO_REWRITER (nf); +Connection *conn; +ConnectionKey key = {{0 } }; +Packet *pkt; +ssize_t size =iov_size (iov, iovcnt); +char *buf =g_malloc0 (size); + +iov_to_buf (iov, iovcnt,0 , buf, size); + pkt =packet_new (buf, size); +/* * if we get tcp packet * we will rewrite it to make secondary guest's * connection established successfully */ +if (is_tcp_packet (pkt)) { +if (sender == nf->netdev) { +fill_connection_key (pkt, &key,SECONDARY ); + }else { +fill_connection_key (pkt, &key,PRIMARY ); + } + + conn = connection_get(s->connection_track_table,
Quoted 8 lines
net/filter-rewriter.c static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, · line 102
Quoted 6 lines
net/filter-rewriter.c +6
-0+ &key, + &s->hashtable_size); +if (!conn->processing) { +qemu_mutex_lock (&s->conn_list_lock); +g_queue_push_tail (&s->conn_list, conn); +qemu_mutex_unlock (&s->conn_list_lock);
conn_listwas never used in this series, and I fail to understand whyconn_listis needed?
When filter-rewriter work with colo-frame we need this do some synchronization job. like colo do checkpoint,we will flush connection.
Maybe I should move this lock to other patch (like filter-rewriter work with colo-frame).
Thanks
Zhang Chen
Quoted 22 lines
Quoted 7 lines
net/filter-rewriter.c +6
-0+ conn->processing = true; + } + +if (sender == nf->netdev) { +/* NET_FILTER_DIRECTION_TX */ +/* handle_primary_tcp_pkt */ net/filter-rewriter.c static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, · line 118
Quoted 10 lines
net/filter-rewriter.c +8
-0+ }else { +/* NET_FILTER_DIRECTION_RX */ +/* handle_secondary_tcp_pkt */ + } + } + +packet_destroy (pkt,NULL ); + pkt =NULL ;return 0 ; }.
Signature · 2 lines
Thanks zhangchen
Re: [Qemu-devel] [RFC PATCH V2 2/3] filter-rewriter: track connection and parse packet
Quoted 15 lines · Zhang Chen
Quoted 15 lines
Quoted 14 lines
net/filter-rewriter.c +14
-0+if (is_tcp_packet (pkt)) { +if (sender == nf->netdev) { +fill_connection_key (pkt, &key,SECONDARY ); + }else { +fill_connection_key (pkt, &key,PRIMARY ); + } + + conn =connection_get (s->connection_track_table, + &key, + &s->hashtable_size); +if (!conn->processing) { +qemu_mutex_lock (&s->conn_list_lock); +g_queue_push_tail (&s->conn_list, conn); +qemu_mutex_unlock (&s->conn_list_lock);
Quoted 8 lines
Quoted 1 line
conn_listwas never used in this series, and I fail to understand whyconn_listis needed?When filter-rewriter work with colo-frame we need this do some synchronization job. like colo do checkpoint,we will flush connection.
Maybe I should move this lock to other patch (like filter-rewriter work with colo-frame).Thanks
Zhang Chen
Yes, please do it.
RFC PATCH v2 3/3 filter-rewriter: rewrite tcp packet to keep secondary connection
In reply to Zhang ChenWe will rewrite tcp packet secondary received and sent. When colo guest is a tcp server.
Firstly, client start a tcp handshake. the packet's seq=client_seq, ack=0,flag=SYN. COLO primary guest get this pkt and mirror(filter-mirror) to secondary guest, secondary get it use filter-redirector. Then,primary guest response pkt (seq=primary_seq,ack=client_seq+1,flag=ACK|SYN).
secondary guest response pkt (seq=secondary_seq,ack=client_seq+1,flag=ACK|SYN).
In here,we use filter-rewriter save the secondary_seq to it's tcp connection. Finally handshake,client send pkt (seq=client_seq+1,ack=primary_seq+1,flag=ACK). Here,filter-rewriter can get primary_seq, and rewrite ack from primary_seq+1 to secondary_seq+1, recalculate checksum. So the secondary tcp connection kept good.
When we send/recv packet.
client send pkt(seq=client_seq+1+data_len,ack=primary_seq+1,flag=ACK|PSH). filter-rewriter rewrite ack and send to secondary guest.
primary guest response pkt (seq=primary_seq+1,ack=client_seq+1+data_len,flag=ACK) secondary guest response pkt (seq=secondary_seq+1,ack=client_seq+1+data_len,flag=ACK) we rewrite secondary guest seq from secondary_seq+1 to primary_seq+1. So tcp connection kept good.
In code We use offset( = secondary_seq - primary_seq ) to rewrite seq or ack.handle_primary_tcp_pkt: tcp_pkt->th_ack += offset;handle_secondary_tcp_pkt: tcp_pkt->th_seq -= offset;
--- net/colo-base.h | 2 + net/filter-rewriter.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++- trace-events | 5 +++ 3 files changed, 115 insertions(+), 2 deletions(-)
net/colo-base.h +2 -0
@@ -71,6 +71,8 @@ typedef struct Connection {uint8_t ip_proto;/* be used by filter-rewriter */ colo_conn_state state; +/* offset = secondary_seq - primary_seq */ +tcp_seq offset; } Connection;uint32_t connection_key_hash (const void *opaque);
net/filter-rewriter.c +108 -2
130 lines not on this page · raw message
trace-events +5 -0
@@ -1927,3 +1927,8 @@ colo_compare_icmp_miscompare_mtu(const char *sta, int size) ": %s %d" colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s" colo_old_packet_check_found(int64_t old_time) "%" PRId64 colo_compare_miscompare(void) "" + +# net/filter-rewriter.c +colo_filter_rewriter_debug(void) "" +colo_filter_rewriter_pkt_info(const char *func, const char *src, const char *dst, uint32_t seq, uint32_t ack, uint32_t flag) "%s: src/dst: %s/%s p: seq/ack=%u/%u flags=%x\n" +colo_filter_rewriter_conn_offset(uint32_t offset) ": offset=%u\n"
Signature · 1 line
2.7.4
Re: [Qemu-devel] [RFC PATCH V2 3/3] filter-rewriter: rewrite tcp packet to keep secondary connection
Quoted 47 lines · Zhang Chen
We will rewrite tcp packet secondary received and sent. When colo guest is a tcp server.
Firstly, client start a tcp handshake. the packet's seq=client_seq, ack=0,flag=SYN. COLO primary guest get this pkt and mirror(filter-mirror) to secondary guest, secondary get it use filter-redirector. Then,primary guest response pkt (seq=primary_seq,ack=client_seq+1,flag=ACK|SYN).
secondary guest response pkt (seq=secondary_seq,ack=client_seq+1,flag=ACK|SYN).
In here,we use filter-rewriter save thesecondary_seqto it's tcp connection. Finally handshake,client send pkt (seq=client_seq+1,ack=primary_seq+1,flag=ACK). Here,filter-rewriter can getprimary_seq, and rewrite ack from primary_seq+1 to secondary_seq+1, recalculate checksum. So the secondary tcp connection kept good.When we send/recv packet.
client sendpkt(seq=client_seq+1+data_len,ack=primary_seq+1,flag=ACK|PSH). filter-rewriter rewrite ack and send to secondary guest.primary guest response pkt (seq=primary_seq+1,ack=client_seq+1+data_len,flag=ACK) secondary guest response pkt (seq=secondary_seq+1,ack=client_seq+1+data_len,flag=ACK) we rewrite secondary guest seq from secondary_seq+1 to primary_seq+1. So tcp connection kept good.
In code We use
offset( = secondary_seq - primary_seq )to rewrite seq or ack.handle_primary_tcp_pkt:tcp_pkt->th_ack+= offset;handle_secondary_tcp_pkt:tcp_pkt->th_seq-= offset;Signed-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/colo-base.h | 2 + net/filter-rewriter.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++- trace-events | 5 +++ 3 files changed, 115 insertions(+), 2 deletions(-)net/colo-base.h +0
-0
net/colo-base.h typedef struct Connection { · line 75
Quoted 5 lines
net/colo-base.h +2
-0@@ -71,6 +71,8 @@ typedef struct Connection {uint8_t ip_proto;/* be used by filter-rewriter */ colo_conn_state state; +/* offset = secondary_seq - primary_seq */ +tcp_seq offset;
Fail to find the definition of 'tcp_seq'.
Quoted 33 lines
net/colo-base.h +0
-0} Connection;uint32_t connection_key_hash (const void *opaque);net/filter-rewriter.c +15
-0@@ -21,6 +21,7 @@#include "qemu/main-loop.h" #include "qemu/iov.h" #include "net/checksum.h" +#include "trace.h" #define FILTER_COLO_REWRITER (obj) \ OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)@@ -64,6 +65,91 @@ static int is_tcp_packet(Packet *pkt) } } +/* handle tcp packet from primary guest */ +static int handle_primary_tcp_pkt (NetFilterState *nf, +Connection *conn, +Packet *pkt) +{ +struct tcphdr *tcp_pkt; +static int syn_flag; + + tcp_pkt = (struct tcphdr *)pkt->transport_layer; +if (trace_event_get_state (TRACE_COLO_FILTER_REWRITER_DEBUG )) { +char *sdebug, *ddebug; + sdebug =strdup (inet_ntoa (pkt->ip->ip_src)); + ddebug =strdup (inet_ntoa (pkt->ip->ip_dst)); + trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt) · line 91
Quoted 10 lines
net/filter-rewriter.c +10
-0+ntohl (tcp_pkt->th_seq),ntohl (tcp_pkt->th_ack), + tcp_pkt->th_flags); +trace_colo_filter_rewriter_conn_offset (conn->offset); +g_free (sdebug); +g_free (ddebug); + } + +if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) { +/* +* this flag update offset func run oncs
typo?
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt) · line 94
Quoted 3 lines
net/filter-rewriter.c +3
-0+* in independent tcp connection +*/ + syn_flag =1 ;
Does this really work if you have more than one tcp connections? You probably need a conn->syn_flag.
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt) · line 105
Quoted 11 lines
net/filter-rewriter.c +11
-0+ } + +if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_ACK )) { +if (syn_flag) { +/* offset = secondary_seq - primary seq */ + conn->offset -= (ntohl (tcp_pkt->th_ack)); + syn_flag =0 ; + + } +/* handle packets to the secondary from the primary */ + tcp_pkt->th_ack =htonl (ntohl (tcp_pkt->th_ack) + conn->offset +1 );
Maybe I miss something, but why +1 here?
Re: [Qemu-devel] [RFC PATCH V2 3/3] filter-rewriter: rewrite tcp packet to keep secondary connection
Quoted 46 lines · Jason Wang
Quoted 43 lines · Zhang Chen
We will rewrite tcp packet secondary received and sent. When colo guest is a tcp server.
Firstly, client start a tcp handshake. the packet's seq=client_seq, ack=0,flag=SYN. COLO primary guest get this pkt and mirror(filter-mirror) to secondary guest, secondary get it use filter-redirector. Then,primary guest response pkt (seq=primary_seq,ack=client_seq+1,flag=ACK|SYN).
secondary guest response pkt (seq=secondary_seq,ack=client_seq+1,flag=ACK|SYN).
In here,we use filter-rewriter save thesecondary_seqto it's tcp connection. Finally handshake,client send pkt (seq=client_seq+1,ack=primary_seq+1,flag=ACK). Here,filter-rewriter can getprimary_seq, and rewrite ack from primary_seq+1 to secondary_seq+1, recalculate checksum. So the secondary tcp connection kept good.When we send/recv packet.
client sendpkt(seq=client_seq+1+data_len,ack=primary_seq+1,flag=ACK|PSH). filter-rewriter rewrite ack and send to secondary guest.primary guest response pkt (seq=primary_seq+1,ack=client_seq+1+data_len,flag=ACK) secondary guest response pkt (seq=secondary_seq+1,ack=client_seq+1+data_len,flag=ACK) we rewrite secondary guest seq from secondary_seq+1 to primary_seq+1. So tcp connection kept good.
In code We use
offset( = secondary_seq - primary_seq )to rewrite seq or ack.handle_primary_tcp_pkt:tcp_pkt->th_ack+= offset;handle_secondary_tcp_pkt:tcp_pkt->th_seq-= offset;Signed-off-by Zhang Chen <[email protected]>Signed-off-by Li Zhijian <[email protected]>Signed-off-by Wen Congyang <[email protected]>--- net/colo-base.h | 2 + net/filter-rewriter.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++- trace-events | 5 +++ 3 files changed, 115 insertions(+), 2 deletions(-)
Quoted 13 lines
net/colo-base.h typedef struct Connection { · line 75
Quoted 10 lines
net/colo-base.h +2
-0@@ -71,6 +71,8 @@ typedef struct Connection {uint8_t ip_proto;/* be used by filter-rewriter */ colo_conn_state state; +/* offset = secondary_seq - primary_seq */ +tcp_seq offset;Fail to find the definition of '
tcp_seq'.
In slirp/tcp.h
typedef uint32_t tcp_seq;
we add this .h in colo-base.h
Quoted 34 lines
Quoted 34 lines
net/colo-base.h +0
-0} Connection;uint32_t connection_key_hash (const void *opaque);net/filter-rewriter.c +18
-0@@ -21,6 +21,7 @@#include "qemu/main-loop.h" #include "qemu/iov.h" #include "net/checksum.h" +#include "trace.h" #define FILTER_COLO_REWRITER (obj) \ OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)@@ -64,6 +65,91 @@ static int is_tcp_packet(Packet *pkt) } } +/* handle tcp packet from primary guest */ +static int handle_primary_tcp_pkt(NetFilterState *nf, +Connection *conn, +Packet *pkt) +{ +struct tcphdr *tcp_pkt; +static int syn_flag; + + tcp_pkt = (struct tcphdr *)pkt->transport_layer; +if (trace_event_get_state (TRACE_COLO_FILTER_REWRITER_DEBUG )) { +char *sdebug, *ddebug; + sdebug =strdup (inet_ntoa (pkt->ip->ip_src)); + ddebug =strdup (inet_ntoa (pkt->ip->ip_dst)); +trace_colo_filter_rewriter_pkt_info (__func__, sdebug, ddebug, +ntohl (tcp_pkt->th_seq),ntohl (tcp_pkt->th_ack), + tcp_pkt->th_flags); +trace_colo_filter_rewriter_conn_offset (conn->offset); +g_free (sdebug);
Quoted 8 lines
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt) · line 91
Quoted 6 lines
net/filter-rewriter.c +6
-0+g_free (ddebug); + } + +if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) { +/* +* this flag update offset func run oncs typo?
S/oncs/once
Quoted 6 lines
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt) · line 94
Quoted 4 lines
net/filter-rewriter.c +3
-0+* in independent tcp connection +*/ + syn_flag =1 ;Does this really work if you have more than one tcp connections? You probably need a
conn->syn_flag.
Good catch...
I will fix it in next.
Quoted 17 lines
net/filter-rewriter.c static int is_tcp_packet(Packet *pkt)
Quoted 13 lines
net/filter-rewriter.c +11
-0+ } + +if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_ACK )) { +if (syn_flag) { +/* offset = secondary_seq - primary seq */ + conn->offset -= (ntohl (tcp_pkt->th_ack)); + syn_flag =0 ; + + } +/* handle packets to the secondary from the primary */ + tcp_pkt->th_ack =htonl (ntohl (tcp_pkt->th_ack) + conn->offset +1 );Maybe I miss something, but why +1 here?
No,I miss something.
+ /* offset = secondary_seq - primary seq */
+ conn->offset -= (ntohl(tcp_pkt->th_ack));
should be
+ conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
(ntohl(tcp_pkt->th_ack) - 1) is the primary seq
+ tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
I will fix it.
Thanks
Zhang Chen
Quoted 3 lines
.
Signature · 2 lines
Thanks zhangchen