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
;
100 static int compare_chr_send(CharBackend
*out
,
105 * Return 0 on success, if return -1 means the pkt
106 * is unsupported(arp and ipv6) and will be sent later
108 static int packet_enqueue(CompareState
*s
, int mode
)
114 if (mode
== PRIMARY_IN
) {
115 pkt
= packet_new(s
->pri_rs
.buf
, s
->pri_rs
.packet_len
);
117 pkt
= packet_new(s
->sec_rs
.buf
, s
->sec_rs
.packet_len
);
120 if (parse_packet_early(pkt
)) {
121 packet_destroy(pkt
, NULL
);
125 fill_connection_key(pkt
, &key
);
127 conn
= connection_get(s
->connection_track_table
,
131 if (!conn
->processing
) {
132 g_queue_push_tail(&s
->conn_list
, conn
);
133 conn
->processing
= true;
136 if (mode
== PRIMARY_IN
) {
137 if (g_queue_get_length(&conn
->primary_list
) <=
139 g_queue_push_tail(&conn
->primary_list
, pkt
);
141 error_report("colo compare primary queue size too big,"
145 if (g_queue_get_length(&conn
->secondary_list
) <=
147 g_queue_push_tail(&conn
->secondary_list
, pkt
);
149 error_report("colo compare secondary queue size too big,"
158 * The IP packets sent by primary and secondary
159 * will be compared in here
160 * TODO support ip fragment, Out-Of-Order
161 * return: 0 means packet same
162 * > 0 || < 0 means packet different
164 static int colo_packet_compare(Packet
*ppkt
, Packet
*spkt
)
166 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
167 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
168 inet_ntoa(spkt
->ip
->ip_src
),
169 inet_ntoa(spkt
->ip
->ip_dst
));
171 if (ppkt
->size
== spkt
->size
) {
172 return memcmp(ppkt
->data
, spkt
->data
, spkt
->size
);
179 * Called from the compare thread on the primary
180 * for compare tcp packet
181 * compare_tcp copied from Dr. David Alan Gilbert's branch
183 static int colo_packet_compare_tcp(Packet
*spkt
, Packet
*ppkt
)
185 struct tcphdr
*ptcp
, *stcp
;
188 trace_colo_compare_main("compare tcp");
189 if (ppkt
->size
!= spkt
->size
) {
190 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
191 trace_colo_compare_main("pkt size not same");
196 ptcp
= (struct tcphdr
*)ppkt
->transport_header
;
197 stcp
= (struct tcphdr
*)spkt
->transport_header
;
200 * The 'identification' field in the IP header is *very* random
201 * it almost never matches. Fudge this by ignoring differences in
202 * unfragmented packets; they'll normally sort themselves out if different
203 * anyway, and it should recover at the TCP level.
204 * An alternative would be to get both the primary and secondary to rewrite
205 * somehow; but that would need some sync traffic to sync the state
207 if (ntohs(ppkt
->ip
->ip_off
) & IP_DF
) {
208 spkt
->ip
->ip_id
= ppkt
->ip
->ip_id
;
209 /* and the sum will be different if the IDs were different */
210 spkt
->ip
->ip_sum
= ppkt
->ip
->ip_sum
;
213 res
= memcmp(ppkt
->data
+ ETH_HLEN
, spkt
->data
+ ETH_HLEN
,
214 (spkt
->size
- ETH_HLEN
));
216 if (res
!= 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
217 trace_colo_compare_pkt_info_src(inet_ntoa(ppkt
->ip
->ip_src
),
223 trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt
->ip
->ip_dst
),
229 qemu_hexdump((char *)ppkt
->data
, stderr
,
230 "colo-compare ppkt", ppkt
->size
);
231 qemu_hexdump((char *)spkt
->data
, stderr
,
232 "colo-compare spkt", spkt
->size
);
239 * Called from the compare thread on the primary
240 * for compare udp packet
242 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
246 trace_colo_compare_main("compare udp");
247 ret
= colo_packet_compare(ppkt
, spkt
);
250 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
251 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare", ppkt
->size
);
252 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
253 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare", spkt
->size
);
260 * Called from the compare thread on the primary
261 * for compare icmp packet
263 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
267 trace_colo_compare_main("compare icmp");
268 network_length
= ppkt
->ip
->ip_hl
* 4;
269 if (ppkt
->size
!= spkt
->size
||
270 ppkt
->size
< network_length
+ ETH_HLEN
) {
274 if (colo_packet_compare(ppkt
, spkt
)) {
275 trace_colo_compare_icmp_miscompare("primary pkt size",
277 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare",
279 trace_colo_compare_icmp_miscompare("Secondary pkt size",
281 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare",
290 * Called from the compare thread on the primary
291 * for compare other packet
293 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
295 trace_colo_compare_main("compare other");
296 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
297 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
298 inet_ntoa(spkt
->ip
->ip_src
),
299 inet_ntoa(spkt
->ip
->ip_dst
));
300 return colo_packet_compare(ppkt
, spkt
);
303 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
305 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
307 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
308 trace_colo_old_packet_check_found(pkt
->creation_ms
);
315 static void colo_old_packet_check_one_conn(void *opaque
,
318 Connection
*conn
= opaque
;
319 GList
*result
= NULL
;
320 int64_t check_time
= REGULAR_PACKET_CHECK_MS
;
322 result
= g_queue_find_custom(&conn
->primary_list
,
324 (GCompareFunc
)colo_old_packet_check_one
);
327 /* do checkpoint will flush old packet */
328 /* TODO: colo_notify_checkpoint();*/
333 * Look for old packets that the secondary hasn't matched,
334 * if we have some then we have to checkpoint to wake
337 static void colo_old_packet_check(void *opaque
)
339 CompareState
*s
= opaque
;
341 g_queue_foreach(&s
->conn_list
, colo_old_packet_check_one_conn
, NULL
);
345 * Called from the compare thread on the primary
346 * for compare connection
348 static void colo_compare_connection(void *opaque
, void *user_data
)
350 CompareState
*s
= user_data
;
351 Connection
*conn
= opaque
;
353 GList
*result
= NULL
;
356 while (!g_queue_is_empty(&conn
->primary_list
) &&
357 !g_queue_is_empty(&conn
->secondary_list
)) {
358 qemu_mutex_lock(&s
->timer_check_lock
);
359 pkt
= g_queue_pop_tail(&conn
->primary_list
);
360 qemu_mutex_unlock(&s
->timer_check_lock
);
361 switch (conn
->ip_proto
) {
363 result
= g_queue_find_custom(&conn
->secondary_list
,
364 pkt
, (GCompareFunc
)colo_packet_compare_tcp
);
367 result
= g_queue_find_custom(&conn
->secondary_list
,
368 pkt
, (GCompareFunc
)colo_packet_compare_udp
);
371 result
= g_queue_find_custom(&conn
->secondary_list
,
372 pkt
, (GCompareFunc
)colo_packet_compare_icmp
);
375 result
= g_queue_find_custom(&conn
->secondary_list
,
376 pkt
, (GCompareFunc
)colo_packet_compare_other
);
381 ret
= compare_chr_send(&s
->chr_out
, pkt
->data
, pkt
->size
);
383 error_report("colo_send_primary_packet failed");
385 trace_colo_compare_main("packet same and release packet");
386 g_queue_remove(&conn
->secondary_list
, result
->data
);
387 packet_destroy(pkt
, NULL
);
390 * If one packet arrive late, the secondary_list or
391 * primary_list will be empty, so we can't compare it
392 * until next comparison.
394 trace_colo_compare_main("packet different");
395 qemu_mutex_lock(&s
->timer_check_lock
);
396 g_queue_push_tail(&conn
->primary_list
, pkt
);
397 qemu_mutex_unlock(&s
->timer_check_lock
);
398 /* TODO: colo_notify_checkpoint();*/
404 static int compare_chr_send(CharBackend
*out
,
409 uint32_t len
= htonl(size
);
415 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)&len
, sizeof(len
));
416 if (ret
!= sizeof(len
)) {
420 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)buf
, size
);
428 return ret
< 0 ? ret
: -EIO
;
431 static int compare_chr_can_read(void *opaque
)
433 return COMPARE_READ_LEN_MAX
;
437 * Called from the main thread on the primary for packets
438 * arriving over the socket from the primary.
440 static void compare_pri_chr_in(void *opaque
, const uint8_t *buf
, int size
)
442 CompareState
*s
= COLO_COMPARE(opaque
);
445 ret
= net_fill_rstate(&s
->pri_rs
, buf
, size
);
447 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, NULL
, NULL
, NULL
,
449 error_report("colo-compare primary_in error");
454 * Called from the main thread on the primary for packets
455 * arriving over the socket from the secondary.
457 static void compare_sec_chr_in(void *opaque
, const uint8_t *buf
, int size
)
459 CompareState
*s
= COLO_COMPARE(opaque
);
462 ret
= net_fill_rstate(&s
->sec_rs
, buf
, size
);
464 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, NULL
, NULL
, NULL
,
466 error_report("colo-compare secondary_in error");
470 static void *colo_compare_thread(void *opaque
)
472 GMainContext
*worker_context
;
473 GMainLoop
*compare_loop
;
474 CompareState
*s
= opaque
;
476 worker_context
= g_main_context_new();
478 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
479 compare_pri_chr_in
, NULL
, s
, worker_context
, true);
480 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
481 compare_sec_chr_in
, NULL
, s
, worker_context
, true);
483 compare_loop
= g_main_loop_new(worker_context
, FALSE
);
485 g_main_loop_run(compare_loop
);
487 g_main_loop_unref(compare_loop
);
488 g_main_context_unref(worker_context
);
492 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
494 CompareState
*s
= COLO_COMPARE(obj
);
496 return g_strdup(s
->pri_indev
);
499 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
501 CompareState
*s
= COLO_COMPARE(obj
);
503 g_free(s
->pri_indev
);
504 s
->pri_indev
= g_strdup(value
);
507 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
509 CompareState
*s
= COLO_COMPARE(obj
);
511 return g_strdup(s
->sec_indev
);
514 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
516 CompareState
*s
= COLO_COMPARE(obj
);
518 g_free(s
->sec_indev
);
519 s
->sec_indev
= g_strdup(value
);
522 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
524 CompareState
*s
= COLO_COMPARE(obj
);
526 return g_strdup(s
->outdev
);
529 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
531 CompareState
*s
= COLO_COMPARE(obj
);
534 s
->outdev
= g_strdup(value
);
537 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
539 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
541 if (packet_enqueue(s
, PRIMARY_IN
)) {
542 trace_colo_compare_main("primary: unsupported packet in");
543 compare_chr_send(&s
->chr_out
, pri_rs
->buf
, pri_rs
->packet_len
);
545 /* compare connection */
546 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
550 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
552 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
554 if (packet_enqueue(s
, SECONDARY_IN
)) {
555 trace_colo_compare_main("secondary: unsupported packet in");
557 /* compare connection */
558 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
564 * Return 0 is success.
565 * Return 1 is failed.
567 static int find_and_check_chardev(CharDriverState
**chr
,
571 *chr
= qemu_chr_find(chr_name
);
573 error_setg(errp
, "Device '%s' not found",
578 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
579 error_setg(errp
, "chardev \"%s\" is not reconnectable",
588 * Check old packet regularly so it can watch for any packets
589 * that the secondary hasn't produced equivalents of.
591 static void check_old_packet_regular(void *opaque
)
593 CompareState
*s
= opaque
;
595 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
596 REGULAR_PACKET_CHECK_MS
);
597 /* if have old packet we will notify checkpoint */
599 * TODO: Make timer handler run in compare thread
600 * like qemu_chr_add_handlers_full.
602 qemu_mutex_lock(&s
->timer_check_lock
);
603 colo_old_packet_check(s
);
604 qemu_mutex_unlock(&s
->timer_check_lock
);
608 * Called from the main thread on the primary
609 * to setup colo-compare.
611 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
613 CompareState
*s
= COLO_COMPARE(uc
);
614 CharDriverState
*chr
;
615 char thread_name
[64];
616 static int compare_id
;
618 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
) {
619 error_setg(errp
, "colo compare needs 'primary_in' ,"
620 "'secondary_in','outdev' property set");
622 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
623 !strcmp(s
->sec_indev
, s
->outdev
) ||
624 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
625 error_setg(errp
, "'indev' and 'outdev' could not be same "
626 "for compare module");
630 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
631 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
635 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
636 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
640 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
641 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
645 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
);
646 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
);
648 g_queue_init(&s
->conn_list
);
649 qemu_mutex_init(&s
->timer_check_lock
);
651 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
652 connection_key_equal
,
656 sprintf(thread_name
, "colo-compare %d", compare_id
);
657 qemu_thread_create(&s
->thread
, thread_name
,
658 colo_compare_thread
, s
,
659 QEMU_THREAD_JOINABLE
);
662 /* A regular timer to kick any packets that the secondary doesn't match */
663 s
->timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, /* Only when guest runs */
664 check_old_packet_regular
, s
);
665 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
666 REGULAR_PACKET_CHECK_MS
);
671 static void colo_compare_class_init(ObjectClass
*oc
, void *data
)
673 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
675 ucc
->complete
= colo_compare_complete
;
678 static void colo_compare_init(Object
*obj
)
680 object_property_add_str(obj
, "primary_in",
681 compare_get_pri_indev
, compare_set_pri_indev
,
683 object_property_add_str(obj
, "secondary_in",
684 compare_get_sec_indev
, compare_set_sec_indev
,
686 object_property_add_str(obj
, "outdev",
687 compare_get_outdev
, compare_set_outdev
,
691 static void colo_compare_finalize(Object
*obj
)
693 CompareState
*s
= COLO_COMPARE(obj
);
695 qemu_chr_fe_deinit(&s
->chr_pri_in
);
696 qemu_chr_fe_deinit(&s
->chr_sec_in
);
697 qemu_chr_fe_deinit(&s
->chr_out
);
699 g_queue_free(&s
->conn_list
);
701 if (qemu_thread_is_self(&s
->thread
)) {
702 /* compare connection */
703 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
704 qemu_thread_join(&s
->thread
);
711 qemu_mutex_destroy(&s
->timer_check_lock
);
713 g_free(s
->pri_indev
);
714 g_free(s
->sec_indev
);
718 static const TypeInfo colo_compare_info
= {
719 .name
= TYPE_COLO_COMPARE
,
720 .parent
= TYPE_OBJECT
,
721 .instance_size
= sizeof(CompareState
),
722 .instance_init
= colo_compare_init
,
723 .instance_finalize
= colo_compare_finalize
,
724 .class_size
= sizeof(CompareClass
),
725 .class_init
= colo_compare_class_init
,
726 .interfaces
= (InterfaceInfo
[]) {
727 { TYPE_USER_CREATABLE
},
732 static void register_types(void)
734 type_register_static(&colo_compare_info
);
737 type_init(register_types
);