]> git.proxmox.com Git - mirror_qemu.git/blob - net/colo-compare.c
9f5968dc0b8040f8331ffa38e6b3c91b2fd7a2e0
[mirror_qemu.git] / net / colo-compare.c
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"
17 #include "trace.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/error.h"
21 #include "net/net.h"
22 #include "net/eth.h"
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"
28 #include "sysemu/char.h"
29 #include "qemu/sockets.h"
30 #include "qapi-visit.h"
31 #include "net/colo.h"
32
33 #define TYPE_COLO_COMPARE "colo-compare"
34 #define COLO_COMPARE(obj) \
35 OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
36
37 #define COMPARE_READ_LEN_MAX NET_BUFSIZE
38 #define MAX_QUEUE_SIZE 1024
39
40 /* TODO: Should be configurable */
41 #define REGULAR_PACKET_CHECK_MS 3000
42
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 */
65 typedef struct CompareState {
66 Object parent;
67
68 char *pri_indev;
69 char *sec_indev;
70 char *outdev;
71 CharBackend chr_pri_in;
72 CharBackend chr_sec_in;
73 CharBackend chr_out;
74 SocketReadState pri_rs;
75 SocketReadState sec_rs;
76
77 /* connection list: the connections belonged to this NIC could be found
78 * in this list.
79 * element type: Connection
80 */
81 GQueue conn_list;
82 /* hashtable to save connection */
83 GHashTable *connection_track_table;
84 /* compare thread, a thread for each NIC */
85 QemuThread thread;
86
87 GMainContext *worker_context;
88 GMainLoop *compare_loop;
89 } CompareState;
90
91 typedef struct CompareClass {
92 ObjectClass parent_class;
93 } CompareClass;
94
95 enum {
96 PRIMARY_IN = 0,
97 SECONDARY_IN,
98 };
99
100 static int compare_chr_send(CharBackend *out,
101 const uint8_t *buf,
102 uint32_t size);
103
104 static gint seq_sorter(Packet *a, Packet *b, gpointer data)
105 {
106 struct tcphdr *atcp, *btcp;
107
108 atcp = (struct tcphdr *)(a->transport_header);
109 btcp = (struct tcphdr *)(b->transport_header);
110 return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
111 }
112
113 /*
114 * Return 0 on success, if return -1 means the pkt
115 * is unsupported(arp and ipv6) and will be sent later
116 */
117 static int packet_enqueue(CompareState *s, int mode)
118 {
119 ConnectionKey key;
120 Packet *pkt = NULL;
121 Connection *conn;
122
123 if (mode == PRIMARY_IN) {
124 pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len);
125 } else {
126 pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len);
127 }
128
129 if (parse_packet_early(pkt)) {
130 packet_destroy(pkt, NULL);
131 pkt = NULL;
132 return -1;
133 }
134 fill_connection_key(pkt, &key);
135
136 conn = connection_get(s->connection_track_table,
137 &key,
138 &s->conn_list);
139
140 if (!conn->processing) {
141 g_queue_push_tail(&s->conn_list, conn);
142 conn->processing = true;
143 }
144
145 if (mode == PRIMARY_IN) {
146 if (g_queue_get_length(&conn->primary_list) <=
147 MAX_QUEUE_SIZE) {
148 g_queue_push_tail(&conn->primary_list, pkt);
149 if (conn->ip_proto == IPPROTO_TCP) {
150 g_queue_sort(&conn->primary_list,
151 (GCompareDataFunc)seq_sorter,
152 NULL);
153 }
154 } else {
155 error_report("colo compare primary queue size too big,"
156 "drop packet");
157 }
158 } else {
159 if (g_queue_get_length(&conn->secondary_list) <=
160 MAX_QUEUE_SIZE) {
161 g_queue_push_tail(&conn->secondary_list, pkt);
162 if (conn->ip_proto == IPPROTO_TCP) {
163 g_queue_sort(&conn->secondary_list,
164 (GCompareDataFunc)seq_sorter,
165 NULL);
166 }
167 } else {
168 error_report("colo compare secondary queue size too big,"
169 "drop packet");
170 }
171 }
172
173 return 0;
174 }
175
176 /*
177 * The IP packets sent by primary and secondary
178 * will be compared in here
179 * TODO support ip fragment, Out-Of-Order
180 * return: 0 means packet same
181 * > 0 || < 0 means packet different
182 */
183 static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
184 {
185 trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
186 inet_ntoa(ppkt->ip->ip_dst), spkt->size,
187 inet_ntoa(spkt->ip->ip_src),
188 inet_ntoa(spkt->ip->ip_dst));
189
190 if (ppkt->size == spkt->size) {
191 return memcmp(ppkt->data + offset, spkt->data + offset,
192 spkt->size - offset);
193 } else {
194 trace_colo_compare_main("Net packet size are not the same");
195 return -1;
196 }
197 }
198
199 /*
200 * Called from the compare thread on the primary
201 * for compare tcp packet
202 * compare_tcp copied from Dr. David Alan Gilbert's branch
203 */
204 static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
205 {
206 struct tcphdr *ptcp, *stcp;
207 int res;
208
209 trace_colo_compare_main("compare tcp");
210
211 ptcp = (struct tcphdr *)ppkt->transport_header;
212 stcp = (struct tcphdr *)spkt->transport_header;
213
214 /*
215 * The 'identification' field in the IP header is *very* random
216 * it almost never matches. Fudge this by ignoring differences in
217 * unfragmented packets; they'll normally sort themselves out if different
218 * anyway, and it should recover at the TCP level.
219 * An alternative would be to get both the primary and secondary to rewrite
220 * somehow; but that would need some sync traffic to sync the state
221 */
222 if (ntohs(ppkt->ip->ip_off) & IP_DF) {
223 spkt->ip->ip_id = ppkt->ip->ip_id;
224 /* and the sum will be different if the IDs were different */
225 spkt->ip->ip_sum = ppkt->ip->ip_sum;
226 }
227
228 if (ptcp->th_sum == stcp->th_sum) {
229 res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
230 } else {
231 res = -1;
232 }
233
234 if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
235 trace_colo_compare_pkt_info_src(inet_ntoa(ppkt->ip->ip_src),
236 ntohl(stcp->th_seq),
237 ntohl(stcp->th_ack),
238 res, stcp->th_flags,
239 spkt->size);
240
241 trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt->ip->ip_dst),
242 ntohl(ptcp->th_seq),
243 ntohl(ptcp->th_ack),
244 res, ptcp->th_flags,
245 ppkt->size);
246
247 qemu_hexdump((char *)ppkt->data, stderr,
248 "colo-compare ppkt", ppkt->size);
249 qemu_hexdump((char *)spkt->data, stderr,
250 "colo-compare spkt", spkt->size);
251 }
252
253 return res;
254 }
255
256 /*
257 * Called from the compare thread on the primary
258 * for compare udp packet
259 */
260 static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
261 {
262 int ret;
263 int network_header_length = ppkt->ip->ip_hl * 4;
264
265 trace_colo_compare_main("compare udp");
266
267 /*
268 * Because of ppkt and spkt are both in the same connection,
269 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
270 * same with spkt. In addition, IP header's Identification is a random
271 * field, we can handle it in IP fragmentation function later.
272 * COLO just concern the response net packet payload from primary guest
273 * and secondary guest are same or not, So we ignored all IP header include
274 * other field like TOS,TTL,IP Checksum. we only need to compare
275 * the ip payload here.
276 */
277 ret = colo_packet_compare_common(ppkt, spkt,
278 network_header_length + ETH_HLEN);
279
280 if (ret) {
281 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
282 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
283 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
284 qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
285 }
286
287 return ret;
288 }
289
290 /*
291 * Called from the compare thread on the primary
292 * for compare icmp packet
293 */
294 static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
295 {
296 int network_header_length = ppkt->ip->ip_hl * 4;
297
298 trace_colo_compare_main("compare icmp");
299
300 /*
301 * Because of ppkt and spkt are both in the same connection,
302 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
303 * same with spkt. In addition, IP header's Identification is a random
304 * field, we can handle it in IP fragmentation function later.
305 * COLO just concern the response net packet payload from primary guest
306 * and secondary guest are same or not, So we ignored all IP header include
307 * other field like TOS,TTL,IP Checksum. we only need to compare
308 * the ip payload here.
309 */
310 if (colo_packet_compare_common(ppkt, spkt,
311 network_header_length + ETH_HLEN)) {
312 trace_colo_compare_icmp_miscompare("primary pkt size",
313 ppkt->size);
314 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare",
315 ppkt->size);
316 trace_colo_compare_icmp_miscompare("Secondary pkt size",
317 spkt->size);
318 qemu_hexdump((char *)spkt->data, stderr, "colo-compare",
319 spkt->size);
320 return -1;
321 } else {
322 return 0;
323 }
324 }
325
326 /*
327 * Called from the compare thread on the primary
328 * for compare other packet
329 */
330 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
331 {
332 trace_colo_compare_main("compare other");
333 trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
334 inet_ntoa(ppkt->ip->ip_dst), spkt->size,
335 inet_ntoa(spkt->ip->ip_src),
336 inet_ntoa(spkt->ip->ip_dst));
337 return colo_packet_compare_common(ppkt, spkt, 0);
338 }
339
340 static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
341 {
342 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
343
344 if ((now - pkt->creation_ms) > (*check_time)) {
345 trace_colo_old_packet_check_found(pkt->creation_ms);
346 return 0;
347 } else {
348 return 1;
349 }
350 }
351
352 static void colo_old_packet_check_one_conn(void *opaque,
353 void *user_data)
354 {
355 Connection *conn = opaque;
356 GList *result = NULL;
357 int64_t check_time = REGULAR_PACKET_CHECK_MS;
358
359 result = g_queue_find_custom(&conn->primary_list,
360 &check_time,
361 (GCompareFunc)colo_old_packet_check_one);
362
363 if (result) {
364 /* do checkpoint will flush old packet */
365 /* TODO: colo_notify_checkpoint();*/
366 }
367 }
368
369 /*
370 * Look for old packets that the secondary hasn't matched,
371 * if we have some then we have to checkpoint to wake
372 * the secondary up.
373 */
374 static void colo_old_packet_check(void *opaque)
375 {
376 CompareState *s = opaque;
377
378 g_queue_foreach(&s->conn_list, colo_old_packet_check_one_conn, NULL);
379 }
380
381 /*
382 * Called from the compare thread on the primary
383 * for compare connection
384 */
385 static void colo_compare_connection(void *opaque, void *user_data)
386 {
387 CompareState *s = user_data;
388 Connection *conn = opaque;
389 Packet *pkt = NULL;
390 GList *result = NULL;
391 int ret;
392
393 while (!g_queue_is_empty(&conn->primary_list) &&
394 !g_queue_is_empty(&conn->secondary_list)) {
395 pkt = g_queue_pop_tail(&conn->primary_list);
396 switch (conn->ip_proto) {
397 case IPPROTO_TCP:
398 result = g_queue_find_custom(&conn->secondary_list,
399 pkt, (GCompareFunc)colo_packet_compare_tcp);
400 break;
401 case IPPROTO_UDP:
402 result = g_queue_find_custom(&conn->secondary_list,
403 pkt, (GCompareFunc)colo_packet_compare_udp);
404 break;
405 case IPPROTO_ICMP:
406 result = g_queue_find_custom(&conn->secondary_list,
407 pkt, (GCompareFunc)colo_packet_compare_icmp);
408 break;
409 default:
410 result = g_queue_find_custom(&conn->secondary_list,
411 pkt, (GCompareFunc)colo_packet_compare_other);
412 break;
413 }
414
415 if (result) {
416 ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
417 if (ret < 0) {
418 error_report("colo_send_primary_packet failed");
419 }
420 trace_colo_compare_main("packet same and release packet");
421 g_queue_remove(&conn->secondary_list, result->data);
422 packet_destroy(pkt, NULL);
423 } else {
424 /*
425 * If one packet arrive late, the secondary_list or
426 * primary_list will be empty, so we can't compare it
427 * until next comparison.
428 */
429 trace_colo_compare_main("packet different");
430 g_queue_push_tail(&conn->primary_list, pkt);
431 /* TODO: colo_notify_checkpoint();*/
432 break;
433 }
434 }
435 }
436
437 static int compare_chr_send(CharBackend *out,
438 const uint8_t *buf,
439 uint32_t size)
440 {
441 int ret = 0;
442 uint32_t len = htonl(size);
443
444 if (!size) {
445 return 0;
446 }
447
448 ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
449 if (ret != sizeof(len)) {
450 goto err;
451 }
452
453 ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
454 if (ret != size) {
455 goto err;
456 }
457
458 return 0;
459
460 err:
461 return ret < 0 ? ret : -EIO;
462 }
463
464 static int compare_chr_can_read(void *opaque)
465 {
466 return COMPARE_READ_LEN_MAX;
467 }
468
469 /*
470 * Called from the main thread on the primary for packets
471 * arriving over the socket from the primary.
472 */
473 static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
474 {
475 CompareState *s = COLO_COMPARE(opaque);
476 int ret;
477
478 ret = net_fill_rstate(&s->pri_rs, buf, size);
479 if (ret == -1) {
480 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
481 NULL, NULL, true);
482 error_report("colo-compare primary_in error");
483 }
484 }
485
486 /*
487 * Called from the main thread on the primary for packets
488 * arriving over the socket from the secondary.
489 */
490 static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
491 {
492 CompareState *s = COLO_COMPARE(opaque);
493 int ret;
494
495 ret = net_fill_rstate(&s->sec_rs, buf, size);
496 if (ret == -1) {
497 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
498 NULL, NULL, true);
499 error_report("colo-compare secondary_in error");
500 }
501 }
502
503 /*
504 * Check old packet regularly so it can watch for any packets
505 * that the secondary hasn't produced equivalents of.
506 */
507 static gboolean check_old_packet_regular(void *opaque)
508 {
509 CompareState *s = opaque;
510
511 /* if have old packet we will notify checkpoint */
512 colo_old_packet_check(s);
513
514 return TRUE;
515 }
516
517 static void *colo_compare_thread(void *opaque)
518 {
519 CompareState *s = opaque;
520 GSource *timeout_source;
521
522 s->worker_context = g_main_context_new();
523
524 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
525 compare_pri_chr_in, NULL, s, s->worker_context, true);
526 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
527 compare_sec_chr_in, NULL, s, s->worker_context, true);
528
529 s->compare_loop = g_main_loop_new(s->worker_context, FALSE);
530
531 /* To kick any packets that the secondary doesn't match */
532 timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS);
533 g_source_set_callback(timeout_source,
534 (GSourceFunc)check_old_packet_regular, s, NULL);
535 g_source_attach(timeout_source, s->worker_context);
536
537 g_main_loop_run(s->compare_loop);
538
539 g_source_unref(timeout_source);
540 g_main_loop_unref(s->compare_loop);
541 g_main_context_unref(s->worker_context);
542 return NULL;
543 }
544
545 static char *compare_get_pri_indev(Object *obj, Error **errp)
546 {
547 CompareState *s = COLO_COMPARE(obj);
548
549 return g_strdup(s->pri_indev);
550 }
551
552 static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
553 {
554 CompareState *s = COLO_COMPARE(obj);
555
556 g_free(s->pri_indev);
557 s->pri_indev = g_strdup(value);
558 }
559
560 static char *compare_get_sec_indev(Object *obj, Error **errp)
561 {
562 CompareState *s = COLO_COMPARE(obj);
563
564 return g_strdup(s->sec_indev);
565 }
566
567 static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
568 {
569 CompareState *s = COLO_COMPARE(obj);
570
571 g_free(s->sec_indev);
572 s->sec_indev = g_strdup(value);
573 }
574
575 static char *compare_get_outdev(Object *obj, Error **errp)
576 {
577 CompareState *s = COLO_COMPARE(obj);
578
579 return g_strdup(s->outdev);
580 }
581
582 static void compare_set_outdev(Object *obj, const char *value, Error **errp)
583 {
584 CompareState *s = COLO_COMPARE(obj);
585
586 g_free(s->outdev);
587 s->outdev = g_strdup(value);
588 }
589
590 static void compare_pri_rs_finalize(SocketReadState *pri_rs)
591 {
592 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
593
594 if (packet_enqueue(s, PRIMARY_IN)) {
595 trace_colo_compare_main("primary: unsupported packet in");
596 compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
597 } else {
598 /* compare connection */
599 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
600 }
601 }
602
603 static void compare_sec_rs_finalize(SocketReadState *sec_rs)
604 {
605 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
606
607 if (packet_enqueue(s, SECONDARY_IN)) {
608 trace_colo_compare_main("secondary: unsupported packet in");
609 } else {
610 /* compare connection */
611 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
612 }
613 }
614
615
616 /*
617 * Return 0 is success.
618 * Return 1 is failed.
619 */
620 static int find_and_check_chardev(Chardev **chr,
621 char *chr_name,
622 Error **errp)
623 {
624 *chr = qemu_chr_find(chr_name);
625 if (*chr == NULL) {
626 error_setg(errp, "Device '%s' not found",
627 chr_name);
628 return 1;
629 }
630
631 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
632 error_setg(errp, "chardev \"%s\" is not reconnectable",
633 chr_name);
634 return 1;
635 }
636
637 return 0;
638 }
639
640 /*
641 * Called from the main thread on the primary
642 * to setup colo-compare.
643 */
644 static void colo_compare_complete(UserCreatable *uc, Error **errp)
645 {
646 CompareState *s = COLO_COMPARE(uc);
647 Chardev *chr;
648 char thread_name[64];
649 static int compare_id;
650
651 if (!s->pri_indev || !s->sec_indev || !s->outdev) {
652 error_setg(errp, "colo compare needs 'primary_in' ,"
653 "'secondary_in','outdev' property set");
654 return;
655 } else if (!strcmp(s->pri_indev, s->outdev) ||
656 !strcmp(s->sec_indev, s->outdev) ||
657 !strcmp(s->pri_indev, s->sec_indev)) {
658 error_setg(errp, "'indev' and 'outdev' could not be same "
659 "for compare module");
660 return;
661 }
662
663 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
664 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
665 return;
666 }
667
668 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
669 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
670 return;
671 }
672
673 if (find_and_check_chardev(&chr, s->outdev, errp) ||
674 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
675 return;
676 }
677
678 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
679 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
680
681 g_queue_init(&s->conn_list);
682
683 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
684 connection_key_equal,
685 g_free,
686 connection_destroy);
687
688 sprintf(thread_name, "colo-compare %d", compare_id);
689 qemu_thread_create(&s->thread, thread_name,
690 colo_compare_thread, s,
691 QEMU_THREAD_JOINABLE);
692 compare_id++;
693
694 return;
695 }
696
697 static void colo_flush_packets(void *opaque, void *user_data)
698 {
699 CompareState *s = user_data;
700 Connection *conn = opaque;
701 Packet *pkt = NULL;
702
703 while (!g_queue_is_empty(&conn->primary_list)) {
704 pkt = g_queue_pop_head(&conn->primary_list);
705 compare_chr_send(&s->chr_out, pkt->data, pkt->size);
706 packet_destroy(pkt, NULL);
707 }
708 while (!g_queue_is_empty(&conn->secondary_list)) {
709 pkt = g_queue_pop_head(&conn->secondary_list);
710 packet_destroy(pkt, NULL);
711 }
712 }
713
714 static void colo_compare_class_init(ObjectClass *oc, void *data)
715 {
716 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
717
718 ucc->complete = colo_compare_complete;
719 }
720
721 static void colo_compare_init(Object *obj)
722 {
723 object_property_add_str(obj, "primary_in",
724 compare_get_pri_indev, compare_set_pri_indev,
725 NULL);
726 object_property_add_str(obj, "secondary_in",
727 compare_get_sec_indev, compare_set_sec_indev,
728 NULL);
729 object_property_add_str(obj, "outdev",
730 compare_get_outdev, compare_set_outdev,
731 NULL);
732 }
733
734 static void colo_compare_finalize(Object *obj)
735 {
736 CompareState *s = COLO_COMPARE(obj);
737
738 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
739 s->worker_context, true);
740 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
741 s->worker_context, true);
742 qemu_chr_fe_deinit(&s->chr_out);
743
744 g_main_loop_quit(s->compare_loop);
745 qemu_thread_join(&s->thread);
746
747 /* Release all unhandled packets after compare thead exited */
748 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
749
750 g_queue_clear(&s->conn_list);
751
752 g_hash_table_destroy(s->connection_track_table);
753 g_free(s->pri_indev);
754 g_free(s->sec_indev);
755 g_free(s->outdev);
756 }
757
758 static const TypeInfo colo_compare_info = {
759 .name = TYPE_COLO_COMPARE,
760 .parent = TYPE_OBJECT,
761 .instance_size = sizeof(CompareState),
762 .instance_init = colo_compare_init,
763 .instance_finalize = colo_compare_finalize,
764 .class_size = sizeof(CompareClass),
765 .class_init = colo_compare_class_init,
766 .interfaces = (InterfaceInfo[]) {
767 { TYPE_USER_CREATABLE },
768 { }
769 }
770 };
771
772 static void register_types(void)
773 {
774 type_register_static(&colo_compare_info);
775 }
776
777 type_init(register_types);