qemu/net/colo-compare.c
<<
>>
Prefs
   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/error-report.h"
  17#include "trace.h"
  18#include "qemu-common.h"
  19#include "qapi/qmp/qerror.h"
  20#include "qapi/error.h"
  21#include "net/net.h"
  22#include "net/eth.h"
  23#include "qom/object_interfaces.h"
  24#include "qemu/iov.h"
  25#include "qom/object.h"
  26#include "qemu/typedefs.h"
  27#include "net/queue.h"
  28#include "sysemu/char.h"
  29#include "qemu/sockets.h"
  30#include "qapi-visit.h"
  31#include "net/colo.h"
  32
  33#define TYPE_COLO_COMPARE "colo-compare"
  34#define COLO_COMPARE(obj) \
  35    OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
  36
  37#define COMPARE_READ_LEN_MAX NET_BUFSIZE
  38#define MAX_QUEUE_SIZE 1024
  39
  40/* TODO: Should be configurable */
  41#define REGULAR_PACKET_CHECK_MS 3000
  42
  43/*
  44  + CompareState ++
  45  |               |
  46  +---------------+   +---------------+         +---------------+
  47  |conn list      +--->conn           +--------->conn           |
  48  +---------------+   +---------------+         +---------------+
  49  |               |     |           |             |          |
  50  +---------------+ +---v----+  +---v----+    +---v----+ +---v----+
  51                    |primary |  |secondary    |primary | |secondary
  52                    |packet  |  |packet  +    |packet  | |packet  +
  53                    +--------+  +--------+    +--------+ +--------+
  54                        |           |             |          |
  55                    +---v----+  +---v----+    +---v----+ +---v----+
  56                    |primary |  |secondary    |primary | |secondary
  57                    |packet  |  |packet  +    |packet  | |packet  +
  58                    +--------+  +--------+    +--------+ +--------+
  59                        |           |             |          |
  60                    +---v----+  +---v----+    +---v----+ +---v----+
  61                    |primary |  |secondary    |primary | |secondary
  62                    |packet  |  |packet  +    |packet  | |packet  +
  63                    +--------+  +--------+    +--------+ +--------+
  64*/
  65typedef struct CompareState {
  66    Object parent;
  67
  68    char *pri_indev;
  69    char *sec_indev;
  70    char *outdev;
  71    CharBackend chr_pri_in;
  72    CharBackend chr_sec_in;
  73    CharBackend chr_out;
  74    SocketReadState pri_rs;
  75    SocketReadState sec_rs;
  76
  77    /* connection list: the connections belonged to this NIC could be found
  78     * in this list.
  79     * element type: Connection
  80     */
  81    GQueue conn_list;
  82    /* hashtable to save connection */
  83    GHashTable *connection_track_table;
  84    /* compare thread, a thread for each NIC */
  85    QemuThread thread;
  86
  87    GMainContext *worker_context;
  88    GMainLoop *compare_loop;
  89} CompareState;
  90
  91typedef struct CompareClass {
  92    ObjectClass parent_class;
  93} CompareClass;
  94
  95enum {
  96    PRIMARY_IN = 0,
  97    SECONDARY_IN,
  98};
  99
 100static int compare_chr_send(CharBackend *out,
 101                            const uint8_t *buf,
 102                            uint32_t size);
 103
 104static gint seq_sorter(Packet *a, Packet *b, gpointer data)
 105{
 106    struct tcphdr *atcp, *btcp;
 107
 108    atcp = (struct tcphdr *)(a->transport_header);
 109    btcp = (struct tcphdr *)(b->transport_header);
 110    return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
 111}
 112
 113/*
 114 * Return 0 on success, if return -1 means the pkt
 115 * is unsupported(arp and ipv6) and will be sent later
 116 */
 117static int packet_enqueue(CompareState *s, int mode)
 118{
 119    ConnectionKey key;
 120    Packet *pkt = NULL;
 121    Connection *conn;
 122
 123    if (mode == PRIMARY_IN) {
 124        pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len);
 125    } else {
 126        pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len);
 127    }
 128
 129    if (parse_packet_early(pkt)) {
 130        packet_destroy(pkt, NULL);
 131        pkt = NULL;
 132        return -1;
 133    }
 134    fill_connection_key(pkt, &key);
 135
 136    conn = connection_get(s->connection_track_table,
 137                          &key,
 138                          &s->conn_list);
 139
 140    if (!conn->processing) {
 141        g_queue_push_tail(&s->conn_list, conn);
 142        conn->processing = true;
 143    }
 144
 145    if (mode == PRIMARY_IN) {
 146        if (g_queue_get_length(&conn->primary_list) <=
 147                               MAX_QUEUE_SIZE) {
 148            g_queue_push_tail(&conn->primary_list, pkt);
 149            if (conn->ip_proto == IPPROTO_TCP) {
 150                g_queue_sort(&conn->primary_list,
 151                             (GCompareDataFunc)seq_sorter,
 152                             NULL);
 153            }
 154        } else {
 155            error_report("colo compare primary queue size too big,"
 156                         "drop packet");
 157        }
 158    } else {
 159        if (g_queue_get_length(&conn->secondary_list) <=
 160                               MAX_QUEUE_SIZE) {
 161            g_queue_push_tail(&conn->secondary_list, pkt);
 162            if (conn->ip_proto == IPPROTO_TCP) {
 163                g_queue_sort(&conn->secondary_list,
 164                             (GCompareDataFunc)seq_sorter,
 165                             NULL);
 166            }
 167        } else {
 168            error_report("colo compare secondary queue size too big,"
 169                         "drop packet");
 170        }
 171    }
 172
 173    return 0;
 174}
 175
 176/*
 177 * The IP packets sent by primary and secondary
 178 * will be compared in here
 179 * TODO support ip fragment, Out-Of-Order
 180 * return:    0  means packet same
 181 *            > 0 || < 0 means packet different
 182 */
 183static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset)
 184{
 185    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
 186        char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 187
 188        strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
 189        strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
 190        strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
 191        strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
 192
 193        trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
 194                                   pri_ip_dst, spkt->size,
 195                                   sec_ip_src, sec_ip_dst);
 196    }
 197
 198    if (ppkt->size == spkt->size) {
 199        return memcmp(ppkt->data + offset, spkt->data + offset,
 200                      spkt->size - offset);
 201    } else {
 202        trace_colo_compare_main("Net packet size are not the same");
 203        return -1;
 204    }
 205}
 206
 207/*
 208 * Called from the compare thread on the primary
 209 * for compare tcp packet
 210 * compare_tcp copied from Dr. David Alan Gilbert's branch
 211 */
 212static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
 213{
 214    struct tcphdr *ptcp, *stcp;
 215    int res;
 216
 217    trace_colo_compare_main("compare tcp");
 218
 219    ptcp = (struct tcphdr *)ppkt->transport_header;
 220    stcp = (struct tcphdr *)spkt->transport_header;
 221
 222    /*
 223     * The 'identification' field in the IP header is *very* random
 224     * it almost never matches.  Fudge this by ignoring differences in
 225     * unfragmented packets; they'll normally sort themselves out if different
 226     * anyway, and it should recover at the TCP level.
 227     * An alternative would be to get both the primary and secondary to rewrite
 228     * somehow; but that would need some sync traffic to sync the state
 229     */
 230    if (ntohs(ppkt->ip->ip_off) & IP_DF) {
 231        spkt->ip->ip_id = ppkt->ip->ip_id;
 232        /* and the sum will be different if the IDs were different */
 233        spkt->ip->ip_sum = ppkt->ip->ip_sum;
 234    }
 235
 236    if (ptcp->th_sum == stcp->th_sum) {
 237        res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN);
 238    } else {
 239        res = -1;
 240    }
 241
 242    if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
 243        trace_colo_compare_pkt_info_src(inet_ntoa(ppkt->ip->ip_src),
 244                                        ntohl(stcp->th_seq),
 245                                        ntohl(stcp->th_ack),
 246                                        res, stcp->th_flags,
 247                                        spkt->size);
 248
 249        trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt->ip->ip_dst),
 250                                        ntohl(ptcp->th_seq),
 251                                        ntohl(ptcp->th_ack),
 252                                        res, ptcp->th_flags,
 253                                        ppkt->size);
 254
 255        qemu_hexdump((char *)ppkt->data, stderr,
 256                     "colo-compare ppkt", ppkt->size);
 257        qemu_hexdump((char *)spkt->data, stderr,
 258                     "colo-compare spkt", spkt->size);
 259    }
 260
 261    return res;
 262}
 263
 264/*
 265 * Called from the compare thread on the primary
 266 * for compare udp packet
 267 */
 268static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
 269{
 270    int ret;
 271    int network_header_length = ppkt->ip->ip_hl * 4;
 272
 273    trace_colo_compare_main("compare udp");
 274
 275    /*
 276     * Because of ppkt and spkt are both in the same connection,
 277     * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
 278     * same with spkt. In addition, IP header's Identification is a random
 279     * field, we can handle it in IP fragmentation function later.
 280     * COLO just concern the response net packet payload from primary guest
 281     * and secondary guest are same or not, So we ignored all IP header include
 282     * other field like TOS,TTL,IP Checksum. we only need to compare
 283     * the ip payload here.
 284     */
 285    ret = colo_packet_compare_common(ppkt, spkt,
 286                                     network_header_length + ETH_HLEN);
 287
 288    if (ret) {
 289        trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
 290        trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
 291        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
 292            qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
 293                         ppkt->size);
 294            qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
 295                         spkt->size);
 296        }
 297    }
 298
 299    return ret;
 300}
 301
 302/*
 303 * Called from the compare thread on the primary
 304 * for compare icmp packet
 305 */
 306static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
 307{
 308    int network_header_length = ppkt->ip->ip_hl * 4;
 309
 310    trace_colo_compare_main("compare icmp");
 311
 312    /*
 313     * Because of ppkt and spkt are both in the same connection,
 314     * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
 315     * same with spkt. In addition, IP header's Identification is a random
 316     * field, we can handle it in IP fragmentation function later.
 317     * COLO just concern the response net packet payload from primary guest
 318     * and secondary guest are same or not, So we ignored all IP header include
 319     * other field like TOS,TTL,IP Checksum. we only need to compare
 320     * the ip payload here.
 321     */
 322    if (colo_packet_compare_common(ppkt, spkt,
 323                                   network_header_length + ETH_HLEN)) {
 324        trace_colo_compare_icmp_miscompare("primary pkt size",
 325                                           ppkt->size);
 326        trace_colo_compare_icmp_miscompare("Secondary pkt size",
 327                                           spkt->size);
 328        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
 329            qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
 330                         ppkt->size);
 331            qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
 332                         spkt->size);
 333        }
 334        return -1;
 335    } else {
 336        return 0;
 337    }
 338}
 339
 340/*
 341 * Called from the compare thread on the primary
 342 * for compare other packet
 343 */
 344static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
 345{
 346    trace_colo_compare_main("compare other");
 347    if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
 348        char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
 349
 350        strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
 351        strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
 352        strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
 353        strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
 354
 355        trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
 356                                   pri_ip_dst, spkt->size,
 357                                   sec_ip_src, sec_ip_dst);
 358    }
 359
 360    return colo_packet_compare_common(ppkt, spkt, 0);
 361}
 362
 363static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
 364{
 365    int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
 366
 367    if ((now - pkt->creation_ms) > (*check_time)) {
 368        trace_colo_old_packet_check_found(pkt->creation_ms);
 369        return 0;
 370    } else {
 371        return 1;
 372    }
 373}
 374
 375static void colo_old_packet_check_one_conn(void *opaque,
 376                                           void *user_data)
 377{
 378    Connection *conn = opaque;
 379    GList *result = NULL;
 380    int64_t check_time = REGULAR_PACKET_CHECK_MS;
 381
 382    result = g_queue_find_custom(&conn->primary_list,
 383                                 &check_time,
 384                                 (GCompareFunc)colo_old_packet_check_one);
 385
 386    if (result) {
 387        /* do checkpoint will flush old packet */
 388        /* TODO: colo_notify_checkpoint();*/
 389    }
 390}
 391
 392/*
 393 * Look for old packets that the secondary hasn't matched,
 394 * if we have some then we have to checkpoint to wake
 395 * the secondary up.
 396 */
 397static void colo_old_packet_check(void *opaque)
 398{
 399    CompareState *s = opaque;
 400
 401    g_queue_foreach(&s->conn_list, colo_old_packet_check_one_conn, NULL);
 402}
 403
 404/*
 405 * Called from the compare thread on the primary
 406 * for compare connection
 407 */
 408static void colo_compare_connection(void *opaque, void *user_data)
 409{
 410    CompareState *s = user_data;
 411    Connection *conn = opaque;
 412    Packet *pkt = NULL;
 413    GList *result = NULL;
 414    int ret;
 415
 416    while (!g_queue_is_empty(&conn->primary_list) &&
 417           !g_queue_is_empty(&conn->secondary_list)) {
 418        pkt = g_queue_pop_tail(&conn->primary_list);
 419        switch (conn->ip_proto) {
 420        case IPPROTO_TCP:
 421            result = g_queue_find_custom(&conn->secondary_list,
 422                     pkt, (GCompareFunc)colo_packet_compare_tcp);
 423            break;
 424        case IPPROTO_UDP:
 425            result = g_queue_find_custom(&conn->secondary_list,
 426                     pkt, (GCompareFunc)colo_packet_compare_udp);
 427            break;
 428        case IPPROTO_ICMP:
 429            result = g_queue_find_custom(&conn->secondary_list,
 430                     pkt, (GCompareFunc)colo_packet_compare_icmp);
 431            break;
 432        default:
 433            result = g_queue_find_custom(&conn->secondary_list,
 434                     pkt, (GCompareFunc)colo_packet_compare_other);
 435            break;
 436        }
 437
 438        if (result) {
 439            ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
 440            if (ret < 0) {
 441                error_report("colo_send_primary_packet failed");
 442            }
 443            trace_colo_compare_main("packet same and release packet");
 444            g_queue_remove(&conn->secondary_list, result->data);
 445            packet_destroy(pkt, NULL);
 446        } else {
 447            /*
 448             * If one packet arrive late, the secondary_list or
 449             * primary_list will be empty, so we can't compare it
 450             * until next comparison.
 451             */
 452            trace_colo_compare_main("packet different");
 453            g_queue_push_tail(&conn->primary_list, pkt);
 454            /* TODO: colo_notify_checkpoint();*/
 455            break;
 456        }
 457    }
 458}
 459
 460static int compare_chr_send(CharBackend *out,
 461                            const uint8_t *buf,
 462                            uint32_t size)
 463{
 464    int ret = 0;
 465    uint32_t len = htonl(size);
 466
 467    if (!size) {
 468        return 0;
 469    }
 470
 471    ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
 472    if (ret != sizeof(len)) {
 473        goto err;
 474    }
 475
 476    ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
 477    if (ret != size) {
 478        goto err;
 479    }
 480
 481    return 0;
 482
 483err:
 484    return ret < 0 ? ret : -EIO;
 485}
 486
 487static int compare_chr_can_read(void *opaque)
 488{
 489    return COMPARE_READ_LEN_MAX;
 490}
 491
 492/*
 493 * Called from the main thread on the primary for packets
 494 * arriving over the socket from the primary.
 495 */
 496static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
 497{
 498    CompareState *s = COLO_COMPARE(opaque);
 499    int ret;
 500
 501    ret = net_fill_rstate(&s->pri_rs, buf, size);
 502    if (ret == -1) {
 503        qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
 504                                 NULL, NULL, true);
 505        error_report("colo-compare primary_in error");
 506    }
 507}
 508
 509/*
 510 * Called from the main thread on the primary for packets
 511 * arriving over the socket from the secondary.
 512 */
 513static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
 514{
 515    CompareState *s = COLO_COMPARE(opaque);
 516    int ret;
 517
 518    ret = net_fill_rstate(&s->sec_rs, buf, size);
 519    if (ret == -1) {
 520        qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
 521                                 NULL, NULL, true);
 522        error_report("colo-compare secondary_in error");
 523    }
 524}
 525
 526/*
 527 * Check old packet regularly so it can watch for any packets
 528 * that the secondary hasn't produced equivalents of.
 529 */
 530static gboolean check_old_packet_regular(void *opaque)
 531{
 532    CompareState *s = opaque;
 533
 534    /* if have old packet we will notify checkpoint */
 535    colo_old_packet_check(s);
 536
 537    return TRUE;
 538}
 539
 540static void *colo_compare_thread(void *opaque)
 541{
 542    CompareState *s = opaque;
 543    GSource *timeout_source;
 544
 545    s->worker_context = g_main_context_new();
 546
 547    qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
 548                          compare_pri_chr_in, NULL, s, s->worker_context, true);
 549    qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
 550                          compare_sec_chr_in, NULL, s, s->worker_context, true);
 551
 552    s->compare_loop = g_main_loop_new(s->worker_context, FALSE);
 553
 554    /* To kick any packets that the secondary doesn't match */
 555    timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS);
 556    g_source_set_callback(timeout_source,
 557                          (GSourceFunc)check_old_packet_regular, s, NULL);
 558    g_source_attach(timeout_source, s->worker_context);
 559
 560    g_main_loop_run(s->compare_loop);
 561
 562    g_source_unref(timeout_source);
 563    g_main_loop_unref(s->compare_loop);
 564    g_main_context_unref(s->worker_context);
 565    return NULL;
 566}
 567
 568static char *compare_get_pri_indev(Object *obj, Error **errp)
 569{
 570    CompareState *s = COLO_COMPARE(obj);
 571
 572    return g_strdup(s->pri_indev);
 573}
 574
 575static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
 576{
 577    CompareState *s = COLO_COMPARE(obj);
 578
 579    g_free(s->pri_indev);
 580    s->pri_indev = g_strdup(value);
 581}
 582
 583static char *compare_get_sec_indev(Object *obj, Error **errp)
 584{
 585    CompareState *s = COLO_COMPARE(obj);
 586
 587    return g_strdup(s->sec_indev);
 588}
 589
 590static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
 591{
 592    CompareState *s = COLO_COMPARE(obj);
 593
 594    g_free(s->sec_indev);
 595    s->sec_indev = g_strdup(value);
 596}
 597
 598static char *compare_get_outdev(Object *obj, Error **errp)
 599{
 600    CompareState *s = COLO_COMPARE(obj);
 601
 602    return g_strdup(s->outdev);
 603}
 604
 605static void compare_set_outdev(Object *obj, const char *value, Error **errp)
 606{
 607    CompareState *s = COLO_COMPARE(obj);
 608
 609    g_free(s->outdev);
 610    s->outdev = g_strdup(value);
 611}
 612
 613static void compare_pri_rs_finalize(SocketReadState *pri_rs)
 614{
 615    CompareState *s = container_of(pri_rs, CompareState, pri_rs);
 616
 617    if (packet_enqueue(s, PRIMARY_IN)) {
 618        trace_colo_compare_main("primary: unsupported packet in");
 619        compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
 620    } else {
 621        /* compare connection */
 622        g_queue_foreach(&s->conn_list, colo_compare_connection, s);
 623    }
 624}
 625
 626static void compare_sec_rs_finalize(SocketReadState *sec_rs)
 627{
 628    CompareState *s = container_of(sec_rs, CompareState, sec_rs);
 629
 630    if (packet_enqueue(s, SECONDARY_IN)) {
 631        trace_colo_compare_main("secondary: unsupported packet in");
 632    } else {
 633        /* compare connection */
 634        g_queue_foreach(&s->conn_list, colo_compare_connection, s);
 635    }
 636}
 637
 638
 639/*
 640 * Return 0 is success.
 641 * Return 1 is failed.
 642 */
 643static int find_and_check_chardev(Chardev **chr,
 644                                  char *chr_name,
 645                                  Error **errp)
 646{
 647    *chr = qemu_chr_find(chr_name);
 648    if (*chr == NULL) {
 649        error_setg(errp, "Device '%s' not found",
 650                   chr_name);
 651        return 1;
 652    }
 653
 654    if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
 655        error_setg(errp, "chardev \"%s\" is not reconnectable",
 656                   chr_name);
 657        return 1;
 658    }
 659
 660    return 0;
 661}
 662
 663/*
 664 * Called from the main thread on the primary
 665 * to setup colo-compare.
 666 */
 667static void colo_compare_complete(UserCreatable *uc, Error **errp)
 668{
 669    CompareState *s = COLO_COMPARE(uc);
 670    Chardev *chr;
 671    char thread_name[64];
 672    static int compare_id;
 673
 674    if (!s->pri_indev || !s->sec_indev || !s->outdev) {
 675        error_setg(errp, "colo compare needs 'primary_in' ,"
 676                   "'secondary_in','outdev' property set");
 677        return;
 678    } else if (!strcmp(s->pri_indev, s->outdev) ||
 679               !strcmp(s->sec_indev, s->outdev) ||
 680               !strcmp(s->pri_indev, s->sec_indev)) {
 681        error_setg(errp, "'indev' and 'outdev' could not be same "
 682                   "for compare module");
 683        return;
 684    }
 685
 686    if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
 687        !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
 688        return;
 689    }
 690
 691    if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
 692        !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
 693        return;
 694    }
 695
 696    if (find_and_check_chardev(&chr, s->outdev, errp) ||
 697        !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
 698        return;
 699    }
 700
 701    net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
 702    net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
 703
 704    g_queue_init(&s->conn_list);
 705
 706    s->connection_track_table = g_hash_table_new_full(connection_key_hash,
 707                                                      connection_key_equal,
 708                                                      g_free,
 709                                                      connection_destroy);
 710
 711    sprintf(thread_name, "colo-compare %d", compare_id);
 712    qemu_thread_create(&s->thread, thread_name,
 713                       colo_compare_thread, s,
 714                       QEMU_THREAD_JOINABLE);
 715    compare_id++;
 716
 717    return;
 718}
 719
 720static void colo_flush_packets(void *opaque, void *user_data)
 721{
 722    CompareState *s = user_data;
 723    Connection *conn = opaque;
 724    Packet *pkt = NULL;
 725
 726    while (!g_queue_is_empty(&conn->primary_list)) {
 727        pkt = g_queue_pop_head(&conn->primary_list);
 728        compare_chr_send(&s->chr_out, pkt->data, pkt->size);
 729        packet_destroy(pkt, NULL);
 730    }
 731    while (!g_queue_is_empty(&conn->secondary_list)) {
 732        pkt = g_queue_pop_head(&conn->secondary_list);
 733        packet_destroy(pkt, NULL);
 734    }
 735}
 736
 737static void colo_compare_class_init(ObjectClass *oc, void *data)
 738{
 739    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 740
 741    ucc->complete = colo_compare_complete;
 742}
 743
 744static void colo_compare_init(Object *obj)
 745{
 746    object_property_add_str(obj, "primary_in",
 747                            compare_get_pri_indev, compare_set_pri_indev,
 748                            NULL);
 749    object_property_add_str(obj, "secondary_in",
 750                            compare_get_sec_indev, compare_set_sec_indev,
 751                            NULL);
 752    object_property_add_str(obj, "outdev",
 753                            compare_get_outdev, compare_set_outdev,
 754                            NULL);
 755}
 756
 757static void colo_compare_finalize(Object *obj)
 758{
 759    CompareState *s = COLO_COMPARE(obj);
 760
 761    qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
 762                             s->worker_context, true);
 763    qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
 764                             s->worker_context, true);
 765    qemu_chr_fe_deinit(&s->chr_out);
 766
 767    g_main_loop_quit(s->compare_loop);
 768    qemu_thread_join(&s->thread);
 769
 770    /* Release all unhandled packets after compare thead exited */
 771    g_queue_foreach(&s->conn_list, colo_flush_packets, s);
 772
 773    g_queue_clear(&s->conn_list);
 774
 775    g_hash_table_destroy(s->connection_track_table);
 776    g_free(s->pri_indev);
 777    g_free(s->sec_indev);
 778    g_free(s->outdev);
 779}
 780
 781static const TypeInfo colo_compare_info = {
 782    .name = TYPE_COLO_COMPARE,
 783    .parent = TYPE_OBJECT,
 784    .instance_size = sizeof(CompareState),
 785    .instance_init = colo_compare_init,
 786    .instance_finalize = colo_compare_finalize,
 787    .class_size = sizeof(CompareClass),
 788    .class_init = colo_compare_class_init,
 789    .interfaces = (InterfaceInfo[]) {
 790        { TYPE_USER_CREATABLE },
 791        { }
 792    }
 793};
 794
 795static void register_types(void)
 796{
 797    type_register_static(&colo_compare_info);
 798}
 799
 800type_init(register_types);
 801