Skip to content

Commit 30656b0

Browse files
zhangckidjasowang
authored andcommitted
filter-rewriter: rewrite tcp packet to keep secondary connection
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 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; Signed-off-by: Zhang Chen <[email protected]> Signed-off-by: Li Zhijian <[email protected]> Signed-off-by: Wen Congyang <[email protected]> Signed-off-by: Jason Wang <[email protected]>
1 parent afe4612 commit 30656b0

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

net/colo.c

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Connection *connection_new(ConnectionKey *key)
134134

135135
conn->ip_proto = key->ip_proto;
136136
conn->processing = false;
137+
conn->offset = 0;
138+
conn->syn_flag = 0;
137139
g_queue_init(&conn->primary_list);
138140
g_queue_init(&conn->secondary_list);
139141

net/colo.h

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ typedef struct Connection {
6262
/* flag to enqueue unprocessed_connections */
6363
bool processing;
6464
uint8_t ip_proto;
65+
/* offset = secondary_seq - primary_seq */
66+
tcp_seq offset;
67+
/*
68+
* we use this flag update offset func
69+
* run once in independent tcp connection
70+
*/
71+
int syn_flag;
6572
} Connection;
6673

6774
uint32_t connection_key_hash(const void *opaque);

net/filter-rewriter.c

+110-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include "qemu/osdep.h"
13+
#include "trace.h"
1314
#include "net/colo.h"
1415
#include "net/filter.h"
1516
#include "net/net.h"
@@ -58,6 +59,93 @@ static int is_tcp_packet(Packet *pkt)
5859
}
5960
}
6061

62+
/* handle tcp packet from primary guest */
63+
static int handle_primary_tcp_pkt(NetFilterState *nf,
64+
Connection *conn,
65+
Packet *pkt)
66+
{
67+
struct tcphdr *tcp_pkt;
68+
69+
tcp_pkt = (struct tcphdr *)pkt->transport_header;
70+
if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
71+
char *sdebug, *ddebug;
72+
sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
73+
ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
74+
trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
75+
ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
76+
tcp_pkt->th_flags);
77+
trace_colo_filter_rewriter_conn_offset(conn->offset);
78+
g_free(sdebug);
79+
g_free(ddebug);
80+
}
81+
82+
if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
83+
/*
84+
* we use this flag update offset func
85+
* run once in independent tcp connection
86+
*/
87+
conn->syn_flag = 1;
88+
}
89+
90+
if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
91+
if (conn->syn_flag) {
92+
/*
93+
* offset = secondary_seq - primary seq
94+
* ack packet sent by guest from primary node,
95+
* so we use th_ack - 1 get primary_seq
96+
*/
97+
conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
98+
conn->syn_flag = 0;
99+
}
100+
/* handle packets to the secondary from the primary */
101+
tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
102+
103+
net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
104+
}
105+
106+
return 0;
107+
}
108+
109+
/* handle tcp packet from secondary guest */
110+
static int handle_secondary_tcp_pkt(NetFilterState *nf,
111+
Connection *conn,
112+
Packet *pkt)
113+
{
114+
struct tcphdr *tcp_pkt;
115+
116+
tcp_pkt = (struct tcphdr *)pkt->transport_header;
117+
118+
if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
119+
char *sdebug, *ddebug;
120+
sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
121+
ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
122+
trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
123+
ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
124+
tcp_pkt->th_flags);
125+
trace_colo_filter_rewriter_conn_offset(conn->offset);
126+
g_free(sdebug);
127+
g_free(ddebug);
128+
}
129+
130+
if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
131+
/*
132+
* save offset = secondary_seq and then
133+
* in handle_primary_tcp_pkt make offset
134+
* = secondary_seq - primary_seq
135+
*/
136+
conn->offset = ntohl(tcp_pkt->th_seq);
137+
}
138+
139+
if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
140+
/* handle packets to the primary from the secondary*/
141+
tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
142+
143+
net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
144+
}
145+
146+
return 0;
147+
}
148+
61149
static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
62150
NetClientState *sender,
63151
unsigned flags,
@@ -97,10 +185,30 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
97185

98186
if (sender == nf->netdev) {
99187
/* NET_FILTER_DIRECTION_TX */
100-
/* handle_primary_tcp_pkt */
188+
if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
189+
qemu_net_queue_send(s->incoming_queue, sender, 0,
190+
(const uint8_t *)pkt->data, pkt->size, NULL);
191+
packet_destroy(pkt, NULL);
192+
pkt = NULL;
193+
/*
194+
* We block the packet here,after rewrite pkt
195+
* and will send it
196+
*/
197+
return 1;
198+
}
101199
} else {
102200
/* NET_FILTER_DIRECTION_RX */
103-
/* handle_secondary_tcp_pkt */
201+
if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
202+
qemu_net_queue_send(s->incoming_queue, sender, 0,
203+
(const uint8_t *)pkt->data, pkt->size, NULL);
204+
packet_destroy(pkt, NULL);
205+
pkt = NULL;
206+
/*
207+
* We block the packet here,after rewrite pkt
208+
* and will send it
209+
*/
210+
return 1;
211+
}
104212
}
105213
}
106214

trace-events

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, con
150150
colo_old_packet_check_found(int64_t old_time) "%" PRId64
151151
colo_compare_miscompare(void) ""
152152

153+
# net/filter-rewriter.c
154+
colo_filter_rewriter_debug(void) ""
155+
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"
156+
colo_filter_rewriter_conn_offset(uint32_t offset) ": offset=%u\n"
157+
153158
### Guest events, keep at bottom
154159

155160
# @vaddr: Access' virtual address.

0 commit comments

Comments
 (0)