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-common.h"
17 #include "qemu/error-report.h"
19 #include "qapi/error.h"
22 #include "qom/object_interfaces.h"
24 #include "qom/object.h"
25 #include "net/queue.h"
26 #include "chardev/char-fe.h"
27 #include "qemu/sockets.h"
29 #include "sysemu/iothread.h"
30 #include "net/colo-compare.h"
31 #include "migration/colo.h"
32 #include "migration/migration.h"
35 #include "block/aio-wait.h"
36 #include "qemu/coroutine.h"
38 #define TYPE_COLO_COMPARE "colo-compare"
39 #define COLO_COMPARE(obj) \
40 OBJECT_CHECK(CompareState, (obj), TYPE_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 3000
55 #define DEFAULT_TIME_OUT_MS 3000
57 static QemuMutex event_mtx
;
58 static QemuCond event_complete_cond
;
59 static int event_unhandled_count
;
64 * +---------------+ +---------------+ +---------------+
65 * | conn list + - > conn + ------- > conn + -- > ......
66 * +---------------+ +---------------+ +---------------+
68 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
69 * |primary | |secondary |primary | |secondary
70 * |packet | |packet + |packet | |packet +
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 * +--------+ +--------+ +--------+ +--------+
84 typedef struct SendCo
{
86 struct CompareState
*s
;
89 bool notify_remote_frame
;
94 typedef struct SendEntry
{
96 uint32_t vnet_hdr_len
;
100 typedef struct CompareState
{
107 CharBackend chr_pri_in
;
108 CharBackend chr_sec_in
;
110 CharBackend chr_notify_dev
;
111 SocketReadState pri_rs
;
112 SocketReadState sec_rs
;
113 SocketReadState notify_rs
;
115 SendCo notify_sendco
;
117 uint32_t compare_timeout
;
118 uint32_t expired_scan_cycle
;
121 * Record the connection that through the NIC
122 * Element type: Connection
125 /* Record the connection without repetition */
126 GHashTable
*connection_track_table
;
129 GMainContext
*worker_context
;
130 QEMUTimer
*packet_check_timer
;
133 enum colo_event event
;
135 QTAILQ_ENTRY(CompareState
) next
;
138 typedef struct CompareClass
{
139 ObjectClass parent_class
;
148 static int compare_chr_send(CompareState
*s
,
151 uint32_t vnet_hdr_len
,
152 bool notify_remote_frame
,
155 static bool packet_matches_str(const char *str
,
159 if (packet_len
!= strlen(str
)) {
163 return !memcmp(str
, buf
, strlen(str
));
166 static void notify_remote_frame(CompareState
*s
)
168 char msg
[] = "DO_CHECKPOINT";
171 ret
= compare_chr_send(s
, (uint8_t *)msg
, strlen(msg
), 0, true, false);
173 error_report("Notify Xen COLO-frame failed");
177 static void colo_compare_inconsistency_notify(CompareState
*s
)
180 notify_remote_frame(s
);
182 notifier_list_notify(&colo_compare_notifiers
,
183 migrate_get_current());
187 static gint
seq_sorter(Packet
*a
, Packet
*b
, gpointer data
)
189 struct tcp_hdr
*atcp
, *btcp
;
191 atcp
= (struct tcp_hdr
*)(a
->transport_header
);
192 btcp
= (struct tcp_hdr
*)(b
->transport_header
);
193 return ntohl(atcp
->th_seq
) - ntohl(btcp
->th_seq
);
196 static void fill_pkt_tcp_info(void *data
, uint32_t *max_ack
)
199 struct tcp_hdr
*tcphd
;
201 tcphd
= (struct tcp_hdr
*)pkt
->transport_header
;
203 pkt
->tcp_seq
= ntohl(tcphd
->th_seq
);
204 pkt
->tcp_ack
= ntohl(tcphd
->th_ack
);
205 *max_ack
= *max_ack
> pkt
->tcp_ack
? *max_ack
: pkt
->tcp_ack
;
206 pkt
->header_size
= pkt
->transport_header
- (uint8_t *)pkt
->data
207 + (tcphd
->th_off
<< 2) - pkt
->vnet_hdr_len
;
208 pkt
->payload_size
= pkt
->size
- pkt
->header_size
;
209 pkt
->seq_end
= pkt
->tcp_seq
+ pkt
->payload_size
;
210 pkt
->flags
= tcphd
->th_flags
;
214 * Return 1 on success, if return 0 means the
215 * packet will be dropped
217 static int colo_insert_packet(GQueue
*queue
, Packet
*pkt
, uint32_t *max_ack
)
219 if (g_queue_get_length(queue
) <= MAX_QUEUE_SIZE
) {
220 if (pkt
->ip
->ip_p
== IPPROTO_TCP
) {
221 fill_pkt_tcp_info(pkt
, max_ack
);
222 g_queue_insert_sorted(queue
,
224 (GCompareDataFunc
)seq_sorter
,
227 g_queue_push_tail(queue
, pkt
);
235 * Return 0 on success, if return -1 means the pkt
236 * is unsupported(arp and ipv6) and will be sent later
238 static int packet_enqueue(CompareState
*s
, int mode
, Connection
**con
)
244 if (mode
== PRIMARY_IN
) {
245 pkt
= packet_new(s
->pri_rs
.buf
,
246 s
->pri_rs
.packet_len
,
247 s
->pri_rs
.vnet_hdr_len
);
249 pkt
= packet_new(s
->sec_rs
.buf
,
250 s
->sec_rs
.packet_len
,
251 s
->sec_rs
.vnet_hdr_len
);
254 if (parse_packet_early(pkt
)) {
255 packet_destroy(pkt
, NULL
);
259 fill_connection_key(pkt
, &key
);
261 conn
= connection_get(s
->connection_track_table
,
265 if (!conn
->processing
) {
266 g_queue_push_tail(&s
->conn_list
, conn
);
267 conn
->processing
= true;
270 if (mode
== PRIMARY_IN
) {
271 if (!colo_insert_packet(&conn
->primary_list
, pkt
, &conn
->pack
)) {
272 error_report("colo compare primary queue size too big,"
276 if (!colo_insert_packet(&conn
->secondary_list
, pkt
, &conn
->sack
)) {
277 error_report("colo compare secondary queue size too big,"
286 static inline bool after(uint32_t seq1
, uint32_t seq2
)
288 return (int32_t)(seq1
- seq2
) > 0;
291 static void colo_release_primary_pkt(CompareState
*s
, Packet
*pkt
)
294 ret
= compare_chr_send(s
,
301 error_report("colo send primary packet failed");
303 trace_colo_compare_main("packet same and release packet");
304 packet_destroy_partial(pkt
, NULL
);
308 * The IP packets sent by primary and secondary
309 * will be compared in here
310 * TODO support ip fragment, Out-Of-Order
311 * return: 0 means packet same
312 * > 0 || < 0 means packet different
314 static int colo_compare_packet_payload(Packet
*ppkt
,
321 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE
)) {
322 char pri_ip_src
[20], pri_ip_dst
[20], sec_ip_src
[20], sec_ip_dst
[20];
324 strcpy(pri_ip_src
, inet_ntoa(ppkt
->ip
->ip_src
));
325 strcpy(pri_ip_dst
, inet_ntoa(ppkt
->ip
->ip_dst
));
326 strcpy(sec_ip_src
, inet_ntoa(spkt
->ip
->ip_src
));
327 strcpy(sec_ip_dst
, inet_ntoa(spkt
->ip
->ip_dst
));
329 trace_colo_compare_ip_info(ppkt
->size
, pri_ip_src
,
330 pri_ip_dst
, spkt
->size
,
331 sec_ip_src
, sec_ip_dst
);
334 return memcmp(ppkt
->data
+ poffset
, spkt
->data
+ soffset
, len
);
338 * return true means that the payload is consist and
339 * need to make the next comparison, false means do
342 static bool colo_mark_tcp_pkt(Packet
*ppkt
, Packet
*spkt
,
343 int8_t *mark
, uint32_t max_ack
)
347 if (ppkt
->tcp_seq
== spkt
->tcp_seq
&& ppkt
->seq_end
== spkt
->seq_end
) {
348 if (!colo_compare_packet_payload(ppkt
, spkt
,
349 ppkt
->header_size
, spkt
->header_size
,
350 ppkt
->payload_size
)) {
351 *mark
= COLO_COMPARE_FREE_SECONDARY
| COLO_COMPARE_FREE_PRIMARY
;
356 /* one part of secondary packet payload still need to be compared */
357 if (!after(ppkt
->seq_end
, spkt
->seq_end
)) {
358 if (!colo_compare_packet_payload(ppkt
, spkt
,
359 ppkt
->header_size
+ ppkt
->offset
,
360 spkt
->header_size
+ spkt
->offset
,
361 ppkt
->payload_size
- ppkt
->offset
)) {
362 if (!after(ppkt
->tcp_ack
, max_ack
)) {
363 *mark
= COLO_COMPARE_FREE_PRIMARY
;
364 spkt
->offset
+= ppkt
->payload_size
- ppkt
->offset
;
367 /* secondary guest hasn't ack the data, don't send
374 /* primary packet is longer than secondary packet, compare
375 * the same part and mark the primary packet offset
377 if (!colo_compare_packet_payload(ppkt
, spkt
,
378 ppkt
->header_size
+ ppkt
->offset
,
379 spkt
->header_size
+ spkt
->offset
,
380 spkt
->payload_size
- spkt
->offset
)) {
381 *mark
= COLO_COMPARE_FREE_SECONDARY
;
382 ppkt
->offset
+= spkt
->payload_size
- spkt
->offset
;
390 static void colo_compare_tcp(CompareState
*s
, Connection
*conn
)
392 Packet
*ppkt
= NULL
, *spkt
= NULL
;
396 * If ppkt and spkt have the same payload, but ppkt's ACK
397 * is greater than spkt's ACK, in this case we can not
398 * send the ppkt because it will cause the secondary guest
399 * to miss sending some data in the next. Therefore, we
400 * record the maximum ACK in the current queue at both
401 * primary side and secondary side. Only when the ack is
402 * less than the smaller of the two maximum ack, then we
403 * can ensure that the packet's payload is acknowledged by
404 * primary and secondary.
406 uint32_t min_ack
= conn
->pack
> conn
->sack
? conn
->sack
: conn
->pack
;
409 if (g_queue_is_empty(&conn
->primary_list
)) {
412 ppkt
= g_queue_pop_head(&conn
->primary_list
);
414 if (g_queue_is_empty(&conn
->secondary_list
)) {
415 g_queue_push_head(&conn
->primary_list
, ppkt
);
418 spkt
= g_queue_pop_head(&conn
->secondary_list
);
420 if (ppkt
->tcp_seq
== ppkt
->seq_end
) {
421 colo_release_primary_pkt(s
, ppkt
);
425 if (ppkt
&& conn
->compare_seq
&& !after(ppkt
->seq_end
, conn
->compare_seq
)) {
426 trace_colo_compare_main("pri: this packet has compared");
427 colo_release_primary_pkt(s
, ppkt
);
431 if (spkt
->tcp_seq
== spkt
->seq_end
) {
432 packet_destroy(spkt
, NULL
);
439 if (conn
->compare_seq
&& !after(spkt
->seq_end
, conn
->compare_seq
)) {
440 trace_colo_compare_main("sec: this packet has compared");
441 packet_destroy(spkt
, NULL
);
449 g_queue_push_head(&conn
->secondary_list
, spkt
);
454 if (colo_mark_tcp_pkt(ppkt
, spkt
, &mark
, min_ack
)) {
455 trace_colo_compare_tcp_info("pri",
456 ppkt
->tcp_seq
, ppkt
->tcp_ack
,
457 ppkt
->header_size
, ppkt
->payload_size
,
458 ppkt
->offset
, ppkt
->flags
);
460 trace_colo_compare_tcp_info("sec",
461 spkt
->tcp_seq
, spkt
->tcp_ack
,
462 spkt
->header_size
, spkt
->payload_size
,
463 spkt
->offset
, spkt
->flags
);
465 if (mark
== COLO_COMPARE_FREE_PRIMARY
) {
466 conn
->compare_seq
= ppkt
->seq_end
;
467 colo_release_primary_pkt(s
, ppkt
);
468 g_queue_push_head(&conn
->secondary_list
, spkt
);
471 if (mark
== COLO_COMPARE_FREE_SECONDARY
) {
472 conn
->compare_seq
= spkt
->seq_end
;
473 packet_destroy(spkt
, NULL
);
476 if (mark
== (COLO_COMPARE_FREE_PRIMARY
| COLO_COMPARE_FREE_SECONDARY
)) {
477 conn
->compare_seq
= ppkt
->seq_end
;
478 colo_release_primary_pkt(s
, ppkt
);
479 packet_destroy(spkt
, NULL
);
483 g_queue_push_head(&conn
->primary_list
, ppkt
);
484 g_queue_push_head(&conn
->secondary_list
, spkt
);
486 qemu_hexdump((char *)ppkt
->data
, stderr
,
487 "colo-compare ppkt", ppkt
->size
);
488 qemu_hexdump((char *)spkt
->data
, stderr
,
489 "colo-compare spkt", spkt
->size
);
491 colo_compare_inconsistency_notify(s
);
497 * Called from the compare thread on the primary
498 * for compare udp packet
500 static int colo_packet_compare_udp(Packet
*spkt
, Packet
*ppkt
)
502 uint16_t network_header_length
= ppkt
->ip
->ip_hl
<< 2;
503 uint16_t offset
= network_header_length
+ ETH_HLEN
+ ppkt
->vnet_hdr_len
;
505 trace_colo_compare_main("compare udp");
508 * Because of ppkt and spkt are both in the same connection,
509 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
510 * same with spkt. In addition, IP header's Identification is a random
511 * field, we can handle it in IP fragmentation function later.
512 * COLO just concern the response net packet payload from primary guest
513 * and secondary guest are same or not, So we ignored all IP header include
514 * other field like TOS,TTL,IP Checksum. we only need to compare
515 * the ip payload here.
517 if (ppkt
->size
!= spkt
->size
) {
518 trace_colo_compare_main("UDP: payload size of packets are different");
521 if (colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
522 ppkt
->size
- offset
)) {
523 trace_colo_compare_udp_miscompare("primary pkt size", ppkt
->size
);
524 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt
->size
);
525 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE
)) {
526 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare pri pkt",
528 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare sec pkt",
538 * Called from the compare thread on the primary
539 * for compare icmp packet
541 static int colo_packet_compare_icmp(Packet
*spkt
, Packet
*ppkt
)
543 uint16_t network_header_length
= ppkt
->ip
->ip_hl
<< 2;
544 uint16_t offset
= network_header_length
+ ETH_HLEN
+ ppkt
->vnet_hdr_len
;
546 trace_colo_compare_main("compare icmp");
549 * Because of ppkt and spkt are both in the same connection,
550 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
551 * same with spkt. In addition, IP header's Identification is a random
552 * field, we can handle it in IP fragmentation function later.
553 * COLO just concern the response net packet payload from primary guest
554 * and secondary guest are same or not, So we ignored all IP header include
555 * other field like TOS,TTL,IP Checksum. we only need to compare
556 * the ip payload here.
558 if (ppkt
->size
!= spkt
->size
) {
559 trace_colo_compare_main("ICMP: payload size of packets are different");
562 if (colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
563 ppkt
->size
- offset
)) {
564 trace_colo_compare_icmp_miscompare("primary pkt size",
566 trace_colo_compare_icmp_miscompare("Secondary pkt size",
568 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE
)) {
569 qemu_hexdump((char *)ppkt
->data
, stderr
, "colo-compare pri pkt",
571 qemu_hexdump((char *)spkt
->data
, stderr
, "colo-compare sec pkt",
581 * Called from the compare thread on the primary
582 * for compare other packet
584 static int colo_packet_compare_other(Packet
*spkt
, Packet
*ppkt
)
586 uint16_t offset
= ppkt
->vnet_hdr_len
;
588 trace_colo_compare_main("compare other");
589 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE
)) {
590 char pri_ip_src
[20], pri_ip_dst
[20], sec_ip_src
[20], sec_ip_dst
[20];
592 strcpy(pri_ip_src
, inet_ntoa(ppkt
->ip
->ip_src
));
593 strcpy(pri_ip_dst
, inet_ntoa(ppkt
->ip
->ip_dst
));
594 strcpy(sec_ip_src
, inet_ntoa(spkt
->ip
->ip_src
));
595 strcpy(sec_ip_dst
, inet_ntoa(spkt
->ip
->ip_dst
));
597 trace_colo_compare_ip_info(ppkt
->size
, pri_ip_src
,
598 pri_ip_dst
, spkt
->size
,
599 sec_ip_src
, sec_ip_dst
);
602 if (ppkt
->size
!= spkt
->size
) {
603 trace_colo_compare_main("Other: payload size of packets are different");
606 return colo_compare_packet_payload(ppkt
, spkt
, offset
, offset
,
607 ppkt
->size
- offset
);
610 static int colo_old_packet_check_one(Packet
*pkt
, int64_t *check_time
)
612 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
614 if ((now
- pkt
->creation_ms
) > (*check_time
)) {
615 trace_colo_old_packet_check_found(pkt
->creation_ms
);
622 void colo_compare_register_notifier(Notifier
*notify
)
624 notifier_list_add(&colo_compare_notifiers
, notify
);
627 void colo_compare_unregister_notifier(Notifier
*notify
)
629 notifier_remove(notify
);
632 static int colo_old_packet_check_one_conn(Connection
*conn
,
635 GList
*result
= NULL
;
637 result
= g_queue_find_custom(&conn
->primary_list
,
639 (GCompareFunc
)colo_old_packet_check_one
);
642 /* Do checkpoint will flush old packet */
643 colo_compare_inconsistency_notify(s
);
651 * Look for old packets that the secondary hasn't matched,
652 * if we have some then we have to checkpoint to wake
655 static void colo_old_packet_check(void *opaque
)
657 CompareState
*s
= opaque
;
660 * If we find one old packet, stop finding job and notify
661 * COLO frame do checkpoint.
663 g_queue_find_custom(&s
->conn_list
, s
,
664 (GCompareFunc
)colo_old_packet_check_one_conn
);
667 static void colo_compare_packet(CompareState
*s
, Connection
*conn
,
668 int (*HandlePacket
)(Packet
*spkt
,
672 GList
*result
= NULL
;
674 while (!g_queue_is_empty(&conn
->primary_list
) &&
675 !g_queue_is_empty(&conn
->secondary_list
)) {
676 pkt
= g_queue_pop_head(&conn
->primary_list
);
677 result
= g_queue_find_custom(&conn
->secondary_list
,
678 pkt
, (GCompareFunc
)HandlePacket
);
681 colo_release_primary_pkt(s
, pkt
);
682 g_queue_remove(&conn
->secondary_list
, result
->data
);
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_head(&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_head(&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_VIRTUAL
) +
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
)
908 qemu_mutex_lock(&event_mtx
);
909 QTAILQ_FOREACH(s
, &net_compares
, next
) {
911 qemu_bh_schedule(s
->event_bh
);
912 event_unhandled_count
++;
914 /* Wait all compare threads to finish handling this event */
915 while (event_unhandled_count
> 0) {
916 qemu_cond_wait(&event_complete_cond
, &event_mtx
);
919 qemu_mutex_unlock(&event_mtx
);
922 static void colo_compare_timer_init(CompareState
*s
)
924 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
926 s
->packet_check_timer
= aio_timer_new(ctx
, QEMU_CLOCK_VIRTUAL
,
927 SCALE_MS
, check_old_packet_regular
,
929 timer_mod(s
->packet_check_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
930 s
->expired_scan_cycle
);
933 static void colo_compare_timer_del(CompareState
*s
)
935 if (s
->packet_check_timer
) {
936 timer_del(s
->packet_check_timer
);
937 timer_free(s
->packet_check_timer
);
938 s
->packet_check_timer
= NULL
;
942 static void colo_flush_packets(void *opaque
, void *user_data
);
944 static void colo_compare_handle_event(void *opaque
)
946 CompareState
*s
= opaque
;
949 case COLO_EVENT_CHECKPOINT
:
950 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
952 case COLO_EVENT_FAILOVER
:
958 qemu_mutex_lock(&event_mtx
);
959 assert(event_unhandled_count
> 0);
960 event_unhandled_count
--;
961 qemu_cond_broadcast(&event_complete_cond
);
962 qemu_mutex_unlock(&event_mtx
);
965 static void colo_compare_iothread(CompareState
*s
)
967 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
968 object_ref(OBJECT(s
->iothread
));
969 s
->worker_context
= iothread_get_g_main_context(s
->iothread
);
971 qemu_chr_fe_set_handlers(&s
->chr_pri_in
, compare_chr_can_read
,
972 compare_pri_chr_in
, NULL
, NULL
,
973 s
, s
->worker_context
, true);
974 qemu_chr_fe_set_handlers(&s
->chr_sec_in
, compare_chr_can_read
,
975 compare_sec_chr_in
, NULL
, NULL
,
976 s
, s
->worker_context
, true);
978 qemu_chr_fe_set_handlers(&s
->chr_notify_dev
, compare_chr_can_read
,
979 compare_notify_chr
, NULL
, NULL
,
980 s
, s
->worker_context
, true);
983 colo_compare_timer_init(s
);
984 s
->event_bh
= aio_bh_new(ctx
, colo_compare_handle_event
, s
);
987 static char *compare_get_pri_indev(Object
*obj
, Error
**errp
)
989 CompareState
*s
= COLO_COMPARE(obj
);
991 return g_strdup(s
->pri_indev
);
994 static void compare_set_pri_indev(Object
*obj
, const char *value
, Error
**errp
)
996 CompareState
*s
= COLO_COMPARE(obj
);
998 g_free(s
->pri_indev
);
999 s
->pri_indev
= g_strdup(value
);
1002 static char *compare_get_sec_indev(Object
*obj
, Error
**errp
)
1004 CompareState
*s
= COLO_COMPARE(obj
);
1006 return g_strdup(s
->sec_indev
);
1009 static void compare_set_sec_indev(Object
*obj
, const char *value
, Error
**errp
)
1011 CompareState
*s
= COLO_COMPARE(obj
);
1013 g_free(s
->sec_indev
);
1014 s
->sec_indev
= g_strdup(value
);
1017 static char *compare_get_outdev(Object
*obj
, Error
**errp
)
1019 CompareState
*s
= COLO_COMPARE(obj
);
1021 return g_strdup(s
->outdev
);
1024 static void compare_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
1026 CompareState
*s
= COLO_COMPARE(obj
);
1029 s
->outdev
= g_strdup(value
);
1032 static bool compare_get_vnet_hdr(Object
*obj
, Error
**errp
)
1034 CompareState
*s
= COLO_COMPARE(obj
);
1039 static void compare_set_vnet_hdr(Object
*obj
,
1043 CompareState
*s
= COLO_COMPARE(obj
);
1045 s
->vnet_hdr
= value
;
1048 static char *compare_get_notify_dev(Object
*obj
, Error
**errp
)
1050 CompareState
*s
= COLO_COMPARE(obj
);
1052 return g_strdup(s
->notify_dev
);
1055 static void compare_set_notify_dev(Object
*obj
, const char *value
, Error
**errp
)
1057 CompareState
*s
= COLO_COMPARE(obj
);
1059 g_free(s
->notify_dev
);
1060 s
->notify_dev
= g_strdup(value
);
1063 static void compare_get_timeout(Object
*obj
, Visitor
*v
,
1064 const char *name
, void *opaque
,
1067 CompareState
*s
= COLO_COMPARE(obj
);
1068 uint32_t value
= s
->compare_timeout
;
1070 visit_type_uint32(v
, name
, &value
, errp
);
1073 static void compare_set_timeout(Object
*obj
, Visitor
*v
,
1074 const char *name
, void *opaque
,
1077 CompareState
*s
= COLO_COMPARE(obj
);
1078 Error
*local_err
= NULL
;
1081 visit_type_uint32(v
, name
, &value
, &local_err
);
1086 error_setg(&local_err
, "Property '%s.%s' requires a positive value",
1087 object_get_typename(obj
), name
);
1090 s
->compare_timeout
= value
;
1093 error_propagate(errp
, local_err
);
1096 static void compare_get_expired_scan_cycle(Object
*obj
, Visitor
*v
,
1097 const char *name
, void *opaque
,
1100 CompareState
*s
= COLO_COMPARE(obj
);
1101 uint32_t value
= s
->expired_scan_cycle
;
1103 visit_type_uint32(v
, name
, &value
, errp
);
1106 static void compare_set_expired_scan_cycle(Object
*obj
, Visitor
*v
,
1107 const char *name
, void *opaque
,
1110 CompareState
*s
= COLO_COMPARE(obj
);
1111 Error
*local_err
= NULL
;
1114 visit_type_uint32(v
, name
, &value
, &local_err
);
1119 error_setg(&local_err
, "Property '%s.%s' requires a positive value",
1120 object_get_typename(obj
), name
);
1123 s
->expired_scan_cycle
= value
;
1126 error_propagate(errp
, local_err
);
1129 static void compare_pri_rs_finalize(SocketReadState
*pri_rs
)
1131 CompareState
*s
= container_of(pri_rs
, CompareState
, pri_rs
);
1132 Connection
*conn
= NULL
;
1134 if (packet_enqueue(s
, PRIMARY_IN
, &conn
)) {
1135 trace_colo_compare_main("primary: unsupported packet in");
1139 pri_rs
->vnet_hdr_len
,
1143 /* compare packet in the specified connection */
1144 colo_compare_connection(conn
, s
);
1148 static void compare_sec_rs_finalize(SocketReadState
*sec_rs
)
1150 CompareState
*s
= container_of(sec_rs
, CompareState
, sec_rs
);
1151 Connection
*conn
= NULL
;
1153 if (packet_enqueue(s
, SECONDARY_IN
, &conn
)) {
1154 trace_colo_compare_main("secondary: unsupported packet in");
1156 /* compare packet in the specified connection */
1157 colo_compare_connection(conn
, s
);
1161 static void compare_notify_rs_finalize(SocketReadState
*notify_rs
)
1163 CompareState
*s
= container_of(notify_rs
, CompareState
, notify_rs
);
1165 const char msg
[] = "COLO_COMPARE_GET_XEN_INIT";
1168 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT",
1170 notify_rs
->packet_len
)) {
1171 ret
= compare_chr_send(s
, (uint8_t *)msg
, strlen(msg
), 0, true, false);
1173 error_report("Notify Xen COLO-frame INIT failed");
1175 } else if (packet_matches_str("COLO_CHECKPOINT",
1177 notify_rs
->packet_len
)) {
1178 /* colo-compare do checkpoint, flush pri packet and remove sec packet */
1179 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
1181 error_report("COLO compare got unsupported instruction");
1186 * Return 0 is success.
1187 * Return 1 is failed.
1189 static int find_and_check_chardev(Chardev
**chr
,
1193 *chr
= qemu_chr_find(chr_name
);
1195 error_setg(errp
, "Device '%s' not found",
1200 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
1201 error_setg(errp
, "chardev \"%s\" is not reconnectable",
1206 if (!qemu_chr_has_feature(*chr
, QEMU_CHAR_FEATURE_GCONTEXT
)) {
1207 error_setg(errp
, "chardev \"%s\" cannot switch context",
1216 * Called from the main thread on the primary
1217 * to setup colo-compare.
1219 static void colo_compare_complete(UserCreatable
*uc
, Error
**errp
)
1221 CompareState
*s
= COLO_COMPARE(uc
);
1224 if (!s
->pri_indev
|| !s
->sec_indev
|| !s
->outdev
|| !s
->iothread
) {
1225 error_setg(errp
, "colo compare needs 'primary_in' ,"
1226 "'secondary_in','outdev','iothread' property set");
1228 } else if (!strcmp(s
->pri_indev
, s
->outdev
) ||
1229 !strcmp(s
->sec_indev
, s
->outdev
) ||
1230 !strcmp(s
->pri_indev
, s
->sec_indev
)) {
1231 error_setg(errp
, "'indev' and 'outdev' could not be same "
1232 "for compare module");
1236 if (!s
->compare_timeout
) {
1237 /* Set default value to 3000 MS */
1238 s
->compare_timeout
= DEFAULT_TIME_OUT_MS
;
1241 if (!s
->expired_scan_cycle
) {
1242 /* Set default value to 3000 MS */
1243 s
->expired_scan_cycle
= REGULAR_PACKET_CHECK_MS
;
1246 if (find_and_check_chardev(&chr
, s
->pri_indev
, errp
) ||
1247 !qemu_chr_fe_init(&s
->chr_pri_in
, chr
, errp
)) {
1251 if (find_and_check_chardev(&chr
, s
->sec_indev
, errp
) ||
1252 !qemu_chr_fe_init(&s
->chr_sec_in
, chr
, errp
)) {
1256 if (find_and_check_chardev(&chr
, s
->outdev
, errp
) ||
1257 !qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
1261 net_socket_rs_init(&s
->pri_rs
, compare_pri_rs_finalize
, s
->vnet_hdr
);
1262 net_socket_rs_init(&s
->sec_rs
, compare_sec_rs_finalize
, s
->vnet_hdr
);
1264 /* Try to enable remote notify chardev, currently just for Xen COLO */
1265 if (s
->notify_dev
) {
1266 if (find_and_check_chardev(&chr
, s
->notify_dev
, errp
) ||
1267 !qemu_chr_fe_init(&s
->chr_notify_dev
, chr
, errp
)) {
1271 net_socket_rs_init(&s
->notify_rs
, compare_notify_rs_finalize
,
1275 QTAILQ_INSERT_TAIL(&net_compares
, s
, next
);
1277 s
->out_sendco
.s
= s
;
1278 s
->out_sendco
.chr
= &s
->chr_out
;
1279 s
->out_sendco
.notify_remote_frame
= false;
1280 s
->out_sendco
.done
= true;
1281 g_queue_init(&s
->out_sendco
.send_list
);
1283 if (s
->notify_dev
) {
1284 s
->notify_sendco
.s
= s
;
1285 s
->notify_sendco
.chr
= &s
->chr_notify_dev
;
1286 s
->notify_sendco
.notify_remote_frame
= true;
1287 s
->notify_sendco
.done
= true;
1288 g_queue_init(&s
->notify_sendco
.send_list
);
1291 g_queue_init(&s
->conn_list
);
1293 qemu_mutex_init(&event_mtx
);
1294 qemu_cond_init(&event_complete_cond
);
1296 s
->connection_track_table
= g_hash_table_new_full(connection_key_hash
,
1297 connection_key_equal
,
1299 connection_destroy
);
1301 colo_compare_iothread(s
);
1305 static void colo_flush_packets(void *opaque
, void *user_data
)
1307 CompareState
*s
= user_data
;
1308 Connection
*conn
= opaque
;
1311 while (!g_queue_is_empty(&conn
->primary_list
)) {
1312 pkt
= g_queue_pop_head(&conn
->primary_list
);
1319 packet_destroy_partial(pkt
, NULL
);
1321 while (!g_queue_is_empty(&conn
->secondary_list
)) {
1322 pkt
= g_queue_pop_head(&conn
->secondary_list
);
1323 packet_destroy(pkt
, NULL
);
1327 static void colo_compare_class_init(ObjectClass
*oc
, void *data
)
1329 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
1331 ucc
->complete
= colo_compare_complete
;
1334 static void colo_compare_init(Object
*obj
)
1336 CompareState
*s
= COLO_COMPARE(obj
);
1338 object_property_add_str(obj
, "primary_in",
1339 compare_get_pri_indev
, compare_set_pri_indev
);
1340 object_property_add_str(obj
, "secondary_in",
1341 compare_get_sec_indev
, compare_set_sec_indev
);
1342 object_property_add_str(obj
, "outdev",
1343 compare_get_outdev
, compare_set_outdev
);
1344 object_property_add_link(obj
, "iothread", TYPE_IOTHREAD
,
1345 (Object
**)&s
->iothread
,
1346 object_property_allow_set_link
,
1347 OBJ_PROP_LINK_STRONG
);
1348 /* This parameter just for Xen COLO */
1349 object_property_add_str(obj
, "notify_dev",
1350 compare_get_notify_dev
, compare_set_notify_dev
);
1352 object_property_add(obj
, "compare_timeout", "uint32",
1353 compare_get_timeout
,
1354 compare_set_timeout
, NULL
, NULL
);
1356 object_property_add(obj
, "expired_scan_cycle", "uint32",
1357 compare_get_expired_scan_cycle
,
1358 compare_set_expired_scan_cycle
, NULL
, NULL
);
1360 s
->vnet_hdr
= false;
1361 object_property_add_bool(obj
, "vnet_hdr_support", compare_get_vnet_hdr
,
1362 compare_set_vnet_hdr
);
1365 static void colo_compare_finalize(Object
*obj
)
1367 CompareState
*s
= COLO_COMPARE(obj
);
1368 CompareState
*tmp
= NULL
;
1370 qemu_chr_fe_deinit(&s
->chr_pri_in
, false);
1371 qemu_chr_fe_deinit(&s
->chr_sec_in
, false);
1372 qemu_chr_fe_deinit(&s
->chr_out
, false);
1373 if (s
->notify_dev
) {
1374 qemu_chr_fe_deinit(&s
->chr_notify_dev
, false);
1378 colo_compare_timer_del(s
);
1381 qemu_bh_delete(s
->event_bh
);
1383 QTAILQ_FOREACH(tmp
, &net_compares
, next
) {
1385 QTAILQ_REMOVE(&net_compares
, s
, next
);
1390 AioContext
*ctx
= iothread_get_aio_context(s
->iothread
);
1391 aio_context_acquire(ctx
);
1392 AIO_WAIT_WHILE(ctx
, !s
->out_sendco
.done
);
1393 if (s
->notify_dev
) {
1394 AIO_WAIT_WHILE(ctx
, !s
->notify_sendco
.done
);
1396 aio_context_release(ctx
);
1398 /* Release all unhandled packets after compare thead exited */
1399 g_queue_foreach(&s
->conn_list
, colo_flush_packets
, s
);
1400 AIO_WAIT_WHILE(NULL
, !s
->out_sendco
.done
);
1402 g_queue_clear(&s
->conn_list
);
1403 g_queue_clear(&s
->out_sendco
.send_list
);
1404 if (s
->notify_dev
) {
1405 g_queue_clear(&s
->notify_sendco
.send_list
);
1408 if (s
->connection_track_table
) {
1409 g_hash_table_destroy(s
->connection_track_table
);
1413 object_unref(OBJECT(s
->iothread
));
1416 qemu_mutex_destroy(&event_mtx
);
1417 qemu_cond_destroy(&event_complete_cond
);
1419 g_free(s
->pri_indev
);
1420 g_free(s
->sec_indev
);
1422 g_free(s
->notify_dev
);
1425 static const TypeInfo colo_compare_info
= {
1426 .name
= TYPE_COLO_COMPARE
,
1427 .parent
= TYPE_OBJECT
,
1428 .instance_size
= sizeof(CompareState
),
1429 .instance_init
= colo_compare_init
,
1430 .instance_finalize
= colo_compare_finalize
,
1431 .class_size
= sizeof(CompareClass
),
1432 .class_init
= colo_compare_class_init
,
1433 .interfaces
= (InterfaceInfo
[]) {
1434 { TYPE_USER_CREATABLE
},
1439 static void register_types(void)
1441 type_register_static(&colo_compare_info
);
1444 type_init(register_types
);