2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
15 #include "net/filter.h"
17 #include "qemu/error-report.h"
18 #include "qom/object.h"
19 #include "qemu/main-loop.h"
21 #include "net/checksum.h"
23 #include "migration/colo.h"
26 #define TYPE_FILTER_REWRITER "filter-rewriter"
27 OBJECT_DECLARE_SIMPLE_TYPE(RewriterState
, FILTER_REWRITER
)
29 #define FAILOVER_MODE_ON true
30 #define FAILOVER_MODE_OFF false
32 struct RewriterState
{
33 NetFilterState parent_obj
;
34 NetQueue
*incoming_queue
;
35 /* hashtable to save connection */
36 GHashTable
*connection_track_table
;
41 static void filter_rewriter_failover_mode(RewriterState
*s
)
43 s
->failover_mode
= FAILOVER_MODE_ON
;
46 static void filter_rewriter_flush(NetFilterState
*nf
)
48 RewriterState
*s
= FILTER_REWRITER(nf
);
50 if (!qemu_net_queue_flush(s
->incoming_queue
)) {
51 /* Unable to empty the queue, purge remaining packets */
52 qemu_net_queue_purge(s
->incoming_queue
, nf
->netdev
);
57 * Return 1 on success, if return 0 means the pkt
60 static int is_tcp_packet(Packet
*pkt
)
62 if (!parse_packet_early(pkt
) &&
63 pkt
->ip
->ip_p
== IPPROTO_TCP
) {
70 /* handle tcp packet from primary guest */
71 static int handle_primary_tcp_pkt(RewriterState
*rf
,
73 Packet
*pkt
, ConnectionKey
*key
)
75 struct tcp_hdr
*tcp_pkt
;
77 tcp_pkt
= (struct tcp_hdr
*)pkt
->transport_header
;
78 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_PKT_INFO
)) {
79 trace_colo_filter_rewriter_pkt_info(__func__
,
80 inet_ntoa(pkt
->ip
->ip_src
), inet_ntoa(pkt
->ip
->ip_dst
),
81 ntohl(tcp_pkt
->th_seq
), ntohl(tcp_pkt
->th_ack
),
84 if (trace_event_get_state_backends(
85 TRACE_COLO_FILTER_REWRITER_CONN_OFFSET
)) {
86 trace_colo_filter_rewriter_conn_offset(conn
->offset
);
89 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == (TH_ACK
| TH_SYN
)) &&
90 conn
->tcp_state
== TCPS_SYN_SENT
) {
91 conn
->tcp_state
= TCPS_ESTABLISHED
;
94 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_SYN
)) {
96 * we use this flag update offset func
97 * run once in independent tcp connection
99 conn
->tcp_state
= TCPS_SYN_RECEIVED
;
102 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_ACK
)) {
103 if (conn
->tcp_state
== TCPS_SYN_RECEIVED
) {
105 * offset = secondary_seq - primary seq
106 * ack packet sent by guest from primary node,
107 * so we use th_ack - 1 get primary_seq
109 conn
->offset
-= (ntohl(tcp_pkt
->th_ack
) - 1);
110 conn
->tcp_state
= TCPS_ESTABLISHED
;
113 /* handle packets to the secondary from the primary */
114 tcp_pkt
->th_ack
= htonl(ntohl(tcp_pkt
->th_ack
) + conn
->offset
);
116 net_checksum_calculate((uint8_t *)pkt
->data
+ pkt
->vnet_hdr_len
,
117 pkt
->size
- pkt
->vnet_hdr_len
);
121 * Passive close step 3
123 if ((conn
->tcp_state
== TCPS_LAST_ACK
) &&
124 (ntohl(tcp_pkt
->th_ack
) == (conn
->fin_ack_seq
+ 1))) {
125 conn
->tcp_state
= TCPS_CLOSED
;
126 g_hash_table_remove(rf
->connection_track_table
, key
);
130 if ((tcp_pkt
->th_flags
& TH_FIN
) == TH_FIN
) {
134 * The *server* side of this connect is VM, *client* tries to close
135 * the connection. We will into CLOSE_WAIT status.
138 * In this step we will into LAST_ACK status.
140 * We got 'fin=1, ack=1' packet from server side, we need to
141 * record the seq of 'fin=1, ack=1' packet.
144 * We got 'ack=1' packets from client side, it acks 'fin=1, ack=1'
145 * packet from server side. From this point, we can ensure that there
146 * will be no packets in the connection, except that, some errors
147 * happen between the path of 'filter object' and vNIC, if this rare
148 * case really happen, we can still create a new connection,
149 * So it is safe to remove the connection from connection_track_table.
152 if (conn
->tcp_state
== TCPS_ESTABLISHED
) {
153 conn
->tcp_state
= TCPS_CLOSE_WAIT
;
157 * Active close step 2.
159 if (conn
->tcp_state
== TCPS_FIN_WAIT_1
) {
161 * For simplify implementation, we needn't wait 2MSL time
162 * in filter rewriter. Because guest kernel will track the
163 * TCP status and wait 2MSL time, if client resend the FIN
164 * packet, guest will apply the last ACK too.
165 * So, we skip the TCPS_TIME_WAIT state here and go straight
166 * to TCPS_CLOSED state.
168 conn
->tcp_state
= TCPS_CLOSED
;
169 g_hash_table_remove(rf
->connection_track_table
, key
);
176 /* handle tcp packet from secondary guest */
177 static int handle_secondary_tcp_pkt(RewriterState
*rf
,
179 Packet
*pkt
, ConnectionKey
*key
)
181 struct tcp_hdr
*tcp_pkt
;
183 tcp_pkt
= (struct tcp_hdr
*)pkt
->transport_header
;
185 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_PKT_INFO
)) {
186 trace_colo_filter_rewriter_pkt_info(__func__
,
187 inet_ntoa(pkt
->ip
->ip_src
), inet_ntoa(pkt
->ip
->ip_dst
),
188 ntohl(tcp_pkt
->th_seq
), ntohl(tcp_pkt
->th_ack
),
191 if (trace_event_get_state_backends(
192 TRACE_COLO_FILTER_REWRITER_CONN_OFFSET
)) {
193 trace_colo_filter_rewriter_conn_offset(conn
->offset
);
196 if (conn
->tcp_state
== TCPS_SYN_RECEIVED
&&
197 ((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == (TH_ACK
| TH_SYN
))) {
199 * save offset = secondary_seq and then
200 * in handle_primary_tcp_pkt make offset
201 * = secondary_seq - primary_seq
203 conn
->offset
= ntohl(tcp_pkt
->th_seq
);
206 /* VM active connect */
207 if (conn
->tcp_state
== TCPS_CLOSED
&&
208 ((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_SYN
)) {
209 conn
->tcp_state
= TCPS_SYN_SENT
;
212 if ((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_ACK
) {
213 /* Only need to adjust seq while offset is Non-zero */
215 /* handle packets to the primary from the secondary*/
216 tcp_pkt
->th_seq
= htonl(ntohl(tcp_pkt
->th_seq
) - conn
->offset
);
218 net_checksum_calculate((uint8_t *)pkt
->data
+ pkt
->vnet_hdr_len
,
219 pkt
->size
- pkt
->vnet_hdr_len
);
224 * Passive close step 2:
226 if (conn
->tcp_state
== TCPS_CLOSE_WAIT
&&
227 (tcp_pkt
->th_flags
& (TH_ACK
| TH_FIN
)) == (TH_ACK
| TH_FIN
)) {
228 conn
->fin_ack_seq
= ntohl(tcp_pkt
->th_seq
);
229 conn
->tcp_state
= TCPS_LAST_ACK
;
236 * The *server* side of this connect is VM, *server* tries to close
240 * We will into CLOSE_WAIT status.
241 * We simplify the TCPS_FIN_WAIT_2, TCPS_TIME_WAIT and
244 if (conn
->tcp_state
== TCPS_ESTABLISHED
&&
245 (tcp_pkt
->th_flags
& (TH_ACK
| TH_FIN
)) == TH_FIN
) {
246 conn
->tcp_state
= TCPS_FIN_WAIT_1
;
252 static ssize_t
colo_rewriter_receive_iov(NetFilterState
*nf
,
253 NetClientState
*sender
,
255 const struct iovec
*iov
,
257 NetPacketSent
*sent_cb
)
259 RewriterState
*s
= FILTER_REWRITER(nf
);
263 ssize_t size
= iov_size(iov
, iovcnt
);
264 ssize_t vnet_hdr_len
= 0;
265 char *buf
= g_malloc0(size
);
267 iov_to_buf(iov
, iovcnt
, 0, buf
, size
);
270 vnet_hdr_len
= nf
->netdev
->vnet_hdr_len
;
273 pkt
= packet_new(buf
, size
, vnet_hdr_len
);
277 * if we get tcp packet
278 * we will rewrite it to make secondary guest's
279 * connection established successfully
281 if (pkt
&& is_tcp_packet(pkt
)) {
283 fill_connection_key(pkt
, &key
);
285 if (sender
== nf
->netdev
) {
287 * We need make tcp TX and RX packet
288 * into one connection.
290 reverse_connection_key(&key
);
293 /* After failover we needn't change new TCP packet */
294 if (s
->failover_mode
&&
295 !connection_has_tracked(s
->connection_track_table
, &key
)) {
299 conn
= connection_get(s
->connection_track_table
,
303 if (sender
== nf
->netdev
) {
304 /* NET_FILTER_DIRECTION_TX */
305 if (!handle_primary_tcp_pkt(s
, conn
, pkt
, &key
)) {
306 qemu_net_queue_send(s
->incoming_queue
, sender
, 0,
307 (const uint8_t *)pkt
->data
, pkt
->size
, NULL
);
308 packet_destroy(pkt
, NULL
);
311 * We block the packet here,after rewrite pkt
317 /* NET_FILTER_DIRECTION_RX */
318 if (!handle_secondary_tcp_pkt(s
, conn
, pkt
, &key
)) {
319 qemu_net_queue_send(s
->incoming_queue
, sender
, 0,
320 (const uint8_t *)pkt
->data
, pkt
->size
, NULL
);
321 packet_destroy(pkt
, NULL
);
324 * We block the packet here,after rewrite pkt
333 packet_destroy(pkt
, NULL
);
338 static void reset_seq_offset(gpointer key
, gpointer value
, gpointer user_data
)
340 Connection
*conn
= (Connection
*)value
;
345 static gboolean
offset_is_nonzero(gpointer key
,
349 Connection
*conn
= (Connection
*)value
;
351 return conn
->offset
? true : false;
354 static void colo_rewriter_handle_event(NetFilterState
*nf
, int event
,
357 RewriterState
*rs
= FILTER_REWRITER(nf
);
360 case COLO_EVENT_CHECKPOINT
:
361 g_hash_table_foreach(rs
->connection_track_table
,
362 reset_seq_offset
, NULL
);
364 case COLO_EVENT_FAILOVER
:
365 if (!g_hash_table_find(rs
->connection_track_table
,
366 offset_is_nonzero
, NULL
)) {
367 filter_rewriter_failover_mode(rs
);
375 static void colo_rewriter_cleanup(NetFilterState
*nf
)
377 RewriterState
*s
= FILTER_REWRITER(nf
);
380 if (s
->incoming_queue
) {
381 filter_rewriter_flush(nf
);
382 g_free(s
->incoming_queue
);
386 static void colo_rewriter_setup(NetFilterState
*nf
, Error
**errp
)
388 RewriterState
*s
= FILTER_REWRITER(nf
);
390 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
391 connection_key_equal
,
394 s
->incoming_queue
= qemu_new_net_queue(qemu_netfilter_pass_to_next
, nf
);
397 static bool filter_rewriter_get_vnet_hdr(Object
*obj
, Error
**errp
)
399 RewriterState
*s
= FILTER_REWRITER(obj
);
404 static void filter_rewriter_set_vnet_hdr(Object
*obj
,
408 RewriterState
*s
= FILTER_REWRITER(obj
);
413 static void filter_rewriter_init(Object
*obj
)
415 RewriterState
*s
= FILTER_REWRITER(obj
);
418 s
->failover_mode
= FAILOVER_MODE_OFF
;
419 object_property_add_bool(obj
, "vnet_hdr_support",
420 filter_rewriter_get_vnet_hdr
,
421 filter_rewriter_set_vnet_hdr
);
424 static void colo_rewriter_class_init(ObjectClass
*oc
, void *data
)
426 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
428 nfc
->setup
= colo_rewriter_setup
;
429 nfc
->cleanup
= colo_rewriter_cleanup
;
430 nfc
->receive_iov
= colo_rewriter_receive_iov
;
431 nfc
->handle_event
= colo_rewriter_handle_event
;
434 static const TypeInfo colo_rewriter_info
= {
435 .name
= TYPE_FILTER_REWRITER
,
436 .parent
= TYPE_NETFILTER
,
437 .class_init
= colo_rewriter_class_init
,
438 .instance_init
= filter_rewriter_init
,
439 .instance_size
= sizeof(RewriterState
),
442 static void register_types(void)
444 type_register_static(&colo_rewriter_info
);
447 type_init(register_types
);