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 */
88 typedef struct CompareClass
{
89 ObjectClass parent_class
;
97 static int compare_chr_send(CharBackend
*out
,
101 static gint
seq_sorter(Packet
*a
, Packet
*b
, gpointer data
)
103 struct tcphdr
*atcp
, *btcp
;
105 atcp
= (struct tcphdr
*)(a
->transport_header
);
106 btcp
= (struct tcphdr
*)(b
->transport_header
);
107 return ntohl(atcp
->th_seq
) - ntohl(btcp
->th_seq
);
111 * Return 0 on success, if return -1 means the pkt
112 * is unsupported(arp and ipv6) and will be sent later
114 static int packet_enqueue(CompareState
*s
, int mode
)
120 if (mode
== PRIMARY_IN
) {
121 pkt
= packet_new(s
->pri_rs
.buf
, s
->pri_rs
.packet_len
);
123 pkt
= packet_new(s
->sec_rs
.buf
, s
->sec_rs
.packet_len
);
126 if (parse_packet_early(pkt
)) {
127 packet_destroy(pkt
, NULL
);
131 fill_connection_key(pkt
, &key
);
133 conn
= connection_get(s
->connection_track_table
,
137 if (!conn
->processing
) {
138 g_queue_push_tail(&s
->conn_list
, conn
);
139 conn
->processing
= true;
142 if (mode
== PRIMARY_IN
) {
143 if (g_queue_get_length(&conn
->primary_list
) <=
145 g_queue_push_tail(&conn
->primary_list
, pkt
);
146 if (conn
->ip_proto
== IPPROTO_TCP
) {
147 g_queue_sort(&conn
->primary_list
,
148 (GCompareDataFunc
)seq_sorter
,
152 error_report("colo compare primary queue size too big,"
156 if (g_queue_get_length(&conn
->secondary_list
) <=
158 g_queue_push_tail(&conn
->secondary_list
, pkt
);
159 if (conn
->ip_proto
== IPPROTO_TCP
) {
160 g_queue_sort(&conn
->secondary_list
,
161 (GCompareDataFunc
)seq_sorter
,
165 error_report("colo compare secondary queue size too big,"
174 * The IP packets sent by primary and secondary
175 * will be compared in here
176 * TODO support ip fragment, Out-Of-Order
177 * return: 0 means packet same
178 * > 0 || < 0 means packet different
180 static int colo_packet_compare(Packet
*ppkt
, Packet
*spkt
)
182 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
183 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
184 inet_ntoa(spkt
->ip
->ip_src
),
185 inet_ntoa(spkt
->ip
->ip_dst
));
187 if (ppkt
->size
== spkt
->size
) {
188 return memcmp(ppkt
->data
, spkt
->data
, spkt
->size
);
195 * Called from the compare thread on the primary
196 * for compare tcp packet
197 * compare_tcp copied from Dr. David Alan Gilbert's branch
199 static int colo_packet_compare_tcp(Packet
*spkt
, Packet
*ppkt
)
201 struct tcphdr
*ptcp
, *stcp
;
204 trace_colo_compare_main("compare tcp");
205 if (ppkt
->size
!= spkt
->size
) {
206 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
207 trace_colo_compare_main("pkt size not same");
212 ptcp
= (struct tcphdr
*)ppkt
->transport_header
;
213 stcp
= (struct tcphdr
*)spkt
->transport_header
;
216 * The 'identification' field in the IP header is *very* random
217 * it almost never matches. Fudge this by ignoring differences in
218 * unfragmented packets; they'll normally sort themselves out if different
219 * anyway, and it should recover at the TCP level.
220 * An alternative would be to get both the primary and secondary to rewrite
221 * somehow; but that would need some sync traffic to sync the state
223 if (ntohs(ppkt
->ip
->ip_off
) & IP_DF
) {
224 spkt
->ip
->ip_id
= ppkt
->ip
->ip_id
;
225 /* and the sum will be different if the IDs were different */
226 spkt
->ip
->ip_sum
= ppkt
->ip
->ip_sum
;
229 res
= memcmp(ppkt
->data
+ ETH_HLEN
, spkt
->data
+ ETH_HLEN
,
230 (spkt
->size
- ETH_HLEN
));
232 if (res
!= 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE
)) {
233 trace_colo_compare_pkt_info_src(inet_ntoa(ppkt
->ip
->ip_src
),
239 trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt
->ip
->ip_dst
),
245 qemu_hexdump((char *)ppkt
->data
, stderr
,
246 "colo-compare ppkt", ppkt
->size
);
247 qemu_hexdump((char *)spkt
->data
, stderr
,
248 "colo-compare spkt", spkt
->size
);
255 * Called from the compare thread on the primary
256 * for compare udp packet
258 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
262 trace_colo_compare_main("compare udp");
263 ret
= colo_packet_compare(ppkt
, spkt
);
266 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
267 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare", ppkt
->size
);
268 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
269 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare", spkt
->size
);
276 * Called from the compare thread on the primary
277 * for compare icmp packet
279 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
283 trace_colo_compare_main("compare icmp");
284 network_length
= ppkt
->ip
->ip_hl
* 4;
285 if (ppkt
->size
!= spkt
->size
||
286 ppkt
->size
< network_length
+ ETH_HLEN
) {
290 if (colo_packet_compare(ppkt
, spkt
)) {
291 trace_colo_compare_icmp_miscompare("primary pkt size",
293 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare",
295 trace_colo_compare_icmp_miscompare("Secondary pkt size",
297 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare",
306 * Called from the compare thread on the primary
307 * for compare other packet
309 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
311 trace_colo_compare_main("compare other");
312 trace_colo_compare_ip_info(ppkt
->size
, inet_ntoa(ppkt
->ip
->ip_src
),
313 inet_ntoa(ppkt
->ip
->ip_dst
), spkt
->size
,
314 inet_ntoa(spkt
->ip
->ip_src
),
315 inet_ntoa(spkt
->ip
->ip_dst
));
316 return colo_packet_compare(ppkt
, spkt
);
319 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
321 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
323 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
324 trace_colo_old_packet_check_found(pkt
->creation_ms
);
331 static void colo_old_packet_check_one_conn(void *opaque
,
334 Connection
*conn
= opaque
;
335 GList
*result
= NULL
;
336 int64_t check_time
= REGULAR_PACKET_CHECK_MS
;
338 result
= g_queue_find_custom(&conn
->primary_list
,
340 (GCompareFunc
)colo_old_packet_check_one
);
343 /* do checkpoint will flush old packet */
344 /* TODO: colo_notify_checkpoint();*/
349 * Look for old packets that the secondary hasn't matched,
350 * if we have some then we have to checkpoint to wake
353 static void colo_old_packet_check(void *opaque
)
355 CompareState
*s
= opaque
;
357 g_queue_foreach(&s
->conn_list
, colo_old_packet_check_one_conn
, NULL
);
361 * Called from the compare thread on the primary
362 * for compare connection
364 static void colo_compare_connection(void *opaque
, void *user_data
)
366 CompareState
*s
= user_data
;
367 Connection
*conn
= opaque
;
369 GList
*result
= NULL
;
372 while (!g_queue_is_empty(&conn
->primary_list
) &&
373 !g_queue_is_empty(&conn
->secondary_list
)) {
374 pkt
= g_queue_pop_tail(&conn
->primary_list
);
375 switch (conn
->ip_proto
) {
377 result
= g_queue_find_custom(&conn
->secondary_list
,
378 pkt
, (GCompareFunc
)colo_packet_compare_tcp
);
381 result
= g_queue_find_custom(&conn
->secondary_list
,
382 pkt
, (GCompareFunc
)colo_packet_compare_udp
);
385 result
= g_queue_find_custom(&conn
->secondary_list
,
386 pkt
, (GCompareFunc
)colo_packet_compare_icmp
);
389 result
= g_queue_find_custom(&conn
->secondary_list
,
390 pkt
, (GCompareFunc
)colo_packet_compare_other
);
395 ret
= compare_chr_send(&s
->chr_out
, pkt
->data
, pkt
->size
);
397 error_report("colo_send_primary_packet failed");
399 trace_colo_compare_main("packet same and release packet");
400 g_queue_remove(&conn
->secondary_list
, result
->data
);
401 packet_destroy(pkt
, NULL
);
404 * If one packet arrive late, the secondary_list or
405 * primary_list will be empty, so we can't compare it
406 * until next comparison.
408 trace_colo_compare_main("packet different");
409 g_queue_push_tail(&conn
->primary_list
, pkt
);
410 /* TODO: colo_notify_checkpoint();*/
416 static int compare_chr_send(CharBackend
*out
,
421 uint32_t len
= htonl(size
);
427 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)&len
, sizeof(len
));
428 if (ret
!= sizeof(len
)) {
432 ret
= qemu_chr_fe_write_all(out
, (uint8_t *)buf
, size
);
440 return ret
< 0 ? ret
: -EIO
;
443 static int compare_chr_can_read(void *opaque
)
445 return COMPARE_READ_LEN_MAX
;
449 * Called from the main thread on the primary for packets
450 * arriving over the socket from the primary.
452 static void compare_pri_chr_in(void *opaque
, const uint8_t *buf
, int size
)
454 CompareState
*s
= COLO_COMPARE(opaque
);
457 ret
= net_fill_rstate(&s
->pri_rs
, buf
, size
);
459 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, NULL
, NULL
, NULL
,
461 error_report("colo-compare primary_in error");
466 * Called from the main thread on the primary for packets
467 * arriving over the socket from the secondary.
469 static void compare_sec_chr_in(void *opaque
, const uint8_t *buf
, int size
)
471 CompareState
*s
= COLO_COMPARE(opaque
);
474 ret
= net_fill_rstate(&s
->sec_rs
, buf
, size
);
476 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, NULL
, NULL
, NULL
,
478 error_report("colo-compare secondary_in error");
483 * Check old packet regularly so it can watch for any packets
484 * that the secondary hasn't produced equivalents of.
486 static gboolean
check_old_packet_regular(void *opaque
)
488 CompareState
*s
= opaque
;
490 /* if have old packet we will notify checkpoint */
491 colo_old_packet_check(s
);
496 static void *colo_compare_thread(void *opaque
)
498 GMainContext
*worker_context
;
499 GMainLoop
*compare_loop
;
500 CompareState
*s
= opaque
;
501 GSource
*timeout_source
;
503 worker_context
= g_main_context_new();
505 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
506 compare_pri_chr_in
, NULL
, s
, worker_context
, true);
507 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
508 compare_sec_chr_in
, NULL
, s
, worker_context
, true);
510 compare_loop
= g_main_loop_new(worker_context
, FALSE
);
512 /* To kick any packets that the secondary doesn't match */
513 timeout_source
= g_timeout_source_new(REGULAR_PACKET_CHECK_MS
);
514 g_source_set_callback(timeout_source
,
515 (GSourceFunc
)check_old_packet_regular
, s
, NULL
);
516 g_source_attach(timeout_source
, worker_context
);
518 g_main_loop_run(compare_loop
);
520 g_source_unref(timeout_source
);
521 g_main_loop_unref(compare_loop
);
522 g_main_context_unref(worker_context
);
526 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
528 CompareState
*s
= COLO_COMPARE(obj
);
530 return g_strdup(s
->pri_indev
);
533 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
535 CompareState
*s
= COLO_COMPARE(obj
);
537 g_free(s
->pri_indev
);
538 s
->pri_indev
= g_strdup(value
);
541 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
543 CompareState
*s
= COLO_COMPARE(obj
);
545 return g_strdup(s
->sec_indev
);
548 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
550 CompareState
*s
= COLO_COMPARE(obj
);
552 g_free(s
->sec_indev
);
553 s
->sec_indev
= g_strdup(value
);
556 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
558 CompareState
*s
= COLO_COMPARE(obj
);
560 return g_strdup(s
->outdev
);
563 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
565 CompareState
*s
= COLO_COMPARE(obj
);
568 s
->outdev
= g_strdup(value
);
571 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
573 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
575 if (packet_enqueue(s
, PRIMARY_IN
)) {
576 trace_colo_compare_main("primary: unsupported packet in");
577 compare_chr_send(&s
->chr_out
, pri_rs
->buf
, pri_rs
->packet_len
);
579 /* compare connection */
580 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
584 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
586 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
588 if (packet_enqueue(s
, SECONDARY_IN
)) {
589 trace_colo_compare_main("secondary: unsupported packet in");
591 /* compare connection */
592 g_queue_foreach(&s
->conn_list
, colo_compare_connection
, s
);
598 * Return 0 is success.
599 * Return 1 is failed.
601 static int find_and_check_chardev(Chardev
**chr
,
605 *chr
= qemu_chr_find(chr_name
);
607 error_setg(errp
, "Device '%s' not found",
612 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
613 error_setg(errp
, "chardev \"%s\" is not reconnectable",
622 * Called from the main thread on the primary
623 * to setup colo-compare.
625 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
627 CompareState
*s
= COLO_COMPARE(uc
);
629 char thread_name
[64];
630 static int compare_id
;
632 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
) {
633 error_setg(errp
, "colo compare needs 'primary_in' ,"
634 "'secondary_in','outdev' property set");
636 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
637 !strcmp(s
->sec_indev
, s
->outdev
) ||
638 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
639 error_setg(errp
, "'indev' and 'outdev' could not be same "
640 "for compare module");
644 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
645 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
649 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
650 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
654 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
655 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
659 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
);
660 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
);
662 g_queue_init(&s
->conn_list
);
664 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
665 connection_key_equal
,
669 sprintf(thread_name
, "colo-compare %d", compare_id
);
670 qemu_thread_create(&s
->thread
, thread_name
,
671 colo_compare_thread
, s
,
672 QEMU_THREAD_JOINABLE
);
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
);
714 g_free(s
->pri_indev
);
715 g_free(s
->sec_indev
);
719 static const TypeInfo colo_compare_info
= {
720 .name
= TYPE_COLO_COMPARE
,
721 .parent
= TYPE_OBJECT
,
722 .instance_size
= sizeof(CompareState
),
723 .instance_init
= colo_compare_init
,
724 .instance_finalize
= colo_compare_finalize
,
725 .class_size
= sizeof(CompareClass
),
726 .class_init
= colo_compare_class_init
,
727 .interfaces
= (InterfaceInfo
[]) {
728 { TYPE_USER_CREATABLE
},
733 static void register_types(void)
735 type_register_static(&colo_compare_info
);
738 type_init(register_types
);