qmp-events: move 'ACPI_DEVICE_OST' doc to schema
[qemu/ar7.git] / net / filter-rewriter.c
blobc4ab91cdee9cb1c816c774adbf5343a652a22197
1 /*
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"
13 #include "trace.h"
14 #include "net/colo.h"
15 #include "net/filter.h"
16 #include "net/net.h"
17 #include "qemu-common.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi-visit.h"
21 #include "qom/object.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/iov.h"
24 #include "net/checksum.h"
26 #define FILTER_COLO_REWRITER(obj) \
27 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
29 #define TYPE_FILTER_REWRITER "filter-rewriter"
31 typedef struct RewriterState {
32 NetFilterState parent_obj;
33 NetQueue *incoming_queue;
34 /* hashtable to save connection */
35 GHashTable *connection_track_table;
36 } RewriterState;
38 static void filter_rewriter_flush(NetFilterState *nf)
40 RewriterState *s = FILTER_COLO_REWRITER(nf);
42 if (!qemu_net_queue_flush(s->incoming_queue)) {
43 /* Unable to empty the queue, purge remaining packets */
44 qemu_net_queue_purge(s->incoming_queue, nf->netdev);
49 * Return 1 on success, if return 0 means the pkt
50 * is not TCP packet
52 static int is_tcp_packet(Packet *pkt)
54 if (!parse_packet_early(pkt) &&
55 pkt->ip->ip_p == IPPROTO_TCP) {
56 return 1;
57 } else {
58 return 0;
62 /* handle tcp packet from primary guest */
63 static int handle_primary_tcp_pkt(NetFilterState *nf,
64 Connection *conn,
65 Packet *pkt)
67 struct tcphdr *tcp_pkt;
69 tcp_pkt = (struct tcphdr *)pkt->transport_header;
70 if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
71 trace_colo_filter_rewriter_pkt_info(__func__,
72 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
73 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
74 tcp_pkt->th_flags);
75 trace_colo_filter_rewriter_conn_offset(conn->offset);
78 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
80 * we use this flag update offset func
81 * run once in independent tcp connection
83 conn->syn_flag = 1;
86 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
87 if (conn->syn_flag) {
89 * offset = secondary_seq - primary seq
90 * ack packet sent by guest from primary node,
91 * so we use th_ack - 1 get primary_seq
93 conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
94 conn->syn_flag = 0;
96 /* handle packets to the secondary from the primary */
97 tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
99 net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
102 return 0;
105 /* handle tcp packet from secondary guest */
106 static int handle_secondary_tcp_pkt(NetFilterState *nf,
107 Connection *conn,
108 Packet *pkt)
110 struct tcphdr *tcp_pkt;
112 tcp_pkt = (struct tcphdr *)pkt->transport_header;
114 if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
115 trace_colo_filter_rewriter_pkt_info(__func__,
116 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
117 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
118 tcp_pkt->th_flags);
119 trace_colo_filter_rewriter_conn_offset(conn->offset);
122 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
124 * save offset = secondary_seq and then
125 * in handle_primary_tcp_pkt make offset
126 * = secondary_seq - primary_seq
128 conn->offset = ntohl(tcp_pkt->th_seq);
131 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
132 /* handle packets to the primary from the secondary*/
133 tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
135 net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
138 return 0;
141 static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
142 NetClientState *sender,
143 unsigned flags,
144 const struct iovec *iov,
145 int iovcnt,
146 NetPacketSent *sent_cb)
148 RewriterState *s = FILTER_COLO_REWRITER(nf);
149 Connection *conn;
150 ConnectionKey key;
151 Packet *pkt;
152 ssize_t size = iov_size(iov, iovcnt);
153 char *buf = g_malloc0(size);
155 iov_to_buf(iov, iovcnt, 0, buf, size);
156 pkt = packet_new(buf, size);
157 g_free(buf);
160 * if we get tcp packet
161 * we will rewrite it to make secondary guest's
162 * connection established successfully
164 if (pkt && is_tcp_packet(pkt)) {
166 fill_connection_key(pkt, &key);
168 if (sender == nf->netdev) {
170 * We need make tcp TX and RX packet
171 * into one connection.
173 reverse_connection_key(&key);
175 conn = connection_get(s->connection_track_table,
176 &key,
177 NULL);
179 if (sender == nf->netdev) {
180 /* NET_FILTER_DIRECTION_TX */
181 if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
182 qemu_net_queue_send(s->incoming_queue, sender, 0,
183 (const uint8_t *)pkt->data, pkt->size, NULL);
184 packet_destroy(pkt, NULL);
185 pkt = NULL;
187 * We block the packet here,after rewrite pkt
188 * and will send it
190 return 1;
192 } else {
193 /* NET_FILTER_DIRECTION_RX */
194 if (!handle_secondary_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);
198 pkt = NULL;
200 * We block the packet here,after rewrite pkt
201 * and will send it
203 return 1;
208 packet_destroy(pkt, NULL);
209 pkt = NULL;
210 return 0;
213 static void colo_rewriter_cleanup(NetFilterState *nf)
215 RewriterState *s = FILTER_COLO_REWRITER(nf);
217 /* flush packets */
218 if (s->incoming_queue) {
219 filter_rewriter_flush(nf);
220 g_free(s->incoming_queue);
224 static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
226 RewriterState *s = FILTER_COLO_REWRITER(nf);
228 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
229 connection_key_equal,
230 g_free,
231 connection_destroy);
232 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
235 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
237 NetFilterClass *nfc = NETFILTER_CLASS(oc);
239 nfc->setup = colo_rewriter_setup;
240 nfc->cleanup = colo_rewriter_cleanup;
241 nfc->receive_iov = colo_rewriter_receive_iov;
244 static const TypeInfo colo_rewriter_info = {
245 .name = TYPE_FILTER_REWRITER,
246 .parent = TYPE_NETFILTER,
247 .class_init = colo_rewriter_class_init,
248 .instance_size = sizeof(RewriterState),
251 static void register_types(void)
253 type_register_static(&colo_rewriter_info);
256 type_init(register_types);