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"
14 #include "net/filter.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi-visit.h"
20 #include "qom/object.h"
21 #include "qemu/main-loop.h"
23 #include "net/checksum.h"
25 #define FILTER_COLO_REWRITER(obj) \
26 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
28 #define TYPE_FILTER_REWRITER "filter-rewriter"
30 typedef struct RewriterState
{
31 NetFilterState parent_obj
;
32 NetQueue
*incoming_queue
;
33 /* hashtable to save connection */
34 GHashTable
*connection_track_table
;
37 static void filter_rewriter_flush(NetFilterState
*nf
)
39 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
41 if (!qemu_net_queue_flush(s
->incoming_queue
)) {
42 /* Unable to empty the queue, purge remaining packets */
43 qemu_net_queue_purge(s
->incoming_queue
, nf
->netdev
);
47 static ssize_t
colo_rewriter_receive_iov(NetFilterState
*nf
,
48 NetClientState
*sender
,
50 const struct iovec
*iov
,
52 NetPacketSent
*sent_cb
)
55 * if we get tcp packet
56 * we will rewrite it to make secondary guest's
57 * connection established successfully
62 static void colo_rewriter_cleanup(NetFilterState
*nf
)
64 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
67 if (s
->incoming_queue
) {
68 filter_rewriter_flush(nf
);
69 g_free(s
->incoming_queue
);
73 static void colo_rewriter_setup(NetFilterState
*nf
, Error
**errp
)
75 RewriterState
*s
= FILTER_COLO_REWRITER(nf
);
77 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
81 s
->incoming_queue
= qemu_new_net_queue(qemu_netfilter_pass_to_next
, nf
);
84 static void colo_rewriter_class_init(ObjectClass
*oc
, void *data
)
86 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
88 nfc
->setup
= colo_rewriter_setup
;
89 nfc
->cleanup
= colo_rewriter_cleanup
;
90 nfc
->receive_iov
= colo_rewriter_receive_iov
;
93 static const TypeInfo colo_rewriter_info
= {
94 .name
= TYPE_FILTER_REWRITER
,
95 .parent
= TYPE_NETFILTER
,
96 .class_init
= colo_rewriter_class_init
,
97 .instance_size
= sizeof(RewriterState
),
100 static void register_types(void)
102 type_register_static(&colo_rewriter_info
);
105 type_init(register_types
);