]> git.proxmox.com Git - mirror_qemu.git/blob - net/filter-rewriter.c
filter-rewriter: introduce filter-rewriter initialization
[mirror_qemu.git] / net / filter-rewriter.c
1 /*
2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
7 *
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.
10 */
11
12 #include "qemu/osdep.h"
13 #include "net/colo.h"
14 #include "net/filter.h"
15 #include "net/net.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"
22 #include "qemu/iov.h"
23 #include "net/checksum.h"
24
25 #define FILTER_COLO_REWRITER(obj) \
26 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
27
28 #define TYPE_FILTER_REWRITER "filter-rewriter"
29
30 typedef struct RewriterState {
31 NetFilterState parent_obj;
32 NetQueue *incoming_queue;
33 /* hashtable to save connection */
34 GHashTable *connection_track_table;
35 } RewriterState;
36
37 static void filter_rewriter_flush(NetFilterState *nf)
38 {
39 RewriterState *s = FILTER_COLO_REWRITER(nf);
40
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);
44 }
45 }
46
47 static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
48 NetClientState *sender,
49 unsigned flags,
50 const struct iovec *iov,
51 int iovcnt,
52 NetPacketSent *sent_cb)
53 {
54 /*
55 * if we get tcp packet
56 * we will rewrite it to make secondary guest's
57 * connection established successfully
58 */
59 return 0;
60 }
61
62 static void colo_rewriter_cleanup(NetFilterState *nf)
63 {
64 RewriterState *s = FILTER_COLO_REWRITER(nf);
65
66 /* flush packets */
67 if (s->incoming_queue) {
68 filter_rewriter_flush(nf);
69 g_free(s->incoming_queue);
70 }
71 }
72
73 static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
74 {
75 RewriterState *s = FILTER_COLO_REWRITER(nf);
76
77 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
78 connection_key_equal,
79 g_free,
80 connection_destroy);
81 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
82 }
83
84 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
85 {
86 NetFilterClass *nfc = NETFILTER_CLASS(oc);
87
88 nfc->setup = colo_rewriter_setup;
89 nfc->cleanup = colo_rewriter_cleanup;
90 nfc->receive_iov = colo_rewriter_receive_iov;
91 }
92
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),
98 };
99
100 static void register_types(void)
101 {
102 type_register_static(&colo_rewriter_info);
103 }
104
105 type_init(register_types);