9pfs: local: mknod: don't follow symlinks
[qemu/ar7.git] / net / colo-compare.c
blob162fd6a570394c9e7d97213cc5a3c82ac6868d88
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/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"
33 #define TYPE_COLO_COMPARE "colo-compare"
34 #define COLO_COMPARE(obj) \
35 OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
37 #define COMPARE_READ_LEN_MAX NET_BUFSIZE
38 #define MAX_QUEUE_SIZE 1024
40 /* TODO: Should be configurable */
41 #define REGULAR_PACKET_CHECK_MS 3000
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 +--------+ +--------+ +--------+ +--------+
65 typedef struct CompareState {
66 Object parent;
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;
77 /* connection list: the connections belonged to this NIC could be found
78 * in this list.
79 * element type: Connection
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 /* Timer used on the primary to find packets that are never matched */
87 QEMUTimer *timer;
88 QemuMutex timer_check_lock;
89 } CompareState;
91 typedef struct CompareClass {
92 ObjectClass parent_class;
93 } CompareClass;
95 enum {
96 PRIMARY_IN = 0,
97 SECONDARY_IN,
100 static int compare_chr_send(CharBackend *out,
101 const uint8_t *buf,
102 uint32_t size);
104 static gint seq_sorter(Packet *a, Packet *b, gpointer data)
106 struct tcphdr *atcp, *btcp;
108 atcp = (struct tcphdr *)(a->transport_header);
109 btcp = (struct tcphdr *)(b->transport_header);
110 return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
114 * Return 0 on success, if return -1 means the pkt
115 * is unsupported(arp and ipv6) and will be sent later
117 static int packet_enqueue(CompareState *s, int mode)
119 ConnectionKey key;
120 Packet *pkt = NULL;
121 Connection *conn;
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);
129 if (parse_packet_early(pkt)) {
130 packet_destroy(pkt, NULL);
131 pkt = NULL;
132 return -1;
134 fill_connection_key(pkt, &key);
136 conn = connection_get(s->connection_track_table,
137 &key,
138 &s->conn_list);
140 if (!conn->processing) {
141 g_queue_push_tail(&s->conn_list, conn);
142 conn->processing = true;
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);
154 } else {
155 error_report("colo compare primary queue size too big,"
156 "drop packet");
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);
167 } else {
168 error_report("colo compare secondary queue size too big,"
169 "drop packet");
173 return 0;
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
183 static int colo_packet_compare(Packet *ppkt, Packet *spkt)
185 trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
186 inet_ntoa(ppkt->ip->ip_dst), spkt->size,
187 inet_ntoa(spkt->ip->ip_src),
188 inet_ntoa(spkt->ip->ip_dst));
190 if (ppkt->size == spkt->size) {
191 return memcmp(ppkt->data, spkt->data, spkt->size);
192 } else {
193 return -1;
198 * Called from the compare thread on the primary
199 * for compare tcp packet
200 * compare_tcp copied from Dr. David Alan Gilbert's branch
202 static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
204 struct tcphdr *ptcp, *stcp;
205 int res;
207 trace_colo_compare_main("compare tcp");
208 if (ppkt->size != spkt->size) {
209 if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
210 trace_colo_compare_main("pkt size not same");
212 return -1;
215 ptcp = (struct tcphdr *)ppkt->transport_header;
216 stcp = (struct tcphdr *)spkt->transport_header;
219 * The 'identification' field in the IP header is *very* random
220 * it almost never matches. Fudge this by ignoring differences in
221 * unfragmented packets; they'll normally sort themselves out if different
222 * anyway, and it should recover at the TCP level.
223 * An alternative would be to get both the primary and secondary to rewrite
224 * somehow; but that would need some sync traffic to sync the state
226 if (ntohs(ppkt->ip->ip_off) & IP_DF) {
227 spkt->ip->ip_id = ppkt->ip->ip_id;
228 /* and the sum will be different if the IDs were different */
229 spkt->ip->ip_sum = ppkt->ip->ip_sum;
232 res = memcmp(ppkt->data + ETH_HLEN, spkt->data + ETH_HLEN,
233 (spkt->size - ETH_HLEN));
235 if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
236 trace_colo_compare_pkt_info_src(inet_ntoa(ppkt->ip->ip_src),
237 ntohl(stcp->th_seq),
238 ntohl(stcp->th_ack),
239 res, stcp->th_flags,
240 spkt->size);
242 trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt->ip->ip_dst),
243 ntohl(ptcp->th_seq),
244 ntohl(ptcp->th_ack),
245 res, ptcp->th_flags,
246 ppkt->size);
248 qemu_hexdump((char *)ppkt->data, stderr,
249 "colo-compare ppkt", ppkt->size);
250 qemu_hexdump((char *)spkt->data, stderr,
251 "colo-compare spkt", spkt->size);
254 return res;
258 * Called from the compare thread on the primary
259 * for compare udp packet
261 static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
263 int ret;
265 trace_colo_compare_main("compare udp");
266 ret = colo_packet_compare(ppkt, spkt);
268 if (ret) {
269 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
270 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
271 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
272 qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
275 return ret;
279 * Called from the compare thread on the primary
280 * for compare icmp packet
282 static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
284 int network_length;
286 trace_colo_compare_main("compare icmp");
287 network_length = ppkt->ip->ip_hl * 4;
288 if (ppkt->size != spkt->size ||
289 ppkt->size < network_length + ETH_HLEN) {
290 return -1;
293 if (colo_packet_compare(ppkt, spkt)) {
294 trace_colo_compare_icmp_miscompare("primary pkt size",
295 ppkt->size);
296 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare",
297 ppkt->size);
298 trace_colo_compare_icmp_miscompare("Secondary pkt size",
299 spkt->size);
300 qemu_hexdump((char *)spkt->data, stderr, "colo-compare",
301 spkt->size);
302 return -1;
303 } else {
304 return 0;
309 * Called from the compare thread on the primary
310 * for compare other packet
312 static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
314 trace_colo_compare_main("compare other");
315 trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
316 inet_ntoa(ppkt->ip->ip_dst), spkt->size,
317 inet_ntoa(spkt->ip->ip_src),
318 inet_ntoa(spkt->ip->ip_dst));
319 return colo_packet_compare(ppkt, spkt);
322 static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
324 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
326 if ((now - pkt->creation_ms) > (*check_time)) {
327 trace_colo_old_packet_check_found(pkt->creation_ms);
328 return 0;
329 } else {
330 return 1;
334 static void colo_old_packet_check_one_conn(void *opaque,
335 void *user_data)
337 Connection *conn = opaque;
338 GList *result = NULL;
339 int64_t check_time = REGULAR_PACKET_CHECK_MS;
341 result = g_queue_find_custom(&conn->primary_list,
342 &check_time,
343 (GCompareFunc)colo_old_packet_check_one);
345 if (result) {
346 /* do checkpoint will flush old packet */
347 /* TODO: colo_notify_checkpoint();*/
352 * Look for old packets that the secondary hasn't matched,
353 * if we have some then we have to checkpoint to wake
354 * the secondary up.
356 static void colo_old_packet_check(void *opaque)
358 CompareState *s = opaque;
360 g_queue_foreach(&s->conn_list, colo_old_packet_check_one_conn, NULL);
364 * Called from the compare thread on the primary
365 * for compare connection
367 static void colo_compare_connection(void *opaque, void *user_data)
369 CompareState *s = user_data;
370 Connection *conn = opaque;
371 Packet *pkt = NULL;
372 GList *result = NULL;
373 int ret;
375 while (!g_queue_is_empty(&conn->primary_list) &&
376 !g_queue_is_empty(&conn->secondary_list)) {
377 qemu_mutex_lock(&s->timer_check_lock);
378 pkt = g_queue_pop_tail(&conn->primary_list);
379 qemu_mutex_unlock(&s->timer_check_lock);
380 switch (conn->ip_proto) {
381 case IPPROTO_TCP:
382 result = g_queue_find_custom(&conn->secondary_list,
383 pkt, (GCompareFunc)colo_packet_compare_tcp);
384 break;
385 case IPPROTO_UDP:
386 result = g_queue_find_custom(&conn->secondary_list,
387 pkt, (GCompareFunc)colo_packet_compare_udp);
388 break;
389 case IPPROTO_ICMP:
390 result = g_queue_find_custom(&conn->secondary_list,
391 pkt, (GCompareFunc)colo_packet_compare_icmp);
392 break;
393 default:
394 result = g_queue_find_custom(&conn->secondary_list,
395 pkt, (GCompareFunc)colo_packet_compare_other);
396 break;
399 if (result) {
400 ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
401 if (ret < 0) {
402 error_report("colo_send_primary_packet failed");
404 trace_colo_compare_main("packet same and release packet");
405 g_queue_remove(&conn->secondary_list, result->data);
406 packet_destroy(pkt, NULL);
407 } else {
409 * If one packet arrive late, the secondary_list or
410 * primary_list will be empty, so we can't compare it
411 * until next comparison.
413 trace_colo_compare_main("packet different");
414 qemu_mutex_lock(&s->timer_check_lock);
415 g_queue_push_tail(&conn->primary_list, pkt);
416 qemu_mutex_unlock(&s->timer_check_lock);
417 /* TODO: colo_notify_checkpoint();*/
418 break;
423 static int compare_chr_send(CharBackend *out,
424 const uint8_t *buf,
425 uint32_t size)
427 int ret = 0;
428 uint32_t len = htonl(size);
430 if (!size) {
431 return 0;
434 ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len));
435 if (ret != sizeof(len)) {
436 goto err;
439 ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size);
440 if (ret != size) {
441 goto err;
444 return 0;
446 err:
447 return ret < 0 ? ret : -EIO;
450 static int compare_chr_can_read(void *opaque)
452 return COMPARE_READ_LEN_MAX;
456 * Called from the main thread on the primary for packets
457 * arriving over the socket from the primary.
459 static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
461 CompareState *s = COLO_COMPARE(opaque);
462 int ret;
464 ret = net_fill_rstate(&s->pri_rs, buf, size);
465 if (ret == -1) {
466 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
467 NULL, NULL, true);
468 error_report("colo-compare primary_in error");
473 * Called from the main thread on the primary for packets
474 * arriving over the socket from the secondary.
476 static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
478 CompareState *s = COLO_COMPARE(opaque);
479 int ret;
481 ret = net_fill_rstate(&s->sec_rs, buf, size);
482 if (ret == -1) {
483 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
484 NULL, NULL, true);
485 error_report("colo-compare secondary_in error");
489 static void *colo_compare_thread(void *opaque)
491 GMainContext *worker_context;
492 GMainLoop *compare_loop;
493 CompareState *s = opaque;
495 worker_context = g_main_context_new();
497 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
498 compare_pri_chr_in, NULL, s, worker_context, true);
499 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
500 compare_sec_chr_in, NULL, s, worker_context, true);
502 compare_loop = g_main_loop_new(worker_context, FALSE);
504 g_main_loop_run(compare_loop);
506 g_main_loop_unref(compare_loop);
507 g_main_context_unref(worker_context);
508 return NULL;
511 static char *compare_get_pri_indev(Object *obj, Error **errp)
513 CompareState *s = COLO_COMPARE(obj);
515 return g_strdup(s->pri_indev);
518 static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
520 CompareState *s = COLO_COMPARE(obj);
522 g_free(s->pri_indev);
523 s->pri_indev = g_strdup(value);
526 static char *compare_get_sec_indev(Object *obj, Error **errp)
528 CompareState *s = COLO_COMPARE(obj);
530 return g_strdup(s->sec_indev);
533 static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
535 CompareState *s = COLO_COMPARE(obj);
537 g_free(s->sec_indev);
538 s->sec_indev = g_strdup(value);
541 static char *compare_get_outdev(Object *obj, Error **errp)
543 CompareState *s = COLO_COMPARE(obj);
545 return g_strdup(s->outdev);
548 static void compare_set_outdev(Object *obj, const char *value, Error **errp)
550 CompareState *s = COLO_COMPARE(obj);
552 g_free(s->outdev);
553 s->outdev = g_strdup(value);
556 static void compare_pri_rs_finalize(SocketReadState *pri_rs)
558 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
560 if (packet_enqueue(s, PRIMARY_IN)) {
561 trace_colo_compare_main("primary: unsupported packet in");
562 compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
563 } else {
564 /* compare connection */
565 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
569 static void compare_sec_rs_finalize(SocketReadState *sec_rs)
571 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
573 if (packet_enqueue(s, SECONDARY_IN)) {
574 trace_colo_compare_main("secondary: unsupported packet in");
575 } else {
576 /* compare connection */
577 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
583 * Return 0 is success.
584 * Return 1 is failed.
586 static int find_and_check_chardev(Chardev **chr,
587 char *chr_name,
588 Error **errp)
590 *chr = qemu_chr_find(chr_name);
591 if (*chr == NULL) {
592 error_setg(errp, "Device '%s' not found",
593 chr_name);
594 return 1;
597 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
598 error_setg(errp, "chardev \"%s\" is not reconnectable",
599 chr_name);
600 return 1;
603 return 0;
607 * Check old packet regularly so it can watch for any packets
608 * that the secondary hasn't produced equivalents of.
610 static void check_old_packet_regular(void *opaque)
612 CompareState *s = opaque;
614 timer_mod(s->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
615 REGULAR_PACKET_CHECK_MS);
616 /* if have old packet we will notify checkpoint */
618 * TODO: Make timer handler run in compare thread
619 * like qemu_chr_add_handlers_full.
621 qemu_mutex_lock(&s->timer_check_lock);
622 colo_old_packet_check(s);
623 qemu_mutex_unlock(&s->timer_check_lock);
627 * Called from the main thread on the primary
628 * to setup colo-compare.
630 static void colo_compare_complete(UserCreatable *uc, Error **errp)
632 CompareState *s = COLO_COMPARE(uc);
633 Chardev *chr;
634 char thread_name[64];
635 static int compare_id;
637 if (!s->pri_indev || !s->sec_indev || !s->outdev) {
638 error_setg(errp, "colo compare needs 'primary_in' ,"
639 "'secondary_in','outdev' property set");
640 return;
641 } else if (!strcmp(s->pri_indev, s->outdev) ||
642 !strcmp(s->sec_indev, s->outdev) ||
643 !strcmp(s->pri_indev, s->sec_indev)) {
644 error_setg(errp, "'indev' and 'outdev' could not be same "
645 "for compare module");
646 return;
649 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
650 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
651 return;
654 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
655 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
656 return;
659 if (find_and_check_chardev(&chr, s->outdev, errp) ||
660 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
661 return;
664 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
665 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
667 g_queue_init(&s->conn_list);
668 qemu_mutex_init(&s->timer_check_lock);
670 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
671 connection_key_equal,
672 g_free,
673 connection_destroy);
675 sprintf(thread_name, "colo-compare %d", compare_id);
676 qemu_thread_create(&s->thread, thread_name,
677 colo_compare_thread, s,
678 QEMU_THREAD_JOINABLE);
679 compare_id++;
681 /* A regular timer to kick any packets that the secondary doesn't match */
682 s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, /* Only when guest runs */
683 check_old_packet_regular, s);
684 timer_mod(s->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
685 REGULAR_PACKET_CHECK_MS);
687 return;
690 static void colo_compare_class_init(ObjectClass *oc, void *data)
692 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
694 ucc->complete = colo_compare_complete;
697 static void colo_compare_init(Object *obj)
699 object_property_add_str(obj, "primary_in",
700 compare_get_pri_indev, compare_set_pri_indev,
701 NULL);
702 object_property_add_str(obj, "secondary_in",
703 compare_get_sec_indev, compare_set_sec_indev,
704 NULL);
705 object_property_add_str(obj, "outdev",
706 compare_get_outdev, compare_set_outdev,
707 NULL);
710 static void colo_compare_finalize(Object *obj)
712 CompareState *s = COLO_COMPARE(obj);
714 qemu_chr_fe_deinit(&s->chr_pri_in);
715 qemu_chr_fe_deinit(&s->chr_sec_in);
716 qemu_chr_fe_deinit(&s->chr_out);
718 g_queue_free(&s->conn_list);
720 if (qemu_thread_is_self(&s->thread)) {
721 /* compare connection */
722 g_queue_foreach(&s->conn_list, colo_compare_connection, s);
723 qemu_thread_join(&s->thread);
726 if (s->timer) {
727 timer_del(s->timer);
730 qemu_mutex_destroy(&s->timer_check_lock);
732 g_free(s->pri_indev);
733 g_free(s->sec_indev);
734 g_free(s->outdev);
737 static const TypeInfo colo_compare_info = {
738 .name = TYPE_COLO_COMPARE,
739 .parent = TYPE_OBJECT,
740 .instance_size = sizeof(CompareState),
741 .instance_init = colo_compare_init,
742 .instance_finalize = colo_compare_finalize,
743 .class_size = sizeof(CompareClass),
744 .class_init = colo_compare_class_init,
745 .interfaces = (InterfaceInfo[]) {
746 { TYPE_USER_CREATABLE },
751 static void register_types(void)
753 type_register_static(&colo_compare_info);
756 type_init(register_types);