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 "qapi/error.h"
21 #include "qom/object_interfaces.h"
23 #include "qom/object.h"
24 #include "net/queue.h"
25 #include "chardev/char-fe.h"
26 #include "qemu/sockets.h"
28 #include "sysemu/iothread.h"
29 #include "net/colo-compare.h"
30 #include "migration/colo.h"
31 #include "migration/migration.h"
34 #include "block/aio-wait.h"
35 #include "qemu/coroutine.h"
37 #define TYPE_COLO_COMPARE "colo-compare"
38 typedef struct CompareState CompareState
;
39 DECLARE_INSTANCE_CHECKER(CompareState
, COLO_COMPARE
,
42 static QTAILQ_HEAD(, CompareState
) net_compares
=
43 QTAILQ_HEAD_INITIALIZER(net_compares
);
45 static NotifierList colo_compare_notifiers
=
46 NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers
);
48 #define COMPARE_READ_LEN_MAX NET_BUFSIZE
49 #define MAX_QUEUE_SIZE 1024
51 #define COLO_COMPARE_FREE_PRIMARY 0x01
52 #define COLO_COMPARE_FREE_SECONDARY 0x02
54 #define REGULAR_PACKET_CHECK_MS 1000
55 #define DEFAULT_TIME_OUT_MS 3000
57 /* #define DEBUG_COLO_PACKETS */
59 static QemuMutex colo_compare_mutex
;
60 static bool colo_compare_active
;
61 static QemuMutex event_mtx
;
62 static QemuCond event_complete_cond
;
63 static int event_unhandled_count
;
64 static uint32_t max_queue_size
;
69 * +---------------+ +---------------+ +---------------+
70 * | conn list + - > conn + ------- > conn + -- > ......
71 * +---------------+ +---------------+ +---------------+
73 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
74 * |primary | |secondary |primary | |secondary
75 * |packet | |packet + |packet | |packet +
76 * +--------+ +--------+ +--------+ +--------+
78 * +---v----+ +---v----+ +---v----+ +---v----+
79 * |primary | |secondary |primary | |secondary
80 * |packet | |packet + |packet | |packet +
81 * +--------+ +--------+ +--------+ +--------+
83 * +---v----+ +---v----+ +---v----+ +---v----+
84 * |primary | |secondary |primary | |secondary
85 * |packet | |packet + |packet | |packet +
86 * +--------+ +--------+ +--------+ +--------+
89 typedef struct SendCo
{
91 struct CompareState
*s
;
94 bool notify_remote_frame
;
99 typedef struct SendEntry
{
101 uint32_t vnet_hdr_len
;
105 struct CompareState
{
112 CharBackend chr_pri_in
;
113 CharBackend chr_sec_in
;
115 CharBackend chr_notify_dev
;
116 SocketReadState pri_rs
;
117 SocketReadState sec_rs
;
118 SocketReadState notify_rs
;
120 SendCo notify_sendco
;
122 uint64_t compare_timeout
;
123 uint32_t expired_scan_cycle
;
126 * Record the connection that through the NIC
127 * Element type: Connection
130 /* Record the connection without repetition */
131 GHashTable
*connection_track_table
;
134 GMainContext
*worker_context
;
135 QEMUTimer
*packet_check_timer
;
138 enum colo_event event
;
140 QTAILQ_ENTRY(CompareState
) next
;
143 typedef struct CompareClass
{
144 ObjectClass parent_class
;
152 static const char *colo_mode
[] = {
153 [PRIMARY_IN
] = "primary",
154 [SECONDARY_IN
] = "secondary",
157 static int compare_chr_send(CompareState
*s
,
160 uint32_t vnet_hdr_len
,
161 bool notify_remote_frame
,
164 static bool packet_matches_str(const char *str
,
168 if (packet_len
!= strlen(str
)) {
172 return !memcmp(str
, buf
, packet_len
);
175 static void notify_remote_frame(CompareState
*s
)
177 char msg
[] = "DO_CHECKPOINT";
180 ret
= compare_chr_send(s
, (uint8_t *)msg
, strlen(msg
), 0, true, false);
182 error_report("Notify Xen COLO-frame failed");
186 static void colo_compare_inconsistency_notify(CompareState
*s
)
189 notify_remote_frame(s
);
191 notifier_list_notify(&colo_compare_notifiers
,
192 migrate_get_current());
196 /* Use restricted to colo_insert_packet() */
197 static gint
seq_sorter(Packet
*a
, Packet
*b
, gpointer data
)
199 return b
->tcp_seq
- a
->tcp_seq
;
202 static void fill_pkt_tcp_info(void *data
, uint32_t *max_ack
)
205 struct tcp_hdr
*tcphd
;
207 tcphd
= (struct tcp_hdr
*)pkt
->transport_header
;
209 pkt
->tcp_seq
= ntohl(tcphd
->th_seq
);
210 pkt
->tcp_ack
= ntohl(tcphd
->th_ack
);
211 /* Need to consider ACK will bigger than uint32_t MAX */
212 *max_ack
= pkt
->tcp_ack
- *max_ack
> 0 ? pkt
->tcp_ack
: *max_ack
;
213 pkt
->header_size
= pkt
->transport_header
- (uint8_t *)pkt
->data
214 + (tcphd
->th_off
<< 2);
215 pkt
->payload_size
= pkt
->size
- pkt
->header_size
;
216 pkt
->seq_end
= pkt
->tcp_seq
+ pkt
->payload_size
;
217 pkt
->flags
= tcphd
->th_flags
;
221 * Return 1 on success, if return 0 means the
222 * packet will be dropped
224 static int colo_insert_packet(GQueue
*queue
, Packet
*pkt
, uint32_t *max_ack
)
226 if (g_queue_get_length(queue
) <= max_queue_size
) {
227 if (pkt
->ip
->ip_p
== IPPROTO_TCP
) {
228 fill_pkt_tcp_info(pkt
, max_ack
);
229 g_queue_insert_sorted(queue
,
231 (GCompareDataFunc
)seq_sorter
,
234 g_queue_push_tail(queue
, pkt
);
242 * Return 0 on success, if return -1 means the pkt
243 * is unsupported(arp and ipv6) and will be sent later
245 static int packet_enqueue(CompareState
*s
, int mode
, Connection
**con
)
252 if (mode
== PRIMARY_IN
) {
253 pkt
= packet_new(s
->pri_rs
.buf
,
254 s
->pri_rs
.packet_len
,
255 s
->pri_rs
.vnet_hdr_len
);
257 pkt
= packet_new(s
->sec_rs
.buf
,
258 s
->sec_rs
.packet_len
,
259 s
->sec_rs
.vnet_hdr_len
);
262 if (parse_packet_early(pkt
)) {
263 packet_destroy(pkt
, NULL
);
267 fill_connection_key(pkt
, &key
, false);
269 conn
= connection_get(s
->connection_track_table
,
273 if (!conn
->processing
) {
274 g_queue_push_tail(&s
->conn_list
, conn
);
275 conn
->processing
= true;
278 if (mode
== PRIMARY_IN
) {
279 ret
= colo_insert_packet(&conn
->primary_list
, pkt
, &conn
->pack
);
281 ret
= colo_insert_packet(&conn
->secondary_list
, pkt
, &conn
->sack
);
285 trace_colo_compare_drop_packet(colo_mode
[mode
],
286 "queue size too big, drop packet");
287 packet_destroy(pkt
, NULL
);
296 static inline bool after(uint32_t seq1
, uint32_t seq2
)
298 return (int32_t)(seq1
- seq2
) > 0;
301 static void colo_release_primary_pkt(CompareState
*s
, Packet
*pkt
)
304 ret
= compare_chr_send(s
,
311 error_report("colo send primary packet failed");
313 trace_colo_compare_main("packet same and release packet");
314 packet_destroy_partial(pkt
, NULL
);
318 * The IP packets sent by primary and secondary
319 * will be compared in here
320 * TODO support ip fragment, Out-Of-Order
321 * return: 0 means packet same
322 * > 0 || < 0 means packet different
324 static int colo_compare_packet_payload(Packet
*ppkt
,
331 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_IP_INFO
)) {
332 char pri_ip_src
[20], pri_ip_dst
[20], sec_ip_src
[20], sec_ip_dst
[20];
334 strcpy(pri_ip_src
, inet_ntoa(ppkt
->ip
->ip_src
));
335 strcpy(pri_ip_dst
, inet_ntoa(ppkt
->ip
->ip_dst
));
336 strcpy(sec_ip_src
, inet_ntoa(spkt
->ip
->ip_src
));
337 strcpy(sec_ip_dst
, inet_ntoa(spkt
->ip
->ip_dst
));
339 trace_colo_compare_ip_info(ppkt
->size
, pri_ip_src
,
340 pri_ip_dst
, spkt
->size
,
341 sec_ip_src
, sec_ip_dst
);
344 return memcmp(ppkt
->data
+ poffset
, spkt
->data
+ soffset
, len
);
348 * return true means that the payload is consist and
349 * need to make the next comparison, false means do
352 static bool colo_mark_tcp_pkt(Packet
*ppkt
, Packet
*spkt
,
353 int8_t *mark
, uint32_t max_ack
)
357 if (ppkt
->tcp_seq
== spkt
->tcp_seq
&& ppkt
->seq_end
== spkt
->seq_end
) {
358 if (!colo_compare_packet_payload(ppkt
, spkt
,
359 ppkt
->header_size
, spkt
->header_size
,
360 ppkt
->payload_size
)) {
361 *mark
= COLO_COMPARE_FREE_SECONDARY
| COLO_COMPARE_FREE_PRIMARY
;
366 /* one part of secondary packet payload still need to be compared */
367 if (!after(ppkt
->seq_end
, spkt
->seq_end
)) {
368 if (!colo_compare_packet_payload(ppkt
, spkt
,
369 ppkt
->header_size
+ ppkt
->offset
,
370 spkt
->header_size
+ spkt
->offset
,
371 ppkt
->payload_size
- ppkt
->offset
)) {
372 if (!after(ppkt
->tcp_ack
, max_ack
)) {
373 *mark
= COLO_COMPARE_FREE_PRIMARY
;
374 spkt
->offset
+= ppkt
->payload_size
- ppkt
->offset
;
377 /* secondary guest hasn't ack the data, don't send
384 /* primary packet is longer than secondary packet, compare
385 * the same part and mark the primary packet offset
387 if (!colo_compare_packet_payload(ppkt
, spkt
,
388 ppkt
->header_size
+ ppkt
->offset
,
389 spkt
->header_size
+ spkt
->offset
,
390 spkt
->payload_size
- spkt
->offset
)) {
391 *mark
= COLO_COMPARE_FREE_SECONDARY
;
392 ppkt
->offset
+= spkt
->payload_size
- spkt
->offset
;
400 static void colo_compare_tcp(CompareState
*s
, Connection
*conn
)
402 Packet
*ppkt
= NULL
, *spkt
= NULL
;
406 * If ppkt and spkt have the same payload, but ppkt's ACK
407 * is greater than spkt's ACK, in this case we can not
408 * send the ppkt because it will cause the secondary guest
409 * to miss sending some data in the next. Therefore, we
410 * record the maximum ACK in the current queue at both
411 * primary side and secondary side. Only when the ack is
412 * less than the smaller of the two maximum ack, then we
413 * can ensure that the packet's payload is acknowledged by
414 * primary and secondary.
416 uint32_t min_ack
= conn
->pack
- conn
->sack
> 0 ?
417 conn
->sack
: conn
->pack
;
420 if (g_queue_is_empty(&conn
->primary_list
)) {
423 ppkt
= g_queue_pop_tail(&conn
->primary_list
);
425 if (g_queue_is_empty(&conn
->secondary_list
)) {
426 g_queue_push_tail(&conn
->primary_list
, ppkt
);
429 spkt
= g_queue_pop_tail(&conn
->secondary_list
);
431 if (ppkt
->tcp_seq
== ppkt
->seq_end
) {
432 colo_release_primary_pkt(s
, ppkt
);
436 if (ppkt
&& conn
->compare_seq
&& !after(ppkt
->seq_end
, conn
->compare_seq
)) {
437 trace_colo_compare_main("pri: this packet has compared");
438 colo_release_primary_pkt(s
, ppkt
);
442 if (spkt
->tcp_seq
== spkt
->seq_end
) {
443 packet_destroy(spkt
, NULL
);
450 if (conn
->compare_seq
&& !after(spkt
->seq_end
, conn
->compare_seq
)) {
451 trace_colo_compare_main("sec: this packet has compared");
452 packet_destroy(spkt
, NULL
);
460 g_queue_push_tail(&conn
->secondary_list
, spkt
);
465 if (colo_mark_tcp_pkt(ppkt
, spkt
, &mark
, min_ack
)) {
466 trace_colo_compare_tcp_info("pri",
467 ppkt
->tcp_seq
, ppkt
->tcp_ack
,
468 ppkt
->header_size
, ppkt
->payload_size
,
469 ppkt
->offset
, ppkt
->flags
);
471 trace_colo_compare_tcp_info("sec",
472 spkt
->tcp_seq
, spkt
->tcp_ack
,
473 spkt
->header_size
, spkt
->payload_size
,
474 spkt
->offset
, spkt
->flags
);
476 if (mark
== COLO_COMPARE_FREE_PRIMARY
) {
477 conn
->compare_seq
= ppkt
->seq_end
;
478 colo_release_primary_pkt(s
, ppkt
);
479 g_queue_push_tail(&conn
->secondary_list
, spkt
);
481 } else if (mark
== COLO_COMPARE_FREE_SECONDARY
) {
482 conn
->compare_seq
= spkt
->seq_end
;
483 packet_destroy(spkt
, NULL
);
485 } else if (mark
== (COLO_COMPARE_FREE_PRIMARY
| COLO_COMPARE_FREE_SECONDARY
)) {
486 conn
->compare_seq
= ppkt
->seq_end
;
487 colo_release_primary_pkt(s
, ppkt
);
488 packet_destroy(spkt
, NULL
);
492 g_queue_push_tail(&conn
->primary_list
, ppkt
);
493 g_queue_push_tail(&conn
->secondary_list
, spkt
);
495 #ifdef DEBUG_COLO_PACKETS
496 qemu_hexdump(stderr
, "colo-compare ppkt", ppkt
->data
, ppkt
->size
);
497 qemu_hexdump(stderr
, "colo-compare spkt", spkt
->data
, spkt
->size
);
500 colo_compare_inconsistency_notify(s
);
506 * Called from the compare thread on the primary
507 * for compare udp packet
509 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
511 uint16_t network_header_length
= ppkt
->ip
->ip_hl
<< 2;
512 uint16_t offset
= network_header_length
+ ETH_HLEN
+ ppkt
->vnet_hdr_len
;
514 trace_colo_compare_main("compare udp");
517 * Because of ppkt and spkt are both in the same connection,
518 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
519 * same with spkt. In addition, IP header's Identification is a random
520 * field, we can handle it in IP fragmentation function later.
521 * COLO just concern the response net packet payload from primary guest
522 * and secondary guest are same or not, So we ignored all IP header include
523 * other field like TOS,TTL,IP Checksum. we only need to compare
524 * the ip payload here.
526 if (ppkt
->size
!= spkt
->size
) {
527 trace_colo_compare_main("UDP: payload size of packets are different");
530 if (colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
531 ppkt
->size
- offset
)) {
532 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
533 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
534 #ifdef DEBUG_COLO_PACKETS
535 qemu_hexdump(stderr
, "colo-compare pri pkt", ppkt
->data
, ppkt
->size
);
536 qemu_hexdump(stderr
, "colo-compare sec pkt", spkt
->data
, spkt
->size
);
545 * Called from the compare thread on the primary
546 * for compare icmp packet
548 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
550 uint16_t network_header_length
= ppkt
->ip
->ip_hl
<< 2;
551 uint16_t offset
= network_header_length
+ ETH_HLEN
+ ppkt
->vnet_hdr_len
;
553 trace_colo_compare_main("compare icmp");
556 * Because of ppkt and spkt are both in the same connection,
557 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
558 * same with spkt. In addition, IP header's Identification is a random
559 * field, we can handle it in IP fragmentation function later.
560 * COLO just concern the response net packet payload from primary guest
561 * and secondary guest are same or not, So we ignored all IP header include
562 * other field like TOS,TTL,IP Checksum. we only need to compare
563 * the ip payload here.
565 if (ppkt
->size
!= spkt
->size
) {
566 trace_colo_compare_main("ICMP: payload size of packets are different");
569 if (colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
570 ppkt
->size
- offset
)) {
571 trace_colo_compare_icmp_miscompare("primary pkt size",
573 trace_colo_compare_icmp_miscompare("Secondary pkt size",
575 #ifdef DEBUG_COLO_PACKETS
576 qemu_hexdump(stderr
, "colo-compare pri pkt", ppkt
->data
, ppkt
->size
);
577 qemu_hexdump(stderr
, "colo-compare sec pkt", spkt
->data
, spkt
->size
);
586 * Called from the compare thread on the primary
587 * for compare other packet
589 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
591 uint16_t offset
= ppkt
->vnet_hdr_len
;
593 trace_colo_compare_main("compare other");
594 if (ppkt
->size
!= spkt
->size
) {
595 trace_colo_compare_main("Other: payload size of packets are different");
598 return colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
599 ppkt
->size
- offset
);
602 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
604 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
606 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
607 trace_colo_old_packet_check_found(pkt
->creation_ms
);
614 void colo_compare_register_notifier(Notifier
*notify
)
616 notifier_list_add(&colo_compare_notifiers
, notify
);
619 void colo_compare_unregister_notifier(Notifier
*notify
)
621 notifier_remove(notify
);
624 static int colo_old_packet_check_one_conn(Connection
*conn
,
627 if (!g_queue_is_empty(&conn
->primary_list
)) {
628 if (g_queue_find_custom(&conn
->primary_list
,
630 (GCompareFunc
)colo_old_packet_check_one
))
634 if (!g_queue_is_empty(&conn
->secondary_list
)) {
635 if (g_queue_find_custom(&conn
->secondary_list
,
637 (GCompareFunc
)colo_old_packet_check_one
))
644 /* Do checkpoint will flush old packet */
645 colo_compare_inconsistency_notify(s
);
650 * Look for old packets that the secondary hasn't matched,
651 * if we have some then we have to checkpoint to wake
654 static void colo_old_packet_check(void *opaque
)
656 CompareState
*s
= opaque
;
659 * If we find one old packet, stop finding job and notify
660 * COLO frame do checkpoint.
662 g_queue_find_custom(&s
->conn_list
, s
,
663 (GCompareFunc
)colo_old_packet_check_one_conn
);
666 static void colo_compare_packet(CompareState
*s
, Connection
*conn
,
667 int (*HandlePacket
)(Packet
*spkt
,
671 GList
*result
= NULL
;
673 while (!g_queue_is_empty(&conn
->primary_list
) &&
674 !g_queue_is_empty(&conn
->secondary_list
)) {
675 pkt
= g_queue_pop_tail(&conn
->primary_list
);
676 result
= g_queue_find_custom(&conn
->secondary_list
,
677 pkt
, (GCompareFunc
)HandlePacket
);
680 colo_release_primary_pkt(s
, pkt
);
681 packet_destroy(result
->data
, NULL
);
682 g_queue_delete_link(&conn
->secondary_list
, result
);
685 * If one packet arrive late, the secondary_list or
686 * primary_list will be empty, so we can't compare it
687 * until next comparison. If the packets in the list are
688 * timeout, it will trigger a checkpoint request.
690 trace_colo_compare_main("packet different");
691 g_queue_push_tail(&conn
->primary_list
, pkt
);
693 colo_compare_inconsistency_notify(s
);
700 * Called from the compare thread on the primary
701 * for compare packet with secondary list of the
702 * specified connection when a new packet was
705 static void colo_compare_connection(void *opaque
, void *user_data
)
707 CompareState
*s
= user_data
;
708 Connection
*conn
= opaque
;
710 switch (conn
->ip_proto
) {
712 colo_compare_tcp(s
, conn
);
715 colo_compare_packet(s
, conn
, colo_packet_compare_udp
);
718 colo_compare_packet(s
, conn
, colo_packet_compare_icmp
);
721 colo_compare_packet(s
, conn
, colo_packet_compare_other
);
726 static void coroutine_fn
_compare_chr_send(void *opaque
)
728 SendCo
*sendco
= opaque
;
729 CompareState
*s
= sendco
->s
;
732 while (!g_queue_is_empty(&sendco
->send_list
)) {
733 SendEntry
*entry
= g_queue_pop_tail(&sendco
->send_list
);
734 uint32_t len
= htonl(entry
->size
);
736 ret
= qemu_chr_fe_write_all(sendco
->chr
, (uint8_t *)&len
, sizeof(len
));
738 if (ret
!= sizeof(len
)) {
740 g_slice_free(SendEntry
, entry
);
744 if (!sendco
->notify_remote_frame
&& s
->vnet_hdr
) {
746 * We send vnet header len make other module(like filter-redirector)
747 * know how to parse net packet correctly.
749 len
= htonl(entry
->vnet_hdr_len
);
751 ret
= qemu_chr_fe_write_all(sendco
->chr
,
755 if (ret
!= sizeof(len
)) {
757 g_slice_free(SendEntry
, entry
);
762 ret
= qemu_chr_fe_write_all(sendco
->chr
,
763 (uint8_t *)entry
->buf
,
766 if (ret
!= entry
->size
) {
768 g_slice_free(SendEntry
, entry
);
773 g_slice_free(SendEntry
, entry
);
780 while (!g_queue_is_empty(&sendco
->send_list
)) {
781 SendEntry
*entry
= g_queue_pop_tail(&sendco
->send_list
);
783 g_slice_free(SendEntry
, entry
);
785 sendco
->ret
= ret
< 0 ? ret
: -EIO
;
792 static int compare_chr_send(CompareState
*s
,
795 uint32_t vnet_hdr_len
,
796 bool notify_remote_frame
,
802 if (notify_remote_frame
) {
803 sendco
= &s
->notify_sendco
;
805 sendco
= &s
->out_sendco
;
812 entry
= g_slice_new(SendEntry
);
814 entry
->vnet_hdr_len
= vnet_hdr_len
;
818 entry
->buf
= g_malloc(size
);
819 memcpy(entry
->buf
, buf
, size
);
821 g_queue_push_tail(&sendco
->send_list
, entry
);
824 sendco
->co
= qemu_coroutine_create(_compare_chr_send
, sendco
);
825 sendco
->done
= false;
826 qemu_coroutine_enter(sendco
->co
);
828 /* report early errors */
837 static int compare_chr_can_read(void *opaque
)
839 return COMPARE_READ_LEN_MAX
;
843 * Called from the main thread on the primary for packets
844 * arriving over the socket from the primary.
846 static void compare_pri_chr_in(void *opaque
, const uint8_t *buf
, int size
)
848 CompareState
*s
= COLO_COMPARE(opaque
);
851 ret
= net_fill_rstate(&s
->pri_rs
, buf
, size
);
853 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, NULL
, NULL
, NULL
, NULL
,
855 error_report("colo-compare primary_in error");
860 * Called from the main thread on the primary for packets
861 * arriving over the socket from the secondary.
863 static void compare_sec_chr_in(void *opaque
, const uint8_t *buf
, int size
)
865 CompareState
*s
= COLO_COMPARE(opaque
);
868 ret
= net_fill_rstate(&s
->sec_rs
, buf
, size
);
870 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, NULL
, NULL
, NULL
, NULL
,
872 error_report("colo-compare secondary_in error");
876 static void compare_notify_chr(void *opaque
, const uint8_t *buf
, int size
)
878 CompareState
*s
= COLO_COMPARE(opaque
);
881 ret
= net_fill_rstate(&s
->notify_rs
, buf
, size
);
883 qemu_chr_fe_set_handlers(&s
->chr_notify_dev
, NULL
, NULL
, NULL
, NULL
,
885 error_report("colo-compare notify_dev error");
890 * Check old packet regularly so it can watch for any packets
891 * that the secondary hasn't produced equivalents of.
893 static void check_old_packet_regular(void *opaque
)
895 CompareState
*s
= opaque
;
897 /* if have old packet we will notify checkpoint */
898 colo_old_packet_check(s
);
899 timer_mod(s
->packet_check_timer
, qemu_clock_get_ms(QEMU_CLOCK_HOST
) +
900 s
->expired_scan_cycle
);
903 /* Public API, Used for COLO frame to notify compare event */
904 void colo_notify_compares_event(void *opaque
, int event
, Error
**errp
)
907 qemu_mutex_lock(&colo_compare_mutex
);
909 if (!colo_compare_active
) {
910 qemu_mutex_unlock(&colo_compare_mutex
);
914 qemu_mutex_lock(&event_mtx
);
915 QTAILQ_FOREACH(s
, &net_compares
, next
) {
917 qemu_bh_schedule(s
->event_bh
);
918 event_unhandled_count
++;
920 /* Wait all compare threads to finish handling this event */
921 while (event_unhandled_count
> 0) {
922 qemu_cond_wait(&event_complete_cond
, &event_mtx
);
925 qemu_mutex_unlock(&event_mtx
);
926 qemu_mutex_unlock(&colo_compare_mutex
);
929 static void colo_compare_timer_init(CompareState
*s
)
931 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
933 s
->packet_check_timer
= aio_timer_new(ctx
, QEMU_CLOCK_HOST
,
934 SCALE_MS
, check_old_packet_regular
,
936 timer_mod(s
->packet_check_timer
, qemu_clock_get_ms(QEMU_CLOCK_HOST
) +
937 s
->expired_scan_cycle
);
940 static void colo_compare_timer_del(CompareState
*s
)
942 if (s
->packet_check_timer
) {
943 timer_free(s
->packet_check_timer
);
944 s
->packet_check_timer
= NULL
;
948 static void colo_flush_packets(void *opaque
, void *user_data
);
950 static void colo_compare_handle_event(void *opaque
)
952 CompareState
*s
= opaque
;
955 case COLO_EVENT_CHECKPOINT
:
956 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
958 case COLO_EVENT_FAILOVER
:
964 qemu_mutex_lock(&event_mtx
);
965 assert(event_unhandled_count
> 0);
966 event_unhandled_count
--;
967 qemu_cond_broadcast(&event_complete_cond
);
968 qemu_mutex_unlock(&event_mtx
);
971 static void colo_compare_iothread(CompareState
*s
)
973 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
974 object_ref(OBJECT(s
->iothread
));
975 s
->worker_context
= iothread_get_g_main_context(s
->iothread
);
977 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
978 compare_pri_chr_in
, NULL
, NULL
,
979 s
, s
->worker_context
, true);
980 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
981 compare_sec_chr_in
, NULL
, NULL
,
982 s
, s
->worker_context
, true);
984 qemu_chr_fe_set_handlers(&s
->chr_notify_dev
, compare_chr_can_read
,
985 compare_notify_chr
, NULL
, NULL
,
986 s
, s
->worker_context
, true);
989 colo_compare_timer_init(s
);
990 s
->event_bh
= aio_bh_new(ctx
, colo_compare_handle_event
, s
);
993 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
995 CompareState
*s
= COLO_COMPARE(obj
);
997 return g_strdup(s
->pri_indev
);
1000 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
1002 CompareState
*s
= COLO_COMPARE(obj
);
1004 g_free(s
->pri_indev
);
1005 s
->pri_indev
= g_strdup(value
);
1008 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
1010 CompareState
*s
= COLO_COMPARE(obj
);
1012 return g_strdup(s
->sec_indev
);
1015 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
1017 CompareState
*s
= COLO_COMPARE(obj
);
1019 g_free(s
->sec_indev
);
1020 s
->sec_indev
= g_strdup(value
);
1023 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
1025 CompareState
*s
= COLO_COMPARE(obj
);
1027 return g_strdup(s
->outdev
);
1030 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
1032 CompareState
*s
= COLO_COMPARE(obj
);
1035 s
->outdev
= g_strdup(value
);
1038 static bool compare_get_vnet_hdr(Object
*obj
, Error
**errp
)
1040 CompareState
*s
= COLO_COMPARE(obj
);
1045 static void compare_set_vnet_hdr(Object
*obj
,
1049 CompareState
*s
= COLO_COMPARE(obj
);
1051 s
->vnet_hdr
= value
;
1054 static char *compare_get_notify_dev(Object
*obj
, Error
**errp
)
1056 CompareState
*s
= COLO_COMPARE(obj
);
1058 return g_strdup(s
->notify_dev
);
1061 static void compare_set_notify_dev(Object
*obj
, const char *value
, Error
**errp
)
1063 CompareState
*s
= COLO_COMPARE(obj
);
1065 g_free(s
->notify_dev
);
1066 s
->notify_dev
= g_strdup(value
);
1069 static void compare_get_timeout(Object
*obj
, Visitor
*v
,
1070 const char *name
, void *opaque
,
1073 CompareState
*s
= COLO_COMPARE(obj
);
1074 uint64_t value
= s
->compare_timeout
;
1076 visit_type_uint64(v
, name
, &value
, errp
);
1079 static void compare_set_timeout(Object
*obj
, Visitor
*v
,
1080 const char *name
, void *opaque
,
1083 CompareState
*s
= COLO_COMPARE(obj
);
1086 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
1090 error_setg(errp
, "Property '%s.%s' requires a positive value",
1091 object_get_typename(obj
), name
);
1094 s
->compare_timeout
= value
;
1097 static void compare_get_expired_scan_cycle(Object
*obj
, Visitor
*v
,
1098 const char *name
, void *opaque
,
1101 CompareState
*s
= COLO_COMPARE(obj
);
1102 uint32_t value
= s
->expired_scan_cycle
;
1104 visit_type_uint32(v
, name
, &value
, errp
);
1107 static void compare_set_expired_scan_cycle(Object
*obj
, Visitor
*v
,
1108 const char *name
, void *opaque
,
1111 CompareState
*s
= COLO_COMPARE(obj
);
1114 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
1118 error_setg(errp
, "Property '%s.%s' requires a positive value",
1119 object_get_typename(obj
), name
);
1122 s
->expired_scan_cycle
= value
;
1125 static void get_max_queue_size(Object
*obj
, Visitor
*v
,
1126 const char *name
, void *opaque
,
1129 uint32_t value
= max_queue_size
;
1131 visit_type_uint32(v
, name
, &value
, errp
);
1134 static void set_max_queue_size(Object
*obj
, Visitor
*v
,
1135 const char *name
, void *opaque
,
1138 Error
*local_err
= NULL
;
1141 visit_type_uint64(v
, name
, &value
, &local_err
);
1146 error_setg(&local_err
, "Property '%s.%s' requires a positive value",
1147 object_get_typename(obj
), name
);
1150 max_queue_size
= value
;
1153 error_propagate(errp
, local_err
);
1156 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
1158 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
1159 Connection
*conn
= NULL
;
1161 if (packet_enqueue(s
, PRIMARY_IN
, &conn
)) {
1162 trace_colo_compare_main("primary: unsupported packet in");
1166 pri_rs
->vnet_hdr_len
,
1170 /* compare packet in the specified connection */
1171 colo_compare_connection(conn
, s
);
1175 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
1177 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
1178 Connection
*conn
= NULL
;
1180 if (packet_enqueue(s
, SECONDARY_IN
, &conn
)) {
1181 trace_colo_compare_main("secondary: unsupported packet in");
1183 /* compare packet in the specified connection */
1184 colo_compare_connection(conn
, s
);
1188 static void compare_notify_rs_finalize(SocketReadState
*notify_rs
)
1190 CompareState
*s
= container_of(notify_rs
, CompareState
, notify_rs
);
1192 const char msg
[] = "COLO_COMPARE_GET_XEN_INIT";
1195 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT",
1197 notify_rs
->packet_len
)) {
1198 ret
= compare_chr_send(s
, (uint8_t *)msg
, strlen(msg
), 0, true, false);
1200 error_report("Notify Xen COLO-frame INIT failed");
1202 } else if (packet_matches_str("COLO_CHECKPOINT",
1204 notify_rs
->packet_len
)) {
1205 /* colo-compare do checkpoint, flush pri packet and remove sec packet */
1206 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
1208 error_report("COLO compare got unsupported instruction");
1213 * Return 0 is success.
1214 * Return 1 is failed.
1216 static int find_and_check_chardev(Chardev
**chr
,
1220 *chr
= qemu_chr_find(chr_name
);
1222 error_setg(errp
, "Device '%s' not found",
1227 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
1228 error_setg(errp
, "chardev \"%s\" is not reconnectable",
1233 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_GCONTEXT
)) {
1234 error_setg(errp
, "chardev \"%s\" cannot switch context",
1243 * Called from the main thread on the primary
1244 * to setup colo-compare.
1246 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
1248 CompareState
*s
= COLO_COMPARE(uc
);
1251 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
|| !s
->iothread
) {
1252 error_setg(errp
, "colo compare needs 'primary_in' ,"
1253 "'secondary_in','outdev','iothread' property set");
1255 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
1256 !strcmp(s
->sec_indev
, s
->outdev
) ||
1257 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
1258 error_setg(errp
, "'indev' and 'outdev' could not be same "
1259 "for compare module");
1263 if (!s
->compare_timeout
) {
1264 /* Set default value to 3000 MS */
1265 s
->compare_timeout
= DEFAULT_TIME_OUT_MS
;
1268 if (!s
->expired_scan_cycle
) {
1269 /* Set default value to 1000 MS */
1270 s
->expired_scan_cycle
= REGULAR_PACKET_CHECK_MS
;
1273 if (!max_queue_size
) {
1274 /* Set default queue size to 1024 */
1275 max_queue_size
= MAX_QUEUE_SIZE
;
1278 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
1279 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
1283 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
1284 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
1288 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
1289 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
1293 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
, s
->vnet_hdr
);
1294 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
, s
->vnet_hdr
);
1296 /* Try to enable remote notify chardev, currently just for Xen COLO */
1297 if (s
->notify_dev
) {
1298 if (find_and_check_chardev(&chr
, s
->notify_dev
, errp
) ||
1299 !qemu_chr_fe_init(&s
->chr_notify_dev
, chr
, errp
)) {
1303 net_socket_rs_init(&s
->notify_rs
, compare_notify_rs_finalize
,
1307 s
->out_sendco
.s
= s
;
1308 s
->out_sendco
.chr
= &s
->chr_out
;
1309 s
->out_sendco
.notify_remote_frame
= false;
1310 s
->out_sendco
.done
= true;
1311 g_queue_init(&s
->out_sendco
.send_list
);
1313 if (s
->notify_dev
) {
1314 s
->notify_sendco
.s
= s
;
1315 s
->notify_sendco
.chr
= &s
->chr_notify_dev
;
1316 s
->notify_sendco
.notify_remote_frame
= true;
1317 s
->notify_sendco
.done
= true;
1318 g_queue_init(&s
->notify_sendco
.send_list
);
1321 g_queue_init(&s
->conn_list
);
1323 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
1324 connection_key_equal
,
1326 connection_destroy
);
1328 colo_compare_iothread(s
);
1330 qemu_mutex_lock(&colo_compare_mutex
);
1331 if (!colo_compare_active
) {
1332 qemu_mutex_init(&event_mtx
);
1333 qemu_cond_init(&event_complete_cond
);
1334 colo_compare_active
= true;
1336 QTAILQ_INSERT_TAIL(&net_compares
, s
, next
);
1337 qemu_mutex_unlock(&colo_compare_mutex
);
1342 static void colo_flush_packets(void *opaque
, void *user_data
)
1344 CompareState
*s
= user_data
;
1345 Connection
*conn
= opaque
;
1348 while (!g_queue_is_empty(&conn
->primary_list
)) {
1349 pkt
= g_queue_pop_tail(&conn
->primary_list
);
1356 packet_destroy_partial(pkt
, NULL
);
1358 while (!g_queue_is_empty(&conn
->secondary_list
)) {
1359 pkt
= g_queue_pop_tail(&conn
->secondary_list
);
1360 packet_destroy(pkt
, NULL
);
1364 static void colo_compare_class_init(ObjectClass
*oc
, void *data
)
1366 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
1368 ucc
->complete
= colo_compare_complete
;
1371 static void colo_compare_init(Object
*obj
)
1373 CompareState
*s
= COLO_COMPARE(obj
);
1375 object_property_add_str(obj
, "primary_in",
1376 compare_get_pri_indev
, compare_set_pri_indev
);
1377 object_property_add_str(obj
, "secondary_in",
1378 compare_get_sec_indev
, compare_set_sec_indev
);
1379 object_property_add_str(obj
, "outdev",
1380 compare_get_outdev
, compare_set_outdev
);
1381 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
1382 (Object
**)&s
->iothread
,
1383 object_property_allow_set_link
,
1384 OBJ_PROP_LINK_STRONG
);
1385 /* This parameter just for Xen COLO */
1386 object_property_add_str(obj
, "notify_dev",
1387 compare_get_notify_dev
, compare_set_notify_dev
);
1389 object_property_add(obj
, "compare_timeout", "uint64",
1390 compare_get_timeout
,
1391 compare_set_timeout
, NULL
, NULL
);
1393 object_property_add(obj
, "expired_scan_cycle", "uint32",
1394 compare_get_expired_scan_cycle
,
1395 compare_set_expired_scan_cycle
, NULL
, NULL
);
1397 object_property_add(obj
, "max_queue_size", "uint32",
1399 set_max_queue_size
, NULL
, NULL
);
1401 s
->vnet_hdr
= false;
1402 object_property_add_bool(obj
, "vnet_hdr_support", compare_get_vnet_hdr
,
1403 compare_set_vnet_hdr
);
1406 void colo_compare_cleanup(void)
1408 CompareState
*tmp
= NULL
;
1409 CompareState
*n
= NULL
;
1411 QTAILQ_FOREACH_SAFE(tmp
, &net_compares
, next
, n
) {
1412 object_unparent(OBJECT(tmp
));
1416 static void colo_compare_finalize(Object
*obj
)
1418 CompareState
*s
= COLO_COMPARE(obj
);
1419 CompareState
*tmp
= NULL
;
1421 qemu_mutex_lock(&colo_compare_mutex
);
1422 QTAILQ_FOREACH(tmp
, &net_compares
, next
) {
1424 QTAILQ_REMOVE(&net_compares
, s
, next
);
1428 if (QTAILQ_EMPTY(&net_compares
)) {
1429 colo_compare_active
= false;
1430 qemu_mutex_destroy(&event_mtx
);
1431 qemu_cond_destroy(&event_complete_cond
);
1433 qemu_mutex_unlock(&colo_compare_mutex
);
1435 qemu_chr_fe_deinit(&s
->chr_pri_in
, false);
1436 qemu_chr_fe_deinit(&s
->chr_sec_in
, false);
1437 qemu_chr_fe_deinit(&s
->chr_out
, false);
1438 if (s
->notify_dev
) {
1439 qemu_chr_fe_deinit(&s
->chr_notify_dev
, false);
1442 colo_compare_timer_del(s
);
1444 qemu_bh_delete(s
->event_bh
);
1446 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
1447 aio_context_acquire(ctx
);
1448 AIO_WAIT_WHILE(ctx
, !s
->out_sendco
.done
);
1449 if (s
->notify_dev
) {
1450 AIO_WAIT_WHILE(ctx
, !s
->notify_sendco
.done
);
1452 aio_context_release(ctx
);
1454 /* Release all unhandled packets after compare thead exited */
1455 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
1456 AIO_WAIT_WHILE(NULL
, !s
->out_sendco
.done
);
1458 g_queue_clear(&s
->conn_list
);
1459 g_queue_clear(&s
->out_sendco
.send_list
);
1460 if (s
->notify_dev
) {
1461 g_queue_clear(&s
->notify_sendco
.send_list
);
1464 if (s
->connection_track_table
) {
1465 g_hash_table_destroy(s
->connection_track_table
);
1468 object_unref(OBJECT(s
->iothread
));
1470 g_free(s
->pri_indev
);
1471 g_free(s
->sec_indev
);
1473 g_free(s
->notify_dev
);
1476 static void __attribute__((__constructor__
)) colo_compare_init_globals(void)
1478 colo_compare_active
= false;
1479 qemu_mutex_init(&colo_compare_mutex
);
1482 static const TypeInfo colo_compare_info
= {
1483 .name
= TYPE_COLO_COMPARE
,
1484 .parent
= TYPE_OBJECT
,
1485 .instance_size
= sizeof(CompareState
),
1486 .instance_init
= colo_compare_init
,
1487 .instance_finalize
= colo_compare_finalize
,
1488 .class_size
= sizeof(CompareClass
),
1489 .class_init
= colo_compare_class_init
,
1490 .interfaces
= (InterfaceInfo
[]) {
1491 { TYPE_USER_CREATABLE
},
1496 static void register_types(void)
1498 type_register_static(&colo_compare_info
);
1501 type_init(register_types
);