xref: /openbmc/qemu/net/colo-compare.c (revision ad5d1add86b9560c22c3fb8718d6a99eabaaed6a)
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     if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
186         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
187 
188         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
189         strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
190         strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
191         strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
192 
193         trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
194                                    pri_ip_dst, spkt->size,
195                                    sec_ip_src, sec_ip_dst);
196     }
197 
198     if (ppkt->size == spkt->size) {
199         return memcmp(ppkt->data + offset, spkt->data + offset,
200                       spkt->size - offset);
201     } else {
202         trace_colo_compare_main("Net packet size are not the same");
203         return -1;
204     }
205 }
206 
207 /*
208  * Called from the compare thread on the primary
209  * for compare tcp packet
210  * compare_tcp copied from Dr. David Alan Gilbert's branch
211  */
212 static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
213 {
214     struct tcphdr *ptcp, *stcp;
215     int res;
216 
217     trace_colo_compare_main("compare tcp");
218 
219     ptcp = (struct tcphdr *)ppkt->transport_header;
220     stcp = (struct tcphdr *)spkt->transport_header;
221 
222     /*
223      * The 'identification' field in the IP header is *very* random
224      * it almost never matches.  Fudge this by ignoring differences in
225      * unfragmented packets; they'll normally sort themselves out if different
226      * anyway, and it should recover at the TCP level.
227      * An alternative would be to get both the primary and secondary to rewrite
228      * somehow; but that would need some sync traffic to sync the state
229      */
230     if (ntohs(ppkt->ip->ip_off) & IP_DF) {
231         spkt->ip->ip_id = ppkt->ip->ip_id;
232         /* and the sum will be different if the IDs were different */
233         spkt->ip->ip_sum = ppkt->ip->ip_sum;
234     }
235 
236     /*
237      * Check tcp header length for tcp option field.
238      * th_off > 5 means this tcp packet have options field.
239      * The tcp options maybe always different.
240      * for example:
241      * From RFC 7323.
242      * TCP Timestamps option (TSopt):
243      * Kind: 8
244      *
245      * Length: 10 bytes
246      *
247      *    +-------+-------+---------------------+---------------------+
248      *    |Kind=8 |  10   |   TS Value (TSval)  |TS Echo Reply (TSecr)|
249      *    +-------+-------+---------------------+---------------------+
250      *       1       1              4                     4
251      *
252      * In this case the primary guest's timestamp always different with
253      * the secondary guest's timestamp. COLO just focus on payload,
254      * so we just need skip this field.
255      */
256     if (ptcp->th_off > 5) {
257         ptrdiff_t tcp_offset;
258         tcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data
259                      + (ptcp->th_off * 4);
260         res = colo_packet_compare_common(ppkt, spkt, tcp_offset);
261     } else if (ptcp->th_sum == stcp->th_sum) {
262         res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
263     } else {
264         res = -1;
265     }
266 
267     if (res && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
268         char ip_src[20], ip_dst[20];
269 
270         strcpy(ip_src, inet_ntoa(ppkt->ip->ip_src));
271         strcpy(ip_dst, inet_ntoa(ppkt->ip->ip_dst));
272 
273         trace_colo_compare_tcp_info(ip_src,
274                                     ip_dst,
275                                     ntohl(ptcp->th_seq),
276                                     ntohl(stcp->th_seq),
277                                     ntohl(ptcp->th_ack),
278                                     ntohl(stcp->th_ack),
279                                     res,
280                                     ptcp->th_flags,
281                                     stcp->th_flags,
282                                     ppkt->size,
283                                     spkt->size);
284 
285         qemu_hexdump((char *)ppkt->data, stderr,
286                      "colo-compare ppkt", ppkt->size);
287         qemu_hexdump((char *)spkt->data, stderr,
288                      "colo-compare spkt", spkt->size);
289     }
290 
291     return res;
292 }
293 
294 /*
295  * Called from the compare thread on the primary
296  * for compare udp packet
297  */
298 static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
299 {
300     int ret;
301     int network_header_length = ppkt->ip->ip_hl * 4;
302 
303     trace_colo_compare_main("compare udp");
304 
305     /*
306      * Because of ppkt and spkt are both in the same connection,
307      * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
308      * same with spkt. In addition, IP header's Identification is a random
309      * field, we can handle it in IP fragmentation function later.
310      * COLO just concern the response net packet payload from primary guest
311      * and secondary guest are same or not, So we ignored all IP header include
312      * other field like TOS,TTL,IP Checksum. we only need to compare
313      * the ip payload here.
314      */
315     ret = colo_packet_compare_common(ppkt, spkt,
316                                      network_header_length + ETH_HLEN);
317 
318     if (ret) {
319         trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
320         trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
321         if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
322             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
323                          ppkt->size);
324             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
325                          spkt->size);
326         }
327     }
328 
329     return ret;
330 }
331 
332 /*
333  * Called from the compare thread on the primary
334  * for compare icmp packet
335  */
336 static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
337 {
338     int network_header_length = ppkt->ip->ip_hl * 4;
339 
340     trace_colo_compare_main("compare icmp");
341 
342     /*
343      * Because of ppkt and spkt are both in the same connection,
344      * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
345      * same with spkt. In addition, IP header's Identification is a random
346      * field, we can handle it in IP fragmentation function later.
347      * COLO just concern the response net packet payload from primary guest
348      * and secondary guest are same or not, So we ignored all IP header include
349      * other field like TOS,TTL,IP Checksum. we only need to compare
350      * the ip payload here.
351      */
352     if (colo_packet_compare_common(ppkt, spkt,
353                                    network_header_length + ETH_HLEN)) {
354         trace_colo_compare_icmp_miscompare("primary pkt size",
355                                            ppkt->size);
356         trace_colo_compare_icmp_miscompare("Secondary pkt size",
357                                            spkt->size);
358         if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
359             qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
360                          ppkt->size);
361             qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
362                          spkt->size);
363         }
364         return -1;
365     } else {
366         return 0;
367     }
368 }
369 
370 /*
371  * Called from the compare thread on the primary
372  * for compare other packet
373  */
374 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
375 {
376     trace_colo_compare_main("compare other");
377     if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
378         char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
379 
380         strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
381         strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
382         strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
383         strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
384 
385         trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
386                                    pri_ip_dst, spkt->size,
387                                    sec_ip_src, sec_ip_dst);
388     }
389 
390     return colo_packet_compare_common(ppkt, spkt, 0);
391 }
392 
393 static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
394 {
395     int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
396 
397     if ((now - pkt->creation_ms) > (*check_time)) {
398         trace_colo_old_packet_check_found(pkt->creation_ms);
399         return 0;
400     } else {
401         return 1;
402     }
403 }
404 
405 static int colo_old_packet_check_one_conn(Connection *conn,
406                                           void *user_data)
407 {
408     GList *result = NULL;
409     int64_t check_time = REGULAR_PACKET_CHECK_MS;
410 
411     result = g_queue_find_custom(&conn->primary_list,
412                                  &check_time,
413                                  (GCompareFunc)colo_old_packet_check_one);
414 
415     if (result) {
416         /* do checkpoint will flush old packet */
417         /* TODO: colo_notify_checkpoint();*/
418         return 0;
419     }
420 
421     return 1;
422 }
423 
424 /*
425  * Look for old packets that the secondary hasn't matched,
426  * if we have some then we have to checkpoint to wake
427  * the secondary up.
428  */
429 static void colo_old_packet_check(void *opaque)
430 {
431     CompareState *s = opaque;
432 
433     /*
434      * If we find one old packet, stop finding job and notify
435      * COLO frame do checkpoint.
436      */
437     g_queue_find_custom(&s->conn_list, NULL,
438                         (GCompareFunc)colo_old_packet_check_one_conn);
439 }
440 
441 /*
442  * Called from the compare thread on the primary
443  * for compare connection
444  */
445 static void colo_compare_connection(void *opaque, void *user_data)
446 {
447     CompareState *s = user_data;
448     Connection *conn = opaque;
449     Packet *pkt = NULL;
450     GList *result = NULL;
451     int ret;
452 
453     while (!g_queue_is_empty(&conn->primary_list) &&
454            !g_queue_is_empty(&conn->secondary_list)) {
455         pkt = g_queue_pop_tail(&conn->primary_list);
456         switch (conn->ip_proto) {
457         case IPPROTO_TCP:
458             result = g_queue_find_custom(&conn->secondary_list,
459                      pkt, (GCompareFunc)colo_packet_compare_tcp);
460             break;
461         case IPPROTO_UDP:
462             result = g_queue_find_custom(&conn->secondary_list,
463                      pkt, (GCompareFunc)colo_packet_compare_udp);
464             break;
465         case IPPROTO_ICMP:
466             result = g_queue_find_custom(&conn->secondary_list,
467                      pkt, (GCompareFunc)colo_packet_compare_icmp);
468             break;
469         default:
470             result = g_queue_find_custom(&conn->secondary_list,
471                      pkt, (GCompareFunc)colo_packet_compare_other);
472             break;
473         }
474 
475         if (result) {
476             ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
477             if (ret < 0) {
478                 error_report("colo_send_primary_packet failed");
479             }
480             trace_colo_compare_main("packet same and release packet");
481             g_queue_remove(&conn->secondary_list, result->data);
482             packet_destroy(pkt, NULL);
483         } else {
484             /*
485              * If one packet arrive late, the secondary_list or
486              * primary_list will be empty, so we can't compare it
487              * until next comparison.
488              */
489             trace_colo_compare_main("packet different");
490             g_queue_push_tail(&conn->primary_list, pkt);
491             /* TODO: colo_notify_checkpoint();*/
492             break;
493         }
494     }
495 }
496 
497 static int compare_chr_send(CharBackend *out,
498                             const uint8_t *buf,
499                             uint32_t size)
500 {
501     int ret = 0;
502     uint32_t len = htonl(size);
503 
504     if (!size) {
505         return 0;
506     }
507 
508     ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
509     if (ret != sizeof(len)) {
510         goto err;
511     }
512 
513     ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
514     if (ret != size) {
515         goto err;
516     }
517 
518     return 0;
519 
520 err:
521     return ret < 0 ? ret : -EIO;
522 }
523 
524 static int compare_chr_can_read(void *opaque)
525 {
526     return COMPARE_READ_LEN_MAX;
527 }
528 
529 /*
530  * Called from the main thread on the primary for packets
531  * arriving over the socket from the primary.
532  */
533 static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
534 {
535     CompareState *s = COLO_COMPARE(opaque);
536     int ret;
537 
538     ret = net_fill_rstate(&s->pri_rs, buf, size);
539     if (ret == -1) {
540         qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
541                                  NULL, NULL, true);
542         error_report("colo-compare primary_in error");
543     }
544 }
545 
546 /*
547  * Called from the main thread on the primary for packets
548  * arriving over the socket from the secondary.
549  */
550 static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
551 {
552     CompareState *s = COLO_COMPARE(opaque);
553     int ret;
554 
555     ret = net_fill_rstate(&s->sec_rs, buf, size);
556     if (ret == -1) {
557         qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
558                                  NULL, NULL, true);
559         error_report("colo-compare secondary_in error");
560     }
561 }
562 
563 /*
564  * Check old packet regularly so it can watch for any packets
565  * that the secondary hasn't produced equivalents of.
566  */
567 static gboolean check_old_packet_regular(void *opaque)
568 {
569     CompareState *s = opaque;
570 
571     /* if have old packet we will notify checkpoint */
572     colo_old_packet_check(s);
573 
574     return TRUE;
575 }
576 
577 static void *colo_compare_thread(void *opaque)
578 {
579     CompareState *s = opaque;
580     GSource *timeout_source;
581 
582     s->worker_context = g_main_context_new();
583 
584     qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
585                           compare_pri_chr_in, NULL, s, s->worker_context, true);
586     qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
587                           compare_sec_chr_in, NULL, s, s->worker_context, true);
588 
589     s->compare_loop = g_main_loop_new(s->worker_context, FALSE);
590 
591     /* To kick any packets that the secondary doesn't match */
592     timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS);
593     g_source_set_callback(timeout_source,
594                           (GSourceFunc)check_old_packet_regular, s, NULL);
595     g_source_attach(timeout_source, s->worker_context);
596 
597     g_main_loop_run(s->compare_loop);
598 
599     g_source_unref(timeout_source);
600     g_main_loop_unref(s->compare_loop);
601     g_main_context_unref(s->worker_context);
602     return NULL;
603 }
604 
605 static char *compare_get_pri_indev(Object *obj, Error **errp)
606 {
607     CompareState *s = COLO_COMPARE(obj);
608 
609     return g_strdup(s->pri_indev);
610 }
611 
612 static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
613 {
614     CompareState *s = COLO_COMPARE(obj);
615 
616     g_free(s->pri_indev);
617     s->pri_indev = g_strdup(value);
618 }
619 
620 static char *compare_get_sec_indev(Object *obj, Error **errp)
621 {
622     CompareState *s = COLO_COMPARE(obj);
623 
624     return g_strdup(s->sec_indev);
625 }
626 
627 static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
628 {
629     CompareState *s = COLO_COMPARE(obj);
630 
631     g_free(s->sec_indev);
632     s->sec_indev = g_strdup(value);
633 }
634 
635 static char *compare_get_outdev(Object *obj, Error **errp)
636 {
637     CompareState *s = COLO_COMPARE(obj);
638 
639     return g_strdup(s->outdev);
640 }
641 
642 static void compare_set_outdev(Object *obj, const char *value, Error **errp)
643 {
644     CompareState *s = COLO_COMPARE(obj);
645 
646     g_free(s->outdev);
647     s->outdev = g_strdup(value);
648 }
649 
650 static void compare_pri_rs_finalize(SocketReadState *pri_rs)
651 {
652     CompareState *s = container_of(pri_rs, CompareState, pri_rs);
653 
654     if (packet_enqueue(s, PRIMARY_IN)) {
655         trace_colo_compare_main("primary: unsupported packet in");
656         compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
657     } else {
658         /* compare connection */
659         g_queue_foreach(&s->conn_list, colo_compare_connection, s);
660     }
661 }
662 
663 static void compare_sec_rs_finalize(SocketReadState *sec_rs)
664 {
665     CompareState *s = container_of(sec_rs, CompareState, sec_rs);
666 
667     if (packet_enqueue(s, SECONDARY_IN)) {
668         trace_colo_compare_main("secondary: unsupported packet in");
669     } else {
670         /* compare connection */
671         g_queue_foreach(&s->conn_list, colo_compare_connection, s);
672     }
673 }
674 
675 
676 /*
677  * Return 0 is success.
678  * Return 1 is failed.
679  */
680 static int find_and_check_chardev(Chardev **chr,
681                                   char *chr_name,
682                                   Error **errp)
683 {
684     *chr = qemu_chr_find(chr_name);
685     if (*chr == NULL) {
686         error_setg(errp, "Device '%s' not found",
687                    chr_name);
688         return 1;
689     }
690 
691     if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
692         error_setg(errp, "chardev \"%s\" is not reconnectable",
693                    chr_name);
694         return 1;
695     }
696 
697     return 0;
698 }
699 
700 /*
701  * Called from the main thread on the primary
702  * to setup colo-compare.
703  */
704 static void colo_compare_complete(UserCreatable *uc, Error **errp)
705 {
706     CompareState *s = COLO_COMPARE(uc);
707     Chardev *chr;
708     char thread_name[64];
709     static int compare_id;
710 
711     if (!s->pri_indev || !s->sec_indev || !s->outdev) {
712         error_setg(errp, "colo compare needs 'primary_in' ,"
713                    "'secondary_in','outdev' property set");
714         return;
715     } else if (!strcmp(s->pri_indev, s->outdev) ||
716                !strcmp(s->sec_indev, s->outdev) ||
717                !strcmp(s->pri_indev, s->sec_indev)) {
718         error_setg(errp, "'indev' and 'outdev' could not be same "
719                    "for compare module");
720         return;
721     }
722 
723     if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
724         !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
725         return;
726     }
727 
728     if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
729         !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
730         return;
731     }
732 
733     if (find_and_check_chardev(&chr, s->outdev, errp) ||
734         !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
735         return;
736     }
737 
738     net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
739     net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
740 
741     g_queue_init(&s->conn_list);
742 
743     s->connection_track_table = g_hash_table_new_full(connection_key_hash,
744                                                       connection_key_equal,
745                                                       g_free,
746                                                       connection_destroy);
747 
748     sprintf(thread_name, "colo-compare %d", compare_id);
749     qemu_thread_create(&s->thread, thread_name,
750                        colo_compare_thread, s,
751                        QEMU_THREAD_JOINABLE);
752     compare_id++;
753 
754     return;
755 }
756 
757 static void colo_flush_packets(void *opaque, void *user_data)
758 {
759     CompareState *s = user_data;
760     Connection *conn = opaque;
761     Packet *pkt = NULL;
762 
763     while (!g_queue_is_empty(&conn->primary_list)) {
764         pkt = g_queue_pop_head(&conn->primary_list);
765         compare_chr_send(&s->chr_out, pkt->data, pkt->size);
766         packet_destroy(pkt, NULL);
767     }
768     while (!g_queue_is_empty(&conn->secondary_list)) {
769         pkt = g_queue_pop_head(&conn->secondary_list);
770         packet_destroy(pkt, NULL);
771     }
772 }
773 
774 static void colo_compare_class_init(ObjectClass *oc, void *data)
775 {
776     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
777 
778     ucc->complete = colo_compare_complete;
779 }
780 
781 static void colo_compare_init(Object *obj)
782 {
783     object_property_add_str(obj, "primary_in",
784                             compare_get_pri_indev, compare_set_pri_indev,
785                             NULL);
786     object_property_add_str(obj, "secondary_in",
787                             compare_get_sec_indev, compare_set_sec_indev,
788                             NULL);
789     object_property_add_str(obj, "outdev",
790                             compare_get_outdev, compare_set_outdev,
791                             NULL);
792 }
793 
794 static void colo_compare_finalize(Object *obj)
795 {
796     CompareState *s = COLO_COMPARE(obj);
797 
798     qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
799                              s->worker_context, true);
800     qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
801                              s->worker_context, true);
802     qemu_chr_fe_deinit(&s->chr_out);
803 
804     g_main_loop_quit(s->compare_loop);
805     qemu_thread_join(&s->thread);
806 
807     /* Release all unhandled packets after compare thead exited */
808     g_queue_foreach(&s->conn_list, colo_flush_packets, s);
809 
810     g_queue_clear(&s->conn_list);
811 
812     g_hash_table_destroy(s->connection_track_table);
813     g_free(s->pri_indev);
814     g_free(s->sec_indev);
815     g_free(s->outdev);
816 }
817 
818 static const TypeInfo colo_compare_info = {
819     .name = TYPE_COLO_COMPARE,
820     .parent = TYPE_OBJECT,
821     .instance_size = sizeof(CompareState),
822     .instance_init = colo_compare_init,
823     .instance_finalize = colo_compare_finalize,
824     .class_size = sizeof(CompareClass),
825     .class_init = colo_compare_class_init,
826     .interfaces = (InterfaceInfo[]) {
827         { TYPE_USER_CREATABLE },
828         { }
829     }
830 };
831 
832 static void register_types(void)
833 {
834     type_register_static(&colo_compare_info);
835 }
836 
837 type_init(register_types);
838