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