]>
Commit | Line | Data |
---|---|---|
7dce4e6f ZC |
1 | /* |
2 | * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) | |
3 | * (a.k.a. Fault Tolerance or Continuous Replication) | |
4 | * | |
5 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. | |
6 | * Copyright (c) 2016 FUJITSU LIMITED | |
7 | * Copyright (c) 2016 Intel Corporation | |
8 | * | |
9 | * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
12 | * later. See the COPYING file in the top-level directory. | |
13 | */ | |
14 | ||
15 | #include "qemu/osdep.h" | |
16 | #include "qemu/error-report.h" | |
59509ec1 | 17 | #include "trace.h" |
7dce4e6f ZC |
18 | #include "qemu-common.h" |
19 | #include "qapi/qmp/qerror.h" | |
20 | #include "qapi/error.h" | |
21 | #include "net/net.h" | |
f4b61836 | 22 | #include "net/eth.h" |
7dce4e6f ZC |
23 | #include "qom/object_interfaces.h" |
24 | #include "qemu/iov.h" | |
25 | #include "qom/object.h" | |
26 | #include "qemu/typedefs.h" | |
27 | #include "net/queue.h" | |
4d43a603 | 28 | #include "chardev/char-fe.h" |
7dce4e6f ZC |
29 | #include "qemu/sockets.h" |
30 | #include "qapi-visit.h" | |
59509ec1 | 31 | #include "net/colo.h" |
7dce4e6f ZC |
32 | |
33 | #define TYPE_COLO_COMPARE "colo-compare" | |
34 | #define COLO_COMPARE(obj) \ | |
35 | OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE) | |
36 | ||
0682e15b | 37 | #define COMPARE_READ_LEN_MAX NET_BUFSIZE |
b6540d40 ZC |
38 | #define MAX_QUEUE_SIZE 1024 |
39 | ||
0682e15b ZC |
40 | /* TODO: Should be configurable */ |
41 | #define REGULAR_PACKET_CHECK_MS 3000 | |
42 | ||
59509ec1 ZC |
43 | /* |
44 | + CompareState ++ | |
45 | | | | |
46 | +---------------+ +---------------+ +---------------+ | |
47 | |conn list +--->conn +--------->conn | | |
48 | +---------------+ +---------------+ +---------------+ | |
49 | | | | | | | | |
50 | +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ | |
51 | |primary | |secondary |primary | |secondary | |
52 | |packet | |packet + |packet | |packet + | |
53 | +--------+ +--------+ +--------+ +--------+ | |
54 | | | | | | |
55 | +---v----+ +---v----+ +---v----+ +---v----+ | |
56 | |primary | |secondary |primary | |secondary | |
57 | |packet | |packet + |packet | |packet + | |
58 | +--------+ +--------+ +--------+ +--------+ | |
59 | | | | | | |
60 | +---v----+ +---v----+ +---v----+ +---v----+ | |
61 | |primary | |secondary |primary | |secondary | |
62 | |packet | |packet + |packet | |packet + | |
63 | +--------+ +--------+ +--------+ +--------+ | |
64 | */ | |
7dce4e6f ZC |
65 | typedef struct CompareState { |
66 | Object parent; | |
67 | ||
68 | char *pri_indev; | |
69 | char *sec_indev; | |
70 | char *outdev; | |
32a6ebec MAL |
71 | CharBackend chr_pri_in; |
72 | CharBackend chr_sec_in; | |
73 | CharBackend chr_out; | |
7dce4e6f ZC |
74 | SocketReadState pri_rs; |
75 | SocketReadState sec_rs; | |
aa3a7032 | 76 | bool vnet_hdr; |
59509ec1 | 77 | |
b6540d40 ZC |
78 | /* connection list: the connections belonged to this NIC could be found |
79 | * in this list. | |
80 | * element type: Connection | |
81 | */ | |
82 | GQueue conn_list; | |
59509ec1 ZC |
83 | /* hashtable to save connection */ |
84 | GHashTable *connection_track_table; | |
0682e15b ZC |
85 | /* compare thread, a thread for each NIC */ |
86 | QemuThread thread; | |
dfd917a9 | 87 | |
b43decb0 | 88 | GMainContext *worker_context; |
dfd917a9 | 89 | GMainLoop *compare_loop; |
7dce4e6f ZC |
90 | } CompareState; |
91 | ||
92 | typedef struct CompareClass { | |
93 | ObjectClass parent_class; | |
94 | } CompareClass; | |
95 | ||
59509ec1 ZC |
96 | enum { |
97 | PRIMARY_IN = 0, | |
98 | SECONDARY_IN, | |
99 | }; | |
100 | ||
3037e7a5 | 101 | static int compare_chr_send(CompareState *s, |
59509ec1 | 102 | const uint8_t *buf, |
aa3a7032 ZC |
103 | uint32_t size, |
104 | uint32_t vnet_hdr_len); | |
59509ec1 | 105 | |
a935cc31 ZC |
106 | static gint seq_sorter(Packet *a, Packet *b, gpointer data) |
107 | { | |
108 | struct tcphdr *atcp, *btcp; | |
109 | ||
110 | atcp = (struct tcphdr *)(a->transport_header); | |
111 | btcp = (struct tcphdr *)(b->transport_header); | |
112 | return ntohl(atcp->th_seq) - ntohl(btcp->th_seq); | |
113 | } | |
114 | ||
59509ec1 ZC |
115 | /* |
116 | * Return 0 on success, if return -1 means the pkt | |
117 | * is unsupported(arp and ipv6) and will be sent later | |
118 | */ | |
119 | static int packet_enqueue(CompareState *s, int mode) | |
120 | { | |
b6540d40 | 121 | ConnectionKey key; |
59509ec1 | 122 | Packet *pkt = NULL; |
b6540d40 | 123 | Connection *conn; |
59509ec1 ZC |
124 | |
125 | if (mode == PRIMARY_IN) { | |
ada1a33f ZC |
126 | pkt = packet_new(s->pri_rs.buf, |
127 | s->pri_rs.packet_len, | |
128 | s->pri_rs.vnet_hdr_len); | |
59509ec1 | 129 | } else { |
ada1a33f ZC |
130 | pkt = packet_new(s->sec_rs.buf, |
131 | s->sec_rs.packet_len, | |
132 | s->sec_rs.vnet_hdr_len); | |
59509ec1 ZC |
133 | } |
134 | ||
135 | if (parse_packet_early(pkt)) { | |
136 | packet_destroy(pkt, NULL); | |
137 | pkt = NULL; | |
138 | return -1; | |
139 | } | |
b6540d40 | 140 | fill_connection_key(pkt, &key); |
59509ec1 | 141 | |
b6540d40 ZC |
142 | conn = connection_get(s->connection_track_table, |
143 | &key, | |
144 | &s->conn_list); | |
59509ec1 | 145 | |
b6540d40 ZC |
146 | if (!conn->processing) { |
147 | g_queue_push_tail(&s->conn_list, conn); | |
148 | conn->processing = true; | |
149 | } | |
150 | ||
151 | if (mode == PRIMARY_IN) { | |
152 | if (g_queue_get_length(&conn->primary_list) <= | |
153 | MAX_QUEUE_SIZE) { | |
154 | g_queue_push_tail(&conn->primary_list, pkt); | |
a935cc31 ZC |
155 | if (conn->ip_proto == IPPROTO_TCP) { |
156 | g_queue_sort(&conn->primary_list, | |
157 | (GCompareDataFunc)seq_sorter, | |
158 | NULL); | |
159 | } | |
b6540d40 ZC |
160 | } else { |
161 | error_report("colo compare primary queue size too big," | |
162 | "drop packet"); | |
163 | } | |
164 | } else { | |
165 | if (g_queue_get_length(&conn->secondary_list) <= | |
166 | MAX_QUEUE_SIZE) { | |
167 | g_queue_push_tail(&conn->secondary_list, pkt); | |
a935cc31 ZC |
168 | if (conn->ip_proto == IPPROTO_TCP) { |
169 | g_queue_sort(&conn->secondary_list, | |
170 | (GCompareDataFunc)seq_sorter, | |
171 | NULL); | |
172 | } | |
b6540d40 ZC |
173 | } else { |
174 | error_report("colo compare secondary queue size too big," | |
175 | "drop packet"); | |
176 | } | |
177 | } | |
59509ec1 ZC |
178 | |
179 | return 0; | |
180 | } | |
181 | ||
0682e15b ZC |
182 | /* |
183 | * The IP packets sent by primary and secondary | |
184 | * will be compared in here | |
185 | * TODO support ip fragment, Out-Of-Order | |
186 | * return: 0 means packet same | |
187 | * > 0 || < 0 means packet different | |
188 | */ | |
6f5009c3 ZC |
189 | static int colo_packet_compare_common(Packet *ppkt, |
190 | Packet *spkt, | |
191 | int poffset, | |
192 | int soffset) | |
0682e15b | 193 | { |
d87aa138 | 194 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
195 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
196 | ||
197 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
198 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
199 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
200 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
201 | ||
202 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
203 | pri_ip_dst, spkt->size, | |
204 | sec_ip_src, sec_ip_dst); | |
205 | } | |
0682e15b | 206 | |
6f5009c3 ZC |
207 | poffset = ppkt->vnet_hdr_len + poffset; |
208 | soffset = ppkt->vnet_hdr_len + soffset; | |
d63b366a | 209 | |
6f5009c3 ZC |
210 | if (ppkt->size - poffset == spkt->size - soffset) { |
211 | return memcmp(ppkt->data + poffset, | |
212 | spkt->data + soffset, | |
213 | spkt->size - soffset); | |
0682e15b | 214 | } else { |
2ad7ca4c | 215 | trace_colo_compare_main("Net packet size are not the same"); |
0682e15b ZC |
216 | return -1; |
217 | } | |
218 | } | |
219 | ||
f4b61836 ZC |
220 | /* |
221 | * Called from the compare thread on the primary | |
222 | * for compare tcp packet | |
223 | * compare_tcp copied from Dr. David Alan Gilbert's branch | |
224 | */ | |
225 | static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt) | |
0682e15b | 226 | { |
f4b61836 ZC |
227 | struct tcphdr *ptcp, *stcp; |
228 | int res; | |
f4b61836 ZC |
229 | |
230 | trace_colo_compare_main("compare tcp"); | |
2ad7ca4c | 231 | |
f4b61836 ZC |
232 | ptcp = (struct tcphdr *)ppkt->transport_header; |
233 | stcp = (struct tcphdr *)spkt->transport_header; | |
234 | ||
235 | /* | |
236 | * The 'identification' field in the IP header is *very* random | |
237 | * it almost never matches. Fudge this by ignoring differences in | |
238 | * unfragmented packets; they'll normally sort themselves out if different | |
239 | * anyway, and it should recover at the TCP level. | |
240 | * An alternative would be to get both the primary and secondary to rewrite | |
241 | * somehow; but that would need some sync traffic to sync the state | |
242 | */ | |
243 | if (ntohs(ppkt->ip->ip_off) & IP_DF) { | |
244 | spkt->ip->ip_id = ppkt->ip->ip_id; | |
245 | /* and the sum will be different if the IDs were different */ | |
246 | spkt->ip->ip_sum = ppkt->ip->ip_sum; | |
247 | } | |
248 | ||
184d4d42 ZC |
249 | /* |
250 | * Check tcp header length for tcp option field. | |
251 | * th_off > 5 means this tcp packet have options field. | |
252 | * The tcp options maybe always different. | |
253 | * for example: | |
254 | * From RFC 7323. | |
255 | * TCP Timestamps option (TSopt): | |
256 | * Kind: 8 | |
257 | * | |
258 | * Length: 10 bytes | |
259 | * | |
260 | * +-------+-------+---------------------+---------------------+ | |
261 | * |Kind=8 | 10 | TS Value (TSval) |TS Echo Reply (TSecr)| | |
262 | * +-------+-------+---------------------+---------------------+ | |
263 | * 1 1 4 4 | |
264 | * | |
265 | * In this case the primary guest's timestamp always different with | |
266 | * the secondary guest's timestamp. COLO just focus on payload, | |
267 | * so we just need skip this field. | |
268 | */ | |
269 | if (ptcp->th_off > 5) { | |
6f5009c3 ZC |
270 | ptrdiff_t ptcp_offset, stcp_offset; |
271 | ||
272 | ptcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data | |
273 | + (ptcp->th_off * 4) - ppkt->vnet_hdr_len; | |
274 | stcp_offset = spkt->transport_header - (uint8_t *)spkt->data | |
275 | + (stcp->th_off * 4) - spkt->vnet_hdr_len; | |
276 | ||
277 | /* | |
278 | * When network is busy, some tcp options(like sack) will unpredictable | |
279 | * occur in primary side or secondary side. it will make packet size | |
280 | * not same, but the two packet's payload is identical. colo just | |
281 | * care about packet payload, so we skip the option field. | |
282 | */ | |
283 | res = colo_packet_compare_common(ppkt, spkt, ptcp_offset, stcp_offset); | |
184d4d42 | 284 | } else if (ptcp->th_sum == stcp->th_sum) { |
6f5009c3 | 285 | res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN, ETH_HLEN); |
6efeb328 ZC |
286 | } else { |
287 | res = -1; | |
288 | } | |
f4b61836 | 289 | |
d87aa138 SH |
290 | if (res != 0 && |
291 | trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { | |
f583dca9 ZC |
292 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
293 | ||
294 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
295 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
296 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
297 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
298 | ||
299 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
300 | pri_ip_dst, spkt->size, | |
301 | sec_ip_src, sec_ip_dst); | |
302 | ||
303 | trace_colo_compare_tcp_info("pri tcp packet", | |
304 | ntohl(ptcp->th_seq), | |
305 | ntohl(ptcp->th_ack), | |
306 | res, ptcp->th_flags, | |
307 | ppkt->size); | |
308 | ||
309 | trace_colo_compare_tcp_info("sec tcp packet", | |
310 | ntohl(stcp->th_seq), | |
311 | ntohl(stcp->th_ack), | |
312 | res, stcp->th_flags, | |
313 | spkt->size); | |
2061c14c ZC |
314 | |
315 | qemu_hexdump((char *)ppkt->data, stderr, | |
316 | "colo-compare ppkt", ppkt->size); | |
317 | qemu_hexdump((char *)spkt->data, stderr, | |
318 | "colo-compare spkt", spkt->size); | |
f4b61836 ZC |
319 | } |
320 | ||
321 | return res; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Called from the compare thread on the primary | |
326 | * for compare udp packet | |
327 | */ | |
328 | static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) | |
329 | { | |
330 | int ret; | |
6efeb328 | 331 | int network_header_length = ppkt->ip->ip_hl * 4; |
f4b61836 ZC |
332 | |
333 | trace_colo_compare_main("compare udp"); | |
2ad7ca4c | 334 | |
6efeb328 ZC |
335 | /* |
336 | * Because of ppkt and spkt are both in the same connection, | |
337 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
338 | * same with spkt. In addition, IP header's Identification is a random | |
339 | * field, we can handle it in IP fragmentation function later. | |
340 | * COLO just concern the response net packet payload from primary guest | |
341 | * and secondary guest are same or not, So we ignored all IP header include | |
342 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
343 | * the ip payload here. | |
344 | */ | |
345 | ret = colo_packet_compare_common(ppkt, spkt, | |
6f5009c3 | 346 | network_header_length + ETH_HLEN, |
6efeb328 | 347 | network_header_length + ETH_HLEN); |
f4b61836 ZC |
348 | |
349 | if (ret) { | |
350 | trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); | |
f4b61836 | 351 | trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); |
d87aa138 | 352 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
353 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
354 | ppkt->size); | |
355 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
356 | spkt->size); | |
357 | } | |
f4b61836 ZC |
358 | } |
359 | ||
360 | return ret; | |
361 | } | |
362 | ||
363 | /* | |
364 | * Called from the compare thread on the primary | |
365 | * for compare icmp packet | |
366 | */ | |
367 | static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) | |
368 | { | |
6efeb328 ZC |
369 | int network_header_length = ppkt->ip->ip_hl * 4; |
370 | ||
f4b61836 | 371 | trace_colo_compare_main("compare icmp"); |
f4b61836 | 372 | |
6efeb328 ZC |
373 | /* |
374 | * Because of ppkt and spkt are both in the same connection, | |
375 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
376 | * same with spkt. In addition, IP header's Identification is a random | |
377 | * field, we can handle it in IP fragmentation function later. | |
378 | * COLO just concern the response net packet payload from primary guest | |
379 | * and secondary guest are same or not, So we ignored all IP header include | |
380 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
381 | * the ip payload here. | |
382 | */ | |
383 | if (colo_packet_compare_common(ppkt, spkt, | |
6f5009c3 | 384 | network_header_length + ETH_HLEN, |
6efeb328 | 385 | network_header_length + ETH_HLEN)) { |
f4b61836 ZC |
386 | trace_colo_compare_icmp_miscompare("primary pkt size", |
387 | ppkt->size); | |
f4b61836 ZC |
388 | trace_colo_compare_icmp_miscompare("Secondary pkt size", |
389 | spkt->size); | |
d87aa138 | 390 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
391 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
392 | ppkt->size); | |
393 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
394 | spkt->size); | |
395 | } | |
f4b61836 ZC |
396 | return -1; |
397 | } else { | |
398 | return 0; | |
399 | } | |
400 | } | |
401 | ||
402 | /* | |
403 | * Called from the compare thread on the primary | |
404 | * for compare other packet | |
405 | */ | |
406 | static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) | |
407 | { | |
408 | trace_colo_compare_main("compare other"); | |
d87aa138 | 409 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
410 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
411 | ||
412 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
413 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
414 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
415 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
416 | ||
417 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
418 | pri_ip_dst, spkt->size, | |
419 | sec_ip_src, sec_ip_dst); | |
420 | } | |
421 | ||
6f5009c3 | 422 | return colo_packet_compare_common(ppkt, spkt, 0, 0); |
0682e15b ZC |
423 | } |
424 | ||
425 | static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) | |
426 | { | |
427 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
428 | ||
429 | if ((now - pkt->creation_ms) > (*check_time)) { | |
430 | trace_colo_old_packet_check_found(pkt->creation_ms); | |
431 | return 0; | |
432 | } else { | |
433 | return 1; | |
434 | } | |
435 | } | |
436 | ||
d25a7dab ZC |
437 | static int colo_old_packet_check_one_conn(Connection *conn, |
438 | void *user_data) | |
0682e15b | 439 | { |
0682e15b ZC |
440 | GList *result = NULL; |
441 | int64_t check_time = REGULAR_PACKET_CHECK_MS; | |
442 | ||
443 | result = g_queue_find_custom(&conn->primary_list, | |
444 | &check_time, | |
445 | (GCompareFunc)colo_old_packet_check_one); | |
446 | ||
447 | if (result) { | |
448 | /* do checkpoint will flush old packet */ | |
449 | /* TODO: colo_notify_checkpoint();*/ | |
d25a7dab | 450 | return 0; |
0682e15b | 451 | } |
d25a7dab ZC |
452 | |
453 | return 1; | |
0682e15b ZC |
454 | } |
455 | ||
456 | /* | |
457 | * Look for old packets that the secondary hasn't matched, | |
458 | * if we have some then we have to checkpoint to wake | |
459 | * the secondary up. | |
460 | */ | |
461 | static void colo_old_packet_check(void *opaque) | |
462 | { | |
463 | CompareState *s = opaque; | |
464 | ||
d25a7dab ZC |
465 | /* |
466 | * If we find one old packet, stop finding job and notify | |
467 | * COLO frame do checkpoint. | |
468 | */ | |
469 | g_queue_find_custom(&s->conn_list, NULL, | |
470 | (GCompareFunc)colo_old_packet_check_one_conn); | |
0682e15b ZC |
471 | } |
472 | ||
473 | /* | |
474 | * Called from the compare thread on the primary | |
475 | * for compare connection | |
476 | */ | |
477 | static void colo_compare_connection(void *opaque, void *user_data) | |
478 | { | |
479 | CompareState *s = user_data; | |
480 | Connection *conn = opaque; | |
481 | Packet *pkt = NULL; | |
482 | GList *result = NULL; | |
483 | int ret; | |
484 | ||
485 | while (!g_queue_is_empty(&conn->primary_list) && | |
486 | !g_queue_is_empty(&conn->secondary_list)) { | |
626bba98 | 487 | pkt = g_queue_pop_head(&conn->primary_list); |
f4b61836 ZC |
488 | switch (conn->ip_proto) { |
489 | case IPPROTO_TCP: | |
490 | result = g_queue_find_custom(&conn->secondary_list, | |
491 | pkt, (GCompareFunc)colo_packet_compare_tcp); | |
492 | break; | |
493 | case IPPROTO_UDP: | |
494 | result = g_queue_find_custom(&conn->secondary_list, | |
495 | pkt, (GCompareFunc)colo_packet_compare_udp); | |
496 | break; | |
497 | case IPPROTO_ICMP: | |
498 | result = g_queue_find_custom(&conn->secondary_list, | |
499 | pkt, (GCompareFunc)colo_packet_compare_icmp); | |
500 | break; | |
501 | default: | |
502 | result = g_queue_find_custom(&conn->secondary_list, | |
503 | pkt, (GCompareFunc)colo_packet_compare_other); | |
504 | break; | |
505 | } | |
0682e15b ZC |
506 | |
507 | if (result) { | |
aa3a7032 ZC |
508 | ret = compare_chr_send(s, |
509 | pkt->data, | |
510 | pkt->size, | |
511 | pkt->vnet_hdr_len); | |
0682e15b ZC |
512 | if (ret < 0) { |
513 | error_report("colo_send_primary_packet failed"); | |
514 | } | |
515 | trace_colo_compare_main("packet same and release packet"); | |
516 | g_queue_remove(&conn->secondary_list, result->data); | |
517 | packet_destroy(pkt, NULL); | |
518 | } else { | |
519 | /* | |
520 | * If one packet arrive late, the secondary_list or | |
521 | * primary_list will be empty, so we can't compare it | |
522 | * until next comparison. | |
523 | */ | |
524 | trace_colo_compare_main("packet different"); | |
626bba98 | 525 | g_queue_push_head(&conn->primary_list, pkt); |
0682e15b ZC |
526 | /* TODO: colo_notify_checkpoint();*/ |
527 | break; | |
528 | } | |
529 | } | |
530 | } | |
531 | ||
3037e7a5 | 532 | static int compare_chr_send(CompareState *s, |
59509ec1 | 533 | const uint8_t *buf, |
aa3a7032 ZC |
534 | uint32_t size, |
535 | uint32_t vnet_hdr_len) | |
59509ec1 ZC |
536 | { |
537 | int ret = 0; | |
538 | uint32_t len = htonl(size); | |
539 | ||
540 | if (!size) { | |
541 | return 0; | |
542 | } | |
543 | ||
3037e7a5 | 544 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); |
59509ec1 ZC |
545 | if (ret != sizeof(len)) { |
546 | goto err; | |
547 | } | |
548 | ||
aa3a7032 ZC |
549 | if (s->vnet_hdr) { |
550 | /* | |
551 | * We send vnet header len make other module(like filter-redirector) | |
552 | * know how to parse net packet correctly. | |
553 | */ | |
554 | len = htonl(vnet_hdr_len); | |
555 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); | |
556 | if (ret != sizeof(len)) { | |
557 | goto err; | |
558 | } | |
559 | } | |
560 | ||
3037e7a5 | 561 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size); |
59509ec1 ZC |
562 | if (ret != size) { |
563 | goto err; | |
564 | } | |
565 | ||
566 | return 0; | |
567 | ||
568 | err: | |
569 | return ret < 0 ? ret : -EIO; | |
570 | } | |
571 | ||
0682e15b ZC |
572 | static int compare_chr_can_read(void *opaque) |
573 | { | |
574 | return COMPARE_READ_LEN_MAX; | |
575 | } | |
576 | ||
577 | /* | |
578 | * Called from the main thread on the primary for packets | |
579 | * arriving over the socket from the primary. | |
580 | */ | |
581 | static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) | |
582 | { | |
583 | CompareState *s = COLO_COMPARE(opaque); | |
584 | int ret; | |
585 | ||
586 | ret = net_fill_rstate(&s->pri_rs, buf, size); | |
587 | if (ret == -1) { | |
81517ba3 | 588 | qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 589 | NULL, NULL, true); |
0682e15b ZC |
590 | error_report("colo-compare primary_in error"); |
591 | } | |
592 | } | |
593 | ||
594 | /* | |
595 | * Called from the main thread on the primary for packets | |
596 | * arriving over the socket from the secondary. | |
597 | */ | |
598 | static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size) | |
599 | { | |
600 | CompareState *s = COLO_COMPARE(opaque); | |
601 | int ret; | |
602 | ||
603 | ret = net_fill_rstate(&s->sec_rs, buf, size); | |
604 | if (ret == -1) { | |
81517ba3 | 605 | qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 606 | NULL, NULL, true); |
0682e15b ZC |
607 | error_report("colo-compare secondary_in error"); |
608 | } | |
609 | } | |
610 | ||
66d2a242 HZ |
611 | /* |
612 | * Check old packet regularly so it can watch for any packets | |
613 | * that the secondary hasn't produced equivalents of. | |
614 | */ | |
615 | static gboolean check_old_packet_regular(void *opaque) | |
616 | { | |
617 | CompareState *s = opaque; | |
618 | ||
619 | /* if have old packet we will notify checkpoint */ | |
620 | colo_old_packet_check(s); | |
621 | ||
622 | return TRUE; | |
623 | } | |
624 | ||
0682e15b ZC |
625 | static void *colo_compare_thread(void *opaque) |
626 | { | |
0682e15b | 627 | CompareState *s = opaque; |
66d2a242 | 628 | GSource *timeout_source; |
0682e15b | 629 | |
b43decb0 | 630 | s->worker_context = g_main_context_new(); |
0682e15b | 631 | |
5345fdb4 | 632 | qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, |
81517ba3 AN |
633 | compare_pri_chr_in, NULL, NULL, |
634 | s, s->worker_context, true); | |
5345fdb4 | 635 | qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, |
81517ba3 AN |
636 | compare_sec_chr_in, NULL, NULL, |
637 | s, s->worker_context, true); | |
0682e15b | 638 | |
b43decb0 | 639 | s->compare_loop = g_main_loop_new(s->worker_context, FALSE); |
0682e15b | 640 | |
66d2a242 HZ |
641 | /* To kick any packets that the secondary doesn't match */ |
642 | timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS); | |
643 | g_source_set_callback(timeout_source, | |
644 | (GSourceFunc)check_old_packet_regular, s, NULL); | |
b43decb0 | 645 | g_source_attach(timeout_source, s->worker_context); |
66d2a242 | 646 | |
dfd917a9 | 647 | g_main_loop_run(s->compare_loop); |
0682e15b | 648 | |
66d2a242 | 649 | g_source_unref(timeout_source); |
dfd917a9 | 650 | g_main_loop_unref(s->compare_loop); |
b43decb0 | 651 | g_main_context_unref(s->worker_context); |
0682e15b ZC |
652 | return NULL; |
653 | } | |
654 | ||
7dce4e6f ZC |
655 | static char *compare_get_pri_indev(Object *obj, Error **errp) |
656 | { | |
657 | CompareState *s = COLO_COMPARE(obj); | |
658 | ||
659 | return g_strdup(s->pri_indev); | |
660 | } | |
661 | ||
662 | static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) | |
663 | { | |
664 | CompareState *s = COLO_COMPARE(obj); | |
665 | ||
666 | g_free(s->pri_indev); | |
667 | s->pri_indev = g_strdup(value); | |
668 | } | |
669 | ||
670 | static char *compare_get_sec_indev(Object *obj, Error **errp) | |
671 | { | |
672 | CompareState *s = COLO_COMPARE(obj); | |
673 | ||
674 | return g_strdup(s->sec_indev); | |
675 | } | |
676 | ||
677 | static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) | |
678 | { | |
679 | CompareState *s = COLO_COMPARE(obj); | |
680 | ||
681 | g_free(s->sec_indev); | |
682 | s->sec_indev = g_strdup(value); | |
683 | } | |
684 | ||
685 | static char *compare_get_outdev(Object *obj, Error **errp) | |
686 | { | |
687 | CompareState *s = COLO_COMPARE(obj); | |
688 | ||
689 | return g_strdup(s->outdev); | |
690 | } | |
691 | ||
692 | static void compare_set_outdev(Object *obj, const char *value, Error **errp) | |
693 | { | |
694 | CompareState *s = COLO_COMPARE(obj); | |
695 | ||
696 | g_free(s->outdev); | |
697 | s->outdev = g_strdup(value); | |
698 | } | |
699 | ||
aa3a7032 ZC |
700 | static bool compare_get_vnet_hdr(Object *obj, Error **errp) |
701 | { | |
702 | CompareState *s = COLO_COMPARE(obj); | |
703 | ||
704 | return s->vnet_hdr; | |
705 | } | |
706 | ||
707 | static void compare_set_vnet_hdr(Object *obj, | |
708 | bool value, | |
709 | Error **errp) | |
710 | { | |
711 | CompareState *s = COLO_COMPARE(obj); | |
712 | ||
713 | s->vnet_hdr = value; | |
714 | } | |
715 | ||
7dce4e6f ZC |
716 | static void compare_pri_rs_finalize(SocketReadState *pri_rs) |
717 | { | |
59509ec1 ZC |
718 | CompareState *s = container_of(pri_rs, CompareState, pri_rs); |
719 | ||
720 | if (packet_enqueue(s, PRIMARY_IN)) { | |
721 | trace_colo_compare_main("primary: unsupported packet in"); | |
aa3a7032 ZC |
722 | compare_chr_send(s, |
723 | pri_rs->buf, | |
724 | pri_rs->packet_len, | |
725 | pri_rs->vnet_hdr_len); | |
0682e15b ZC |
726 | } else { |
727 | /* compare connection */ | |
728 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 729 | } |
7dce4e6f ZC |
730 | } |
731 | ||
732 | static void compare_sec_rs_finalize(SocketReadState *sec_rs) | |
733 | { | |
59509ec1 ZC |
734 | CompareState *s = container_of(sec_rs, CompareState, sec_rs); |
735 | ||
736 | if (packet_enqueue(s, SECONDARY_IN)) { | |
737 | trace_colo_compare_main("secondary: unsupported packet in"); | |
0682e15b ZC |
738 | } else { |
739 | /* compare connection */ | |
740 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 741 | } |
7dce4e6f ZC |
742 | } |
743 | ||
7dce4e6f ZC |
744 | |
745 | /* | |
746 | * Return 0 is success. | |
747 | * Return 1 is failed. | |
748 | */ | |
0ec7b3e7 | 749 | static int find_and_check_chardev(Chardev **chr, |
7dce4e6f ZC |
750 | char *chr_name, |
751 | Error **errp) | |
752 | { | |
7dce4e6f ZC |
753 | *chr = qemu_chr_find(chr_name); |
754 | if (*chr == NULL) { | |
755 | error_setg(errp, "Device '%s' not found", | |
756 | chr_name); | |
757 | return 1; | |
758 | } | |
759 | ||
0a73336d DB |
760 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { |
761 | error_setg(errp, "chardev \"%s\" is not reconnectable", | |
7dce4e6f ZC |
762 | chr_name); |
763 | return 1; | |
764 | } | |
fbf3cc3a | 765 | |
7dce4e6f ZC |
766 | return 0; |
767 | } | |
768 | ||
769 | /* | |
770 | * Called from the main thread on the primary | |
771 | * to setup colo-compare. | |
772 | */ | |
773 | static void colo_compare_complete(UserCreatable *uc, Error **errp) | |
774 | { | |
775 | CompareState *s = COLO_COMPARE(uc); | |
0ec7b3e7 | 776 | Chardev *chr; |
0682e15b ZC |
777 | char thread_name[64]; |
778 | static int compare_id; | |
7dce4e6f ZC |
779 | |
780 | if (!s->pri_indev || !s->sec_indev || !s->outdev) { | |
781 | error_setg(errp, "colo compare needs 'primary_in' ," | |
782 | "'secondary_in','outdev' property set"); | |
783 | return; | |
784 | } else if (!strcmp(s->pri_indev, s->outdev) || | |
785 | !strcmp(s->sec_indev, s->outdev) || | |
786 | !strcmp(s->pri_indev, s->sec_indev)) { | |
787 | error_setg(errp, "'indev' and 'outdev' could not be same " | |
788 | "for compare module"); | |
789 | return; | |
790 | } | |
791 | ||
5345fdb4 MAL |
792 | if (find_and_check_chardev(&chr, s->pri_indev, errp) || |
793 | !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { | |
7dce4e6f ZC |
794 | return; |
795 | } | |
796 | ||
5345fdb4 MAL |
797 | if (find_and_check_chardev(&chr, s->sec_indev, errp) || |
798 | !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { | |
7dce4e6f ZC |
799 | return; |
800 | } | |
801 | ||
5345fdb4 MAL |
802 | if (find_and_check_chardev(&chr, s->outdev, errp) || |
803 | !qemu_chr_fe_init(&s->chr_out, chr, errp)) { | |
7dce4e6f ZC |
804 | return; |
805 | } | |
806 | ||
aa3a7032 ZC |
807 | net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr); |
808 | net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr); | |
7dce4e6f | 809 | |
b6540d40 ZC |
810 | g_queue_init(&s->conn_list); |
811 | ||
812 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, | |
813 | connection_key_equal, | |
814 | g_free, | |
815 | connection_destroy); | |
59509ec1 | 816 | |
0682e15b ZC |
817 | sprintf(thread_name, "colo-compare %d", compare_id); |
818 | qemu_thread_create(&s->thread, thread_name, | |
819 | colo_compare_thread, s, | |
820 | QEMU_THREAD_JOINABLE); | |
821 | compare_id++; | |
822 | ||
7dce4e6f ZC |
823 | return; |
824 | } | |
825 | ||
dfd917a9 HZ |
826 | static void colo_flush_packets(void *opaque, void *user_data) |
827 | { | |
828 | CompareState *s = user_data; | |
829 | Connection *conn = opaque; | |
830 | Packet *pkt = NULL; | |
831 | ||
832 | while (!g_queue_is_empty(&conn->primary_list)) { | |
833 | pkt = g_queue_pop_head(&conn->primary_list); | |
aa3a7032 ZC |
834 | compare_chr_send(s, |
835 | pkt->data, | |
836 | pkt->size, | |
837 | pkt->vnet_hdr_len); | |
dfd917a9 HZ |
838 | packet_destroy(pkt, NULL); |
839 | } | |
840 | while (!g_queue_is_empty(&conn->secondary_list)) { | |
841 | pkt = g_queue_pop_head(&conn->secondary_list); | |
842 | packet_destroy(pkt, NULL); | |
843 | } | |
844 | } | |
845 | ||
7dce4e6f ZC |
846 | static void colo_compare_class_init(ObjectClass *oc, void *data) |
847 | { | |
848 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
849 | ||
850 | ucc->complete = colo_compare_complete; | |
851 | } | |
852 | ||
853 | static void colo_compare_init(Object *obj) | |
854 | { | |
aa3a7032 ZC |
855 | CompareState *s = COLO_COMPARE(obj); |
856 | ||
7dce4e6f ZC |
857 | object_property_add_str(obj, "primary_in", |
858 | compare_get_pri_indev, compare_set_pri_indev, | |
859 | NULL); | |
860 | object_property_add_str(obj, "secondary_in", | |
861 | compare_get_sec_indev, compare_set_sec_indev, | |
862 | NULL); | |
863 | object_property_add_str(obj, "outdev", | |
864 | compare_get_outdev, compare_set_outdev, | |
865 | NULL); | |
aa3a7032 ZC |
866 | |
867 | s->vnet_hdr = false; | |
868 | object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr, | |
869 | compare_set_vnet_hdr, NULL); | |
7dce4e6f ZC |
870 | } |
871 | ||
872 | static void colo_compare_finalize(Object *obj) | |
873 | { | |
874 | CompareState *s = COLO_COMPARE(obj); | |
875 | ||
1ce2610c MAL |
876 | qemu_chr_fe_deinit(&s->chr_pri_in, false); |
877 | qemu_chr_fe_deinit(&s->chr_sec_in, false); | |
878 | qemu_chr_fe_deinit(&s->chr_out, false); | |
7dce4e6f | 879 | |
dfd917a9 HZ |
880 | g_main_loop_quit(s->compare_loop); |
881 | qemu_thread_join(&s->thread); | |
b6540d40 | 882 | |
dfd917a9 HZ |
883 | /* Release all unhandled packets after compare thead exited */ |
884 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
885 | ||
727c2d76 | 886 | g_queue_clear(&s->conn_list); |
0682e15b | 887 | |
dfd917a9 | 888 | g_hash_table_destroy(s->connection_track_table); |
7dce4e6f ZC |
889 | g_free(s->pri_indev); |
890 | g_free(s->sec_indev); | |
891 | g_free(s->outdev); | |
892 | } | |
893 | ||
894 | static const TypeInfo colo_compare_info = { | |
895 | .name = TYPE_COLO_COMPARE, | |
896 | .parent = TYPE_OBJECT, | |
897 | .instance_size = sizeof(CompareState), | |
898 | .instance_init = colo_compare_init, | |
899 | .instance_finalize = colo_compare_finalize, | |
900 | .class_size = sizeof(CompareClass), | |
901 | .class_init = colo_compare_class_init, | |
902 | .interfaces = (InterfaceInfo[]) { | |
903 | { TYPE_USER_CREATABLE }, | |
904 | { } | |
905 | } | |
906 | }; | |
907 | ||
908 | static void register_types(void) | |
909 | { | |
910 | type_register_static(&colo_compare_info); | |
911 | } | |
912 | ||
913 | type_init(register_types); |