]> git.proxmox.com Git - mirror_qemu.git/blame - net/colo-compare.c
Merge remote-tracking branch 'remotes/gkurz/tags/cve-2016-9602-for-upstream' into...
[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;
86 /* Timer used on the primary to find packets that are never matched */
87 QEMUTimer *timer;
88 QemuMutex timer_check_lock;
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)) {
377 qemu_mutex_lock(&s->timer_check_lock);
378 pkt = g_queue_pop_tail(&conn->primary_list);
379 qemu_mutex_unlock(&s->timer_check_lock);
f4b61836
ZC
380 switch (conn->ip_proto) {
381 case IPPROTO_TCP:
382 result = g_queue_find_custom(&conn->secondary_list,
383 pkt, (GCompareFunc)colo_packet_compare_tcp);
384 break;
385 case IPPROTO_UDP:
386 result = g_queue_find_custom(&conn->secondary_list,
387 pkt, (GCompareFunc)colo_packet_compare_udp);
388 break;
389 case IPPROTO_ICMP:
390 result = g_queue_find_custom(&conn->secondary_list,
391 pkt, (GCompareFunc)colo_packet_compare_icmp);
392 break;
393 default:
394 result = g_queue_find_custom(&conn->secondary_list,
395 pkt, (GCompareFunc)colo_packet_compare_other);
396 break;
397 }
0682e15b
ZC
398
399 if (result) {
5345fdb4 400 ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
0682e15b
ZC
401 if (ret < 0) {
402 error_report("colo_send_primary_packet failed");
403 }
404 trace_colo_compare_main("packet same and release packet");
405 g_queue_remove(&conn->secondary_list, result->data);
406 packet_destroy(pkt, NULL);
407 } else {
408 /*
409 * If one packet arrive late, the secondary_list or
410 * primary_list will be empty, so we can't compare it
411 * until next comparison.
412 */
413 trace_colo_compare_main("packet different");
414 qemu_mutex_lock(&s->timer_check_lock);
415 g_queue_push_tail(&conn->primary_list, pkt);
416 qemu_mutex_unlock(&s->timer_check_lock);
417 /* TODO: colo_notify_checkpoint();*/
418 break;
419 }
420 }
421}
422
5345fdb4 423static int compare_chr_send(CharBackend *out,
59509ec1
ZC
424 const uint8_t *buf,
425 uint32_t size)
426{
427 int ret = 0;
428 uint32_t len = htonl(size);
429
430 if (!size) {
431 return 0;
432 }
433
434 ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
435 if (ret != sizeof(len)) {
436 goto err;
437 }
438
439 ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
440 if (ret != size) {
441 goto err;
442 }
443
444 return 0;
445
446err:
447 return ret < 0 ? ret : -EIO;
448}
449
0682e15b
ZC
450static int compare_chr_can_read(void *opaque)
451{
452 return COMPARE_READ_LEN_MAX;
453}
454
455/*
456 * Called from the main thread on the primary for packets
457 * arriving over the socket from the primary.
458 */
459static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
460{
461 CompareState *s = COLO_COMPARE(opaque);
462 int ret;
463
464 ret = net_fill_rstate(&s->pri_rs, buf, size);
465 if (ret == -1) {
39ab61c6
MAL
466 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
467 NULL, NULL, true);
0682e15b
ZC
468 error_report("colo-compare primary_in error");
469 }
470}
471
472/*
473 * Called from the main thread on the primary for packets
474 * arriving over the socket from the secondary.
475 */
476static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
477{
478 CompareState *s = COLO_COMPARE(opaque);
479 int ret;
480
481 ret = net_fill_rstate(&s->sec_rs, buf, size);
482 if (ret == -1) {
39ab61c6
MAL
483 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
484 NULL, NULL, true);
0682e15b
ZC
485 error_report("colo-compare secondary_in error");
486 }
487}
488
489static void *colo_compare_thread(void *opaque)
490{
491 GMainContext *worker_context;
492 GMainLoop *compare_loop;
493 CompareState *s = opaque;
494
495 worker_context = g_main_context_new();
496
5345fdb4 497 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
39ab61c6 498 compare_pri_chr_in, NULL, s, worker_context, true);
5345fdb4 499 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
39ab61c6 500 compare_sec_chr_in, NULL, s, worker_context, true);
0682e15b
ZC
501
502 compare_loop = g_main_loop_new(worker_context, FALSE);
503
504 g_main_loop_run(compare_loop);
505
506 g_main_loop_unref(compare_loop);
507 g_main_context_unref(worker_context);
508 return NULL;
509}
510
7dce4e6f
ZC
511static char *compare_get_pri_indev(Object *obj, Error **errp)
512{
513 CompareState *s = COLO_COMPARE(obj);
514
515 return g_strdup(s->pri_indev);
516}
517
518static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
519{
520 CompareState *s = COLO_COMPARE(obj);
521
522 g_free(s->pri_indev);
523 s->pri_indev = g_strdup(value);
524}
525
526static char *compare_get_sec_indev(Object *obj, Error **errp)
527{
528 CompareState *s = COLO_COMPARE(obj);
529
530 return g_strdup(s->sec_indev);
531}
532
533static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
534{
535 CompareState *s = COLO_COMPARE(obj);
536
537 g_free(s->sec_indev);
538 s->sec_indev = g_strdup(value);
539}
540
541static char *compare_get_outdev(Object *obj, Error **errp)
542{
543 CompareState *s = COLO_COMPARE(obj);
544
545 return g_strdup(s->outdev);
546}
547
548static void compare_set_outdev(Object *obj, const char *value, Error **errp)
549{
550 CompareState *s = COLO_COMPARE(obj);
551
552 g_free(s->outdev);
553 s->outdev = g_strdup(value);
554}
555
556static void compare_pri_rs_finalize(SocketReadState *pri_rs)
557{
59509ec1
ZC
558 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
559
560 if (packet_enqueue(s, PRIMARY_IN)) {
561 trace_colo_compare_main("primary: unsupported packet in");
5345fdb4 562 compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
0682e15b
ZC
563 } else {
564 /* compare connection */
565 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
59509ec1 566 }
7dce4e6f
ZC
567}
568
569static void compare_sec_rs_finalize(SocketReadState *sec_rs)
570{
59509ec1
ZC
571 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
572
573 if (packet_enqueue(s, SECONDARY_IN)) {
574 trace_colo_compare_main("secondary: unsupported packet in");
0682e15b
ZC
575 } else {
576 /* compare connection */
577 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
59509ec1 578 }
7dce4e6f
ZC
579}
580
7dce4e6f
ZC
581
582/*
583 * Return 0 is success.
584 * Return 1 is failed.
585 */
0ec7b3e7 586static int find_and_check_chardev(Chardev **chr,
7dce4e6f
ZC
587 char *chr_name,
588 Error **errp)
589{
7dce4e6f
ZC
590 *chr = qemu_chr_find(chr_name);
591 if (*chr == NULL) {
592 error_setg(errp, "Device '%s' not found",
593 chr_name);
594 return 1;
595 }
596
0a73336d
DB
597 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
598 error_setg(errp, "chardev \"%s\" is not reconnectable",
7dce4e6f
ZC
599 chr_name);
600 return 1;
601 }
fbf3cc3a 602
7dce4e6f
ZC
603 return 0;
604}
605
0682e15b
ZC
606/*
607 * Check old packet regularly so it can watch for any packets
608 * that the secondary hasn't produced equivalents of.
609 */
610static void check_old_packet_regular(void *opaque)
611{
612 CompareState *s = opaque;
613
614 timer_mod(s->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
615 REGULAR_PACKET_CHECK_MS);
616 /* if have old packet we will notify checkpoint */
617 /*
618 * TODO: Make timer handler run in compare thread
619 * like qemu_chr_add_handlers_full.
620 */
621 qemu_mutex_lock(&s->timer_check_lock);
622 colo_old_packet_check(s);
623 qemu_mutex_unlock(&s->timer_check_lock);
624}
625
7dce4e6f
ZC
626/*
627 * Called from the main thread on the primary
628 * to setup colo-compare.
629 */
630static void colo_compare_complete(UserCreatable *uc, Error **errp)
631{
632 CompareState *s = COLO_COMPARE(uc);
0ec7b3e7 633 Chardev *chr;
0682e15b
ZC
634 char thread_name[64];
635 static int compare_id;
7dce4e6f
ZC
636
637 if (!s->pri_indev || !s->sec_indev || !s->outdev) {
638 error_setg(errp, "colo compare needs 'primary_in' ,"
639 "'secondary_in','outdev' property set");
640 return;
641 } else if (!strcmp(s->pri_indev, s->outdev) ||
642 !strcmp(s->sec_indev, s->outdev) ||
643 !strcmp(s->pri_indev, s->sec_indev)) {
644 error_setg(errp, "'indev' and 'outdev' could not be same "
645 "for compare module");
646 return;
647 }
648
5345fdb4
MAL
649 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
650 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
7dce4e6f
ZC
651 return;
652 }
653
5345fdb4
MAL
654 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
655 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
7dce4e6f
ZC
656 return;
657 }
658
5345fdb4
MAL
659 if (find_and_check_chardev(&chr, s->outdev, errp) ||
660 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
7dce4e6f
ZC
661 return;
662 }
663
7dce4e6f
ZC
664 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
665 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
666
b6540d40 667 g_queue_init(&s->conn_list);
0682e15b 668 qemu_mutex_init(&s->timer_check_lock);
b6540d40
ZC
669
670 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
671 connection_key_equal,
672 g_free,
673 connection_destroy);
59509ec1 674
0682e15b
ZC
675 sprintf(thread_name, "colo-compare %d", compare_id);
676 qemu_thread_create(&s->thread, thread_name,
677 colo_compare_thread, s,
678 QEMU_THREAD_JOINABLE);
679 compare_id++;
680
681 /* A regular timer to kick any packets that the secondary doesn't match */
682 s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, /* Only when guest runs */
683 check_old_packet_regular, s);
684 timer_mod(s->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
685 REGULAR_PACKET_CHECK_MS);
686
7dce4e6f
ZC
687 return;
688}
689
690static void colo_compare_class_init(ObjectClass *oc, void *data)
691{
692 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
693
694 ucc->complete = colo_compare_complete;
695}
696
697static void colo_compare_init(Object *obj)
698{
699 object_property_add_str(obj, "primary_in",
700 compare_get_pri_indev, compare_set_pri_indev,
701 NULL);
702 object_property_add_str(obj, "secondary_in",
703 compare_get_sec_indev, compare_set_sec_indev,
704 NULL);
705 object_property_add_str(obj, "outdev",
706 compare_get_outdev, compare_set_outdev,
707 NULL);
708}
709
710static void colo_compare_finalize(Object *obj)
711{
712 CompareState *s = COLO_COMPARE(obj);
713
c39860e6
MAL
714 qemu_chr_fe_deinit(&s->chr_pri_in);
715 qemu_chr_fe_deinit(&s->chr_sec_in);
716 qemu_chr_fe_deinit(&s->chr_out);
7dce4e6f 717
b6540d40
ZC
718 g_queue_free(&s->conn_list);
719
0682e15b
ZC
720 if (qemu_thread_is_self(&s->thread)) {
721 /* compare connection */
722 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
723 qemu_thread_join(&s->thread);
724 }
725
726 if (s->timer) {
727 timer_del(s->timer);
728 }
729
730 qemu_mutex_destroy(&s->timer_check_lock);
731
7dce4e6f
ZC
732 g_free(s->pri_indev);
733 g_free(s->sec_indev);
734 g_free(s->outdev);
735}
736
737static const TypeInfo colo_compare_info = {
738 .name = TYPE_COLO_COMPARE,
739 .parent = TYPE_OBJECT,
740 .instance_size = sizeof(CompareState),
741 .instance_init = colo_compare_init,
742 .instance_finalize = colo_compare_finalize,
743 .class_size = sizeof(CompareClass),
744 .class_init = colo_compare_class_init,
745 .interfaces = (InterfaceInfo[]) {
746 { TYPE_USER_CREATABLE },
747 { }
748 }
749};
750
751static void register_types(void)
752{
753 type_register_static(&colo_compare_info);
754}
755
756type_init(register_types);