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(inet_ntoa(ppkt
->ip
->ip_src
),
218 inet_ntoa(ppkt
->ip
->ip_dst
),
228 qemu_hexdump((char *)ppkt
->data
, stderr
,
229 "colo-compare ppkt", ppkt
->size
);
230 qemu_hexdump((char *)spkt
->data
, stderr
,
231 "colo-compare spkt", spkt
->size
);
238 * Called from the compare thread on the primary
239 * for compare udp packet
241 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
245 trace_colo_compare_main("compare udp");
246 ret
= colo_packet_compare(ppkt
, spkt
);
249 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
250 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare", ppkt
->size
);
251 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
252 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare", spkt
->size
);
259 * Called from the compare thread on the primary
260 * for compare icmp packet
262 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
266 trace_colo_compare_main("compare icmp");
267 network_length
= ppkt
->ip
->ip_hl
* 4;
268 if (ppkt
->size
!= spkt
->size
||
269 ppkt
->size
< network_length
+ ETH_HLEN
) {
273 if (colo_packet_compare(ppkt
, spkt
)) {
274 trace_colo_compare_icmp_miscompare("primary pkt size",
276 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare",
278 trace_colo_compare_icmp_miscompare("Secondary pkt size",
280 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare",
289 * Called from the compare thread on the primary
290 * for compare other packet
292 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
294 trace_colo_compare_main("compare other");
295 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
296 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
297 inet_ntoa(spkt
->ip
->ip_src
),
298 inet_ntoa(spkt
->ip
->ip_dst
));
299 return colo_packet_compare(ppkt
, spkt
);
302 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
304 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
306 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
307 trace_colo_old_packet_check_found(pkt
->creation_ms
);
314 static void colo_old_packet_check_one_conn(void *opaque
,
317 Connection
*conn
= opaque
;
318 GList
*result
= NULL
;
319 int64_t check_time
= REGULAR_PACKET_CHECK_MS
;
321 result
= g_queue_find_custom(&conn
->primary_list
,
323 (GCompareFunc
)colo_old_packet_check_one
);
326 /* do checkpoint will flush old packet */
327 /* TODO: colo_notify_checkpoint();*/
332 * Look for old packets that the secondary hasn't matched,
333 * if we have some then we have to checkpoint to wake
336 static void colo_old_packet_check(void *opaque
)
338 CompareState
*s
= opaque
;
340 g_queue_foreach(&s
->conn_list
, colo_old_packet_check_one_conn
, NULL
);
344 * Called from the compare thread on the primary
345 * for compare connection
347 static void colo_compare_connection(void *opaque
, void *user_data
)
349 CompareState
*s
= user_data
;
350 Connection
*conn
= opaque
;
352 GList
*result
= NULL
;
355 while (!g_queue_is_empty(&conn
->primary_list
) &&
356 !g_queue_is_empty(&conn
->secondary_list
)) {
357 qemu_mutex_lock(&s
->timer_check_lock
);
358 pkt
= g_queue_pop_tail(&conn
->primary_list
);
359 qemu_mutex_unlock(&s
->timer_check_lock
);
360 switch (conn
->ip_proto
) {
362 result
= g_queue_find_custom(&conn
->secondary_list
,
363 pkt
, (GCompareFunc
)colo_packet_compare_tcp
);
366 result
= g_queue_find_custom(&conn
->secondary_list
,
367 pkt
, (GCompareFunc
)colo_packet_compare_udp
);
370 result
= g_queue_find_custom(&conn
->secondary_list
,
371 pkt
, (GCompareFunc
)colo_packet_compare_icmp
);
374 result
= g_queue_find_custom(&conn
->secondary_list
,
375 pkt
, (GCompareFunc
)colo_packet_compare_other
);
380 ret
= compare_chr_send(&s
->chr_out
, pkt
->data
, pkt
->size
);
382 error_report("colo_send_primary_packet failed");
384 trace_colo_compare_main("packet same and release packet");
385 g_queue_remove(&conn
->secondary_list
, result
->data
);
386 packet_destroy(pkt
, NULL
);
389 * If one packet arrive late, the secondary_list or
390 * primary_list will be empty, so we can't compare it
391 * until next comparison.
393 trace_colo_compare_main("packet different");
394 qemu_mutex_lock(&s
->timer_check_lock
);
395 g_queue_push_tail(&conn
->primary_list
, pkt
);
396 qemu_mutex_unlock(&s
->timer_check_lock
);
397 /* TODO: colo_notify_checkpoint();*/
403 static int compare_chr_send(CharBackend
*out
,
408 uint32_t len
= htonl(size
);
414 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)&len
, sizeof(len
));
415 if (ret
!= sizeof(len
)) {
419 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)buf
, size
);
427 return ret
< 0 ? ret
: -EIO
;
430 static int compare_chr_can_read(void *opaque
)
432 return COMPARE_READ_LEN_MAX
;
436 * Called from the main thread on the primary for packets
437 * arriving over the socket from the primary.
439 static void compare_pri_chr_in(void *opaque
, const uint8_t *buf
, int size
)
441 CompareState
*s
= COLO_COMPARE(opaque
);
444 ret
= net_fill_rstate(&s
->pri_rs
, buf
, size
);
446 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, NULL
, NULL
, NULL
,
448 error_report("colo-compare primary_in error");
453 * Called from the main thread on the primary for packets
454 * arriving over the socket from the secondary.
456 static void compare_sec_chr_in(void *opaque
, const uint8_t *buf
, int size
)
458 CompareState
*s
= COLO_COMPARE(opaque
);
461 ret
= net_fill_rstate(&s
->sec_rs
, buf
, size
);
463 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, NULL
, NULL
, NULL
,
465 error_report("colo-compare secondary_in error");
469 static void *colo_compare_thread(void *opaque
)
471 GMainContext
*worker_context
;
472 GMainLoop
*compare_loop
;
473 CompareState
*s
= opaque
;
475 worker_context
= g_main_context_new();
477 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
478 compare_pri_chr_in
, NULL
, s
, worker_context
, true);
479 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
480 compare_sec_chr_in
, NULL
, s
, worker_context
, true);
482 compare_loop
= g_main_loop_new(worker_context
, FALSE
);
484 g_main_loop_run(compare_loop
);
486 g_main_loop_unref(compare_loop
);
487 g_main_context_unref(worker_context
);
491 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
493 CompareState
*s
= COLO_COMPARE(obj
);
495 return g_strdup(s
->pri_indev
);
498 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
500 CompareState
*s
= COLO_COMPARE(obj
);
502 g_free(s
->pri_indev
);
503 s
->pri_indev
= g_strdup(value
);
506 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
508 CompareState
*s
= COLO_COMPARE(obj
);
510 return g_strdup(s
->sec_indev
);
513 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
515 CompareState
*s
= COLO_COMPARE(obj
);
517 g_free(s
->sec_indev
);
518 s
->sec_indev
= g_strdup(value
);
521 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
523 CompareState
*s
= COLO_COMPARE(obj
);
525 return g_strdup(s
->outdev
);
528 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
530 CompareState
*s
= COLO_COMPARE(obj
);
533 s
->outdev
= g_strdup(value
);
536 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
538 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
540 if (packet_enqueue(s
, PRIMARY_IN
)) {
541 trace_colo_compare_main("primary: unsupported packet in");
542 compare_chr_send(&s
->chr_out
, pri_rs
->buf
, pri_rs
->packet_len
);
544 /* compare connection */
545 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
549 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
551 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
553 if (packet_enqueue(s
, SECONDARY_IN
)) {
554 trace_colo_compare_main("secondary: unsupported packet in");
556 /* compare connection */
557 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
563 * Return 0 is success.
564 * Return 1 is failed.
566 static int find_and_check_chardev(CharDriverState
**chr
,
570 *chr
= qemu_chr_find(chr_name
);
572 error_setg(errp
, "Device '%s' not found",
577 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
578 error_setg(errp
, "chardev \"%s\" is not reconnectable",
587 * Check old packet regularly so it can watch for any packets
588 * that the secondary hasn't produced equivalents of.
590 static void check_old_packet_regular(void *opaque
)
592 CompareState
*s
= opaque
;
594 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
595 REGULAR_PACKET_CHECK_MS
);
596 /* if have old packet we will notify checkpoint */
598 * TODO: Make timer handler run in compare thread
599 * like qemu_chr_add_handlers_full.
601 qemu_mutex_lock(&s
->timer_check_lock
);
602 colo_old_packet_check(s
);
603 qemu_mutex_unlock(&s
->timer_check_lock
);
607 * Called from the main thread on the primary
608 * to setup colo-compare.
610 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
612 CompareState
*s
= COLO_COMPARE(uc
);
613 CharDriverState
*chr
;
614 char thread_name
[64];
615 static int compare_id
;
617 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
) {
618 error_setg(errp
, "colo compare needs 'primary_in' ,"
619 "'secondary_in','outdev' property set");
621 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
622 !strcmp(s
->sec_indev
, s
->outdev
) ||
623 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
624 error_setg(errp
, "'indev' and 'outdev' could not be same "
625 "for compare module");
629 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
630 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
634 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
635 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
639 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
640 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
644 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
);
645 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
);
647 g_queue_init(&s
->conn_list
);
648 qemu_mutex_init(&s
->timer_check_lock
);
650 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
651 connection_key_equal
,
655 sprintf(thread_name
, "colo-compare %d", compare_id
);
656 qemu_thread_create(&s
->thread
, thread_name
,
657 colo_compare_thread
, s
,
658 QEMU_THREAD_JOINABLE
);
661 /* A regular timer to kick any packets that the secondary doesn't match */
662 s
->timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, /* Only when guest runs */
663 check_old_packet_regular
, s
);
664 timer_mod(s
->timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
665 REGULAR_PACKET_CHECK_MS
);
670 static void colo_compare_class_init(ObjectClass
*oc
, void *data
)
672 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
674 ucc
->complete
= colo_compare_complete
;
677 static void colo_compare_init(Object
*obj
)
679 object_property_add_str(obj
, "primary_in",
680 compare_get_pri_indev
, compare_set_pri_indev
,
682 object_property_add_str(obj
, "secondary_in",
683 compare_get_sec_indev
, compare_set_sec_indev
,
685 object_property_add_str(obj
, "outdev",
686 compare_get_outdev
, compare_set_outdev
,
690 static void colo_compare_finalize(Object
*obj
)
692 CompareState
*s
= COLO_COMPARE(obj
);
694 qemu_chr_fe_deinit(&s
->chr_pri_in
);
695 qemu_chr_fe_deinit(&s
->chr_sec_in
);
696 qemu_chr_fe_deinit(&s
->chr_out
);
698 g_queue_free(&s
->conn_list
);
700 if (qemu_thread_is_self(&s
->thread
)) {
701 /* compare connection */
702 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
703 qemu_thread_join(&s
->thread
);
710 qemu_mutex_destroy(&s
->timer_check_lock
);
712 g_free(s
->pri_indev
);
713 g_free(s
->sec_indev
);
717 static const TypeInfo colo_compare_info
= {
718 .name
= TYPE_COLO_COMPARE
,
719 .parent
= TYPE_OBJECT
,
720 .instance_size
= sizeof(CompareState
),
721 .instance_init
= colo_compare_init
,
722 .instance_finalize
= colo_compare_finalize
,
723 .class_size
= sizeof(CompareClass
),
724 .class_init
= colo_compare_class_init
,
725 .interfaces
= (InterfaceInfo
[]) {
726 { TYPE_USER_CREATABLE
},
731 static void register_types(void)
733 type_register_static(&colo_compare_info
);
736 type_init(register_types
);