blob: f15779dedcb578b79458e736c7ff774d891f5965 [file] [log] [blame]
Zhang Chen7dce4e62016-09-27 10:22:26 +08001/*
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"
Markus Armbrustera8d25322019-05-23 16:35:08 +020016#include "qemu-common.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080017#include "qemu/error-report.h"
Zhang Chen59509ec2016-09-27 10:22:27 +080018#include "trace.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080019#include "qapi/error.h"
20#include "net/net.h"
Zhang Chenf4b61832016-09-27 10:22:31 +080021#include "net/eth.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080022#include "qom/object_interfaces.h"
23#include "qemu/iov.h"
24#include "qom/object.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080025#include "net/queue.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040026#include "chardev/char-fe.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080027#include "qemu/sockets.h"
Michael S. Tsirkinf27f01d2018-05-03 22:50:56 +030028#include "colo.h"
Wang Yongdd321ec2017-08-29 15:22:38 +080029#include "sysemu/iothread.h"
Zhang Chen0ffcece2018-09-03 12:38:43 +080030#include "net/colo-compare.h"
31#include "migration/colo.h"
Zhang Chendccd0312018-09-03 12:38:44 +080032#include "migration/migration.h"
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +040033#include "util.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080034
Lukas Straub9c55fe92020-05-22 15:53:53 +080035#include "block/aio-wait.h"
36#include "qemu/coroutine.h"
37
Zhang Chen7dce4e62016-09-27 10:22:26 +080038#define TYPE_COLO_COMPARE "colo-compare"
39#define COLO_COMPARE(obj) \
40 OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE)
41
Zhang Chen0ffcece2018-09-03 12:38:43 +080042static QTAILQ_HEAD(, CompareState) net_compares =
43 QTAILQ_HEAD_INITIALIZER(net_compares);
44
Zhang Chendccd0312018-09-03 12:38:44 +080045static NotifierList colo_compare_notifiers =
46 NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers);
47
Zhang Chen0682e152016-09-27 10:22:30 +080048#define COMPARE_READ_LEN_MAX NET_BUFSIZE
Zhang Chenb6540d42016-09-27 10:22:29 +080049#define MAX_QUEUE_SIZE 1024
50
Mao Zhongyif449c9e2017-12-25 10:54:12 +080051#define COLO_COMPARE_FREE_PRIMARY 0x01
52#define COLO_COMPARE_FREE_SECONDARY 0x02
53
Zhang Chen0682e152016-09-27 10:22:30 +080054#define REGULAR_PACKET_CHECK_MS 3000
Zhang Chen9cc43c92020-03-18 16:23:19 +080055#define DEFAULT_TIME_OUT_MS 3000
Zhang Chen0682e152016-09-27 10:22:30 +080056
Lukas Straub45942b72020-05-22 15:53:55 +080057static QemuMutex colo_compare_mutex;
58static bool colo_compare_active;
Zhang Chen0ffcece2018-09-03 12:38:43 +080059static QemuMutex event_mtx;
60static QemuCond event_complete_cond;
61static int event_unhandled_count;
62
Zhang Chen59509ec2016-09-27 10:22:27 +080063/*
Zhang Chen61c5f462017-09-05 14:31:06 +080064 * + CompareState ++
65 * | |
66 * +---------------+ +---------------+ +---------------+
67 * | conn list + - > conn + ------- > conn + -- > ......
68 * +---------------+ +---------------+ +---------------+
69 * | | | | | |
70 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
71 * |primary | |secondary |primary | |secondary
72 * |packet | |packet + |packet | |packet +
73 * +--------+ +--------+ +--------+ +--------+
74 * | | | |
75 * +---v----+ +---v----+ +---v----+ +---v----+
76 * |primary | |secondary |primary | |secondary
77 * |packet | |packet + |packet | |packet +
78 * +--------+ +--------+ +--------+ +--------+
79 * | | | |
80 * +---v----+ +---v----+ +---v----+ +---v----+
81 * |primary | |secondary |primary | |secondary
82 * |packet | |packet + |packet | |packet +
83 * +--------+ +--------+ +--------+ +--------+
84 */
Lukas Straub9c55fe92020-05-22 15:53:53 +080085
86typedef struct SendCo {
87 Coroutine *co;
88 struct CompareState *s;
89 CharBackend *chr;
90 GQueue send_list;
91 bool notify_remote_frame;
92 bool done;
93 int ret;
94} SendCo;
95
96typedef struct SendEntry {
97 uint32_t size;
98 uint32_t vnet_hdr_len;
99 uint8_t *buf;
100} SendEntry;
101
Zhang Chen7dce4e62016-09-27 10:22:26 +0800102typedef struct CompareState {
103 Object parent;
104
105 char *pri_indev;
106 char *sec_indev;
107 char *outdev;
Zhang Chencf6af762019-06-10 00:44:29 +0800108 char *notify_dev;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300109 CharBackend chr_pri_in;
110 CharBackend chr_sec_in;
111 CharBackend chr_out;
Zhang Chen13025fe2019-06-10 00:44:30 +0800112 CharBackend chr_notify_dev;
Zhang Chen7dce4e62016-09-27 10:22:26 +0800113 SocketReadState pri_rs;
114 SocketReadState sec_rs;
Zhang Chen13025fe2019-06-10 00:44:30 +0800115 SocketReadState notify_rs;
Lukas Straub9c55fe92020-05-22 15:53:53 +0800116 SendCo out_sendco;
117 SendCo notify_sendco;
Zhang Chenaa3a7032017-07-04 14:53:52 +0800118 bool vnet_hdr;
Zhang Chen9cc43c92020-03-18 16:23:19 +0800119 uint32_t compare_timeout;
Zhang Chencca35ac2020-03-18 16:23:20 +0800120 uint32_t expired_scan_cycle;
Zhang Chen59509ec2016-09-27 10:22:27 +0800121
Zhang Chen61c5f462017-09-05 14:31:06 +0800122 /*
123 * Record the connection that through the NIC
124 * Element type: Connection
Zhang Chenb6540d42016-09-27 10:22:29 +0800125 */
126 GQueue conn_list;
Zhang Chen61c5f462017-09-05 14:31:06 +0800127 /* Record the connection without repetition */
Zhang Chen59509ec2016-09-27 10:22:27 +0800128 GHashTable *connection_track_table;
zhanghailiangdfd917a2017-02-17 10:53:12 +0800129
Wang Yongdd321ec2017-08-29 15:22:38 +0800130 IOThread *iothread;
zhanghailiangb43decb2017-02-17 10:53:14 +0800131 GMainContext *worker_context;
Wang Yongdd321ec2017-08-29 15:22:38 +0800132 QEMUTimer *packet_check_timer;
Zhang Chen0ffcece2018-09-03 12:38:43 +0800133
134 QEMUBH *event_bh;
135 enum colo_event event;
136
137 QTAILQ_ENTRY(CompareState) next;
Zhang Chen7dce4e62016-09-27 10:22:26 +0800138} CompareState;
139
140typedef struct CompareClass {
141 ObjectClass parent_class;
142} CompareClass;
143
Zhang Chen59509ec2016-09-27 10:22:27 +0800144enum {
145 PRIMARY_IN = 0,
146 SECONDARY_IN,
147};
148
Derek Subdadbb02020-05-22 15:53:57 +0800149static const char *colo_mode[] = {
150 [PRIMARY_IN] = "primary",
151 [SECONDARY_IN] = "secondary",
152};
Zhang Chen24525e92018-09-03 12:38:57 +0800153
Zhang Chen3037e7a2017-07-04 14:53:51 +0800154static int compare_chr_send(CompareState *s,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800155 uint8_t *buf,
Zhang Chenaa3a7032017-07-04 14:53:52 +0800156 uint32_t size,
Zhang Chen30685c02019-06-10 00:44:31 +0800157 uint32_t vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800158 bool notify_remote_frame,
159 bool zero_copy);
Zhang Chen59509ec2016-09-27 10:22:27 +0800160
Zhang Chenf77bed12019-07-23 01:18:28 +0800161static bool packet_matches_str(const char *str,
162 const uint8_t *buf,
163 uint32_t packet_len)
164{
165 if (packet_len != strlen(str)) {
166 return false;
167 }
168
169 return !memcmp(str, buf, strlen(str));
170}
171
Zhang Chen1d09f702019-06-10 00:44:32 +0800172static void notify_remote_frame(CompareState *s)
173{
174 char msg[] = "DO_CHECKPOINT";
175 int ret = 0;
176
Lukas Straub9c55fe92020-05-22 15:53:53 +0800177 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
Zhang Chen1d09f702019-06-10 00:44:32 +0800178 if (ret < 0) {
179 error_report("Notify Xen COLO-frame failed");
180 }
181}
182
183static void colo_compare_inconsistency_notify(CompareState *s)
184{
185 if (s->notify_dev) {
186 notify_remote_frame(s);
187 } else {
188 notifier_list_notify(&colo_compare_notifiers,
189 migrate_get_current());
190 }
191}
192
Zhang Chena935cc32017-01-24 16:53:46 +0800193static gint seq_sorter(Packet *a, Packet *b, gpointer data)
194{
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +0400195 struct tcp_hdr *atcp, *btcp;
Zhang Chena935cc32017-01-24 16:53:46 +0800196
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +0400197 atcp = (struct tcp_hdr *)(a->transport_header);
198 btcp = (struct tcp_hdr *)(b->transport_header);
Zhang Chena935cc32017-01-24 16:53:46 +0800199 return ntohl(atcp->th_seq) - ntohl(btcp->th_seq);
200}
201
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800202static void fill_pkt_tcp_info(void *data, uint32_t *max_ack)
203{
204 Packet *pkt = data;
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +0400205 struct tcp_hdr *tcphd;
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800206
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +0400207 tcphd = (struct tcp_hdr *)pkt->transport_header;
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800208
209 pkt->tcp_seq = ntohl(tcphd->th_seq);
210 pkt->tcp_ack = ntohl(tcphd->th_ack);
211 *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack;
212 pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data
213 + (tcphd->th_off << 2) - pkt->vnet_hdr_len;
214 pkt->payload_size = pkt->size - pkt->header_size;
215 pkt->seq_end = pkt->tcp_seq + pkt->payload_size;
216 pkt->flags = tcphd->th_flags;
217}
218
Zhang Chen59509ec2016-09-27 10:22:27 +0800219/*
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800220 * Return 1 on success, if return 0 means the
221 * packet will be dropped
222 */
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800223static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack)
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800224{
225 if (g_queue_get_length(queue) <= MAX_QUEUE_SIZE) {
226 if (pkt->ip->ip_p == IPPROTO_TCP) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800227 fill_pkt_tcp_info(pkt, max_ack);
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800228 g_queue_insert_sorted(queue,
229 pkt,
230 (GCompareDataFunc)seq_sorter,
231 NULL);
232 } else {
233 g_queue_push_tail(queue, pkt);
234 }
235 return 1;
236 }
237 return 0;
238}
239
240/*
Zhang Chen59509ec2016-09-27 10:22:27 +0800241 * Return 0 on success, if return -1 means the pkt
242 * is unsupported(arp and ipv6) and will be sent later
243 */
Mao Zhongyi8ec14402017-10-13 14:32:07 +0800244static int packet_enqueue(CompareState *s, int mode, Connection **con)
Zhang Chen59509ec2016-09-27 10:22:27 +0800245{
Zhang Chenb6540d42016-09-27 10:22:29 +0800246 ConnectionKey key;
Zhang Chen59509ec2016-09-27 10:22:27 +0800247 Packet *pkt = NULL;
Zhang Chenb6540d42016-09-27 10:22:29 +0800248 Connection *conn;
Derek Subdadbb02020-05-22 15:53:57 +0800249 int ret;
Zhang Chen59509ec2016-09-27 10:22:27 +0800250
251 if (mode == PRIMARY_IN) {
Zhang Chenada1a332017-07-04 14:53:50 +0800252 pkt = packet_new(s->pri_rs.buf,
253 s->pri_rs.packet_len,
254 s->pri_rs.vnet_hdr_len);
Zhang Chen59509ec2016-09-27 10:22:27 +0800255 } else {
Zhang Chenada1a332017-07-04 14:53:50 +0800256 pkt = packet_new(s->sec_rs.buf,
257 s->sec_rs.packet_len,
258 s->sec_rs.vnet_hdr_len);
Zhang Chen59509ec2016-09-27 10:22:27 +0800259 }
260
261 if (parse_packet_early(pkt)) {
262 packet_destroy(pkt, NULL);
263 pkt = NULL;
264 return -1;
265 }
Zhang Chenb6540d42016-09-27 10:22:29 +0800266 fill_connection_key(pkt, &key);
Zhang Chen59509ec2016-09-27 10:22:27 +0800267
Zhang Chenb6540d42016-09-27 10:22:29 +0800268 conn = connection_get(s->connection_track_table,
269 &key,
270 &s->conn_list);
Zhang Chen59509ec2016-09-27 10:22:27 +0800271
Zhang Chenb6540d42016-09-27 10:22:29 +0800272 if (!conn->processing) {
273 g_queue_push_tail(&s->conn_list, conn);
274 conn->processing = true;
275 }
276
277 if (mode == PRIMARY_IN) {
Derek Subdadbb02020-05-22 15:53:57 +0800278 ret = colo_insert_packet(&conn->primary_list, pkt, &conn->pack);
Zhang Chenb6540d42016-09-27 10:22:29 +0800279 } else {
Derek Subdadbb02020-05-22 15:53:57 +0800280 ret = colo_insert_packet(&conn->secondary_list, pkt, &conn->sack);
Zhang Chenb6540d42016-09-27 10:22:29 +0800281 }
Derek Subdadbb02020-05-22 15:53:57 +0800282
283 if (!ret) {
284 trace_colo_compare_drop_packet(colo_mode[mode],
285 "queue size too big, drop packet");
286 packet_destroy(pkt, NULL);
287 pkt = NULL;
288 }
289
Mao Zhongyi4d366232017-11-16 10:28:32 +0800290 *con = conn;
Zhang Chen59509ec2016-09-27 10:22:27 +0800291
292 return 0;
293}
294
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800295static inline bool after(uint32_t seq1, uint32_t seq2)
296{
297 return (int32_t)(seq1 - seq2) > 0;
298}
299
300static void colo_release_primary_pkt(CompareState *s, Packet *pkt)
301{
302 int ret;
303 ret = compare_chr_send(s,
304 pkt->data,
305 pkt->size,
Zhang Chen30685c02019-06-10 00:44:31 +0800306 pkt->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800307 false,
308 true);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800309 if (ret < 0) {
310 error_report("colo send primary packet failed");
311 }
312 trace_colo_compare_main("packet same and release packet");
Lukas Straub9c55fe92020-05-22 15:53:53 +0800313 packet_destroy_partial(pkt, NULL);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800314}
315
Zhang Chen0682e152016-09-27 10:22:30 +0800316/*
317 * The IP packets sent by primary and secondary
318 * will be compared in here
319 * TODO support ip fragment, Out-Of-Order
320 * return: 0 means packet same
321 * > 0 || < 0 means packet different
322 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800323static int colo_compare_packet_payload(Packet *ppkt,
324 Packet *spkt,
325 uint16_t poffset,
326 uint16_t soffset,
327 uint16_t len)
328
Zhang Chen0682e152016-09-27 10:22:30 +0800329{
Stefan Hajnoczid87aa132017-07-31 15:07:18 +0100330 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
Zhang Chene630b2b2017-03-09 15:40:07 +0800331 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
332
333 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
334 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
335 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
336 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
337
338 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
339 pri_ip_dst, spkt->size,
340 sec_ip_src, sec_ip_dst);
341 }
Zhang Chen0682e152016-09-27 10:22:30 +0800342
Mao Zhongyi93941332017-12-25 10:54:11 +0800343 return memcmp(ppkt->data + poffset, spkt->data + soffset, len);
Zhang Chen0682e152016-09-27 10:22:30 +0800344}
345
Zhang Chenf4b61832016-09-27 10:22:31 +0800346/*
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800347 * return true means that the payload is consist and
348 * need to make the next comparison, false means do
349 * the checkpoint
350*/
351static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
352 int8_t *mark, uint32_t max_ack)
Zhang Chen0682e152016-09-27 10:22:30 +0800353{
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800354 *mark = 0;
Zhang Chenf4b61832016-09-27 10:22:31 +0800355
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800356 if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) {
Fan Yang1e907a32019-09-24 22:08:29 +0800357 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800358 ppkt->header_size, spkt->header_size,
359 ppkt->payload_size)) {
360 *mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY;
361 return true;
362 }
363 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800364
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800365 /* one part of secondary packet payload still need to be compared */
366 if (!after(ppkt->seq_end, spkt->seq_end)) {
Fan Yang1e907a32019-09-24 22:08:29 +0800367 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800368 ppkt->header_size + ppkt->offset,
369 spkt->header_size + spkt->offset,
370 ppkt->payload_size - ppkt->offset)) {
371 if (!after(ppkt->tcp_ack, max_ack)) {
372 *mark = COLO_COMPARE_FREE_PRIMARY;
373 spkt->offset += ppkt->payload_size - ppkt->offset;
374 return true;
375 } else {
376 /* secondary guest hasn't ack the data, don't send
377 * out this packet
378 */
379 return false;
380 }
381 }
Zhang Chen6efeb322017-03-02 17:54:17 +0800382 } else {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800383 /* primary packet is longer than secondary packet, compare
384 * the same part and mark the primary packet offset
385 */
Fan Yang1e907a32019-09-24 22:08:29 +0800386 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800387 ppkt->header_size + ppkt->offset,
388 spkt->header_size + spkt->offset,
389 spkt->payload_size - spkt->offset)) {
390 *mark = COLO_COMPARE_FREE_SECONDARY;
391 ppkt->offset += spkt->payload_size - spkt->offset;
392 return true;
393 }
Zhang Chen6efeb322017-03-02 17:54:17 +0800394 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800395
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800396 return false;
397}
Alex Bennée2dfe5112016-10-28 14:25:59 +0100398
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800399static void colo_compare_tcp(CompareState *s, Connection *conn)
400{
401 Packet *ppkt = NULL, *spkt = NULL;
402 int8_t mark;
Zhang Chenf583dca2017-04-27 11:46:45 +0800403
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800404 /*
405 * If ppkt and spkt have the same payload, but ppkt's ACK
406 * is greater than spkt's ACK, in this case we can not
407 * send the ppkt because it will cause the secondary guest
408 * to miss sending some data in the next. Therefore, we
409 * record the maximum ACK in the current queue at both
410 * primary side and secondary side. Only when the ack is
411 * less than the smaller of the two maximum ack, then we
412 * can ensure that the packet's payload is acknowledged by
413 * primary and secondary.
414 */
415 uint32_t min_ack = conn->pack > conn->sack ? conn->sack : conn->pack;
Zhang Chenf583dca2017-04-27 11:46:45 +0800416
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800417pri:
418 if (g_queue_is_empty(&conn->primary_list)) {
419 return;
420 }
421 ppkt = g_queue_pop_head(&conn->primary_list);
422sec:
423 if (g_queue_is_empty(&conn->secondary_list)) {
424 g_queue_push_head(&conn->primary_list, ppkt);
425 return;
426 }
427 spkt = g_queue_pop_head(&conn->secondary_list);
Zhang Chenf583dca2017-04-27 11:46:45 +0800428
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800429 if (ppkt->tcp_seq == ppkt->seq_end) {
430 colo_release_primary_pkt(s, ppkt);
431 ppkt = NULL;
432 }
433
434 if (ppkt && conn->compare_seq && !after(ppkt->seq_end, conn->compare_seq)) {
435 trace_colo_compare_main("pri: this packet has compared");
436 colo_release_primary_pkt(s, ppkt);
437 ppkt = NULL;
438 }
439
440 if (spkt->tcp_seq == spkt->seq_end) {
441 packet_destroy(spkt, NULL);
442 if (!ppkt) {
443 goto pri;
444 } else {
445 goto sec;
446 }
447 } else {
448 if (conn->compare_seq && !after(spkt->seq_end, conn->compare_seq)) {
449 trace_colo_compare_main("sec: this packet has compared");
450 packet_destroy(spkt, NULL);
451 if (!ppkt) {
452 goto pri;
453 } else {
454 goto sec;
455 }
456 }
457 if (!ppkt) {
458 g_queue_push_head(&conn->secondary_list, spkt);
459 goto pri;
460 }
461 }
462
463 if (colo_mark_tcp_pkt(ppkt, spkt, &mark, min_ack)) {
464 trace_colo_compare_tcp_info("pri",
465 ppkt->tcp_seq, ppkt->tcp_ack,
466 ppkt->header_size, ppkt->payload_size,
467 ppkt->offset, ppkt->flags);
468
469 trace_colo_compare_tcp_info("sec",
470 spkt->tcp_seq, spkt->tcp_ack,
471 spkt->header_size, spkt->payload_size,
472 spkt->offset, spkt->flags);
473
474 if (mark == COLO_COMPARE_FREE_PRIMARY) {
475 conn->compare_seq = ppkt->seq_end;
476 colo_release_primary_pkt(s, ppkt);
477 g_queue_push_head(&conn->secondary_list, spkt);
478 goto pri;
479 }
480 if (mark == COLO_COMPARE_FREE_SECONDARY) {
481 conn->compare_seq = spkt->seq_end;
482 packet_destroy(spkt, NULL);
483 goto sec;
484 }
485 if (mark == (COLO_COMPARE_FREE_PRIMARY | COLO_COMPARE_FREE_SECONDARY)) {
486 conn->compare_seq = ppkt->seq_end;
487 colo_release_primary_pkt(s, ppkt);
488 packet_destroy(spkt, NULL);
489 goto pri;
490 }
491 } else {
492 g_queue_push_head(&conn->primary_list, ppkt);
493 g_queue_push_head(&conn->secondary_list, spkt);
Zhang Chenf4b61832016-09-27 10:22:31 +0800494
Lukas Straub76658542020-05-22 15:53:54 +0800495 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
496 qemu_hexdump((char *)ppkt->data, stderr,
497 "colo-compare ppkt", ppkt->size);
498 qemu_hexdump((char *)spkt->data, stderr,
499 "colo-compare spkt", spkt->size);
500 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800501
Zhang Chen1d09f702019-06-10 00:44:32 +0800502 colo_compare_inconsistency_notify(s);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800503 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800504}
505
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800506
Zhang Chenf4b61832016-09-27 10:22:31 +0800507/*
508 * Called from the compare thread on the primary
509 * for compare udp packet
510 */
511static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
512{
Mao Zhongyi93941332017-12-25 10:54:11 +0800513 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
514 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
Zhang Chenf4b61832016-09-27 10:22:31 +0800515
516 trace_colo_compare_main("compare udp");
Zhang Chen2ad7ca42017-03-02 17:54:16 +0800517
Zhang Chen6efeb322017-03-02 17:54:17 +0800518 /*
519 * Because of ppkt and spkt are both in the same connection,
520 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
521 * same with spkt. In addition, IP header's Identification is a random
522 * field, we can handle it in IP fragmentation function later.
523 * COLO just concern the response net packet payload from primary guest
524 * and secondary guest are same or not, So we ignored all IP header include
525 * other field like TOS,TTL,IP Checksum. we only need to compare
526 * the ip payload here.
527 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800528 if (ppkt->size != spkt->size) {
529 trace_colo_compare_main("UDP: payload size of packets are different");
530 return -1;
531 }
532 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
533 ppkt->size - offset)) {
Zhang Chenf4b61832016-09-27 10:22:31 +0800534 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
Zhang Chenf4b61832016-09-27 10:22:31 +0800535 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
Stefan Hajnoczid87aa132017-07-31 15:07:18 +0100536 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
Zhang Chen1723a7f2017-03-02 17:54:18 +0800537 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
538 ppkt->size);
539 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
540 spkt->size);
541 }
Mao Zhongyi93941332017-12-25 10:54:11 +0800542 return -1;
543 } else {
544 return 0;
Zhang Chenf4b61832016-09-27 10:22:31 +0800545 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800546}
547
548/*
549 * Called from the compare thread on the primary
550 * for compare icmp packet
551 */
552static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
553{
Mao Zhongyi93941332017-12-25 10:54:11 +0800554 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
555 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
Zhang Chen6efeb322017-03-02 17:54:17 +0800556
Zhang Chenf4b61832016-09-27 10:22:31 +0800557 trace_colo_compare_main("compare icmp");
Zhang Chenf4b61832016-09-27 10:22:31 +0800558
Zhang Chen6efeb322017-03-02 17:54:17 +0800559 /*
560 * Because of ppkt and spkt are both in the same connection,
561 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
562 * same with spkt. In addition, IP header's Identification is a random
563 * field, we can handle it in IP fragmentation function later.
564 * COLO just concern the response net packet payload from primary guest
565 * and secondary guest are same or not, So we ignored all IP header include
566 * other field like TOS,TTL,IP Checksum. we only need to compare
567 * the ip payload here.
568 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800569 if (ppkt->size != spkt->size) {
570 trace_colo_compare_main("ICMP: payload size of packets are different");
571 return -1;
572 }
573 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
574 ppkt->size - offset)) {
Zhang Chenf4b61832016-09-27 10:22:31 +0800575 trace_colo_compare_icmp_miscompare("primary pkt size",
576 ppkt->size);
Zhang Chenf4b61832016-09-27 10:22:31 +0800577 trace_colo_compare_icmp_miscompare("Secondary pkt size",
578 spkt->size);
Stefan Hajnoczid87aa132017-07-31 15:07:18 +0100579 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
Zhang Chen1723a7f2017-03-02 17:54:18 +0800580 qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt",
581 ppkt->size);
582 qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt",
583 spkt->size);
584 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800585 return -1;
586 } else {
587 return 0;
588 }
589}
590
591/*
592 * Called from the compare thread on the primary
593 * for compare other packet
594 */
595static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
596{
Mao Zhongyi93941332017-12-25 10:54:11 +0800597 uint16_t offset = ppkt->vnet_hdr_len;
598
Zhang Chenf4b61832016-09-27 10:22:31 +0800599 trace_colo_compare_main("compare other");
Stefan Hajnoczid87aa132017-07-31 15:07:18 +0100600 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) {
Zhang Chene630b2b2017-03-09 15:40:07 +0800601 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
602
603 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
604 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
605 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
606 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
607
608 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
609 pri_ip_dst, spkt->size,
610 sec_ip_src, sec_ip_dst);
611 }
612
Mao Zhongyi93941332017-12-25 10:54:11 +0800613 if (ppkt->size != spkt->size) {
614 trace_colo_compare_main("Other: payload size of packets are different");
615 return -1;
616 }
617 return colo_compare_packet_payload(ppkt, spkt, offset, offset,
618 ppkt->size - offset);
Zhang Chen0682e152016-09-27 10:22:30 +0800619}
620
621static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
622{
623 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
624
625 if ((now - pkt->creation_ms) > (*check_time)) {
626 trace_colo_old_packet_check_found(pkt->creation_ms);
627 return 0;
628 } else {
629 return 1;
630 }
631}
632
Zhang Chendccd0312018-09-03 12:38:44 +0800633void colo_compare_register_notifier(Notifier *notify)
634{
635 notifier_list_add(&colo_compare_notifiers, notify);
636}
637
638void colo_compare_unregister_notifier(Notifier *notify)
639{
640 notifier_remove(notify);
641}
642
Zhang Chend25a7da2017-04-12 20:12:07 +0800643static int colo_old_packet_check_one_conn(Connection *conn,
Zhang Chen1d09f702019-06-10 00:44:32 +0800644 CompareState *s)
Zhang Chen0682e152016-09-27 10:22:30 +0800645{
Zhang Chen0682e152016-09-27 10:22:30 +0800646 GList *result = NULL;
Zhang Chen0682e152016-09-27 10:22:30 +0800647
648 result = g_queue_find_custom(&conn->primary_list,
Zhang Chen9cc43c92020-03-18 16:23:19 +0800649 &s->compare_timeout,
Zhang Chen0682e152016-09-27 10:22:30 +0800650 (GCompareFunc)colo_old_packet_check_one);
651
652 if (result) {
Zhang Chen61c5f462017-09-05 14:31:06 +0800653 /* Do checkpoint will flush old packet */
Zhang Chen1d09f702019-06-10 00:44:32 +0800654 colo_compare_inconsistency_notify(s);
Zhang Chend25a7da2017-04-12 20:12:07 +0800655 return 0;
Zhang Chen0682e152016-09-27 10:22:30 +0800656 }
Zhang Chend25a7da2017-04-12 20:12:07 +0800657
658 return 1;
Zhang Chen0682e152016-09-27 10:22:30 +0800659}
660
661/*
662 * Look for old packets that the secondary hasn't matched,
663 * if we have some then we have to checkpoint to wake
664 * the secondary up.
665 */
666static void colo_old_packet_check(void *opaque)
667{
668 CompareState *s = opaque;
669
Zhang Chend25a7da2017-04-12 20:12:07 +0800670 /*
671 * If we find one old packet, stop finding job and notify
672 * COLO frame do checkpoint.
673 */
Zhang Chen1d09f702019-06-10 00:44:32 +0800674 g_queue_find_custom(&s->conn_list, s,
Zhang Chend25a7da2017-04-12 20:12:07 +0800675 (GCompareFunc)colo_old_packet_check_one_conn);
Zhang Chen0682e152016-09-27 10:22:30 +0800676}
677
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800678static void colo_compare_packet(CompareState *s, Connection *conn,
679 int (*HandlePacket)(Packet *spkt,
680 Packet *ppkt))
Zhang Chen0682e152016-09-27 10:22:30 +0800681{
Zhang Chen0682e152016-09-27 10:22:30 +0800682 Packet *pkt = NULL;
683 GList *result = NULL;
Zhang Chen0682e152016-09-27 10:22:30 +0800684
685 while (!g_queue_is_empty(&conn->primary_list) &&
686 !g_queue_is_empty(&conn->secondary_list)) {
Zhang Chen626bba92017-09-05 14:31:05 +0800687 pkt = g_queue_pop_head(&conn->primary_list);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800688 result = g_queue_find_custom(&conn->secondary_list,
689 pkt, (GCompareFunc)HandlePacket);
Zhang Chen0682e152016-09-27 10:22:30 +0800690
691 if (result) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800692 colo_release_primary_pkt(s, pkt);
Zhang Chen0682e152016-09-27 10:22:30 +0800693 g_queue_remove(&conn->secondary_list, result->data);
Zhang Chen0682e152016-09-27 10:22:30 +0800694 } else {
695 /*
696 * If one packet arrive late, the secondary_list or
697 * primary_list will be empty, so we can't compare it
Zhang Chendccd0312018-09-03 12:38:44 +0800698 * until next comparison. If the packets in the list are
699 * timeout, it will trigger a checkpoint request.
Zhang Chen0682e152016-09-27 10:22:30 +0800700 */
701 trace_colo_compare_main("packet different");
Zhang Chen626bba92017-09-05 14:31:05 +0800702 g_queue_push_head(&conn->primary_list, pkt);
Zhang Chen1d09f702019-06-10 00:44:32 +0800703
704 colo_compare_inconsistency_notify(s);
Zhang Chen0682e152016-09-27 10:22:30 +0800705 break;
706 }
707 }
708}
709
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800710/*
711 * Called from the compare thread on the primary
712 * for compare packet with secondary list of the
713 * specified connection when a new packet was
714 * queued to it.
715 */
716static void colo_compare_connection(void *opaque, void *user_data)
717{
718 CompareState *s = user_data;
719 Connection *conn = opaque;
720
721 switch (conn->ip_proto) {
722 case IPPROTO_TCP:
723 colo_compare_tcp(s, conn);
724 break;
725 case IPPROTO_UDP:
726 colo_compare_packet(s, conn, colo_packet_compare_udp);
727 break;
728 case IPPROTO_ICMP:
729 colo_compare_packet(s, conn, colo_packet_compare_icmp);
730 break;
731 default:
732 colo_compare_packet(s, conn, colo_packet_compare_other);
733 break;
734 }
735}
736
Lukas Straub9c55fe92020-05-22 15:53:53 +0800737static void coroutine_fn _compare_chr_send(void *opaque)
738{
739 SendCo *sendco = opaque;
740 CompareState *s = sendco->s;
741 int ret = 0;
742
743 while (!g_queue_is_empty(&sendco->send_list)) {
744 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
745 uint32_t len = htonl(entry->size);
746
747 ret = qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof(len));
748
749 if (ret != sizeof(len)) {
750 g_free(entry->buf);
751 g_slice_free(SendEntry, entry);
752 goto err;
753 }
754
755 if (!sendco->notify_remote_frame && s->vnet_hdr) {
756 /*
757 * We send vnet header len make other module(like filter-redirector)
758 * know how to parse net packet correctly.
759 */
760 len = htonl(entry->vnet_hdr_len);
761
762 ret = qemu_chr_fe_write_all(sendco->chr,
763 (uint8_t *)&len,
764 sizeof(len));
765
766 if (ret != sizeof(len)) {
767 g_free(entry->buf);
768 g_slice_free(SendEntry, entry);
769 goto err;
770 }
771 }
772
773 ret = qemu_chr_fe_write_all(sendco->chr,
774 (uint8_t *)entry->buf,
775 entry->size);
776
777 if (ret != entry->size) {
778 g_free(entry->buf);
779 g_slice_free(SendEntry, entry);
780 goto err;
781 }
782
783 g_free(entry->buf);
784 g_slice_free(SendEntry, entry);
785 }
786
787 sendco->ret = 0;
788 goto out;
789
790err:
791 while (!g_queue_is_empty(&sendco->send_list)) {
792 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
793 g_free(entry->buf);
794 g_slice_free(SendEntry, entry);
795 }
796 sendco->ret = ret < 0 ? ret : -EIO;
797out:
798 sendco->co = NULL;
799 sendco->done = true;
800 aio_wait_kick();
801}
802
Zhang Chen3037e7a2017-07-04 14:53:51 +0800803static int compare_chr_send(CompareState *s,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800804 uint8_t *buf,
Zhang Chenaa3a7032017-07-04 14:53:52 +0800805 uint32_t size,
Zhang Chen30685c02019-06-10 00:44:31 +0800806 uint32_t vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800807 bool notify_remote_frame,
808 bool zero_copy)
Zhang Chen59509ec2016-09-27 10:22:27 +0800809{
Lukas Straub9c55fe92020-05-22 15:53:53 +0800810 SendCo *sendco;
811 SendEntry *entry;
812
813 if (notify_remote_frame) {
814 sendco = &s->notify_sendco;
815 } else {
816 sendco = &s->out_sendco;
817 }
Zhang Chen59509ec2016-09-27 10:22:27 +0800818
819 if (!size) {
820 return 0;
821 }
822
Lukas Straub9c55fe92020-05-22 15:53:53 +0800823 entry = g_slice_new(SendEntry);
824 entry->size = size;
825 entry->vnet_hdr_len = vnet_hdr_len;
826 if (zero_copy) {
827 entry->buf = buf;
Zhang Chen30685c02019-06-10 00:44:31 +0800828 } else {
Lukas Straub9c55fe92020-05-22 15:53:53 +0800829 entry->buf = g_malloc(size);
830 memcpy(entry->buf, buf, size);
Zhang Chen30685c02019-06-10 00:44:31 +0800831 }
Lukas Straub9c55fe92020-05-22 15:53:53 +0800832 g_queue_push_head(&sendco->send_list, entry);
Zhang Chen30685c02019-06-10 00:44:31 +0800833
Lukas Straub9c55fe92020-05-22 15:53:53 +0800834 if (sendco->done) {
835 sendco->co = qemu_coroutine_create(_compare_chr_send, sendco);
836 sendco->done = false;
837 qemu_coroutine_enter(sendco->co);
838 if (sendco->done) {
839 /* report early errors */
840 return sendco->ret;
Zhang Chenaa3a7032017-07-04 14:53:52 +0800841 }
842 }
843
Lukas Straub9c55fe92020-05-22 15:53:53 +0800844 /* assume success */
Zhang Chen59509ec2016-09-27 10:22:27 +0800845 return 0;
Zhang Chen59509ec2016-09-27 10:22:27 +0800846}
847
Zhang Chen0682e152016-09-27 10:22:30 +0800848static int compare_chr_can_read(void *opaque)
849{
850 return COMPARE_READ_LEN_MAX;
851}
852
853/*
854 * Called from the main thread on the primary for packets
855 * arriving over the socket from the primary.
856 */
857static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
858{
859 CompareState *s = COLO_COMPARE(opaque);
860 int ret;
861
862 ret = net_fill_rstate(&s->pri_rs, buf, size);
863 if (ret == -1) {
Anton Nefedov81517ba2017-07-06 15:08:49 +0300864 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300865 NULL, NULL, true);
Zhang Chen0682e152016-09-27 10:22:30 +0800866 error_report("colo-compare primary_in error");
867 }
868}
869
870/*
871 * Called from the main thread on the primary for packets
872 * arriving over the socket from the secondary.
873 */
874static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
875{
876 CompareState *s = COLO_COMPARE(opaque);
877 int ret;
878
879 ret = net_fill_rstate(&s->sec_rs, buf, size);
880 if (ret == -1) {
Anton Nefedov81517ba2017-07-06 15:08:49 +0300881 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300882 NULL, NULL, true);
Zhang Chen0682e152016-09-27 10:22:30 +0800883 error_report("colo-compare secondary_in error");
884 }
885}
886
Zhang Chen13025fe2019-06-10 00:44:30 +0800887static void compare_notify_chr(void *opaque, const uint8_t *buf, int size)
888{
889 CompareState *s = COLO_COMPARE(opaque);
890 int ret;
891
892 ret = net_fill_rstate(&s->notify_rs, buf, size);
893 if (ret == -1) {
894 qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL,
895 NULL, NULL, true);
896 error_report("colo-compare notify_dev error");
897 }
898}
899
zhanghailiang66d2a242017-02-17 10:53:11 +0800900/*
901 * Check old packet regularly so it can watch for any packets
902 * that the secondary hasn't produced equivalents of.
903 */
Wang Yongdd321ec2017-08-29 15:22:38 +0800904static void check_old_packet_regular(void *opaque)
zhanghailiang66d2a242017-02-17 10:53:11 +0800905{
906 CompareState *s = opaque;
907
908 /* if have old packet we will notify checkpoint */
909 colo_old_packet_check(s);
Wang Yongdd321ec2017-08-29 15:22:38 +0800910 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
Zhang Chencca35ac2020-03-18 16:23:20 +0800911 s->expired_scan_cycle);
zhanghailiang66d2a242017-02-17 10:53:11 +0800912}
913
Zhang Chen0ffcece2018-09-03 12:38:43 +0800914/* Public API, Used for COLO frame to notify compare event */
915void colo_notify_compares_event(void *opaque, int event, Error **errp)
916{
917 CompareState *s;
Lukas Straub45942b72020-05-22 15:53:55 +0800918 qemu_mutex_lock(&colo_compare_mutex);
919
920 if (!colo_compare_active) {
921 qemu_mutex_unlock(&colo_compare_mutex);
922 return;
923 }
Zhang Chen0ffcece2018-09-03 12:38:43 +0800924
925 qemu_mutex_lock(&event_mtx);
926 QTAILQ_FOREACH(s, &net_compares, next) {
927 s->event = event;
928 qemu_bh_schedule(s->event_bh);
929 event_unhandled_count++;
930 }
931 /* Wait all compare threads to finish handling this event */
932 while (event_unhandled_count > 0) {
933 qemu_cond_wait(&event_complete_cond, &event_mtx);
934 }
935
936 qemu_mutex_unlock(&event_mtx);
Lukas Straub45942b72020-05-22 15:53:55 +0800937 qemu_mutex_unlock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +0800938}
939
Wang Yongdd321ec2017-08-29 15:22:38 +0800940static void colo_compare_timer_init(CompareState *s)
Zhang Chen0682e152016-09-27 10:22:30 +0800941{
Wang Yongdd321ec2017-08-29 15:22:38 +0800942 AioContext *ctx = iothread_get_aio_context(s->iothread);
Zhang Chen0682e152016-09-27 10:22:30 +0800943
Wang Yongdd321ec2017-08-29 15:22:38 +0800944 s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL,
945 SCALE_MS, check_old_packet_regular,
946 s);
947 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
Zhang Chencca35ac2020-03-18 16:23:20 +0800948 s->expired_scan_cycle);
Wang Yongdd321ec2017-08-29 15:22:38 +0800949}
950
951static void colo_compare_timer_del(CompareState *s)
952{
953 if (s->packet_check_timer) {
954 timer_del(s->packet_check_timer);
955 timer_free(s->packet_check_timer);
956 s->packet_check_timer = NULL;
957 }
958 }
959
Zhang Chen0ffcece2018-09-03 12:38:43 +0800960static void colo_flush_packets(void *opaque, void *user_data);
961
962static void colo_compare_handle_event(void *opaque)
963{
964 CompareState *s = opaque;
965
966 switch (s->event) {
967 case COLO_EVENT_CHECKPOINT:
968 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
969 break;
970 case COLO_EVENT_FAILOVER:
971 break;
972 default:
973 break;
974 }
975
Zhang Chen0ffcece2018-09-03 12:38:43 +0800976 qemu_mutex_lock(&event_mtx);
Lukas Straub78e4f442019-04-20 19:14:25 +0200977 assert(event_unhandled_count > 0);
Zhang Chen0ffcece2018-09-03 12:38:43 +0800978 event_unhandled_count--;
979 qemu_cond_broadcast(&event_complete_cond);
980 qemu_mutex_unlock(&event_mtx);
981}
982
Wang Yongdd321ec2017-08-29 15:22:38 +0800983static void colo_compare_iothread(CompareState *s)
984{
Lukas Straub5893c732020-05-22 15:53:51 +0800985 AioContext *ctx = iothread_get_aio_context(s->iothread);
Wang Yongdd321ec2017-08-29 15:22:38 +0800986 object_ref(OBJECT(s->iothread));
987 s->worker_context = iothread_get_g_main_context(s->iothread);
Zhang Chen0682e152016-09-27 10:22:30 +0800988
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300989 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300990 compare_pri_chr_in, NULL, NULL,
991 s, s->worker_context, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300992 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300993 compare_sec_chr_in, NULL, NULL,
994 s, s->worker_context, true);
Zhang Chen13025fe2019-06-10 00:44:30 +0800995 if (s->notify_dev) {
996 qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read,
997 compare_notify_chr, NULL, NULL,
998 s, s->worker_context, true);
999 }
Zhang Chen0682e152016-09-27 10:22:30 +08001000
Wang Yongdd321ec2017-08-29 15:22:38 +08001001 colo_compare_timer_init(s);
Lukas Straub5893c732020-05-22 15:53:51 +08001002 s->event_bh = aio_bh_new(ctx, colo_compare_handle_event, s);
Zhang Chen0682e152016-09-27 10:22:30 +08001003}
1004
Zhang Chen7dce4e62016-09-27 10:22:26 +08001005static char *compare_get_pri_indev(Object *obj, Error **errp)
1006{
1007 CompareState *s = COLO_COMPARE(obj);
1008
1009 return g_strdup(s->pri_indev);
1010}
1011
1012static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
1013{
1014 CompareState *s = COLO_COMPARE(obj);
1015
1016 g_free(s->pri_indev);
1017 s->pri_indev = g_strdup(value);
1018}
1019
1020static char *compare_get_sec_indev(Object *obj, Error **errp)
1021{
1022 CompareState *s = COLO_COMPARE(obj);
1023
1024 return g_strdup(s->sec_indev);
1025}
1026
1027static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
1028{
1029 CompareState *s = COLO_COMPARE(obj);
1030
1031 g_free(s->sec_indev);
1032 s->sec_indev = g_strdup(value);
1033}
1034
1035static char *compare_get_outdev(Object *obj, Error **errp)
1036{
1037 CompareState *s = COLO_COMPARE(obj);
1038
1039 return g_strdup(s->outdev);
1040}
1041
1042static void compare_set_outdev(Object *obj, const char *value, Error **errp)
1043{
1044 CompareState *s = COLO_COMPARE(obj);
1045
1046 g_free(s->outdev);
1047 s->outdev = g_strdup(value);
1048}
1049
Zhang Chenaa3a7032017-07-04 14:53:52 +08001050static bool compare_get_vnet_hdr(Object *obj, Error **errp)
1051{
1052 CompareState *s = COLO_COMPARE(obj);
1053
1054 return s->vnet_hdr;
1055}
1056
1057static void compare_set_vnet_hdr(Object *obj,
1058 bool value,
1059 Error **errp)
1060{
1061 CompareState *s = COLO_COMPARE(obj);
1062
1063 s->vnet_hdr = value;
1064}
1065
Zhang Chencf6af762019-06-10 00:44:29 +08001066static char *compare_get_notify_dev(Object *obj, Error **errp)
1067{
1068 CompareState *s = COLO_COMPARE(obj);
1069
1070 return g_strdup(s->notify_dev);
1071}
1072
1073static void compare_set_notify_dev(Object *obj, const char *value, Error **errp)
1074{
1075 CompareState *s = COLO_COMPARE(obj);
1076
1077 g_free(s->notify_dev);
1078 s->notify_dev = g_strdup(value);
1079}
1080
Zhang Chen9cc43c92020-03-18 16:23:19 +08001081static void compare_get_timeout(Object *obj, Visitor *v,
1082 const char *name, void *opaque,
1083 Error **errp)
1084{
1085 CompareState *s = COLO_COMPARE(obj);
1086 uint32_t value = s->compare_timeout;
1087
1088 visit_type_uint32(v, name, &value, errp);
1089}
1090
1091static void compare_set_timeout(Object *obj, Visitor *v,
1092 const char *name, void *opaque,
1093 Error **errp)
1094{
1095 CompareState *s = COLO_COMPARE(obj);
1096 Error *local_err = NULL;
1097 uint32_t value;
1098
1099 visit_type_uint32(v, name, &value, &local_err);
1100 if (local_err) {
1101 goto out;
1102 }
1103 if (!value) {
1104 error_setg(&local_err, "Property '%s.%s' requires a positive value",
1105 object_get_typename(obj), name);
1106 goto out;
1107 }
1108 s->compare_timeout = value;
1109
1110out:
1111 error_propagate(errp, local_err);
1112}
1113
Zhang Chencca35ac2020-03-18 16:23:20 +08001114static void compare_get_expired_scan_cycle(Object *obj, Visitor *v,
1115 const char *name, void *opaque,
1116 Error **errp)
1117{
1118 CompareState *s = COLO_COMPARE(obj);
1119 uint32_t value = s->expired_scan_cycle;
1120
1121 visit_type_uint32(v, name, &value, errp);
1122}
1123
1124static void compare_set_expired_scan_cycle(Object *obj, Visitor *v,
1125 const char *name, void *opaque,
1126 Error **errp)
1127{
1128 CompareState *s = COLO_COMPARE(obj);
1129 Error *local_err = NULL;
1130 uint32_t value;
1131
1132 visit_type_uint32(v, name, &value, &local_err);
1133 if (local_err) {
1134 goto out;
1135 }
1136 if (!value) {
1137 error_setg(&local_err, "Property '%s.%s' requires a positive value",
1138 object_get_typename(obj), name);
1139 goto out;
1140 }
1141 s->expired_scan_cycle = value;
1142
1143out:
1144 error_propagate(errp, local_err);
1145}
1146
Zhang Chen7dce4e62016-09-27 10:22:26 +08001147static void compare_pri_rs_finalize(SocketReadState *pri_rs)
1148{
Zhang Chen59509ec2016-09-27 10:22:27 +08001149 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001150 Connection *conn = NULL;
Zhang Chen59509ec2016-09-27 10:22:27 +08001151
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001152 if (packet_enqueue(s, PRIMARY_IN, &conn)) {
Zhang Chen59509ec2016-09-27 10:22:27 +08001153 trace_colo_compare_main("primary: unsupported packet in");
Zhang Chenaa3a7032017-07-04 14:53:52 +08001154 compare_chr_send(s,
1155 pri_rs->buf,
1156 pri_rs->packet_len,
Zhang Chen30685c02019-06-10 00:44:31 +08001157 pri_rs->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +08001158 false,
Zhang Chen30685c02019-06-10 00:44:31 +08001159 false);
Zhang Chen0682e152016-09-27 10:22:30 +08001160 } else {
Mao Zhongyi34632182017-10-13 14:32:08 +08001161 /* compare packet in the specified connection */
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001162 colo_compare_connection(conn, s);
Zhang Chen59509ec2016-09-27 10:22:27 +08001163 }
Zhang Chen7dce4e62016-09-27 10:22:26 +08001164}
1165
1166static void compare_sec_rs_finalize(SocketReadState *sec_rs)
1167{
Zhang Chen59509ec2016-09-27 10:22:27 +08001168 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001169 Connection *conn = NULL;
Zhang Chen59509ec2016-09-27 10:22:27 +08001170
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001171 if (packet_enqueue(s, SECONDARY_IN, &conn)) {
Zhang Chen59509ec2016-09-27 10:22:27 +08001172 trace_colo_compare_main("secondary: unsupported packet in");
Zhang Chen0682e152016-09-27 10:22:30 +08001173 } else {
Mao Zhongyi34632182017-10-13 14:32:08 +08001174 /* compare packet in the specified connection */
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001175 colo_compare_connection(conn, s);
Zhang Chen59509ec2016-09-27 10:22:27 +08001176 }
Zhang Chen7dce4e62016-09-27 10:22:26 +08001177}
1178
Zhang Chen13025fe2019-06-10 00:44:30 +08001179static void compare_notify_rs_finalize(SocketReadState *notify_rs)
1180{
Zhang Chen1d09f702019-06-10 00:44:32 +08001181 CompareState *s = container_of(notify_rs, CompareState, notify_rs);
1182
Zhang Chenf77bed12019-07-23 01:18:28 +08001183 const char msg[] = "COLO_COMPARE_GET_XEN_INIT";
Zhang Chen1d09f702019-06-10 00:44:32 +08001184 int ret;
1185
Zhang Chenf77bed12019-07-23 01:18:28 +08001186 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT",
1187 notify_rs->buf,
1188 notify_rs->packet_len)) {
Lukas Straub9c55fe92020-05-22 15:53:53 +08001189 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
Zhang Chen1d09f702019-06-10 00:44:32 +08001190 if (ret < 0) {
1191 error_report("Notify Xen COLO-frame INIT failed");
1192 }
Zhang Chenf77bed12019-07-23 01:18:28 +08001193 } else if (packet_matches_str("COLO_CHECKPOINT",
1194 notify_rs->buf,
1195 notify_rs->packet_len)) {
Zhang Chen1d09f702019-06-10 00:44:32 +08001196 /* colo-compare do checkpoint, flush pri packet and remove sec packet */
1197 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
Zhang Chenf77bed12019-07-23 01:18:28 +08001198 } else {
1199 error_report("COLO compare got unsupported instruction");
Zhang Chen1d09f702019-06-10 00:44:32 +08001200 }
Zhang Chen13025fe2019-06-10 00:44:30 +08001201}
Zhang Chen7dce4e62016-09-27 10:22:26 +08001202
1203/*
1204 * Return 0 is success.
1205 * Return 1 is failed.
1206 */
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001207static int find_and_check_chardev(Chardev **chr,
Zhang Chen7dce4e62016-09-27 10:22:26 +08001208 char *chr_name,
1209 Error **errp)
1210{
Zhang Chen7dce4e62016-09-27 10:22:26 +08001211 *chr = qemu_chr_find(chr_name);
1212 if (*chr == NULL) {
1213 error_setg(errp, "Device '%s' not found",
1214 chr_name);
1215 return 1;
1216 }
1217
Daniel P. Berrange0a733362016-10-07 13:18:34 +01001218 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
1219 error_setg(errp, "chardev \"%s\" is not reconnectable",
Zhang Chen7dce4e62016-09-27 10:22:26 +08001220 chr_name);
1221 return 1;
1222 }
Marc-André Lureaufbf3cc32016-10-22 12:52:54 +03001223
Marc-André Lureau269d25c2018-12-06 00:37:35 +04001224 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) {
1225 error_setg(errp, "chardev \"%s\" cannot switch context",
1226 chr_name);
1227 return 1;
1228 }
1229
Zhang Chen7dce4e62016-09-27 10:22:26 +08001230 return 0;
1231}
1232
1233/*
1234 * Called from the main thread on the primary
1235 * to setup colo-compare.
1236 */
1237static void colo_compare_complete(UserCreatable *uc, Error **errp)
1238{
1239 CompareState *s = COLO_COMPARE(uc);
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001240 Chardev *chr;
Zhang Chen7dce4e62016-09-27 10:22:26 +08001241
Wang Yongdd321ec2017-08-29 15:22:38 +08001242 if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001243 error_setg(errp, "colo compare needs 'primary_in' ,"
Wang Yongdd321ec2017-08-29 15:22:38 +08001244 "'secondary_in','outdev','iothread' property set");
Zhang Chen7dce4e62016-09-27 10:22:26 +08001245 return;
1246 } else if (!strcmp(s->pri_indev, s->outdev) ||
1247 !strcmp(s->sec_indev, s->outdev) ||
1248 !strcmp(s->pri_indev, s->sec_indev)) {
1249 error_setg(errp, "'indev' and 'outdev' could not be same "
1250 "for compare module");
1251 return;
1252 }
1253
Zhang Chen9cc43c92020-03-18 16:23:19 +08001254 if (!s->compare_timeout) {
1255 /* Set default value to 3000 MS */
1256 s->compare_timeout = DEFAULT_TIME_OUT_MS;
1257 }
1258
Zhang Chencca35ac2020-03-18 16:23:20 +08001259 if (!s->expired_scan_cycle) {
1260 /* Set default value to 3000 MS */
1261 s->expired_scan_cycle = REGULAR_PACKET_CHECK_MS;
1262 }
1263
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001264 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
1265 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001266 return;
1267 }
1268
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001269 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
1270 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001271 return;
1272 }
1273
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001274 if (find_and_check_chardev(&chr, s->outdev, errp) ||
1275 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001276 return;
1277 }
1278
Zhang Chenaa3a7032017-07-04 14:53:52 +08001279 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
1280 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001281
Zhang Chen13025fe2019-06-10 00:44:30 +08001282 /* Try to enable remote notify chardev, currently just for Xen COLO */
1283 if (s->notify_dev) {
1284 if (find_and_check_chardev(&chr, s->notify_dev, errp) ||
1285 !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) {
1286 return;
1287 }
1288
1289 net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize,
1290 s->vnet_hdr);
1291 }
1292
Lukas Straub9c55fe92020-05-22 15:53:53 +08001293 s->out_sendco.s = s;
1294 s->out_sendco.chr = &s->chr_out;
1295 s->out_sendco.notify_remote_frame = false;
1296 s->out_sendco.done = true;
1297 g_queue_init(&s->out_sendco.send_list);
1298
1299 if (s->notify_dev) {
1300 s->notify_sendco.s = s;
1301 s->notify_sendco.chr = &s->chr_notify_dev;
1302 s->notify_sendco.notify_remote_frame = true;
1303 s->notify_sendco.done = true;
1304 g_queue_init(&s->notify_sendco.send_list);
1305 }
1306
Zhang Chenb6540d42016-09-27 10:22:29 +08001307 g_queue_init(&s->conn_list);
1308
1309 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
1310 connection_key_equal,
1311 g_free,
1312 connection_destroy);
Zhang Chen59509ec2016-09-27 10:22:27 +08001313
Wang Yongdd321ec2017-08-29 15:22:38 +08001314 colo_compare_iothread(s);
Lukas Straub5bd57eb2020-05-22 15:53:56 +08001315
1316 qemu_mutex_lock(&colo_compare_mutex);
1317 if (!colo_compare_active) {
1318 qemu_mutex_init(&event_mtx);
1319 qemu_cond_init(&event_complete_cond);
1320 colo_compare_active = true;
1321 }
1322 QTAILQ_INSERT_TAIL(&net_compares, s, next);
1323 qemu_mutex_unlock(&colo_compare_mutex);
1324
Zhang Chen7dce4e62016-09-27 10:22:26 +08001325 return;
1326}
1327
zhanghailiangdfd917a2017-02-17 10:53:12 +08001328static void colo_flush_packets(void *opaque, void *user_data)
1329{
1330 CompareState *s = user_data;
1331 Connection *conn = opaque;
1332 Packet *pkt = NULL;
1333
1334 while (!g_queue_is_empty(&conn->primary_list)) {
1335 pkt = g_queue_pop_head(&conn->primary_list);
Zhang Chenaa3a7032017-07-04 14:53:52 +08001336 compare_chr_send(s,
1337 pkt->data,
1338 pkt->size,
Zhang Chen30685c02019-06-10 00:44:31 +08001339 pkt->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +08001340 false,
1341 true);
1342 packet_destroy_partial(pkt, NULL);
zhanghailiangdfd917a2017-02-17 10:53:12 +08001343 }
1344 while (!g_queue_is_empty(&conn->secondary_list)) {
1345 pkt = g_queue_pop_head(&conn->secondary_list);
1346 packet_destroy(pkt, NULL);
1347 }
1348}
1349
Zhang Chen7dce4e62016-09-27 10:22:26 +08001350static void colo_compare_class_init(ObjectClass *oc, void *data)
1351{
1352 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
1353
1354 ucc->complete = colo_compare_complete;
1355}
1356
1357static void colo_compare_init(Object *obj)
1358{
Zhang Chenaa3a7032017-07-04 14:53:52 +08001359 CompareState *s = COLO_COMPARE(obj);
1360
Zhang Chen7dce4e62016-09-27 10:22:26 +08001361 object_property_add_str(obj, "primary_in",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001362 compare_get_pri_indev, compare_set_pri_indev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001363 object_property_add_str(obj, "secondary_in",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001364 compare_get_sec_indev, compare_set_sec_indev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001365 object_property_add_str(obj, "outdev",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001366 compare_get_outdev, compare_set_outdev);
Wang Yongdd321ec2017-08-29 15:22:38 +08001367 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
1368 (Object **)&s->iothread,
1369 object_property_allow_set_link,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001370 OBJ_PROP_LINK_STRONG);
Zhang Chencf6af762019-06-10 00:44:29 +08001371 /* This parameter just for Xen COLO */
1372 object_property_add_str(obj, "notify_dev",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001373 compare_get_notify_dev, compare_set_notify_dev);
Zhang Chenaa3a7032017-07-04 14:53:52 +08001374
Zhang Chen9cc43c92020-03-18 16:23:19 +08001375 object_property_add(obj, "compare_timeout", "uint32",
1376 compare_get_timeout,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001377 compare_set_timeout, NULL, NULL);
Zhang Chen9cc43c92020-03-18 16:23:19 +08001378
Zhang Chencca35ac2020-03-18 16:23:20 +08001379 object_property_add(obj, "expired_scan_cycle", "uint32",
1380 compare_get_expired_scan_cycle,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001381 compare_set_expired_scan_cycle, NULL, NULL);
Zhang Chencca35ac2020-03-18 16:23:20 +08001382
Zhang Chenaa3a7032017-07-04 14:53:52 +08001383 s->vnet_hdr = false;
1384 object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001385 compare_set_vnet_hdr);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001386}
1387
1388static void colo_compare_finalize(Object *obj)
1389{
1390 CompareState *s = COLO_COMPARE(obj);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001391 CompareState *tmp = NULL;
Zhang Chen7dce4e62016-09-27 10:22:26 +08001392
Lukas Straub45942b72020-05-22 15:53:55 +08001393 qemu_mutex_lock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001394 QTAILQ_FOREACH(tmp, &net_compares, next) {
1395 if (tmp == s) {
1396 QTAILQ_REMOVE(&net_compares, s, next);
1397 break;
1398 }
1399 }
Lukas Straub45942b72020-05-22 15:53:55 +08001400 if (QTAILQ_EMPTY(&net_compares)) {
1401 colo_compare_active = false;
1402 qemu_mutex_destroy(&event_mtx);
1403 qemu_cond_destroy(&event_complete_cond);
1404 }
1405 qemu_mutex_unlock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001406
Lukas Straub5bd57eb2020-05-22 15:53:56 +08001407 qemu_chr_fe_deinit(&s->chr_pri_in, false);
1408 qemu_chr_fe_deinit(&s->chr_sec_in, false);
1409 qemu_chr_fe_deinit(&s->chr_out, false);
1410 if (s->notify_dev) {
1411 qemu_chr_fe_deinit(&s->chr_notify_dev, false);
1412 }
1413
1414 if (s->iothread) {
1415 colo_compare_timer_del(s);
1416 }
1417
1418 qemu_bh_delete(s->event_bh);
1419
Lukas Straub9c55fe92020-05-22 15:53:53 +08001420 AioContext *ctx = iothread_get_aio_context(s->iothread);
1421 aio_context_acquire(ctx);
1422 AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
1423 if (s->notify_dev) {
1424 AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
1425 }
1426 aio_context_release(ctx);
1427
zhanghailiangdfd917a2017-02-17 10:53:12 +08001428 /* Release all unhandled packets after compare thead exited */
1429 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
Lukas Straub9c55fe92020-05-22 15:53:53 +08001430 AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
zhanghailiangdfd917a2017-02-17 10:53:12 +08001431
Zhang Chen727c2d72017-02-22 13:16:06 +08001432 g_queue_clear(&s->conn_list);
Lukas Straub9c55fe92020-05-22 15:53:53 +08001433 g_queue_clear(&s->out_sendco.send_list);
1434 if (s->notify_dev) {
1435 g_queue_clear(&s->notify_sendco.send_list);
1436 }
Zhang Chenb6540d42016-09-27 10:22:29 +08001437
Wang Yongdd321ec2017-08-29 15:22:38 +08001438 if (s->connection_track_table) {
1439 g_hash_table_destroy(s->connection_track_table);
1440 }
1441
1442 if (s->iothread) {
1443 object_unref(OBJECT(s->iothread));
1444 }
Zhang Chen0ffcece2018-09-03 12:38:43 +08001445
Zhang Chen7dce4e62016-09-27 10:22:26 +08001446 g_free(s->pri_indev);
1447 g_free(s->sec_indev);
1448 g_free(s->outdev);
Zhang Chencf6af762019-06-10 00:44:29 +08001449 g_free(s->notify_dev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001450}
1451
Lukas Straub45942b72020-05-22 15:53:55 +08001452static void __attribute__((__constructor__)) colo_compare_init_globals(void)
1453{
1454 colo_compare_active = false;
1455 qemu_mutex_init(&colo_compare_mutex);
1456}
1457
Zhang Chen7dce4e62016-09-27 10:22:26 +08001458static const TypeInfo colo_compare_info = {
1459 .name = TYPE_COLO_COMPARE,
1460 .parent = TYPE_OBJECT,
1461 .instance_size = sizeof(CompareState),
1462 .instance_init = colo_compare_init,
1463 .instance_finalize = colo_compare_finalize,
1464 .class_size = sizeof(CompareClass),
1465 .class_init = colo_compare_class_init,
1466 .interfaces = (InterfaceInfo[]) {
1467 { TYPE_USER_CREATABLE },
1468 { }
1469 }
1470};
1471
1472static void register_types(void)
1473{
1474 type_register_static(&colo_compare_info);
1475}
1476
1477type_init(register_types);