Received: from eggs.gnu.org ([2001:4830:134:3::10]:38065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bSIsk-0000IO-EZ for qemu-devel@nongnu.org; Wed, 27 Jul 2016 03:03:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bSIsh-0006Hm-3G for qemu-devel@nongnu.org; Wed, 27 Jul 2016 03:03:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43734) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bSIsg-0006Hi-Re for qemu-devel@nongnu.org; Wed, 27 Jul 2016 03:03:51 -0400 References: <1467714580-17581-1-git-send-email-zhangchen.fnst@cn.fujitsu.com> <1467714580-17581-4-git-send-email-zhangchen.fnst@cn.fujitsu.com> From: Jason Wang Message-ID: Date: Wed, 27 Jul 2016 15:03:44 +0800 MIME-Version: 1.0 In-Reply-To: <1467714580-17581-4-git-send-email-zhangchen.fnst@cn.fujitsu.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [RFC PATCH V3 3/3] filter-rewriter: rewrite tcp packet to keep secondary connection List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Zhang Chen , qemu devel , "Daniel P . Berrange" , Paolo Bonzini Cc: Li Zhijian , Wen Congyang , "eddie . dong" On 2016=E5=B9=B407=E6=9C=8805=E6=97=A5 18:29, Zhang Chen wrote: > 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=3Dclient_seq, > ack=3D0,flag=3DSYN. COLO primary guest get this pkt and mirror(filter-m= irror) > to secondary guest, secondary get it use filter-redirector. > Then,primary guest response pkt > (seq=3Dprimary_seq,ack=3Dclient_seq+1,flag=3DACK|SYN). > secondary guest response pkt > (seq=3Dsecondary_seq,ack=3Dclient_seq+1,flag=3DACK|SYN). > In here,we use filter-rewriter save the secondary_seq to it's tcp conne= ction. > Finally handshake,client send pkt > (seq=3Dclient_seq+1,ack=3Dprimary_seq+1,flag=3DACK). > Here,filter-rewriter can get primary_seq, and rewrite ack from primary_= seq+1 > to secondary_seq+1, recalculate checksum. So the secondary tcp connecti= on > kept good. > > When we send/recv packet. > client send pkt(seq=3Dclient_seq+1+data_len,ack=3Dprimary_seq+1,flag=3D= ACK|PSH). > filter-rewriter rewrite ack and send to secondary guest. > > primary guest response pkt > (seq=3Dprimary_seq+1,ack=3Dclient_seq+1+data_len,flag=3DACK) > secondary guest response pkt > (seq=3Dsecondary_seq+1,ack=3Dclient_seq+1+data_len,flag=3DACK) > we rewrite secondary guest seq from secondary_seq+1 to primary_seq+1. > So tcp connection kept good. > > In code We use offset( =3D secondary_seq - primary_seq ) > to rewrite seq or ack. > handle_primary_tcp_pkt: tcp_pkt->th_ack +=3D offset; > handle_secondary_tcp_pkt: tcp_pkt->th_seq -=3D offset; > > Signed-off-by: Zhang Chen > Signed-off-by: Li Zhijian > Signed-off-by: Wen Congyang > --- > net/colo-base.c | 2 + > net/colo-base.h | 7 ++++ > net/filter-rewriter.c | 108 +++++++++++++++++++++++++++++++++++++++++= ++++++++- > trace-events | 5 +++ > 4 files changed, 120 insertions(+), 2 deletions(-) > > diff --git a/net/colo-base.c b/net/colo-base.c > index 9673661..58fbd9d 100644 > --- a/net/colo-base.c > +++ b/net/colo-base.c > @@ -123,6 +123,8 @@ Connection *connection_new(ConnectionKey *key) > =20 > conn->ip_proto =3D key->ip_proto; > conn->processing =3D false; > + conn->offset =3D 0; > + conn->syn_flag =3D 0; > g_queue_init(&conn->primary_list); > g_queue_init(&conn->secondary_list); > =20 > diff --git a/net/colo-base.h b/net/colo-base.h > index 62460c5..353bd55 100644 > --- a/net/colo-base.h > +++ b/net/colo-base.h > @@ -71,6 +71,13 @@ typedef struct Connection { > uint8_t ip_proto; > /* be used by filter-rewriter */ > colo_conn_state state; > + /* offset =3D secondary_seq - primary_seq */ > + tcp_seq offset; > + /* > + * we use this flag update offset func > + * run once in independent tcp connection > + */ > + int syn_flag; > } Connection; > =20 > uint32_t connection_key_hash(const void *opaque); > diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c > index 7f0da2c..f911f99 100644 > --- a/net/filter-rewriter.c > +++ b/net/filter-rewriter.c > @@ -21,6 +21,7 @@ > #include "qemu/main-loop.h" > #include "qemu/iov.h" > #include "net/checksum.h" > +#include "trace.h" > =20 > #define FILTER_COLO_REWRITER(obj) \ > OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER) > @@ -62,6 +63,89 @@ static int is_tcp_packet(Packet *pkt) > } > } > =20 > +/* handle tcp packet from primary guest */ > +static int handle_primary_tcp_pkt(NetFilterState *nf, > + Connection *conn, > + Packet *pkt) > +{ > + struct tcphdr *tcp_pkt; > + > + tcp_pkt =3D (struct tcphdr *)pkt->transport_layer; > + if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) { > + char *sdebug, *ddebug; > + sdebug =3D strdup(inet_ntoa(pkt->ip->ip_src)); > + ddebug =3D 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); > + g_free(ddebug); > + } > + > + if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) =3D=3D TH_SYN)) { > + /* > + * we use this flag update offset func > + * run once in independent tcp connection > + */ > + conn->syn_flag =3D 1; > + } > + > + if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) =3D=3D TH_ACK)) { > + if (conn->syn_flag) { > + /* offset =3D secondary_seq - primary seq */ > + conn->offset -=3D (ntohl(tcp_pkt->th_ack) - 1); The code here is conflict with your comment above. Why need - 1 here? > + conn->syn_flag =3D 0; > + } > + /* handle packets to the secondary from the primary */ > + tcp_pkt->th_ack =3D htonl(ntohl(tcp_pkt->th_ack) + conn->offse= t); > + > + net_checksum_calculate((uint8_t *)pkt->data, pkt->size); > + } > + > + return 0; > +} > + > +/* handle tcp packet from secondary guest */ > +static int handle_secondary_tcp_pkt(NetFilterState *nf, > + Connection *conn, > + Packet *pkt) > +{ > + struct tcphdr *tcp_pkt; > + > + tcp_pkt =3D (struct tcphdr *)pkt->transport_layer; > + > + if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) { > + char *sdebug, *ddebug; > + sdebug =3D strdup(inet_ntoa(pkt->ip->ip_src)); > + ddebug =3D 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); > + g_free(ddebug); > + } > + > + if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) =3D=3D (TH_ACK | TH_S= YN))) { > + /* > + * save offset =3D secondary_seq and then > + * in handle_primary_tcp_pkt make offset > + * =3D secondary_seq - primary_seq > + */ > + conn->offset =3D ntohl(tcp_pkt->th_seq); > + } > + > + if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) =3D=3D TH_ACK) { > + /* handle packets to the primary from the secondary*/ > + tcp_pkt->th_seq =3D htonl(ntohl(tcp_pkt->th_seq) - conn->offse= t); > + > + net_checksum_calculate((uint8_t *)pkt->data, pkt->size); > + } > + > + return 0; > +} > + > static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, > NetClientState *sender, > unsigned flags, > @@ -101,10 +185,30 @@ static ssize_t colo_rewriter_receive_iov(NetFilte= rState *nf, > =20 > if (sender =3D=3D nf->netdev) { > /* NET_FILTER_DIRECTION_TX */ > - /* handle_primary_tcp_pkt */ > + if (!handle_primary_tcp_pkt(nf, conn, pkt)) { > + qemu_net_queue_send(s->incoming_queue, sender, 0, > + (const uint8_t *)pkt->data, pkt->size, NULL); > + packet_destroy(pkt, NULL); > + pkt =3D NULL; > + /* > + * We block the packet here,after rewrite pkt > + * and will send it > + */ > + return 1; > + } > } else { > /* NET_FILTER_DIRECTION_RX */ > - /* handle_secondary_tcp_pkt */ > + if (!handle_secondary_tcp_pkt(nf, conn, pkt)) { > + qemu_net_queue_send(s->incoming_queue, sender, 0, > + (const uint8_t *)pkt->data, pkt->size, NULL); > + packet_destroy(pkt, NULL); > + pkt =3D NULL; > + /* > + * We block the packet here,after rewrite pkt > + * and will send it > + */ > + return 1; > + } > } > } > =20 > diff --git a/trace-events b/trace-events > index 6686cdf..5ac56f6 100644 > --- a/trace-events > +++ b/trace-events > @@ -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 =3D %d, ip_src =3D %= s, ip_dst =3D %s, spkt size =3D %d, ip_src =3D %s, ip_dst =3D %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=3D%u/%u flags=3D%x\n" > +colo_filter_rewriter_conn_offset(uint32_t offset) ": offset=3D%u\n"