1 /* 2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) 3 * (a.k.a. Fault Tolerance or Continuous Replication) 4 * 5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. 6 * Copyright (c) 2016 FUJITSU LIMITED 7 * Copyright (c) 2016 Intel Corporation 8 * 9 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> 10 * 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. 13 */ 14 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" 34 35 #include "block/aio-wait.h" 36 #include "qemu/coroutine.h" 37 38 #define TYPE_COLO_COMPARE "colo-compare" 39 typedef struct CompareState CompareState; 40 DECLARE_INSTANCE_CHECKER(CompareState, COLO_COMPARE, 41 TYPE_COLO_COMPARE) 42 43 static QTAILQ_HEAD(, CompareState) net_compares = 44 QTAILQ_HEAD_INITIALIZER(net_compares); 45 46 static NotifierList colo_compare_notifiers = 47 NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers); 48 49 #define COMPARE_READ_LEN_MAX NET_BUFSIZE 50 #define MAX_QUEUE_SIZE 1024 51 52 #define COLO_COMPARE_FREE_PRIMARY 0x01 53 #define COLO_COMPARE_FREE_SECONDARY 0x02 54 55 #define REGULAR_PACKET_CHECK_MS 3000 56 #define DEFAULT_TIME_OUT_MS 3000 57 58 /* #define DEBUG_COLO_PACKETS */ 59 60 static QemuMutex colo_compare_mutex; 61 static bool colo_compare_active; 62 static QemuMutex event_mtx; 63 static QemuCond event_complete_cond; 64 static int event_unhandled_count; 65 static uint32_t max_queue_size; 66 67 /* 68 * + CompareState ++ 69 * | | 70 * +---------------+ +---------------+ +---------------+ 71 * | conn list + - > conn + ------- > conn + -- > ...... 72 * +---------------+ +---------------+ +---------------+ 73 * | | | | | | 74 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ 75 * |primary | |secondary |primary | |secondary 76 * |packet | |packet + |packet | |packet + 77 * +--------+ +--------+ +--------+ +--------+ 78 * | | | | 79 * +---v----+ +---v----+ +---v----+ +---v----+ 80 * |primary | |secondary |primary | |secondary 81 * |packet | |packet + |packet | |packet + 82 * +--------+ +--------+ +--------+ +--------+ 83 * | | | | 84 * +---v----+ +---v----+ +---v----+ +---v----+ 85 * |primary | |secondary |primary | |secondary 86 * |packet | |packet + |packet | |packet + 87 * +--------+ +--------+ +--------+ +--------+ 88 */ 89 90 typedef struct SendCo { 91 Coroutine *co; 92 struct CompareState *s; 93 CharBackend *chr; 94 GQueue send_list; 95 bool notify_remote_frame; 96 bool done; 97 int ret; 98 } SendCo; 99 100 typedef struct SendEntry { 101 uint32_t size; 102 uint32_t vnet_hdr_len; 103 uint8_t *buf; 104 } SendEntry; 105 106 struct CompareState { 107 Object parent; 108 109 char *pri_indev; 110 char *sec_indev; 111 char *outdev; 112 char *notify_dev; 113 CharBackend chr_pri_in; 114 CharBackend chr_sec_in; 115 CharBackend chr_out; 116 CharBackend chr_notify_dev; 117 SocketReadState pri_rs; 118 SocketReadState sec_rs; 119 SocketReadState notify_rs; 120 SendCo out_sendco; 121 SendCo notify_sendco; 122 bool vnet_hdr; 123 uint64_t compare_timeout; 124 uint32_t expired_scan_cycle; 125 126 /* 127 * Record the connection that through the NIC 128 * Element type: Connection 129 */ 130 GQueue conn_list; 131 /* Record the connection without repetition */ 132 GHashTable *connection_track_table; 133 134 IOThread *iothread; 135 GMainContext *worker_context; 136 QEMUTimer *packet_check_timer; 137 138 QEMUBH *event_bh; 139 enum colo_event event; 140 141 QTAILQ_ENTRY(CompareState) next; 142 }; 143 144 typedef struct CompareClass { 145 ObjectClass parent_class; 146 } CompareClass; 147 148 enum { 149 PRIMARY_IN = 0, 150 SECONDARY_IN, 151 }; 152 153 static const char *colo_mode[] = { 154 [PRIMARY_IN] = "primary", 155 [SECONDARY_IN] = "secondary", 156 }; 157 158 static int compare_chr_send(CompareState *s, 159 uint8_t *buf, 160 uint32_t size, 161 uint32_t vnet_hdr_len, 162 bool notify_remote_frame, 163 bool zero_copy); 164 165 static bool packet_matches_str(const char *str, 166 const uint8_t *buf, 167 uint32_t packet_len) 168 { 169 if (packet_len != strlen(str)) { 170 return false; 171 } 172 173 return !memcmp(str, buf, strlen(str)); 174 } 175 176 static void notify_remote_frame(CompareState *s) 177 { 178 char msg[] = "DO_CHECKPOINT"; 179 int ret = 0; 180 181 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false); 182 if (ret < 0) { 183 error_report("Notify Xen COLO-frame failed"); 184 } 185 } 186 187 static void colo_compare_inconsistency_notify(CompareState *s) 188 { 189 if (s->notify_dev) { 190 notify_remote_frame(s); 191 } else { 192 notifier_list_notify(&colo_compare_notifiers, 193 migrate_get_current()); 194 } 195 } 196 197 /* Use restricted to colo_insert_packet() */ 198 static gint seq_sorter(Packet *a, Packet *b, gpointer data) 199 { 200 return a->tcp_seq - b->tcp_seq; 201 } 202 203 static void fill_pkt_tcp_info(void *data, uint32_t *max_ack) 204 { 205 Packet *pkt = data; 206 struct tcp_hdr *tcphd; 207 208 tcphd = (struct tcp_hdr *)pkt->transport_header; 209 210 pkt->tcp_seq = ntohl(tcphd->th_seq); 211 pkt->tcp_ack = ntohl(tcphd->th_ack); 212 *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack; 213 pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data 214 + (tcphd->th_off << 2) - pkt->vnet_hdr_len; 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; 218 } 219 220 /* 221 * Return 1 on success, if return 0 means the 222 * packet will be dropped 223 */ 224 static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack) 225 { 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, 230 pkt, 231 (GCompareDataFunc)seq_sorter, 232 NULL); 233 } else { 234 g_queue_push_tail(queue, pkt); 235 } 236 return 1; 237 } 238 return 0; 239 } 240 241 /* 242 * Return 0 on success, if return -1 means the pkt 243 * is unsupported(arp and ipv6) and will be sent later 244 */ 245 static int packet_enqueue(CompareState *s, int mode, Connection **con) 246 { 247 ConnectionKey key; 248 Packet *pkt = NULL; 249 Connection *conn; 250 int ret; 251 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); 256 } else { 257 pkt = packet_new(s->sec_rs.buf, 258 s->sec_rs.packet_len, 259 s->sec_rs.vnet_hdr_len); 260 } 261 262 if (parse_packet_early(pkt)) { 263 packet_destroy(pkt, NULL); 264 pkt = NULL; 265 return -1; 266 } 267 fill_connection_key(pkt, &key); 268 269 conn = connection_get(s->connection_track_table, 270 &key, 271 &s->conn_list); 272 273 if (!conn->processing) { 274 g_queue_push_tail(&s->conn_list, conn); 275 conn->processing = true; 276 } 277 278 if (mode == PRIMARY_IN) { 279 ret = colo_insert_packet(&conn->primary_list, pkt, &conn->pack); 280 } else { 281 ret = colo_insert_packet(&conn->secondary_list, pkt, &conn->sack); 282 } 283 284 if (!ret) { 285 trace_colo_compare_drop_packet(colo_mode[mode], 286 "queue size too big, drop packet"); 287 packet_destroy(pkt, NULL); 288 pkt = NULL; 289 } 290 291 *con = conn; 292 293 return 0; 294 } 295 296 static inline bool after(uint32_t seq1, uint32_t seq2) 297 { 298 return (int32_t)(seq1 - seq2) > 0; 299 } 300 301 static void colo_release_primary_pkt(CompareState *s, Packet *pkt) 302 { 303 int ret; 304 ret = compare_chr_send(s, 305 pkt->data, 306 pkt->size, 307 pkt->vnet_hdr_len, 308 false, 309 true); 310 if (ret < 0) { 311 error_report("colo send primary packet failed"); 312 } 313 trace_colo_compare_main("packet same and release packet"); 314 packet_destroy_partial(pkt, NULL); 315 } 316 317 /* 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 323 */ 324 static int colo_compare_packet_payload(Packet *ppkt, 325 Packet *spkt, 326 uint16_t poffset, 327 uint16_t soffset, 328 uint16_t len) 329 330 { 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]; 333 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)); 338 339 trace_colo_compare_ip_info(ppkt->size, pri_ip_src, 340 pri_ip_dst, spkt->size, 341 sec_ip_src, sec_ip_dst); 342 } 343 344 return memcmp(ppkt->data + poffset, spkt->data + soffset, len); 345 } 346 347 /* 348 * return true means that the payload is consist and 349 * need to make the next comparison, false means do 350 * the checkpoint 351 */ 352 static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt, 353 int8_t *mark, uint32_t max_ack) 354 { 355 *mark = 0; 356 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; 362 return true; 363 } 364 } 365 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; 375 return true; 376 } else { 377 /* secondary guest hasn't ack the data, don't send 378 * out this packet 379 */ 380 return false; 381 } 382 } 383 } else { 384 /* primary packet is longer than secondary packet, compare 385 * the same part and mark the primary packet offset 386 */ 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; 393 return true; 394 } 395 } 396 397 return false; 398 } 399 400 static void colo_compare_tcp(CompareState *s, Connection *conn) 401 { 402 Packet *ppkt = NULL, *spkt = NULL; 403 int8_t mark; 404 405 /* 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. 415 */ 416 uint32_t min_ack = conn->pack > conn->sack ? conn->sack : conn->pack; 417 418 pri: 419 if (g_queue_is_empty(&conn->primary_list)) { 420 return; 421 } 422 ppkt = g_queue_pop_head(&conn->primary_list); 423 sec: 424 if (g_queue_is_empty(&conn->secondary_list)) { 425 g_queue_push_head(&conn->primary_list, ppkt); 426 return; 427 } 428 spkt = g_queue_pop_head(&conn->secondary_list); 429 430 if (ppkt->tcp_seq == ppkt->seq_end) { 431 colo_release_primary_pkt(s, ppkt); 432 ppkt = NULL; 433 } 434 435 if (ppkt && conn->compare_seq && !after(ppkt->seq_end, conn->compare_seq)) { 436 trace_colo_compare_main("pri: this packet has compared"); 437 colo_release_primary_pkt(s, ppkt); 438 ppkt = NULL; 439 } 440 441 if (spkt->tcp_seq == spkt->seq_end) { 442 packet_destroy(spkt, NULL); 443 if (!ppkt) { 444 goto pri; 445 } else { 446 goto sec; 447 } 448 } else { 449 if (conn->compare_seq && !after(spkt->seq_end, conn->compare_seq)) { 450 trace_colo_compare_main("sec: this packet has compared"); 451 packet_destroy(spkt, NULL); 452 if (!ppkt) { 453 goto pri; 454 } else { 455 goto sec; 456 } 457 } 458 if (!ppkt) { 459 g_queue_push_head(&conn->secondary_list, spkt); 460 goto pri; 461 } 462 } 463 464 if (colo_mark_tcp_pkt(ppkt, spkt, &mark, min_ack)) { 465 trace_colo_compare_tcp_info("pri", 466 ppkt->tcp_seq, ppkt->tcp_ack, 467 ppkt->header_size, ppkt->payload_size, 468 ppkt->offset, ppkt->flags); 469 470 trace_colo_compare_tcp_info("sec", 471 spkt->tcp_seq, spkt->tcp_ack, 472 spkt->header_size, spkt->payload_size, 473 spkt->offset, spkt->flags); 474 475 if (mark == COLO_COMPARE_FREE_PRIMARY) { 476 conn->compare_seq = ppkt->seq_end; 477 colo_release_primary_pkt(s, ppkt); 478 g_queue_push_head(&conn->secondary_list, spkt); 479 goto pri; 480 } else if (mark == COLO_COMPARE_FREE_SECONDARY) { 481 conn->compare_seq = spkt->seq_end; 482 packet_destroy(spkt, NULL); 483 goto sec; 484 } else if (mark == (COLO_COMPARE_FREE_PRIMARY | COLO_COMPARE_FREE_SECONDARY)) { 485 conn->compare_seq = ppkt->seq_end; 486 colo_release_primary_pkt(s, ppkt); 487 packet_destroy(spkt, NULL); 488 goto pri; 489 } 490 } else { 491 g_queue_push_head(&conn->primary_list, ppkt); 492 g_queue_push_head(&conn->secondary_list, spkt); 493 494 #ifdef DEBUG_COLO_PACKETS 495 qemu_hexdump(stderr, "colo-compare ppkt", ppkt->data, ppkt->size); 496 qemu_hexdump(stderr, "colo-compare spkt", spkt->data, spkt->size); 497 #endif 498 499 colo_compare_inconsistency_notify(s); 500 } 501 } 502 503 504 /* 505 * Called from the compare thread on the primary 506 * for compare udp packet 507 */ 508 static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) 509 { 510 uint16_t network_header_length = ppkt->ip->ip_hl << 2; 511 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; 512 513 trace_colo_compare_main("compare udp"); 514 515 /* 516 * Because of ppkt and spkt are both in the same connection, 517 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are 518 * same with spkt. In addition, IP header's Identification is a random 519 * field, we can handle it in IP fragmentation function later. 520 * COLO just concern the response net packet payload from primary guest 521 * and secondary guest are same or not, So we ignored all IP header include 522 * other field like TOS,TTL,IP Checksum. we only need to compare 523 * the ip payload here. 524 */ 525 if (ppkt->size != spkt->size) { 526 trace_colo_compare_main("UDP: payload size of packets are different"); 527 return -1; 528 } 529 if (colo_compare_packet_payload(ppkt, spkt, offset, offset, 530 ppkt->size - offset)) { 531 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); 532 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); 533 #ifdef DEBUG_COLO_PACKETS 534 qemu_hexdump(stderr, "colo-compare pri pkt", ppkt->data, ppkt->size); 535 qemu_hexdump(stderr, "colo-compare sec pkt", spkt->data, spkt->size); 536 #endif 537 return -1; 538 } else { 539 return 0; 540 } 541 } 542 543 /* 544 * Called from the compare thread on the primary 545 * for compare icmp packet 546 */ 547 static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) 548 { 549 uint16_t network_header_length = ppkt->ip->ip_hl << 2; 550 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; 551 552 trace_colo_compare_main("compare icmp"); 553 554 /* 555 * Because of ppkt and spkt are both in the same connection, 556 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are 557 * same with spkt. In addition, IP header's Identification is a random 558 * field, we can handle it in IP fragmentation function later. 559 * COLO just concern the response net packet payload from primary guest 560 * and secondary guest are same or not, So we ignored all IP header include 561 * other field like TOS,TTL,IP Checksum. we only need to compare 562 * the ip payload here. 563 */ 564 if (ppkt->size != spkt->size) { 565 trace_colo_compare_main("ICMP: payload size of packets are different"); 566 return -1; 567 } 568 if (colo_compare_packet_payload(ppkt, spkt, offset, offset, 569 ppkt->size - offset)) { 570 trace_colo_compare_icmp_miscompare("primary pkt size", 571 ppkt->size); 572 trace_colo_compare_icmp_miscompare("Secondary pkt size", 573 spkt->size); 574 #ifdef DEBUG_COLO_PACKETS 575 qemu_hexdump(stderr, "colo-compare pri pkt", ppkt->data, ppkt->size); 576 qemu_hexdump(stderr, "colo-compare sec pkt", spkt->data, spkt->size); 577 #endif 578 return -1; 579 } else { 580 return 0; 581 } 582 } 583 584 /* 585 * Called from the compare thread on the primary 586 * for compare other packet 587 */ 588 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) 589 { 590 uint16_t offset = ppkt->vnet_hdr_len; 591 592 trace_colo_compare_main("compare other"); 593 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_IP_INFO)) { 594 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; 595 596 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); 597 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); 598 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); 599 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); 600 601 trace_colo_compare_ip_info(ppkt->size, pri_ip_src, 602 pri_ip_dst, spkt->size, 603 sec_ip_src, sec_ip_dst); 604 } 605 606 if (ppkt->size != spkt->size) { 607 trace_colo_compare_main("Other: payload size of packets are different"); 608 return -1; 609 } 610 return colo_compare_packet_payload(ppkt, spkt, offset, offset, 611 ppkt->size - offset); 612 } 613 614 static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) 615 { 616 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); 617 618 if ((now - pkt->creation_ms) > (*check_time)) { 619 trace_colo_old_packet_check_found(pkt->creation_ms); 620 return 0; 621 } else { 622 return 1; 623 } 624 } 625 626 void colo_compare_register_notifier(Notifier *notify) 627 { 628 notifier_list_add(&colo_compare_notifiers, notify); 629 } 630 631 void colo_compare_unregister_notifier(Notifier *notify) 632 { 633 notifier_remove(notify); 634 } 635 636 static int colo_old_packet_check_one_conn(Connection *conn, 637 CompareState *s) 638 { 639 GList *result = NULL; 640 641 result = g_queue_find_custom(&conn->primary_list, 642 &s->compare_timeout, 643 (GCompareFunc)colo_old_packet_check_one); 644 645 if (result) { 646 /* Do checkpoint will flush old packet */ 647 colo_compare_inconsistency_notify(s); 648 return 0; 649 } 650 651 return 1; 652 } 653 654 /* 655 * Look for old packets that the secondary hasn't matched, 656 * if we have some then we have to checkpoint to wake 657 * the secondary up. 658 */ 659 static void colo_old_packet_check(void *opaque) 660 { 661 CompareState *s = opaque; 662 663 /* 664 * If we find one old packet, stop finding job and notify 665 * COLO frame do checkpoint. 666 */ 667 g_queue_find_custom(&s->conn_list, s, 668 (GCompareFunc)colo_old_packet_check_one_conn); 669 } 670 671 static void colo_compare_packet(CompareState *s, Connection *conn, 672 int (*HandlePacket)(Packet *spkt, 673 Packet *ppkt)) 674 { 675 Packet *pkt = NULL; 676 GList *result = NULL; 677 678 while (!g_queue_is_empty(&conn->primary_list) && 679 !g_queue_is_empty(&conn->secondary_list)) { 680 pkt = g_queue_pop_head(&conn->primary_list); 681 result = g_queue_find_custom(&conn->secondary_list, 682 pkt, (GCompareFunc)HandlePacket); 683 684 if (result) { 685 colo_release_primary_pkt(s, pkt); 686 g_queue_remove(&conn->secondary_list, result->data); 687 } else { 688 /* 689 * If one packet arrive late, the secondary_list or 690 * primary_list will be empty, so we can't compare it 691 * until next comparison. If the packets in the list are 692 * timeout, it will trigger a checkpoint request. 693 */ 694 trace_colo_compare_main("packet different"); 695 g_queue_push_head(&conn->primary_list, pkt); 696 697 colo_compare_inconsistency_notify(s); 698 break; 699 } 700 } 701 } 702 703 /* 704 * Called from the compare thread on the primary 705 * for compare packet with secondary list of the 706 * specified connection when a new packet was 707 * queued to it. 708 */ 709 static void colo_compare_connection(void *opaque, void *user_data) 710 { 711 CompareState *s = user_data; 712 Connection *conn = opaque; 713 714 switch (conn->ip_proto) { 715 case IPPROTO_TCP: 716 colo_compare_tcp(s, conn); 717 break; 718 case IPPROTO_UDP: 719 colo_compare_packet(s, conn, colo_packet_compare_udp); 720 break; 721 case IPPROTO_ICMP: 722 colo_compare_packet(s, conn, colo_packet_compare_icmp); 723 break; 724 default: 725 colo_compare_packet(s, conn, colo_packet_compare_other); 726 break; 727 } 728 } 729 730 static void coroutine_fn _compare_chr_send(void *opaque) 731 { 732 SendCo *sendco = opaque; 733 CompareState *s = sendco->s; 734 int ret = 0; 735 736 while (!g_queue_is_empty(&sendco->send_list)) { 737 SendEntry *entry = g_queue_pop_tail(&sendco->send_list); 738 uint32_t len = htonl(entry->size); 739 740 ret = qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof(len)); 741 742 if (ret != sizeof(len)) { 743 g_free(entry->buf); 744 g_slice_free(SendEntry, entry); 745 goto err; 746 } 747 748 if (!sendco->notify_remote_frame && s->vnet_hdr) { 749 /* 750 * We send vnet header len make other module(like filter-redirector) 751 * know how to parse net packet correctly. 752 */ 753 len = htonl(entry->vnet_hdr_len); 754 755 ret = qemu_chr_fe_write_all(sendco->chr, 756 (uint8_t *)&len, 757 sizeof(len)); 758 759 if (ret != sizeof(len)) { 760 g_free(entry->buf); 761 g_slice_free(SendEntry, entry); 762 goto err; 763 } 764 } 765 766 ret = qemu_chr_fe_write_all(sendco->chr, 767 (uint8_t *)entry->buf, 768 entry->size); 769 770 if (ret != entry->size) { 771 g_free(entry->buf); 772 g_slice_free(SendEntry, entry); 773 goto err; 774 } 775 776 g_free(entry->buf); 777 g_slice_free(SendEntry, entry); 778 } 779 780 sendco->ret = 0; 781 goto out; 782 783 err: 784 while (!g_queue_is_empty(&sendco->send_list)) { 785 SendEntry *entry = g_queue_pop_tail(&sendco->send_list); 786 g_free(entry->buf); 787 g_slice_free(SendEntry, entry); 788 } 789 sendco->ret = ret < 0 ? ret : -EIO; 790 out: 791 sendco->co = NULL; 792 sendco->done = true; 793 aio_wait_kick(); 794 } 795 796 static int compare_chr_send(CompareState *s, 797 uint8_t *buf, 798 uint32_t size, 799 uint32_t vnet_hdr_len, 800 bool notify_remote_frame, 801 bool zero_copy) 802 { 803 SendCo *sendco; 804 SendEntry *entry; 805 806 if (notify_remote_frame) { 807 sendco = &s->notify_sendco; 808 } else { 809 sendco = &s->out_sendco; 810 } 811 812 if (!size) { 813 return 0; 814 } 815 816 entry = g_slice_new(SendEntry); 817 entry->size = size; 818 entry->vnet_hdr_len = vnet_hdr_len; 819 if (zero_copy) { 820 entry->buf = buf; 821 } else { 822 entry->buf = g_malloc(size); 823 memcpy(entry->buf, buf, size); 824 } 825 g_queue_push_head(&sendco->send_list, entry); 826 827 if (sendco->done) { 828 sendco->co = qemu_coroutine_create(_compare_chr_send, sendco); 829 sendco->done = false; 830 qemu_coroutine_enter(sendco->co); 831 if (sendco->done) { 832 /* report early errors */ 833 return sendco->ret; 834 } 835 } 836 837 /* assume success */ 838 return 0; 839 } 840 841 static int compare_chr_can_read(void *opaque) 842 { 843 return COMPARE_READ_LEN_MAX; 844 } 845 846 /* 847 * Called from the main thread on the primary for packets 848 * arriving over the socket from the primary. 849 */ 850 static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) 851 { 852 CompareState *s = COLO_COMPARE(opaque); 853 int ret; 854 855 ret = net_fill_rstate(&s->pri_rs, buf, size); 856 if (ret == -1) { 857 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL, 858 NULL, NULL, true); 859 error_report("colo-compare primary_in error"); 860 } 861 } 862 863 /* 864 * Called from the main thread on the primary for packets 865 * arriving over the socket from the secondary. 866 */ 867 static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size) 868 { 869 CompareState *s = COLO_COMPARE(opaque); 870 int ret; 871 872 ret = net_fill_rstate(&s->sec_rs, buf, size); 873 if (ret == -1) { 874 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL, 875 NULL, NULL, true); 876 error_report("colo-compare secondary_in error"); 877 } 878 } 879 880 static void compare_notify_chr(void *opaque, const uint8_t *buf, int size) 881 { 882 CompareState *s = COLO_COMPARE(opaque); 883 int ret; 884 885 ret = net_fill_rstate(&s->notify_rs, buf, size); 886 if (ret == -1) { 887 qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL, 888 NULL, NULL, true); 889 error_report("colo-compare notify_dev error"); 890 } 891 } 892 893 /* 894 * Check old packet regularly so it can watch for any packets 895 * that the secondary hasn't produced equivalents of. 896 */ 897 static void check_old_packet_regular(void *opaque) 898 { 899 CompareState *s = opaque; 900 901 /* if have old packet we will notify checkpoint */ 902 colo_old_packet_check(s); 903 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 904 s->expired_scan_cycle); 905 } 906 907 /* Public API, Used for COLO frame to notify compare event */ 908 void colo_notify_compares_event(void *opaque, int event, Error **errp) 909 { 910 CompareState *s; 911 qemu_mutex_lock(&colo_compare_mutex); 912 913 if (!colo_compare_active) { 914 qemu_mutex_unlock(&colo_compare_mutex); 915 return; 916 } 917 918 qemu_mutex_lock(&event_mtx); 919 QTAILQ_FOREACH(s, &net_compares, next) { 920 s->event = event; 921 qemu_bh_schedule(s->event_bh); 922 event_unhandled_count++; 923 } 924 /* Wait all compare threads to finish handling this event */ 925 while (event_unhandled_count > 0) { 926 qemu_cond_wait(&event_complete_cond, &event_mtx); 927 } 928 929 qemu_mutex_unlock(&event_mtx); 930 qemu_mutex_unlock(&colo_compare_mutex); 931 } 932 933 static void colo_compare_timer_init(CompareState *s) 934 { 935 AioContext *ctx = iothread_get_aio_context(s->iothread); 936 937 s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL, 938 SCALE_MS, check_old_packet_regular, 939 s); 940 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 941 s->expired_scan_cycle); 942 } 943 944 static void colo_compare_timer_del(CompareState *s) 945 { 946 if (s->packet_check_timer) { 947 timer_del(s->packet_check_timer); 948 timer_free(s->packet_check_timer); 949 s->packet_check_timer = NULL; 950 } 951 } 952 953 static void colo_flush_packets(void *opaque, void *user_data); 954 955 static void colo_compare_handle_event(void *opaque) 956 { 957 CompareState *s = opaque; 958 959 switch (s->event) { 960 case COLO_EVENT_CHECKPOINT: 961 g_queue_foreach(&s->conn_list, colo_flush_packets, s); 962 break; 963 case COLO_EVENT_FAILOVER: 964 break; 965 default: 966 break; 967 } 968 969 qemu_mutex_lock(&event_mtx); 970 assert(event_unhandled_count > 0); 971 event_unhandled_count--; 972 qemu_cond_broadcast(&event_complete_cond); 973 qemu_mutex_unlock(&event_mtx); 974 } 975 976 static void colo_compare_iothread(CompareState *s) 977 { 978 AioContext *ctx = iothread_get_aio_context(s->iothread); 979 object_ref(OBJECT(s->iothread)); 980 s->worker_context = iothread_get_g_main_context(s->iothread); 981 982 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, 983 compare_pri_chr_in, NULL, NULL, 984 s, s->worker_context, true); 985 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, 986 compare_sec_chr_in, NULL, NULL, 987 s, s->worker_context, true); 988 if (s->notify_dev) { 989 qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read, 990 compare_notify_chr, NULL, NULL, 991 s, s->worker_context, true); 992 } 993 994 colo_compare_timer_init(s); 995 s->event_bh = aio_bh_new(ctx, colo_compare_handle_event, s); 996 } 997 998 static char *compare_get_pri_indev(Object *obj, Error **errp) 999 { 1000 CompareState *s = COLO_COMPARE(obj); 1001 1002 return g_strdup(s->pri_indev); 1003 } 1004 1005 static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) 1006 { 1007 CompareState *s = COLO_COMPARE(obj); 1008 1009 g_free(s->pri_indev); 1010 s->pri_indev = g_strdup(value); 1011 } 1012 1013 static char *compare_get_sec_indev(Object *obj, Error **errp) 1014 { 1015 CompareState *s = COLO_COMPARE(obj); 1016 1017 return g_strdup(s->sec_indev); 1018 } 1019 1020 static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) 1021 { 1022 CompareState *s = COLO_COMPARE(obj); 1023 1024 g_free(s->sec_indev); 1025 s->sec_indev = g_strdup(value); 1026 } 1027 1028 static char *compare_get_outdev(Object *obj, Error **errp) 1029 { 1030 CompareState *s = COLO_COMPARE(obj); 1031 1032 return g_strdup(s->outdev); 1033 } 1034 1035 static void compare_set_outdev(Object *obj, const char *value, Error **errp) 1036 { 1037 CompareState *s = COLO_COMPARE(obj); 1038 1039 g_free(s->outdev); 1040 s->outdev = g_strdup(value); 1041 } 1042 1043 static bool compare_get_vnet_hdr(Object *obj, Error **errp) 1044 { 1045 CompareState *s = COLO_COMPARE(obj); 1046 1047 return s->vnet_hdr; 1048 } 1049 1050 static void compare_set_vnet_hdr(Object *obj, 1051 bool value, 1052 Error **errp) 1053 { 1054 CompareState *s = COLO_COMPARE(obj); 1055 1056 s->vnet_hdr = value; 1057 } 1058 1059 static char *compare_get_notify_dev(Object *obj, Error **errp) 1060 { 1061 CompareState *s = COLO_COMPARE(obj); 1062 1063 return g_strdup(s->notify_dev); 1064 } 1065 1066 static void compare_set_notify_dev(Object *obj, const char *value, Error **errp) 1067 { 1068 CompareState *s = COLO_COMPARE(obj); 1069 1070 g_free(s->notify_dev); 1071 s->notify_dev = g_strdup(value); 1072 } 1073 1074 static void compare_get_timeout(Object *obj, Visitor *v, 1075 const char *name, void *opaque, 1076 Error **errp) 1077 { 1078 CompareState *s = COLO_COMPARE(obj); 1079 uint64_t value = s->compare_timeout; 1080 1081 visit_type_uint64(v, name, &value, errp); 1082 } 1083 1084 static void compare_set_timeout(Object *obj, Visitor *v, 1085 const char *name, void *opaque, 1086 Error **errp) 1087 { 1088 CompareState *s = COLO_COMPARE(obj); 1089 uint32_t value; 1090 1091 if (!visit_type_uint32(v, name, &value, errp)) { 1092 return; 1093 } 1094 if (!value) { 1095 error_setg(errp, "Property '%s.%s' requires a positive value", 1096 object_get_typename(obj), name); 1097 return; 1098 } 1099 s->compare_timeout = value; 1100 } 1101 1102 static void compare_get_expired_scan_cycle(Object *obj, Visitor *v, 1103 const char *name, void *opaque, 1104 Error **errp) 1105 { 1106 CompareState *s = COLO_COMPARE(obj); 1107 uint32_t value = s->expired_scan_cycle; 1108 1109 visit_type_uint32(v, name, &value, errp); 1110 } 1111 1112 static void compare_set_expired_scan_cycle(Object *obj, Visitor *v, 1113 const char *name, void *opaque, 1114 Error **errp) 1115 { 1116 CompareState *s = COLO_COMPARE(obj); 1117 uint32_t value; 1118 1119 if (!visit_type_uint32(v, name, &value, errp)) { 1120 return; 1121 } 1122 if (!value) { 1123 error_setg(errp, "Property '%s.%s' requires a positive value", 1124 object_get_typename(obj), name); 1125 return; 1126 } 1127 s->expired_scan_cycle = value; 1128 } 1129 1130 static void get_max_queue_size(Object *obj, Visitor *v, 1131 const char *name, void *opaque, 1132 Error **errp) 1133 { 1134 uint32_t value = max_queue_size; 1135 1136 visit_type_uint32(v, name, &value, errp); 1137 } 1138 1139 static void set_max_queue_size(Object *obj, Visitor *v, 1140 const char *name, void *opaque, 1141 Error **errp) 1142 { 1143 Error *local_err = NULL; 1144 uint64_t value; 1145 1146 visit_type_uint64(v, name, &value, &local_err); 1147 if (local_err) { 1148 goto out; 1149 } 1150 if (!value) { 1151 error_setg(&local_err, "Property '%s.%s' requires a positive value", 1152 object_get_typename(obj), name); 1153 goto out; 1154 } 1155 max_queue_size = value; 1156 1157 out: 1158 error_propagate(errp, local_err); 1159 } 1160 1161 static void compare_pri_rs_finalize(SocketReadState *pri_rs) 1162 { 1163 CompareState *s = container_of(pri_rs, CompareState, pri_rs); 1164 Connection *conn = NULL; 1165 1166 if (packet_enqueue(s, PRIMARY_IN, &conn)) { 1167 trace_colo_compare_main("primary: unsupported packet in"); 1168 compare_chr_send(s, 1169 pri_rs->buf, 1170 pri_rs->packet_len, 1171 pri_rs->vnet_hdr_len, 1172 false, 1173 false); 1174 } else { 1175 /* compare packet in the specified connection */ 1176 colo_compare_connection(conn, s); 1177 } 1178 } 1179 1180 static void compare_sec_rs_finalize(SocketReadState *sec_rs) 1181 { 1182 CompareState *s = container_of(sec_rs, CompareState, sec_rs); 1183 Connection *conn = NULL; 1184 1185 if (packet_enqueue(s, SECONDARY_IN, &conn)) { 1186 trace_colo_compare_main("secondary: unsupported packet in"); 1187 } else { 1188 /* compare packet in the specified connection */ 1189 colo_compare_connection(conn, s); 1190 } 1191 } 1192 1193 static void compare_notify_rs_finalize(SocketReadState *notify_rs) 1194 { 1195 CompareState *s = container_of(notify_rs, CompareState, notify_rs); 1196 1197 const char msg[] = "COLO_COMPARE_GET_XEN_INIT"; 1198 int ret; 1199 1200 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT", 1201 notify_rs->buf, 1202 notify_rs->packet_len)) { 1203 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false); 1204 if (ret < 0) { 1205 error_report("Notify Xen COLO-frame INIT failed"); 1206 } 1207 } else if (packet_matches_str("COLO_CHECKPOINT", 1208 notify_rs->buf, 1209 notify_rs->packet_len)) { 1210 /* colo-compare do checkpoint, flush pri packet and remove sec packet */ 1211 g_queue_foreach(&s->conn_list, colo_flush_packets, s); 1212 } else { 1213 error_report("COLO compare got unsupported instruction"); 1214 } 1215 } 1216 1217 /* 1218 * Return 0 is success. 1219 * Return 1 is failed. 1220 */ 1221 static int find_and_check_chardev(Chardev **chr, 1222 char *chr_name, 1223 Error **errp) 1224 { 1225 *chr = qemu_chr_find(chr_name); 1226 if (*chr == NULL) { 1227 error_setg(errp, "Device '%s' not found", 1228 chr_name); 1229 return 1; 1230 } 1231 1232 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { 1233 error_setg(errp, "chardev \"%s\" is not reconnectable", 1234 chr_name); 1235 return 1; 1236 } 1237 1238 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) { 1239 error_setg(errp, "chardev \"%s\" cannot switch context", 1240 chr_name); 1241 return 1; 1242 } 1243 1244 return 0; 1245 } 1246 1247 /* 1248 * Called from the main thread on the primary 1249 * to setup colo-compare. 1250 */ 1251 static void colo_compare_complete(UserCreatable *uc, Error **errp) 1252 { 1253 CompareState *s = COLO_COMPARE(uc); 1254 Chardev *chr; 1255 1256 if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) { 1257 error_setg(errp, "colo compare needs 'primary_in' ," 1258 "'secondary_in','outdev','iothread' property set"); 1259 return; 1260 } else if (!strcmp(s->pri_indev, s->outdev) || 1261 !strcmp(s->sec_indev, s->outdev) || 1262 !strcmp(s->pri_indev, s->sec_indev)) { 1263 error_setg(errp, "'indev' and 'outdev' could not be same " 1264 "for compare module"); 1265 return; 1266 } 1267 1268 if (!s->compare_timeout) { 1269 /* Set default value to 3000 MS */ 1270 s->compare_timeout = DEFAULT_TIME_OUT_MS; 1271 } 1272 1273 if (!s->expired_scan_cycle) { 1274 /* Set default value to 3000 MS */ 1275 s->expired_scan_cycle = REGULAR_PACKET_CHECK_MS; 1276 } 1277 1278 if (!max_queue_size) { 1279 /* Set default queue size to 1024 */ 1280 max_queue_size = MAX_QUEUE_SIZE; 1281 } 1282 1283 if (find_and_check_chardev(&chr, s->pri_indev, errp) || 1284 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { 1285 return; 1286 } 1287 1288 if (find_and_check_chardev(&chr, s->sec_indev, errp) || 1289 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { 1290 return; 1291 } 1292 1293 if (find_and_check_chardev(&chr, s->outdev, errp) || 1294 !qemu_chr_fe_init(&s->chr_out, chr, errp)) { 1295 return; 1296 } 1297 1298 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr); 1299 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr); 1300 1301 /* Try to enable remote notify chardev, currently just for Xen COLO */ 1302 if (s->notify_dev) { 1303 if (find_and_check_chardev(&chr, s->notify_dev, errp) || 1304 !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) { 1305 return; 1306 } 1307 1308 net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize, 1309 s->vnet_hdr); 1310 } 1311 1312 s->out_sendco.s = s; 1313 s->out_sendco.chr = &s->chr_out; 1314 s->out_sendco.notify_remote_frame = false; 1315 s->out_sendco.done = true; 1316 g_queue_init(&s->out_sendco.send_list); 1317 1318 if (s->notify_dev) { 1319 s->notify_sendco.s = s; 1320 s->notify_sendco.chr = &s->chr_notify_dev; 1321 s->notify_sendco.notify_remote_frame = true; 1322 s->notify_sendco.done = true; 1323 g_queue_init(&s->notify_sendco.send_list); 1324 } 1325 1326 g_queue_init(&s->conn_list); 1327 1328 s->connection_track_table = g_hash_table_new_full(connection_key_hash, 1329 connection_key_equal, 1330 g_free, 1331 connection_destroy); 1332 1333 colo_compare_iothread(s); 1334 1335 qemu_mutex_lock(&colo_compare_mutex); 1336 if (!colo_compare_active) { 1337 qemu_mutex_init(&event_mtx); 1338 qemu_cond_init(&event_complete_cond); 1339 colo_compare_active = true; 1340 } 1341 QTAILQ_INSERT_TAIL(&net_compares, s, next); 1342 qemu_mutex_unlock(&colo_compare_mutex); 1343 1344 return; 1345 } 1346 1347 static void colo_flush_packets(void *opaque, void *user_data) 1348 { 1349 CompareState *s = user_data; 1350 Connection *conn = opaque; 1351 Packet *pkt = NULL; 1352 1353 while (!g_queue_is_empty(&conn->primary_list)) { 1354 pkt = g_queue_pop_head(&conn->primary_list); 1355 compare_chr_send(s, 1356 pkt->data, 1357 pkt->size, 1358 pkt->vnet_hdr_len, 1359 false, 1360 true); 1361 packet_destroy_partial(pkt, NULL); 1362 } 1363 while (!g_queue_is_empty(&conn->secondary_list)) { 1364 pkt = g_queue_pop_head(&conn->secondary_list); 1365 packet_destroy(pkt, NULL); 1366 } 1367 } 1368 1369 static void colo_compare_class_init(ObjectClass *oc, void *data) 1370 { 1371 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 1372 1373 ucc->complete = colo_compare_complete; 1374 } 1375 1376 static void colo_compare_init(Object *obj) 1377 { 1378 CompareState *s = COLO_COMPARE(obj); 1379 1380 object_property_add_str(obj, "primary_in", 1381 compare_get_pri_indev, compare_set_pri_indev); 1382 object_property_add_str(obj, "secondary_in", 1383 compare_get_sec_indev, compare_set_sec_indev); 1384 object_property_add_str(obj, "outdev", 1385 compare_get_outdev, compare_set_outdev); 1386 object_property_add_link(obj, "iothread", TYPE_IOTHREAD, 1387 (Object **)&s->iothread, 1388 object_property_allow_set_link, 1389 OBJ_PROP_LINK_STRONG); 1390 /* This parameter just for Xen COLO */ 1391 object_property_add_str(obj, "notify_dev", 1392 compare_get_notify_dev, compare_set_notify_dev); 1393 1394 object_property_add(obj, "compare_timeout", "uint64", 1395 compare_get_timeout, 1396 compare_set_timeout, NULL, NULL); 1397 1398 object_property_add(obj, "expired_scan_cycle", "uint32", 1399 compare_get_expired_scan_cycle, 1400 compare_set_expired_scan_cycle, NULL, NULL); 1401 1402 object_property_add(obj, "max_queue_size", "uint32", 1403 get_max_queue_size, 1404 set_max_queue_size, NULL, NULL); 1405 1406 s->vnet_hdr = false; 1407 object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr, 1408 compare_set_vnet_hdr); 1409 } 1410 1411 static void colo_compare_finalize(Object *obj) 1412 { 1413 CompareState *s = COLO_COMPARE(obj); 1414 CompareState *tmp = NULL; 1415 1416 qemu_mutex_lock(&colo_compare_mutex); 1417 QTAILQ_FOREACH(tmp, &net_compares, next) { 1418 if (tmp == s) { 1419 QTAILQ_REMOVE(&net_compares, s, next); 1420 break; 1421 } 1422 } 1423 if (QTAILQ_EMPTY(&net_compares)) { 1424 colo_compare_active = false; 1425 qemu_mutex_destroy(&event_mtx); 1426 qemu_cond_destroy(&event_complete_cond); 1427 } 1428 qemu_mutex_unlock(&colo_compare_mutex); 1429 1430 qemu_chr_fe_deinit(&s->chr_pri_in, false); 1431 qemu_chr_fe_deinit(&s->chr_sec_in, false); 1432 qemu_chr_fe_deinit(&s->chr_out, false); 1433 if (s->notify_dev) { 1434 qemu_chr_fe_deinit(&s->chr_notify_dev, false); 1435 } 1436 1437 colo_compare_timer_del(s); 1438 1439 qemu_bh_delete(s->event_bh); 1440 1441 AioContext *ctx = iothread_get_aio_context(s->iothread); 1442 aio_context_acquire(ctx); 1443 AIO_WAIT_WHILE(ctx, !s->out_sendco.done); 1444 if (s->notify_dev) { 1445 AIO_WAIT_WHILE(ctx, !s->notify_sendco.done); 1446 } 1447 aio_context_release(ctx); 1448 1449 /* Release all unhandled packets after compare thead exited */ 1450 g_queue_foreach(&s->conn_list, colo_flush_packets, s); 1451 AIO_WAIT_WHILE(NULL, !s->out_sendco.done); 1452 1453 g_queue_clear(&s->conn_list); 1454 g_queue_clear(&s->out_sendco.send_list); 1455 if (s->notify_dev) { 1456 g_queue_clear(&s->notify_sendco.send_list); 1457 } 1458 1459 if (s->connection_track_table) { 1460 g_hash_table_destroy(s->connection_track_table); 1461 } 1462 1463 object_unref(OBJECT(s->iothread)); 1464 1465 g_free(s->pri_indev); 1466 g_free(s->sec_indev); 1467 g_free(s->outdev); 1468 g_free(s->notify_dev); 1469 } 1470 1471 static void __attribute__((__constructor__)) colo_compare_init_globals(void) 1472 { 1473 colo_compare_active = false; 1474 qemu_mutex_init(&colo_compare_mutex); 1475 } 1476 1477 static const TypeInfo colo_compare_info = { 1478 .name = TYPE_COLO_COMPARE, 1479 .parent = TYPE_OBJECT, 1480 .instance_size = sizeof(CompareState), 1481 .instance_init = colo_compare_init, 1482 .instance_finalize = colo_compare_finalize, 1483 .class_size = sizeof(CompareClass), 1484 .class_init = colo_compare_class_init, 1485 .interfaces = (InterfaceInfo[]) { 1486 { TYPE_USER_CREATABLE }, 1487 { } 1488 } 1489 }; 1490 1491 static void register_types(void) 1492 { 1493 type_register_static(&colo_compare_info); 1494 } 1495 1496 type_init(register_types); 1497