2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
3 * (a.k.a. Fault Tolerance or Continuous Replication)
5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Copyright (c) 2016 FUJITSU LIMITED
7 * Copyright (c) 2016 Intel Corporation
9 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
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.
15 #include "qemu/osdep.h"
16 #include "qemu/error-report.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/error.h"
23 #include "qom/object_interfaces.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"
33 #define TYPE_COLO_COMPARE "colo-compare"
34 #define COLO_COMPARE(obj) \
35 OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
37 #define COMPARE_READ_LEN_MAX NET_BUFSIZE
38 #define MAX_QUEUE_SIZE 1024
40 /* TODO: Should be configurable */
41 #define REGULAR_PACKET_CHECK_MS 3000
46 +---------------+ +---------------+ +---------------+
47 |conn list +--->conn +--------->conn |
48 +---------------+ +---------------+ +---------------+
50 +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
51 |primary | |secondary |primary | |secondary
52 |packet | |packet + |packet | |packet +
53 +--------+ +--------+ +--------+ +--------+
55 +---v----+ +---v----+ +---v----+ +---v----+
56 |primary | |secondary |primary | |secondary
57 |packet | |packet + |packet | |packet +
58 +--------+ +--------+ +--------+ +--------+
60 +---v----+ +---v----+ +---v----+ +---v----+
61 |primary | |secondary |primary | |secondary
62 |packet | |packet + |packet | |packet +
63 +--------+ +--------+ +--------+ +--------+
65 typedef struct CompareState
{
71 CharBackend chr_pri_in
;
72 CharBackend chr_sec_in
;
74 SocketReadState pri_rs
;
75 SocketReadState sec_rs
;
77 /* connection list: the connections belonged to this NIC could be found
79 * element type: Connection
82 /* hashtable to save connection */
83 GHashTable
*connection_track_table
;
84 /* compare thread, a thread for each NIC */
86 /* Timer used on the primary to find packets that are never matched */
88 QemuMutex timer_check_lock
;
91 typedef struct CompareClass
{
92 ObjectClass parent_class
;
95 typedef struct CompareChardevProps
{
97 } CompareChardevProps
;
104 static int compare_chr_send(CharBackend
*out
,
109 * Return 0 on success, if return -1 means the pkt
110 * is unsupported(arp and ipv6) and will be sent later
112 static int packet_enqueue(CompareState
*s
, int mode
)
118 if (mode
== PRIMARY_IN
) {
119 pkt
= packet_new(s
->pri_rs
.buf
, s
->pri_rs
.packet_len
);
121 pkt
= packet_new(s
->sec_rs
.buf
, s
->sec_rs
.packet_len
);
124 if (parse_packet_early(pkt
)) {
125 packet_destroy(pkt
, NULL
);
129 fill_connection_key(pkt
, &key
);
131 conn
= connection_get(s
->connection_track_table
,
135 if (!conn
->processing
) {
136 g_queue_push_tail(&s
->conn_list
, conn
);
137 conn
->processing
= true;
140 if (mode
== PRIMARY_IN
) {
141 if (g_queue_get_length(&conn
->primary_list
) <=
143 g_queue_push_tail(&conn
->primary_list
, pkt
);
145 error_report("colo compare primary queue size too big,"
149 if (g_queue_get_length(&conn
->secondary_list
) <=
151 g_queue_push_tail(&conn
->secondary_list
, pkt
);
153 error_report("colo compare secondary queue size too big,"
162 * The IP packets sent by primary and secondary
163 * will be compared in here
164 * TODO support ip fragment, Out-Of-Order
165 * return: 0 means packet same
166 * > 0 || < 0 means packet different
168 static int colo_packet_compare(Packet
*ppkt
, Packet
*spkt
)
170 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
171 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
172 inet_ntoa(spkt
->ip
->ip_src
),
173 inet_ntoa(spkt
->ip
->ip_dst
));
175 if (ppkt
->size
== spkt
->size
) {
176 return memcmp(ppkt
->data
, spkt
->data
, spkt
->size
);
183 * Called from the compare thread on the primary
184 * for compare tcp packet
185 * compare_tcp copied from Dr. David Alan Gilbert's branch
187 static int colo_packet_compare_tcp(Packet
*spkt
, Packet
*ppkt
)
189 struct tcphdr
*ptcp
, *stcp
;
192 trace_colo_compare_main("compare tcp");
193 if (ppkt
->size
!= spkt
->size
) {
194 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
195 trace_colo_compare_main("pkt size not same");
200 ptcp
= (struct tcphdr
*)ppkt
->transport_header
;
201 stcp
= (struct tcphdr
*)spkt
->transport_header
;
204 * The 'identification' field in the IP header is *very* random
205 * it almost never matches. Fudge this by ignoring differences in
206 * unfragmented packets; they'll normally sort themselves out if different
207 * anyway, and it should recover at the TCP level.
208 * An alternative would be to get both the primary and secondary to rewrite
209 * somehow; but that would need some sync traffic to sync the state
211 if (ntohs(ppkt
->ip
->ip_off
) & IP_DF
) {
212 spkt
->ip
->ip_id
= ppkt
->ip
->ip_id
;
213 /* and the sum will be different if the IDs were different */
214 spkt
->ip
->ip_sum
= ppkt
->ip
->ip_sum
;
217 res
= memcmp(ppkt
->data
+ ETH_HLEN
, spkt
->data
+ ETH_HLEN
,
218 (spkt
->size
- ETH_HLEN
));
220 if (res
!= 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
221 trace_colo_compare_pkt_info(inet_ntoa(ppkt
->ip
->ip_src
),
222 inet_ntoa(ppkt
->ip
->ip_dst
),
232 qemu_hexdump((char *)ppkt
->data
, stderr
,
233 "colo-compare ppkt", ppkt
->size
);
234 qemu_hexdump((char *)spkt
->data
, stderr
,
235 "colo-compare spkt", spkt
->size
);
242 * Called from the compare thread on the primary
243 * for compare udp packet
245 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
249 trace_colo_compare_main("compare udp");
250 ret
= colo_packet_compare(ppkt
, spkt
);
253 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
254 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare", ppkt
->size
);
255 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
256 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare", spkt
->size
);
263 * Called from the compare thread on the primary
264 * for compare icmp packet
266 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
270 trace_colo_compare_main("compare icmp");
271 network_length
= ppkt
->ip
->ip_hl
* 4;
272 if (ppkt
->size
!= spkt
->size
||
273 ppkt
->size
< network_length
+ ETH_HLEN
) {
277 if (colo_packet_compare(ppkt
, spkt
)) {
278 trace_colo_compare_icmp_miscompare("primary pkt size",
280 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare",
282 trace_colo_compare_icmp_miscompare("Secondary pkt size",
284 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare",
293 * Called from the compare thread on the primary
294 * for compare other packet
296 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
298 trace_colo_compare_main("compare other");
299 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
300 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
301 inet_ntoa(spkt
->ip
->ip_src
),
302 inet_ntoa(spkt
->ip
->ip_dst
));
303 return colo_packet_compare(ppkt
, spkt
);
306 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
308 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
310 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
311 trace_colo_old_packet_check_found(pkt
->creation_ms
);
318 static void colo_old_packet_check_one_conn(void *opaque
,
321 Connection
*conn
= opaque
;
322 GList
*result
= NULL
;
323 int64_t check_time
= REGULAR_PACKET_CHECK_MS
;
325 result
= g_queue_find_custom(&conn
->primary_list
,
327 (GCompareFunc
)colo_old_packet_check_one
);
330 /* do checkpoint will flush old packet */
331 /* TODO: colo_notify_checkpoint();*/
336 * Look for old packets that the secondary hasn't matched,
337 * if we have some then we have to checkpoint to wake
340 static void colo_old_packet_check(void *opaque
)
342 CompareState
*s
= opaque
;
344 g_queue_foreach(&s
->conn_list
, colo_old_packet_check_one_conn
, NULL
);
348 * Called from the compare thread on the primary
349 * for compare connection
351 static void colo_compare_connection(void *opaque
, void *user_data
)
353 CompareState
*s
= user_data
;
354 Connection
*conn
= opaque
;
356 GList
*result
= NULL
;
359 while (!g_queue_is_empty(&conn
->primary_list
) &&
360 !g_queue_is_empty(&conn
->secondary_list
)) {
361 qemu_mutex_lock(&s
->timer_check_lock
);
362 pkt
= g_queue_pop_tail(&conn
->primary_list
);
363 qemu_mutex_unlock(&s
->timer_check_lock
);
364 switch (conn
->ip_proto
) {
366 result
= g_queue_find_custom(&conn
->secondary_list
,
367 pkt
, (GCompareFunc
)colo_packet_compare_tcp
);
370 result
= g_queue_find_custom(&conn
->secondary_list
,
371 pkt
, (GCompareFunc
)colo_packet_compare_udp
);
374 result
= g_queue_find_custom(&conn
->secondary_list
,
375 pkt
, (GCompareFunc
)colo_packet_compare_icmp
);
378 result
= g_queue_find_custom(&conn
->secondary_list
,
379 pkt
, (GCompareFunc
)colo_packet_compare_other
);
384 ret
= compare_chr_send(&s
->chr_out
, pkt
->data
, pkt
->size
);
386 error_report("colo_send_primary_packet failed");
388 trace_colo_compare_main("packet same and release packet");
389 g_queue_remove(&conn
->secondary_list
, result
->data
);
390 packet_destroy(pkt
, NULL
);
393 * If one packet arrive late, the secondary_list or
394 * primary_list will be empty, so we can't compare it
395 * until next comparison.
397 trace_colo_compare_main("packet different");
398 qemu_mutex_lock(&s
->timer_check_lock
);
399 g_queue_push_tail(&conn
->primary_list
, pkt
);
400 qemu_mutex_unlock(&s
->timer_check_lock
);
401 /* TODO: colo_notify_checkpoint();*/
407 static int compare_chr_send(CharBackend
*out
,
412 uint32_t len
= htonl(size
);
418 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)&len
, sizeof(len
));
419 if (ret
!= sizeof(len
)) {
423 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)buf
, size
);
431 return ret
< 0 ? ret
: -EIO
;
434 static int compare_chr_can_read(void *opaque
)
436 return COMPARE_READ_LEN_MAX
;
440 * Called from the main thread on the primary for packets
441 * arriving over the socket from the primary.
443 static void compare_pri_chr_in(void *opaque
, const uint8_t *buf
, int size
)
445 CompareState
*s
= COLO_COMPARE(opaque
);
448 ret
= net_fill_rstate(&s
->pri_rs
, buf
, size
);
450 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, NULL
, NULL
, NULL
,
452 error_report("colo-compare primary_in error");
457 * Called from the main thread on the primary for packets
458 * arriving over the socket from the secondary.
460 static void compare_sec_chr_in(void *opaque
, const uint8_t *buf
, int size
)
462 CompareState
*s
= COLO_COMPARE(opaque
);
465 ret
= net_fill_rstate(&s
->sec_rs
, buf
, size
);
467 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, NULL
, NULL
, NULL
,
469 error_report("colo-compare secondary_in error");
473 static void *colo_compare_thread(void *opaque
)
475 GMainContext
*worker_context
;
476 GMainLoop
*compare_loop
;
477 CompareState
*s
= opaque
;
479 worker_context
= g_main_context_new();
481 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
482 compare_pri_chr_in
, NULL
, s
, worker_context
, true);
483 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
484 compare_sec_chr_in
, NULL
, s
, worker_context
, true);
486 compare_loop
= g_main_loop_new(worker_context
, FALSE
);
488 g_main_loop_run(compare_loop
);
490 g_main_loop_unref(compare_loop
);
491 g_main_context_unref(worker_context
);
495 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
497 CompareState
*s
= COLO_COMPARE(obj
);
499 return g_strdup(s
->pri_indev
);
502 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
504 CompareState
*s
= COLO_COMPARE(obj
);
506 g_free(s
->pri_indev
);
507 s
->pri_indev
= g_strdup(value
);
510 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
512 CompareState
*s
= COLO_COMPARE(obj
);
514 return g_strdup(s
->sec_indev
);
517 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
519 CompareState
*s
= COLO_COMPARE(obj
);
521 g_free(s
->sec_indev
);
522 s
->sec_indev
= g_strdup(value
);
525 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
527 CompareState
*s
= COLO_COMPARE(obj
);
529 return g_strdup(s
->outdev
);
532 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
534 CompareState
*s
= COLO_COMPARE(obj
);
537 s
->outdev
= g_strdup(value
);
540 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
542 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
544 if (packet_enqueue(s
, PRIMARY_IN
)) {
545 trace_colo_compare_main("primary: unsupported packet in");
546 compare_chr_send(&s
->chr_out
, pri_rs
->buf
, pri_rs
->packet_len
);
548 /* compare connection */
549 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
553 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
555 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
557 if (packet_enqueue(s
, SECONDARY_IN
)) {
558 trace_colo_compare_main("secondary: unsupported packet in");
560 /* compare connection */
561 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
567 * Return 0 is success.
568 * Return 1 is failed.
570 static int find_and_check_chardev(CharDriverState
**chr
,
574 CompareChardevProps props
;
576 *chr
= qemu_chr_find(chr_name
);
578 error_setg(errp
, "Device '%s' not found",
583 memset(&props
, 0, sizeof(props
));
585 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
586 error_setg(errp
, "chardev \"%s\" is not reconnectable",
595 * Check old packet regularly so it can watch for any packets
596 * that the secondary hasn't produced equivalents of.
598 static void check_old_packet_regular(void *opaque
)
600 CompareState
*s
= opaque
;
602 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
603 REGULAR_PACKET_CHECK_MS
);
604 /* if have old packet we will notify checkpoint */
606 * TODO: Make timer handler run in compare thread
607 * like qemu_chr_add_handlers_full.
609 qemu_mutex_lock(&s
->timer_check_lock
);
610 colo_old_packet_check(s
);
611 qemu_mutex_unlock(&s
->timer_check_lock
);
615 * Called from the main thread on the primary
616 * to setup colo-compare.
618 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
620 CompareState
*s
= COLO_COMPARE(uc
);
621 CharDriverState
*chr
;
622 char thread_name
[64];
623 static int compare_id
;
625 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
) {
626 error_setg(errp
, "colo compare needs 'primary_in' ,"
627 "'secondary_in','outdev' property set");
629 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
630 !strcmp(s
->sec_indev
, s
->outdev
) ||
631 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
632 error_setg(errp
, "'indev' and 'outdev' could not be same "
633 "for compare module");
637 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
638 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
642 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
643 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
647 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
648 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
652 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
);
653 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
);
655 g_queue_init(&s
->conn_list
);
656 qemu_mutex_init(&s
->timer_check_lock
);
658 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
659 connection_key_equal
,
663 sprintf(thread_name
, "colo-compare %d", compare_id
);
664 qemu_thread_create(&s
->thread
, thread_name
,
665 colo_compare_thread
, s
,
666 QEMU_THREAD_JOINABLE
);
669 /* A regular timer to kick any packets that the secondary doesn't match */
670 s
->timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, /* Only when guest runs */
671 check_old_packet_regular
, s
);
672 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
673 REGULAR_PACKET_CHECK_MS
);
678 static void colo_compare_class_init(ObjectClass
*oc
, void *data
)
680 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
682 ucc
->complete
= colo_compare_complete
;
685 static void colo_compare_init(Object
*obj
)
687 object_property_add_str(obj
, "primary_in",
688 compare_get_pri_indev
, compare_set_pri_indev
,
690 object_property_add_str(obj
, "secondary_in",
691 compare_get_sec_indev
, compare_set_sec_indev
,
693 object_property_add_str(obj
, "outdev",
694 compare_get_outdev
, compare_set_outdev
,
698 static void colo_compare_finalize(Object
*obj
)
700 CompareState
*s
= COLO_COMPARE(obj
);
702 qemu_chr_fe_deinit(&s
->chr_pri_in
);
703 qemu_chr_fe_deinit(&s
->chr_sec_in
);
704 qemu_chr_fe_deinit(&s
->chr_out
);
706 g_queue_free(&s
->conn_list
);
708 if (qemu_thread_is_self(&s
->thread
)) {
709 /* compare connection */
710 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
711 qemu_thread_join(&s
->thread
);
718 qemu_mutex_destroy(&s
->timer_check_lock
);
720 g_free(s
->pri_indev
);
721 g_free(s
->sec_indev
);
725 static const TypeInfo colo_compare_info
= {
726 .name
= TYPE_COLO_COMPARE
,
727 .parent
= TYPE_OBJECT
,
728 .instance_size
= sizeof(CompareState
),
729 .instance_init
= colo_compare_init
,
730 .instance_finalize
= colo_compare_finalize
,
731 .class_size
= sizeof(CompareClass
),
732 .class_init
= colo_compare_class_init
,
733 .interfaces
= (InterfaceInfo
[]) {
734 { TYPE_USER_CREATABLE
},
739 static void register_types(void)
741 type_register_static(&colo_compare_info
);
744 type_init(register_types
);