]> git.proxmox.com Git - mirror_qemu.git/blame - net/filter-rewriter.c
Use OBJECT_DECLARE_TYPE when possible
[mirror_qemu.git] / net / filter-rewriter.c
CommitLineData
e6eee8ab
ZC
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"
30656b09 13#include "trace.h"
f27f01db 14#include "colo.h"
e6eee8ab
ZC
15#include "net/filter.h"
16#include "net/net.h"
4b39bdce 17#include "qemu/error-report.h"
e6eee8ab
ZC
18#include "qom/object.h"
19#include "qemu/main-loop.h"
20#include "qemu/iov.h"
21#include "net/checksum.h"
24525e93
ZC
22#include "net/colo.h"
23#include "migration/colo.h"
e05ae1d9 24#include "util.h"
e6eee8ab 25
e6eee8ab 26#define TYPE_FILTER_REWRITER "filter-rewriter"
db1015e9 27typedef struct RewriterState RewriterState;
50cd7d54 28DECLARE_INSTANCE_CHECKER(RewriterState, FILTER_REWRITER,
8110fa1d 29 TYPE_FILTER_REWRITER)
e6eee8ab 30
24525e93
ZC
31#define FAILOVER_MODE_ON true
32#define FAILOVER_MODE_OFF false
e6eee8ab 33
db1015e9 34struct RewriterState {
e6eee8ab
ZC
35 NetFilterState parent_obj;
36 NetQueue *incoming_queue;
37 /* hashtable to save connection */
38 GHashTable *connection_track_table;
4b39bdce 39 bool vnet_hdr;
24525e93 40 bool failover_mode;
db1015e9 41};
e6eee8ab 42
24525e93
ZC
43static void filter_rewriter_failover_mode(RewriterState *s)
44{
45 s->failover_mode = FAILOVER_MODE_ON;
46}
47
e6eee8ab
ZC
48static void filter_rewriter_flush(NetFilterState *nf)
49{
50cd7d54 50 RewriterState *s = FILTER_REWRITER(nf);
e6eee8ab
ZC
51
52 if (!qemu_net_queue_flush(s->incoming_queue)) {
53 /* Unable to empty the queue, purge remaining packets */
54 qemu_net_queue_purge(s->incoming_queue, nf->netdev);
55 }
56}
57
afe46124
ZC
58/*
59 * Return 1 on success, if return 0 means the pkt
60 * is not TCP packet
61 */
62static int is_tcp_packet(Packet *pkt)
63{
64 if (!parse_packet_early(pkt) &&
65 pkt->ip->ip_p == IPPROTO_TCP) {
66 return 1;
67 } else {
68 return 0;
69 }
70}
71
30656b09 72/* handle tcp packet from primary guest */
6214231a 73static int handle_primary_tcp_pkt(RewriterState *rf,
30656b09 74 Connection *conn,
6214231a 75 Packet *pkt, ConnectionKey *key)
30656b09 76{
e05ae1d9 77 struct tcp_hdr *tcp_pkt;
30656b09 78
e05ae1d9 79 tcp_pkt = (struct tcp_hdr *)pkt->transport_header;
8c8ed038 80 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_PKT_INFO)) {
2061c14c
ZC
81 trace_colo_filter_rewriter_pkt_info(__func__,
82 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
30656b09
ZC
83 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
84 tcp_pkt->th_flags);
8c8ed038
RB
85 }
86 if (trace_event_get_state_backends(
87 TRACE_COLO_FILTER_REWRITER_CONN_OFFSET)) {
30656b09 88 trace_colo_filter_rewriter_conn_offset(conn->offset);
30656b09
ZC
89 }
90
6214231a
ZC
91 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN)) &&
92 conn->tcp_state == TCPS_SYN_SENT) {
93 conn->tcp_state = TCPS_ESTABLISHED;
94 }
95
30656b09
ZC
96 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
97 /*
98 * we use this flag update offset func
99 * run once in independent tcp connection
100 */
6214231a 101 conn->tcp_state = TCPS_SYN_RECEIVED;
30656b09
ZC
102 }
103
104 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
6214231a 105 if (conn->tcp_state == TCPS_SYN_RECEIVED) {
30656b09
ZC
106 /*
107 * offset = secondary_seq - primary seq
108 * ack packet sent by guest from primary node,
109 * so we use th_ack - 1 get primary_seq
110 */
111 conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
6214231a 112 conn->tcp_state = TCPS_ESTABLISHED;
30656b09 113 }
db0a762e
HZ
114 if (conn->offset) {
115 /* handle packets to the secondary from the primary */
116 tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
30656b09 117
6ce310b5
ZC
118 net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
119 pkt->size - pkt->vnet_hdr_len);
db0a762e 120 }
6214231a
ZC
121
122 /*
123 * Passive close step 3
124 */
125 if ((conn->tcp_state == TCPS_LAST_ACK) &&
126 (ntohl(tcp_pkt->th_ack) == (conn->fin_ack_seq + 1))) {
127 conn->tcp_state = TCPS_CLOSED;
128 g_hash_table_remove(rf->connection_track_table, key);
129 }
130 }
131
132 if ((tcp_pkt->th_flags & TH_FIN) == TH_FIN) {
133 /*
134 * Passive close.
135 * Step 1:
136 * The *server* side of this connect is VM, *client* tries to close
137 * the connection. We will into CLOSE_WAIT status.
138 *
139 * Step 2:
140 * In this step we will into LAST_ACK status.
141 *
142 * We got 'fin=1, ack=1' packet from server side, we need to
143 * record the seq of 'fin=1, ack=1' packet.
144 *
145 * Step 3:
146 * We got 'ack=1' packets from client side, it acks 'fin=1, ack=1'
147 * packet from server side. From this point, we can ensure that there
148 * will be no packets in the connection, except that, some errors
149 * happen between the path of 'filter object' and vNIC, if this rare
150 * case really happen, we can still create a new connection,
151 * So it is safe to remove the connection from connection_track_table.
152 *
153 */
154 if (conn->tcp_state == TCPS_ESTABLISHED) {
155 conn->tcp_state = TCPS_CLOSE_WAIT;
156 }
157
158 /*
159 * Active close step 2.
160 */
161 if (conn->tcp_state == TCPS_FIN_WAIT_1) {
6214231a
ZC
162 /*
163 * For simplify implementation, we needn't wait 2MSL time
164 * in filter rewriter. Because guest kernel will track the
165 * TCP status and wait 2MSL time, if client resend the FIN
166 * packet, guest will apply the last ACK too.
013a6202
ZC
167 * So, we skip the TCPS_TIME_WAIT state here and go straight
168 * to TCPS_CLOSED state.
6214231a
ZC
169 */
170 conn->tcp_state = TCPS_CLOSED;
171 g_hash_table_remove(rf->connection_track_table, key);
172 }
30656b09
ZC
173 }
174
175 return 0;
176}
177
178/* handle tcp packet from secondary guest */
6214231a 179static int handle_secondary_tcp_pkt(RewriterState *rf,
30656b09 180 Connection *conn,
6214231a 181 Packet *pkt, ConnectionKey *key)
30656b09 182{
e05ae1d9 183 struct tcp_hdr *tcp_pkt;
30656b09 184
e05ae1d9 185 tcp_pkt = (struct tcp_hdr *)pkt->transport_header;
30656b09 186
8c8ed038 187 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_PKT_INFO)) {
2061c14c
ZC
188 trace_colo_filter_rewriter_pkt_info(__func__,
189 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
30656b09
ZC
190 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
191 tcp_pkt->th_flags);
8c8ed038
RB
192 }
193 if (trace_event_get_state_backends(
194 TRACE_COLO_FILTER_REWRITER_CONN_OFFSET)) {
30656b09 195 trace_colo_filter_rewriter_conn_offset(conn->offset);
30656b09
ZC
196 }
197
6214231a
ZC
198 if (conn->tcp_state == TCPS_SYN_RECEIVED &&
199 ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
30656b09
ZC
200 /*
201 * save offset = secondary_seq and then
202 * in handle_primary_tcp_pkt make offset
203 * = secondary_seq - primary_seq
204 */
205 conn->offset = ntohl(tcp_pkt->th_seq);
206 }
207
6214231a
ZC
208 /* VM active connect */
209 if (conn->tcp_state == TCPS_CLOSED &&
210 ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
211 conn->tcp_state = TCPS_SYN_SENT;
212 }
213
30656b09 214 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
db0a762e
HZ
215 /* Only need to adjust seq while offset is Non-zero */
216 if (conn->offset) {
217 /* handle packets to the primary from the secondary*/
218 tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
30656b09 219
6ce310b5
ZC
220 net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
221 pkt->size - pkt->vnet_hdr_len);
db0a762e 222 }
30656b09
ZC
223 }
224
6214231a
ZC
225 /*
226 * Passive close step 2:
227 */
228 if (conn->tcp_state == TCPS_CLOSE_WAIT &&
229 (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
230 conn->fin_ack_seq = ntohl(tcp_pkt->th_seq);
231 conn->tcp_state = TCPS_LAST_ACK;
232 }
233
234 /*
235 * Active close
236 *
237 * Step 1:
238 * The *server* side of this connect is VM, *server* tries to close
239 * the connection.
240 *
241 * Step 2:
242 * We will into CLOSE_WAIT status.
243 * We simplify the TCPS_FIN_WAIT_2, TCPS_TIME_WAIT and
244 * CLOSING status.
245 */
246 if (conn->tcp_state == TCPS_ESTABLISHED &&
247 (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == TH_FIN) {
248 conn->tcp_state = TCPS_FIN_WAIT_1;
249 }
250
30656b09
ZC
251 return 0;
252}
253
e6eee8ab
ZC
254static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
255 NetClientState *sender,
256 unsigned flags,
257 const struct iovec *iov,
258 int iovcnt,
259 NetPacketSent *sent_cb)
260{
50cd7d54 261 RewriterState *s = FILTER_REWRITER(nf);
afe46124
ZC
262 Connection *conn;
263 ConnectionKey key;
264 Packet *pkt;
265 ssize_t size = iov_size(iov, iovcnt);
4b39bdce 266 ssize_t vnet_hdr_len = 0;
afe46124
ZC
267 char *buf = g_malloc0(size);
268
269 iov_to_buf(iov, iovcnt, 0, buf, size);
4b39bdce
ZC
270
271 if (s->vnet_hdr) {
272 vnet_hdr_len = nf->netdev->vnet_hdr_len;
273 }
274
275 pkt = packet_new(buf, size, vnet_hdr_len);
2061c14c 276 g_free(buf);
afe46124 277
e6eee8ab
ZC
278 /*
279 * if we get tcp packet
280 * we will rewrite it to make secondary guest's
281 * connection established successfully
282 */
afe46124
ZC
283 if (pkt && is_tcp_packet(pkt)) {
284
285 fill_connection_key(pkt, &key);
286
287 if (sender == nf->netdev) {
288 /*
289 * We need make tcp TX and RX packet
290 * into one connection.
291 */
292 reverse_connection_key(&key);
293 }
24525e93
ZC
294
295 /* After failover we needn't change new TCP packet */
296 if (s->failover_mode &&
297 !connection_has_tracked(s->connection_track_table, &key)) {
298 goto out;
299 }
300
afe46124
ZC
301 conn = connection_get(s->connection_track_table,
302 &key,
303 NULL);
304
305 if (sender == nf->netdev) {
306 /* NET_FILTER_DIRECTION_TX */
6214231a 307 if (!handle_primary_tcp_pkt(s, conn, pkt, &key)) {
30656b09
ZC
308 qemu_net_queue_send(s->incoming_queue, sender, 0,
309 (const uint8_t *)pkt->data, pkt->size, NULL);
310 packet_destroy(pkt, NULL);
311 pkt = NULL;
312 /*
313 * We block the packet here,after rewrite pkt
314 * and will send it
315 */
316 return 1;
317 }
afe46124
ZC
318 } else {
319 /* NET_FILTER_DIRECTION_RX */
6214231a 320 if (!handle_secondary_tcp_pkt(s, conn, pkt, &key)) {
30656b09
ZC
321 qemu_net_queue_send(s->incoming_queue, sender, 0,
322 (const uint8_t *)pkt->data, pkt->size, NULL);
323 packet_destroy(pkt, NULL);
324 pkt = NULL;
325 /*
326 * We block the packet here,after rewrite pkt
327 * and will send it
328 */
329 return 1;
330 }
afe46124
ZC
331 }
332 }
333
24525e93 334out:
afe46124
ZC
335 packet_destroy(pkt, NULL);
336 pkt = NULL;
e6eee8ab
ZC
337 return 0;
338}
339
24525e93
ZC
340static void reset_seq_offset(gpointer key, gpointer value, gpointer user_data)
341{
342 Connection *conn = (Connection *)value;
343
344 conn->offset = 0;
345}
346
347static gboolean offset_is_nonzero(gpointer key,
348 gpointer value,
349 gpointer user_data)
350{
351 Connection *conn = (Connection *)value;
352
353 return conn->offset ? true : false;
354}
355
356static void colo_rewriter_handle_event(NetFilterState *nf, int event,
357 Error **errp)
358{
50cd7d54 359 RewriterState *rs = FILTER_REWRITER(nf);
24525e93
ZC
360
361 switch (event) {
362 case COLO_EVENT_CHECKPOINT:
363 g_hash_table_foreach(rs->connection_track_table,
364 reset_seq_offset, NULL);
365 break;
366 case COLO_EVENT_FAILOVER:
367 if (!g_hash_table_find(rs->connection_track_table,
368 offset_is_nonzero, NULL)) {
369 filter_rewriter_failover_mode(rs);
370 }
371 break;
372 default:
373 break;
374 }
375}
376
e6eee8ab
ZC
377static void colo_rewriter_cleanup(NetFilterState *nf)
378{
50cd7d54 379 RewriterState *s = FILTER_REWRITER(nf);
e6eee8ab
ZC
380
381 /* flush packets */
382 if (s->incoming_queue) {
383 filter_rewriter_flush(nf);
384 g_free(s->incoming_queue);
385 }
386}
387
388static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
389{
50cd7d54 390 RewriterState *s = FILTER_REWRITER(nf);
e6eee8ab
ZC
391
392 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
393 connection_key_equal,
394 g_free,
395 connection_destroy);
396 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
397}
398
4b39bdce
ZC
399static bool filter_rewriter_get_vnet_hdr(Object *obj, Error **errp)
400{
50cd7d54 401 RewriterState *s = FILTER_REWRITER(obj);
4b39bdce
ZC
402
403 return s->vnet_hdr;
404}
405
406static void filter_rewriter_set_vnet_hdr(Object *obj,
407 bool value,
408 Error **errp)
409{
50cd7d54 410 RewriterState *s = FILTER_REWRITER(obj);
4b39bdce
ZC
411
412 s->vnet_hdr = value;
413}
414
415static void filter_rewriter_init(Object *obj)
416{
50cd7d54 417 RewriterState *s = FILTER_REWRITER(obj);
4b39bdce
ZC
418
419 s->vnet_hdr = false;
24525e93 420 s->failover_mode = FAILOVER_MODE_OFF;
4b39bdce
ZC
421 object_property_add_bool(obj, "vnet_hdr_support",
422 filter_rewriter_get_vnet_hdr,
d2623129 423 filter_rewriter_set_vnet_hdr);
4b39bdce
ZC
424}
425
e6eee8ab
ZC
426static void colo_rewriter_class_init(ObjectClass *oc, void *data)
427{
428 NetFilterClass *nfc = NETFILTER_CLASS(oc);
429
430 nfc->setup = colo_rewriter_setup;
431 nfc->cleanup = colo_rewriter_cleanup;
432 nfc->receive_iov = colo_rewriter_receive_iov;
24525e93 433 nfc->handle_event = colo_rewriter_handle_event;
e6eee8ab
ZC
434}
435
436static const TypeInfo colo_rewriter_info = {
437 .name = TYPE_FILTER_REWRITER,
438 .parent = TYPE_NETFILTER,
439 .class_init = colo_rewriter_class_init,
4b39bdce 440 .instance_init = filter_rewriter_init,
e6eee8ab
ZC
441 .instance_size = sizeof(RewriterState),
442};
443
444static void register_types(void)
445{
446 type_register_static(&colo_rewriter_info);
447}
448
449type_init(register_types);