block/amend: refactor qcow2 amend options
[qemu/ar7.git] / net / colo-compare.c
blobf15779dedcb578b79458e736c7ff774d891f5965
1 /*
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"
18 #include "trace.h"
19 #include "qapi/error.h"
20 #include "net/net.h"
21 #include "net/eth.h"
22 #include "qom/object_interfaces.h"
23 #include "qemu/iov.h"
24 #include "qom/object.h"
25 #include "net/queue.h"
26 #include "chardev/char-fe.h"
27 #include "qemu/sockets.h"
28 #include "colo.h"
29 #include "sysemu/iothread.h"
30 #include "net/colo-compare.h"
31 #include "migration/colo.h"
32 #include "migration/migration.h"
33 #include "util.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 colo_compare_mutex;
58 static bool colo_compare_active;
59 static QemuMutex event_mtx;
60 static QemuCond event_complete_cond;
61 static int event_unhandled_count;
64 * + CompareState ++
65 * | |
66 * +---------------+ +---------------+ +---------------+
67 * | conn list + - > conn + ------- > conn + -- > ......
68 * +---------------+ +---------------+ +---------------+
69 * | | | | | |
70 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
71 * |primary | |secondary |primary | |secondary
72 * |packet | |packet + |packet | |packet +
73 * +--------+ +--------+ +--------+ +--------+
74 * | | | |
75 * +---v----+ +---v----+ +---v----+ +---v----+
76 * |primary | |secondary |primary | |secondary
77 * |packet | |packet + |packet | |packet +
78 * +--------+ +--------+ +--------+ +--------+
79 * | | | |
80 * +---v----+ +---v----+ +---v----+ +---v----+
81 * |primary | |secondary |primary | |secondary
82 * |packet | |packet + |packet | |packet +
83 * +--------+ +--------+ +--------+ +--------+
86 typedef struct SendCo {
87 Coroutine *co;
88 struct CompareState *s;
89 CharBackend *chr;
90 GQueue send_list;
91 bool notify_remote_frame;
92 bool done;
93 int ret;
94 } SendCo;
96 typedef struct SendEntry {
97 uint32_t size;
98 uint32_t vnet_hdr_len;
99 uint8_t *buf;
100 } SendEntry;
102 typedef struct CompareState {
103 Object parent;
105 char *pri_indev;
106 char *sec_indev;
107 char *outdev;
108 char *notify_dev;
109 CharBackend chr_pri_in;
110 CharBackend chr_sec_in;
111 CharBackend chr_out;
112 CharBackend chr_notify_dev;
113 SocketReadState pri_rs;
114 SocketReadState sec_rs;
115 SocketReadState notify_rs;
116 SendCo out_sendco;
117 SendCo notify_sendco;
118 bool vnet_hdr;
119 uint32_t compare_timeout;
120 uint32_t expired_scan_cycle;
123 * Record the connection that through the NIC
124 * Element type: Connection
126 GQueue conn_list;
127 /* Record the connection without repetition */
128 GHashTable *connection_track_table;
130 IOThread *iothread;
131 GMainContext *worker_context;
132 QEMUTimer *packet_check_timer;
134 QEMUBH *event_bh;
135 enum colo_event event;
137 QTAILQ_ENTRY(CompareState) next;
138 } CompareState;
140 typedef struct CompareClass {
141 ObjectClass parent_class;
142 } CompareClass;
144 enum {
145 PRIMARY_IN = 0,
146 SECONDARY_IN,
149 static const char *colo_mode[] = {
150 [PRIMARY_IN] = "primary",
151 [SECONDARY_IN] = "secondary",
154 static int compare_chr_send(CompareState *s,
155 uint8_t *buf,
156 uint32_t size,
157 uint32_t vnet_hdr_len,
158 bool notify_remote_frame,
159 bool zero_copy);
161 static bool packet_matches_str(const char *str,
162 const uint8_t *buf,
163 uint32_t packet_len)
165 if (packet_len != strlen(str)) {
166 return false;
169 return !memcmp(str, buf, strlen(str));
172 static void notify_remote_frame(CompareState *s)
174 char msg[] = "DO_CHECKPOINT";
175 int ret = 0;
177 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
178 if (ret < 0) {
179 error_report("Notify Xen COLO-frame failed");
183 static void colo_compare_inconsistency_notify(CompareState *s)
185 if (s->notify_dev) {
186 notify_remote_frame(s);
187 } else {
188 notifier_list_notify(&colo_compare_notifiers,
189 migrate_get_current());
193 static gint seq_sorter(Packet *a, Packet *b, gpointer data)
195 struct tcp_hdr *atcp, *btcp;
197 atcp = (struct tcp_hdr *)(a->transport_header);
198 btcp = (struct tcp_hdr *)(b->transport_header);
199 return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
202 static void fill_pkt_tcp_info(void *data, uint32_t *max_ack)
204 Packet *pkt = data;
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 *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack;
212 pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data
213 + (tcphd->th_off << 2) - pkt->vnet_hdr_len;
214 pkt->payload_size = pkt->size - pkt->header_size;
215 pkt->seq_end = pkt->tcp_seq + pkt->payload_size;
216 pkt->flags = tcphd->th_flags;
220 * Return 1 on success, if return 0 means the
221 * packet will be dropped
223 static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack)
225 if (g_queue_get_length(queue) <= MAX_QUEUE_SIZE) {
226 if (pkt->ip->ip_p == IPPROTO_TCP) {
227 fill_pkt_tcp_info(pkt, max_ack);
228 g_queue_insert_sorted(queue,
229 pkt,
230 (GCompareDataFunc)seq_sorter,
231 NULL);
232 } else {
233 g_queue_push_tail(queue, pkt);
235 return 1;
237 return 0;
241 * Return 0 on success, if return -1 means the pkt
242 * is unsupported(arp and ipv6) and will be sent later
244 static int packet_enqueue(CompareState *s, int mode, Connection **con)
246 ConnectionKey key;
247 Packet *pkt = NULL;
248 Connection *conn;
249 int ret;
251 if (mode == PRIMARY_IN) {
252 pkt = packet_new(s->pri_rs.buf,
253 s->pri_rs.packet_len,
254 s->pri_rs.vnet_hdr_len);
255 } else {
256 pkt = packet_new(s->sec_rs.buf,
257 s->sec_rs.packet_len,
258 s->sec_rs.vnet_hdr_len);
261 if (parse_packet_early(pkt)) {
262 packet_destroy(pkt, NULL);
263 pkt = NULL;
264 return -1;
266 fill_connection_key(pkt, &key);
268 conn = connection_get(s->connection_track_table,
269 &key,
270 &s->conn_list);
272 if (!conn->processing) {
273 g_queue_push_tail(&s->conn_list, conn);
274 conn->processing = true;
277 if (mode == PRIMARY_IN) {
278 ret = colo_insert_packet(&conn->primary_list, pkt, &conn->pack);
279 } else {
280 ret = colo_insert_packet(&conn->secondary_list, pkt, &conn->sack);
283 if (!ret) {
284 trace_colo_compare_drop_packet(colo_mode[mode],
285 "queue size too big, drop packet");
286 packet_destroy(pkt, NULL);
287 pkt = NULL;
290 *con = conn;
292 return 0;
295 static inline bool after(uint32_t seq1, uint32_t seq2)
297 return (int32_t)(seq1 - seq2) > 0;
300 static void colo_release_primary_pkt(CompareState *s, Packet *pkt)
302 int ret;
303 ret = compare_chr_send(s,
304 pkt->data,
305 pkt->size,
306 pkt->vnet_hdr_len,
307 false,
308 true);
309 if (ret < 0) {
310 error_report("colo send primary packet failed");
312 trace_colo_compare_main("packet same and release packet");
313 packet_destroy_partial(pkt, NULL);
317 * The IP packets sent by primary and secondary
318 * will be compared in here
319 * TODO support ip fragment, Out-Of-Order
320 * return: 0 means packet same
321 * > 0 || < 0 means packet different
323 static int colo_compare_packet_payload(Packet *ppkt,
324 Packet *spkt,
325 uint16_t poffset,
326 uint16_t soffset,
327 uint16_t len)
330 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
331 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
333 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
334 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
335 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
336 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
338 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
339 pri_ip_dst, spkt->size,
340 sec_ip_src, sec_ip_dst);
343 return memcmp(ppkt->data + poffset, spkt->data + soffset, len);
347 * return true means that the payload is consist and
348 * need to make the next comparison, false means do
349 * the checkpoint
351 static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
352 int8_t *mark, uint32_t max_ack)
354 *mark = 0;
356 if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) {
357 if (!colo_compare_packet_payload(ppkt, spkt,
358 ppkt->header_size, spkt->header_size,
359 ppkt->payload_size)) {
360 *mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY;
361 return true;
365 /* one part of secondary packet payload still need to be compared */
366 if (!after(ppkt->seq_end, spkt->seq_end)) {
367 if (!colo_compare_packet_payload(ppkt, spkt,
368 ppkt->header_size + ppkt->offset,
369 spkt->header_size + spkt->offset,
370 ppkt->payload_size - ppkt->offset)) {
371 if (!after(ppkt->tcp_ack, max_ack)) {
372 *mark = COLO_COMPARE_FREE_PRIMARY;
373 spkt->offset += ppkt->payload_size - ppkt->offset;
374 return true;
375 } else {
376 /* secondary guest hasn't ack the data, don't send
377 * out this packet
379 return false;
382 } else {
383 /* primary packet is longer than secondary packet, compare
384 * the same part and mark the primary packet offset
386 if (!colo_compare_packet_payload(ppkt, spkt,
387 ppkt->header_size + ppkt->offset,
388 spkt->header_size + spkt->offset,
389 spkt->payload_size - spkt->offset)) {
390 *mark = COLO_COMPARE_FREE_SECONDARY;
391 ppkt->offset += spkt->payload_size - spkt->offset;
392 return true;
396 return false;
399 static void colo_compare_tcp(CompareState *s, Connection *conn)
401 Packet *ppkt = NULL, *spkt = NULL;
402 int8_t mark;
405 * If ppkt and spkt have the same payload, but ppkt's ACK
406 * is greater than spkt's ACK, in this case we can not
407 * send the ppkt because it will cause the secondary guest
408 * to miss sending some data in the next. Therefore, we
409 * record the maximum ACK in the current queue at both
410 * primary side and secondary side. Only when the ack is
411 * less than the smaller of the two maximum ack, then we
412 * can ensure that the packet's payload is acknowledged by
413 * primary and secondary.
415 uint32_t min_ack = conn->pack > conn->sack ? conn->sack : conn->pack;
417 pri:
418 if (g_queue_is_empty(&conn->primary_list)) {
419 return;
421 ppkt = g_queue_pop_head(&conn->primary_list);
422 sec:
423 if (g_queue_is_empty(&conn->secondary_list)) {
424 g_queue_push_head(&conn->primary_list, ppkt);
425 return;
427 spkt = g_queue_pop_head(&conn->secondary_list);
429 if (ppkt->tcp_seq == ppkt->seq_end) {
430 colo_release_primary_pkt(s, ppkt);
431 ppkt = NULL;
434 if (ppkt && conn->compare_seq && !after(ppkt->seq_end, conn->compare_seq)) {
435 trace_colo_compare_main("pri: this packet has compared");
436 colo_release_primary_pkt(s, ppkt);
437 ppkt = NULL;
440 if (spkt->tcp_seq == spkt->seq_end) {
441 packet_destroy(spkt, NULL);
442 if (!ppkt) {
443 goto pri;
444 } else {
445 goto sec;
447 } else {
448 if (conn->compare_seq && !after(spkt->seq_end, conn->compare_seq)) {
449 trace_colo_compare_main("sec: this packet has compared");
450 packet_destroy(spkt, NULL);
451 if (!ppkt) {
452 goto pri;
453 } else {
454 goto sec;
457 if (!ppkt) {
458 g_queue_push_head(&conn->secondary_list, spkt);
459 goto pri;
463 if (colo_mark_tcp_pkt(ppkt, spkt, &mark, min_ack)) {
464 trace_colo_compare_tcp_info("pri",
465 ppkt->tcp_seq, ppkt->tcp_ack,
466 ppkt->header_size, ppkt->payload_size,
467 ppkt->offset, ppkt->flags);
469 trace_colo_compare_tcp_info("sec",
470 spkt->tcp_seq, spkt->tcp_ack,
471 spkt->header_size, spkt->payload_size,
472 spkt->offset, spkt->flags);
474 if (mark == COLO_COMPARE_FREE_PRIMARY) {
475 conn->compare_seq = ppkt->seq_end;
476 colo_release_primary_pkt(s, ppkt);
477 g_queue_push_head(&conn->secondary_list, spkt);
478 goto pri;
480 if (mark == COLO_COMPARE_FREE_SECONDARY) {
481 conn->compare_seq = spkt->seq_end;
482 packet_destroy(spkt, NULL);
483 goto sec;
485 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);
489 goto pri;
491 } else {
492 g_queue_push_head(&conn->primary_list, ppkt);
493 g_queue_push_head(&conn->secondary_list, spkt);
495 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
496 qemu_hexdump((char *)ppkt->data, stderr,
497 "colo-compare ppkt", ppkt->size);
498 qemu_hexdump((char *)spkt->data, stderr,
499 "colo-compare spkt", spkt->size);
502 colo_compare_inconsistency_notify(s);
508 * Called from the compare thread on the primary
509 * for compare udp packet
511 static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
513 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
514 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
516 trace_colo_compare_main("compare udp");
519 * Because of ppkt and spkt are both in the same connection,
520 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
521 * same with spkt. In addition, IP header's Identification is a random
522 * field, we can handle it in IP fragmentation function later.
523 * COLO just concern the response net packet payload from primary guest
524 * and secondary guest are same or not, So we ignored all IP header include
525 * other field like TOS,TTL,IP Checksum. we only need to compare
526 * the ip payload here.
528 if (ppkt->size != spkt->size) {
529 trace_colo_compare_main("UDP: payload size of packets are different");
530 return -1;
532 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
533 ppkt->size - offset)) {
534 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
535 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
536 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
537 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
538 ppkt->size);
539 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
540 spkt->size);
542 return -1;
543 } else {
544 return 0;
549 * Called from the compare thread on the primary
550 * for compare icmp packet
552 static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
554 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
555 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
557 trace_colo_compare_main("compare icmp");
560 * Because of ppkt and spkt are both in the same connection,
561 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
562 * same with spkt. In addition, IP header's Identification is a random
563 * field, we can handle it in IP fragmentation function later.
564 * COLO just concern the response net packet payload from primary guest
565 * and secondary guest are same or not, So we ignored all IP header include
566 * other field like TOS,TTL,IP Checksum. we only need to compare
567 * the ip payload here.
569 if (ppkt->size != spkt->size) {
570 trace_colo_compare_main("ICMP: payload size of packets are different");
571 return -1;
573 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
574 ppkt->size - offset)) {
575 trace_colo_compare_icmp_miscompare("primary pkt size",
576 ppkt->size);
577 trace_colo_compare_icmp_miscompare("Secondary pkt size",
578 spkt->size);
579 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
580 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
581 ppkt->size);
582 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
583 spkt->size);
585 return -1;
586 } else {
587 return 0;
592 * Called from the compare thread on the primary
593 * for compare other packet
595 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
597 uint16_t offset = ppkt->vnet_hdr_len;
599 trace_colo_compare_main("compare other");
600 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
601 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
603 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
604 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
605 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
606 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
608 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
609 pri_ip_dst, spkt->size,
610 sec_ip_src, sec_ip_dst);
613 if (ppkt->size != spkt->size) {
614 trace_colo_compare_main("Other: payload size of packets are different");
615 return -1;
617 return colo_compare_packet_payload(ppkt, spkt, offset, offset,
618 ppkt->size - offset);
621 static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
623 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
625 if ((now - pkt->creation_ms) > (*check_time)) {
626 trace_colo_old_packet_check_found(pkt->creation_ms);
627 return 0;
628 } else {
629 return 1;
633 void colo_compare_register_notifier(Notifier *notify)
635 notifier_list_add(&colo_compare_notifiers, notify);
638 void colo_compare_unregister_notifier(Notifier *notify)
640 notifier_remove(notify);
643 static int colo_old_packet_check_one_conn(Connection *conn,
644 CompareState *s)
646 GList *result = NULL;
648 result = g_queue_find_custom(&conn->primary_list,
649 &s->compare_timeout,
650 (GCompareFunc)colo_old_packet_check_one);
652 if (result) {
653 /* Do checkpoint will flush old packet */
654 colo_compare_inconsistency_notify(s);
655 return 0;
658 return 1;
662 * Look for old packets that the secondary hasn't matched,
663 * if we have some then we have to checkpoint to wake
664 * the secondary up.
666 static void colo_old_packet_check(void *opaque)
668 CompareState *s = opaque;
671 * If we find one old packet, stop finding job and notify
672 * COLO frame do checkpoint.
674 g_queue_find_custom(&s->conn_list, s,
675 (GCompareFunc)colo_old_packet_check_one_conn);
678 static void colo_compare_packet(CompareState *s, Connection *conn,
679 int (*HandlePacket)(Packet *spkt,
680 Packet *ppkt))
682 Packet *pkt = NULL;
683 GList *result = NULL;
685 while (!g_queue_is_empty(&conn->primary_list) &&
686 !g_queue_is_empty(&conn->secondary_list)) {
687 pkt = g_queue_pop_head(&conn->primary_list);
688 result = g_queue_find_custom(&conn->secondary_list,
689 pkt, (GCompareFunc)HandlePacket);
691 if (result) {
692 colo_release_primary_pkt(s, pkt);
693 g_queue_remove(&conn->secondary_list, result->data);
694 } else {
696 * If one packet arrive late, the secondary_list or
697 * primary_list will be empty, so we can't compare it
698 * until next comparison. If the packets in the list are
699 * timeout, it will trigger a checkpoint request.
701 trace_colo_compare_main("packet different");
702 g_queue_push_head(&conn->primary_list, pkt);
704 colo_compare_inconsistency_notify(s);
705 break;
711 * Called from the compare thread on the primary
712 * for compare packet with secondary list of the
713 * specified connection when a new packet was
714 * queued to it.
716 static void colo_compare_connection(void *opaque, void *user_data)
718 CompareState *s = user_data;
719 Connection *conn = opaque;
721 switch (conn->ip_proto) {
722 case IPPROTO_TCP:
723 colo_compare_tcp(s, conn);
724 break;
725 case IPPROTO_UDP:
726 colo_compare_packet(s, conn, colo_packet_compare_udp);
727 break;
728 case IPPROTO_ICMP:
729 colo_compare_packet(s, conn, colo_packet_compare_icmp);
730 break;
731 default:
732 colo_compare_packet(s, conn, colo_packet_compare_other);
733 break;
737 static void coroutine_fn _compare_chr_send(void *opaque)
739 SendCo *sendco = opaque;
740 CompareState *s = sendco->s;
741 int ret = 0;
743 while (!g_queue_is_empty(&sendco->send_list)) {
744 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
745 uint32_t len = htonl(entry->size);
747 ret = qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof(len));
749 if (ret != sizeof(len)) {
750 g_free(entry->buf);
751 g_slice_free(SendEntry, entry);
752 goto err;
755 if (!sendco->notify_remote_frame && s->vnet_hdr) {
757 * We send vnet header len make other module(like filter-redirector)
758 * know how to parse net packet correctly.
760 len = htonl(entry->vnet_hdr_len);
762 ret = qemu_chr_fe_write_all(sendco->chr,
763 (uint8_t *)&len,
764 sizeof(len));
766 if (ret != sizeof(len)) {
767 g_free(entry->buf);
768 g_slice_free(SendEntry, entry);
769 goto err;
773 ret = qemu_chr_fe_write_all(sendco->chr,
774 (uint8_t *)entry->buf,
775 entry->size);
777 if (ret != entry->size) {
778 g_free(entry->buf);
779 g_slice_free(SendEntry, entry);
780 goto err;
783 g_free(entry->buf);
784 g_slice_free(SendEntry, entry);
787 sendco->ret = 0;
788 goto out;
790 err:
791 while (!g_queue_is_empty(&sendco->send_list)) {
792 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
793 g_free(entry->buf);
794 g_slice_free(SendEntry, entry);
796 sendco->ret = ret < 0 ? ret : -EIO;
797 out:
798 sendco->co = NULL;
799 sendco->done = true;
800 aio_wait_kick();
803 static int compare_chr_send(CompareState *s,
804 uint8_t *buf,
805 uint32_t size,
806 uint32_t vnet_hdr_len,
807 bool notify_remote_frame,
808 bool zero_copy)
810 SendCo *sendco;
811 SendEntry *entry;
813 if (notify_remote_frame) {
814 sendco = &s->notify_sendco;
815 } else {
816 sendco = &s->out_sendco;
819 if (!size) {
820 return 0;
823 entry = g_slice_new(SendEntry);
824 entry->size = size;
825 entry->vnet_hdr_len = vnet_hdr_len;
826 if (zero_copy) {
827 entry->buf = buf;
828 } else {
829 entry->buf = g_malloc(size);
830 memcpy(entry->buf, buf, size);
832 g_queue_push_head(&sendco->send_list, entry);
834 if (sendco->done) {
835 sendco->co = qemu_coroutine_create(_compare_chr_send, sendco);
836 sendco->done = false;
837 qemu_coroutine_enter(sendco->co);
838 if (sendco->done) {
839 /* report early errors */
840 return sendco->ret;
844 /* assume success */
845 return 0;
848 static int compare_chr_can_read(void *opaque)
850 return COMPARE_READ_LEN_MAX;
854 * Called from the main thread on the primary for packets
855 * arriving over the socket from the primary.
857 static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
859 CompareState *s = COLO_COMPARE(opaque);
860 int ret;
862 ret = net_fill_rstate(&s->pri_rs, buf, size);
863 if (ret == -1) {
864 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
865 NULL, NULL, true);
866 error_report("colo-compare primary_in error");
871 * Called from the main thread on the primary for packets
872 * arriving over the socket from the secondary.
874 static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
876 CompareState *s = COLO_COMPARE(opaque);
877 int ret;
879 ret = net_fill_rstate(&s->sec_rs, buf, size);
880 if (ret == -1) {
881 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
882 NULL, NULL, true);
883 error_report("colo-compare secondary_in error");
887 static void compare_notify_chr(void *opaque, const uint8_t *buf, int size)
889 CompareState *s = COLO_COMPARE(opaque);
890 int ret;
892 ret = net_fill_rstate(&s->notify_rs, buf, size);
893 if (ret == -1) {
894 qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL,
895 NULL, NULL, true);
896 error_report("colo-compare notify_dev error");
901 * Check old packet regularly so it can watch for any packets
902 * that the secondary hasn't produced equivalents of.
904 static void check_old_packet_regular(void *opaque)
906 CompareState *s = opaque;
908 /* if have old packet we will notify checkpoint */
909 colo_old_packet_check(s);
910 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
911 s->expired_scan_cycle);
914 /* Public API, Used for COLO frame to notify compare event */
915 void colo_notify_compares_event(void *opaque, int event, Error **errp)
917 CompareState *s;
918 qemu_mutex_lock(&colo_compare_mutex);
920 if (!colo_compare_active) {
921 qemu_mutex_unlock(&colo_compare_mutex);
922 return;
925 qemu_mutex_lock(&event_mtx);
926 QTAILQ_FOREACH(s, &net_compares, next) {
927 s->event = event;
928 qemu_bh_schedule(s->event_bh);
929 event_unhandled_count++;
931 /* Wait all compare threads to finish handling this event */
932 while (event_unhandled_count > 0) {
933 qemu_cond_wait(&event_complete_cond, &event_mtx);
936 qemu_mutex_unlock(&event_mtx);
937 qemu_mutex_unlock(&colo_compare_mutex);
940 static void colo_compare_timer_init(CompareState *s)
942 AioContext *ctx = iothread_get_aio_context(s->iothread);
944 s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL,
945 SCALE_MS, check_old_packet_regular,
947 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
948 s->expired_scan_cycle);
951 static void colo_compare_timer_del(CompareState *s)
953 if (s->packet_check_timer) {
954 timer_del(s->packet_check_timer);
955 timer_free(s->packet_check_timer);
956 s->packet_check_timer = NULL;
960 static void colo_flush_packets(void *opaque, void *user_data);
962 static void colo_compare_handle_event(void *opaque)
964 CompareState *s = opaque;
966 switch (s->event) {
967 case COLO_EVENT_CHECKPOINT:
968 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
969 break;
970 case COLO_EVENT_FAILOVER:
971 break;
972 default:
973 break;
976 qemu_mutex_lock(&event_mtx);
977 assert(event_unhandled_count > 0);
978 event_unhandled_count--;
979 qemu_cond_broadcast(&event_complete_cond);
980 qemu_mutex_unlock(&event_mtx);
983 static void colo_compare_iothread(CompareState *s)
985 AioContext *ctx = iothread_get_aio_context(s->iothread);
986 object_ref(OBJECT(s->iothread));
987 s->worker_context = iothread_get_g_main_context(s->iothread);
989 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
990 compare_pri_chr_in, NULL, NULL,
991 s, s->worker_context, true);
992 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
993 compare_sec_chr_in, NULL, NULL,
994 s, s->worker_context, true);
995 if (s->notify_dev) {
996 qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read,
997 compare_notify_chr, NULL, NULL,
998 s, s->worker_context, true);
1001 colo_compare_timer_init(s);
1002 s->event_bh = aio_bh_new(ctx, colo_compare_handle_event, s);
1005 static char *compare_get_pri_indev(Object *obj, Error **errp)
1007 CompareState *s = COLO_COMPARE(obj);
1009 return g_strdup(s->pri_indev);
1012 static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
1014 CompareState *s = COLO_COMPARE(obj);
1016 g_free(s->pri_indev);
1017 s->pri_indev = g_strdup(value);
1020 static char *compare_get_sec_indev(Object *obj, Error **errp)
1022 CompareState *s = COLO_COMPARE(obj);
1024 return g_strdup(s->sec_indev);
1027 static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
1029 CompareState *s = COLO_COMPARE(obj);
1031 g_free(s->sec_indev);
1032 s->sec_indev = g_strdup(value);
1035 static char *compare_get_outdev(Object *obj, Error **errp)
1037 CompareState *s = COLO_COMPARE(obj);
1039 return g_strdup(s->outdev);
1042 static void compare_set_outdev(Object *obj, const char *value, Error **errp)
1044 CompareState *s = COLO_COMPARE(obj);
1046 g_free(s->outdev);
1047 s->outdev = g_strdup(value);
1050 static bool compare_get_vnet_hdr(Object *obj, Error **errp)
1052 CompareState *s = COLO_COMPARE(obj);
1054 return s->vnet_hdr;
1057 static void compare_set_vnet_hdr(Object *obj,
1058 bool value,
1059 Error **errp)
1061 CompareState *s = COLO_COMPARE(obj);
1063 s->vnet_hdr = value;
1066 static char *compare_get_notify_dev(Object *obj, Error **errp)
1068 CompareState *s = COLO_COMPARE(obj);
1070 return g_strdup(s->notify_dev);
1073 static void compare_set_notify_dev(Object *obj, const char *value, Error **errp)
1075 CompareState *s = COLO_COMPARE(obj);
1077 g_free(s->notify_dev);
1078 s->notify_dev = g_strdup(value);
1081 static void compare_get_timeout(Object *obj, Visitor *v,
1082 const char *name, void *opaque,
1083 Error **errp)
1085 CompareState *s = COLO_COMPARE(obj);
1086 uint32_t value = s->compare_timeout;
1088 visit_type_uint32(v, name, &value, errp);
1091 static void compare_set_timeout(Object *obj, Visitor *v,
1092 const char *name, void *opaque,
1093 Error **errp)
1095 CompareState *s = COLO_COMPARE(obj);
1096 Error *local_err = NULL;
1097 uint32_t value;
1099 visit_type_uint32(v, name, &value, &local_err);
1100 if (local_err) {
1101 goto out;
1103 if (!value) {
1104 error_setg(&local_err, "Property '%s.%s' requires a positive value",
1105 object_get_typename(obj), name);
1106 goto out;
1108 s->compare_timeout = value;
1110 out:
1111 error_propagate(errp, local_err);
1114 static void compare_get_expired_scan_cycle(Object *obj, Visitor *v,
1115 const char *name, void *opaque,
1116 Error **errp)
1118 CompareState *s = COLO_COMPARE(obj);
1119 uint32_t value = s->expired_scan_cycle;
1121 visit_type_uint32(v, name, &value, errp);
1124 static void compare_set_expired_scan_cycle(Object *obj, Visitor *v,
1125 const char *name, void *opaque,
1126 Error **errp)
1128 CompareState *s = COLO_COMPARE(obj);
1129 Error *local_err = NULL;
1130 uint32_t value;
1132 visit_type_uint32(v, name, &value, &local_err);
1133 if (local_err) {
1134 goto out;
1136 if (!value) {
1137 error_setg(&local_err, "Property '%s.%s' requires a positive value",
1138 object_get_typename(obj), name);
1139 goto out;
1141 s->expired_scan_cycle = value;
1143 out:
1144 error_propagate(errp, local_err);
1147 static void compare_pri_rs_finalize(SocketReadState *pri_rs)
1149 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
1150 Connection *conn = NULL;
1152 if (packet_enqueue(s, PRIMARY_IN, &conn)) {
1153 trace_colo_compare_main("primary: unsupported packet in");
1154 compare_chr_send(s,
1155 pri_rs->buf,
1156 pri_rs->packet_len,
1157 pri_rs->vnet_hdr_len,
1158 false,
1159 false);
1160 } else {
1161 /* compare packet in the specified connection */
1162 colo_compare_connection(conn, s);
1166 static void compare_sec_rs_finalize(SocketReadState *sec_rs)
1168 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
1169 Connection *conn = NULL;
1171 if (packet_enqueue(s, SECONDARY_IN, &conn)) {
1172 trace_colo_compare_main("secondary: unsupported packet in");
1173 } else {
1174 /* compare packet in the specified connection */
1175 colo_compare_connection(conn, s);
1179 static void compare_notify_rs_finalize(SocketReadState *notify_rs)
1181 CompareState *s = container_of(notify_rs, CompareState, notify_rs);
1183 const char msg[] = "COLO_COMPARE_GET_XEN_INIT";
1184 int ret;
1186 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT",
1187 notify_rs->buf,
1188 notify_rs->packet_len)) {
1189 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
1190 if (ret < 0) {
1191 error_report("Notify Xen COLO-frame INIT failed");
1193 } else if (packet_matches_str("COLO_CHECKPOINT",
1194 notify_rs->buf,
1195 notify_rs->packet_len)) {
1196 /* colo-compare do checkpoint, flush pri packet and remove sec packet */
1197 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
1198 } else {
1199 error_report("COLO compare got unsupported instruction");
1204 * Return 0 is success.
1205 * Return 1 is failed.
1207 static int find_and_check_chardev(Chardev **chr,
1208 char *chr_name,
1209 Error **errp)
1211 *chr = qemu_chr_find(chr_name);
1212 if (*chr == NULL) {
1213 error_setg(errp, "Device '%s' not found",
1214 chr_name);
1215 return 1;
1218 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
1219 error_setg(errp, "chardev \"%s\" is not reconnectable",
1220 chr_name);
1221 return 1;
1224 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) {
1225 error_setg(errp, "chardev \"%s\" cannot switch context",
1226 chr_name);
1227 return 1;
1230 return 0;
1234 * Called from the main thread on the primary
1235 * to setup colo-compare.
1237 static void colo_compare_complete(UserCreatable *uc, Error **errp)
1239 CompareState *s = COLO_COMPARE(uc);
1240 Chardev *chr;
1242 if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) {
1243 error_setg(errp, "colo compare needs 'primary_in' ,"
1244 "'secondary_in','outdev','iothread' property set");
1245 return;
1246 } else if (!strcmp(s->pri_indev, s->outdev) ||
1247 !strcmp(s->sec_indev, s->outdev) ||
1248 !strcmp(s->pri_indev, s->sec_indev)) {
1249 error_setg(errp, "'indev' and 'outdev' could not be same "
1250 "for compare module");
1251 return;
1254 if (!s->compare_timeout) {
1255 /* Set default value to 3000 MS */
1256 s->compare_timeout = DEFAULT_TIME_OUT_MS;
1259 if (!s->expired_scan_cycle) {
1260 /* Set default value to 3000 MS */
1261 s->expired_scan_cycle = REGULAR_PACKET_CHECK_MS;
1264 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
1265 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
1266 return;
1269 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
1270 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
1271 return;
1274 if (find_and_check_chardev(&chr, s->outdev, errp) ||
1275 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
1276 return;
1279 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
1280 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
1282 /* Try to enable remote notify chardev, currently just for Xen COLO */
1283 if (s->notify_dev) {
1284 if (find_and_check_chardev(&chr, s->notify_dev, errp) ||
1285 !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) {
1286 return;
1289 net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize,
1290 s->vnet_hdr);
1293 s->out_sendco.s = s;
1294 s->out_sendco.chr = &s->chr_out;
1295 s->out_sendco.notify_remote_frame = false;
1296 s->out_sendco.done = true;
1297 g_queue_init(&s->out_sendco.send_list);
1299 if (s->notify_dev) {
1300 s->notify_sendco.s = s;
1301 s->notify_sendco.chr = &s->chr_notify_dev;
1302 s->notify_sendco.notify_remote_frame = true;
1303 s->notify_sendco.done = true;
1304 g_queue_init(&s->notify_sendco.send_list);
1307 g_queue_init(&s->conn_list);
1309 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
1310 connection_key_equal,
1311 g_free,
1312 connection_destroy);
1314 colo_compare_iothread(s);
1316 qemu_mutex_lock(&colo_compare_mutex);
1317 if (!colo_compare_active) {
1318 qemu_mutex_init(&event_mtx);
1319 qemu_cond_init(&event_complete_cond);
1320 colo_compare_active = true;
1322 QTAILQ_INSERT_TAIL(&net_compares, s, next);
1323 qemu_mutex_unlock(&colo_compare_mutex);
1325 return;
1328 static void colo_flush_packets(void *opaque, void *user_data)
1330 CompareState *s = user_data;
1331 Connection *conn = opaque;
1332 Packet *pkt = NULL;
1334 while (!g_queue_is_empty(&conn->primary_list)) {
1335 pkt = g_queue_pop_head(&conn->primary_list);
1336 compare_chr_send(s,
1337 pkt->data,
1338 pkt->size,
1339 pkt->vnet_hdr_len,
1340 false,
1341 true);
1342 packet_destroy_partial(pkt, NULL);
1344 while (!g_queue_is_empty(&conn->secondary_list)) {
1345 pkt = g_queue_pop_head(&conn->secondary_list);
1346 packet_destroy(pkt, NULL);
1350 static void colo_compare_class_init(ObjectClass *oc, void *data)
1352 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
1354 ucc->complete = colo_compare_complete;
1357 static void colo_compare_init(Object *obj)
1359 CompareState *s = COLO_COMPARE(obj);
1361 object_property_add_str(obj, "primary_in",
1362 compare_get_pri_indev, compare_set_pri_indev);
1363 object_property_add_str(obj, "secondary_in",
1364 compare_get_sec_indev, compare_set_sec_indev);
1365 object_property_add_str(obj, "outdev",
1366 compare_get_outdev, compare_set_outdev);
1367 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
1368 (Object **)&s->iothread,
1369 object_property_allow_set_link,
1370 OBJ_PROP_LINK_STRONG);
1371 /* This parameter just for Xen COLO */
1372 object_property_add_str(obj, "notify_dev",
1373 compare_get_notify_dev, compare_set_notify_dev);
1375 object_property_add(obj, "compare_timeout", "uint32",
1376 compare_get_timeout,
1377 compare_set_timeout, NULL, NULL);
1379 object_property_add(obj, "expired_scan_cycle", "uint32",
1380 compare_get_expired_scan_cycle,
1381 compare_set_expired_scan_cycle, NULL, NULL);
1383 s->vnet_hdr = false;
1384 object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
1385 compare_set_vnet_hdr);
1388 static void colo_compare_finalize(Object *obj)
1390 CompareState *s = COLO_COMPARE(obj);
1391 CompareState *tmp = NULL;
1393 qemu_mutex_lock(&colo_compare_mutex);
1394 QTAILQ_FOREACH(tmp, &net_compares, next) {
1395 if (tmp == s) {
1396 QTAILQ_REMOVE(&net_compares, s, next);
1397 break;
1400 if (QTAILQ_EMPTY(&net_compares)) {
1401 colo_compare_active = false;
1402 qemu_mutex_destroy(&event_mtx);
1403 qemu_cond_destroy(&event_complete_cond);
1405 qemu_mutex_unlock(&colo_compare_mutex);
1407 qemu_chr_fe_deinit(&s->chr_pri_in, false);
1408 qemu_chr_fe_deinit(&s->chr_sec_in, false);
1409 qemu_chr_fe_deinit(&s->chr_out, false);
1410 if (s->notify_dev) {
1411 qemu_chr_fe_deinit(&s->chr_notify_dev, false);
1414 if (s->iothread) {
1415 colo_compare_timer_del(s);
1418 qemu_bh_delete(s->event_bh);
1420 AioContext *ctx = iothread_get_aio_context(s->iothread);
1421 aio_context_acquire(ctx);
1422 AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
1423 if (s->notify_dev) {
1424 AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
1426 aio_context_release(ctx);
1428 /* Release all unhandled packets after compare thead exited */
1429 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
1430 AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
1432 g_queue_clear(&s->conn_list);
1433 g_queue_clear(&s->out_sendco.send_list);
1434 if (s->notify_dev) {
1435 g_queue_clear(&s->notify_sendco.send_list);
1438 if (s->connection_track_table) {
1439 g_hash_table_destroy(s->connection_track_table);
1442 if (s->iothread) {
1443 object_unref(OBJECT(s->iothread));
1446 g_free(s->pri_indev);
1447 g_free(s->sec_indev);
1448 g_free(s->outdev);
1449 g_free(s->notify_dev);
1452 static void __attribute__((__constructor__)) colo_compare_init_globals(void)
1454 colo_compare_active = false;
1455 qemu_mutex_init(&colo_compare_mutex);
1458 static const TypeInfo colo_compare_info = {
1459 .name = TYPE_COLO_COMPARE,
1460 .parent = TYPE_OBJECT,
1461 .instance_size = sizeof(CompareState),
1462 .instance_init = colo_compare_init,
1463 .instance_finalize = colo_compare_finalize,
1464 .class_size = sizeof(CompareClass),
1465 .class_init = colo_compare_class_init,
1466 .interfaces = (InterfaceInfo[]) {
1467 { TYPE_USER_CREATABLE },
1472 static void register_types(void)
1474 type_register_static(&colo_compare_info);
1477 type_init(register_types);