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-common.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qemu/error-report.h"
21 #include "qapi-visit.h"
22 #include "qom/object.h"
23 #include "qemu/main-loop.h"
25 #include "net/checksum.h"
27 #define FILTER_COLO_REWRITER(obj) \
28 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
30 #define TYPE_FILTER_REWRITER "filter-rewriter"
32 typedef struct RewriterState
{
33 NetFilterState parent_obj
;
34 NetQueue
*incoming_queue
;
35 /* hashtable to save connection */
36 GHashTable
*connection_track_table
;
40 static void filter_rewriter_flush(NetFilterState
*nf
)
42 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
44 if (!qemu_net_queue_flush(s
->incoming_queue
)) {
45 /* Unable to empty the queue, purge remaining packets */
46 qemu_net_queue_purge(s
->incoming_queue
, nf
->netdev
);
51 * Return 1 on success, if return 0 means the pkt
54 static int is_tcp_packet(Packet
*pkt
)
56 if (!parse_packet_early(pkt
) &&
57 pkt
->ip
->ip_p
== IPPROTO_TCP
) {
64 /* handle tcp packet from primary guest */
65 static int handle_primary_tcp_pkt(NetFilterState
*nf
,
69 struct tcphdr
*tcp_pkt
;
71 tcp_pkt
= (struct tcphdr
*)pkt
->transport_header
;
72 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG
)) {
73 trace_colo_filter_rewriter_pkt_info(__func__
,
74 inet_ntoa(pkt
->ip
->ip_src
), inet_ntoa(pkt
->ip
->ip_dst
),
75 ntohl(tcp_pkt
->th_seq
), ntohl(tcp_pkt
->th_ack
),
77 trace_colo_filter_rewriter_conn_offset(conn
->offset
);
80 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_SYN
)) {
82 * we use this flag update offset func
83 * run once in independent tcp connection
88 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_ACK
)) {
91 * offset = secondary_seq - primary seq
92 * ack packet sent by guest from primary node,
93 * so we use th_ack - 1 get primary_seq
95 conn
->offset
-= (ntohl(tcp_pkt
->th_ack
) - 1);
99 /* handle packets to the secondary from the primary */
100 tcp_pkt
->th_ack
= htonl(ntohl(tcp_pkt
->th_ack
) + conn
->offset
);
102 net_checksum_calculate((uint8_t *)pkt
->data
, pkt
->size
);
109 /* handle tcp packet from secondary guest */
110 static int handle_secondary_tcp_pkt(NetFilterState
*nf
,
114 struct tcphdr
*tcp_pkt
;
116 tcp_pkt
= (struct tcphdr
*)pkt
->transport_header
;
118 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG
)) {
119 trace_colo_filter_rewriter_pkt_info(__func__
,
120 inet_ntoa(pkt
->ip
->ip_src
), inet_ntoa(pkt
->ip
->ip_dst
),
121 ntohl(tcp_pkt
->th_seq
), ntohl(tcp_pkt
->th_ack
),
123 trace_colo_filter_rewriter_conn_offset(conn
->offset
);
126 if (((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == (TH_ACK
| TH_SYN
))) {
128 * save offset = secondary_seq and then
129 * in handle_primary_tcp_pkt make offset
130 * = secondary_seq - primary_seq
132 conn
->offset
= ntohl(tcp_pkt
->th_seq
);
135 if ((tcp_pkt
->th_flags
& (TH_ACK
| TH_SYN
)) == TH_ACK
) {
136 /* Only need to adjust seq while offset is Non-zero */
138 /* handle packets to the primary from the secondary*/
139 tcp_pkt
->th_seq
= htonl(ntohl(tcp_pkt
->th_seq
) - conn
->offset
);
141 net_checksum_calculate((uint8_t *)pkt
->data
, pkt
->size
);
148 static ssize_t
colo_rewriter_receive_iov(NetFilterState
*nf
,
149 NetClientState
*sender
,
151 const struct iovec
*iov
,
153 NetPacketSent
*sent_cb
)
155 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
159 ssize_t size
= iov_size(iov
, iovcnt
);
160 ssize_t vnet_hdr_len
= 0;
161 char *buf
= g_malloc0(size
);
163 iov_to_buf(iov
, iovcnt
, 0, buf
, size
);
166 vnet_hdr_len
= nf
->netdev
->vnet_hdr_len
;
169 pkt
= packet_new(buf
, size
, vnet_hdr_len
);
173 * if we get tcp packet
174 * we will rewrite it to make secondary guest's
175 * connection established successfully
177 if (pkt
&& is_tcp_packet(pkt
)) {
179 fill_connection_key(pkt
, &key
);
181 if (sender
== nf
->netdev
) {
183 * We need make tcp TX and RX packet
184 * into one connection.
186 reverse_connection_key(&key
);
188 conn
= connection_get(s
->connection_track_table
,
192 if (sender
== nf
->netdev
) {
193 /* NET_FILTER_DIRECTION_TX */
194 if (!handle_primary_tcp_pkt(nf
, conn
, pkt
)) {
195 qemu_net_queue_send(s
->incoming_queue
, sender
, 0,
196 (const uint8_t *)pkt
->data
, pkt
->size
, NULL
);
197 packet_destroy(pkt
, NULL
);
200 * We block the packet here,after rewrite pkt
206 /* NET_FILTER_DIRECTION_RX */
207 if (!handle_secondary_tcp_pkt(nf
, conn
, pkt
)) {
208 qemu_net_queue_send(s
->incoming_queue
, sender
, 0,
209 (const uint8_t *)pkt
->data
, pkt
->size
, NULL
);
210 packet_destroy(pkt
, NULL
);
213 * We block the packet here,after rewrite pkt
221 packet_destroy(pkt
, NULL
);
226 static void colo_rewriter_cleanup(NetFilterState
*nf
)
228 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
231 if (s
->incoming_queue
) {
232 filter_rewriter_flush(nf
);
233 g_free(s
->incoming_queue
);
237 static void colo_rewriter_setup(NetFilterState
*nf
, Error
**errp
)
239 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
241 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
242 connection_key_equal
,
245 s
->incoming_queue
= qemu_new_net_queue(qemu_netfilter_pass_to_next
, nf
);
248 static bool filter_rewriter_get_vnet_hdr(Object
*obj
, Error
**errp
)
250 RewriterState
*s
= FILTER_COLO_REWRITER(obj
);
255 static void filter_rewriter_set_vnet_hdr(Object
*obj
,
259 RewriterState
*s
= FILTER_COLO_REWRITER(obj
);
264 static void filter_rewriter_init(Object
*obj
)
266 RewriterState
*s
= FILTER_COLO_REWRITER(obj
);
269 object_property_add_bool(obj
, "vnet_hdr_support",
270 filter_rewriter_get_vnet_hdr
,
271 filter_rewriter_set_vnet_hdr
, NULL
);
274 static void colo_rewriter_class_init(ObjectClass
*oc
, void *data
)
276 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
278 nfc
->setup
= colo_rewriter_setup
;
279 nfc
->cleanup
= colo_rewriter_cleanup
;
280 nfc
->receive_iov
= colo_rewriter_receive_iov
;
283 static const TypeInfo colo_rewriter_info
= {
284 .name
= TYPE_FILTER_REWRITER
,
285 .parent
= TYPE_NETFILTER
,
286 .class_init
= colo_rewriter_class_init
,
287 .instance_init
= filter_rewriter_init
,
288 .instance_size
= sizeof(RewriterState
),
291 static void register_types(void)
293 type_register_static(&colo_rewriter_info
);
296 type_init(register_types
);