]> git.proxmox.com Git - mirror_qemu.git/blame - net/colo-compare.c
Merge remote-tracking branch 'remotes/gkurz/tags/fixes-for-2.9' into staging
[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 */
6efeb328 183static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
0682e15b
ZC
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) {
6efeb328
ZC
191 return memcmp(ppkt->data + offset, spkt->data + offset,
192 spkt->size - offset);
0682e15b 193 } else {
2ad7ca4c 194 trace_colo_compare_main("Net packet size are not the same");
0682e15b
ZC
195 return -1;
196 }
197}
198
f4b61836
ZC
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 */
204static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
0682e15b 205{
f4b61836
ZC
206 struct tcphdr *ptcp, *stcp;
207 int res;
f4b61836
ZC
208
209 trace_colo_compare_main("compare tcp");
2ad7ca4c 210
f4b61836
ZC
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
6efeb328
ZC
228 if (ptcp->th_sum == stcp->th_sum) {
229 res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
230 } else {
231 res = -1;
232 }
f4b61836
ZC
233
234 if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
2dfe5113
AB
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);
2061c14c
ZC
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);
f4b61836
ZC
251 }
252
253 return res;
254}
255
256/*
257 * Called from the compare thread on the primary
258 * for compare udp packet
259 */
260static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
261{
262 int ret;
6efeb328 263 int network_header_length = ppkt->ip->ip_hl * 4;
f4b61836
ZC
264
265 trace_colo_compare_main("compare udp");
2ad7ca4c 266
6efeb328
ZC
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);
f4b61836
ZC
279
280 if (ret) {
281 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
f4b61836 282 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
1723a7f7
ZC
283 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
284 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
285 ppkt->size);
286 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
287 spkt->size);
288 }
f4b61836
ZC
289 }
290
291 return ret;
292}
293
294/*
295 * Called from the compare thread on the primary
296 * for compare icmp packet
297 */
298static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
299{
6efeb328
ZC
300 int network_header_length = ppkt->ip->ip_hl * 4;
301
f4b61836 302 trace_colo_compare_main("compare icmp");
f4b61836 303
6efeb328
ZC
304 /*
305 * Because of ppkt and spkt are both in the same connection,
306 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
307 * same with spkt. In addition, IP header's Identification is a random
308 * field, we can handle it in IP fragmentation function later.
309 * COLO just concern the response net packet payload from primary guest
310 * and secondary guest are same or not, So we ignored all IP header include
311 * other field like TOS,TTL,IP Checksum. we only need to compare
312 * the ip payload here.
313 */
314 if (colo_packet_compare_common(ppkt, spkt,
315 network_header_length + ETH_HLEN)) {
f4b61836
ZC
316 trace_colo_compare_icmp_miscompare("primary pkt size",
317 ppkt->size);
f4b61836
ZC
318 trace_colo_compare_icmp_miscompare("Secondary pkt size",
319 spkt->size);
1723a7f7
ZC
320 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
321 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
322 ppkt->size);
323 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
324 spkt->size);
325 }
f4b61836
ZC
326 return -1;
327 } else {
328 return 0;
329 }
330}
331
332/*
333 * Called from the compare thread on the primary
334 * for compare other packet
335 */
336static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
337{
338 trace_colo_compare_main("compare other");
339 trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
340 inet_ntoa(ppkt->ip->ip_dst), spkt->size,
341 inet_ntoa(spkt->ip->ip_src),
342 inet_ntoa(spkt->ip->ip_dst));
6efeb328 343 return colo_packet_compare_common(ppkt, spkt, 0);
0682e15b
ZC
344}
345
346static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
347{
348 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
349
350 if ((now - pkt->creation_ms) > (*check_time)) {
351 trace_colo_old_packet_check_found(pkt->creation_ms);
352 return 0;
353 } else {
354 return 1;
355 }
356}
357
358static void colo_old_packet_check_one_conn(void *opaque,
359 void *user_data)
360{
361 Connection *conn = opaque;
362 GList *result = NULL;
363 int64_t check_time = REGULAR_PACKET_CHECK_MS;
364
365 result = g_queue_find_custom(&conn->primary_list,
366 &check_time,
367 (GCompareFunc)colo_old_packet_check_one);
368
369 if (result) {
370 /* do checkpoint will flush old packet */
371 /* TODO: colo_notify_checkpoint();*/
372 }
373}
374
375/*
376 * Look for old packets that the secondary hasn't matched,
377 * if we have some then we have to checkpoint to wake
378 * the secondary up.
379 */
380static void colo_old_packet_check(void *opaque)
381{
382 CompareState *s = opaque;
383
384 g_queue_foreach(&s->conn_list, colo_old_packet_check_one_conn, NULL);
385}
386
387/*
388 * Called from the compare thread on the primary
389 * for compare connection
390 */
391static void colo_compare_connection(void *opaque, void *user_data)
392{
393 CompareState *s = user_data;
394 Connection *conn = opaque;
395 Packet *pkt = NULL;
396 GList *result = NULL;
397 int ret;
398
399 while (!g_queue_is_empty(&conn->primary_list) &&
400 !g_queue_is_empty(&conn->secondary_list)) {
0682e15b 401 pkt = g_queue_pop_tail(&conn->primary_list);
f4b61836
ZC
402 switch (conn->ip_proto) {
403 case IPPROTO_TCP:
404 result = g_queue_find_custom(&conn->secondary_list,
405 pkt, (GCompareFunc)colo_packet_compare_tcp);
406 break;
407 case IPPROTO_UDP:
408 result = g_queue_find_custom(&conn->secondary_list,
409 pkt, (GCompareFunc)colo_packet_compare_udp);
410 break;
411 case IPPROTO_ICMP:
412 result = g_queue_find_custom(&conn->secondary_list,
413 pkt, (GCompareFunc)colo_packet_compare_icmp);
414 break;
415 default:
416 result = g_queue_find_custom(&conn->secondary_list,
417 pkt, (GCompareFunc)colo_packet_compare_other);
418 break;
419 }
0682e15b
ZC
420
421 if (result) {
5345fdb4 422 ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
0682e15b
ZC
423 if (ret < 0) {
424 error_report("colo_send_primary_packet failed");
425 }
426 trace_colo_compare_main("packet same and release packet");
427 g_queue_remove(&conn->secondary_list, result->data);
428 packet_destroy(pkt, NULL);
429 } else {
430 /*
431 * If one packet arrive late, the secondary_list or
432 * primary_list will be empty, so we can't compare it
433 * until next comparison.
434 */
435 trace_colo_compare_main("packet different");
0682e15b 436 g_queue_push_tail(&conn->primary_list, pkt);
0682e15b
ZC
437 /* TODO: colo_notify_checkpoint();*/
438 break;
439 }
440 }
441}
442
5345fdb4 443static int compare_chr_send(CharBackend *out,
59509ec1
ZC
444 const uint8_t *buf,
445 uint32_t size)
446{
447 int ret = 0;
448 uint32_t len = htonl(size);
449
450 if (!size) {
451 return 0;
452 }
453
454 ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
455 if (ret != sizeof(len)) {
456 goto err;
457 }
458
459 ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
460 if (ret != size) {
461 goto err;
462 }
463
464 return 0;
465
466err:
467 return ret < 0 ? ret : -EIO;
468}
469
0682e15b
ZC
470static int compare_chr_can_read(void *opaque)
471{
472 return COMPARE_READ_LEN_MAX;
473}
474
475/*
476 * Called from the main thread on the primary for packets
477 * arriving over the socket from the primary.
478 */
479static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
480{
481 CompareState *s = COLO_COMPARE(opaque);
482 int ret;
483
484 ret = net_fill_rstate(&s->pri_rs, buf, size);
485 if (ret == -1) {
39ab61c6
MAL
486 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
487 NULL, NULL, true);
0682e15b
ZC
488 error_report("colo-compare primary_in error");
489 }
490}
491
492/*
493 * Called from the main thread on the primary for packets
494 * arriving over the socket from the secondary.
495 */
496static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
497{
498 CompareState *s = COLO_COMPARE(opaque);
499 int ret;
500
501 ret = net_fill_rstate(&s->sec_rs, buf, size);
502 if (ret == -1) {
39ab61c6
MAL
503 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
504 NULL, NULL, true);
0682e15b
ZC
505 error_report("colo-compare secondary_in error");
506 }
507}
508
66d2a242
HZ
509/*
510 * Check old packet regularly so it can watch for any packets
511 * that the secondary hasn't produced equivalents of.
512 */
513static gboolean check_old_packet_regular(void *opaque)
514{
515 CompareState *s = opaque;
516
517 /* if have old packet we will notify checkpoint */
518 colo_old_packet_check(s);
519
520 return TRUE;
521}
522
0682e15b
ZC
523static void *colo_compare_thread(void *opaque)
524{
0682e15b 525 CompareState *s = opaque;
66d2a242 526 GSource *timeout_source;
0682e15b 527
b43decb0 528 s->worker_context = g_main_context_new();
0682e15b 529
5345fdb4 530 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
b43decb0 531 compare_pri_chr_in, NULL, s, s->worker_context, true);
5345fdb4 532 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
b43decb0 533 compare_sec_chr_in, NULL, s, s->worker_context, true);
0682e15b 534
b43decb0 535 s->compare_loop = g_main_loop_new(s->worker_context, FALSE);
0682e15b 536
66d2a242
HZ
537 /* To kick any packets that the secondary doesn't match */
538 timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS);
539 g_source_set_callback(timeout_source,
540 (GSourceFunc)check_old_packet_regular, s, NULL);
b43decb0 541 g_source_attach(timeout_source, s->worker_context);
66d2a242 542
dfd917a9 543 g_main_loop_run(s->compare_loop);
0682e15b 544
66d2a242 545 g_source_unref(timeout_source);
dfd917a9 546 g_main_loop_unref(s->compare_loop);
b43decb0 547 g_main_context_unref(s->worker_context);
0682e15b
ZC
548 return NULL;
549}
550
7dce4e6f
ZC
551static char *compare_get_pri_indev(Object *obj, Error **errp)
552{
553 CompareState *s = COLO_COMPARE(obj);
554
555 return g_strdup(s->pri_indev);
556}
557
558static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
559{
560 CompareState *s = COLO_COMPARE(obj);
561
562 g_free(s->pri_indev);
563 s->pri_indev = g_strdup(value);
564}
565
566static char *compare_get_sec_indev(Object *obj, Error **errp)
567{
568 CompareState *s = COLO_COMPARE(obj);
569
570 return g_strdup(s->sec_indev);
571}
572
573static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
574{
575 CompareState *s = COLO_COMPARE(obj);
576
577 g_free(s->sec_indev);
578 s->sec_indev = g_strdup(value);
579}
580
581static char *compare_get_outdev(Object *obj, Error **errp)
582{
583 CompareState *s = COLO_COMPARE(obj);
584
585 return g_strdup(s->outdev);
586}
587
588static void compare_set_outdev(Object *obj, const char *value, Error **errp)
589{
590 CompareState *s = COLO_COMPARE(obj);
591
592 g_free(s->outdev);
593 s->outdev = g_strdup(value);
594}
595
596static void compare_pri_rs_finalize(SocketReadState *pri_rs)
597{
59509ec1
ZC
598 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
599
600 if (packet_enqueue(s, PRIMARY_IN)) {
601 trace_colo_compare_main("primary: unsupported packet in");
5345fdb4 602 compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
0682e15b
ZC
603 } else {
604 /* compare connection */
605 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
59509ec1 606 }
7dce4e6f
ZC
607}
608
609static void compare_sec_rs_finalize(SocketReadState *sec_rs)
610{
59509ec1
ZC
611 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
612
613 if (packet_enqueue(s, SECONDARY_IN)) {
614 trace_colo_compare_main("secondary: unsupported packet in");
0682e15b
ZC
615 } else {
616 /* compare connection */
617 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
59509ec1 618 }
7dce4e6f
ZC
619}
620
7dce4e6f
ZC
621
622/*
623 * Return 0 is success.
624 * Return 1 is failed.
625 */
0ec7b3e7 626static int find_and_check_chardev(Chardev **chr,
7dce4e6f
ZC
627 char *chr_name,
628 Error **errp)
629{
7dce4e6f
ZC
630 *chr = qemu_chr_find(chr_name);
631 if (*chr == NULL) {
632 error_setg(errp, "Device '%s' not found",
633 chr_name);
634 return 1;
635 }
636
0a73336d
DB
637 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
638 error_setg(errp, "chardev \"%s\" is not reconnectable",
7dce4e6f
ZC
639 chr_name);
640 return 1;
641 }
fbf3cc3a 642
7dce4e6f
ZC
643 return 0;
644}
645
646/*
647 * Called from the main thread on the primary
648 * to setup colo-compare.
649 */
650static void colo_compare_complete(UserCreatable *uc, Error **errp)
651{
652 CompareState *s = COLO_COMPARE(uc);
0ec7b3e7 653 Chardev *chr;
0682e15b
ZC
654 char thread_name[64];
655 static int compare_id;
7dce4e6f
ZC
656
657 if (!s->pri_indev || !s->sec_indev || !s->outdev) {
658 error_setg(errp, "colo compare needs 'primary_in' ,"
659 "'secondary_in','outdev' property set");
660 return;
661 } else if (!strcmp(s->pri_indev, s->outdev) ||
662 !strcmp(s->sec_indev, s->outdev) ||
663 !strcmp(s->pri_indev, s->sec_indev)) {
664 error_setg(errp, "'indev' and 'outdev' could not be same "
665 "for compare module");
666 return;
667 }
668
5345fdb4
MAL
669 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
670 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
7dce4e6f
ZC
671 return;
672 }
673
5345fdb4
MAL
674 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
675 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
7dce4e6f
ZC
676 return;
677 }
678
5345fdb4
MAL
679 if (find_and_check_chardev(&chr, s->outdev, errp) ||
680 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
7dce4e6f
ZC
681 return;
682 }
683
7dce4e6f
ZC
684 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
685 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
686
b6540d40
ZC
687 g_queue_init(&s->conn_list);
688
689 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
690 connection_key_equal,
691 g_free,
692 connection_destroy);
59509ec1 693
0682e15b
ZC
694 sprintf(thread_name, "colo-compare %d", compare_id);
695 qemu_thread_create(&s->thread, thread_name,
696 colo_compare_thread, s,
697 QEMU_THREAD_JOINABLE);
698 compare_id++;
699
7dce4e6f
ZC
700 return;
701}
702
dfd917a9
HZ
703static void colo_flush_packets(void *opaque, void *user_data)
704{
705 CompareState *s = user_data;
706 Connection *conn = opaque;
707 Packet *pkt = NULL;
708
709 while (!g_queue_is_empty(&conn->primary_list)) {
710 pkt = g_queue_pop_head(&conn->primary_list);
711 compare_chr_send(&s->chr_out, pkt->data, pkt->size);
712 packet_destroy(pkt, NULL);
713 }
714 while (!g_queue_is_empty(&conn->secondary_list)) {
715 pkt = g_queue_pop_head(&conn->secondary_list);
716 packet_destroy(pkt, NULL);
717 }
718}
719
7dce4e6f
ZC
720static void colo_compare_class_init(ObjectClass *oc, void *data)
721{
722 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
723
724 ucc->complete = colo_compare_complete;
725}
726
727static void colo_compare_init(Object *obj)
728{
729 object_property_add_str(obj, "primary_in",
730 compare_get_pri_indev, compare_set_pri_indev,
731 NULL);
732 object_property_add_str(obj, "secondary_in",
733 compare_get_sec_indev, compare_set_sec_indev,
734 NULL);
735 object_property_add_str(obj, "outdev",
736 compare_get_outdev, compare_set_outdev,
737 NULL);
738}
739
740static void colo_compare_finalize(Object *obj)
741{
742 CompareState *s = COLO_COMPARE(obj);
743
b43decb0
HZ
744 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
745 s->worker_context, true);
746 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
747 s->worker_context, true);
c39860e6 748 qemu_chr_fe_deinit(&s->chr_out);
7dce4e6f 749
dfd917a9
HZ
750 g_main_loop_quit(s->compare_loop);
751 qemu_thread_join(&s->thread);
b6540d40 752
dfd917a9
HZ
753 /* Release all unhandled packets after compare thead exited */
754 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
755
727c2d76 756 g_queue_clear(&s->conn_list);
0682e15b 757
dfd917a9 758 g_hash_table_destroy(s->connection_track_table);
7dce4e6f
ZC
759 g_free(s->pri_indev);
760 g_free(s->sec_indev);
761 g_free(s->outdev);
762}
763
764static const TypeInfo colo_compare_info = {
765 .name = TYPE_COLO_COMPARE,
766 .parent = TYPE_OBJECT,
767 .instance_size = sizeof(CompareState),
768 .instance_init = colo_compare_init,
769 .instance_finalize = colo_compare_finalize,
770 .class_size = sizeof(CompareClass),
771 .class_init = colo_compare_class_init,
772 .interfaces = (InterfaceInfo[]) {
773 { TYPE_USER_CREATABLE },
774 { }
775 }
776};
777
778static void register_types(void)
779{
780 type_register_static(&colo_compare_info);
781}
782
783type_init(register_types);