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