virtio-gpu: fix crashes upon warm reboot with vga mode
[qemu/ar7.git] / net / filter-rewriter.c
blobf584e4eba46b2317bc8d991a6992873875226e3b
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 "colo.h"
15 #include "net/filter.h"
16 #include "net/net.h"
17 #include "qemu-common.h"
18 #include "qemu/error-report.h"
19 #include "qom/object.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/iov.h"
22 #include "net/checksum.h"
24 #define FILTER_COLO_REWRITER(obj) \
25 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
27 #define TYPE_FILTER_REWRITER "filter-rewriter"
29 typedef struct RewriterState {
30 NetFilterState parent_obj;
31 NetQueue *incoming_queue;
32 /* hashtable to save connection */
33 GHashTable *connection_track_table;
34 bool vnet_hdr;
35 } RewriterState;
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);
48 * Return 1 on success, if return 0 means the pkt
49 * is not TCP packet
51 static int is_tcp_packet(Packet *pkt)
53 if (!parse_packet_early(pkt) &&
54 pkt->ip->ip_p == IPPROTO_TCP) {
55 return 1;
56 } else {
57 return 0;
61 /* handle tcp packet from primary guest */
62 static int handle_primary_tcp_pkt(NetFilterState *nf,
63 Connection *conn,
64 Packet *pkt)
66 struct tcphdr *tcp_pkt;
68 tcp_pkt = (struct tcphdr *)pkt->transport_header;
69 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
70 trace_colo_filter_rewriter_pkt_info(__func__,
71 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
72 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
73 tcp_pkt->th_flags);
74 trace_colo_filter_rewriter_conn_offset(conn->offset);
77 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
79 * we use this flag update offset func
80 * run once in independent tcp connection
82 conn->syn_flag = 1;
85 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
86 if (conn->syn_flag) {
88 * offset = secondary_seq - primary seq
89 * ack packet sent by guest from primary node,
90 * so we use th_ack - 1 get primary_seq
92 conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
93 conn->syn_flag = 0;
95 if (conn->offset) {
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->vnet_hdr_len,
100 pkt->size - pkt->vnet_hdr_len);
104 return 0;
107 /* handle tcp packet from secondary guest */
108 static int handle_secondary_tcp_pkt(NetFilterState *nf,
109 Connection *conn,
110 Packet *pkt)
112 struct tcphdr *tcp_pkt;
114 tcp_pkt = (struct tcphdr *)pkt->transport_header;
116 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
117 trace_colo_filter_rewriter_pkt_info(__func__,
118 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
119 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
120 tcp_pkt->th_flags);
121 trace_colo_filter_rewriter_conn_offset(conn->offset);
124 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
126 * save offset = secondary_seq and then
127 * in handle_primary_tcp_pkt make offset
128 * = secondary_seq - primary_seq
130 conn->offset = ntohl(tcp_pkt->th_seq);
133 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
134 /* Only need to adjust seq while offset is Non-zero */
135 if (conn->offset) {
136 /* handle packets to the primary from the secondary*/
137 tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
139 net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
140 pkt->size - pkt->vnet_hdr_len);
144 return 0;
147 static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
148 NetClientState *sender,
149 unsigned flags,
150 const struct iovec *iov,
151 int iovcnt,
152 NetPacketSent *sent_cb)
154 RewriterState *s = FILTER_COLO_REWRITER(nf);
155 Connection *conn;
156 ConnectionKey key;
157 Packet *pkt;
158 ssize_t size = iov_size(iov, iovcnt);
159 ssize_t vnet_hdr_len = 0;
160 char *buf = g_malloc0(size);
162 iov_to_buf(iov, iovcnt, 0, buf, size);
164 if (s->vnet_hdr) {
165 vnet_hdr_len = nf->netdev->vnet_hdr_len;
168 pkt = packet_new(buf, size, vnet_hdr_len);
169 g_free(buf);
172 * if we get tcp packet
173 * we will rewrite it to make secondary guest's
174 * connection established successfully
176 if (pkt && is_tcp_packet(pkt)) {
178 fill_connection_key(pkt, &key);
180 if (sender == nf->netdev) {
182 * We need make tcp TX and RX packet
183 * into one connection.
185 reverse_connection_key(&key);
187 conn = connection_get(s->connection_track_table,
188 &key,
189 NULL);
191 if (sender == nf->netdev) {
192 /* NET_FILTER_DIRECTION_TX */
193 if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
194 qemu_net_queue_send(s->incoming_queue, sender, 0,
195 (const uint8_t *)pkt->data, pkt->size, NULL);
196 packet_destroy(pkt, NULL);
197 pkt = NULL;
199 * We block the packet here,after rewrite pkt
200 * and will send it
202 return 1;
204 } else {
205 /* NET_FILTER_DIRECTION_RX */
206 if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
207 qemu_net_queue_send(s->incoming_queue, sender, 0,
208 (const uint8_t *)pkt->data, pkt->size, NULL);
209 packet_destroy(pkt, NULL);
210 pkt = NULL;
212 * We block the packet here,after rewrite pkt
213 * and will send it
215 return 1;
220 packet_destroy(pkt, NULL);
221 pkt = NULL;
222 return 0;
225 static void colo_rewriter_cleanup(NetFilterState *nf)
227 RewriterState *s = FILTER_COLO_REWRITER(nf);
229 /* flush packets */
230 if (s->incoming_queue) {
231 filter_rewriter_flush(nf);
232 g_free(s->incoming_queue);
236 static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
238 RewriterState *s = FILTER_COLO_REWRITER(nf);
240 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
241 connection_key_equal,
242 g_free,
243 connection_destroy);
244 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
247 static bool filter_rewriter_get_vnet_hdr(Object *obj, Error **errp)
249 RewriterState *s = FILTER_COLO_REWRITER(obj);
251 return s->vnet_hdr;
254 static void filter_rewriter_set_vnet_hdr(Object *obj,
255 bool value,
256 Error **errp)
258 RewriterState *s = FILTER_COLO_REWRITER(obj);
260 s->vnet_hdr = value;
263 static void filter_rewriter_init(Object *obj)
265 RewriterState *s = FILTER_COLO_REWRITER(obj);
267 s->vnet_hdr = false;
268 object_property_add_bool(obj, "vnet_hdr_support",
269 filter_rewriter_get_vnet_hdr,
270 filter_rewriter_set_vnet_hdr, NULL);
273 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
275 NetFilterClass *nfc = NETFILTER_CLASS(oc);
277 nfc->setup = colo_rewriter_setup;
278 nfc->cleanup = colo_rewriter_cleanup;
279 nfc->receive_iov = colo_rewriter_receive_iov;
282 static const TypeInfo colo_rewriter_info = {
283 .name = TYPE_FILTER_REWRITER,
284 .parent = TYPE_NETFILTER,
285 .class_init = colo_rewriter_class_init,
286 .instance_init = filter_rewriter_init,
287 .instance_size = sizeof(RewriterState),
290 static void register_types(void)
292 type_register_static(&colo_rewriter_info);
295 type_init(register_types);