]> git.proxmox.com Git - mirror_qemu.git/blame - net/colo-compare.c
colo-compare: compare the packet in a specified Connection
[mirror_qemu.git] / net / colo-compare.c
CommitLineData
7dce4e6f
ZC
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"
59509ec1 17#include "trace.h"
7dce4e6f
ZC
18#include "qemu-common.h"
19#include "qapi/qmp/qerror.h"
20#include "qapi/error.h"
21#include "net/net.h"
f4b61836 22#include "net/eth.h"
7dce4e6f
ZC
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"
4d43a603 28#include "chardev/char-fe.h"
7dce4e6f
ZC
29#include "qemu/sockets.h"
30#include "qapi-visit.h"
59509ec1 31#include "net/colo.h"
dd321ecf 32#include "sysemu/iothread.h"
7dce4e6f
ZC
33
34#define TYPE_COLO_COMPARE "colo-compare"
35#define COLO_COMPARE(obj) \
36 OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
37
0682e15b 38#define COMPARE_READ_LEN_MAX NET_BUFSIZE
b6540d40
ZC
39#define MAX_QUEUE_SIZE 1024
40
0682e15b
ZC
41/* TODO: Should be configurable */
42#define REGULAR_PACKET_CHECK_MS 3000
43
59509ec1 44/*
61c5f469
ZC
45 * + CompareState ++
46 * | |
47 * +---------------+ +---------------+ +---------------+
48 * | conn list + - > conn + ------- > conn + -- > ......
49 * +---------------+ +---------------+ +---------------+
50 * | | | | | |
51 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
52 * |primary | |secondary |primary | |secondary
53 * |packet | |packet + |packet | |packet +
54 * +--------+ +--------+ +--------+ +--------+
55 * | | | |
56 * +---v----+ +---v----+ +---v----+ +---v----+
57 * |primary | |secondary |primary | |secondary
58 * |packet | |packet + |packet | |packet +
59 * +--------+ +--------+ +--------+ +--------+
60 * | | | |
61 * +---v----+ +---v----+ +---v----+ +---v----+
62 * |primary | |secondary |primary | |secondary
63 * |packet | |packet + |packet | |packet +
64 * +--------+ +--------+ +--------+ +--------+
65 */
7dce4e6f
ZC
66typedef struct CompareState {
67 Object parent;
68
69 char *pri_indev;
70 char *sec_indev;
71 char *outdev;
32a6ebec
MAL
72 CharBackend chr_pri_in;
73 CharBackend chr_sec_in;
74 CharBackend chr_out;
7dce4e6f
ZC
75 SocketReadState pri_rs;
76 SocketReadState sec_rs;
aa3a7032 77 bool vnet_hdr;
59509ec1 78
61c5f469
ZC
79 /*
80 * Record the connection that through the NIC
81 * Element type: Connection
b6540d40
ZC
82 */
83 GQueue conn_list;
61c5f469 84 /* Record the connection without repetition */
59509ec1 85 GHashTable *connection_track_table;
dfd917a9 86
dd321ecf 87 IOThread *iothread;
b43decb0 88 GMainContext *worker_context;
dd321ecf 89 QEMUTimer *packet_check_timer;
7dce4e6f
ZC
90} CompareState;
91
92typedef struct CompareClass {
93 ObjectClass parent_class;
94} CompareClass;
95
59509ec1
ZC
96enum {
97 PRIMARY_IN = 0,
98 SECONDARY_IN,
99};
100
3037e7a5 101static int compare_chr_send(CompareState *s,
59509ec1 102 const uint8_t *buf,
aa3a7032
ZC
103 uint32_t size,
104 uint32_t vnet_hdr_len);
59509ec1 105
a935cc31
ZC
106static gint seq_sorter(Packet *a, Packet *b, gpointer data)
107{
108 struct tcphdr *atcp, *btcp;
109
110 atcp = (struct tcphdr *)(a->transport_header);
111 btcp = (struct tcphdr *)(b->transport_header);
112 return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
113}
114
8850d4ca
MZ
115/*
116 * Return 1 on success, if return 0 means the
117 * packet will be dropped
118 */
119static int colo_insert_packet(GQueue *queue, Packet *pkt)
120{
121 if (g_queue_get_length(queue) <= MAX_QUEUE_SIZE) {
122 if (pkt->ip->ip_p == IPPROTO_TCP) {
123 g_queue_insert_sorted(queue,
124 pkt,
125 (GCompareDataFunc)seq_sorter,
126 NULL);
127 } else {
128 g_queue_push_tail(queue, pkt);
129 }
130 return 1;
131 }
132 return 0;
133}
134
59509ec1
ZC
135/*
136 * Return 0 on success, if return -1 means the pkt
137 * is unsupported(arp and ipv6) and will be sent later
138 */
8ec14402 139static int packet_enqueue(CompareState *s, int mode, Connection **con)
59509ec1 140{
b6540d40 141 ConnectionKey key;
59509ec1 142 Packet *pkt = NULL;
b6540d40 143 Connection *conn;
59509ec1
ZC
144
145 if (mode == PRIMARY_IN) {
ada1a33f
ZC
146 pkt = packet_new(s->pri_rs.buf,
147 s->pri_rs.packet_len,
148 s->pri_rs.vnet_hdr_len);
59509ec1 149 } else {
ada1a33f
ZC
150 pkt = packet_new(s->sec_rs.buf,
151 s->sec_rs.packet_len,
152 s->sec_rs.vnet_hdr_len);
59509ec1
ZC
153 }
154
155 if (parse_packet_early(pkt)) {
156 packet_destroy(pkt, NULL);
157 pkt = NULL;
158 return -1;
159 }
b6540d40 160 fill_connection_key(pkt, &key);
59509ec1 161
b6540d40
ZC
162 conn = connection_get(s->connection_track_table,
163 &key,
164 &s->conn_list);
59509ec1 165
b6540d40
ZC
166 if (!conn->processing) {
167 g_queue_push_tail(&s->conn_list, conn);
168 conn->processing = true;
169 }
170
171 if (mode == PRIMARY_IN) {
8850d4ca 172 if (!colo_insert_packet(&conn->primary_list, pkt)) {
b6540d40
ZC
173 error_report("colo compare primary queue size too big,"
174 "drop packet");
175 }
176 } else {
8850d4ca 177 if (!colo_insert_packet(&conn->secondary_list, pkt)) {
b6540d40
ZC
178 error_report("colo compare secondary queue size too big,"
179 "drop packet");
180 }
181 }
8ec14402 182 con = &conn;
59509ec1
ZC
183
184 return 0;
185}
186
0682e15b
ZC
187/*
188 * The IP packets sent by primary and secondary
189 * will be compared in here
190 * TODO support ip fragment, Out-Of-Order
191 * return: 0 means packet same
192 * > 0 || < 0 means packet different
193 */
6f5009c3
ZC
194static int colo_packet_compare_common(Packet *ppkt,
195 Packet *spkt,
196 int poffset,
197 int soffset)
0682e15b 198{
d87aa138 199 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
e630b2bf
ZC
200 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
201
202 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
203 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
204 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
205 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
206
207 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
208 pri_ip_dst, spkt->size,
209 sec_ip_src, sec_ip_dst);
210 }
0682e15b 211
6f5009c3
ZC
212 poffset = ppkt->vnet_hdr_len + poffset;
213 soffset = ppkt->vnet_hdr_len + soffset;
d63b366a 214
6f5009c3
ZC
215 if (ppkt->size - poffset == spkt->size - soffset) {
216 return memcmp(ppkt->data + poffset,
217 spkt->data + soffset,
218 spkt->size - soffset);
0682e15b 219 } else {
2ad7ca4c 220 trace_colo_compare_main("Net packet size are not the same");
0682e15b
ZC
221 return -1;
222 }
223}
224
f4b61836
ZC
225/*
226 * Called from the compare thread on the primary
227 * for compare tcp packet
228 * compare_tcp copied from Dr. David Alan Gilbert's branch
229 */
230static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
0682e15b 231{
f4b61836
ZC
232 struct tcphdr *ptcp, *stcp;
233 int res;
f4b61836
ZC
234
235 trace_colo_compare_main("compare tcp");
2ad7ca4c 236
f4b61836
ZC
237 ptcp = (struct tcphdr *)ppkt->transport_header;
238 stcp = (struct tcphdr *)spkt->transport_header;
239
240 /*
241 * The 'identification' field in the IP header is *very* random
242 * it almost never matches. Fudge this by ignoring differences in
243 * unfragmented packets; they'll normally sort themselves out if different
244 * anyway, and it should recover at the TCP level.
245 * An alternative would be to get both the primary and secondary to rewrite
246 * somehow; but that would need some sync traffic to sync the state
247 */
248 if (ntohs(ppkt->ip->ip_off) & IP_DF) {
249 spkt->ip->ip_id = ppkt->ip->ip_id;
250 /* and the sum will be different if the IDs were different */
251 spkt->ip->ip_sum = ppkt->ip->ip_sum;
252 }
253
184d4d42
ZC
254 /*
255 * Check tcp header length for tcp option field.
256 * th_off > 5 means this tcp packet have options field.
257 * The tcp options maybe always different.
258 * for example:
259 * From RFC 7323.
260 * TCP Timestamps option (TSopt):
261 * Kind: 8
262 *
263 * Length: 10 bytes
264 *
265 * +-------+-------+---------------------+---------------------+
266 * |Kind=8 | 10 | TS Value (TSval) |TS Echo Reply (TSecr)|
267 * +-------+-------+---------------------+---------------------+
268 * 1 1 4 4
269 *
270 * In this case the primary guest's timestamp always different with
271 * the secondary guest's timestamp. COLO just focus on payload,
272 * so we just need skip this field.
273 */
274 if (ptcp->th_off > 5) {
6f5009c3
ZC
275 ptrdiff_t ptcp_offset, stcp_offset;
276
277 ptcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data
278 + (ptcp->th_off * 4) - ppkt->vnet_hdr_len;
279 stcp_offset = spkt->transport_header - (uint8_t *)spkt->data
280 + (stcp->th_off * 4) - spkt->vnet_hdr_len;
281
282 /*
283 * When network is busy, some tcp options(like sack) will unpredictable
284 * occur in primary side or secondary side. it will make packet size
285 * not same, but the two packet's payload is identical. colo just
286 * care about packet payload, so we skip the option field.
287 */
288 res = colo_packet_compare_common(ppkt, spkt, ptcp_offset, stcp_offset);
184d4d42 289 } else if (ptcp->th_sum == stcp->th_sum) {
6f5009c3 290 res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN, ETH_HLEN);
6efeb328
ZC
291 } else {
292 res = -1;
293 }
f4b61836 294
d87aa138
SH
295 if (res != 0 &&
296 trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
f583dca9
ZC
297 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
298
299 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
300 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
301 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
302 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
303
304 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
305 pri_ip_dst, spkt->size,
306 sec_ip_src, sec_ip_dst);
307
308 trace_colo_compare_tcp_info("pri tcp packet",
309 ntohl(ptcp->th_seq),
310 ntohl(ptcp->th_ack),
311 res, ptcp->th_flags,
312 ppkt->size);
313
314 trace_colo_compare_tcp_info("sec tcp packet",
315 ntohl(stcp->th_seq),
316 ntohl(stcp->th_ack),
317 res, stcp->th_flags,
318 spkt->size);
2061c14c
ZC
319
320 qemu_hexdump((char *)ppkt->data, stderr,
321 "colo-compare ppkt", ppkt->size);
322 qemu_hexdump((char *)spkt->data, stderr,
323 "colo-compare spkt", spkt->size);
f4b61836
ZC
324 }
325
326 return res;
327}
328
329/*
330 * Called from the compare thread on the primary
331 * for compare udp packet
332 */
333static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
334{
335 int ret;
6efeb328 336 int network_header_length = ppkt->ip->ip_hl * 4;
f4b61836
ZC
337
338 trace_colo_compare_main("compare udp");
2ad7ca4c 339
6efeb328
ZC
340 /*
341 * Because of ppkt and spkt are both in the same connection,
342 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
343 * same with spkt. In addition, IP header's Identification is a random
344 * field, we can handle it in IP fragmentation function later.
345 * COLO just concern the response net packet payload from primary guest
346 * and secondary guest are same or not, So we ignored all IP header include
347 * other field like TOS,TTL,IP Checksum. we only need to compare
348 * the ip payload here.
349 */
350 ret = colo_packet_compare_common(ppkt, spkt,
6f5009c3 351 network_header_length + ETH_HLEN,
6efeb328 352 network_header_length + ETH_HLEN);
f4b61836
ZC
353
354 if (ret) {
355 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
f4b61836 356 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
d87aa138 357 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
1723a7f7
ZC
358 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
359 ppkt->size);
360 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
361 spkt->size);
362 }
f4b61836
ZC
363 }
364
365 return ret;
366}
367
368/*
369 * Called from the compare thread on the primary
370 * for compare icmp packet
371 */
372static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
373{
6efeb328
ZC
374 int network_header_length = ppkt->ip->ip_hl * 4;
375
f4b61836 376 trace_colo_compare_main("compare icmp");
f4b61836 377
6efeb328
ZC
378 /*
379 * Because of ppkt and spkt are both in the same connection,
380 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
381 * same with spkt. In addition, IP header's Identification is a random
382 * field, we can handle it in IP fragmentation function later.
383 * COLO just concern the response net packet payload from primary guest
384 * and secondary guest are same or not, So we ignored all IP header include
385 * other field like TOS,TTL,IP Checksum. we only need to compare
386 * the ip payload here.
387 */
388 if (colo_packet_compare_common(ppkt, spkt,
6f5009c3 389 network_header_length + ETH_HLEN,
6efeb328 390 network_header_length + ETH_HLEN)) {
f4b61836
ZC
391 trace_colo_compare_icmp_miscompare("primary pkt size",
392 ppkt->size);
f4b61836
ZC
393 trace_colo_compare_icmp_miscompare("Secondary pkt size",
394 spkt->size);
d87aa138 395 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
1723a7f7
ZC
396 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
397 ppkt->size);
398 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
399 spkt->size);
400 }
f4b61836
ZC
401 return -1;
402 } else {
403 return 0;
404 }
405}
406
407/*
408 * Called from the compare thread on the primary
409 * for compare other packet
410 */
411static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
412{
413 trace_colo_compare_main("compare other");
d87aa138 414 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
e630b2bf
ZC
415 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
416
417 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
418 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
419 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
420 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
421
422 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
423 pri_ip_dst, spkt->size,
424 sec_ip_src, sec_ip_dst);
425 }
426
6f5009c3 427 return colo_packet_compare_common(ppkt, spkt, 0, 0);
0682e15b
ZC
428}
429
430static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
431{
432 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
433
434 if ((now - pkt->creation_ms) > (*check_time)) {
435 trace_colo_old_packet_check_found(pkt->creation_ms);
436 return 0;
437 } else {
438 return 1;
439 }
440}
441
d25a7dab
ZC
442static int colo_old_packet_check_one_conn(Connection *conn,
443 void *user_data)
0682e15b 444{
0682e15b
ZC
445 GList *result = NULL;
446 int64_t check_time = REGULAR_PACKET_CHECK_MS;
447
448 result = g_queue_find_custom(&conn->primary_list,
449 &check_time,
450 (GCompareFunc)colo_old_packet_check_one);
451
452 if (result) {
61c5f469
ZC
453 /* Do checkpoint will flush old packet */
454 /*
455 * TODO: Notify colo frame to do checkpoint.
456 * colo_compare_inconsistent_notify();
457 */
d25a7dab 458 return 0;
0682e15b 459 }
d25a7dab
ZC
460
461 return 1;
0682e15b
ZC
462}
463
464/*
465 * Look for old packets that the secondary hasn't matched,
466 * if we have some then we have to checkpoint to wake
467 * the secondary up.
468 */
469static void colo_old_packet_check(void *opaque)
470{
471 CompareState *s = opaque;
472
d25a7dab
ZC
473 /*
474 * If we find one old packet, stop finding job and notify
475 * COLO frame do checkpoint.
476 */
477 g_queue_find_custom(&s->conn_list, NULL,
478 (GCompareFunc)colo_old_packet_check_one_conn);
0682e15b
ZC
479}
480
481/*
482 * Called from the compare thread on the primary
483 * for compare connection
484 */
485static void colo_compare_connection(void *opaque, void *user_data)
486{
487 CompareState *s = user_data;
488 Connection *conn = opaque;
489 Packet *pkt = NULL;
490 GList *result = NULL;
491 int ret;
492
493 while (!g_queue_is_empty(&conn->primary_list) &&
494 !g_queue_is_empty(&conn->secondary_list)) {
626bba98 495 pkt = g_queue_pop_head(&conn->primary_list);
f4b61836
ZC
496 switch (conn->ip_proto) {
497 case IPPROTO_TCP:
498 result = g_queue_find_custom(&conn->secondary_list,
499 pkt, (GCompareFunc)colo_packet_compare_tcp);
500 break;
501 case IPPROTO_UDP:
502 result = g_queue_find_custom(&conn->secondary_list,
503 pkt, (GCompareFunc)colo_packet_compare_udp);
504 break;
505 case IPPROTO_ICMP:
506 result = g_queue_find_custom(&conn->secondary_list,
507 pkt, (GCompareFunc)colo_packet_compare_icmp);
508 break;
509 default:
510 result = g_queue_find_custom(&conn->secondary_list,
511 pkt, (GCompareFunc)colo_packet_compare_other);
512 break;
513 }
0682e15b
ZC
514
515 if (result) {
aa3a7032
ZC
516 ret = compare_chr_send(s,
517 pkt->data,
518 pkt->size,
519 pkt->vnet_hdr_len);
0682e15b
ZC
520 if (ret < 0) {
521 error_report("colo_send_primary_packet failed");
522 }
523 trace_colo_compare_main("packet same and release packet");
524 g_queue_remove(&conn->secondary_list, result->data);
525 packet_destroy(pkt, NULL);
526 } else {
527 /*
528 * If one packet arrive late, the secondary_list or
529 * primary_list will be empty, so we can't compare it
530 * until next comparison.
531 */
532 trace_colo_compare_main("packet different");
626bba98 533 g_queue_push_head(&conn->primary_list, pkt);
0682e15b
ZC
534 /* TODO: colo_notify_checkpoint();*/
535 break;
536 }
537 }
538}
539
3037e7a5 540static int compare_chr_send(CompareState *s,
59509ec1 541 const uint8_t *buf,
aa3a7032
ZC
542 uint32_t size,
543 uint32_t vnet_hdr_len)
59509ec1
ZC
544{
545 int ret = 0;
546 uint32_t len = htonl(size);
547
548 if (!size) {
549 return 0;
550 }
551
3037e7a5 552 ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
59509ec1
ZC
553 if (ret != sizeof(len)) {
554 goto err;
555 }
556
aa3a7032
ZC
557 if (s->vnet_hdr) {
558 /*
559 * We send vnet header len make other module(like filter-redirector)
560 * know how to parse net packet correctly.
561 */
562 len = htonl(vnet_hdr_len);
563 ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
564 if (ret != sizeof(len)) {
565 goto err;
566 }
567 }
568
3037e7a5 569 ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
59509ec1
ZC
570 if (ret != size) {
571 goto err;
572 }
573
574 return 0;
575
576err:
577 return ret < 0 ? ret : -EIO;
578}
579
0682e15b
ZC
580static int compare_chr_can_read(void *opaque)
581{
582 return COMPARE_READ_LEN_MAX;
583}
584
585/*
586 * Called from the main thread on the primary for packets
587 * arriving over the socket from the primary.
588 */
589static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
590{
591 CompareState *s = COLO_COMPARE(opaque);
592 int ret;
593
594 ret = net_fill_rstate(&s->pri_rs, buf, size);
595 if (ret == -1) {
81517ba3 596 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
39ab61c6 597 NULL, NULL, true);
0682e15b
ZC
598 error_report("colo-compare primary_in error");
599 }
600}
601
602/*
603 * Called from the main thread on the primary for packets
604 * arriving over the socket from the secondary.
605 */
606static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
607{
608 CompareState *s = COLO_COMPARE(opaque);
609 int ret;
610
611 ret = net_fill_rstate(&s->sec_rs, buf, size);
612 if (ret == -1) {
81517ba3 613 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
39ab61c6 614 NULL, NULL, true);
0682e15b
ZC
615 error_report("colo-compare secondary_in error");
616 }
617}
618
66d2a242
HZ
619/*
620 * Check old packet regularly so it can watch for any packets
621 * that the secondary hasn't produced equivalents of.
622 */
dd321ecf 623static void check_old_packet_regular(void *opaque)
66d2a242
HZ
624{
625 CompareState *s = opaque;
626
627 /* if have old packet we will notify checkpoint */
628 colo_old_packet_check(s);
dd321ecf
WY
629 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
630 REGULAR_PACKET_CHECK_MS);
631}
632
633static void colo_compare_timer_init(CompareState *s)
634{
635 AioContext *ctx = iothread_get_aio_context(s->iothread);
66d2a242 636
dd321ecf
WY
637 s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL,
638 SCALE_MS, check_old_packet_regular,
639 s);
640 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
641 REGULAR_PACKET_CHECK_MS);
66d2a242
HZ
642}
643
dd321ecf 644static void colo_compare_timer_del(CompareState *s)
0682e15b 645{
dd321ecf
WY
646 if (s->packet_check_timer) {
647 timer_del(s->packet_check_timer);
648 timer_free(s->packet_check_timer);
649 s->packet_check_timer = NULL;
650 }
651 }
0682e15b 652
dd321ecf
WY
653static void colo_compare_iothread(CompareState *s)
654{
655 object_ref(OBJECT(s->iothread));
656 s->worker_context = iothread_get_g_main_context(s->iothread);
0682e15b 657
5345fdb4 658 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
81517ba3
AN
659 compare_pri_chr_in, NULL, NULL,
660 s, s->worker_context, true);
5345fdb4 661 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
81517ba3
AN
662 compare_sec_chr_in, NULL, NULL,
663 s, s->worker_context, true);
0682e15b 664
dd321ecf 665 colo_compare_timer_init(s);
0682e15b
ZC
666}
667
7dce4e6f
ZC
668static char *compare_get_pri_indev(Object *obj, Error **errp)
669{
670 CompareState *s = COLO_COMPARE(obj);
671
672 return g_strdup(s->pri_indev);
673}
674
675static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
676{
677 CompareState *s = COLO_COMPARE(obj);
678
679 g_free(s->pri_indev);
680 s->pri_indev = g_strdup(value);
681}
682
683static char *compare_get_sec_indev(Object *obj, Error **errp)
684{
685 CompareState *s = COLO_COMPARE(obj);
686
687 return g_strdup(s->sec_indev);
688}
689
690static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
691{
692 CompareState *s = COLO_COMPARE(obj);
693
694 g_free(s->sec_indev);
695 s->sec_indev = g_strdup(value);
696}
697
698static char *compare_get_outdev(Object *obj, Error **errp)
699{
700 CompareState *s = COLO_COMPARE(obj);
701
702 return g_strdup(s->outdev);
703}
704
705static void compare_set_outdev(Object *obj, const char *value, Error **errp)
706{
707 CompareState *s = COLO_COMPARE(obj);
708
709 g_free(s->outdev);
710 s->outdev = g_strdup(value);
711}
712
aa3a7032
ZC
713static bool compare_get_vnet_hdr(Object *obj, Error **errp)
714{
715 CompareState *s = COLO_COMPARE(obj);
716
717 return s->vnet_hdr;
718}
719
720static void compare_set_vnet_hdr(Object *obj,
721 bool value,
722 Error **errp)
723{
724 CompareState *s = COLO_COMPARE(obj);
725
726 s->vnet_hdr = value;
727}
728
7dce4e6f
ZC
729static void compare_pri_rs_finalize(SocketReadState *pri_rs)
730{
59509ec1 731 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
8ec14402 732 Connection *conn = NULL;
59509ec1 733
8ec14402 734 if (packet_enqueue(s, PRIMARY_IN, &conn)) {
59509ec1 735 trace_colo_compare_main("primary: unsupported packet in");
aa3a7032
ZC
736 compare_chr_send(s,
737 pri_rs->buf,
738 pri_rs->packet_len,
739 pri_rs->vnet_hdr_len);
0682e15b
ZC
740 } else {
741 /* compare connection */
8ec14402 742 colo_compare_connection(conn, s);
59509ec1 743 }
7dce4e6f
ZC
744}
745
746static void compare_sec_rs_finalize(SocketReadState *sec_rs)
747{
59509ec1 748 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
8ec14402 749 Connection *conn = NULL;
59509ec1 750
8ec14402 751 if (packet_enqueue(s, SECONDARY_IN, &conn)) {
59509ec1 752 trace_colo_compare_main("secondary: unsupported packet in");
0682e15b
ZC
753 } else {
754 /* compare connection */
8ec14402 755 colo_compare_connection(conn, s);
59509ec1 756 }
7dce4e6f
ZC
757}
758
7dce4e6f
ZC
759
760/*
761 * Return 0 is success.
762 * Return 1 is failed.
763 */
0ec7b3e7 764static int find_and_check_chardev(Chardev **chr,
7dce4e6f
ZC
765 char *chr_name,
766 Error **errp)
767{
7dce4e6f
ZC
768 *chr = qemu_chr_find(chr_name);
769 if (*chr == NULL) {
770 error_setg(errp, "Device '%s' not found",
771 chr_name);
772 return 1;
773 }
774
0a73336d
DB
775 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
776 error_setg(errp, "chardev \"%s\" is not reconnectable",
7dce4e6f
ZC
777 chr_name);
778 return 1;
779 }
fbf3cc3a 780
7dce4e6f
ZC
781 return 0;
782}
783
784/*
785 * Called from the main thread on the primary
786 * to setup colo-compare.
787 */
788static void colo_compare_complete(UserCreatable *uc, Error **errp)
789{
790 CompareState *s = COLO_COMPARE(uc);
0ec7b3e7 791 Chardev *chr;
7dce4e6f 792
dd321ecf 793 if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) {
7dce4e6f 794 error_setg(errp, "colo compare needs 'primary_in' ,"
dd321ecf 795 "'secondary_in','outdev','iothread' property set");
7dce4e6f
ZC
796 return;
797 } else if (!strcmp(s->pri_indev, s->outdev) ||
798 !strcmp(s->sec_indev, s->outdev) ||
799 !strcmp(s->pri_indev, s->sec_indev)) {
800 error_setg(errp, "'indev' and 'outdev' could not be same "
801 "for compare module");
802 return;
803 }
804
5345fdb4
MAL
805 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
806 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
7dce4e6f
ZC
807 return;
808 }
809
5345fdb4
MAL
810 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
811 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
7dce4e6f
ZC
812 return;
813 }
814
5345fdb4
MAL
815 if (find_and_check_chardev(&chr, s->outdev, errp) ||
816 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
7dce4e6f
ZC
817 return;
818 }
819
aa3a7032
ZC
820 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
821 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
7dce4e6f 822
b6540d40
ZC
823 g_queue_init(&s->conn_list);
824
825 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
826 connection_key_equal,
827 g_free,
828 connection_destroy);
59509ec1 829
dd321ecf 830 colo_compare_iothread(s);
7dce4e6f
ZC
831 return;
832}
833
dfd917a9
HZ
834static void colo_flush_packets(void *opaque, void *user_data)
835{
836 CompareState *s = user_data;
837 Connection *conn = opaque;
838 Packet *pkt = NULL;
839
840 while (!g_queue_is_empty(&conn->primary_list)) {
841 pkt = g_queue_pop_head(&conn->primary_list);
aa3a7032
ZC
842 compare_chr_send(s,
843 pkt->data,
844 pkt->size,
845 pkt->vnet_hdr_len);
dfd917a9
HZ
846 packet_destroy(pkt, NULL);
847 }
848 while (!g_queue_is_empty(&conn->secondary_list)) {
849 pkt = g_queue_pop_head(&conn->secondary_list);
850 packet_destroy(pkt, NULL);
851 }
852}
853
7dce4e6f
ZC
854static void colo_compare_class_init(ObjectClass *oc, void *data)
855{
856 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
857
858 ucc->complete = colo_compare_complete;
859}
860
861static void colo_compare_init(Object *obj)
862{
aa3a7032
ZC
863 CompareState *s = COLO_COMPARE(obj);
864
7dce4e6f
ZC
865 object_property_add_str(obj, "primary_in",
866 compare_get_pri_indev, compare_set_pri_indev,
867 NULL);
868 object_property_add_str(obj, "secondary_in",
869 compare_get_sec_indev, compare_set_sec_indev,
870 NULL);
871 object_property_add_str(obj, "outdev",
872 compare_get_outdev, compare_set_outdev,
873 NULL);
dd321ecf
WY
874 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
875 (Object **)&s->iothread,
876 object_property_allow_set_link,
877 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
aa3a7032
ZC
878
879 s->vnet_hdr = false;
880 object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
881 compare_set_vnet_hdr, NULL);
7dce4e6f
ZC
882}
883
884static void colo_compare_finalize(Object *obj)
885{
886 CompareState *s = COLO_COMPARE(obj);
887
1ce2610c
MAL
888 qemu_chr_fe_deinit(&s->chr_pri_in, false);
889 qemu_chr_fe_deinit(&s->chr_sec_in, false);
890 qemu_chr_fe_deinit(&s->chr_out, false);
dd321ecf
WY
891 if (s->iothread) {
892 colo_compare_timer_del(s);
893 }
dfd917a9
HZ
894 /* Release all unhandled packets after compare thead exited */
895 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
896
727c2d76 897 g_queue_clear(&s->conn_list);
0682e15b 898
dd321ecf
WY
899 if (s->connection_track_table) {
900 g_hash_table_destroy(s->connection_track_table);
901 }
902
903 if (s->iothread) {
904 object_unref(OBJECT(s->iothread));
905 }
7dce4e6f
ZC
906 g_free(s->pri_indev);
907 g_free(s->sec_indev);
908 g_free(s->outdev);
909}
910
911static const TypeInfo colo_compare_info = {
912 .name = TYPE_COLO_COMPARE,
913 .parent = TYPE_OBJECT,
914 .instance_size = sizeof(CompareState),
915 .instance_init = colo_compare_init,
916 .instance_finalize = colo_compare_finalize,
917 .class_size = sizeof(CompareClass),
918 .class_init = colo_compare_class_init,
919 .interfaces = (InterfaceInfo[]) {
920 { TYPE_USER_CREATABLE },
921 { }
922 }
923};
924
925static void register_types(void)
926{
927 type_register_static(&colo_compare_info);
928}
929
930type_init(register_types);